b3Scalar.h 20 KB

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