00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef FFMPEG_SOFTFLOAT_H
00022 #define FFMPEG_SOFTFLOAT_H
00023
00024 #include <stdint.h>
00025
00026 #define MIN_EXP -126
00027 #define MAX_EXP 126
00028 #define ONE_BITS 29
00029
00030 typedef struct SoftFloat{
00031 int32_t exp;
00032 int32_t mant;
00033 }SoftFloat;
00034
00035 static SoftFloat av_normalize_sf(SoftFloat a){
00036 if(a.mant){
00037 #if 1
00038 while((a.mant + 0x20000000U)<0x40000000U){
00039 a.mant += a.mant;
00040 a.exp -= 1;
00041 }
00042 #else
00043 int s=ONE_BITS + 1 - av_log2(a.mant ^ (a.mant<<1));
00044 a.exp -= s;
00045 a.mant <<= s;
00046 #endif
00047 if(a.exp < MIN_EXP){
00048 a.exp = MIN_EXP;
00049 a.mant= 0;
00050 }
00051 }else{
00052 a.exp= MIN_EXP;
00053 }
00054 return a;
00055 }
00056
00057 static inline SoftFloat av_normalize1_sf(SoftFloat a){
00058 #if 1
00059 if(a.mant + 0x40000000 < 0){
00060 a.exp++;
00061 a.mant>>=1;
00062 }
00063 return a;
00064 #elif 1
00065 int t= a.mant + 0x40000000 < 0;
00066 return (SoftFloat){a.exp+t, a.mant>>t};
00067 #else
00068 int t= (a.mant + 0x40000000U)>>31;
00069 return (SoftFloat){a.exp+t, a.mant>>t};
00070 #endif
00071 }
00072
00079 static inline SoftFloat av_mul_sf(SoftFloat a, SoftFloat b){
00080 a.exp += b.exp;
00081 a.mant = (a.mant * (int64_t)b.mant) >> ONE_BITS;
00082 return av_normalize1_sf(a);
00083 }
00084
00090 static SoftFloat av_div_sf(SoftFloat a, SoftFloat b){
00091 a.exp -= b.exp+1;
00092 a.mant = ((int64_t)a.mant<<(ONE_BITS+1)) / b.mant;
00093 return av_normalize1_sf(a);
00094 }
00095
00096 static inline int av_cmp_sf(SoftFloat a, SoftFloat b){
00097 int t= a.exp - b.exp;
00098 if(t<0) return (a.mant >> (-t)) - b.mant ;
00099 else return a.mant - (b.mant >> t);
00100 }
00101
00102 static inline SoftFloat av_add_sf(SoftFloat a, SoftFloat b){
00103 int t= a.exp - b.exp;
00104 if(t<0) return av_normalize1_sf((SoftFloat){b.exp, b.mant + (a.mant >> (-t))});
00105 else return av_normalize1_sf((SoftFloat){a.exp, a.mant + (b.mant >> t )});
00106 }
00107
00108 static inline SoftFloat av_sub_sf(SoftFloat a, SoftFloat b){
00109 return av_add_sf(a, (SoftFloat){b.exp, -b.mant});
00110 }
00111
00112
00113
00114 static inline SoftFloat av_int2sf(int v, int frac_bits){
00115 return av_normalize_sf((SoftFloat){ONE_BITS-frac_bits, v});
00116 }
00117
00122 static inline int av_sf2int(SoftFloat v, int frac_bits){
00123 v.exp += frac_bits - ONE_BITS;
00124 if(v.exp >= 0) return v.mant << v.exp ;
00125 else return v.mant >>(-v.exp);
00126 }
00127
00128 #endif