Debian Codegraph @

thirdparty/vhacd/inc/btScalar.h

raw symbols SCIP
1 /*
2 Copyright (c) 2003-2009 Erwin Coumans http://bullet.googlecode.com
3
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
9
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
13 */
14
15 #ifndef BT_SCALAR_H
16 #define BT_SCALAR_H
17
18 #ifdef BT_MANAGED_CODE
19 //Aligned data types not supported in managed code
20 #pragma unmanaged
21 #endif
22
23 #include <float.h>
24 #include <math.h>
25 #include <stdlib.h> //size_t for MSVC 6.0
26 #include <stdint.h>
27
28 /* SVN $Revision$ on $Date$ from http://bullet.googlecode.com*/
29 #define BT_BULLET_VERSION 279
30
31 namespace VHACD {
32
33 inline int32_t btGetVersion()
34 {
35 return BT_BULLET_VERSION;
36 }
37
38 }; // namespace VHACD
39
40 #if defined(DEBUG) || defined(_DEBUG)
41 #define BT_DEBUG
42 #endif
43
44 #ifdef _WIN32
45
46 #if defined(__MINGW32__) || defined(__CYGWIN__) || (defined(_MSC_VER) && _MSC_VER < 1300)
47
48 #define SIMD_FORCE_INLINE inline
49 #define ATTRIBUTE_ALIGNED16(a) a
50 #define ATTRIBUTE_ALIGNED64(a) a
51 #define ATTRIBUTE_ALIGNED128(a) a
52 #else
53 //#define BT_HAS_ALIGNED_ALLOCATOR
54 #pragma warning(disable : 4324) // disable padding warning
55 // #pragma warning(disable:4530) // Disable the exception disable but used in MSCV Stl warning.
56 // #pragma warning(disable:4996) //Turn off warnings about deprecated C routines
57 // #pragma warning(disable:4786) // Disable the "debug name too long" warning
58
59 #define SIMD_FORCE_INLINE __forceinline
60 #define ATTRIBUTE_ALIGNED16(a) __declspec(align(16)) a
61 #define ATTRIBUTE_ALIGNED64(a) __declspec(align(64)) a
62 #define ATTRIBUTE_ALIGNED128(a) __declspec(align(128)) a
63 #ifdef _XBOX
64 #define BT_USE_VMX128
65
66 #include <ppcintrinsics.h>
67 #define BT_HAVE_NATIVE_FSEL
68 #define btFsel(a, b, c) __fsel((a), (b), (c))
69 #else
70
71 //#if (defined(_WIN32) && (_MSC_VER) && _MSC_VER >= 1400) && (!defined(BT_USE_DOUBLE_PRECISION))
72 #if (defined(_WIN32) && (_MSC_VER) && _MSC_VER >= 1400) && (!defined(BT_USE_DOUBLE_PRECISION)) && (!defined(_M_ARM)) && (!defined(_M_ARM64))
73 #define BT_USE_SSE
74 #include <emmintrin.h>
75 #endif
76
77 #endif //_XBOX
78
79 #endif //__MINGW32__
80
81 #include <assert.h>
82 #ifdef BT_DEBUG
83 #define btAssert assert
84 #else
85 #define btAssert(x)
86 #endif
87 //btFullAssert is optional, slows down a lot
88 #define btFullAssert(x)
89
90 #define btLikely(_c) _c
91 #define btUnlikely(_c) _c
92
93 #else
94
95 #if defined(__CELLOS_LV2__)
96 #define SIMD_FORCE_INLINE inline __attribute__((always_inline))
97 #define ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
98 #define ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
99 #define ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
100 #ifndef assert
101 #include <assert.h>
102 #endif
103 #ifdef BT_DEBUG
104 #ifdef __SPU__
105 #include <spu_printf.h>
106 #define printf spu_printf
107 #define btAssert(x) \
108 { \
109 if (!(x)) { \
110 printf("Assert " __FILE__ ":%u (" #x ")\n", __LINE__); \
111 spu_hcmpeq(0, 0); \
112 } \
113 }
114 #else
115 #define btAssert assert
116 #endif
117
118 #else
119 #define btAssert(x)
120 #endif
121 //btFullAssert is optional, slows down a lot
122 #define btFullAssert(x)
123
124 #define btLikely(_c) _c
125 #define btUnlikely(_c) _c
126
127 #else
128
129 #ifdef USE_LIBSPE2
130
131 #define SIMD_FORCE_INLINE __inline
132 #define ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
133 #define ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
134 #define ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
135 #ifndef assert
136 #include <assert.h>
137 #endif
138 #ifdef BT_DEBUG
139 #define btAssert assert
140 #else
141 #define btAssert(x)
142 #endif
143 //btFullAssert is optional, slows down a lot
144 #define btFullAssert(x)
145
146 #define btLikely(_c) __builtin_expect((_c), 1)
147 #define btUnlikely(_c) __builtin_expect((_c), 0)
148
149 #else
150 //non-windows systems
151
152 #if (defined(__APPLE__) && defined(__i386__) && (!defined(BT_USE_DOUBLE_PRECISION)))
153 #define BT_USE_SSE
154 #include <emmintrin.h>
155
156 #define SIMD_FORCE_INLINE inline
157 ///@todo: check out alignment methods for other platforms/compilers
158 #define ATTRIBUTE_ALIGNED16(a) a __attribute__((aligned(16)))
159 #define ATTRIBUTE_ALIGNED64(a) a __attribute__((aligned(64)))
160 #define ATTRIBUTE_ALIGNED128(a) a __attribute__((aligned(128)))
161 #ifndef assert
162 #include <assert.h>
163 #endif
164
165 #if defined(DEBUG) || defined(_DEBUG)
166 #define btAssert assert
167 #else
168 #define btAssert(x)
169 #endif
170
171 //btFullAssert is optional, slows down a lot
172 #define btFullAssert(x)
173 #define btLikely(_c) _c
174 #define btUnlikely(_c) _c
175
176 #else
177
178 #define SIMD_FORCE_INLINE inline
179 ///@todo: check out alignment methods for other platforms/compilers
180 ///#define ATTRIBUTE_ALIGNED16(a) a __attribute__ ((aligned (16)))
181 ///#define ATTRIBUTE_ALIGNED64(a) a __attribute__ ((aligned (64)))
182 ///#define ATTRIBUTE_ALIGNED128(a) a __attribute__ ((aligned (128)))
183 #define ATTRIBUTE_ALIGNED16(a) a
184 #define ATTRIBUTE_ALIGNED64(a) a
185 #define ATTRIBUTE_ALIGNED128(a) a
186 #ifndef assert
187 #include <assert.h>
188 #endif
189
190 #if defined(DEBUG) || defined(_DEBUG)
191 #define btAssert assert
192 #else
193 #define btAssert(x)
194 #endif
195
196 //btFullAssert is optional, slows down a lot
197 #define btFullAssert(x)
198 #define btLikely(_c) _c
199 #define btUnlikely(_c) _c
200 #endif //__APPLE__
201
202 #endif // LIBSPE2
203
204 #endif //__CELLOS_LV2__
205 #endif
206
207 namespace VHACD {
208
209 ///The btScalar type abstracts floating point numbers, to easily switch between double and single floating point precision.
210 #if defined(BT_USE_DOUBLE_PRECISION)
211 typedef double btScalar;
212 //this number could be bigger in double precision
213 #define BT_LARGE_FLOAT 1e30
214 #else
215 typedef float btScalar;
216 //keep BT_LARGE_FLOAT*BT_LARGE_FLOAT < FLT_MAX
217 #define BT_LARGE_FLOAT 1e18f
218 #endif
219
220 #define BT_DECLARE_ALIGNED_ALLOCATOR() \
221 SIMD_FORCE_INLINE void* operator new(size_t sizeInBytes) { return btAlignedAlloc(sizeInBytes, 16); } \
222 SIMD_FORCE_INLINE void operator delete(void* ptr) { btAlignedFree(ptr); } \
223 SIMD_FORCE_INLINE void* operator new(size_t, void* ptr) { return ptr; } \
224 SIMD_FORCE_INLINE void operator delete(void*, void*) {} \
225 SIMD_FORCE_INLINE void* operator new[](size_t sizeInBytes) { return btAlignedAlloc(sizeInBytes, 16); } \
226 SIMD_FORCE_INLINE void operator delete[](void* ptr) { btAlignedFree(ptr); } \
227 SIMD_FORCE_INLINE void* operator new[](size_t, void* ptr) { return ptr; } \
228 SIMD_FORCE_INLINE void operator delete[](void*, void*) {}
229
230 #if defined(BT_USE_DOUBLE_PRECISION) || defined(BT_FORCE_DOUBLE_FUNCTIONS)
231
232 SIMD_FORCE_INLINE btScalar btSqrt(btScalar x)
233 {
234 return sqrt(x);
235 }
236 SIMD_FORCE_INLINE btScalar btFabs(btScalar x) { return fabs(x); }
237 SIMD_FORCE_INLINE btScalar btCos(btScalar x) { return cos(x); }
238 SIMD_FORCE_INLINE btScalar btSin(btScalar x) { return sin(x); }
239 SIMD_FORCE_INLINE btScalar btTan(btScalar x) { return tan(x); }
240 SIMD_FORCE_INLINE btScalar btAcos(btScalar x)
241 {
242 if (x < btScalar(-1))
243 x = btScalar(-1);
244 if (x > btScalar(1))
245 x = btScalar(1);
246 return acos(x);
247 }
248 SIMD_FORCE_INLINE btScalar btAsin(btScalar x)
249 {
250 if (x < btScalar(-1))
251 x = btScalar(-1);
252 if (x > btScalar(1))
253 x = btScalar(1);
254 return asin(x);
255 }
256 SIMD_FORCE_INLINE btScalar btAtan(btScalar x) { return atan(x); }
257 SIMD_FORCE_INLINE btScalar btAtan2(btScalar x, btScalar y) { return atan2(x, y); }
258 SIMD_FORCE_INLINE btScalar btExp(btScalar x) { return exp(x); }
259 SIMD_FORCE_INLINE btScalar btLog(btScalar x) { return log(x); }
260 SIMD_FORCE_INLINE btScalar btPow(btScalar x, btScalar y) { return pow(x, y); }
261 SIMD_FORCE_INLINE btScalar btFmod(btScalar x, btScalar y) { return fmod(x, y); }
262
263 #else
264
265 SIMD_FORCE_INLINE btScalar btSqrt(btScalar y)
266 {
267 #ifdef USE_APPROXIMATION
268 double x, z, tempf;
269 unsigned long* tfptr = ((unsigned long*)&tempf) + 1;
270
271 tempf = y;
272 *tfptr = (0xbfcdd90a - *tfptr) >> 1; /* estimate of 1/sqrt(y) */
273 x = tempf;
274 z = y * btScalar(0.5);
275 x = (btScalar(1.5) * x) - (x * x) * (x * z); /* iteration formula */
276 x = (btScalar(1.5) * x) - (x * x) * (x * z);
277 x = (btScalar(1.5) * x) - (x * x) * (x * z);
278 x = (btScalar(1.5) * x) - (x * x) * (x * z);
279 x = (btScalar(1.5) * x) - (x * x) * (x * z);
280 return x * y;
281 #else
282 return sqrtf(y);
283 #endif
284 }
285 SIMD_FORCE_INLINE btScalar btFabs(btScalar x) { return fabsf(x); }
286 SIMD_FORCE_INLINE btScalar btCos(btScalar x) { return cosf(x); }
287 SIMD_FORCE_INLINE btScalar btSin(btScalar x) { return sinf(x); }
288 SIMD_FORCE_INLINE btScalar btTan(btScalar x) { return tanf(x); }
289 SIMD_FORCE_INLINE btScalar btAcos(btScalar x)
290 {
291 if (x < btScalar(-1))
292 x = btScalar(-1);
293 if (x > btScalar(1))
294 x = btScalar(1);
295 return acosf(x);
296 }
297 SIMD_FORCE_INLINE btScalar btAsin(btScalar x)
298 {
299 if (x < btScalar(-1))
300 x = btScalar(-1);
301 if (x > btScalar(1))
302 x = btScalar(1);
303 return asinf(x);
304 }
305 SIMD_FORCE_INLINE btScalar btAtan(btScalar x) { return atanf(x); }
306 SIMD_FORCE_INLINE btScalar btAtan2(btScalar x, btScalar y) { return atan2f(x, y); }
307 SIMD_FORCE_INLINE btScalar btExp(btScalar x) { return expf(x); }
308 SIMD_FORCE_INLINE btScalar btLog(btScalar x) { return logf(x); }
309 SIMD_FORCE_INLINE btScalar btPow(btScalar x, btScalar y) { return powf(x, y); }
310 SIMD_FORCE_INLINE btScalar btFmod(btScalar x, btScalar y) { return fmodf(x, y); }
311
312 #endif
313
314 #define SIMD_2_PI btScalar(6.283185307179586232)
315 #define SIMD_PI (SIMD_2_PI * btScalar(0.5))
316 #define SIMD_HALF_PI (SIMD_2_PI * btScalar(0.25))
317 #define SIMD_RADS_PER_DEG (SIMD_2_PI / btScalar(360.0))
318 #define SIMD_DEGS_PER_RAD (btScalar(360.0) / SIMD_2_PI)
319 #define SIMDSQRT12 btScalar(0.7071067811865475244008443621048490)
320
321 #define btRecipSqrt(x) ((btScalar)(btScalar(1.0) / btSqrt(btScalar(x)))) /* reciprocal square root */
322
323 #ifdef BT_USE_DOUBLE_PRECISION
324 #define SIMD_EPSILON DBL_EPSILON
325 #define SIMD_INFINITY DBL_MAX
326 #else
327 #define SIMD_EPSILON FLT_EPSILON
328 #define SIMD_INFINITY FLT_MAX
329 #endif
330
331 SIMD_FORCE_INLINE btScalar btAtan2Fast(btScalar y, btScalar x)
332 {
333 btScalar coeff_1 = SIMD_PI / 4.0f;
334 btScalar coeff_2 = 3.0f * coeff_1;
335 btScalar abs_y = btFabs(y);
336 btScalar angle;
337 if (x >= 0.0f) {
338 btScalar r = (x - abs_y) / (x + abs_y);
339 angle = coeff_1 - coeff_1 * r;
340 }
341 else {
342 btScalar r = (x + abs_y) / (abs_y - x);
343 angle = coeff_2 - coeff_1 * r;
344 }
345 return (y < 0.0f) ? -angle : angle;
346 }
347
348 SIMD_FORCE_INLINE bool btFuzzyZero(btScalar x) { return btFabs(x) < SIMD_EPSILON; }
349
350 SIMD_FORCE_INLINE bool btEqual(btScalar a, btScalar eps)
351 {
352 return (((a) <= eps) && !((a) < -eps));
353 }
354 SIMD_FORCE_INLINE bool btGreaterEqual(btScalar a, btScalar eps)
355 {
356 return (!((a) <= eps));
357 }
358
359 SIMD_FORCE_INLINE int32_t btIsNegative(btScalar x)
360 {
361 return x < btScalar(0.0) ? 1 : 0;
362 }
363
364 SIMD_FORCE_INLINE btScalar btRadians(btScalar x) { return x * SIMD_RADS_PER_DEG; }
365 SIMD_FORCE_INLINE btScalar btDegrees(btScalar x) { return x * SIMD_DEGS_PER_RAD; }
366
367 #define BT_DECLARE_HANDLE(name) \
368 typedef struct name##__ { \
369 int32_t unused; \
370 } * name
371
372 #ifndef btFsel
373 SIMD_FORCE_INLINE btScalar btFsel(btScalar a, btScalar b, btScalar c)
374 {
375 return a >= 0 ? b : c;
376 }
377 #endif
378 #define btFsels(a, b, c) (btScalar) btFsel(a, b, c)
379
380 SIMD_FORCE_INLINE bool btMachineIsLittleEndian()
381 {
382 long int i = 1;
383 const char* p = (const char*)&i;
384 if (p[0] == 1) // Lowest address contains the least significant byte
385 return true;
386 else
387 return false;
388 }
389
390 ///btSelect 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 SIMD_FORCE_INLINE unsigned btSelect(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)(((int32_t)condition | -(int32_t)condition) >> 31);
399 unsigned testEqz = ~testNz;
400 return ((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz));
401 }
402 SIMD_FORCE_INLINE int32_t btSelect(unsigned condition, int32_t valueIfConditionNonZero, int32_t valueIfConditionZero)
403 {
404 unsigned testNz = (unsigned)(((int32_t)condition | -(int32_t)condition) >> 31);
405 unsigned testEqz = ~testNz;
406 return static_cast<int32_t>((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz));
407 }
408 SIMD_FORCE_INLINE float btSelect(unsigned condition, float valueIfConditionNonZero, float valueIfConditionZero)
409 {
410 #ifdef BT_HAVE_NATIVE_FSEL
411 return (float)btFsel((btScalar)condition - btScalar(1.0f), valueIfConditionNonZero, valueIfConditionZero);
412 #else
413 return (condition != 0) ? valueIfConditionNonZero : valueIfConditionZero;
414 #endif
415 }
416
417 template <typename T>
418 SIMD_FORCE_INLINE void btSwap(T& a, T& b)
419 {
420 T tmp = a;
421 a = b;
422 b = tmp;
423 }
424
425 //PCK: endian swapping functions
426 SIMD_FORCE_INLINE unsigned btSwapEndian(unsigned val)
427 {
428 return (((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) | ((val & 0x0000ff00) << 8) | ((val & 0x000000ff) << 24));
429 }
430
431 SIMD_FORCE_INLINE unsigned short btSwapEndian(unsigned short val)
432 {
433 return static_cast<unsigned short>(((val & 0xff00) >> 8) | ((val & 0x00ff) << 8));
434 }
435
436 SIMD_FORCE_INLINE unsigned btSwapEndian(int32_t val)
437 {
438 return btSwapEndian((unsigned)val);
439 }
440
441 SIMD_FORCE_INLINE unsigned short btSwapEndian(short val)
442 {
443 return btSwapEndian((unsigned short)val);
444 }
445
446 ///btSwapFloat uses using char pointers to swap the endianness
447 ////btSwapFloat/btSwapDouble will NOT return a float, because the machine might 'correct' invalid floating point values
448 ///Not all values of sign/exponent/mantissa are valid floating point numbers according to IEEE 754.
449 ///When a floating point unit is faced with an invalid value, it may actually change the value, or worse, throw an exception.
450 ///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.
451 ///so instead of returning a float/double, we return integer/long long integer
452 SIMD_FORCE_INLINE uint32_t btSwapEndianFloat(float d)
453 {
454 uint32_t a = 0;
455 unsigned char* dst = (unsigned char*)&a;
456 unsigned char* src = (unsigned char*)&d;
457
458 dst[0] = src[3];
459 dst[1] = src[2];
460 dst[2] = src[1];
461 dst[3] = src[0];
462 return a;
463 }
464
465 // unswap using char pointers
466 SIMD_FORCE_INLINE float btUnswapEndianFloat(uint32_t a)
467 {
468 float d = 0.0f;
469 unsigned char* src = (unsigned char*)&a;
470 unsigned char* dst = (unsigned char*)&d;
471
472 dst[0] = src[3];
473 dst[1] = src[2];
474 dst[2] = src[1];
475 dst[3] = src[0];
476
477 return d;
478 }
479
480 // swap using char pointers
481 SIMD_FORCE_INLINE void btSwapEndianDouble(double d, unsigned char* dst)
482 {
483 unsigned char* src = (unsigned char*)&d;
484
485 dst[0] = src[7];
486 dst[1] = src[6];
487 dst[2] = src[5];
488 dst[3] = src[4];
489 dst[4] = src[3];
490 dst[5] = src[2];
491 dst[6] = src[1];
492 dst[7] = src[0];
493 }
494
495 // unswap using char pointers
496 SIMD_FORCE_INLINE double btUnswapEndianDouble(const unsigned char* src)
497 {
498 double d = 0.0;
499 unsigned char* dst = (unsigned char*)&d;
500
501 dst[0] = src[7];
502 dst[1] = src[6];
503 dst[2] = src[5];
504 dst[3] = src[4];
505 dst[4] = src[3];
506 dst[5] = src[2];
507 dst[6] = src[1];
508 dst[7] = src[0];
509
510 return d;
511 }
512
513 // returns normalized value in range [-SIMD_PI, SIMD_PI]
514 SIMD_FORCE_INLINE btScalar btNormalizeAngle(btScalar angleInRadians)
515 {
516 angleInRadians = btFmod(angleInRadians, SIMD_2_PI);
517 if (angleInRadians < -SIMD_PI) {
518 return angleInRadians + SIMD_2_PI;
519 }
520 else if (angleInRadians > SIMD_PI) {
521 return angleInRadians - SIMD_2_PI;
522 }
523 else {
524 return angleInRadians;
525 }
526 }
527
528 ///rudimentary class to provide type info
529 struct btTypedObject {
530 btTypedObject(int32_t objectType)
531 : m_objectType(objectType)
532 {
533 }
534 int32_t m_objectType;
535 inline int32_t getObjectType() const
536 {
537 return m_objectType;
538 }
539 };
540
541 // Cherry-picked from Bullet 2.88 to fix GH-27926
542 ///align a pointer to the provided alignment, upwards
543 template <typename T>
544 T *btAlignPointer(T *unalignedPtr, size_t alignment)
545 {
546 struct btConvertPointerSizeT
547 {
548 union {
549 T *ptr;
550 size_t integer;
551 };
552 };
553 btConvertPointerSizeT converter;
554
555 const size_t bit_mask = ~(alignment - 1);
556 converter.ptr = unalignedPtr;
557 converter.integer += alignment - 1;
558 converter.integer &= bit_mask;
559 return converter.ptr;
560 }
561
562 }; // namespace VHACD
563
564 #endif //BT_SCALAR_H