comparison softfloat.h @ 478:8d2c05ebf5f1 libavutil

revert r12489.
author benoit
date Tue, 18 Mar 2008 16:29:47 +0000
parents 8d25a4b50588
children f4187c1c15a6
comparison
equal deleted inserted replaced
477:8d25a4b50588 478:8d2c05ebf5f1
30 typedef struct SoftFloat{ 30 typedef struct SoftFloat{
31 int32_t exp; 31 int32_t exp;
32 int32_t mant; 32 int32_t mant;
33 }SoftFloat; 33 }SoftFloat;
34 34
35 static av_const SoftFloat av_normalize_sf(SoftFloat a){ 35 static SoftFloat av_normalize_sf(SoftFloat a){
36 if(a.mant){ 36 if(a.mant){
37 #if 1 37 #if 1
38 while((a.mant + 0x20000000U)<0x40000000U){ 38 while((a.mant + 0x20000000U)<0x40000000U){
39 a.mant += a.mant; 39 a.mant += a.mant;
40 a.exp -= 1; 40 a.exp -= 1;
52 a.exp= MIN_EXP; 52 a.exp= MIN_EXP;
53 } 53 }
54 return a; 54 return a;
55 } 55 }
56 56
57 static inline av_const SoftFloat av_normalize1_sf(SoftFloat a){ 57 static inline SoftFloat av_normalize1_sf(SoftFloat a){
58 #if 1 58 #if 1
59 if(a.mant + 0x40000000 < 0){ 59 if(a.mant + 0x40000000 < 0){
60 a.exp++; 60 a.exp++;
61 a.mant>>=1; 61 a.mant>>=1;
62 } 62 }
74 * 74 *
75 * @return will not be more denormalized then a+b, so if either input is 75 * @return will not be more denormalized then a+b, so if either input is
76 * normalized then the output wont be worse then the other input 76 * normalized then the output wont be worse then the other input
77 * if both are normalized then the output will be normalized 77 * if both are normalized then the output will be normalized
78 */ 78 */
79 static inline av_const SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){ 79 static inline SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){
80 a.exp += b.exp; 80 a.exp += b.exp;
81 a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS; 81 a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS;
82 return av_normalize1_sf(a); 82 return av_normalize1_sf(a);
83 } 83 }
84 84
85 /** 85 /**
86 * 86 *
87 * b has to be normalized and not zero 87 * b has to be normalized and not zero
88 * @return will not be more denormalized then a 88 * @return will not be more denormalized then a
89 */ 89 */
90 static av_const SoftFloat av_div_sf(SoftFloat a, SoftFloat b){ 90 static SoftFloat av_div_sf(SoftFloat a, SoftFloat b){
91 a.exp -= b.exp+1; 91 a.exp -= b.exp+1;
92 a.mant = ((int64_t)a.mant<<(ONE_BITS+1)) / b.mant; 92 a.mant = ((int64_t)a.mant<<(ONE_BITS+1)) / b.mant;
93 return av_normalize1_sf(a); 93 return av_normalize1_sf(a);
94 } 94 }
95 95
96 static inline av_const int av_cmp_sf(SoftFloat a, SoftFloat b){ 96 static inline int av_cmp_sf(SoftFloat a, SoftFloat b){
97 int t= a.exp - b.exp; 97 int t= a.exp - b.exp;
98 if(t<0) return (a.mant >> (-t)) - b.mant ; 98 if(t<0) return (a.mant >> (-t)) - b.mant ;
99 else return a.mant - (b.mant >> t); 99 else return a.mant - (b.mant >> t);
100 } 100 }
101 101
102 static inline av_const SoftFloat av_add_sf(SoftFloat a, SoftFloat b){ 102 static inline SoftFloat av_add_sf(SoftFloat a, SoftFloat b){
103 int t= a.exp - b.exp; 103 int t= a.exp - b.exp;
104 if(t<0) return av_normalize1_sf((SoftFloat){b.exp, b.mant + (a.mant >> (-t))}); 104 if(t<0) return av_normalize1_sf((SoftFloat){b.exp, b.mant + (a.mant >> (-t))});
105 else return av_normalize1_sf((SoftFloat){a.exp, a.mant + (b.mant >> t )}); 105 else return av_normalize1_sf((SoftFloat){a.exp, a.mant + (b.mant >> t )});
106 } 106 }
107 107
108 static inline av_const SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){ 108 static inline SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){
109 return av_add_sf(a, (SoftFloat){b.exp, -b.mant}); 109 return av_add_sf(a, (SoftFloat){b.exp, -b.mant});
110 } 110 }
111 111
112 //FIXME sqrt, log, exp, pow, sin, cos 112 //FIXME sqrt, log, exp, pow, sin, cos
113 113
114 static inline av_const SoftFloat av_int2sf(int v, int frac_bits){ 114 static inline SoftFloat av_int2sf(int v, int frac_bits){
115 return av_normalize_sf((SoftFloat){ONE_BITS-frac_bits, v}); 115 return av_normalize_sf((SoftFloat){ONE_BITS-frac_bits, v});
116 } 116 }
117 117
118 /** 118 /**
119 * 119 *
120 * rounding is to -inf 120 * rounding is to -inf
121 */ 121 */
122 static inline av_const int av_sf2int(SoftFloat v, int frac_bits){ 122 static inline int av_sf2int(SoftFloat v, int frac_bits){
123 v.exp += frac_bits - ONE_BITS; 123 v.exp += frac_bits - ONE_BITS;
124 if(v.exp >= 0) return v.mant << v.exp ; 124 if(v.exp >= 0) return v.mant << v.exp ;
125 else return v.mant >>(-v.exp); 125 else return v.mant >>(-v.exp);
126 } 126 }
127 127