annotate softfloat.h @ 91:04f62b676170 libavutil

floating point "emulation" code unused currently, but might come in handy for some fpu-less cpus
author michael
date Tue, 08 Aug 2006 07:58:10 +0000
parents
children 87a8edfd1434
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
91
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
1 //copyright Michael Niedermayer 2006 license:LGPL
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
2
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
3 #define MIN_EXP -126
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
4 #define MAX_EXP 126
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
5 #define ONE_BITS 29
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
6
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
7 typedef struct SoftFloat{
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
8 int32_t exp;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
9 int32_t mant;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
10 }SoftFloat;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
11
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
12 static SoftFloat av_normalize_sf(SoftFloat a){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
13 if(a.mant){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
14 #if 1
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
15 while((a.mant + 0x20000000U)<0x40000000U){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
16 a.mant += a.mant;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
17 a.exp -= 1;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
18 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
19 #else
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
20 int s=ONE_BITS + 1 - av_log2(a.mant ^ (a.mant<<1));
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
21 a.exp -= s;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
22 a.mant <<= s;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
23 #endif
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
24 if(a.exp < MIN_EXP){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
25 a.exp = MIN_EXP;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
26 a.mant= 0;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
27 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
28 }else{
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
29 a.exp= MIN_EXP;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
30 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
31 return a;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
32 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
33
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
34 static inline SoftFloat av_normalize1_sf(SoftFloat a){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
35 #if 1
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
36 if(a.mant + 0x40000000 < 0){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
37 a.exp++;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
38 a.mant>>=1;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
39 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
40 return a;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
41 #elif 1
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
42 int t= a.mant + 0x40000000 < 0;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
43 return (SoftFloat){a.exp+t, a.mant>>t};
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
44 #else
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
45 int t= (a.mant + 0x40000000U)>>31;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
46 return (SoftFloat){a.exp+t, a.mant>>t};
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
47 #endif
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
48 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
49
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
50 /**
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
51 *
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
52 * @return will not be more denormalized then a+b, so if either input is
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
53 * normalized then the output wont be worse then the other input
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
54 * if both are normalized then the output will be normalized
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
55 */
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
56 static inline SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
57 a.exp += b.exp;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
58 a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
59 return av_normalize1_sf(a);
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
60 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
61
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
62 /**
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
63 *
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
64 * b has to be normalized and not zero
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
65 * @return will not be more denormalized then a
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
66 */
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
67 static SoftFloat av_div_sf(SoftFloat a, SoftFloat b){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
68 a.exp -= b.exp+1;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
69 a.mant = ((int64_t)a.mant<<(ONE_BITS+1)) / b.mant;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
70 return av_normalize1_sf(a);
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
71 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
72
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
73 static inline int av_cmp_sf(SoftFloat a, SoftFloat b){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
74 int t= a.exp - b.exp;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
75 if(t<0) return (a.mant >> (-t)) - b.mant ;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
76 else return a.mant - (b.mant >> t);
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
77 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
78
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
79 static inline SoftFloat av_add_sf(SoftFloat a, SoftFloat b){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
80 int t= a.exp - b.exp;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
81 if(t<0) return av_normalize1_sf((SoftFloat){b.exp, b.mant + (a.mant >> (-t))});
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
82 else return av_normalize1_sf((SoftFloat){a.exp, a.mant + (b.mant >> t )});
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
83 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
84
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
85 static inline SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
86 return av_add_sf(a, (SoftFloat){b.exp, -b.mant});
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
87 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
88
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
89 //FIXME sqrt, log, exp, pow, sin, cos
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
90
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
91 static inline SoftFloat av_int2sf(int v, int frac_bits){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
92 return av_normalize_sf((SoftFloat){ONE_BITS-frac_bits, v});
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
93 }
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
94
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
95 /**
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
96 *
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
97 * rounding is to -inf
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
98 */
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
99 static inline int av_sf2int(SoftFloat v, int frac_bits){
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
100 v.exp += frac_bits - ONE_BITS;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
101 if(v.exp >= 0) return v.mant << v.exp ;
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
102 else return v.mant >>(-v.exp);
04f62b676170 floating point "emulation" code
michael
parents:
diff changeset
103 }