Mercurial > libavutil.hg
annotate intfloat_readwrite.c @ 980:f08c66310125 libavutil
ARM: intmath.h cosmetics
author | mru |
---|---|
date | Wed, 07 Jul 2010 17:27:48 +0000 |
parents | 0795a743bda1 |
children |
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 | 22 |
0
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
23 /** |
899
0795a743bda1
Remove explicit filename from Doxygen @file commands.
diego
parents:
873
diff
changeset
|
24 * @file |
633 | 25 * portable IEEE float/double read/write functions |
0
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
26 */ |
12 | 27 |
873
4d9ad0ed07d0
Replace many includes of libavutil/common.h with what is actually needed
mru
parents:
642
diff
changeset
|
28 #include <stdint.h> |
4d9ad0ed07d0
Replace many includes of libavutil/common.h with what is actually needed
mru
parents:
642
diff
changeset
|
29 #include <math.h> |
23 | 30 #include "intfloat_readwrite.h" |
0
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
31 |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
32 double av_int2dbl(int64_t v){ |
92 | 33 if(v+v > 0xFFEULL<<52) |
0
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
34 return 0.0/0.0; |
2 | 35 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
|
36 } |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
37 |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
38 float av_int2flt(int32_t v){ |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
39 if(v+v > 0xFF000000U) |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
40 return 0.0/0.0; |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
41 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
|
42 } |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
43 |
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
|
44 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
|
45 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
|
46 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
|
47 |
2b3573111ff0
AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents:
23
diff
changeset
|
48 for (i = 0; i < 8; i++) |
54 | 49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 * mantissa bit is written as opposed to the |
633 | 55 * single and double precision formats. */ |
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
|
56 if (ext.exponent[0]&0x80) |
54 | 57 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
|
58 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
|
59 } |
2b3573111ff0
AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents:
23
diff
changeset
|
60 |
0
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
61 int64_t av_dbl2int(double d){ |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
62 int e; |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
63 if ( !d) return 0; |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
64 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
|
65 d= frexp(d, &e); |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
66 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
|
67 } |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
68 |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
69 int32_t av_flt2int(float d){ |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
70 int e; |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
71 if ( !d) return 0; |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
72 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
|
73 d= frexp(d, &e); |
ee8f44bb7c4d
libavutil: Utility code from libavcodec moved to a separate library.
al
parents:
diff
changeset
|
74 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
|
75 } |
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
|
76 |
2b3573111ff0
AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents:
23
diff
changeset
|
77 AVExtFloat av_dbl2ext(double d){ |
54 | 78 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
|
79 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
|
80 |
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 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 ext.mantissa[i] = m>>(56-(i<<3)); |
54 | 89 } 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
|
90 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
|
91 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
|
92 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
|
93 } |
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 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
|
95 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
|
96 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
|
97 } |
2b3573111ff0
AIFF format support by (Patrick Guimond <patg a.t patg d.o.t homeunix d.o.t org)
michael
parents:
23
diff
changeset
|
98 |