irrMath.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef IRR_MATH_H_INCLUDED
  5. #define IRR_MATH_H_INCLUDED
  6. #include "IrrCompileConfig.h"
  7. #include "irrTypes.h"
  8. #include <math.h>
  9. #include <float.h>
  10. #include <stdlib.h> // for abs() etc.
  11. #include <limits.h> // For INT_MAX / UINT_MAX
  12. #if defined(_IRR_SOLARIS_PLATFORM_) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__) || defined (_WIN32_WCE)
  13. #define sqrtf(X) (irr::f32)sqrt((irr::f64)(X))
  14. #define sinf(X) (irr::f32)sin((irr::f64)(X))
  15. #define cosf(X) (irr::f32)cos((irr::f64)(X))
  16. #define asinf(X) (irr::f32)asin((irr::f64)(X))
  17. #define acosf(X) (irr::f32)acos((irr::f64)(X))
  18. #define atan2f(X,Y) (irr::f32)atan2((irr::f64)(X),(irr::f64)(Y))
  19. #define ceilf(X) (irr::f32)ceil((irr::f64)(X))
  20. #define floorf(X) (irr::f32)floor((irr::f64)(X))
  21. #define powf(X,Y) (irr::f32)pow((irr::f64)(X),(irr::f64)(Y))
  22. #define fmodf(X,Y) (irr::f32)fmod((irr::f64)(X),(irr::f64)(Y))
  23. #define fabsf(X) (irr::f32)fabs((irr::f64)(X))
  24. #define logf(X) (irr::f32)log((irr::f64)(X))
  25. #endif
  26. #ifndef FLT_MAX
  27. #define FLT_MAX 3.402823466E+38F
  28. #endif
  29. #ifndef FLT_MIN
  30. #define FLT_MIN 1.17549435e-38F
  31. #endif
  32. namespace irr
  33. {
  34. namespace core
  35. {
  36. //! Rounding error constant often used when comparing f32 values.
  37. const s32 ROUNDING_ERROR_S32 = 0;
  38. #ifdef __IRR_HAS_S64
  39. const s64 ROUNDING_ERROR_S64 = 0;
  40. #endif
  41. const f32 ROUNDING_ERROR_f32 = 0.000001f;
  42. const f64 ROUNDING_ERROR_f64 = 0.00000001;
  43. #ifdef PI // make sure we don't collide with a define
  44. #undef PI
  45. #endif
  46. //! Constant for PI.
  47. const f32 PI = 3.14159265359f;
  48. //! Constant for reciprocal of PI.
  49. const f32 RECIPROCAL_PI = 1.0f/PI;
  50. //! Constant for half of PI.
  51. const f32 HALF_PI = PI/2.0f;
  52. #ifdef PI64 // make sure we don't collide with a define
  53. #undef PI64
  54. #endif
  55. //! Constant for 64bit PI.
  56. const f64 PI64 = 3.1415926535897932384626433832795028841971693993751;
  57. //! Constant for 64bit reciprocal of PI.
  58. const f64 RECIPROCAL_PI64 = 1.0/PI64;
  59. //! 32bit Constant for converting from degrees to radians
  60. const f32 DEGTORAD = PI / 180.0f;
  61. //! 32bit constant for converting from radians to degrees (formally known as GRAD_PI)
  62. const f32 RADTODEG = 180.0f / PI;
  63. //! 64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
  64. const f64 DEGTORAD64 = PI64 / 180.0;
  65. //! 64bit constant for converting from radians to degrees
  66. const f64 RADTODEG64 = 180.0 / PI64;
  67. //! Utility function to convert a radian value to degrees
  68. /** Provided as it can be clearer to write radToDeg(X) than RADTODEG * X
  69. \param radians The radians value to convert to degrees.
  70. */
  71. inline f32 radToDeg(f32 radians)
  72. {
  73. return RADTODEG * radians;
  74. }
  75. //! Utility function to convert a radian value to degrees
  76. /** Provided as it can be clearer to write radToDeg(X) than RADTODEG * X
  77. \param radians The radians value to convert to degrees.
  78. */
  79. inline f64 radToDeg(f64 radians)
  80. {
  81. return RADTODEG64 * radians;
  82. }
  83. //! Utility function to convert a degrees value to radians
  84. /** Provided as it can be clearer to write degToRad(X) than DEGTORAD * X
  85. \param degrees The degrees value to convert to radians.
  86. */
  87. inline f32 degToRad(f32 degrees)
  88. {
  89. return DEGTORAD * degrees;
  90. }
  91. //! Utility function to convert a degrees value to radians
  92. /** Provided as it can be clearer to write degToRad(X) than DEGTORAD * X
  93. \param degrees The degrees value to convert to radians.
  94. */
  95. inline f64 degToRad(f64 degrees)
  96. {
  97. return DEGTORAD64 * degrees;
  98. }
  99. //! returns minimum of two values. Own implementation to get rid of the STL (VS6 problems)
  100. template<class T>
  101. inline const T& min_(const T& a, const T& b)
  102. {
  103. return a < b ? a : b;
  104. }
  105. //! returns minimum of three values. Own implementation to get rid of the STL (VS6 problems)
  106. template<class T>
  107. inline const T& min_(const T& a, const T& b, const T& c)
  108. {
  109. return a < b ? min_(a, c) : min_(b, c);
  110. }
  111. //! returns maximum of two values. Own implementation to get rid of the STL (VS6 problems)
  112. template<class T>
  113. inline const T& max_(const T& a, const T& b)
  114. {
  115. return a < b ? b : a;
  116. }
  117. //! returns maximum of three values. Own implementation to get rid of the STL (VS6 problems)
  118. template<class T>
  119. inline const T& max_(const T& a, const T& b, const T& c)
  120. {
  121. return a < b ? max_(b, c) : max_(a, c);
  122. }
  123. //! returns abs of two values. Own implementation to get rid of STL (VS6 problems)
  124. template<class T>
  125. inline T abs_(const T& a)
  126. {
  127. return a < (T)0 ? -a : a;
  128. }
  129. //! returns linear interpolation of a and b with ratio t
  130. //! \return: a if t==0, b if t==1, and the linear interpolation else
  131. template<class T>
  132. inline T lerp(const T& a, const T& b, const f32 t)
  133. {
  134. return (T)(a*(1.f-t)) + (b*t);
  135. }
  136. //! clamps a value between low and high
  137. template <class T>
  138. inline const T clamp (const T& value, const T& low, const T& high)
  139. {
  140. return min_ (max_(value,low), high);
  141. }
  142. //! swaps the content of the passed parameters
  143. // Note: We use the same trick as boost and use two template arguments to
  144. // avoid ambiguity when swapping objects of an Irrlicht type that has not
  145. // it's own swap overload. Otherwise we get conflicts with some compilers
  146. // in combination with stl.
  147. template <class T1, class T2>
  148. inline void swap(T1& a, T2& b)
  149. {
  150. T1 c(a);
  151. a = b;
  152. b = c;
  153. }
  154. template <class T>
  155. inline T roundingError();
  156. template <>
  157. inline f32 roundingError()
  158. {
  159. return ROUNDING_ERROR_f32;
  160. }
  161. template <>
  162. inline f64 roundingError()
  163. {
  164. return ROUNDING_ERROR_f64;
  165. }
  166. template <>
  167. inline s32 roundingError()
  168. {
  169. return ROUNDING_ERROR_S32;
  170. }
  171. template <>
  172. inline u32 roundingError()
  173. {
  174. return ROUNDING_ERROR_S32;
  175. }
  176. #ifdef __IRR_HAS_S64
  177. template <>
  178. inline s64 roundingError()
  179. {
  180. return ROUNDING_ERROR_S64;
  181. }
  182. template <>
  183. inline u64 roundingError()
  184. {
  185. return ROUNDING_ERROR_S64;
  186. }
  187. #endif
  188. template <class T>
  189. inline T relativeErrorFactor()
  190. {
  191. return 1;
  192. }
  193. template <>
  194. inline f32 relativeErrorFactor()
  195. {
  196. return 4;
  197. }
  198. template <>
  199. inline f64 relativeErrorFactor()
  200. {
  201. return 8;
  202. }
  203. //! returns if a equals b, taking possible rounding errors into account
  204. template <class T>
  205. inline bool equals(const T a, const T b, const T tolerance = roundingError<T>())
  206. {
  207. return (a + tolerance >= b) && (a - tolerance <= b);
  208. }
  209. //! returns if a equals b, taking relative error in form of factor
  210. //! this particular function does not involve any division.
  211. template <class T>
  212. inline bool equalsRelative( const T a, const T b, const T factor = relativeErrorFactor<T>())
  213. {
  214. //https://eagergames.wordpress.com/2017/04/01/fast-parallel-lines-and-vectors-test/
  215. const T maxi = max_( a, b);
  216. const T mini = min_( a, b);
  217. const T maxMagnitude = max_( maxi, -mini);
  218. return (maxMagnitude*factor + maxi) == (maxMagnitude*factor + mini); // MAD Wise
  219. }
  220. union FloatIntUnion32
  221. {
  222. FloatIntUnion32(float f1 = 0.0f) : f(f1) {}
  223. // Portable sign-extraction
  224. bool sign() const { return (i >> 31) != 0; }
  225. irr::s32 i;
  226. irr::f32 f;
  227. };
  228. //! We compare the difference in ULP's (spacing between floating-point numbers, aka ULP=1 means there exists no float between).
  229. //\result true when numbers have a ULP <= maxUlpDiff AND have the same sign.
  230. inline bool equalsByUlp(f32 a, f32 b, int maxUlpDiff)
  231. {
  232. // Based on the ideas and code from Bruce Dawson on
  233. // http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/
  234. // When floats are interpreted as integers the two nearest possible float numbers differ just
  235. // by one integer number. Also works the other way round, an integer of 1 interpreted as float
  236. // is for example the smallest possible float number.
  237. const FloatIntUnion32 fa(a);
  238. const FloatIntUnion32 fb(b);
  239. // Different signs, we could maybe get difference to 0, but so close to 0 using epsilons is better.
  240. if ( fa.sign() != fb.sign() )
  241. {
  242. // Check for equality to make sure +0==-0
  243. if (fa.i == fb.i)
  244. return true;
  245. return false;
  246. }
  247. // Find the difference in ULPs.
  248. const int ulpsDiff = abs_(fa.i- fb.i);
  249. if (ulpsDiff <= maxUlpDiff)
  250. return true;
  251. return false;
  252. }
  253. //! returns if a equals zero, taking rounding errors into account
  254. inline bool iszero(const f64 a, const f64 tolerance = ROUNDING_ERROR_f64)
  255. {
  256. return fabs(a) <= tolerance;
  257. }
  258. //! returns if a equals zero, taking rounding errors into account
  259. inline bool iszero(const f32 a, const f32 tolerance = ROUNDING_ERROR_f32)
  260. {
  261. return fabsf(a) <= tolerance;
  262. }
  263. //! returns if a equals not zero, taking rounding errors into account
  264. inline bool isnotzero(const f32 a, const f32 tolerance = ROUNDING_ERROR_f32)
  265. {
  266. return fabsf(a) > tolerance;
  267. }
  268. //! returns if a equals zero, taking rounding errors into account
  269. inline bool iszero(const s32 a, const s32 tolerance = 0)
  270. {
  271. return ( a & 0x7ffffff ) <= tolerance;
  272. }
  273. //! returns if a equals zero, taking rounding errors into account
  274. inline bool iszero(const u32 a, const u32 tolerance = 0)
  275. {
  276. return a <= tolerance;
  277. }
  278. #ifdef __IRR_HAS_S64
  279. //! returns if a equals zero, taking rounding errors into account
  280. inline bool iszero(const s64 a, const s64 tolerance = 0)
  281. {
  282. return abs_(a) <= tolerance;
  283. }
  284. #endif
  285. inline s32 s32_min(s32 a, s32 b)
  286. {
  287. const s32 mask = (a - b) >> 31;
  288. return (a & mask) | (b & ~mask);
  289. }
  290. inline s32 s32_max(s32 a, s32 b)
  291. {
  292. const s32 mask = (a - b) >> 31;
  293. return (b & mask) | (a & ~mask);
  294. }
  295. inline s32 s32_clamp (s32 value, s32 low, s32 high)
  296. {
  297. return s32_min(s32_max(value,low), high);
  298. }
  299. /*
  300. float IEEE-754 bit representation
  301. 0 0x00000000
  302. 1.0 0x3f800000
  303. 0.5 0x3f000000
  304. 3 0x40400000
  305. +inf 0x7f800000
  306. -inf 0xff800000
  307. +NaN 0x7fc00000 or 0x7ff00000
  308. in general: number = (sign ? -1:1) * 2^(exponent) * 1.(mantissa bits)
  309. */
  310. typedef union { u32 u; s32 s; f32 f; } inttofloat;
  311. #define F32_AS_S32(f) (*((s32 *) &(f)))
  312. #define F32_AS_U32(f) (*((u32 *) &(f)))
  313. #define F32_AS_U32_POINTER(f) ( ((u32 *) &(f)))
  314. #define F32_VALUE_0 0x00000000
  315. #define F32_VALUE_1 0x3f800000
  316. #define F32_SIGN_BIT 0x80000000U
  317. #define F32_EXPON_MANTISSA 0x7FFFFFFFU
  318. //! code is taken from IceFPU
  319. //! Integer representation of a floating-point value.
  320. #ifdef IRRLICHT_FAST_MATH
  321. #define IR(x) ((u32&)(x))
  322. #else
  323. inline u32 IR(f32 x) {inttofloat tmp; tmp.f=x; return tmp.u;}
  324. #endif
  325. //! Absolute integer representation of a floating-point value
  326. #define AIR(x) (IR(x)&0x7fffffff)
  327. //! Floating-point representation of an integer value.
  328. #ifdef IRRLICHT_FAST_MATH
  329. #define FR(x) ((f32&)(x))
  330. #else
  331. inline f32 FR(u32 x) {inttofloat tmp; tmp.u=x; return tmp.f;}
  332. inline f32 FR(s32 x) {inttofloat tmp; tmp.s=x; return tmp.f;}
  333. #endif
  334. //! integer representation of 1.0
  335. #define IEEE_1_0 0x3f800000
  336. //! integer representation of 255.0
  337. #define IEEE_255_0 0x437f0000
  338. #ifdef IRRLICHT_FAST_MATH
  339. #define F32_LOWER_0(f) (F32_AS_U32(f) > F32_SIGN_BIT)
  340. #define F32_LOWER_EQUAL_0(f) (F32_AS_S32(f) <= F32_VALUE_0)
  341. #define F32_GREATER_0(f) (F32_AS_S32(f) > F32_VALUE_0)
  342. #define F32_GREATER_EQUAL_0(f) (F32_AS_U32(f) <= F32_SIGN_BIT)
  343. #define F32_EQUAL_1(f) (F32_AS_U32(f) == F32_VALUE_1)
  344. #define F32_EQUAL_0(f) ( (F32_AS_U32(f) & F32_EXPON_MANTISSA ) == F32_VALUE_0)
  345. // only same sign
  346. #define F32_A_GREATER_B(a,b) (F32_AS_S32((a)) > F32_AS_S32((b)))
  347. #else
  348. #define F32_LOWER_0(n) ((n) < 0.0f)
  349. #define F32_LOWER_EQUAL_0(n) ((n) <= 0.0f)
  350. #define F32_GREATER_0(n) ((n) > 0.0f)
  351. #define F32_GREATER_EQUAL_0(n) ((n) >= 0.0f)
  352. #define F32_EQUAL_1(n) ((n) == 1.0f)
  353. #define F32_EQUAL_0(n) ((n) == 0.0f)
  354. #define F32_A_GREATER_B(a,b) ((a) > (b))
  355. #endif
  356. #ifndef REALINLINE
  357. #ifdef _MSC_VER
  358. #define REALINLINE __forceinline
  359. #else
  360. #define REALINLINE inline
  361. #endif
  362. #endif
  363. #if defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
  364. // 8-bit bools in Borland builder
  365. //! conditional set based on mask and arithmetic shift
  366. REALINLINE u32 if_c_a_else_b ( const c8 condition, const u32 a, const u32 b )
  367. {
  368. return ( ( -condition >> 7 ) & ( a ^ b ) ) ^ b;
  369. }
  370. //! conditional set based on mask and arithmetic shift
  371. REALINLINE u32 if_c_a_else_0 ( const c8 condition, const u32 a )
  372. {
  373. return ( -condition >> 31 ) & a;
  374. }
  375. #else
  376. //! conditional set based on mask and arithmetic shift
  377. REALINLINE u32 if_c_a_else_b ( const s32 condition, const u32 a, const u32 b )
  378. {
  379. return ( ( -condition >> 31 ) & ( a ^ b ) ) ^ b;
  380. }
  381. //! conditional set based on mask and arithmetic shift
  382. REALINLINE u16 if_c_a_else_b ( const s16 condition, const u16 a, const u16 b )
  383. {
  384. return ( ( -condition >> 15 ) & ( a ^ b ) ) ^ b;
  385. }
  386. //! conditional set based on mask and arithmetic shift
  387. REALINLINE u32 if_c_a_else_0 ( const s32 condition, const u32 a )
  388. {
  389. return ( -condition >> 31 ) & a;
  390. }
  391. #endif
  392. /*
  393. if (condition) state |= m; else state &= ~m;
  394. */
  395. REALINLINE void setbit_cond ( u32 &state, s32 condition, u32 mask )
  396. {
  397. // 0, or any positive to mask
  398. //s32 conmask = -condition >> 31;
  399. state ^= ( ( -condition >> 31 ) ^ state ) & mask;
  400. }
  401. // NOTE: This is not as exact as the c99/c++11 round function, especially at high numbers starting with 8388609
  402. // (only low number which seems to go wrong is 0.49999997 which is rounded to 1)
  403. // Also negative 0.5 is rounded up not down unlike with the standard function (p.E. input -0.5 will be 0 and not -1)
  404. inline f32 round_( f32 x )
  405. {
  406. return floorf( x + 0.5f );
  407. }
  408. // calculate: sqrt ( x )
  409. REALINLINE f32 squareroot(const f32 f)
  410. {
  411. return sqrtf(f);
  412. }
  413. // calculate: sqrt ( x )
  414. REALINLINE f64 squareroot(const f64 f)
  415. {
  416. return sqrt(f);
  417. }
  418. // calculate: sqrt ( x )
  419. REALINLINE s32 squareroot(const s32 f)
  420. {
  421. return static_cast<s32>(squareroot(static_cast<f32>(f)));
  422. }
  423. #ifdef __IRR_HAS_S64
  424. // calculate: sqrt ( x )
  425. REALINLINE s64 squareroot(const s64 f)
  426. {
  427. return static_cast<s64>(squareroot(static_cast<f64>(f)));
  428. }
  429. #endif
  430. // calculate: 1 / sqrt ( x )
  431. REALINLINE f64 reciprocal_squareroot(const f64 x)
  432. {
  433. return 1.0 / sqrt(x);
  434. }
  435. // calculate: 1 / sqrtf ( x )
  436. REALINLINE f32 reciprocal_squareroot(const f32 f)
  437. {
  438. #if defined ( IRRLICHT_FAST_MATH )
  439. // NOTE: Unlike comment below says I found inaccuracies already at 4'th significant bit.
  440. // p.E: Input 1, expected 1, got 0.999755859
  441. #if defined(_MSC_VER) && !defined(_WIN64)
  442. // SSE reciprocal square root estimate, accurate to 12 significant
  443. // bits of the mantissa
  444. f32 recsqrt;
  445. __asm rsqrtss xmm0, f // xmm0 = rsqrtss(f)
  446. __asm movss recsqrt, xmm0 // return xmm0
  447. return recsqrt;
  448. /*
  449. // comes from Nvidia
  450. u32 tmp = (u32(IEEE_1_0 << 1) + IEEE_1_0 - *(u32*)&x) >> 1;
  451. f32 y = *(f32*)&tmp;
  452. return y * (1.47f - 0.47f * x * y * y);
  453. */
  454. #else
  455. return 1.f / sqrtf(f);
  456. #endif
  457. #else // no fast math
  458. return 1.f / sqrtf(f);
  459. #endif
  460. }
  461. // calculate: 1 / sqrtf( x )
  462. REALINLINE s32 reciprocal_squareroot(const s32 x)
  463. {
  464. return static_cast<s32>(reciprocal_squareroot(static_cast<f32>(x)));
  465. }
  466. // calculate: 1 / x
  467. REALINLINE f32 reciprocal( const f32 f )
  468. {
  469. #if defined (IRRLICHT_FAST_MATH)
  470. // NOTE: Unlike with 1.f / f the values very close to 0 return -nan instead of inf
  471. // SSE Newton-Raphson reciprocal estimate, accurate to 23 significant
  472. // bi ts of the mantissa
  473. // One Newton-Raphson Iteration:
  474. // f(i+1) = 2 * rcpss(f) - f * rcpss(f) * rcpss(f)
  475. #if defined(_MSC_VER) && !defined(_WIN64)
  476. f32 rec;
  477. __asm rcpss xmm0, f // xmm0 = rcpss(f)
  478. __asm movss xmm1, f // xmm1 = f
  479. __asm mulss xmm1, xmm0 // xmm1 = f * rcpss(f)
  480. __asm mulss xmm1, xmm0 // xmm2 = f * rcpss(f) * rcpss(f)
  481. __asm addss xmm0, xmm0 // xmm0 = 2 * rcpss(f)
  482. __asm subss xmm0, xmm1 // xmm0 = 2 * rcpss(f)
  483. // - f * rcpss(f) * rcpss(f)
  484. __asm movss rec, xmm0 // return xmm0
  485. return rec;
  486. #else // no support yet for other compilers
  487. return 1.f / f;
  488. #endif
  489. //! i do not divide through 0.. (fpu expection)
  490. // instead set f to a high value to get a return value near zero..
  491. // -1000000000000.f.. is use minus to stay negative..
  492. // must test's here (plane.normal dot anything ) checks on <= 0.f
  493. //u32 x = (-(AIR(f) != 0 ) >> 31 ) & ( IR(f) ^ 0xd368d4a5 ) ^ 0xd368d4a5;
  494. //return 1.f / FR ( x );
  495. #else // no fast math
  496. return 1.f / f;
  497. #endif
  498. }
  499. // calculate: 1 / x
  500. REALINLINE f64 reciprocal ( const f64 f )
  501. {
  502. return 1.0 / f;
  503. }
  504. // calculate: 1 / x, low precision allowed
  505. REALINLINE f32 reciprocal_approxim ( const f32 f )
  506. {
  507. #if defined( IRRLICHT_FAST_MATH)
  508. // SSE Newton-Raphson reciprocal estimate, accurate to 23 significant
  509. // bi ts of the mantissa
  510. // One Newton-Raphson Iteration:
  511. // f(i+1) = 2 * rcpss(f) - f * rcpss(f) * rcpss(f)
  512. #if defined(_MSC_VER) && !defined(_WIN64)
  513. f32 rec;
  514. __asm rcpss xmm0, f // xmm0 = rcpss(f)
  515. __asm movss xmm1, f // xmm1 = f
  516. __asm mulss xmm1, xmm0 // xmm1 = f * rcpss(f)
  517. __asm mulss xmm1, xmm0 // xmm2 = f * rcpss(f) * rcpss(f)
  518. __asm addss xmm0, xmm0 // xmm0 = 2 * rcpss(f)
  519. __asm subss xmm0, xmm1 // xmm0 = 2 * rcpss(f)
  520. // - f * rcpss(f) * rcpss(f)
  521. __asm movss rec, xmm0 // return xmm0
  522. return rec;
  523. #else // no support yet for other compilers
  524. return 1.f / f;
  525. #endif
  526. /*
  527. // SSE reciprocal estimate, accurate to 12 significant bits of
  528. f32 rec;
  529. __asm rcpss xmm0, f // xmm0 = rcpss(f)
  530. __asm movss rec , xmm0 // return xmm0
  531. return rec;
  532. */
  533. /*
  534. u32 x = 0x7F000000 - IR ( p );
  535. const f32 r = FR ( x );
  536. return r * (2.0f - p * r);
  537. */
  538. #else // no fast math
  539. return 1.f / f;
  540. #endif
  541. }
  542. REALINLINE s32 floor32(f32 x)
  543. {
  544. return (s32) floorf ( x );
  545. }
  546. REALINLINE s32 ceil32 ( f32 x )
  547. {
  548. return (s32) ceilf ( x );
  549. }
  550. // NOTE: Please check round_ documentation about some inaccuracies in this compared to standard library round function.
  551. REALINLINE s32 round32(f32 x)
  552. {
  553. return (s32) round_(x);
  554. }
  555. inline f32 f32_max3(const f32 a, const f32 b, const f32 c)
  556. {
  557. return a > b ? (a > c ? a : c) : (b > c ? b : c);
  558. }
  559. inline f32 f32_min3(const f32 a, const f32 b, const f32 c)
  560. {
  561. return a < b ? (a < c ? a : c) : (b < c ? b : c);
  562. }
  563. inline f32 fract ( f32 x )
  564. {
  565. return x - floorf ( x );
  566. }
  567. } // end namespace core
  568. } // end namespace irr
  569. #ifndef IRRLICHT_FAST_MATH
  570. using irr::core::IR;
  571. using irr::core::FR;
  572. #endif
  573. #endif