fastmath.h

Go to the documentation of this file.
00001 #ifndef _BML_FAST_MATH_H_
00002 #define _BML_FAST_MATH_H_
00003 
00012 /* 32-bit floats and ints only ! */
00014 #define BIAS_HEX ( ((23+127)<<23) + (1<<22) )
00015 
00016 #define BIAS_INT ((long int)BIAS_HEX)
00017 
00018 #define BIAS_FLOAT ((float)12582912.0f)
00019 
00021 union _intfloat_conv {
00022         long int i;     
00023         float f;        
00024 };
00025 
00027 typedef union _intfloat_conv _IFC;
00028 
00030 static inline long int f2i(float f) {
00031         _IFC c;
00032         c.f=f+BIAS_FLOAT;
00033         return c.i-BIAS_INT;
00034 }
00035 
00037 static inline float i2f(long int i) {
00038         _IFC c;
00039         c.i=i+BIAS_INT;
00040         return c.f-BIAS_FLOAT;
00041 }
00042 
00043 
00045 #define fast_apply_bin_int_func(_ta,_a,_tb,_b,_op,_ret) do {\
00046                 _IFC _R;\
00047                 switch((((word_t)_ta)<<1)|((word_t)_tb)) {\
00048                 case 0:\
00049                         _R.i = _op((_a), (_b));\
00050                         break;\
00051                 case 1:\
00052                         _R.i = _op((_a), f2i(_b));\
00053                         break;\
00054                 case 2:\
00055                         _R.i = _op(f2i(_a), (_b));\
00056                         break;\
00057                 case 3:\
00058                         _R.i = _op(f2i(_a), f2i(_b));\
00059                 };\
00060                 _ret=_R.i;\
00061         } while(0)
00062 
00063 
00064 #define fast_apply_bin_func(_ta,_a,_tb,_b,_opi,_opf,_ret,_ret_typ)      do {\
00065                 _IFC _R,_X,_Y;\
00066                 switch((((word_t)_ta)<<1)|((word_t)_tb)) {\
00067                 case 0:\
00068                         _R.i = _opi((_a), (_b));\
00069                         break;\
00070                 case 1:\
00071                         _X.i = (_b);\
00072                         _R.f = _opf(i2f(_a), _X.f);\
00073                         break;\
00074                 case 2:\
00075                         _X.i = (_a);\
00076                         _R.f = _opf(_X.f, i2f(_b));\
00077                         break;\
00078                 case 3:\
00079                         _X.i = (_a);\
00080                         _Y.i = (_b);\
00081                         _R.f = _opf(_X.f, _Y.f);\
00082                 };\
00083                 _ret_typ=(_ta|_tb);\
00084                 _ret=_R.i;\
00085         } while(0)
00086 
00089 
00090 
00091 #endif
00092 

Generated on Wed Feb 6 14:46:04 2008 for TinyaML by  doxygen 1.5.3