func_integer.inl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /// @ref core
  2. #include "_vectorize.hpp"
  3. #if(GLM_ARCH & GLM_ARCH_X86 && GLM_COMPILER & GLM_COMPILER_VC)
  4. # include <intrin.h>
  5. # pragma intrinsic(_BitScanReverse)
  6. #endif//(GLM_ARCH & GLM_ARCH_X86 && GLM_COMPILER & GLM_COMPILER_VC)
  7. #include <limits>
  8. #if !GLM_HAS_EXTENDED_INTEGER_TYPE
  9. # if GLM_COMPILER & GLM_COMPILER_GCC
  10. # pragma GCC diagnostic ignored "-Wlong-long"
  11. # endif
  12. # if (GLM_COMPILER & GLM_COMPILER_CLANG)
  13. # pragma clang diagnostic ignored "-Wc++11-long-long"
  14. # endif
  15. #endif
  16. namespace glm{
  17. namespace detail
  18. {
  19. template<typename T>
  20. GLM_FUNC_QUALIFIER T mask(T Bits)
  21. {
  22. return Bits >= static_cast<T>(sizeof(T) * 8) ? ~static_cast<T>(0) : (static_cast<T>(1) << Bits) - static_cast<T>(1);
  23. }
  24. template<length_t L, typename T, qualifier Q, bool Aligned, bool EXEC>
  25. struct compute_bitfieldReverseStep
  26. {
  27. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T, T)
  28. {
  29. return v;
  30. }
  31. };
  32. template<length_t L, typename T, qualifier Q, bool Aligned>
  33. struct compute_bitfieldReverseStep<L, T, Q, Aligned, true>
  34. {
  35. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T Mask, T Shift)
  36. {
  37. return (v & Mask) << Shift | (v & (~Mask)) >> Shift;
  38. }
  39. };
  40. template<length_t L, typename T, qualifier Q, bool Aligned, bool EXEC>
  41. struct compute_bitfieldBitCountStep
  42. {
  43. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T, T)
  44. {
  45. return v;
  46. }
  47. };
  48. template<length_t L, typename T, qualifier Q, bool Aligned>
  49. struct compute_bitfieldBitCountStep<L, T, Q, Aligned, true>
  50. {
  51. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T Mask, T Shift)
  52. {
  53. return (v & Mask) + ((v >> Shift) & Mask);
  54. }
  55. };
  56. template<typename genIUType, size_t Bits>
  57. struct compute_findLSB
  58. {
  59. GLM_FUNC_QUALIFIER static int call(genIUType Value)
  60. {
  61. if(Value == 0)
  62. return -1;
  63. return glm::bitCount(~Value & (Value - static_cast<genIUType>(1)));
  64. }
  65. };
  66. # if GLM_HAS_BITSCAN_WINDOWS
  67. template<typename genIUType>
  68. struct compute_findLSB<genIUType, 32>
  69. {
  70. GLM_FUNC_QUALIFIER static int call(genIUType Value)
  71. {
  72. unsigned long Result(0);
  73. unsigned char IsNotNull = _BitScanForward(&Result, *reinterpret_cast<unsigned long*>(&Value));
  74. return IsNotNull ? int(Result) : -1;
  75. }
  76. };
  77. # if !((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_MODEL == GLM_MODEL_32))
  78. template<typename genIUType>
  79. struct compute_findLSB<genIUType, 64>
  80. {
  81. GLM_FUNC_QUALIFIER static int call(genIUType Value)
  82. {
  83. unsigned long Result(0);
  84. unsigned char IsNotNull = _BitScanForward64(&Result, *reinterpret_cast<unsigned __int64*>(&Value));
  85. return IsNotNull ? int(Result) : -1;
  86. }
  87. };
  88. # endif
  89. # endif//GLM_HAS_BITSCAN_WINDOWS
  90. template<length_t L, typename T, qualifier Q, bool EXEC = true>
  91. struct compute_findMSB_step_vec
  92. {
  93. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, T Shift)
  94. {
  95. return x | (x >> Shift);
  96. }
  97. };
  98. template<length_t L, typename T, qualifier Q>
  99. struct compute_findMSB_step_vec<L, T, Q, false>
  100. {
  101. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, T)
  102. {
  103. return x;
  104. }
  105. };
  106. template<length_t L, typename T, qualifier Q, int>
  107. struct compute_findMSB_vec
  108. {
  109. GLM_FUNC_QUALIFIER static vec<L, int, Q> call(vec<L, T, Q> const& v)
  110. {
  111. vec<L, T, Q> x(v);
  112. x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 8>::call(x, static_cast<T>( 1));
  113. x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 8>::call(x, static_cast<T>( 2));
  114. x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 8>::call(x, static_cast<T>( 4));
  115. x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 16>::call(x, static_cast<T>( 8));
  116. x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 32>::call(x, static_cast<T>(16));
  117. x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 64>::call(x, static_cast<T>(32));
  118. return vec<L, int, Q>(sizeof(T) * 8 - 1) - glm::bitCount(~x);
  119. }
  120. };
  121. # if GLM_HAS_BITSCAN_WINDOWS
  122. template<typename genIUType>
  123. GLM_FUNC_QUALIFIER int compute_findMSB_32(genIUType Value)
  124. {
  125. unsigned long Result(0);
  126. unsigned char IsNotNull = _BitScanReverse(&Result, *reinterpret_cast<unsigned long*>(&Value));
  127. return IsNotNull ? int(Result) : -1;
  128. }
  129. template<length_t L, typename T, qualifier Q>
  130. struct compute_findMSB_vec<L, T, Q, 32>
  131. {
  132. GLM_FUNC_QUALIFIER static vec<L, int, Q> call(vec<L, T, Q> const& x)
  133. {
  134. return detail::functor1<vec, L, int, T, Q>::call(compute_findMSB_32, x);
  135. }
  136. };
  137. # if !((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_MODEL == GLM_MODEL_32))
  138. template<typename genIUType>
  139. GLM_FUNC_QUALIFIER int compute_findMSB_64(genIUType Value)
  140. {
  141. unsigned long Result(0);
  142. unsigned char IsNotNull = _BitScanReverse64(&Result, *reinterpret_cast<unsigned __int64*>(&Value));
  143. return IsNotNull ? int(Result) : -1;
  144. }
  145. template<length_t L, typename T, qualifier Q>
  146. struct compute_findMSB_vec<L, T, Q, 64>
  147. {
  148. GLM_FUNC_QUALIFIER static vec<L, int, Q> call(vec<L, T, Q> const& x)
  149. {
  150. return detail::functor1<vec, L, int, T, Q>::call(compute_findMSB_64, x);
  151. }
  152. };
  153. # endif
  154. # endif//GLM_HAS_BITSCAN_WINDOWS
  155. }//namespace detail
  156. // uaddCarry
  157. GLM_FUNC_QUALIFIER uint uaddCarry(uint const& x, uint const& y, uint & Carry)
  158. {
  159. detail::uint64 const Value64(static_cast<detail::uint64>(x) + static_cast<detail::uint64>(y));
  160. detail::uint64 const Max32((static_cast<detail::uint64>(1) << static_cast<detail::uint64>(32)) - static_cast<detail::uint64>(1));
  161. Carry = Value64 > Max32 ? 1u : 0u;
  162. return static_cast<uint>(Value64 % (Max32 + static_cast<detail::uint64>(1)));
  163. }
  164. template<length_t L, qualifier Q>
  165. GLM_FUNC_QUALIFIER vec<L, uint, Q> uaddCarry(vec<L, uint, Q> const& x, vec<L, uint, Q> const& y, vec<L, uint, Q>& Carry)
  166. {
  167. vec<L, detail::uint64, Q> Value64(vec<L, detail::uint64, Q>(x) + vec<L, detail::uint64, Q>(y));
  168. vec<L, detail::uint64, Q> Max32((static_cast<detail::uint64>(1) << static_cast<detail::uint64>(32)) - static_cast<detail::uint64>(1));
  169. Carry = mix(vec<L, uint, Q>(0), vec<L, uint, Q>(1), greaterThan(Value64, Max32));
  170. return vec<L, uint, Q>(Value64 % (Max32 + static_cast<detail::uint64>(1)));
  171. }
  172. // usubBorrow
  173. GLM_FUNC_QUALIFIER uint usubBorrow(uint const& x, uint const& y, uint & Borrow)
  174. {
  175. Borrow = x >= y ? static_cast<uint>(0) : static_cast<uint>(1);
  176. if(y >= x)
  177. return y - x;
  178. else
  179. return static_cast<uint>((static_cast<detail::int64>(1) << static_cast<detail::int64>(32)) + (static_cast<detail::int64>(y) - static_cast<detail::int64>(x)));
  180. }
  181. template<length_t L, qualifier Q>
  182. GLM_FUNC_QUALIFIER vec<L, uint, Q> usubBorrow(vec<L, uint, Q> const& x, vec<L, uint, Q> const& y, vec<L, uint, Q>& Borrow)
  183. {
  184. Borrow = mix(vec<L, uint, Q>(1), vec<L, uint, Q>(0), greaterThanEqual(x, y));
  185. vec<L, uint, Q> const YgeX(y - x);
  186. vec<L, uint, Q> const XgeY(vec<L, uint, Q>((static_cast<detail::int64>(1) << static_cast<detail::int64>(32)) + (vec<L, detail::int64, Q>(y) - vec<L, detail::int64, Q>(x))));
  187. return mix(XgeY, YgeX, greaterThanEqual(y, x));
  188. }
  189. // umulExtended
  190. GLM_FUNC_QUALIFIER void umulExtended(uint const& x, uint const& y, uint & msb, uint & lsb)
  191. {
  192. detail::uint64 Value64 = static_cast<detail::uint64>(x) * static_cast<detail::uint64>(y);
  193. msb = static_cast<uint>(Value64 >> static_cast<detail::uint64>(32));
  194. lsb = static_cast<uint>(Value64);
  195. }
  196. template<length_t L, qualifier Q>
  197. GLM_FUNC_QUALIFIER void umulExtended(vec<L, uint, Q> const& x, vec<L, uint, Q> const& y, vec<L, uint, Q>& msb, vec<L, uint, Q>& lsb)
  198. {
  199. vec<L, detail::uint64, Q> Value64(vec<L, detail::uint64, Q>(x) * vec<L, detail::uint64, Q>(y));
  200. msb = vec<L, uint, Q>(Value64 >> static_cast<detail::uint64>(32));
  201. lsb = vec<L, uint, Q>(Value64);
  202. }
  203. // imulExtended
  204. GLM_FUNC_QUALIFIER void imulExtended(int x, int y, int& msb, int& lsb)
  205. {
  206. detail::int64 Value64 = static_cast<detail::int64>(x) * static_cast<detail::int64>(y);
  207. msb = static_cast<int>(Value64 >> static_cast<detail::int64>(32));
  208. lsb = static_cast<int>(Value64);
  209. }
  210. template<length_t L, qualifier Q>
  211. GLM_FUNC_QUALIFIER void imulExtended(vec<L, int, Q> const& x, vec<L, int, Q> const& y, vec<L, int, Q>& msb, vec<L, int, Q>& lsb)
  212. {
  213. vec<L, detail::int64, Q> Value64(vec<L, detail::int64, Q>(x) * vec<L, detail::int64, Q>(y));
  214. lsb = vec<L, int, Q>(Value64 & static_cast<detail::int64>(0xFFFFFFFF));
  215. msb = vec<L, int, Q>((Value64 >> static_cast<detail::int64>(32)) & static_cast<detail::int64>(0xFFFFFFFF));
  216. }
  217. // bitfieldExtract
  218. template<typename genIUType>
  219. GLM_FUNC_QUALIFIER genIUType bitfieldExtract(genIUType Value, int Offset, int Bits)
  220. {
  221. return bitfieldExtract(vec<1, genIUType>(Value), Offset, Bits).x;
  222. }
  223. template<length_t L, typename T, qualifier Q>
  224. GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldExtract(vec<L, T, Q> const& Value, int Offset, int Bits)
  225. {
  226. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldExtract' only accept integer inputs");
  227. return (Value >> static_cast<T>(Offset)) & static_cast<T>(detail::mask(Bits));
  228. }
  229. // bitfieldInsert
  230. template<typename genIUType>
  231. GLM_FUNC_QUALIFIER genIUType bitfieldInsert(genIUType const& Base, genIUType const& Insert, int Offset, int Bits)
  232. {
  233. return bitfieldInsert(vec<1, genIUType>(Base), vec<1, genIUType>(Insert), Offset, Bits).x;
  234. }
  235. template<length_t L, typename T, qualifier Q>
  236. GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldInsert(vec<L, T, Q> const& Base, vec<L, T, Q> const& Insert, int Offset, int Bits)
  237. {
  238. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldInsert' only accept integer values");
  239. T const Mask = static_cast<T>(detail::mask(Bits) << Offset);
  240. return (Base & ~Mask) | ((Insert << static_cast<T>(Offset)) & Mask);
  241. }
  242. // bitfieldReverse
  243. template<typename genType>
  244. GLM_FUNC_QUALIFIER genType bitfieldReverse(genType x)
  245. {
  246. return bitfieldReverse(glm::vec<1, genType, glm::defaultp>(x)).x;
  247. }
  248. template<length_t L, typename T, qualifier Q>
  249. GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldReverse(vec<L, T, Q> const& v)
  250. {
  251. vec<L, T, Q> x(v);
  252. x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 2>::call(x, static_cast<T>(0x5555555555555555ull), static_cast<T>( 1));
  253. x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 4>::call(x, static_cast<T>(0x3333333333333333ull), static_cast<T>( 2));
  254. x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 8>::call(x, static_cast<T>(0x0F0F0F0F0F0F0F0Full), static_cast<T>( 4));
  255. x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 16>::call(x, static_cast<T>(0x00FF00FF00FF00FFull), static_cast<T>( 8));
  256. x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 32>::call(x, static_cast<T>(0x0000FFFF0000FFFFull), static_cast<T>(16));
  257. x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 64>::call(x, static_cast<T>(0x00000000FFFFFFFFull), static_cast<T>(32));
  258. return x;
  259. }
  260. // bitCount
  261. template<typename genType>
  262. GLM_FUNC_QUALIFIER int bitCount(genType x)
  263. {
  264. return bitCount(glm::vec<1, genType, glm::defaultp>(x)).x;
  265. }
  266. template<length_t L, typename T, qualifier Q>
  267. GLM_FUNC_QUALIFIER vec<L, int, Q> bitCount(vec<L, T, Q> const& v)
  268. {
  269. # if GLM_COMPILER & GLM_COMPILER_VC
  270. # pragma warning(push)
  271. # pragma warning(disable : 4310) //cast truncates constant value
  272. # endif
  273. vec<L, typename detail::make_unsigned<T>::type, Q> x(*reinterpret_cast<vec<L, typename detail::make_unsigned<T>::type, Q> const *>(&v));
  274. x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 2>::call(x, typename detail::make_unsigned<T>::type(0x5555555555555555ull), typename detail::make_unsigned<T>::type( 1));
  275. x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 4>::call(x, typename detail::make_unsigned<T>::type(0x3333333333333333ull), typename detail::make_unsigned<T>::type( 2));
  276. x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 8>::call(x, typename detail::make_unsigned<T>::type(0x0F0F0F0F0F0F0F0Full), typename detail::make_unsigned<T>::type( 4));
  277. x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 16>::call(x, typename detail::make_unsigned<T>::type(0x00FF00FF00FF00FFull), typename detail::make_unsigned<T>::type( 8));
  278. x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 32>::call(x, typename detail::make_unsigned<T>::type(0x0000FFFF0000FFFFull), typename detail::make_unsigned<T>::type(16));
  279. x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 64>::call(x, typename detail::make_unsigned<T>::type(0x00000000FFFFFFFFull), typename detail::make_unsigned<T>::type(32));
  280. return vec<L, int, Q>(x);
  281. # if GLM_COMPILER & GLM_COMPILER_VC
  282. # pragma warning(pop)
  283. # endif
  284. }
  285. // findLSB
  286. template<typename genIUType>
  287. GLM_FUNC_QUALIFIER int findLSB(genIUType Value)
  288. {
  289. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findLSB' only accept integer values");
  290. return detail::compute_findLSB<genIUType, sizeof(genIUType) * 8>::call(Value);
  291. }
  292. template<length_t L, typename T, qualifier Q>
  293. GLM_FUNC_QUALIFIER vec<L, int, Q> findLSB(vec<L, T, Q> const& x)
  294. {
  295. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'findLSB' only accept integer values");
  296. return detail::functor1<vec, L, int, T, Q>::call(findLSB, x);
  297. }
  298. // findMSB
  299. template<typename genIUType>
  300. GLM_FUNC_QUALIFIER int findMSB(genIUType v)
  301. {
  302. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
  303. return findMSB(vec<1, genIUType>(v)).x;
  304. }
  305. template<length_t L, typename T, qualifier Q>
  306. GLM_FUNC_QUALIFIER vec<L, int, Q> findMSB(vec<L, T, Q> const& v)
  307. {
  308. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'findMSB' only accept integer values");
  309. return detail::compute_findMSB_vec<L, T, Q, sizeof(T) * 8>::call(v);
  310. }
  311. }//namespace glm
  312. #if GLM_CONFIG_SIMD == GLM_ENABLE
  313. # include "func_integer_simd.inl"
  314. #endif