FFmpeg
softfloat_ieee754.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Umair Khan <omerjerk@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #ifndef AVUTIL_SOFTFLOAT_IEEE754_H
22 #define AVUTIL_SOFTFLOAT_IEEE754_H
23 
24 #include <stdint.h>
25 
26 #define EXP_BIAS 127
27 #define MANT_BITS 23
28 
29 typedef struct SoftFloat_IEEE754 {
31  uint64_t mant;
34 
35 static const SoftFloat_IEEE754 FLOAT_0 = {0, 0, -126};
36 static const SoftFloat_IEEE754 FLOAT_1 = {0, 0, 0};
37 
38 /** Normalize the softfloat as defined by IEEE 754 single-recision floating
39  * point specification
40  */
42  while( sf.mant >= 0x1000000UL ) {
43  sf.exp++;
44  sf.mant >>= 1;
45  }
46  sf.mant &= 0x007fffffUL;
47  return sf;
48 }
49 
50 /** Convert integer to softfloat.
51  * @return softfloat with value n * 2^e
52  */
53 static inline SoftFloat_IEEE754 av_int2sf_ieee754(int64_t n, int e) {
54  int sign = 0;
55 
56  if (n < 0) {
57  sign = 1;
58  n *= -1;
59  }
60  return av_normalize_sf_ieee754((SoftFloat_IEEE754) {sign, n << MANT_BITS, 0 + e});
61 }
62 
63 /** Make a softfloat out of the bitstream. Assumes the bits are in the form as defined
64  * by the IEEE 754 spec.
65  */
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)});
68 }
69 
70 /** Convert the softfloat to integer
71  */
73  if(a.exp >= 0) return a.mant << a.exp ;
74  else return a.mant >>(-a.exp);
75 }
76 
77 /** Divide a by b. b should not be zero.
78  * @return normalized result
79  */
81  int32_t mant, exp, sign;
84  sign = a.sign ^ b.sign;
85  mant = ((((uint64_t) (a.mant | 0x00800000UL)) << MANT_BITS) / (b.mant| 0x00800000UL));
86  exp = a.exp - b.exp;
87  return av_normalize_sf_ieee754((SoftFloat_IEEE754) {sign, mant, exp});
88 }
89 
90 /** Multiply a with b
91  * #return normalized result
92  */
94  int32_t sign, mant, exp;
97  sign = a.sign ^ b.sign;
98  mant = (((uint64_t)(a.mant|0x00800000UL) * (uint64_t)(b.mant|0x00800000UL))>>MANT_BITS);
99  exp = a.exp + b.exp;
100  return av_normalize_sf_ieee754((SoftFloat_IEEE754) {sign, mant, exp});
101 }
102 
103 /** Compare a with b strictly
104  * @returns 1 if the a and b are equal, 0 otherwise.
105  */
109  if (a.sign != b.sign) return 0;
110  if (a.mant != b.mant) return 0;
111  if (a.exp != b.exp ) return 0;
112  return 1;
113 }
114 
115 #endif /*AVUTIL_SOFTFLOAT_IEEE754_H*/
av_mul_sf_ieee754
static SoftFloat_IEEE754 av_mul_sf_ieee754(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b)
Multiply a with b return normalized result.
Definition: softfloat_ieee754.h:93
av_bits2sf_ieee754
static SoftFloat_IEEE754 av_bits2sf_ieee754(uint32_t n)
Make a softfloat out of the bitstream.
Definition: softfloat_ieee754.h:66
b
#define b
Definition: input.c:40
MANT_BITS
#define MANT_BITS
Definition: softfloat_ieee754.h:27
av_int2sf_ieee754
static SoftFloat_IEEE754 av_int2sf_ieee754(int64_t n, int e)
Convert integer to softfloat.
Definition: softfloat_ieee754.h:53
av_cmp_sf_ieee754
static int av_cmp_sf_ieee754(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b)
Compare a with b strictly.
Definition: softfloat_ieee754.h:106
exp
int8_t exp
Definition: eval.c:72
FLOAT_1
static const SoftFloat_IEEE754 FLOAT_1
Definition: softfloat_ieee754.h:36
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
SoftFloat_IEEE754::mant
uint64_t mant
Definition: softfloat_ieee754.h:31
FLOAT_0
static const SoftFloat_IEEE754 FLOAT_0
Definition: softfloat_ieee754.h:35
SoftFloat_IEEE754
Definition: softfloat_ieee754.h:29
av_div_sf_ieee754
static SoftFloat_IEEE754 av_div_sf_ieee754(SoftFloat_IEEE754 a, SoftFloat_IEEE754 b)
Divide a by b.
Definition: softfloat_ieee754.h:80
av_sf2int_ieee754
static int av_sf2int_ieee754(SoftFloat_IEEE754 a)
Convert the softfloat to integer.
Definition: softfloat_ieee754.h:72
int32_t
int32_t
Definition: audioconvert.c:56
SoftFloat_IEEE754::exp
int32_t exp
Definition: softfloat_ieee754.h:32
av_normalize_sf_ieee754
static SoftFloat_IEEE754 av_normalize_sf_ieee754(SoftFloat_IEEE754 sf)
Normalize the softfloat as defined by IEEE 754 single-recision floating point specification.
Definition: softfloat_ieee754.h:41
SoftFloat_IEEE754::sign
int32_t sign
Definition: softfloat_ieee754.h:30