Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5738 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

  • Property svn:eol-style set to native
File size: 13.7 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); }
239  #if defined( __MINGW32__ )
240  SIMD_FORCE_INLINE btScalar btPow(btScalar x,btScalar y) { return pow(x,y); }
241  #else
242  SIMD_FORCE_INLINE btScalar btPow(btScalar x,btScalar y) { return powf(x,y); }
243  #endif
244       
245#endif
246
247#define SIMD_2_PI         btScalar(6.283185307179586232)
248#define SIMD_PI           (SIMD_2_PI * btScalar(0.5))
249#define SIMD_HALF_PI      (SIMD_2_PI * btScalar(0.25))
250#define SIMD_RADS_PER_DEG (SIMD_2_PI / btScalar(360.0))
251#define SIMD_DEGS_PER_RAD  (btScalar(360.0) / SIMD_2_PI)
252
253#ifdef BT_USE_DOUBLE_PRECISION
254#define SIMD_EPSILON      DBL_EPSILON
255#define SIMD_INFINITY     DBL_MAX
256#else
257#define SIMD_EPSILON      FLT_EPSILON
258#define SIMD_INFINITY     FLT_MAX
259#endif
260
261SIMD_FORCE_INLINE btScalar btAtan2Fast(btScalar y, btScalar x) 
262{
263        btScalar coeff_1 = SIMD_PI / 4.0f;
264        btScalar coeff_2 = 3.0f * coeff_1;
265        btScalar abs_y = btFabs(y);
266        btScalar angle;
267        if (x >= 0.0f) {
268                btScalar r = (x - abs_y) / (x + abs_y);
269                angle = coeff_1 - coeff_1 * r;
270        } else {
271                btScalar r = (x + abs_y) / (abs_y - x);
272                angle = coeff_2 - coeff_1 * r;
273        }
274        return (y < 0.0f) ? -angle : angle;
275}
276
277SIMD_FORCE_INLINE bool      btFuzzyZero(btScalar x) { return btFabs(x) < SIMD_EPSILON; }
278
279SIMD_FORCE_INLINE bool  btEqual(btScalar a, btScalar eps) {
280        return (((a) <= eps) && !((a) < -eps));
281}
282SIMD_FORCE_INLINE bool  btGreaterEqual (btScalar a, btScalar eps) {
283        return (!((a) <= eps));
284}
285
286
287SIMD_FORCE_INLINE int       btIsNegative(btScalar x) {
288    return x < btScalar(0.0) ? 1 : 0;
289}
290
291SIMD_FORCE_INLINE btScalar btRadians(btScalar x) { return x * SIMD_RADS_PER_DEG; }
292SIMD_FORCE_INLINE btScalar btDegrees(btScalar x) { return x * SIMD_DEGS_PER_RAD; }
293
294#define BT_DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name
295
296#ifndef btFsel
297SIMD_FORCE_INLINE btScalar btFsel(btScalar a, btScalar b, btScalar c)
298{
299        return a >= 0 ? b : c;
300}
301#endif
302#define btFsels(a,b,c) (btScalar)btFsel(a,b,c)
303
304
305SIMD_FORCE_INLINE bool btMachineIsLittleEndian()
306{
307   long int i = 1;
308   const char *p = (const char *) &i;
309   if (p[0] == 1)  // Lowest address contains the least significant byte
310           return true;
311   else
312           return false;
313}
314
315
316
317///btSelect avoids branches, which makes performance much better for consoles like Playstation 3 and XBox 360
318///Thanks Phil Knight. See also http://www.cellperformance.com/articles/2006/04/more_techniques_for_eliminatin_1.html
319SIMD_FORCE_INLINE unsigned btSelect(unsigned condition, unsigned valueIfConditionNonZero, unsigned valueIfConditionZero) 
320{
321    // Set testNz to 0xFFFFFFFF if condition is nonzero, 0x00000000 if condition is zero
322    // Rely on positive value or'ed with its negative having sign bit on
323    // and zero value or'ed with its negative (which is still zero) having sign bit off
324    // Use arithmetic shift right, shifting the sign bit through all 32 bits
325    unsigned testNz = (unsigned)(((int)condition | -(int)condition) >> 31);
326    unsigned testEqz = ~testNz;
327    return ((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz)); 
328}
329SIMD_FORCE_INLINE int btSelect(unsigned condition, int valueIfConditionNonZero, int valueIfConditionZero)
330{
331    unsigned testNz = (unsigned)(((int)condition | -(int)condition) >> 31);
332    unsigned testEqz = ~testNz; 
333    return static_cast<int>((valueIfConditionNonZero & testNz) | (valueIfConditionZero & testEqz));
334}
335SIMD_FORCE_INLINE float btSelect(unsigned condition, float valueIfConditionNonZero, float valueIfConditionZero)
336{
337#ifdef BT_HAVE_NATIVE_FSEL
338    return (float)btFsel((btScalar)condition - btScalar(1.0f), valueIfConditionNonZero, valueIfConditionZero);
339#else
340    return (condition != 0) ? valueIfConditionNonZero : valueIfConditionZero; 
341#endif
342}
343
344template<typename T> SIMD_FORCE_INLINE void btSwap(T& a, T& b)
345{
346        T tmp = a;
347        a = b;
348        b = tmp;
349}
350
351
352//PCK: endian swapping functions
353SIMD_FORCE_INLINE unsigned btSwapEndian(unsigned val)
354{
355        return (((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) | ((val & 0x0000ff00) << 8)  | ((val & 0x000000ff) << 24));
356}
357
358SIMD_FORCE_INLINE unsigned short btSwapEndian(unsigned short val)
359{
360        return static_cast<unsigned short>(((val & 0xff00) >> 8) | ((val & 0x00ff) << 8));
361}
362
363SIMD_FORCE_INLINE unsigned btSwapEndian(int val)
364{
365        return btSwapEndian((unsigned)val);
366}
367
368SIMD_FORCE_INLINE unsigned short btSwapEndian(short val)
369{
370        return btSwapEndian((unsigned short) val);
371}
372
373///btSwapFloat uses using char pointers to swap the endianness
374////btSwapFloat/btSwapDouble will NOT return a float, because the machine might 'correct' invalid floating point values
375///Not all values of sign/exponent/mantissa are valid floating point numbers according to IEEE 754.
376///When a floating point unit is faced with an invalid value, it may actually change the value, or worse, throw an exception.
377///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.
378///so instead of returning a float/double, we return integer/long long integer
379SIMD_FORCE_INLINE unsigned int  btSwapEndianFloat(float d)
380{
381    unsigned int a = 0;
382    unsigned char *dst = (unsigned char *)&a;
383    unsigned char *src = (unsigned char *)&d;
384
385    dst[0] = src[3];
386    dst[1] = src[2];
387    dst[2] = src[1];
388    dst[3] = src[0];
389    return a;
390}
391
392// unswap using char pointers
393SIMD_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
399    dst[0] = src[3];
400    dst[1] = src[2];
401    dst[2] = src[1];
402    dst[3] = src[0];
403
404    return d;
405}
406
407
408// swap using char pointers
409SIMD_FORCE_INLINE void  btSwapEndianDouble(double d, unsigned char* dst)
410{
411    unsigned char *src = (unsigned char *)&d;
412
413    dst[0] = src[7];
414    dst[1] = src[6];
415    dst[2] = src[5];
416    dst[3] = src[4];
417    dst[4] = src[3];
418    dst[5] = src[2];
419    dst[6] = src[1];
420    dst[7] = src[0];
421
422}
423
424// unswap using char pointers
425SIMD_FORCE_INLINE double btUnswapEndianDouble(const unsigned char *src) 
426{
427    double d = 0.0;
428    unsigned char *dst = (unsigned char *)&d;
429
430    dst[0] = src[7];
431    dst[1] = src[6];
432    dst[2] = src[5];
433    dst[3] = src[4];
434    dst[4] = src[3];
435    dst[5] = src[2];
436    dst[6] = src[1];
437    dst[7] = src[0];
438
439        return d;
440}
441
442
443#endif //SIMD___SCALAR_H
Note: See TracBrowser for help on using the repository browser.