btScalar.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. Copyright (c) 2003-2009 Erwin Coumans http://bullet.googlecode.com
  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 BT_SCALAR_H
  13. #define BT_SCALAR_H
  14. #ifdef BT_MANAGED_CODE
  15. //Aligned data types not supported in managed code
  16. #pragma unmanaged
  17. #endif
  18. #include <float.h>
  19. #include <math.h>
  20. #include <stdlib.h> //size_t for MSVC 6.0
  21. /* SVN $Revision$ on $Date$ from http://bullet.googlecode.com*/
  22. #define BT_BULLET_VERSION 279
  23. inline int btGetVersion()
  24. {
  25. return BT_BULLET_VERSION;
  26. }
  27. #if defined(DEBUG) || defined(_DEBUG)
  28. #define BT_DEBUG
  29. #endif
  30. #ifdef _WIN32
  31. #if defined(__MINGW32__) || defined(__CYGWIN__) || (defined(_MSC_VER) && _MSC_VER < 1300)
  32. #define SIMD_FORCE_INLINE inline
  33. #define ATTRIBUTE_ALIGNED16(a) a
  34. #define ATTRIBUTE_ALIGNED64(a) a
  35. #define ATTRIBUTE_ALIGNED128(a) a
  36. #else
  37. //#define BT_HAS_ALIGNED_ALLOCATOR
  38. #pragma warning(disable : 4324) // disable padding warning
  39. // #pragma warning(disable:4530) // Disable the exception disable but used in MSCV Stl warning.
  40. // #pragma warning(disable:4996) //Turn off warnings about deprecated C routines
  41. // #pragma warning(disable:4786) // Disable the "debug name too long" warning
  42. #define SIMD_FORCE_INLINE __forceinline
  43. #define ATTRIBUTE_ALIGNED16(a) __declspec(align(16)) a
  44. #define ATTRIBUTE_ALIGNED64(a) __declspec(align(64)) a
  45. #define ATTRIBUTE_ALIGNED128(a) __declspec(align(128)) a
  46. #ifdef _XBOX
  47. #define BT_USE_VMX128
  48. #include <ppcintrinsics.h>
  49. #define BT_HAVE_NATIVE_FSEL
  50. #define btFsel(a, b, c) __fsel((a), (b), (c))
  51. #else
  52. #if (defined(_WIN32) && (_MSC_VER) && _MSC_VER >= 1400) && (!defined(BT_USE_DOUBLE_PRECISION))
  53. #define BT_USE_SSE
  54. #include <emmintrin.h>
  55. #endif
  56. #endif //_XBOX
  57. #endif //__MINGW32__
  58. #include <assert.h>
  59. #ifdef BT_DEBUG
  60. #define btAssert assert
  61. #else
  62. #define btAssert(x)
  63. #endif
  64. //btFullAssert is optional, slows down a lot
  65. #define btFullAssert(x)
  66. #define btLikely(_c) _c
  67. #define btUnlikely(_c) _c
  68. #else
  69. #if defined(__CELLOS_LV2__)
  70. #define SIMD_FORCE_INLINE inline __attribute__((always_inline))
  71. #define ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
  72. #define ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
  73. #define ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
  74. #ifndef assert
  75. #include <assert.h>
  76. #endif
  77. #ifdef BT_DEBUG
  78. #ifdef __SPU__
  79. #include <spu_printf.h>
  80. #define printf spu_printf
  81. #define btAssert(x) \
  82. { \
  83. if (!(x)) { \
  84. printf("Assert " __FILE__ ":%u (" #x ")\n", __LINE__); \
  85. spu_hcmpeq(0, 0); \
  86. } \
  87. }
  88. #else
  89. #define btAssert assert
  90. #endif
  91. #else
  92. #define btAssert(x)
  93. #endif
  94. //btFullAssert is optional, slows down a lot
  95. #define btFullAssert(x)
  96. #define btLikely(_c) _c
  97. #define btUnlikely(_c) _c
  98. #else
  99. #ifdef USE_LIBSPE2
  100. #define SIMD_FORCE_INLINE __inline
  101. #define ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
  102. #define ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
  103. #define ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
  104. #ifndef assert
  105. #include <assert.h>
  106. #endif
  107. #ifdef BT_DEBUG
  108. #define btAssert assert
  109. #else
  110. #define btAssert(x)
  111. #endif
  112. //btFullAssert is optional, slows down a lot
  113. #define btFullAssert(x)
  114. #define btLikely(_c) __builtin_expect((_c), 1)
  115. #define btUnlikely(_c) __builtin_expect((_c), 0)
  116. #else
  117. //non-windows systems
  118. #if (defined(__APPLE__) && defined(__i386__) && (!defined(BT_USE_DOUBLE_PRECISION)))
  119. #define BT_USE_SSE
  120. #include <emmintrin.h>
  121. #define SIMD_FORCE_INLINE inline
  122. ///@todo: check out alignment methods for other platforms/compilers
  123. #define ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
  124. #define ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
  125. #define ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
  126. #ifndef assert
  127. #include <assert.h>
  128. #endif
  129. #if defined(DEBUG) || defined(_DEBUG)
  130. #define btAssert assert
  131. #else
  132. #define btAssert(x)
  133. #endif
  134. //btFullAssert is optional, slows down a lot
  135. #define btFullAssert(x)
  136. #define btLikely(_c) _c
  137. #define btUnlikely(_c) _c
  138. #else
  139. #define SIMD_FORCE_INLINE inline
  140. ///@todo: check out alignment methods for other platforms/compilers
  141. ///#define ATTRIBUTE_ALIGNED16(a) a __attribute__ ((aligned (16)))
  142. ///#define ATTRIBUTE_ALIGNED64(a) a __attribute__ ((aligned (64)))
  143. ///#define ATTRIBUTE_ALIGNED128(a) a __attribute__ ((aligned (128)))
  144. #define ATTRIBUTE_ALIGNED16(a) a
  145. #define ATTRIBUTE_ALIGNED64(a) a
  146. #define ATTRIBUTE_ALIGNED128(a) a
  147. #ifndef assert
  148. #include <assert.h>
  149. #endif
  150. #if defined(DEBUG) || defined(_DEBUG)
  151. #define btAssert assert
  152. #else
  153. #define btAssert(x)
  154. #endif
  155. //btFullAssert is optional, slows down a lot
  156. #define btFullAssert(x)
  157. #define btLikely(_c) _c
  158. #define btUnlikely(_c) _c
  159. #endif //__APPLE__
  160. #endif // LIBSPE2
  161. #endif //__CELLOS_LV2__
  162. #endif
  163. ///The btScalar type abstracts floating point numbers, to easily switch between double and single floating point precision.
  164. #if defined(BT_USE_DOUBLE_PRECISION)
  165. typedef double btScalar;
  166. //this number could be bigger in double precision
  167. #define BT_LARGE_FLOAT 1e30
  168. #else
  169. typedef float btScalar;
  170. //keep BT_LARGE_FLOAT*BT_LARGE_FLOAT < FLT_MAX
  171. #define BT_LARGE_FLOAT 1e18f
  172. #endif
  173. #define BT_DECLARE_ALIGNED_ALLOCATOR() \
  174. SIMD_FORCE_INLINE void* operator new(size_t sizeInBytes) { return btAlignedAlloc(sizeInBytes, 16); } \
  175. SIMD_FORCE_INLINE void operator delete(void* ptr) { btAlignedFree(ptr); } \
  176. SIMD_FORCE_INLINE void* operator new(size_t, void* ptr) { return ptr; } \
  177. SIMD_FORCE_INLINE void operator delete(void*, void*) {} \
  178. SIMD_FORCE_INLINE void* operator new[](size_t sizeInBytes) { return btAlignedAlloc(sizeInBytes, 16); } \
  179. SIMD_FORCE_INLINE void operator delete[](void* ptr) { btAlignedFree(ptr); } \
  180. SIMD_FORCE_INLINE void* operator new[](size_t, void* ptr) { return ptr; } \
  181. SIMD_FORCE_INLINE void operator delete[](void*, void*) {}
  182. #if defined(BT_USE_DOUBLE_PRECISION) || defined(BT_FORCE_DOUBLE_FUNCTIONS)
  183. SIMD_FORCE_INLINE btScalar btSqrt(btScalar x)
  184. {
  185. return sqrt(x);
  186. }
  187. SIMD_FORCE_INLINE btScalar btFabs(btScalar x) { return fabs(x); }
  188. SIMD_FORCE_INLINE btScalar btCos(btScalar x) { return cos(x); }
  189. SIMD_FORCE_INLINE btScalar btSin(btScalar x) { return sin(x); }
  190. SIMD_FORCE_INLINE btScalar btTan(btScalar x) { return tan(x); }
  191. SIMD_FORCE_INLINE btScalar btAcos(btScalar x)
  192. {
  193. if (x < btScalar(-1))
  194. x = btScalar(-1);
  195. if (x > btScalar(1))
  196. x = btScalar(1);
  197. return acos(x);
  198. }
  199. SIMD_FORCE_INLINE btScalar btAsin(btScalar x)
  200. {
  201. if (x < btScalar(-1))
  202. x = btScalar(-1);
  203. if (x > btScalar(1))
  204. x = btScalar(1);
  205. return asin(x);
  206. }
  207. SIMD_FORCE_INLINE btScalar btAtan(btScalar x) { return atan(x); }
  208. SIMD_FORCE_INLINE btScalar btAtan2(btScalar x, btScalar y) { return atan2(x, y); }
  209. SIMD_FORCE_INLINE btScalar btExp(btScalar x) { return exp(x); }
  210. SIMD_FORCE_INLINE btScalar btLog(btScalar x) { return log(x); }
  211. SIMD_FORCE_INLINE btScalar btPow(btScalar x, btScalar y) { return pow(x, y); }
  212. SIMD_FORCE_INLINE btScalar btFmod(btScalar x, btScalar y) { return fmod(x, y); }
  213. #else
  214. SIMD_FORCE_INLINE btScalar btSqrt(btScalar y)
  215. {
  216. #ifdef USE_APPROXIMATION
  217. double x, z, tempf;
  218. unsigned long* tfptr = ((unsigned long*)&tempf) + 1;
  219. tempf = y;
  220. *tfptr = (0xbfcdd90a - *tfptr) >> 1; /* estimate of 1/sqrt(y) */
  221. x = tempf;
  222. z = y * btScalar(0.5);
  223. x = (btScalar(1.5) * x) - (x * x) * (x * z); /* iteration formula */
  224. x = (btScalar(1.5) * x) - (x * x) * (x * z);
  225. x = (btScalar(1.5) * x) - (x * x) * (x * z);
  226. x = (btScalar(1.5) * x) - (x * x) * (x * z);
  227. x = (btScalar(1.5) * x) - (x * x) * (x * z);
  228. return x * y;
  229. #else
  230. return sqrtf(y);
  231. #endif
  232. }
  233. SIMD_FORCE_INLINE btScalar btFabs(btScalar x) { return fabsf(x); }
  234. SIMD_FORCE_INLINE btScalar btCos(btScalar x) { return cosf(x); }
  235. SIMD_FORCE_INLINE btScalar btSin(btScalar x) { return sinf(x); }
  236. SIMD_FORCE_INLINE btScalar btTan(btScalar x) { return tanf(x); }
  237. SIMD_FORCE_INLINE btScalar btAcos(btScalar x)
  238. {
  239. if (x < btScalar(-1))
  240. x = btScalar(-1);
  241. if (x > btScalar(1))
  242. x = btScalar(1);
  243. return acosf(x);
  244. }
  245. SIMD_FORCE_INLINE btScalar btAsin(btScalar x)
  246. {
  247. if (x < btScalar(-1))
  248. x = btScalar(-1);
  249. if (x > btScalar(1))
  250. x = btScalar(1);
  251. return asinf(x);
  252. }
  253. SIMD_FORCE_INLINE btScalar btAtan(btScalar x) { return atanf(x); }
  254. SIMD_FORCE_INLINE btScalar btAtan2(btScalar x, btScalar y) { return atan2f(x, y); }
  255. SIMD_FORCE_INLINE btScalar btExp(btScalar x) { return expf(x); }
  256. SIMD_FORCE_INLINE btScalar btLog(btScalar x) { return logf(x); }
  257. SIMD_FORCE_INLINE btScalar btPow(btScalar x, btScalar y) { return powf(x, y); }
  258. SIMD_FORCE_INLINE btScalar btFmod(btScalar x, btScalar y) { return fmodf(x, y); }
  259. #endif
  260. #define SIMD_2_PI btScalar(6.283185307179586232)
  261. #define SIMD_PI (SIMD_2_PI * btScalar(0.5))
  262. #define SIMD_HALF_PI (SIMD_2_PI * btScalar(0.25))
  263. #define SIMD_RADS_PER_DEG (SIMD_2_PI / btScalar(360.0))
  264. #define SIMD_DEGS_PER_RAD (btScalar(360.0) / SIMD_2_PI)
  265. #define SIMDSQRT12 btScalar(0.7071067811865475244008443621048490)
  266. #define btRecipSqrt(x) ((btScalar)(btScalar(1.0) / btSqrt(btScalar(x)))) /* reciprocal square root */
  267. #ifdef BT_USE_DOUBLE_PRECISION
  268. #define SIMD_EPSILON DBL_EPSILON
  269. #define SIMD_INFINITY DBL_MAX
  270. #else
  271. #define SIMD_EPSILON FLT_EPSILON
  272. #define SIMD_INFINITY FLT_MAX
  273. #endif
  274. SIMD_FORCE_INLINE btScalar btAtan2Fast(btScalar y, btScalar x)
  275. {
  276. btScalar coeff_1 = SIMD_PI / 4.0f;
  277. btScalar coeff_2 = 3.0f * coeff_1;
  278. btScalar abs_y = btFabs(y);
  279. btScalar angle;
  280. if (x >= 0.0f) {
  281. btScalar r = (x - abs_y) / (x + abs_y);
  282. angle = coeff_1 - coeff_1 * r;
  283. }
  284. else {
  285. btScalar r = (x + abs_y) / (abs_y - x);
  286. angle = coeff_2 - coeff_1 * r;
  287. }
  288. return (y < 0.0f) ? -angle : angle;
  289. }
  290. SIMD_FORCE_INLINE bool btFuzzyZero(btScalar x) { return btFabs(x) < SIMD_EPSILON; }
  291. SIMD_FORCE_INLINE bool btEqual(btScalar a, btScalar eps)
  292. {
  293. return (((a) <= eps) && !((a) < -eps));
  294. }
  295. SIMD_FORCE_INLINE bool btGreaterEqual(btScalar a, btScalar eps)
  296. {
  297. return (!((a) <= eps));
  298. }
  299. SIMD_FORCE_INLINE int btIsNegative(btScalar x)
  300. {
  301. return x < btScalar(0.0) ? 1 : 0;
  302. }
  303. SIMD_FORCE_INLINE btScalar btRadians(btScalar x) { return x * SIMD_RADS_PER_DEG; }
  304. SIMD_FORCE_INLINE btScalar btDegrees(btScalar x) { return x * SIMD_DEGS_PER_RAD; }
  305. #define BT_DECLARE_HANDLE(name) \
  306. typedef struct name##__ { \
  307. int unused; \
  308. } * name
  309. #ifndef btFsel
  310. SIMD_FORCE_INLINE btScalar btFsel(btScalar a, btScalar b, btScalar c)
  311. {
  312. return a >= 0 ? b : c;
  313. }
  314. #endif
  315. #define btFsels(a, b, c) (btScalar) btFsel(a, b, c)
  316. SIMD_FORCE_INLINE bool btMachineIsLittleEndian()
  317. {
  318. long int i = 1;
  319. const char* p = (const char*)&i;
  320. if (p[0] == 1) // Lowest address contains the least significant byte
  321. return true;
  322. else
  323. return false;
  324. }
  325. ///btSelect avoids branches, which makes performance much better for consoles like Playstation 3 and XBox 360
  326. ///Thanks Phil Knight. See also http://www.cellperformance.com/articles/2006/04/more_techniques_for_eliminatin_1.html
  327. SIMD_FORCE_INLINE unsigned btSelect(unsigned condition, unsigned valueIfConditionNonZero, unsigned valueIfConditionZero)
  328. {
  329. // Set testNz to 0xFFFFFFFF if condition is nonzero, 0x00000000 if condition is zero
  330. // Rely on positive value or'ed with its negative having sign bit on
  331. // and zero value or'ed with its negative (which is still zero) having sign bit off
  332. // Use arithmetic shift right, shifting the sign bit through all 32 bits
  333. unsigned testNz = (unsigned)(((int)condition | -(int)condition) >> 31);
  334. unsigned testEqz = ~testNz;
  335. return ((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz));
  336. }
  337. SIMD_FORCE_INLINE int btSelect(unsigned condition, int valueIfConditionNonZero, int valueIfConditionZero)
  338. {
  339. unsigned testNz = (unsigned)(((int)condition | -(int)condition) >> 31);
  340. unsigned testEqz = ~testNz;
  341. return static_cast<int>((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz));
  342. }
  343. SIMD_FORCE_INLINE float btSelect(unsigned condition, float valueIfConditionNonZero, float valueIfConditionZero)
  344. {
  345. #ifdef BT_HAVE_NATIVE_FSEL
  346. return (float)btFsel((btScalar)condition - btScalar(1.0f), valueIfConditionNonZero, valueIfConditionZero);
  347. #else
  348. return (condition != 0) ? valueIfConditionNonZero : valueIfConditionZero;
  349. #endif
  350. }
  351. template <typename T>
  352. SIMD_FORCE_INLINE void btSwap(T& a, T& b)
  353. {
  354. T tmp = a;
  355. a = b;
  356. b = tmp;
  357. }
  358. //PCK: endian swapping functions
  359. SIMD_FORCE_INLINE unsigned btSwapEndian(unsigned val)
  360. {
  361. return (((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) | ((val & 0x0000ff00) << 8) | ((val & 0x000000ff) << 24));
  362. }
  363. SIMD_FORCE_INLINE unsigned short btSwapEndian(unsigned short val)
  364. {
  365. return static_cast<unsigned short>(((val & 0xff00) >> 8) | ((val & 0x00ff) << 8));
  366. }
  367. SIMD_FORCE_INLINE unsigned btSwapEndian(int val)
  368. {
  369. return btSwapEndian((unsigned)val);
  370. }
  371. SIMD_FORCE_INLINE unsigned short btSwapEndian(short val)
  372. {
  373. return btSwapEndian((unsigned short)val);
  374. }
  375. ///btSwapFloat uses using char pointers to swap the endianness
  376. ////btSwapFloat/btSwapDouble will NOT return a float, because the machine might 'correct' invalid floating point values
  377. ///Not all values of sign/exponent/mantissa are valid floating point numbers according to IEEE 754.
  378. ///When a floating point unit is faced with an invalid value, it may actually change the value, or worse, throw an exception.
  379. ///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.
  380. ///so instead of returning a float/double, we return integer/long long integer
  381. SIMD_FORCE_INLINE unsigned int btSwapEndianFloat(float d)
  382. {
  383. unsigned int a = 0;
  384. unsigned char* dst = (unsigned char*)&a;
  385. unsigned char* src = (unsigned char*)&d;
  386. dst[0] = src[3];
  387. dst[1] = src[2];
  388. dst[2] = src[1];
  389. dst[3] = src[0];
  390. return a;
  391. }
  392. // unswap using char pointers
  393. SIMD_FORCE_INLINE float btUnswapEndianFloat(unsigned int a)
  394. {
  395. float d = 0.0f;
  396. unsigned char* src = (unsigned char*)&a;
  397. unsigned char* dst = (unsigned char*)&d;
  398. dst[0] = src[3];
  399. dst[1] = src[2];
  400. dst[2] = src[1];
  401. dst[3] = src[0];
  402. return d;
  403. }
  404. // swap using char pointers
  405. SIMD_FORCE_INLINE void btSwapEndianDouble(double d, unsigned char* dst)
  406. {
  407. unsigned char* src = (unsigned char*)&d;
  408. dst[0] = src[7];
  409. dst[1] = src[6];
  410. dst[2] = src[5];
  411. dst[3] = src[4];
  412. dst[4] = src[3];
  413. dst[5] = src[2];
  414. dst[6] = src[1];
  415. dst[7] = src[0];
  416. }
  417. // unswap using char pointers
  418. SIMD_FORCE_INLINE double btUnswapEndianDouble(const unsigned char* src)
  419. {
  420. double d = 0.0;
  421. unsigned char* dst = (unsigned char*)&d;
  422. dst[0] = src[7];
  423. dst[1] = src[6];
  424. dst[2] = src[5];
  425. dst[3] = src[4];
  426. dst[4] = src[3];
  427. dst[5] = src[2];
  428. dst[6] = src[1];
  429. dst[7] = src[0];
  430. return d;
  431. }
  432. // returns normalized value in range [-SIMD_PI, SIMD_PI]
  433. SIMD_FORCE_INLINE btScalar btNormalizeAngle(btScalar angleInRadians)
  434. {
  435. angleInRadians = btFmod(angleInRadians, SIMD_2_PI);
  436. if (angleInRadians < -SIMD_PI) {
  437. return angleInRadians + SIMD_2_PI;
  438. }
  439. else if (angleInRadians > SIMD_PI) {
  440. return angleInRadians - SIMD_2_PI;
  441. }
  442. else {
  443. return angleInRadians;
  444. }
  445. }
  446. ///rudimentary class to provide type info
  447. struct btTypedObject {
  448. btTypedObject(int objectType)
  449. : m_objectType(objectType)
  450. {
  451. }
  452. int m_objectType;
  453. inline int getObjectType() const
  454. {
  455. return m_objectType;
  456. }
  457. };
  458. #endif //BT_SCALAR_H