Newton Dynamics  4.00
ndTypes.h
1 /* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __ND_TYPES_H__
23 #define __ND_TYPES_H__
24 
25 #ifdef _MSC_VER
26  #if _MSC_VER >= 1400
27  #define HAVE_STRUCT_TIMESPEC
28  #endif
29 #endif
30 
31 #define ND_NEWTON_VERSION 402
32 
33 #if (defined (WIN32) || defined(_WIN32) || defined (_M_ARM) || defined (_M_ARM64))
34  #define NOMINMAX
35  #include <io.h>
36  #include <stdint.h>
37  #include <direct.h>
38  #include <malloc.h>
39  #include <stdarg.h>
40  #include <process.h>
41  #include <windows.h>
42  #include <crtdbg.h>
43 #else
44  #include <sys/stat.h>
45 #endif
46 
47 #include <new>
48 #include <mutex>
49 #include <atomic>
50 #include <thread>
51 #include <chrono>
52 #include <random>
53 #include <math.h>
54 #include <fenv.h>
55 #include <ctype.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <stdarg.h>
60 #include <locale.h>
61 #include <tinyxml.h>
62 #include <condition_variable>
63 
64 // we need _clearfp() and _controlfp() from float.h which are excluded if __STRICT_ANSI__ is defined
65 // weird workaround but i'm not sure how a global compiler flag would affect the rest of the code
66 // probably useless since this functionality will not be used/implemented but to avoid compiler errors
67 #if defined(__STRICT_ANSI__) && (defined (__MINGW32__) || defined (__MINGW64__))
68  #pragma push_macro("__STRICT_ANSI__")
69  #undef __STRICT_ANSI__
70 #endif
71 
72 #include <float.h>
73 #if (defined (__MINGW32__) || defined (__MINGW64__))
74  #pragma pop_macro("__STRICT_ANSI__")
75 #endif
76 
77 #if (defined (__MINGW32__) || defined (__MINGW64__))
78  #include <io.h>
79  #include <direct.h>
80  #include <malloc.h>
81  #include <windows.h>
82  #include <process.h>
83 #endif
84 
85 #if (defined (__linux__ ) || defined (__MINGW32__) || defined (__MINGW64__))
86  //CMake defines NDEBUG for _not_ debug builds. Therefore, set
87  //Newton's _DEBUG flag only when NDEBUG is not defined.
88 
89  #ifndef NDEBUG
90  #define _DEBUG 1
91  #endif
92 
93  #include <unistd.h>
94  #include <assert.h>
95  // it was __ARMCC_VERSION before, it should be __ARM__ or aarch64, otherwise cross compiling in gcc fails.
96  #if (!defined(__arm__) && !defined(__aarch64__))
97  extern "C"
98  {
99  // for SSE3 and up
100  #include <immintrin.h>
101  #include <pmmintrin.h>
102  #include <emmintrin.h>
103  #include <mmintrin.h>
104  }
105  #endif
106 #endif
107 
108 #if defined (__x86_64) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
109  #include <immintrin.h>
110 #endif
111 
112 #if defined(__arm__) && defined(__aarch64__)
113  #include <arm_neon.h>
114 #endif
115 
116 #if defined (__APPLE__)
117  #include <unistd.h>
118  #include <sys/sysctl.h>
119  #include <assert.h>
120  #if (defined __i386__ || defined __x86_64__)
121  #include <immintrin.h>
122  #include <pmmintrin.h>
123  #include <emmintrin.h> //sse3
124  #include <mmintrin.h>
125  #endif
126 #endif
127 
128 // uncomment out D_FORCE_PROFILE_PHYSICS to enable profiler frame capture profiler traces
129 // alternatively the end application can use a command line option to enable this define
130 //#define D_FORCE_PROFILE_PHYSICS
131 
132 #ifdef D_FORCE_PROFILE_PHYSICS
133  #ifndef D_PROFILER
134  #define D_PROFILER
135  #endif
136 #endif
137 
138 // uncomment this for Scalar floating point
139 // alternatively the end application can use a command line option to enable this define
140 //#define D_SCALAR_VECTOR_CLASS
141 
142 // uncomment this for Scalar floating point
143 // alternatively the end application can use a command line option to enable this define
144 //#define __ANDROID__
145 
146 // by default newton run on a separate thread and
147 // optionally concurrent with the calling thread,
148 // it also uses a thread job pool for multi core systems.
149 // define D_USE_THREAD_EMULATION on the command line for
150 // platform that do not support hardware multi threading or
151 // if the and application want to control threading at the application level
152 //#define D_USE_FORCE_THREAD_EMULATION
153 
154 #ifdef D_USE_FORCE_THREAD_EMULATION
155  #ifndef D_USE_THREAD_EMULATION
156  #define D_USE_THREAD_EMULATION
157  #endif
158 #endif
159 
160 //************************************************************
161 #ifdef D_DISABLE_ASSERT
162  #define ndAssert(x)
163 #else
164  #if (defined (WIN32) || defined(_WIN32) || defined (_M_ARM) || defined (_M_ARM64))
165  #define ndAssert(x) _ASSERTE(x)
166  #else
167  #ifdef _DEBUG
168  #define ndAssert(x) assert(x)
169  #else
170  #define ndAssert(x)
171  #endif
172  #endif
173 #endif
174 
175 #ifdef _DEBUG
176 //#define __ENABLE_D_CONTAINERS_SANITY_CHECK
177 #endif
178 
179 #if defined(_MSC_VER)
180  #define D_GCC_NEWTON_ALIGN_16
181  #define D_MSV_NEWTON_ALIGN_16 __declspec(align(16))
182 
183  #define D_GCC_NEWTON_ALIGN_32
184  #define D_MSV_NEWTON_ALIGN_32 __declspec(align(32))
185 #else
186  #define D_GCC_NEWTON_ALIGN_16 __attribute__((aligned (16)))
187  #define D_MSV_NEWTON_ALIGN_16
188 
189  #define D_GCC_NEWTON_ALIGN_32 __attribute__((aligned (32)))
190  #define D_MSV_NEWTON_ALIGN_32
191 #endif
192 
193 #if defined(_MSC_VER)
194  #define D_LIBRARY_EXPORT __declspec(dllexport)
195  #define D_LIBRARY_IMPORT __declspec(dllimport)
196 #else
197  #define D_LIBRARY_EXPORT __attribute__((visibility("default")))
198  #define D_LIBRARY_IMPORT __attribute__((visibility("default")))
199 #endif
200 
201 #ifdef _D_CORE_DLL
202  #ifdef _D_CORE_EXPORT_DLL
203  #define D_CORE_API D_LIBRARY_EXPORT
204  #else
205  #define D_CORE_API D_LIBRARY_IMPORT
206  #endif
207 #else
208  #define D_CORE_API
209 #endif
210 
211 typedef float ndReal;
212 typedef int8_t ndInt8;
213 typedef uint8_t ndUnsigned8;
214 typedef int16_t ndInt16;
215 typedef uint16_t ndUnsigned16;
216 typedef int32_t ndInt32;
217 typedef uint32_t ndUnsigned32;
218 typedef int64_t ndInt64;
219 typedef uint64_t ndUnsigned64;
220 
221 typedef double ndFloat64;
222 #ifdef D_NEWTON_USE_DOUBLE
223  typedef double ndFloat32;
224 #else
225  typedef float ndFloat32;
226 #endif
227 
228 #define ndPi ndFloat32 (3.141592f)
229 #define ndEXP ndFloat32 (2.71828f)
230 #define ndEpsilon ndFloat32 (1.0e-5f)
231 #define ndDegreeToRad ndFloat32 (ndPi / 180.0f)
232 #define ndRadToDegree ndFloat32 (180.0f / ndPi)
233 
234 #define ndSqrt(x) ndFloat32 (sqrt(x))
235 #define ndSin(x) ndFloat32 (sin(x))
236 #define ndCos(x) ndFloat32 (cos(x))
237 #define ndAsin(x) ndFloat32 (asin(x))
238 #define ndAcos(x) ndFloat32 (acos(x))
239 #define ndLog(x) ndFloat32 (log(x))
240 #define ndCeil(x) ndFloat32 (ceil(x))
241 #define ndFloor(x) ndFloat32 (floor(x))
242 #define ndPow(x,y) ndFloat32 (pow(x,y))
243 #define ndFmod(x,y) ndFloat32 (fmod(x,y))
244 #define ndTan(x) ndFloat32 (tan(x))
245 #define ndAtan(x) ndFloat32 (atan(x))
246 #define ndAtan2(x,y) ndFloat32 (atan2(x,y))
247 #define ndRsqrt(x) (ndFloat32 (1.0f) / ndSqrt(x))
248 
249 #if (defined (WIN32) || defined(_WIN32) || defined (_M_ARM) || defined (_M_ARM64))
250  //#define ndCheckFloat(x) (_finite(x) && !_isnan(x))
251  #define ndCheckFloat(x) (1)
252 #else
253  //#define ndCheckFloat(x) (isfinite(x) && !isnan(x))
254  #define ndCheckFloat(x) (1)
255 #endif
256 
257 #ifdef D_NEWTON_USE_DOUBLE
258  union ndFloatSign
259  {
260  ndInt64 m_iVal;
261  ndFloat64 m_fVal;
262  };
263 #else
265  {
266  ndInt32 m_iVal;
267  ndFloat32 m_fVal;
268  };
269 #endif
270 
272 {
273  struct
274  {
275  ndInt32 m_intL;
276  ndInt32 m_intH;
277  };
278  void* m_ptr;
279  ndInt64 m_int;
280  ndFloat64 m_float;
281 };
282 
284 {
285  public:
286  ndFloat32 m_x;
287  ndFloat32 m_y;
288  ndFloat32 m_z;
289 };
290 
291 #define D_OPERATOR_NEW_AND_DELETE \
292 inline void *operator new (size_t size) \
293 { \
294  return ndMemory::Malloc(size); \
295 } \
296  \
297 inline void *operator new[](size_t size) \
298 { \
299  return ndMemory::Malloc(size); \
300 } \
301  \
302 inline void operator delete (void* ptr) \
303 { \
304  ndMemory::Free(ptr); \
305 } \
306  \
307 inline void operator delete[](void* ptr) \
308 { \
309  ndMemory::Free(ptr); \
310 }
311 
312 
313 #ifdef D_USE_THREAD_EMULATION
314  template<class T>
316  class ndAtomic
317  {
318  public:
319  ndAtomic<T>()
320  : m_val(T(0))
321  {
322  }
323 
324  ndAtomic<T>(T val)
325  : m_val(val)
326  {
327  }
328 
329  operator T() const
330  {
331  return m_val;
332  }
333 
334  T load() const
335  {
336  return m_val;
337  }
338 
339  void store(T val)
340  {
341  m_val = val;
342  }
343 
344  T exchange(T val)
345  {
346  ndSwap(val, m_val);
347  return val;
348  }
349 
350  T fetch_add(T val)
351  {
352  T ret = m_val;
353  m_val += val;
354  return ret;
355  }
356 
357  T fetch_sub(T val)
358  {
359  T ret = m_val;
360  m_val -= val;
361  return ret;
362  }
363 
364  bool compare_exchange_weak(T oldValue, T newValue)
365  {
366  if (m_val == oldValue)
367  {
368  m_val = newValue;
369  return true;
370  }
371  return false;
372  }
373 
374  private:
375  T m_val;
376  };
377 #else
378  template<class T>
380  class ndAtomic : public std::atomic<T>
381  {
382  public:
383  ndAtomic<T>()
384  : std::atomic<T>(T(0))
385  {
386  }
387 
388  ndAtomic<T>(T val)
389  : std::atomic<T>(val)
390  {
391  }
392 
393  ndAtomic<T>(const ndAtomic<T>& copy)
394  : std::atomic<T>(copy.load())
395  {
396  }
397 
398  T operator=(T value)
399  {
400  return std::atomic<T>::operator=(value);
401  }
402  };
403 #endif
404 
405 
406 #endif
407 
ndTriplex
Definition: ndTypes.h:284
ndDoubleInt
Definition: ndTypes.h:272
ndFloatSign
Definition: ndTypes.h:265
ndAtomic
wrapper over standard atomic operations
Definition: ndTypes.h:381