func_integer.inl 15 KB

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