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