annotate intfloat_readwrite.c @ 577:2fe796dc4c68 libavutil

Bump minor version after addition of FF_ARRAY_ELEMS macro.
author benoit
date Wed, 15 Oct 2008 08:06:12 +0000
parents d76a36742464
children 8c48a1b999a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
1 /*
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
2 * portable IEEE float/double read/write functions
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
3 *
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
4 * Copyright (c) 2005 Michael Niedermayer <michaelni@gmx.at>
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
5 *
116
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 92
diff changeset
6 * This file is part of FFmpeg.
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 92
diff changeset
7 *
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 92
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
116
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 92
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
12 *
116
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 92
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
16 * Lesser General Public License for more details.
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
17 *
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
116
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 92
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
15
af59e84e283d Update licensing information: The FSF changed postal address.
diego
parents: 12
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
21 */
12
ce8f9f4390c3 COSMETICS: Remove all trailing whitespace.
diego
parents: 2
diff changeset
22
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
23 /**
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
24 * @file intfloat_readwrite.c
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
25 * Portable IEEE float/double read/write functions.
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
26 */
12
ce8f9f4390c3 COSMETICS: Remove all trailing whitespace.
diego
parents: 2
diff changeset
27
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
28 #include "common.h"
23
f70fdc881a24 add missing #includes
mru
parents: 15
diff changeset
29 #include "intfloat_readwrite.h"
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
30
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
31 double av_int2dbl(int64_t v){
92
79e95d74ce85 use C99 standard constant, thanks to Foxy Shadis
bcoudurier
parents: 54
diff changeset
32 if(v+v > 0xFFEULL<<52)
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
33 return 0.0/0.0;
2
89cdb3b541e2 kill a bunch of compiler warnings
mru
parents: 0
diff changeset
34 return ldexp(((v&((1LL<<52)-1)) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075);
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
35 }
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
36
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
37 float av_int2flt(int32_t v){
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
38 if(v+v > 0xFF000000U)
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
39 return 0.0/0.0;
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
40 return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150);
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
41 }
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
42
24
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
43 double av_ext2dbl(const AVExtFloat ext){
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
44 uint64_t m = 0;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
45 int e, i;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
46
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
47 for (i = 0; i < 8; i++)
54
4d0fe18e5ce3 simplify
michael
parents: 24
diff changeset
48 m = (m<<8) + ext.mantissa[i];
24
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
49 e = (((int)ext.exponent[0]&0x7f)<<8) | ext.exponent[1];
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
50 if (e == 0x7fff && m)
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
51 return 0.0/0.0;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
52 e -= 16383 + 63; /* In IEEE 80 bits, the whole (i.e. 1.xxxx)
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
53 * mantissa bit is written as opposed to the
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
54 * single and double precision formats */
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
55 if (ext.exponent[0]&0x80)
54
4d0fe18e5ce3 simplify
michael
parents: 24
diff changeset
56 m= -m;
24
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
57 return ldexp(m, e);
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
58 }
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
59
0
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
60 int64_t av_dbl2int(double d){
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
61 int e;
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
62 if ( !d) return 0;
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
63 else if(d-d) return 0x7FF0000000000000LL + ((int64_t)(d<0)<<63) + (d!=d);
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
64 d= frexp(d, &e);
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
65 return (int64_t)(d<0)<<63 | (e+1022LL)<<52 | (int64_t)((fabs(d)-0.5)*(1LL<<53));
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
66 }
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
67
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
68 int32_t av_flt2int(float d){
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
69 int e;
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
70 if ( !d) return 0;
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
71 else if(d-d) return 0x7F800000 + ((d<0)<<31) + (d!=d);
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
72 d= frexp(d, &e);
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
73 return (d<0)<<31 | (e+126)<<23 | (int64_t)((fabs(d)-0.5)*(1<<24));
ee8f44bb7c4d libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff changeset
74 }
24
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
75
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
76 AVExtFloat av_dbl2ext(double d){
54
4d0fe18e5ce3 simplify
michael
parents: 24
diff changeset
77 struct AVExtFloat ext= {{0}};
24
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
78 int e, i; double f; uint64_t m;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
79
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
80 f = fabs(frexp(d, &e));
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
81 if (f >= 0.5 && f < 1) {
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
82 e += 16382;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
83 ext.exponent[0] = e>>8;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
84 ext.exponent[1] = e;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
85 m = (uint64_t)ldexp(f, 64);
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
86 for (i=0; i < 8; i++)
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
87 ext.mantissa[i] = m>>(56-(i<<3));
54
4d0fe18e5ce3 simplify
michael
parents: 24
diff changeset
88 } else if (f != 0.0) {
24
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
89 ext.exponent[0] = 0x7f; ext.exponent[1] = 0xff;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
90 if (f != 1/0.0)
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
91 ext.mantissa[0] = ~0;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
92 }
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
93 if (d < 0)
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
94 ext.exponent[0] |= 0x80;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
95 return ext;
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
96 }
2b3573111ff0 AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents: 23
diff changeset
97