2 * Copyright (c) 2016 Umair Khan <omerjerk@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef AVUTIL_SOFTFLOAT_IEEE754_H
22 #define AVUTIL_SOFTFLOAT_IEEE754_H
29 typedef struct SoftFloat_IEEE754
{
35 static const SoftFloat_IEEE754 FLOAT_0
= {0, 0, -126};
36 static const SoftFloat_IEEE754 FLOAT_1
= {0, 0, 0};
38 /** Normalize the softfloat as defined by IEEE 754 single-recision floating
41 static inline SoftFloat_IEEE754
av_normalize_sf_ieee754(SoftFloat_IEEE754 sf
) {
42 while( sf
.mant
>= 0x1000000UL
) {
46 sf
.mant
&= 0x007fffffUL
;
50 /** Convert integer to softfloat.
51 * @return softfloat with value n * 2^e
53 static inline SoftFloat_IEEE754
av_int2sf_ieee754(int64_t n
, int e
) {
60 return av_normalize_sf_ieee754((SoftFloat_IEEE754
) {sign
, n
<< MANT_BITS
, 0 + e
});
63 /** Make a softfloat out of the bitstream. Assumes the bits are in the form as defined
64 * by the IEEE 754 spec.
66 static inline SoftFloat_IEEE754
av_bits2sf_ieee754(uint32_t n
) {
67 return ((SoftFloat_IEEE754
) { (n
& 0x80000000UL
) >> 31, (n
& 0x7FFFFFUL
), (int8_t)((n
& 0x7F800000UL
) >> 23)});
70 /** Convert the softfloat to integer
72 static inline int av_sf2int_ieee754(SoftFloat_IEEE754 a
) {
73 if(a
.exp
>= 0) return a
.mant
<< a
.exp
;
74 else return a
.mant
>>(-a
.exp
);
77 /** Divide a by b. b should not be zero.
78 * @return normalized result
80 static inline SoftFloat_IEEE754
av_div_sf_ieee754(SoftFloat_IEEE754 a
, SoftFloat_IEEE754 b
) {
81 int32_t mant
, exp
, sign
;
82 a
= av_normalize_sf_ieee754(a
);
83 b
= av_normalize_sf_ieee754(b
);
84 sign
= a
.sign
^ b
.sign
;
85 mant
= ((((uint64_t) (a
.mant
| 0x00800000UL
)) << MANT_BITS
) / (b
.mant
| 0x00800000UL
));
87 return av_normalize_sf_ieee754((SoftFloat_IEEE754
) {sign
, mant
, exp
});
91 * #return normalized result
93 static inline SoftFloat_IEEE754
av_mul_sf_ieee754(SoftFloat_IEEE754 a
, SoftFloat_IEEE754 b
) {
94 int32_t sign
, mant
, exp
;
95 a
= av_normalize_sf_ieee754(a
);
96 b
= av_normalize_sf_ieee754(b
);
97 sign
= a
.sign
^ b
.sign
;
98 mant
= (((uint64_t)(a
.mant
|0x00800000UL
) * (uint64_t)(b
.mant
|0x00800000UL
))>>MANT_BITS
);
100 return av_normalize_sf_ieee754((SoftFloat_IEEE754
) {sign
, mant
, exp
});
103 /** Compare a with b strictly
104 * @returns 1 if the a and b are equal, 0 otherwise.
106 static inline int av_cmp_sf_ieee754(SoftFloat_IEEE754 a
, SoftFloat_IEEE754 b
) {
107 a
= av_normalize_sf_ieee754(a
);
108 b
= av_normalize_sf_ieee754(b
);
109 if (a
.sign
!= b
.sign
) return 0;
110 if (a
.mant
!= b
.mant
) return 0;
111 if (a
.exp
!= b
.exp
) return 0;
115 #endif /*AVUTIL_SOFTFLOAT_IEEE754_H*/