Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/bullet/LinearMath/btScalar.h @ 2882

Last change on this file since 2882 was 2882, checked in by rgrieder, 15 years ago

Update from Bullet 2.73 to 2.74.

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