wwmath.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWMath *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/wwmath.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 3/01/02 9:06a $*
  29. * *
  30. * $Revision:: 65 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef WWMATH_H
  39. #define WWMATH_H
  40. #include "always.h"
  41. #include <math.h>
  42. #include <float.h>
  43. #include <assert.h>
  44. #include <float.h>
  45. /*
  46. ** Some global constants.
  47. */
  48. #define WWMATH_EPSILON 0.0001f
  49. #define WWMATH_EPSILON2 WWMATH_EPSILON * WWMATH_EPSILON
  50. #define WWMATH_PI 3.141592654f
  51. #define WWMATH_FLOAT_MAX (FLT_MAX)
  52. #define WWMATH_FLOAT_MIN (FLT_MIN)
  53. #define WWMATH_SQRT2 1.414213562f
  54. #define WWMATH_SQRT3 1.732050808f
  55. #define WWMATH_OOSQRT2 0.707106781f
  56. #define WWMATH_OOSQRT3 0.577350269f
  57. // (DRM 05/07/01) Temporarily eliminated _fastcall
  58. // on non-Microsoft compatible compilers. Jani
  59. // should be replacing this soon.
  60. #ifndef _MSC_VER
  61. #define __fastcall
  62. #endif // _MSC_VER
  63. /*
  64. ** Macros to convert between degrees and radians
  65. */
  66. #ifndef RAD_TO_DEG
  67. #define RAD_TO_DEG(x) (((double)x)*180.0/WWMATH_PI)
  68. #endif
  69. #ifndef DEG_TO_RAD
  70. #define DEG_TO_RAD(x) (((double)x)*WWMATH_PI/180.0)
  71. #endif
  72. #ifndef RAD_TO_DEGF
  73. #define RAD_TO_DEGF(x) (((float)x)*180.0f/WWMATH_PI)
  74. #endif
  75. #ifndef DEG_TO_RADF
  76. #define DEG_TO_RADF(x) (((float)x)*WWMATH_PI/180.0f)
  77. #endif
  78. const int ARC_TABLE_SIZE=1024;
  79. const int SIN_TABLE_SIZE=1024;
  80. extern float _FastAcosTable[ARC_TABLE_SIZE];
  81. extern float _FastAsinTable[ARC_TABLE_SIZE];
  82. extern float _FastSinTable[SIN_TABLE_SIZE];
  83. extern float _FastInvSinTable[SIN_TABLE_SIZE];
  84. /*
  85. ** Some simple math functions which work on the built-in types.
  86. ** Include the various other header files in the WWMATH library
  87. ** in order to get matrices, quaternions, etc.
  88. */
  89. class WWMath
  90. {
  91. public:
  92. // Initialization and Shutdown. Other math sub-systems which require initialization and
  93. // shutdown processing will be handled in these functions
  94. static void Init(void);
  95. static void Shutdown(void);
  96. // These are meant to be a collection of small math utility functions to be optimized at some point.
  97. static WWINLINE float Fabs(float val)
  98. {
  99. int value=*(int*)&val;
  100. value&=0x7fffffff;
  101. return *(float*)&value;
  102. }
  103. static WWINLINE int Float_To_Int_Chop(const float& f);
  104. static WWINLINE int Float_To_Int_Floor(const float& f);
  105. #if defined(_MSC_VER) && defined(_M_IX86)
  106. static WWINLINE float Cos(float val);
  107. static WWINLINE float Sin(float val);
  108. static WWINLINE float Sqrt(float val);
  109. static float __fastcall Inv_Sqrt(float a); // Some 30% faster inverse square root than regular C++ compiled, from Intel's math library
  110. static WWINLINE long Float_To_Long(float f);
  111. #else
  112. static float Cos(float val);
  113. static float Sin(float val);
  114. static float Sqrt(float val);
  115. static float Inv_Sqrt(float a);
  116. static long Float_To_Long(float f);
  117. #endif
  118. static WWINLINE float Fast_Sin(float val);
  119. static WWINLINE float Fast_Inv_Sin(float val);
  120. static WWINLINE float Fast_Cos(float val);
  121. static WWINLINE float Fast_Inv_Cos(float val);
  122. static WWINLINE float Fast_Acos(float val);
  123. static WWINLINE float Acos(float val);
  124. static WWINLINE float Fast_Asin(float val);
  125. static WWINLINE float Asin(float val);
  126. static float Atan(float x) { return static_cast<float>(atan(x)); }
  127. static float Atan2(float y,float x) { return static_cast<float>(atan2(y,x)); }
  128. static float Sign(float val);
  129. static float Ceil(float val) { return ceilf(val); }
  130. static float Floor(float val) { return floorf(val); }
  131. static bool Fast_Is_Float_Positive(const float & val);
  132. static float Random_Float(void);
  133. static float Random_Float(float min,float max);
  134. static float Clamp(float val, float min = 0.0f, float max = 1.0f);
  135. static double Clamp(double val, double min = 0.0f, double max = 1.0f);
  136. static int Clamp_Int(int val, int min_val, int max_val);
  137. static float Wrap(float val, float min = 0.0f, float max = 1.0f);
  138. static double Wrap(double val, double min = 0.0f, double max = 1.0f);
  139. static float Min(float a, float b);
  140. static float Max(float a, float b);
  141. static float Lerp(float a, float b, float lerp );
  142. static double Lerp(double a, double b, float lerp );
  143. static long Float_To_Long(double f);
  144. static unsigned char Unit_Float_To_Byte(float f) { return (unsigned char)(f*255.0f); }
  145. static float Byte_To_Unit_Float(unsigned char byte) { return ((float)byte) / 255.0f; }
  146. static bool Is_Valid_Float(float x);
  147. static bool Is_Valid_Double(double x);
  148. };
  149. WWINLINE float WWMath::Sign(float val)
  150. {
  151. if (val > 0.0f) {
  152. return +1.0f;
  153. }
  154. if (val < 0.0f) {
  155. return -1.0f;
  156. }
  157. return 0.0f;
  158. }
  159. WWINLINE bool WWMath::Fast_Is_Float_Positive(const float & val)
  160. {
  161. return !((*(int *)(&val)) & 0x80000000);
  162. }
  163. WWINLINE float WWMath::Random_Float(float min,float max)
  164. {
  165. return Random_Float() * (max-min) + min;
  166. }
  167. WWINLINE float WWMath::Clamp(float val, float min /*= 0.0f*/, float max /*= 1.0f*/)
  168. {
  169. if(val < min) return min;
  170. if(val > max) return max;
  171. return val;
  172. }
  173. WWINLINE double WWMath::Clamp(double val, double min /*= 0.0f*/, double max /*= 1.0f*/)
  174. {
  175. if(val < min) return min;
  176. if(val > max) return max;
  177. return val;
  178. }
  179. WWINLINE int WWMath::Clamp_Int(int val, int min_val, int max_val)
  180. {
  181. if(val < min_val) return min_val;
  182. if(val > max_val) return max_val;
  183. return val;
  184. }
  185. WWINLINE float WWMath::Wrap(float val, float min /*= 0.0f*/, float max /*= 1.0f*/)
  186. {
  187. // Implemented as an if rather than a while, to long loops
  188. if ( val >= max ) val -= (max-min);
  189. if ( val < min ) val += (max-min);
  190. if ( val < min ) {
  191. val = min;
  192. }
  193. if ( val > max ) {
  194. val = max;
  195. }
  196. return val;
  197. }
  198. WWINLINE double WWMath::Wrap(double val, double min /*= 0.0f*/, double max /*= 1.0f*/)
  199. {
  200. // Implemented as an if rather than a while, to long loops
  201. if ( val >= max ) val -= (max-min);
  202. if ( val < min ) val += (max-min);
  203. if ( val < min ) {
  204. val = min;
  205. }
  206. if ( val > max ) {
  207. val = max;
  208. }
  209. return val;
  210. }
  211. WWINLINE float WWMath::Min(float a, float b)
  212. {
  213. if (a<b) return a;
  214. return b;
  215. }
  216. WWINLINE float WWMath::Max(float a, float b)
  217. {
  218. if (a>b) return a;
  219. return b;
  220. }
  221. WWINLINE float WWMath::Lerp(float a, float b, float lerp )
  222. {
  223. return (a + (b - a)*lerp);
  224. }
  225. WWINLINE double WWMath::Lerp(double a, double b, float lerp )
  226. {
  227. return (a + (b - a)*lerp);
  228. }
  229. WWINLINE bool WWMath::Is_Valid_Float(float x)
  230. {
  231. unsigned long * plong = (unsigned long *)(&x);
  232. unsigned long exponent = ((*plong) & 0x7F800000) >> (32-9);
  233. // if exponent is 0xFF, this is a NAN
  234. if (exponent == 0xFF) {
  235. return false;
  236. }
  237. return true;
  238. }
  239. WWINLINE bool WWMath::Is_Valid_Double(double x)
  240. {
  241. unsigned long * plong = (unsigned long *)(&x) + 1;
  242. unsigned long exponent = ((*plong) & 0x7FF00000) >> (32-12);
  243. // if exponent is 0x7FF, this is a NAN
  244. if (exponent == 0x7FF) {
  245. return false;
  246. }
  247. return true;
  248. }
  249. // ----------------------------------------------------------------------------
  250. // Float to long
  251. // ----------------------------------------------------------------------------
  252. #if defined(_MSC_VER) && defined(_M_IX86)
  253. WWINLINE long WWMath::Float_To_Long(float f)
  254. {
  255. long i;
  256. __asm {
  257. fld [f]
  258. fistp [i]
  259. }
  260. return i;
  261. }
  262. #else
  263. WWINLINE long WWMath::Float_To_Long(float f)
  264. {
  265. return (long) f;
  266. }
  267. #endif
  268. WWINLINE long WWMath::Float_To_Long(double f)
  269. {
  270. #if defined(_MSC_VER) && defined(_M_IX86)
  271. long retval;
  272. __asm fld qword ptr [f]
  273. __asm fistp dword ptr [retval]
  274. return retval;
  275. #else
  276. return (long) f;
  277. #endif
  278. }
  279. // ----------------------------------------------------------------------------
  280. // Cos
  281. // ----------------------------------------------------------------------------
  282. #if defined(_MSC_VER) && defined(_M_IX86)
  283. WWINLINE float WWMath::Cos(float val)
  284. {
  285. float retval;
  286. __asm {
  287. fld [val]
  288. fcos
  289. fstp [retval]
  290. }
  291. return retval;
  292. }
  293. #else
  294. WWINLINE float WWMath::Cos(float val)
  295. {
  296. return cosf(val);
  297. }
  298. #endif
  299. // ----------------------------------------------------------------------------
  300. // Sin
  301. // ----------------------------------------------------------------------------
  302. #if defined(_MSC_VER) && defined(_M_IX86)
  303. WWINLINE float WWMath::Sin(float val)
  304. {
  305. float retval;
  306. __asm {
  307. fld [val]
  308. fsin
  309. fstp [retval]
  310. }
  311. return retval;
  312. }
  313. #else
  314. WWINLINE float WWMath::Sin(float val)
  315. {
  316. return sinf(val);
  317. }
  318. #endif
  319. // ----------------------------------------------------------------------------
  320. // Fast, table based sin
  321. // ----------------------------------------------------------------------------
  322. WWINLINE float WWMath::Fast_Sin(float val)
  323. {
  324. val*=float(SIN_TABLE_SIZE) / (2.0f * WWMATH_PI);
  325. int idx0=Float_To_Int_Floor(val);
  326. int idx1=idx0+1;
  327. float frac=val-(float)idx0;
  328. idx0 = ((unsigned)idx0) & (SIN_TABLE_SIZE-1);
  329. idx1 = ((unsigned)idx1) & (SIN_TABLE_SIZE-1);
  330. return (1.0f - frac) * _FastSinTable[idx0] + frac * _FastSinTable[idx1];
  331. }
  332. // ----------------------------------------------------------------------------
  333. // Fast, table based 1.0f/sin
  334. // ----------------------------------------------------------------------------
  335. WWINLINE float WWMath::Fast_Inv_Sin(float val)
  336. {
  337. #if 0 // TODO: more testing, not reliable!
  338. float index = val * float(SIN_TABLE_SIZE) / (2.0f * WWMATH_PI);
  339. int idx0=Float_To_Int_Floor(index);
  340. int idx1=idx0+1;
  341. float frac=val-(float)idx0;
  342. idx0 = ((unsigned)idx0) & (SIN_TABLE_SIZE-1);
  343. idx1 = ((unsigned)idx1) & (SIN_TABLE_SIZE-1);
  344. // The table becomes inaccurate near 0 and 2pi so fall back to doing a divide.
  345. const int BUFFER = 16;
  346. if ((idx0 <= BUFFER) || (idx0 >= SIN_TABLE_SIZE-BUFFER-1)) {
  347. return 1.0f / WWMath::Fast_Sin(val);
  348. } else {
  349. return (1.0f - frac) * _FastInvSinTable[idx0] + frac * _FastInvSinTable[idx1];
  350. }
  351. #else
  352. return 1.0f / WWMath::Fast_Sin(val);
  353. #endif
  354. }
  355. // ----------------------------------------------------------------------------
  356. // Fast, table based cos
  357. // ----------------------------------------------------------------------------
  358. WWINLINE float WWMath::Fast_Cos(float val)
  359. {
  360. val+=(WWMATH_PI * 0.5f);
  361. val*=float(SIN_TABLE_SIZE) / (2.0f * WWMATH_PI);
  362. int idx0=Float_To_Int_Floor(val);
  363. int idx1=idx0+1;
  364. float frac=val-(float)idx0;
  365. idx0 = ((unsigned)idx0) & (SIN_TABLE_SIZE-1);
  366. idx1 = ((unsigned)idx1) & (SIN_TABLE_SIZE-1);
  367. return (1.0f - frac) * _FastSinTable[idx0] + frac * _FastSinTable[idx1];
  368. }
  369. // ----------------------------------------------------------------------------
  370. // Fast, table based 1.0f/cos
  371. // ----------------------------------------------------------------------------
  372. WWINLINE float WWMath::Fast_Inv_Cos(float val)
  373. {
  374. #if 0 // TODO: more testing, not reliable!
  375. float index = val + (WWMATH_PI * 0.5f);
  376. index *= float(SIN_TABLE_SIZE) / (2.0f * WWMATH_PI);
  377. int idx0=Float_To_Int_Chop(index);
  378. int idx1=idx0+1;
  379. float frac=val-(float)idx0;
  380. idx0 = ((unsigned)idx0) & (SIN_TABLE_SIZE-1);
  381. idx1 = ((unsigned)idx1) & (SIN_TABLE_SIZE-1);
  382. // The table becomes inaccurate near 0 and 2pi so fall back to doing a divide.
  383. if ((idx0 <= 2) || (idx0 >= SIN_TABLE_SIZE-3)) {
  384. return 1.0f / WWMath::Fast_Cos(val);
  385. } else {
  386. return (1.0f - frac) * _FastInvSinTable[idx0] + frac * _FastInvSinTable[idx1];
  387. }
  388. #else
  389. return 1.0f / WWMath::Fast_Cos(val);
  390. #endif
  391. }
  392. // ----------------------------------------------------------------------------
  393. // Fast, table based arc cos
  394. // ----------------------------------------------------------------------------
  395. WWINLINE float WWMath::Fast_Acos(float val)
  396. {
  397. // Near -1 and +1, the table becomes too inaccurate
  398. if (WWMath::Fabs(val) > 0.975f) {
  399. return WWMath::Acos(val);
  400. }
  401. val*=float(ARC_TABLE_SIZE/2);
  402. int idx0=Float_To_Int_Floor(val);
  403. int idx1=idx0+1;
  404. float frac=val-(float)idx0;
  405. idx0+=ARC_TABLE_SIZE/2;
  406. idx1+=ARC_TABLE_SIZE/2;
  407. // we dont even get close to the edge of the table...
  408. assert((idx0 >= 0) && (idx0 < ARC_TABLE_SIZE));
  409. assert((idx1 >= 0) && (idx1 < ARC_TABLE_SIZE));
  410. // compute and return the interpolated value
  411. return (1.0f - frac) * _FastAcosTable[idx0] + frac * _FastAcosTable[idx1];
  412. }
  413. // ----------------------------------------------------------------------------
  414. // Arc cos
  415. // ----------------------------------------------------------------------------
  416. WWINLINE float WWMath::Acos(float val)
  417. {
  418. return (float)acos(val);
  419. }
  420. // ----------------------------------------------------------------------------
  421. // Fast, table based arc sin
  422. // ----------------------------------------------------------------------------
  423. WWINLINE float WWMath::Fast_Asin(float val)
  424. {
  425. // Near -1 and +1, the table becomes too inaccurate
  426. if (WWMath::Fabs(val) > 0.975f) {
  427. return WWMath::Asin(val);
  428. }
  429. val*=float(ARC_TABLE_SIZE/2);
  430. int idx0=Float_To_Int_Floor(val);
  431. int idx1=idx0+1;
  432. float frac=val-(float)idx0;
  433. idx0+=ARC_TABLE_SIZE/2;
  434. idx1+=ARC_TABLE_SIZE/2;
  435. // we dont even get close to the edge of the table...
  436. assert((idx0 >= 0) && (idx0 < ARC_TABLE_SIZE));
  437. assert((idx1 >= 0) && (idx1 < ARC_TABLE_SIZE));
  438. // compute and return the interpolated value
  439. return (1.0f - frac) * _FastAsinTable[idx0] + frac * _FastAsinTable[idx1];
  440. }
  441. // ----------------------------------------------------------------------------
  442. // Arc sin
  443. // ----------------------------------------------------------------------------
  444. WWINLINE float WWMath::Asin(float val)
  445. {
  446. return (float)asin(val);
  447. }
  448. // ----------------------------------------------------------------------------
  449. // Sqrt
  450. // ----------------------------------------------------------------------------
  451. #if defined(_MSC_VER) && defined(_M_IX86)
  452. WWINLINE float WWMath::Sqrt(float val)
  453. {
  454. float retval;
  455. __asm {
  456. fld [val]
  457. fsqrt
  458. fstp [retval]
  459. }
  460. return retval;
  461. }
  462. #else
  463. WWINLINE float WWMath::Sqrt(float val)
  464. {
  465. return (float)sqrt(val);
  466. }
  467. #endif
  468. WWINLINE int WWMath::Float_To_Int_Chop(const float& f)
  469. {
  470. int a = *reinterpret_cast<const int*>(&f); // take bit pattern of float into a register
  471. int sign = (a>>31); // sign = 0xFFFFFFFF if original value is negative, 0 if positive
  472. int mantissa = (a&((1<<23)-1))|(1<<23); // extract mantissa and add the hidden bit
  473. int exponent = ((a&0x7fffffff)>>23)-127; // extract the exponent
  474. int r = ((unsigned int)(mantissa)<<8)>>(31-exponent); // ((1<<exponent)*mantissa)>>24 -- (we know that mantissa > (1<<24))
  475. return ((r ^ (sign)) - sign ) &~ (exponent>>31); // add original sign. If exponent was negative, make return value 0.
  476. }
  477. WWINLINE int WWMath::Float_To_Int_Floor (const float& f)
  478. {
  479. int a = *reinterpret_cast<const int*>(&f); // take bit pattern of float into a register
  480. int sign = (a>>31); // sign = 0xFFFFFFFF if original value is negative, 0 if positive
  481. a&=0x7fffffff; // we don't need the sign any more
  482. int exponent = (a>>23)-127; // extract the exponent
  483. int expsign = ~(exponent>>31); // 0xFFFFFFFF if exponent is positive, 0 otherwise
  484. int imask = ( (1<<(31-(exponent))))-1; // mask for true integer values
  485. int mantissa = (a&((1<<23)-1)); // extract mantissa (without the hidden bit)
  486. int r = ((unsigned int)(mantissa|(1<<23))<<8)>>(31-exponent); // ((1<<exponent)*(mantissa|hidden bit))>>24 -- (we know that mantissa > (1<<24))
  487. r = ((r & expsign) ^ (sign)) + ((!((mantissa<<8)&imask)&(expsign^((a-1)>>31)))&sign); // if (fabs(value)<1.0) value = 0; copy sign; if (value < 0 && value==(int)(value)) value++;
  488. return r;
  489. }
  490. // ----------------------------------------------------------------------------
  491. // Inverse square root
  492. // ----------------------------------------------------------------------------
  493. #if defined(_MSC_VER) && defined(_M_IX86)
  494. WWINLINE __declspec(naked) float __fastcall WWMath::Inv_Sqrt(float a)
  495. {
  496. __asm {
  497. mov eax, 0be6eb508h
  498. mov DWORD PTR [esp-12],03fc00000h ; 1.5 on the stack
  499. sub eax, DWORD PTR [esp+4]; a
  500. sub DWORD PTR [esp+4], 800000h ; a/2 a=Y0
  501. shr eax, 1 ; firs approx in eax=R0
  502. mov DWORD PTR [esp-8], eax
  503. fld DWORD PTR [esp-8] ;r
  504. fmul st, st ;r*r
  505. fld DWORD PTR [esp-8] ;r
  506. fxch st(1)
  507. fmul DWORD PTR [esp+4];a ;r*r*y0
  508. fld DWORD PTR [esp-12];load 1.5
  509. fld st(0)
  510. fsub st,st(2) ;r1 = 1.5 - y1
  511. ;x1 = st(3)
  512. ;y1 = st(2)
  513. ;1.5 = st(1)
  514. ;r1 = st(0)
  515. fld st(1)
  516. fxch st(1)
  517. fmul st(3),st ; y2=y1*r1*...
  518. fmul st(3),st ; y2=y1*r1*r1
  519. fmulp st(4),st ; x2=x1*r1
  520. fsub st,st(2) ; r2=1.5-y2
  521. ;x2=st(3)
  522. ;y2=st(2)
  523. ;1.5=st(1)
  524. ;r2 = st(0)
  525. fmul st(2),st ;y3=y2*r2*...
  526. fmul st(3),st ;x3=x2*r2
  527. fmulp st(2),st ;y3=y2*r2*r2
  528. fxch st(1)
  529. fsubp st(1),st ;r3= 1.5 - y3
  530. ;x3 = st(1)
  531. ;r3 = st(0)
  532. fmulp st(1), st
  533. ret 4
  534. }
  535. }
  536. #else
  537. WWINLINE float WWMath::Inv_Sqrt(float val)
  538. {
  539. return 1.0f / (float)sqrt(val);
  540. }
  541. #endif
  542. #endif