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