9
3

bitfield.inl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /// @ref gtc_bitfield
  2. #include "../simd/integer.h"
  3. namespace glm{
  4. namespace detail
  5. {
  6. template<typename PARAM, typename RET>
  7. GLM_FUNC_DECL RET bitfieldInterleave(PARAM x, PARAM y);
  8. template<typename PARAM, typename RET>
  9. GLM_FUNC_DECL RET bitfieldInterleave(PARAM x, PARAM y, PARAM z);
  10. template<typename PARAM, typename RET>
  11. GLM_FUNC_DECL RET bitfieldInterleave(PARAM x, PARAM y, PARAM z, PARAM w);
  12. template<>
  13. GLM_FUNC_QUALIFIER glm::uint16 bitfieldInterleave(glm::uint8 x, glm::uint8 y)
  14. {
  15. glm::uint16 REG1(x);
  16. glm::uint16 REG2(y);
  17. REG1 = ((REG1 << 4) | REG1) & static_cast<glm::uint16>(0x0F0F);
  18. REG2 = ((REG2 << 4) | REG2) & static_cast<glm::uint16>(0x0F0F);
  19. REG1 = ((REG1 << 2) | REG1) & static_cast<glm::uint16>(0x3333);
  20. REG2 = ((REG2 << 2) | REG2) & static_cast<glm::uint16>(0x3333);
  21. REG1 = ((REG1 << 1) | REG1) & static_cast<glm::uint16>(0x5555);
  22. REG2 = ((REG2 << 1) | REG2) & static_cast<glm::uint16>(0x5555);
  23. return REG1 | static_cast<glm::uint16>(REG2 << 1);
  24. }
  25. template<>
  26. GLM_FUNC_QUALIFIER glm::uint32 bitfieldInterleave(glm::uint16 x, glm::uint16 y)
  27. {
  28. glm::uint32 REG1(x);
  29. glm::uint32 REG2(y);
  30. REG1 = ((REG1 << 8) | REG1) & static_cast<glm::uint32>(0x00FF00FF);
  31. REG2 = ((REG2 << 8) | REG2) & static_cast<glm::uint32>(0x00FF00FF);
  32. REG1 = ((REG1 << 4) | REG1) & static_cast<glm::uint32>(0x0F0F0F0F);
  33. REG2 = ((REG2 << 4) | REG2) & static_cast<glm::uint32>(0x0F0F0F0F);
  34. REG1 = ((REG1 << 2) | REG1) & static_cast<glm::uint32>(0x33333333);
  35. REG2 = ((REG2 << 2) | REG2) & static_cast<glm::uint32>(0x33333333);
  36. REG1 = ((REG1 << 1) | REG1) & static_cast<glm::uint32>(0x55555555);
  37. REG2 = ((REG2 << 1) | REG2) & static_cast<glm::uint32>(0x55555555);
  38. return REG1 | (REG2 << 1);
  39. }
  40. template<>
  41. GLM_FUNC_QUALIFIER glm::uint64 bitfieldInterleave(glm::uint32 x, glm::uint32 y)
  42. {
  43. glm::uint64 REG1(x);
  44. glm::uint64 REG2(y);
  45. REG1 = ((REG1 << 16) | REG1) & static_cast<glm::uint64>(0x0000FFFF0000FFFFull);
  46. REG2 = ((REG2 << 16) | REG2) & static_cast<glm::uint64>(0x0000FFFF0000FFFFull);
  47. REG1 = ((REG1 << 8) | REG1) & static_cast<glm::uint64>(0x00FF00FF00FF00FFull);
  48. REG2 = ((REG2 << 8) | REG2) & static_cast<glm::uint64>(0x00FF00FF00FF00FFull);
  49. REG1 = ((REG1 << 4) | REG1) & static_cast<glm::uint64>(0x0F0F0F0F0F0F0F0Full);
  50. REG2 = ((REG2 << 4) | REG2) & static_cast<glm::uint64>(0x0F0F0F0F0F0F0F0Full);
  51. REG1 = ((REG1 << 2) | REG1) & static_cast<glm::uint64>(0x3333333333333333ull);
  52. REG2 = ((REG2 << 2) | REG2) & static_cast<glm::uint64>(0x3333333333333333ull);
  53. REG1 = ((REG1 << 1) | REG1) & static_cast<glm::uint64>(0x5555555555555555ull);
  54. REG2 = ((REG2 << 1) | REG2) & static_cast<glm::uint64>(0x5555555555555555ull);
  55. return REG1 | (REG2 << 1);
  56. }
  57. template<>
  58. GLM_FUNC_QUALIFIER glm::uint32 bitfieldInterleave(glm::uint8 x, glm::uint8 y, glm::uint8 z)
  59. {
  60. glm::uint32 REG1(x);
  61. glm::uint32 REG2(y);
  62. glm::uint32 REG3(z);
  63. REG1 = ((REG1 << 16) | REG1) & static_cast<glm::uint32>(0xFF0000FFu);
  64. REG2 = ((REG2 << 16) | REG2) & static_cast<glm::uint32>(0xFF0000FFu);
  65. REG3 = ((REG3 << 16) | REG3) & static_cast<glm::uint32>(0xFF0000FFu);
  66. REG1 = ((REG1 << 8) | REG1) & static_cast<glm::uint32>(0x0F00F00Fu);
  67. REG2 = ((REG2 << 8) | REG2) & static_cast<glm::uint32>(0x0F00F00Fu);
  68. REG3 = ((REG3 << 8) | REG3) & static_cast<glm::uint32>(0x0F00F00Fu);
  69. REG1 = ((REG1 << 4) | REG1) & static_cast<glm::uint32>(0xC30C30C3u);
  70. REG2 = ((REG2 << 4) | REG2) & static_cast<glm::uint32>(0xC30C30C3u);
  71. REG3 = ((REG3 << 4) | REG3) & static_cast<glm::uint32>(0xC30C30C3u);
  72. REG1 = ((REG1 << 2) | REG1) & static_cast<glm::uint32>(0x49249249u);
  73. REG2 = ((REG2 << 2) | REG2) & static_cast<glm::uint32>(0x49249249u);
  74. REG3 = ((REG3 << 2) | REG3) & static_cast<glm::uint32>(0x49249249u);
  75. return REG1 | (REG2 << 1) | (REG3 << 2);
  76. }
  77. template<>
  78. GLM_FUNC_QUALIFIER glm::uint64 bitfieldInterleave(glm::uint16 x, glm::uint16 y, glm::uint16 z)
  79. {
  80. glm::uint64 REG1(x);
  81. glm::uint64 REG2(y);
  82. glm::uint64 REG3(z);
  83. REG1 = ((REG1 << 32) | REG1) & static_cast<glm::uint64>(0xFFFF00000000FFFFull);
  84. REG2 = ((REG2 << 32) | REG2) & static_cast<glm::uint64>(0xFFFF00000000FFFFull);
  85. REG3 = ((REG3 << 32) | REG3) & static_cast<glm::uint64>(0xFFFF00000000FFFFull);
  86. REG1 = ((REG1 << 16) | REG1) & static_cast<glm::uint64>(0x00FF0000FF0000FFull);
  87. REG2 = ((REG2 << 16) | REG2) & static_cast<glm::uint64>(0x00FF0000FF0000FFull);
  88. REG3 = ((REG3 << 16) | REG3) & static_cast<glm::uint64>(0x00FF0000FF0000FFull);
  89. REG1 = ((REG1 << 8) | REG1) & static_cast<glm::uint64>(0xF00F00F00F00F00Full);
  90. REG2 = ((REG2 << 8) | REG2) & static_cast<glm::uint64>(0xF00F00F00F00F00Full);
  91. REG3 = ((REG3 << 8) | REG3) & static_cast<glm::uint64>(0xF00F00F00F00F00Full);
  92. REG1 = ((REG1 << 4) | REG1) & static_cast<glm::uint64>(0x30C30C30C30C30C3ull);
  93. REG2 = ((REG2 << 4) | REG2) & static_cast<glm::uint64>(0x30C30C30C30C30C3ull);
  94. REG3 = ((REG3 << 4) | REG3) & static_cast<glm::uint64>(0x30C30C30C30C30C3ull);
  95. REG1 = ((REG1 << 2) | REG1) & static_cast<glm::uint64>(0x9249249249249249ull);
  96. REG2 = ((REG2 << 2) | REG2) & static_cast<glm::uint64>(0x9249249249249249ull);
  97. REG3 = ((REG3 << 2) | REG3) & static_cast<glm::uint64>(0x9249249249249249ull);
  98. return REG1 | (REG2 << 1) | (REG3 << 2);
  99. }
  100. template<>
  101. GLM_FUNC_QUALIFIER glm::uint64 bitfieldInterleave(glm::uint32 x, glm::uint32 y, glm::uint32 z)
  102. {
  103. glm::uint64 REG1(x);
  104. glm::uint64 REG2(y);
  105. glm::uint64 REG3(z);
  106. REG1 = ((REG1 << 32) | REG1) & static_cast<glm::uint64>(0xFFFF00000000FFFFull);
  107. REG2 = ((REG2 << 32) | REG2) & static_cast<glm::uint64>(0xFFFF00000000FFFFull);
  108. REG3 = ((REG3 << 32) | REG3) & static_cast<glm::uint64>(0xFFFF00000000FFFFull);
  109. REG1 = ((REG1 << 16) | REG1) & static_cast<glm::uint64>(0x00FF0000FF0000FFull);
  110. REG2 = ((REG2 << 16) | REG2) & static_cast<glm::uint64>(0x00FF0000FF0000FFull);
  111. REG3 = ((REG3 << 16) | REG3) & static_cast<glm::uint64>(0x00FF0000FF0000FFull);
  112. REG1 = ((REG1 << 8) | REG1) & static_cast<glm::uint64>(0xF00F00F00F00F00Full);
  113. REG2 = ((REG2 << 8) | REG2) & static_cast<glm::uint64>(0xF00F00F00F00F00Full);
  114. REG3 = ((REG3 << 8) | REG3) & static_cast<glm::uint64>(0xF00F00F00F00F00Full);
  115. REG1 = ((REG1 << 4) | REG1) & static_cast<glm::uint64>(0x30C30C30C30C30C3ull);
  116. REG2 = ((REG2 << 4) | REG2) & static_cast<glm::uint64>(0x30C30C30C30C30C3ull);
  117. REG3 = ((REG3 << 4) | REG3) & static_cast<glm::uint64>(0x30C30C30C30C30C3ull);
  118. REG1 = ((REG1 << 2) | REG1) & static_cast<glm::uint64>(0x9249249249249249ull);
  119. REG2 = ((REG2 << 2) | REG2) & static_cast<glm::uint64>(0x9249249249249249ull);
  120. REG3 = ((REG3 << 2) | REG3) & static_cast<glm::uint64>(0x9249249249249249ull);
  121. return REG1 | (REG2 << 1) | (REG3 << 2);
  122. }
  123. template<>
  124. GLM_FUNC_QUALIFIER glm::uint32 bitfieldInterleave(glm::uint8 x, glm::uint8 y, glm::uint8 z, glm::uint8 w)
  125. {
  126. glm::uint32 REG1(x);
  127. glm::uint32 REG2(y);
  128. glm::uint32 REG3(z);
  129. glm::uint32 REG4(w);
  130. REG1 = ((REG1 << 12) | REG1) & static_cast<glm::uint32>(0x000F000Fu);
  131. REG2 = ((REG2 << 12) | REG2) & static_cast<glm::uint32>(0x000F000Fu);
  132. REG3 = ((REG3 << 12) | REG3) & static_cast<glm::uint32>(0x000F000Fu);
  133. REG4 = ((REG4 << 12) | REG4) & static_cast<glm::uint32>(0x000F000Fu);
  134. REG1 = ((REG1 << 6) | REG1) & static_cast<glm::uint32>(0x03030303u);
  135. REG2 = ((REG2 << 6) | REG2) & static_cast<glm::uint32>(0x03030303u);
  136. REG3 = ((REG3 << 6) | REG3) & static_cast<glm::uint32>(0x03030303u);
  137. REG4 = ((REG4 << 6) | REG4) & static_cast<glm::uint32>(0x03030303u);
  138. REG1 = ((REG1 << 3) | REG1) & static_cast<glm::uint32>(0x11111111u);
  139. REG2 = ((REG2 << 3) | REG2) & static_cast<glm::uint32>(0x11111111u);
  140. REG3 = ((REG3 << 3) | REG3) & static_cast<glm::uint32>(0x11111111u);
  141. REG4 = ((REG4 << 3) | REG4) & static_cast<glm::uint32>(0x11111111u);
  142. return REG1 | (REG2 << 1) | (REG3 << 2) | (REG4 << 3);
  143. }
  144. template<>
  145. GLM_FUNC_QUALIFIER glm::uint64 bitfieldInterleave(glm::uint16 x, glm::uint16 y, glm::uint16 z, glm::uint16 w)
  146. {
  147. glm::uint64 REG1(x);
  148. glm::uint64 REG2(y);
  149. glm::uint64 REG3(z);
  150. glm::uint64 REG4(w);
  151. REG1 = ((REG1 << 24) | REG1) & static_cast<glm::uint64>(0x000000FF000000FFull);
  152. REG2 = ((REG2 << 24) | REG2) & static_cast<glm::uint64>(0x000000FF000000FFull);
  153. REG3 = ((REG3 << 24) | REG3) & static_cast<glm::uint64>(0x000000FF000000FFull);
  154. REG4 = ((REG4 << 24) | REG4) & static_cast<glm::uint64>(0x000000FF000000FFull);
  155. REG1 = ((REG1 << 12) | REG1) & static_cast<glm::uint64>(0x000F000F000F000Full);
  156. REG2 = ((REG2 << 12) | REG2) & static_cast<glm::uint64>(0x000F000F000F000Full);
  157. REG3 = ((REG3 << 12) | REG3) & static_cast<glm::uint64>(0x000F000F000F000Full);
  158. REG4 = ((REG4 << 12) | REG4) & static_cast<glm::uint64>(0x000F000F000F000Full);
  159. REG1 = ((REG1 << 6) | REG1) & static_cast<glm::uint64>(0x0303030303030303ull);
  160. REG2 = ((REG2 << 6) | REG2) & static_cast<glm::uint64>(0x0303030303030303ull);
  161. REG3 = ((REG3 << 6) | REG3) & static_cast<glm::uint64>(0x0303030303030303ull);
  162. REG4 = ((REG4 << 6) | REG4) & static_cast<glm::uint64>(0x0303030303030303ull);
  163. REG1 = ((REG1 << 3) | REG1) & static_cast<glm::uint64>(0x1111111111111111ull);
  164. REG2 = ((REG2 << 3) | REG2) & static_cast<glm::uint64>(0x1111111111111111ull);
  165. REG3 = ((REG3 << 3) | REG3) & static_cast<glm::uint64>(0x1111111111111111ull);
  166. REG4 = ((REG4 << 3) | REG4) & static_cast<glm::uint64>(0x1111111111111111ull);
  167. return REG1 | (REG2 << 1) | (REG3 << 2) | (REG4 << 3);
  168. }
  169. }//namespace detail
  170. template<typename genIUType>
  171. GLM_FUNC_QUALIFIER genIUType mask(genIUType Bits)
  172. {
  173. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'mask' accepts only integer values");
  174. return Bits >= sizeof(genIUType) * 8 ? ~static_cast<genIUType>(0) : (static_cast<genIUType>(1) << Bits) - static_cast<genIUType>(1);
  175. }
  176. template<length_t L, typename T, qualifier Q>
  177. GLM_FUNC_QUALIFIER vec<L, T, Q> mask(vec<L, T, Q> const& v)
  178. {
  179. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'mask' accepts only integer values");
  180. return detail::functor1<vec, L, T, T, Q>::call(mask, v);
  181. }
  182. template<typename genIType>
  183. GLM_FUNC_QUALIFIER genIType bitfieldRotateRight(genIType In, int Shift)
  184. {
  185. GLM_STATIC_ASSERT(std::numeric_limits<genIType>::is_integer, "'bitfieldRotateRight' accepts only integer values");
  186. int const BitSize = static_cast<genIType>(sizeof(genIType) * 8);
  187. return (In << static_cast<genIType>(Shift)) | (In >> static_cast<genIType>(BitSize - Shift));
  188. }
  189. template<length_t L, typename T, qualifier Q>
  190. GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldRotateRight(vec<L, T, Q> const& In, int Shift)
  191. {
  192. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldRotateRight' accepts only integer values");
  193. int const BitSize = static_cast<int>(sizeof(T) * 8);
  194. return (In << static_cast<T>(Shift)) | (In >> static_cast<T>(BitSize - Shift));
  195. }
  196. template<typename genIType>
  197. GLM_FUNC_QUALIFIER genIType bitfieldRotateLeft(genIType In, int Shift)
  198. {
  199. GLM_STATIC_ASSERT(std::numeric_limits<genIType>::is_integer, "'bitfieldRotateLeft' accepts only integer values");
  200. int const BitSize = static_cast<genIType>(sizeof(genIType) * 8);
  201. return (In >> static_cast<genIType>(Shift)) | (In << static_cast<genIType>(BitSize - Shift));
  202. }
  203. template<length_t L, typename T, qualifier Q>
  204. GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldRotateLeft(vec<L, T, Q> const& In, int Shift)
  205. {
  206. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldRotateLeft' accepts only integer values");
  207. int const BitSize = static_cast<int>(sizeof(T) * 8);
  208. return (In >> static_cast<T>(Shift)) | (In << static_cast<T>(BitSize - Shift));
  209. }
  210. template<typename genIUType>
  211. GLM_FUNC_QUALIFIER genIUType bitfieldFillOne(genIUType Value, int FirstBit, int BitCount)
  212. {
  213. return Value | static_cast<genIUType>(mask(BitCount) << FirstBit);
  214. }
  215. template<length_t L, typename T, qualifier Q>
  216. GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldFillOne(vec<L, T, Q> const& Value, int FirstBit, int BitCount)
  217. {
  218. return Value | static_cast<T>(mask(BitCount) << FirstBit);
  219. }
  220. template<typename genIUType>
  221. GLM_FUNC_QUALIFIER genIUType bitfieldFillZero(genIUType Value, int FirstBit, int BitCount)
  222. {
  223. return Value & static_cast<genIUType>(~(mask(BitCount) << FirstBit));
  224. }
  225. template<length_t L, typename T, qualifier Q>
  226. GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldFillZero(vec<L, T, Q> const& Value, int FirstBit, int BitCount)
  227. {
  228. return Value & static_cast<T>(~(mask(BitCount) << FirstBit));
  229. }
  230. GLM_FUNC_QUALIFIER int16 bitfieldInterleave(int8 x, int8 y)
  231. {
  232. union sign8
  233. {
  234. int8 i;
  235. uint8 u;
  236. } sign_x, sign_y;
  237. union sign16
  238. {
  239. int16 i;
  240. uint16 u;
  241. } result;
  242. sign_x.i = x;
  243. sign_y.i = y;
  244. result.u = bitfieldInterleave(sign_x.u, sign_y.u);
  245. return result.i;
  246. }
  247. GLM_FUNC_QUALIFIER uint16 bitfieldInterleave(uint8 x, uint8 y)
  248. {
  249. return detail::bitfieldInterleave<uint8, uint16>(x, y);
  250. }
  251. GLM_FUNC_QUALIFIER uint16 bitfieldInterleave(u8vec2 const& v)
  252. {
  253. return detail::bitfieldInterleave<uint8, uint16>(v.x, v.y);
  254. }
  255. GLM_FUNC_QUALIFIER u8vec2 bitfieldDeinterleave(glm::uint16 x)
  256. {
  257. uint16 REG1(x);
  258. uint16 REG2(x >>= 1);
  259. REG1 = REG1 & static_cast<uint16>(0x5555);
  260. REG2 = REG2 & static_cast<uint16>(0x5555);
  261. REG1 = ((REG1 >> 1) | REG1) & static_cast<uint16>(0x3333);
  262. REG2 = ((REG2 >> 1) | REG2) & static_cast<uint16>(0x3333);
  263. REG1 = ((REG1 >> 2) | REG1) & static_cast<uint16>(0x0F0F);
  264. REG2 = ((REG2 >> 2) | REG2) & static_cast<uint16>(0x0F0F);
  265. REG1 = ((REG1 >> 4) | REG1) & static_cast<uint16>(0x00FF);
  266. REG2 = ((REG2 >> 4) | REG2) & static_cast<uint16>(0x00FF);
  267. REG1 = ((REG1 >> 8) | REG1) & static_cast<uint16>(0xFFFF);
  268. REG2 = ((REG2 >> 8) | REG2) & static_cast<uint16>(0xFFFF);
  269. return glm::u8vec2(REG1, REG2);
  270. }
  271. GLM_FUNC_QUALIFIER int32 bitfieldInterleave(int16 x, int16 y)
  272. {
  273. union sign16
  274. {
  275. int16 i;
  276. uint16 u;
  277. } sign_x, sign_y;
  278. union sign32
  279. {
  280. int32 i;
  281. uint32 u;
  282. } result;
  283. sign_x.i = x;
  284. sign_y.i = y;
  285. result.u = bitfieldInterleave(sign_x.u, sign_y.u);
  286. return result.i;
  287. }
  288. GLM_FUNC_QUALIFIER uint32 bitfieldInterleave(uint16 x, uint16 y)
  289. {
  290. return detail::bitfieldInterleave<uint16, uint32>(x, y);
  291. }
  292. GLM_FUNC_QUALIFIER glm::uint32 bitfieldInterleave(u16vec2 const& v)
  293. {
  294. return detail::bitfieldInterleave<uint16, uint32>(v.x, v.y);
  295. }
  296. GLM_FUNC_QUALIFIER glm::u16vec2 bitfieldDeinterleave(glm::uint32 x)
  297. {
  298. glm::uint32 REG1(x);
  299. glm::uint32 REG2(x >>= 1);
  300. REG1 = REG1 & static_cast<glm::uint32>(0x55555555);
  301. REG2 = REG2 & static_cast<glm::uint32>(0x55555555);
  302. REG1 = ((REG1 >> 1) | REG1) & static_cast<glm::uint32>(0x33333333);
  303. REG2 = ((REG2 >> 1) | REG2) & static_cast<glm::uint32>(0x33333333);
  304. REG1 = ((REG1 >> 2) | REG1) & static_cast<glm::uint32>(0x0F0F0F0F);
  305. REG2 = ((REG2 >> 2) | REG2) & static_cast<glm::uint32>(0x0F0F0F0F);
  306. REG1 = ((REG1 >> 4) | REG1) & static_cast<glm::uint32>(0x00FF00FF);
  307. REG2 = ((REG2 >> 4) | REG2) & static_cast<glm::uint32>(0x00FF00FF);
  308. REG1 = ((REG1 >> 8) | REG1) & static_cast<glm::uint32>(0x0000FFFF);
  309. REG2 = ((REG2 >> 8) | REG2) & static_cast<glm::uint32>(0x0000FFFF);
  310. return glm::u16vec2(REG1, REG2);
  311. }
  312. GLM_FUNC_QUALIFIER int64 bitfieldInterleave(int32 x, int32 y)
  313. {
  314. union sign32
  315. {
  316. int32 i;
  317. uint32 u;
  318. } sign_x, sign_y;
  319. union sign64
  320. {
  321. int64 i;
  322. uint64 u;
  323. } result;
  324. sign_x.i = x;
  325. sign_y.i = y;
  326. result.u = bitfieldInterleave(sign_x.u, sign_y.u);
  327. return result.i;
  328. }
  329. GLM_FUNC_QUALIFIER uint64 bitfieldInterleave(uint32 x, uint32 y)
  330. {
  331. return detail::bitfieldInterleave<uint32, uint64>(x, y);
  332. }
  333. GLM_FUNC_QUALIFIER glm::uint64 bitfieldInterleave(u32vec2 const& v)
  334. {
  335. return detail::bitfieldInterleave<uint32, uint64>(v.x, v.y);
  336. }
  337. GLM_FUNC_QUALIFIER glm::u32vec2 bitfieldDeinterleave(glm::uint64 x)
  338. {
  339. glm::uint64 REG1(x);
  340. glm::uint64 REG2(x >>= 1);
  341. REG1 = REG1 & static_cast<glm::uint64>(0x5555555555555555ull);
  342. REG2 = REG2 & static_cast<glm::uint64>(0x5555555555555555ull);
  343. REG1 = ((REG1 >> 1) | REG1) & static_cast<glm::uint64>(0x3333333333333333ull);
  344. REG2 = ((REG2 >> 1) | REG2) & static_cast<glm::uint64>(0x3333333333333333ull);
  345. REG1 = ((REG1 >> 2) | REG1) & static_cast<glm::uint64>(0x0F0F0F0F0F0F0F0Full);
  346. REG2 = ((REG2 >> 2) | REG2) & static_cast<glm::uint64>(0x0F0F0F0F0F0F0F0Full);
  347. REG1 = ((REG1 >> 4) | REG1) & static_cast<glm::uint64>(0x00FF00FF00FF00FFull);
  348. REG2 = ((REG2 >> 4) | REG2) & static_cast<glm::uint64>(0x00FF00FF00FF00FFull);
  349. REG1 = ((REG1 >> 8) | REG1) & static_cast<glm::uint64>(0x0000FFFF0000FFFFull);
  350. REG2 = ((REG2 >> 8) | REG2) & static_cast<glm::uint64>(0x0000FFFF0000FFFFull);
  351. REG1 = ((REG1 >> 16) | REG1) & static_cast<glm::uint64>(0x00000000FFFFFFFFull);
  352. REG2 = ((REG2 >> 16) | REG2) & static_cast<glm::uint64>(0x00000000FFFFFFFFull);
  353. return glm::u32vec2(REG1, REG2);
  354. }
  355. GLM_FUNC_QUALIFIER int32 bitfieldInterleave(int8 x, int8 y, int8 z)
  356. {
  357. union sign8
  358. {
  359. int8 i;
  360. uint8 u;
  361. } sign_x, sign_y, sign_z;
  362. union sign32
  363. {
  364. int32 i;
  365. uint32 u;
  366. } result;
  367. sign_x.i = x;
  368. sign_y.i = y;
  369. sign_z.i = z;
  370. result.u = bitfieldInterleave(sign_x.u, sign_y.u, sign_z.u);
  371. return result.i;
  372. }
  373. GLM_FUNC_QUALIFIER uint32 bitfieldInterleave(uint8 x, uint8 y, uint8 z)
  374. {
  375. return detail::bitfieldInterleave<uint8, uint32>(x, y, z);
  376. }
  377. GLM_FUNC_QUALIFIER uint32 bitfieldInterleave(u8vec3 const& v)
  378. {
  379. return detail::bitfieldInterleave<uint8, uint32>(v.x, v.y, v.z);
  380. }
  381. GLM_FUNC_QUALIFIER int64 bitfieldInterleave(int16 x, int16 y, int16 z)
  382. {
  383. union sign16
  384. {
  385. int16 i;
  386. uint16 u;
  387. } sign_x, sign_y, sign_z;
  388. union sign64
  389. {
  390. int64 i;
  391. uint64 u;
  392. } result;
  393. sign_x.i = x;
  394. sign_y.i = y;
  395. sign_z.i = z;
  396. result.u = bitfieldInterleave(sign_x.u, sign_y.u, sign_z.u);
  397. return result.i;
  398. }
  399. GLM_FUNC_QUALIFIER uint64 bitfieldInterleave(uint16 x, uint16 y, uint16 z)
  400. {
  401. return detail::bitfieldInterleave<uint32, uint64>(x, y, z);
  402. }
  403. GLM_FUNC_QUALIFIER uint64 bitfieldInterleave(u16vec3 const& v)
  404. {
  405. return detail::bitfieldInterleave<uint32, uint64>(v.x, v.y, v.z);
  406. }
  407. GLM_FUNC_QUALIFIER int64 bitfieldInterleave(int32 x, int32 y, int32 z)
  408. {
  409. union sign16
  410. {
  411. int32 i;
  412. uint32 u;
  413. } sign_x, sign_y, sign_z;
  414. union sign64
  415. {
  416. int64 i;
  417. uint64 u;
  418. } result;
  419. sign_x.i = x;
  420. sign_y.i = y;
  421. sign_z.i = z;
  422. result.u = bitfieldInterleave(sign_x.u, sign_y.u, sign_z.u);
  423. return result.i;
  424. }
  425. GLM_FUNC_QUALIFIER uint64 bitfieldInterleave(uint32 x, uint32 y, uint32 z)
  426. {
  427. return detail::bitfieldInterleave<uint32, uint64>(x, y, z);
  428. }
  429. GLM_FUNC_QUALIFIER uint64 bitfieldInterleave(u32vec3 const& v)
  430. {
  431. return detail::bitfieldInterleave<uint32, uint64>(v.x, v.y, v.z);
  432. }
  433. GLM_FUNC_QUALIFIER int32 bitfieldInterleave(int8 x, int8 y, int8 z, int8 w)
  434. {
  435. union sign8
  436. {
  437. int8 i;
  438. uint8 u;
  439. } sign_x, sign_y, sign_z, sign_w;
  440. union sign32
  441. {
  442. int32 i;
  443. uint32 u;
  444. } result;
  445. sign_x.i = x;
  446. sign_y.i = y;
  447. sign_z.i = z;
  448. sign_w.i = w;
  449. result.u = bitfieldInterleave(sign_x.u, sign_y.u, sign_z.u, sign_w.u);
  450. return result.i;
  451. }
  452. GLM_FUNC_QUALIFIER uint32 bitfieldInterleave(uint8 x, uint8 y, uint8 z, uint8 w)
  453. {
  454. return detail::bitfieldInterleave<uint8, uint32>(x, y, z, w);
  455. }
  456. GLM_FUNC_QUALIFIER uint32 bitfieldInterleave(u8vec4 const& v)
  457. {
  458. return detail::bitfieldInterleave<uint8, uint32>(v.x, v.y, v.z, v.w);
  459. }
  460. GLM_FUNC_QUALIFIER int64 bitfieldInterleave(int16 x, int16 y, int16 z, int16 w)
  461. {
  462. union sign16
  463. {
  464. int16 i;
  465. uint16 u;
  466. } sign_x, sign_y, sign_z, sign_w;
  467. union sign64
  468. {
  469. int64 i;
  470. uint64 u;
  471. } result;
  472. sign_x.i = x;
  473. sign_y.i = y;
  474. sign_z.i = z;
  475. sign_w.i = w;
  476. result.u = bitfieldInterleave(sign_x.u, sign_y.u, sign_z.u, sign_w.u);
  477. return result.i;
  478. }
  479. GLM_FUNC_QUALIFIER uint64 bitfieldInterleave(uint16 x, uint16 y, uint16 z, uint16 w)
  480. {
  481. return detail::bitfieldInterleave<uint16, uint64>(x, y, z, w);
  482. }
  483. GLM_FUNC_QUALIFIER uint64 bitfieldInterleave(u16vec4 const& v)
  484. {
  485. return detail::bitfieldInterleave<uint16, uint64>(v.x, v.y, v.z, v.w);
  486. }
  487. }//namespace glm