1 //copyright Michael Niedermayer 2006 license:LGPL
7 typedef struct SoftFloat{
12 static SoftFloat av_normalize_sf(SoftFloat a){
15 while((a.mant + 0x20000000U)<0x40000000U){
20 int s=ONE_BITS + 1 - av_log2(a.mant ^ (a.mant<<1));
34 static inline SoftFloat av_normalize1_sf(SoftFloat a){
36 if(a.mant + 0x40000000 < 0){
42 int t= a.mant + 0x40000000 < 0;
43 return (SoftFloat){a.exp+t, a.mant>>t};
45 int t= (a.mant + 0x40000000U)>>31;
46 return (SoftFloat){a.exp+t, a.mant>>t};
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
56 static inline SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){
58 a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS;
59 return av_normalize1_sf(a);
64 * b has to be normalized and not zero
65 * @return will not be more denormalized then a
67 static SoftFloat av_div_sf(SoftFloat a, SoftFloat b){
69 a.mant = ((int64_t)a.mant<<(ONE_BITS+1)) / b.mant;
70 return av_normalize1_sf(a);
73 static inline int av_cmp_sf(SoftFloat a, SoftFloat b){
75 if(t<0) return (a.mant >> (-t)) - b.mant ;
76 else return a.mant - (b.mant >> t);
79 static inline SoftFloat av_add_sf(SoftFloat a, SoftFloat b){
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 )});
85 static inline SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){
86 return av_add_sf(a, (SoftFloat){b.exp, -b.mant});
89 //FIXME sqrt, log, exp, pow, sin, cos
91 static inline SoftFloat av_int2sf(int v, int frac_bits){
92 return av_normalize_sf((SoftFloat){ONE_BITS-frac_bits, v});
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);