b3Scalar.h 20 KB

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