fix arm asm compilation in mpegaudiodec
[libav.git] / libavutil / softfloat.h
1 //copyright Michael Niedermayer 2006 license:LGPL
2
3 #define MIN_EXP -126
4 #define MAX_EXP 126
5 #define ONE_BITS 29
6
7 typedef struct SoftFloat{
8 int32_t exp;
9 int32_t mant;
10 }SoftFloat;
11
12 static SoftFloat av_normalize_sf(SoftFloat a){
13 if(a.mant){
14 #if 1
15 while((a.mant + 0x20000000U)<0x40000000U){
16 a.mant += a.mant;
17 a.exp -= 1;
18 }
19 #else
20 int s=ONE_BITS + 1 - av_log2(a.mant ^ (a.mant<<1));
21 a.exp -= s;
22 a.mant <<= s;
23 #endif
24 if(a.exp < MIN_EXP){
25 a.exp = MIN_EXP;
26 a.mant= 0;
27 }
28 }else{
29 a.exp= MIN_EXP;
30 }
31 return a;
32 }
33
34 static inline SoftFloat av_normalize1_sf(SoftFloat a){
35 #if 1
36 if(a.mant + 0x40000000 < 0){
37 a.exp++;
38 a.mant>>=1;
39 }
40 return a;
41 #elif 1
42 int t= a.mant + 0x40000000 < 0;
43 return (SoftFloat){a.exp+t, a.mant>>t};
44 #else
45 int t= (a.mant + 0x40000000U)>>31;
46 return (SoftFloat){a.exp+t, a.mant>>t};
47 #endif
48 }
49
50 /**
51 *
52 * @return will not be more denormalized then a+b, so if either input is
53 * normalized then the output wont be worse then the other input
54 * if both are normalized then the output will be normalized
55 */
56 static inline SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){
57 a.exp += b.exp;
58 a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS;
59 return av_normalize1_sf(a);
60 }
61
62 /**
63 *
64 * b has to be normalized and not zero
65 * @return will not be more denormalized then a
66 */
67 static SoftFloat av_div_sf(SoftFloat a, SoftFloat b){
68 a.exp -= b.exp+1;
69 a.mant = ((int64_t)a.mant<<(ONE_BITS+1)) / b.mant;
70 return av_normalize1_sf(a);
71 }
72
73 static inline int av_cmp_sf(SoftFloat a, SoftFloat b){
74 int t= a.exp - b.exp;
75 if(t<0) return (a.mant >> (-t)) - b.mant ;
76 else return a.mant - (b.mant >> t);
77 }
78
79 static inline SoftFloat av_add_sf(SoftFloat a, SoftFloat b){
80 int t= a.exp - b.exp;
81 if(t<0) return av_normalize1_sf((SoftFloat){b.exp, b.mant + (a.mant >> (-t))});
82 else return av_normalize1_sf((SoftFloat){a.exp, a.mant + (b.mant >> t )});
83 }
84
85 static inline SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){
86 return av_add_sf(a, (SoftFloat){b.exp, -b.mant});
87 }
88
89 //FIXME sqrt, log, exp, pow, sin, cos
90
91 static inline SoftFloat av_int2sf(int v, int frac_bits){
92 return av_normalize_sf((SoftFloat){ONE_BITS-frac_bits, v});
93 }
94
95 /**
96 *
97 * rounding is to -inf
98 */
99 static inline int av_sf2int(SoftFloat v, int frac_bits){
100 v.exp += frac_bits - ONE_BITS;
101 if(v.exp >= 0) return v.mant << v.exp ;
102 else return v.mant >>(-v.exp);
103 }