common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. #ifndef _ODE_PRIVATE_COMMON_H_
  23. #define _ODE_PRIVATE_COMMON_H_
  24. #include "typedefs.h"
  25. #include "error.h"
  26. #include <ode/memory.h>
  27. #include <algorithm>
  28. /*
  29. * Some reliability re-definitions
  30. */
  31. #ifndef offsetof
  32. #define offsetof(s, m) ((sizeint)&(((s *)8)->m) - (sizeint)8)
  33. #endif
  34. #ifndef membersize
  35. #define membersize(s, m) (sizeof(((s *)8)->m))
  36. #endif
  37. #ifndef endoffsetof
  38. #define endoffsetof(s, m) ((sizeint)((sizeint)&(((s *)8)->m) - (sizeint)8) + sizeof(((s *)8)->m))
  39. #endif
  40. /*
  41. * SIZE_MAX reliability re-definition
  42. */
  43. #ifndef SIZE_MAX
  44. #define SIZE_MAX ((sizeint)(-1))
  45. #endif
  46. /*
  47. * Macros for minimum and maximum (to be surly the macros
  48. */
  49. #define dMACRO_MAX(a, b) ((a) > (b) ? (a) : (b))
  50. #define dMACRO_MIN(a, b) ((a) < (b) ? (a) : (b))
  51. /*
  52. * Floating point related definitions
  53. */
  54. #ifdef dSINGLE
  55. #define dEpsilon FLT_EPSILON
  56. #else
  57. #define dEpsilon DBL_EPSILON
  58. #endif
  59. #ifdef dSINGLE
  60. #if !defined(FLT_MANT_DIG)
  61. #define FLT_MANT_DIG 24
  62. #endif
  63. #define dMaxExact ((float)((1UL << FLT_MANT_DIG) - 1))
  64. #define dMinExact ((float)(-dMaxExact))
  65. #else // #ifndef dSINGLE
  66. #if !defined(DBL_MANT_DIG)
  67. #define DBL_MANT_DIG 53
  68. #endif
  69. #define dMaxExact (double)((1ULL << DBL_MANT_DIG) - 1)
  70. #define dMinExact ((double)(-dMaxExact))
  71. #endif // #ifndef dSINGLE
  72. #define dMaxIntExact dMACRO_MIN(dMaxExact, (dReal)INT_MAX)
  73. #define dMinIntExact dMACRO_MAX(dMinExact, (dReal)INT_MIN)
  74. /*
  75. * Alignment related helpers
  76. */
  77. /* the efficient alignment. most platforms align data structures to some
  78. * number of bytes, but this is not always the most efficient alignment.
  79. * for example, many x86 compilers align to 4 bytes, but on a pentium it
  80. * is important to align doubles to 8 byte boundaries (for speed), and
  81. * the 4 floats in a SIMD register to 16 byte boundaries. many other
  82. * platforms have similar behavior. setting a larger alignment can waste
  83. * a (very) small amount of memory. NOTE: this number must be a power of
  84. * two. this is set to 16 by default.
  85. */
  86. #ifndef EFFICIENT_ALIGNMENT
  87. #define EFFICIENT_ALIGNMENT 16
  88. #endif
  89. #define dALIGN_SIZE(buf_size, alignment) (((buf_size) + (alignment - 1)) & (int)(~(alignment - 1))) // Casting the mask to int ensures sign-extension to larger integer sizes
  90. #define dALIGN_PTR(buf_ptr, alignment) ((void *)(((uintptr)(buf_ptr) + ((alignment) - 1)) & (int)(~(alignment - 1)))) // Casting the mask to int ensures sign-extension to larger integer sizes
  91. /* round something up to be a multiple of the EFFICIENT_ALIGNMENT */
  92. #define dEFFICIENT_SIZE(x) dALIGN_SIZE(x, EFFICIENT_ALIGNMENT)
  93. #define dEFFICIENT_PTR(p) dALIGN_PTR(p, EFFICIENT_ALIGNMENT)
  94. #define dOFFSET_EFFICIENTLY(p, b) ((void *)((uintptr)(p) + dEFFICIENT_SIZE(b)))
  95. #define dOVERALIGNED_SIZE(size, alignment) dEFFICIENT_SIZE((size) + ((alignment) - EFFICIENT_ALIGNMENT))
  96. #define dOVERALIGNED_PTR(buf_ptr, alignment) dALIGN_PTR(buf_ptr, alignment)
  97. #define dOFFSET_OVERALIGNEDLY(buf_ptr, size, alignment) ((void *)((uintptr)(buf_ptr) + dOVERALIGNED_SIZE(size, alignment)))
  98. #define dDERIVE_SIZE_UNION_PADDING_ELEMENTS(DataSize, ElementType) (((DataSize) + sizeof(ElementType) - 1) / sizeof(ElementType))
  99. #define dDERIVE_TYPE_UNION_PADDING_ELEMENTS(DataType, ElementType) dDERIVE_SIZE_UNION_PADDING_ELEMENTS(sizeof(DataType), ElementType)
  100. #define dDERIVE_SIZE_EXTRA_PADDING_ELEMENTS(DataSize, AlignmentSize, ElementType) (((dALIGN_SIZE(DataSize, dMACRO_MAX(AlignmentSize, sizeof(ElementType))) - (DataSize)) / sizeof(ElementType))
  101. /* alloca aligned to the EFFICIENT_ALIGNMENT. note that this can waste
  102. * up to 15 bytes per allocation, depending on what alloca() returns.
  103. */
  104. #define dALLOCA16(n) \
  105. dEFFICIENT_PTR(alloca((n)+(EFFICIENT_ALIGNMENT)))
  106. class dxAlignedAllocation
  107. {
  108. public:
  109. dxAlignedAllocation(): m_userAreaPointer(NULL), m_bufferAllocated(NULL), m_sizeUsed(0) {}
  110. ~dxAlignedAllocation() { freeAllocation(); }
  111. void *allocAligned(sizeint sizeRequired, unsigned alignmentRequired)
  112. {
  113. dIASSERT((alignmentRequired & (alignmentRequired - 1)) == 0);
  114. dIASSERT(alignmentRequired <= SIZE_MAX - sizeRequired);
  115. sizeint sizeToUse = sizeRequired + alignmentRequired;
  116. void *bufferPointer = dAlloc(sizeToUse);
  117. void *userAreaPointer = bufferPointer != NULL && alignmentRequired != 0 ? dALIGN_PTR(bufferPointer, alignmentRequired) : bufferPointer;
  118. assignData(userAreaPointer, bufferPointer, sizeToUse);
  119. return userAreaPointer;
  120. }
  121. void *getUserAreaPointer() const { return m_userAreaPointer; }
  122. sizeint getUserAreaSize() const { return m_sizeUsed - ((uint8 *)m_userAreaPointer - (uint8 *)m_bufferAllocated); }
  123. void freeAllocation()
  124. {
  125. sizeint sizeUsed;
  126. void *bufferPointer = extractData(sizeUsed);
  127. if (bufferPointer != NULL)
  128. {
  129. dFree(bufferPointer, sizeUsed);
  130. }
  131. }
  132. private:
  133. void assignData(void *userAreaPointer, void *bufferAllocated, sizeint sizeUsed)
  134. {
  135. dIASSERT(m_userAreaPointer == NULL);
  136. dIASSERT(m_bufferAllocated == NULL);
  137. dIASSERT(m_sizeUsed == 0);
  138. m_userAreaPointer = userAreaPointer;
  139. m_bufferAllocated = bufferAllocated;
  140. m_sizeUsed = sizeUsed;
  141. }
  142. void *extractData(sizeint &out_sizeUsed)
  143. {
  144. void *bufferPointer = m_bufferAllocated;
  145. if (bufferPointer != NULL)
  146. {
  147. out_sizeUsed = m_sizeUsed;
  148. m_userAreaPointer = NULL;
  149. m_bufferAllocated = NULL;
  150. m_sizeUsed = 0;
  151. }
  152. return bufferPointer;
  153. }
  154. private:
  155. void *m_userAreaPointer;
  156. void *m_bufferAllocated;
  157. sizeint m_sizeUsed;
  158. };
  159. /*
  160. * Type casting related helpers
  161. */
  162. template<typename DstType, typename SrcType>
  163. inline
  164. bool _cast_to_smaller(DstType &dtOutResult, const SrcType &stArgument)
  165. {
  166. return (SrcType)(dtOutResult = (DstType)stArgument) == stArgument;
  167. }
  168. #if defined(__GNUC__)
  169. #define dCAST_TO_SMALLER(TargetType, SourceValue) ({ TargetType ttCastSmallerValue; dIVERIFY(_cast_to_smaller(ttCastSmallerValue, SourceValue)); ttCastSmallerValue; })
  170. #else // #if !defined(__GNUC__)
  171. #define dCAST_TO_SMALLER(TargetType, SourceValue) templateCAST_TO_SMALLER<TargetType>(SourceValue)
  172. template <typename TTargetType, typename TSourceType>
  173. inline TTargetType templateCAST_TO_SMALLER(const TSourceType &stSourceValue)
  174. {
  175. TTargetType ttCastSmallerValue;
  176. dIVERIFY(_cast_to_smaller(ttCastSmallerValue, stSourceValue));
  177. return ttCastSmallerValue;
  178. }
  179. #endif // #if !defined(__GNUC__)
  180. template <typename Type>
  181. union _const_type_cast_union
  182. {
  183. explicit _const_type_cast_union(const void *psvCharBuffer): m_psvCharBuffer(psvCharBuffer) {}
  184. operator const Type *() const { return m_pstTypedPointer; }
  185. const Type &operator *() const { return *m_pstTypedPointer; }
  186. const Type *operator ->() const { return m_pstTypedPointer; }
  187. const Type &operator [](diffint diElementIndex) const { return m_pstTypedPointer[diElementIndex]; }
  188. const Type &operator [](sizeint siElementIndex) const { return m_pstTypedPointer[siElementIndex]; }
  189. const void *m_psvCharBuffer;
  190. const Type *m_pstTypedPointer;
  191. };
  192. template <typename Type>
  193. union _type_cast_union
  194. {
  195. explicit _type_cast_union(void *psvCharBuffer): m_psvCharBuffer(psvCharBuffer) {}
  196. operator Type *() const { return m_pstTypedPointer; }
  197. Type &operator *() const { return *m_pstTypedPointer; }
  198. Type *operator ->() const { return m_pstTypedPointer; }
  199. Type &operator [](diffint diElementIndex) const { return m_pstTypedPointer[diElementIndex]; }
  200. Type &operator [](sizeint siElementIndex) const { return m_pstTypedPointer[siElementIndex]; }
  201. void *m_psvCharBuffer;
  202. Type *m_pstTypedPointer;
  203. };
  204. template<sizeint tsiTypeSize>
  205. struct _sized_signed;
  206. template<>
  207. struct _sized_signed<sizeof(uint8)>
  208. {
  209. typedef int8 type;
  210. };
  211. template<>
  212. struct _sized_signed<sizeof(uint16)>
  213. {
  214. typedef int16 type;
  215. };
  216. template<>
  217. struct _sized_signed<sizeof(uint32)>
  218. {
  219. typedef int32 type;
  220. };
  221. template<>
  222. struct _sized_signed<sizeof(uint64)>
  223. {
  224. typedef int64 type;
  225. };
  226. template<typename tintergraltype>
  227. struct _make_signed
  228. {
  229. typedef typename _sized_signed<sizeof(tintergraltype)>::type type;
  230. };
  231. template<sizeint tsiTypeSize>
  232. struct _sized_unsigned;
  233. template<>
  234. struct _sized_unsigned<sizeof(int8)>
  235. {
  236. typedef uint8 type;
  237. };
  238. template<>
  239. struct _sized_unsigned<sizeof(int16)>
  240. {
  241. typedef uint16 type;
  242. };
  243. template<>
  244. struct _sized_unsigned<sizeof(int32)>
  245. {
  246. typedef uint32 type;
  247. };
  248. template<>
  249. struct _sized_unsigned<sizeof(int64)>
  250. {
  251. typedef uint64 type;
  252. };
  253. template<typename tintergraltype>
  254. struct _make_unsigned
  255. {
  256. typedef typename _sized_unsigned<sizeof(tintergraltype)>::type type;
  257. };
  258. /*
  259. * Some handy utilities
  260. */
  261. template<typename value_type>
  262. inline
  263. void dxSwap(value_type &one, value_type &another)
  264. {
  265. std::swap(one, another);
  266. }
  267. template<typename value_type, typename lo_type, typename hi_type>
  268. inline
  269. value_type dxClamp(const value_type &value, const lo_type &lo, const hi_type &hi)
  270. {
  271. return value < lo ? (value_type)lo : value > hi ? (value_type)hi : value;
  272. }
  273. // template<typename tvalueint, typename tminint, typename tmaxint>
  274. // inline
  275. // bool dxInRange(tvalueint viValue, tminint miMin, tmaxint miMax)
  276. // {
  277. // return (typename _sized_unsigned<dMACRO_MAX(sizeof(tvalueint), sizeof(tminint))>::type)(viValue - miMin) < (typename _sized_unsigned<dMACRO_MAX(sizeof(tmaxint), sizeof(tminint))>::type)(miMax - miMin);
  278. // }
  279. // #define dIN_RANGE(aval, amin, amax) dxInRange(aval, amin, amax)
  280. #define dIN_RANGE(aval, amin, amax) ((_sized_unsigned<dMACRO_MAX(sizeof(aval), sizeof(amin))>::type)((_sized_unsigned<dMACRO_MAX(sizeof(aval), sizeof(amin))>::type)(aval) - (_sized_unsigned<dMACRO_MAX(sizeof(aval), sizeof(amin))>::type)(amin)) < (_sized_unsigned<dMACRO_MAX(sizeof(amax), sizeof(amin))>::type)((_sized_unsigned<dMACRO_MAX(sizeof(amax), sizeof(amin))>::type)(amax) - (_sized_unsigned<dMACRO_MAX(sizeof(amax), sizeof(amin))>::type)(amin)))
  281. #define dTMPL_IN_RANGE(aval, amin, amax) ((typename _sized_unsigned<dMACRO_MAX(sizeof(aval), sizeof(amin))>::type)((typename _sized_unsigned<dMACRO_MAX(sizeof(aval), sizeof(amin))>::type)(aval) - (typename _sized_unsigned<dMACRO_MAX(sizeof(aval), sizeof(amin))>::type)(amin)) < (typename _sized_unsigned<dMACRO_MAX(sizeof(amax), sizeof(amin))>::type)((typename _sized_unsigned<dMACRO_MAX(sizeof(amax), sizeof(amin))>::type)(amax) - (typename _sized_unsigned<dMACRO_MAX(sizeof(amax), sizeof(amin))>::type)(amin)))
  282. #define dCLAMP(aval, alo, ahi) dxClamp(aval, alo, ahi)
  283. #define dARRAY_SIZE(aarr) (sizeof(aarr) / sizeof((aarr)[0]))
  284. #define dSTATIC_ARRAY_SIZE(aclass, aarr) dARRAY_SIZE(((aclass *)sizeof(void *))->aarr)
  285. #endif