22 #ifndef __ND_UTILS_H__
23 #define __ND_UTILS_H__
25 #include "ndCoreStdafx.h"
31 #define ndAlloca(type, count) (type*) alloca (sizeof (type) * (count))
33 inline ndInt32 ndExp2 (ndInt32 x)
43 inline ndInt32 ndBitReversal(ndInt32 v, ndInt32 base)
46 ndInt32 power = ndExp2 (base) - 1;
49 x += (v & 1) << power;
58 inline T ndMod(T val, T mod)
60 return T(fmod(T(val), T(mod)));
64 inline T ndMin(T A, T B)
66 return (A < B) ? A : B;
70 inline T ndMax(T A, T B)
72 return (A > B) ? A : B;
76 inline T ndClamp(T val, T min, T max)
78 return ndMax (min, ndMin (max, val));
82 inline void ndSwap(T& A, T& B)
93 return (A >= T(0)) ? A : -A;
99 return (A >= T(0)) ? T(1) : T(-1);
103 inline bool ndAreEqual(T A, T B, T tol)
106 if ((ndAbs(A) < tol) && (ndAbs(B) < tol))
112 ndFloat64 mantissa0 = frexp(ndFloat64 (A), &exp0);
115 ndFloat64 mantissa1 = frexp(ndFloat64(B), &exp1);
116 if (ndAbs(exp0 - exp1) > 1)
124 mantissa0 *= ndFloat32(2.0f);
128 mantissa1 *= ndFloat32(2.0f);
131 return ndAbs(mantissa0 - mantissa1) < tol;
136 inline T ndAnglesAdd (T angleInRadiand1, T angleInRadiand0)
138 T s1 = T(ndSin(angleInRadiand1));
139 T c1 = T(ndCos(angleInRadiand1));
140 T s0 = T(ndSin(angleInRadiand0));
141 T c0 = T(ndCos(angleInRadiand0));
143 T s = s1 * c0 + s0 * c1;
144 T c = c1 * c0 - s0 * s1;
145 return T(ndAtan2(s, c));
149 #define _stricmp(x,y) strcasecmp(x,y)
152 inline void strtolwr(
char*
const string)
154 for (
char * cp =
string; *cp; ++cp)
156 if ((*cp >=
'A') && (*cp <=
'Z'))
164 D_CORE_API ndUnsigned64 ndGetTimeInMicroseconds();
169 D_CORE_API ndFloat64 ndRoundToFloat(ndFloat64 val);
173 D_CORE_API
void ndThreadYield();
176 D_CORE_API ndInt32 ndVertexListToIndexList(ndFloat64*
const vertexList, ndInt32 strideInBytes, ndInt32 compareCount, ndInt32 vertexCount, ndInt32*
const indexListOut, ndFloat64 tolerance = ndEpsilon);
180 ndInt32 ndVertexListToIndexList(T*
const vertexList, ndInt32 strideInBytes, ndInt32 compareCount, ndInt32 vertexCount, ndInt32*
const indexListOut, T tolerance = ndEpsilon)
182 ndInt32 stride = ndInt32(strideInBytes /
sizeof(T));
185 ndFloat64*
const data = &pool[0];
186 for (ndInt32 i = 0; i < vertexCount; ++i)
188 ndFloat64*
const dst = &data[i * stride];
189 const T*
const src = &vertexList[i * stride];
190 for (ndInt32 j = 0; j < stride; ++j)
196 ndInt32 count = ndVertexListToIndexList(data, ndInt32(stride *
sizeof(ndFloat64)), compareCount, vertexCount, indexListOut, ndFloat64(tolerance));
197 for (ndInt32 i = 0; i < count; ++i)
199 const ndFloat64*
const src = &data[i * stride];
200 T*
const dst = &vertexList[i * stride];
201 for (ndInt32 j = 0; j < stride; ++j)
215 #ifndef D_USE_THREAD_EMULATION
223 #ifndef D_USE_THREAD_EMULATION
225 for (ndUnsigned32 test = 0; !m_lock.compare_exchange_weak(test, 1); test = 0)
234 #ifndef D_USE_THREAD_EMULATION
239 #ifndef D_USE_THREAD_EMULATION
241 D_CORE_API
void Delay(ndInt32& exp);
251 :m_spinLock(spinLock)
269 #if defined (WIN32) || defined(_WIN32)
270 #define D_FLOAT_EXCEPTIONS_MASK (EM_INVALID | EM_DENORMAL)
272 #define D_FLOAT_EXCEPTIONS_MASK (FE_INVALID | FE_INEXACT)
279 ndUnsigned32 m_floatMask;
280 ndUnsigned32 m_simdMask;