00001 00026 #define DKUTIL_C_MATH_C 00027 #include "dkcMath.h" 00028 00029 00030 DKC_INLINE void WINAPI dkcSrand(ULONG *seed,ULONG num) 00031 { 00032 *seed = num; 00033 } 00034 00035 DKC_INLINE int WINAPI dkcRand(ULONG *seed) 00036 { 00037 (*seed) = (*seed) * 1103515245L + 12345; 00038 return (unsigned)((*seed) / 65536L) % 32768U; 00039 } 00040 00041 DKC_INLINE int WINAPI dkcRandom(ULONG *seed,ULONG Max_) 00042 { 00043 return dkcRand(seed) * (Max_ ) / SHRT_MAX; 00044 } 00045 00046 DKC_INLINE uint32 WINAPI dkcPrime79(uint32 n) 00047 { 00048 dkcmFORCE_NOT_ASSERT(n > 79); 00049 return dkcmPrime79(n); 00050 } 00051 00052 /* 00053 const static double sin360[]={ 00054 0.000000, 00055 0.017452,0.034899,0.052336,0.069756,0.087156,0.104528,0.121869,0.139173, 00056 0.156434,0.173648,0.190809,0.207912,0.224951,0.241922,0.258819,0.275637, 00057 0.292372,0.309017,0.325568,0.342020,0.358368,0.374607,0.390731,0.406737, 00058 0.422618,0.438371,0.453990,0.469472,0.484810,0.500000,0.515038,0.529919, 00059 0.544639,0.559193,0.573576,0.587785,0.601815,0.615661,0.629320,0.642788, 00060 0.656059,0.669131,0.681998,0.694658,0.707107,0.719340,0.731354,0.743145, 00061 0.754710,0.766044,0.777146,0.788011,0.798636,0.809017,0.819152,0.829038, 00062 0.838671,0.848048,0.857167,0.866025,0.874620,0.882948,0.891007,0.898794, 00063 0.906308,0.913545,0.920505,0.927184,0.933580,0.939693,0.945519,0.951057, 00064 0.956305,0.961262,0.965926,0.970296,0.974370,0.978148,0.981627,0.984808, 00065 0.987688,0.990268,0.992546,0.994522,0.996195,0.997564,0.998630,0.999391, 00066 0.999848,1.000000,0.999848,0.999391,0.998630,0.997564,0.996195,0.994522, 00067 0.992546,0.990268,0.987688,0.984808,0.981627,0.978148,0.974370,0.970296, 00068 0.965926,0.961262,0.956305,0.951057,0.945519,0.939693,0.933580,0.927184, 00069 0.920505,0.913545,0.906308,0.898794,0.891007,0.882948,0.874620,0.866025, 00070 0.857167,0.848048,0.838671,0.829038,0.819152,0.809017,0.798636,0.788011, 00071 0.777146,0.766044,0.754710,0.743145,0.731354,0.719340,0.707107,0.694658, 00072 0.681998,0.669131,0.656059,0.642788,0.629320,0.615661,0.601815,0.587785, 00073 0.573576,0.559193,0.544639,0.529919,0.515038,0.500000,0.484810,0.469472, 00074 0.453990,0.438371,0.422618,0.406737,0.390731,0.374607,0.358368,0.342020, 00075 0.325568,0.309017,0.292372,0.275637,0.258819,0.241922,0.224951,0.207912, 00076 0.190809,0.173648,0.156434,0.139173,0.121869,0.104528,0.087156,0.069756, 00077 0.052336,0.034899,0.017452,0.000000,-0.017452,-0.034899,-0.052336,-0.069756, 00078 -0.087156,-0.104528,-0.121869,-0.139173,-0.156434,-0.173648,-0.190809,-0.207912, 00079 -0.224951,-0.241922,-0.258819,-0.275637,-0.292372,-0.309017,-0.325568,-0.342020, 00080 -0.358368,-0.374607,-0.390731,-0.406737,-0.422618,-0.438371,-0.453990,-0.469472, 00081 -0.484810,-0.500000,-0.515038,-0.529919,-0.544639,-0.559193,-0.573576,-0.587785, 00082 -0.601815,-0.615661,-0.629320,-0.642788,-0.656059,-0.669131,-0.681998,-0.694658, 00083 -0.707107,-0.719340,-0.731354,-0.743145,-0.754710,-0.766044,-0.777146,-0.788011, 00084 -0.798636,-0.809017,-0.819152,-0.829038,-0.838671,-0.848048,-0.857167,-0.866025, 00085 -0.874620,-0.882948,-0.891007,-0.898794,-0.906308,-0.913545,-0.920505,-0.927184, 00086 -0.933580,-0.939693,-0.945519,-0.951057,-0.956305,-0.961262,-0.965926,-0.970296, 00087 -0.974370,-0.978148,-0.981627,-0.984808,-0.987688,-0.990268,-0.992546,-0.994522, 00088 -0.996195,-0.997564,-0.998630,-0.999391,-0.999848,-1.000000,-0.999848,-0.999391, 00089 -0.998630,-0.997564,-0.996195,-0.994522,-0.992546,-0.990268,-0.987688,-0.984808, 00090 -0.981627,-0.978148,-0.974370,-0.970296,-0.965926,-0.961262,-0.956305,-0.951057, 00091 -0.945519,-0.939693,-0.933580,-0.927184,-0.920505,-0.913545,-0.906308,-0.898794, 00092 -0.891007,-0.882948,-0.874620,-0.866025,-0.857167,-0.848048,-0.838671,-0.829038, 00093 -0.819152,-0.809017,-0.798636,-0.788011,-0.777146,-0.766044,-0.754710,-0.743145, 00094 -0.731354,-0.719340,-0.707107,-0.694658,-0.681998,-0.669131,-0.656059,-0.642788, 00095 -0.629320,-0.615661,-0.601815,-0.587785,-0.573576,-0.559193,-0.544639,-0.529919, 00096 -0.515038,-0.500000,-0.484810,-0.469472,-0.453990,-0.438371,-0.422618,-0.406737, 00097 -0.390731,-0.374607,-0.358368,-0.342020,-0.325568,-0.309017,-0.292372,-0.275637, 00098 -0.258819,-0.241922,-0.224951,-0.207912,-0.190809,-0.173648,-0.156434,-0.139173, 00099 -0.121869,-0.104528,-0.087156,-0.069756,-0.052336,-0.034899,-0.017452,};//end of sin360 00100 00101 00102 00103 DKC_INLINE int dkcRadianToDegree(double rad){ 00104 00105 } 00106 00107 00108 DKC_INLINE double dkcSin256(double f){ 00109 return sin256[dkcRadianToDegree(f)]; 00110 } 00111 00112 DKC_INLINE *dkc 00113 00114 */ 00115 00116 #if 0 00117 //以下fastmath.cppより引用 ライセンスはオリジナルに準拠する 00118 00119 00120 00121 #define FP_BITS(fp) (*(DWORD *)&(fp)) 00122 #define FP_ABS_BITS(fp) (FP_BITS(fp)&0x7FFFFFFF) 00123 #define FP_SIGN_BIT(fp) (FP_BITS(fp)&0x80000000) 00124 #define FP_ONE_BITS 0x3F800000 00125 00126 00127 // r = 1/p 00128 #define FP_INV(r,p) \ 00129 { \ 00130 int _i = 2 * FP_ONE_BITS - *(int *)&(p); \ 00131 r = *(float *)&_i; \ 00132 r = r * (2.0f - (p) * r); \ 00133 } 00134 00136 // The following comes from Vincent Van Eeckhout 00137 // Thanks for sending us the code! 00138 // It's the same thing in assembly but without this C-needed line: 00139 // r = *(float *)&_i; 00140 00141 static float two = 2.0f; 00142 00143 #define FP_INV2(r,p) \ 00144 { \ 00145 __asm { mov eax,0x7F000000 }; \ 00146 __asm { sub eax,dword ptr [p] }; \ 00147 __asm { mov dword ptr [r],eax }; \ 00148 __asm { fld dword ptr [p] }; \ 00149 __asm { fmul dword ptr [r] }; \ 00150 __asm { fsubr [two] }; \ 00151 __asm { fmul dword ptr [r] }; \ 00152 __asm { fstp dword ptr [r] }; \ 00153 } 00154 00156 00157 00158 #define FP_EXP(e,p) \ 00159 { \ 00160 int _i; \ 00161 e = -1.44269504f * (float)0x00800000 * (p); \ 00162 _i = (int)e + 0x3F800000; \ 00163 e = *(float *)&_i; \ 00164 } 00165 00166 #define FP_NORM_TO_BYTE(i,p) \ 00167 { \ 00168 float _n = (p) + 1.0f; \ 00169 i = *(int *)&_n; \ 00170 if (i >= 0x40000000) i = 0xFF; \ 00171 else if (i <=0x3F800000) i = 0; \ 00172 else i = ((i) >> 15) & 0xFF; \ 00173 } 00174 00175 00176 00177 DKC_INLINE unsigned long FP_NORM_TO_BYTE2(float p) 00178 { 00179 float fpTmp = p + 1.0f; 00180 return ((*(unsigned *)&fpTmp) >> 15) & 0xFF; 00181 } 00182 00183 00184 DKC_INLINE unsigned long FP_NORM_TO_BYTE3(float p) 00185 { 00186 float ftmp = p + 12582912.0f; 00187 return ((*(unsigned long *)&ftmp) & 0xFF); 00188 } 00189 00190 static unsigned int fast_sqrt_table[0x10000]; // declare table of square roots 00191 00192 00193 static void build_sqrt_table() 00194 { 00195 unsigned int i; 00196 FastSqrtUnion s; 00197 00198 for (i = 0; i <= 0x7FFF; i++) 00199 { 00200 00201 // Build a float with the bit pattern i as mantissa 00202 // and an exponent of 0, stored as 127 00203 00204 s.i = (i << 8) | (0x7F << 23); 00205 s.f = (float)sqrt(s.f); 00206 00207 // Take the square root then strip the first 7 bits of 00208 // the mantissa into the table 00209 00210 fast_sqrt_table[i + 0x8000] = (s.i & 0x7FFFFF); 00211 00212 // Repeat the process, this time with an exponent of 1, 00213 // stored as 128 00214 00215 s.i = (i << 8) | (0x80 << 23); 00216 s.f = (float)sqrt(s.f); 00217 00218 fast_sqrt_table[i] = (s.i & 0x7FFFFF); 00219 } 00220 } 00221 00222 00223 typedef union FastSqrtUnion 00224 { 00225 float f; 00226 unsigned int i; 00227 } FastSqrtUnion; 00228 00229 00230 static DKC_INLINE float fastsqrt(float n) 00231 { 00232 00233 if (FP_BITS(n) == 0) 00234 return 0.0; // check for square root of 0 00235 00236 FP_BITS(n) = fast_sqrt_table[(FP_BITS(n) >> 8) & 0xFFFF] | ((((FP_BITS(n) - 0x3F800000) >> 1) + 0x3F800000) & 0x7F800000); 00237 00238 return n; 00239 } 00240 00241 00242 // At the assembly level the recommended workaround for the second FIST bug is the same for the first; 00243 // inserting the FRNDINT instruction immediately preceding the FIST instruction. 00244 00245 static DKC_FORCE_INLINE void FloatToInt(int *int_pointer, float f) 00246 { 00247 __asm fld f 00248 __asm mov edx,int_pointer 00249 __asm FRNDINT 00250 __asm fistp dword ptr [edx]; 00251 00252 } 00253 00254 00255 //こういうのはしたくないのだが・・・ 00256 static BYTE f = FALSE; 00257 00258 DKC_FORCE_INLINE void WINAPI dkcSqrtInit(){ 00259 if(FALSE==f){ 00260 build_sqrt_table(); 00261 f=TRUE; 00262 } 00263 } 00264 00265 DKC_FORCE_INLINE float WINAPI dkcSqrtFast(float a){ 00266 dkcmNOT_ASSERT(FALSE==f); 00267 return fastsqrt(a); 00268 } 00269 00270 DKC_FORCE_INLINE int WINAPI dkcFloatToInt(float a){ 00271 00272 } 00273 #endif 00274 00275 //**********************************************************