b3Scalar.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. Copyright (c) 2003-2013 Gino van den Bergen / Erwin Coumans http://bulletphysics.org
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. #ifndef B3_SCALAR_H
  13. #define B3_SCALAR_H
  14. #ifdef B3_MANAGED_CODE
  15. //Aligned data types not supported in managed code
  16. #pragma unmanaged
  17. #endif
  18. #include <math.h>
  19. #include <stdlib.h> //size_t for MSVC 6.0
  20. #include <float.h>
  21. //Original repository is at http://github.com/erwincoumans/bullet3
  22. #define B3_BULLET_VERSION 300
  23. inline int b3GetVersion()
  24. {
  25. return B3_BULLET_VERSION;
  26. }
  27. #if defined(DEBUG) || defined(_DEBUG)
  28. #define B3_DEBUG
  29. #endif
  30. #include "b3Logging.h" //for b3Error
  31. #ifdef _WIN32
  32. #if defined(__GNUC__) // it should handle both MINGW and CYGWIN
  33. #define B3_FORCE_INLINE __inline__ __attribute__((always_inline))
  34. #define B3_ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
  35. #define B3_ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
  36. #define B3_ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
  37. #elif ( defined(_MSC_VER) && _MSC_VER < 1300 )
  38. #define B3_FORCE_INLINE inline
  39. #define B3_ATTRIBUTE_ALIGNED16(a) a
  40. #define B3_ATTRIBUTE_ALIGNED64(a) a
  41. #define B3_ATTRIBUTE_ALIGNED128(a) a
  42. #else
  43. //#define B3_HAS_ALIGNED_ALLOCATOR
  44. #pragma warning(disable : 4324) // disable padding warning
  45. // #pragma warning(disable:4530) // Disable the exception disable but used in MSCV Stl warning.
  46. #pragma warning(disable : 4996) //Turn off warnings about deprecated C routines
  47. // #pragma warning(disable:4786) // Disable the "debug name too long" warning
  48. #define B3_FORCE_INLINE __forceinline
  49. #define B3_ATTRIBUTE_ALIGNED16(a) __declspec(align(16)) a
  50. #define B3_ATTRIBUTE_ALIGNED64(a) __declspec(align(64)) a
  51. #define B3_ATTRIBUTE_ALIGNED128(a) __declspec(align(128)) a
  52. #ifdef _XBOX
  53. #define B3_USE_VMX128
  54. #include <ppcintrinsics.h>
  55. #define B3_HAVE_NATIVE_FSEL
  56. #define b3Fsel(a, b, c) __fsel((a), (b), (c))
  57. #else
  58. #if (defined(_WIN32) && (_MSC_VER) && _MSC_VER >= 1400) && (!defined(B3_USE_DOUBLE_PRECISION))
  59. #if (defined(_M_IX86) || defined(_M_X64))
  60. #ifdef __clang__
  61. //#define B3_NO_SIMD_OPERATOR_OVERLOADS
  62. #define B3_DISABLE_SSE
  63. #endif //__clang__
  64. #ifndef B3_DISABLE_SSE
  65. #define B3_USE_SSE
  66. #endif //B3_DISABLE_SSE
  67. #ifdef B3_USE_SSE
  68. //B3_USE_SSE_IN_API is disabled under Windows by default, because
  69. //it makes it harder to integrate Bullet into your application under Windows
  70. //(structured embedding Bullet structs/classes need to be 16-byte aligned)
  71. //with relatively little performance gain
  72. //If you are not embedded Bullet data in your classes, or make sure that you align those classes on 16-byte boundaries
  73. //you can manually enable this line or set it in the build system for a bit of performance gain (a few percent, dependent on usage)
  74. //#define B3_USE_SSE_IN_API
  75. #endif //B3_USE_SSE
  76. #include <emmintrin.h>
  77. #endif
  78. #endif
  79. #endif //_XBOX
  80. #endif //__MINGW32__
  81. #ifdef B3_DEBUG
  82. #ifdef _MSC_VER
  83. #include <stdio.h>
  84. #define b3Assert(x) { if(!(x)){b3Error("Assert " __FILE__ ":%u (%s)\n", __LINE__, #x);__debugbreak(); }}
  85. #else //_MSC_VER
  86. #include <assert.h>
  87. #define b3Assert assert
  88. #endif //_MSC_VER
  89. #else
  90. #define b3Assert(x)
  91. #endif
  92. //b3FullAssert is optional, slows down a lot
  93. #define b3FullAssert(x)
  94. #define b3Likely(_c) _c
  95. #define b3Unlikely(_c) _c
  96. #else
  97. #if defined(__CELLOS_LV2__)
  98. #define B3_FORCE_INLINE inline __attribute__((always_inline))
  99. #define B3_ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
  100. #define B3_ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
  101. #define B3_ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
  102. #ifndef assert
  103. #include <assert.h>
  104. #endif
  105. #ifdef B3_DEBUG
  106. #ifdef __SPU__
  107. #include <spu_printf.h>
  108. #define printf spu_printf
  109. #define b3Assert(x) \
  110. { \
  111. if (!(x)) \
  112. { \
  113. b3Error( \
  114. "Assert "__FILE__ \
  115. ":%u (" #x ")\n", \
  116. __LINE__); \
  117. spu_hcmpeq(0, 0); \
  118. } \
  119. }
  120. #else
  121. #define b3Assert assert
  122. #endif
  123. #else
  124. #define b3Assert(x)
  125. #endif
  126. //b3FullAssert is optional, slows down a lot
  127. #define b3FullAssert(x)
  128. #define b3Likely(_c) _c
  129. #define b3Unlikely(_c) _c
  130. #else
  131. #ifdef USE_LIBSPE2
  132. #define B3_FORCE_INLINE __inline
  133. #define B3_ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
  134. #define B3_ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
  135. #define B3_ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
  136. #ifndef assert
  137. #include <assert.h>
  138. #endif
  139. #ifdef B3_DEBUG
  140. #define b3Assert assert
  141. #else
  142. #define b3Assert(x)
  143. #endif
  144. //b3FullAssert is optional, slows down a lot
  145. #define b3FullAssert(x)
  146. #define b3Likely(_c) __builtin_expect((_c), 1)
  147. #define b3Unlikely(_c) __builtin_expect((_c), 0)
  148. #else
  149. //non-windows systems
  150. #if (defined(__APPLE__) && (!defined(B3_USE_DOUBLE_PRECISION)))
  151. #if defined(__i386__) || defined(__x86_64__)
  152. #define B3_USE_SSE
  153. //B3_USE_SSE_IN_API is enabled on Mac OSX by default, because memory is automatically aligned on 16-byte boundaries
  154. //if apps run into issues, we will disable the next line
  155. #define B3_USE_SSE_IN_API
  156. #ifdef B3_USE_SSE
  157. // include appropriate SSE level
  158. #if defined(__SSE4_1__)
  159. #include <smmintrin.h>
  160. #elif defined(__SSSE3__)
  161. #include <tmmintrin.h>
  162. #elif defined(__SSE3__)
  163. #include <pmmintrin.h>
  164. #else
  165. #include <emmintrin.h>
  166. #endif
  167. #endif //B3_USE_SSE
  168. #elif defined(__armv7__)
  169. #ifdef __clang__
  170. #define B3_USE_NEON 1
  171. #if defined B3_USE_NEON && defined(__clang__)
  172. #include <arm_neon.h>
  173. #endif //B3_USE_NEON
  174. #endif //__clang__
  175. #endif //__arm__
  176. #define B3_FORCE_INLINE inline __attribute__((always_inline))
  177. ///@todo: check out alignment methods for other platforms/compilers
  178. #define B3_ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
  179. #define B3_ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
  180. #define B3_ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
  181. #ifndef assert
  182. #include <assert.h>
  183. #endif
  184. #if defined(DEBUG) || defined(_DEBUG)
  185. #if defined(__i386__) || defined(__x86_64__)
  186. #include <stdio.h>
  187. #define b3Assert(x) \
  188. { \
  189. if (!(x)) \
  190. { \
  191. b3Error("Assert %s in line %d, file %s\n", #x, __LINE__, __FILE__); \
  192. asm volatile("int3"); \
  193. } \
  194. }
  195. #else //defined (__i386__) || defined (__x86_64__)
  196. #define b3Assert assert
  197. #endif //defined (__i386__) || defined (__x86_64__)
  198. #else //defined(DEBUG) || defined (_DEBUG)
  199. #define b3Assert(x)
  200. #endif //defined(DEBUG) || defined (_DEBUG)
  201. //b3FullAssert is optional, slows down a lot
  202. #define b3FullAssert(x)
  203. #define b3Likely(_c) _c
  204. #define b3Unlikely(_c) _c
  205. #else
  206. #define B3_FORCE_INLINE inline
  207. ///@todo: check out alignment methods for other platforms/compilers
  208. #define B3_ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
  209. #define B3_ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
  210. #define B3_ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
  211. ///#define B3_ATTRIBUTE_ALIGNED16(a) a
  212. ///#define B3_ATTRIBUTE_ALIGNED64(a) a
  213. ///#define B3_ATTRIBUTE_ALIGNED128(a) a
  214. #ifndef assert
  215. #include <assert.h>
  216. #endif
  217. #if defined(DEBUG) || defined(_DEBUG)
  218. #define b3Assert assert
  219. #else
  220. #define b3Assert(x)
  221. #endif
  222. //b3FullAssert is optional, slows down a lot
  223. #define b3FullAssert(x)
  224. #define b3Likely(_c) _c
  225. #define b3Unlikely(_c) _c
  226. #endif //__APPLE__
  227. #endif // LIBSPE2
  228. #endif //__CELLOS_LV2__
  229. #endif
  230. ///The b3Scalar type abstracts floating point numbers, to easily switch between double and single floating point precision.
  231. #if defined(B3_USE_DOUBLE_PRECISION)
  232. typedef double b3Scalar;
  233. //this number could be bigger in double precision
  234. #define B3_LARGE_FLOAT 1e30
  235. #else
  236. typedef float b3Scalar;
  237. //keep B3_LARGE_FLOAT*B3_LARGE_FLOAT < FLT_MAX
  238. #define B3_LARGE_FLOAT 1e18f
  239. #endif
  240. #ifdef B3_USE_SSE
  241. typedef __m128 b3SimdFloat4;
  242. #endif //B3_USE_SSE
  243. #if defined B3_USE_SSE_IN_API && defined(B3_USE_SSE)
  244. #ifdef _WIN32
  245. #ifndef B3_NAN
  246. static int b3NanMask = 0x7F800001;
  247. #define B3_NAN (*(float *)&b3NanMask)
  248. #endif
  249. #ifndef B3_INFINITY_MASK
  250. static int b3InfinityMask = 0x7F800000;
  251. #define B3_INFINITY_MASK (*(float *)&b3InfinityMask)
  252. #endif
  253. #ifndef B3_NO_SIMD_OPERATOR_OVERLOADS
  254. inline __m128 operator+(const __m128 A, const __m128 B)
  255. {
  256. return _mm_add_ps(A, B);
  257. }
  258. inline __m128 operator-(const __m128 A, const __m128 B)
  259. {
  260. return _mm_sub_ps(A, B);
  261. }
  262. inline __m128 operator*(const __m128 A, const __m128 B)
  263. {
  264. return _mm_mul_ps(A, B);
  265. }
  266. #endif //B3_NO_SIMD_OPERATOR_OVERLOADS
  267. #define b3CastfTo128i(a) (_mm_castps_si128(a))
  268. #define b3CastfTo128d(a) (_mm_castps_pd(a))
  269. #define b3CastiTo128f(a) (_mm_castsi128_ps(a))
  270. #define b3CastdTo128f(a) (_mm_castpd_ps(a))
  271. #define b3CastdTo128i(a) (_mm_castpd_si128(a))
  272. #define b3Assign128(r0, r1, r2, r3) _mm_setr_ps(r0, r1, r2, r3)
  273. #else //_WIN32
  274. #define b3CastfTo128i(a) ((__m128i)(a))
  275. #define b3CastfTo128d(a) ((__m128d)(a))
  276. #define b3CastiTo128f(a) ((__m128)(a))
  277. #define b3CastdTo128f(a) ((__m128)(a))
  278. #define b3CastdTo128i(a) ((__m128i)(a))
  279. #define b3Assign128(r0, r1, r2, r3) \
  280. (__m128) { r0, r1, r2, r3 }
  281. #endif //_WIN32
  282. #endif //B3_USE_SSE_IN_API
  283. #ifdef B3_USE_NEON
  284. #include <arm_neon.h>
  285. typedef float32x4_t b3SimdFloat4;
  286. #define B3_INFINITY INFINITY
  287. #define B3_NAN NAN
  288. #define b3Assign128(r0, r1, r2, r3) \
  289. (float32x4_t) { r0, r1, r2, r3 }
  290. #endif
  291. #define B3_DECLARE_ALIGNED_ALLOCATOR() \
  292. B3_FORCE_INLINE void *operator new(size_t sizeInBytes) { return b3AlignedAlloc(sizeInBytes, 16); } \
  293. B3_FORCE_INLINE void operator delete(void *ptr) { b3AlignedFree(ptr); } \
  294. B3_FORCE_INLINE void *operator new(size_t, void *ptr) { return ptr; } \
  295. B3_FORCE_INLINE void operator delete(void *, void *) {} \
  296. B3_FORCE_INLINE void *operator new[](size_t sizeInBytes) { return b3AlignedAlloc(sizeInBytes, 16); } \
  297. B3_FORCE_INLINE void operator delete[](void *ptr) { b3AlignedFree(ptr); } \
  298. B3_FORCE_INLINE void *operator new[](size_t, void *ptr) { return ptr; } \
  299. B3_FORCE_INLINE void operator delete[](void *, void *) {}
  300. #if defined(B3_USE_DOUBLE_PRECISION) || defined(B3_FORCE_DOUBLE_FUNCTIONS)
  301. B3_FORCE_INLINE b3Scalar b3Sqrt(b3Scalar x)
  302. {
  303. return sqrt(x);
  304. }
  305. B3_FORCE_INLINE b3Scalar b3Fabs(b3Scalar x) { return fabs(x); }
  306. B3_FORCE_INLINE b3Scalar b3Cos(b3Scalar x) { return cos(x); }
  307. B3_FORCE_INLINE b3Scalar b3Sin(b3Scalar x) { return sin(x); }
  308. B3_FORCE_INLINE b3Scalar b3Tan(b3Scalar x) { return tan(x); }
  309. B3_FORCE_INLINE b3Scalar b3Acos(b3Scalar x)
  310. {
  311. if (x < b3Scalar(-1)) x = b3Scalar(-1);
  312. if (x > b3Scalar(1)) x = b3Scalar(1);
  313. return acos(x);
  314. }
  315. B3_FORCE_INLINE b3Scalar b3Asin(b3Scalar x)
  316. {
  317. if (x < b3Scalar(-1)) x = b3Scalar(-1);
  318. if (x > b3Scalar(1)) x = b3Scalar(1);
  319. return asin(x);
  320. }
  321. B3_FORCE_INLINE b3Scalar b3Atan(b3Scalar x) { return atan(x); }
  322. B3_FORCE_INLINE b3Scalar b3Atan2(b3Scalar x, b3Scalar y) { return atan2(x, y); }
  323. B3_FORCE_INLINE b3Scalar b3Exp(b3Scalar x) { return exp(x); }
  324. B3_FORCE_INLINE b3Scalar b3Log(b3Scalar x) { return log(x); }
  325. B3_FORCE_INLINE b3Scalar b3Pow(b3Scalar x, b3Scalar y) { return pow(x, y); }
  326. B3_FORCE_INLINE b3Scalar b3Fmod(b3Scalar x, b3Scalar y) { return fmod(x, y); }
  327. #else
  328. B3_FORCE_INLINE b3Scalar b3Sqrt(b3Scalar y)
  329. {
  330. #ifdef USE_APPROXIMATION
  331. double x, z, tempf;
  332. unsigned long *tfptr = ((unsigned long *)&tempf) + 1;
  333. tempf = y;
  334. *tfptr = (0xbfcdd90a - *tfptr) >> 1; /* estimate of 1/sqrt(y) */
  335. x = tempf;
  336. z = y * b3Scalar(0.5);
  337. x = (b3Scalar(1.5) * x) - (x * x) * (x * z); /* iteration formula */
  338. x = (b3Scalar(1.5) * x) - (x * x) * (x * z);
  339. x = (b3Scalar(1.5) * x) - (x * x) * (x * z);
  340. x = (b3Scalar(1.5) * x) - (x * x) * (x * z);
  341. x = (b3Scalar(1.5) * x) - (x * x) * (x * z);
  342. return x * y;
  343. #else
  344. return sqrtf(y);
  345. #endif
  346. }
  347. B3_FORCE_INLINE b3Scalar b3Fabs(b3Scalar x) { return fabsf(x); }
  348. B3_FORCE_INLINE b3Scalar b3Cos(b3Scalar x) { return cosf(x); }
  349. B3_FORCE_INLINE b3Scalar b3Sin(b3Scalar x) { return sinf(x); }
  350. B3_FORCE_INLINE b3Scalar b3Tan(b3Scalar x) { return tanf(x); }
  351. B3_FORCE_INLINE b3Scalar b3Acos(b3Scalar x)
  352. {
  353. if (x < b3Scalar(-1))
  354. x = b3Scalar(-1);
  355. if (x > b3Scalar(1))
  356. x = b3Scalar(1);
  357. return acosf(x);
  358. }
  359. B3_FORCE_INLINE b3Scalar b3Asin(b3Scalar x)
  360. {
  361. if (x < b3Scalar(-1))
  362. x = b3Scalar(-1);
  363. if (x > b3Scalar(1))
  364. x = b3Scalar(1);
  365. return asinf(x);
  366. }
  367. B3_FORCE_INLINE b3Scalar b3Atan(b3Scalar x) { return atanf(x); }
  368. B3_FORCE_INLINE b3Scalar b3Atan2(b3Scalar x, b3Scalar y) { return atan2f(x, y); }
  369. B3_FORCE_INLINE b3Scalar b3Exp(b3Scalar x) { return expf(x); }
  370. B3_FORCE_INLINE b3Scalar b3Log(b3Scalar x) { return logf(x); }
  371. B3_FORCE_INLINE b3Scalar b3Pow(b3Scalar x, b3Scalar y) { return powf(x, y); }
  372. B3_FORCE_INLINE b3Scalar b3Fmod(b3Scalar x, b3Scalar y) { return fmodf(x, y); }
  373. #endif
  374. #define B3_2_PI b3Scalar(6.283185307179586232)
  375. #define B3_PI (B3_2_PI * b3Scalar(0.5))
  376. #define B3_HALF_PI (B3_2_PI * b3Scalar(0.25))
  377. #define B3_RADS_PER_DEG (B3_2_PI / b3Scalar(360.0))
  378. #define B3_DEGS_PER_RAD (b3Scalar(360.0) / B3_2_PI)
  379. #define B3_SQRT12 b3Scalar(0.7071067811865475244008443621048490)
  380. #define b3RecipSqrt(x) ((b3Scalar)(b3Scalar(1.0) / b3Sqrt(b3Scalar(x)))) /* reciprocal square root */
  381. #ifdef B3_USE_DOUBLE_PRECISION
  382. #define B3_EPSILON DBL_EPSILON
  383. #define B3_INFINITY DBL_MAX
  384. #else
  385. #define B3_EPSILON FLT_EPSILON
  386. #define B3_INFINITY FLT_MAX
  387. #endif
  388. B3_FORCE_INLINE b3Scalar b3Atan2Fast(b3Scalar y, b3Scalar x)
  389. {
  390. b3Scalar coeff_1 = B3_PI / 4.0f;
  391. b3Scalar coeff_2 = 3.0f * coeff_1;
  392. b3Scalar abs_y = b3Fabs(y);
  393. b3Scalar angle;
  394. if (x >= 0.0f)
  395. {
  396. b3Scalar r = (x - abs_y) / (x + abs_y);
  397. angle = coeff_1 - coeff_1 * r;
  398. }
  399. else
  400. {
  401. b3Scalar r = (x + abs_y) / (abs_y - x);
  402. angle = coeff_2 - coeff_1 * r;
  403. }
  404. return (y < 0.0f) ? -angle : angle;
  405. }
  406. B3_FORCE_INLINE bool b3FuzzyZero(b3Scalar x) { return b3Fabs(x) < B3_EPSILON; }
  407. B3_FORCE_INLINE bool b3Equal(b3Scalar a, b3Scalar eps)
  408. {
  409. return (((a) <= eps) && !((a) < -eps));
  410. }
  411. B3_FORCE_INLINE bool b3GreaterEqual(b3Scalar a, b3Scalar eps)
  412. {
  413. return (!((a) <= eps));
  414. }
  415. B3_FORCE_INLINE int b3IsNegative(b3Scalar x)
  416. {
  417. return x < b3Scalar(0.0) ? 1 : 0;
  418. }
  419. B3_FORCE_INLINE b3Scalar b3Radians(b3Scalar x) { return x * B3_RADS_PER_DEG; }
  420. B3_FORCE_INLINE b3Scalar b3Degrees(b3Scalar x) { return x * B3_DEGS_PER_RAD; }
  421. #define B3_DECLARE_HANDLE(name) \
  422. typedef struct name##__ \
  423. { \
  424. int unused; \
  425. } * name
  426. #ifndef b3Fsel
  427. B3_FORCE_INLINE b3Scalar b3Fsel(b3Scalar a, b3Scalar b, b3Scalar c)
  428. {
  429. return a >= 0 ? b : c;
  430. }
  431. #endif
  432. #define b3Fsels(a, b, c) (b3Scalar) b3Fsel(a, b, c)
  433. B3_FORCE_INLINE bool b3MachineIsLittleEndian()
  434. {
  435. long int i = 1;
  436. const char *p = (const char *)&i;
  437. if (p[0] == 1) // Lowest address contains the least significant byte
  438. return true;
  439. else
  440. return false;
  441. }
  442. ///b3Select avoids branches, which makes performance much better for consoles like Playstation 3 and XBox 360
  443. ///Thanks Phil Knight. See also http://www.cellperformance.com/articles/2006/04/more_techniques_for_eliminatin_1.html
  444. B3_FORCE_INLINE unsigned b3Select(unsigned condition, unsigned valueIfConditionNonZero, unsigned valueIfConditionZero)
  445. {
  446. // Set testNz to 0xFFFFFFFF if condition is nonzero, 0x00000000 if condition is zero
  447. // Rely on positive value or'ed with its negative having sign bit on
  448. // and zero value or'ed with its negative (which is still zero) having sign bit off
  449. // Use arithmetic shift right, shifting the sign bit through all 32 bits
  450. unsigned testNz = (unsigned)(((int)condition | -(int)condition) >> 31);
  451. unsigned testEqz = ~testNz;
  452. return ((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz));
  453. }
  454. B3_FORCE_INLINE int b3Select(unsigned condition, int valueIfConditionNonZero, int valueIfConditionZero)
  455. {
  456. unsigned testNz = (unsigned)(((int)condition | -(int)condition) >> 31);
  457. unsigned testEqz = ~testNz;
  458. return static_cast<int>((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz));
  459. }
  460. B3_FORCE_INLINE float b3Select(unsigned condition, float valueIfConditionNonZero, float valueIfConditionZero)
  461. {
  462. #ifdef B3_HAVE_NATIVE_FSEL
  463. return (float)b3Fsel((b3Scalar)condition - b3Scalar(1.0f), valueIfConditionNonZero, valueIfConditionZero);
  464. #else
  465. return (condition != 0) ? valueIfConditionNonZero : valueIfConditionZero;
  466. #endif
  467. }
  468. template <typename T>
  469. B3_FORCE_INLINE void b3Swap(T &a, T &b)
  470. {
  471. T tmp = a;
  472. a = b;
  473. b = tmp;
  474. }
  475. //PCK: endian swapping functions
  476. B3_FORCE_INLINE unsigned b3SwapEndian(unsigned val)
  477. {
  478. return (((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) | ((val & 0x0000ff00) << 8) | ((val & 0x000000ff) << 24));
  479. }
  480. B3_FORCE_INLINE unsigned short b3SwapEndian(unsigned short val)
  481. {
  482. return static_cast<unsigned short>(((val & 0xff00) >> 8) | ((val & 0x00ff) << 8));
  483. }
  484. B3_FORCE_INLINE unsigned b3SwapEndian(int val)
  485. {
  486. return b3SwapEndian((unsigned)val);
  487. }
  488. B3_FORCE_INLINE unsigned short b3SwapEndian(short val)
  489. {
  490. return b3SwapEndian((unsigned short)val);
  491. }
  492. ///b3SwapFloat uses using char pointers to swap the endianness
  493. ////b3SwapFloat/b3SwapDouble will NOT return a float, because the machine might 'correct' invalid floating point values
  494. ///Not all values of sign/exponent/mantissa are valid floating point numbers according to IEEE 754.
  495. ///When a floating point unit is faced with an invalid value, it may actually change the value, or worse, throw an exception.
  496. ///In most systems, running user mode code, you wouldn't get an exception, but instead the hardware/os/runtime will 'fix' the number for you.
  497. ///so instead of returning a float/double, we return integer/long long integer
  498. B3_FORCE_INLINE unsigned int b3SwapEndianFloat(float d)
  499. {
  500. unsigned int a = 0;
  501. unsigned char *dst = (unsigned char *)&a;
  502. unsigned char *src = (unsigned char *)&d;
  503. dst[0] = src[3];
  504. dst[1] = src[2];
  505. dst[2] = src[1];
  506. dst[3] = src[0];
  507. return a;
  508. }
  509. // unswap using char pointers
  510. B3_FORCE_INLINE float b3UnswapEndianFloat(unsigned int a)
  511. {
  512. float d = 0.0f;
  513. unsigned char *src = (unsigned char *)&a;
  514. unsigned char *dst = (unsigned char *)&d;
  515. dst[0] = src[3];
  516. dst[1] = src[2];
  517. dst[2] = src[1];
  518. dst[3] = src[0];
  519. return d;
  520. }
  521. // swap using char pointers
  522. B3_FORCE_INLINE void b3SwapEndianDouble(double d, unsigned char *dst)
  523. {
  524. unsigned char *src = (unsigned char *)&d;
  525. dst[0] = src[7];
  526. dst[1] = src[6];
  527. dst[2] = src[5];
  528. dst[3] = src[4];
  529. dst[4] = src[3];
  530. dst[5] = src[2];
  531. dst[6] = src[1];
  532. dst[7] = src[0];
  533. }
  534. // unswap using char pointers
  535. B3_FORCE_INLINE double b3UnswapEndianDouble(const unsigned char *src)
  536. {
  537. double d = 0.0;
  538. unsigned char *dst = (unsigned char *)&d;
  539. dst[0] = src[7];
  540. dst[1] = src[6];
  541. dst[2] = src[5];
  542. dst[3] = src[4];
  543. dst[4] = src[3];
  544. dst[5] = src[2];
  545. dst[6] = src[1];
  546. dst[7] = src[0];
  547. return d;
  548. }
  549. // returns normalized value in range [-B3_PI, B3_PI]
  550. B3_FORCE_INLINE b3Scalar b3NormalizeAngle(b3Scalar angleInRadians)
  551. {
  552. angleInRadians = b3Fmod(angleInRadians, B3_2_PI);
  553. if (angleInRadians < -B3_PI)
  554. {
  555. return angleInRadians + B3_2_PI;
  556. }
  557. else if (angleInRadians > B3_PI)
  558. {
  559. return angleInRadians - B3_2_PI;
  560. }
  561. else
  562. {
  563. return angleInRadians;
  564. }
  565. }
  566. ///rudimentary class to provide type info
  567. struct b3TypedObject
  568. {
  569. b3TypedObject(int objectType)
  570. : m_objectType(objectType)
  571. {
  572. }
  573. int m_objectType;
  574. inline int getObjectType() const
  575. {
  576. return m_objectType;
  577. }
  578. };
  579. ///align a pointer to the provided alignment, upwards
  580. template <typename T>
  581. T *b3AlignPointer(T *unalignedPtr, size_t alignment)
  582. {
  583. struct b3ConvertPointerSizeT
  584. {
  585. union {
  586. T *ptr;
  587. size_t integer;
  588. };
  589. };
  590. b3ConvertPointerSizeT converter;
  591. const size_t bit_mask = ~(alignment - 1);
  592. converter.ptr = unalignedPtr;
  593. converter.integer += alignment - 1;
  594. converter.integer &= bit_mask;
  595. return converter.ptr;
  596. }
  597. #endif //B3_SCALAR_H