Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

917 lines
26 KiB

5 years ago
  1. #include <glm/gtc/bitfield.hpp>
  2. #include <glm/gtc/type_precision.hpp>
  3. #include <glm/vector_relational.hpp>
  4. #include <glm/integer.hpp>
  5. #include <ctime>
  6. #include <cstdio>
  7. #include <vector>
  8. namespace mask
  9. {
  10. template<typename genType>
  11. struct type
  12. {
  13. genType Value;
  14. genType Return;
  15. };
  16. inline int mask_zero(int Bits)
  17. {
  18. return ~((~0) << Bits);
  19. }
  20. inline int mask_mix(int Bits)
  21. {
  22. return Bits >= sizeof(int) * 8 ? 0xffffffff : (static_cast<int>(1) << Bits) - static_cast<int>(1);
  23. }
  24. inline int mask_half(int Bits)
  25. {
  26. // We do the shift in two steps because 1 << 32 on an int is undefined.
  27. int const Half = Bits >> 1;
  28. int const Fill = ~0;
  29. int const ShiftHaft = (Fill << Half);
  30. int const Rest = Bits - Half;
  31. int const Reversed = ShiftHaft << Rest;
  32. return ~Reversed;
  33. }
  34. inline int mask_loop(int Bits)
  35. {
  36. int Mask = 0;
  37. for(int Bit = 0; Bit < Bits; ++Bit)
  38. Mask |= (static_cast<int>(1) << Bit);
  39. return Mask;
  40. }
  41. int perf()
  42. {
  43. int const Count = 100000000;
  44. std::clock_t Timestamp1 = std::clock();
  45. {
  46. std::vector<int> Mask;
  47. Mask.resize(Count);
  48. for(int i = 0; i < Count; ++i)
  49. Mask[i] = mask_mix(i % 32);
  50. }
  51. std::clock_t Timestamp2 = std::clock();
  52. {
  53. std::vector<int> Mask;
  54. Mask.resize(Count);
  55. for(int i = 0; i < Count; ++i)
  56. Mask[i] = mask_loop(i % 32);
  57. }
  58. std::clock_t Timestamp3 = std::clock();
  59. {
  60. std::vector<int> Mask;
  61. Mask.resize(Count);
  62. for(int i = 0; i < Count; ++i)
  63. Mask[i] = glm::mask(i % 32);
  64. }
  65. std::clock_t Timestamp4 = std::clock();
  66. {
  67. std::vector<int> Mask;
  68. Mask.resize(Count);
  69. for(int i = 0; i < Count; ++i)
  70. Mask[i] = mask_zero(i % 32);
  71. }
  72. std::clock_t Timestamp5 = std::clock();
  73. {
  74. std::vector<int> Mask;
  75. Mask.resize(Count);
  76. for(int i = 0; i < Count; ++i)
  77. Mask[i] = mask_half(i % 32);
  78. }
  79. std::clock_t Timestamp6 = std::clock();
  80. std::clock_t TimeMix = Timestamp2 - Timestamp1;
  81. std::clock_t TimeLoop = Timestamp3 - Timestamp2;
  82. std::clock_t TimeDefault = Timestamp4 - Timestamp3;
  83. std::clock_t TimeZero = Timestamp5 - Timestamp4;
  84. std::clock_t TimeHalf = Timestamp6 - Timestamp5;
  85. printf("mask[mix]: %d\n", static_cast<unsigned int>(TimeMix));
  86. printf("mask[loop]: %d\n", static_cast<unsigned int>(TimeLoop));
  87. printf("mask[default]: %d\n", static_cast<unsigned int>(TimeDefault));
  88. printf("mask[zero]: %d\n", static_cast<unsigned int>(TimeZero));
  89. printf("mask[half]: %d\n", static_cast<unsigned int>(TimeHalf));
  90. return TimeDefault < TimeLoop ? 0 : 1;
  91. }
  92. int test_uint()
  93. {
  94. type<glm::uint> const Data[] =
  95. {
  96. { 0, 0x00000000},
  97. { 1, 0x00000001},
  98. { 2, 0x00000003},
  99. { 3, 0x00000007},
  100. {31, 0x7fffffff},
  101. {32, 0xffffffff}
  102. };
  103. int Error = 0;
  104. /* mask_zero is sadly not a correct code
  105. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  106. {
  107. int Result = mask_zero(Data[i].Value);
  108. Error += Data[i].Return == Result ? 0 : 1;
  109. }
  110. */
  111. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  112. {
  113. int Result = mask_mix(Data[i].Value);
  114. Error += Data[i].Return == Result ? 0 : 1;
  115. }
  116. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  117. {
  118. int Result = mask_half(Data[i].Value);
  119. Error += Data[i].Return == Result ? 0 : 1;
  120. }
  121. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  122. {
  123. int Result = mask_loop(Data[i].Value);
  124. Error += Data[i].Return == Result ? 0 : 1;
  125. }
  126. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  127. {
  128. int Result = glm::mask(Data[i].Value);
  129. Error += Data[i].Return == Result ? 0 : 1;
  130. }
  131. return Error;
  132. }
  133. int test_uvec4()
  134. {
  135. type<glm::ivec4> const Data[] =
  136. {
  137. {glm::ivec4( 0), glm::ivec4(0x00000000)},
  138. {glm::ivec4( 1), glm::ivec4(0x00000001)},
  139. {glm::ivec4( 2), glm::ivec4(0x00000003)},
  140. {glm::ivec4( 3), glm::ivec4(0x00000007)},
  141. {glm::ivec4(31), glm::ivec4(0x7fffffff)},
  142. {glm::ivec4(32), glm::ivec4(0xffffffff)}
  143. };
  144. int Error(0);
  145. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::ivec4>); i < n; ++i)
  146. {
  147. glm::ivec4 Result = glm::mask(Data[i].Value);
  148. Error += glm::all(glm::equal(Data[i].Return, Result)) ? 0 : 1;
  149. }
  150. return Error;
  151. }
  152. int test()
  153. {
  154. int Error(0);
  155. Error += test_uint();
  156. Error += test_uvec4();
  157. return Error;
  158. }
  159. }//namespace mask
  160. namespace bitfieldInterleave3
  161. {
  162. template<typename PARAM, typename RET>
  163. inline RET refBitfieldInterleave(PARAM x, PARAM y, PARAM z)
  164. {
  165. RET Result = 0;
  166. for(RET i = 0; i < sizeof(PARAM) * 8; ++i)
  167. {
  168. Result |= ((RET(x) & (RET(1U) << i)) << ((i << 1) + 0));
  169. Result |= ((RET(y) & (RET(1U) << i)) << ((i << 1) + 1));
  170. Result |= ((RET(z) & (RET(1U) << i)) << ((i << 1) + 2));
  171. }
  172. return Result;
  173. }
  174. int test()
  175. {
  176. int Error(0);
  177. glm::uint16 x_max = 1 << 11;
  178. glm::uint16 y_max = 1 << 11;
  179. glm::uint16 z_max = 1 << 11;
  180. for(glm::uint16 z = 0; z < z_max; z += 27)
  181. for(glm::uint16 y = 0; y < y_max; y += 27)
  182. for(glm::uint16 x = 0; x < x_max; x += 27)
  183. {
  184. glm::uint64 ResultA = refBitfieldInterleave<glm::uint16, glm::uint64>(x, y, z);
  185. glm::uint64 ResultB = glm::bitfieldInterleave(x, y, z);
  186. Error += ResultA == ResultB ? 0 : 1;
  187. }
  188. return Error;
  189. }
  190. }
  191. namespace bitfieldInterleave4
  192. {
  193. template<typename PARAM, typename RET>
  194. inline RET loopBitfieldInterleave(PARAM x, PARAM y, PARAM z, PARAM w)
  195. {
  196. RET const v[4] = {x, y, z, w};
  197. RET Result = 0;
  198. for(RET i = 0; i < sizeof(PARAM) * 8; i++)
  199. {
  200. Result |= ((((v[0] >> i) & 1U)) << ((i << 2) + 0));
  201. Result |= ((((v[1] >> i) & 1U)) << ((i << 2) + 1));
  202. Result |= ((((v[2] >> i) & 1U)) << ((i << 2) + 2));
  203. Result |= ((((v[3] >> i) & 1U)) << ((i << 2) + 3));
  204. }
  205. return Result;
  206. }
  207. int test()
  208. {
  209. int Error(0);
  210. glm::uint16 x_max = 1 << 11;
  211. glm::uint16 y_max = 1 << 11;
  212. glm::uint16 z_max = 1 << 11;
  213. glm::uint16 w_max = 1 << 11;
  214. for(glm::uint16 w = 0; w < w_max; w += 27)
  215. for(glm::uint16 z = 0; z < z_max; z += 27)
  216. for(glm::uint16 y = 0; y < y_max; y += 27)
  217. for(glm::uint16 x = 0; x < x_max; x += 27)
  218. {
  219. glm::uint64 ResultA = loopBitfieldInterleave<glm::uint16, glm::uint64>(x, y, z, w);
  220. glm::uint64 ResultB = glm::bitfieldInterleave(x, y, z, w);
  221. Error += ResultA == ResultB ? 0 : 1;
  222. }
  223. return Error;
  224. }
  225. }
  226. namespace bitfieldInterleave
  227. {
  228. inline glm::uint64 fastBitfieldInterleave(glm::uint32 x, glm::uint32 y)
  229. {
  230. glm::uint64 REG1;
  231. glm::uint64 REG2;
  232. REG1 = x;
  233. REG1 = ((REG1 << 16) | REG1) & glm::uint64(0x0000FFFF0000FFFF);
  234. REG1 = ((REG1 << 8) | REG1) & glm::uint64(0x00FF00FF00FF00FF);
  235. REG1 = ((REG1 << 4) | REG1) & glm::uint64(0x0F0F0F0F0F0F0F0F);
  236. REG1 = ((REG1 << 2) | REG1) & glm::uint64(0x3333333333333333);
  237. REG1 = ((REG1 << 1) | REG1) & glm::uint64(0x5555555555555555);
  238. REG2 = y;
  239. REG2 = ((REG2 << 16) | REG2) & glm::uint64(0x0000FFFF0000FFFF);
  240. REG2 = ((REG2 << 8) | REG2) & glm::uint64(0x00FF00FF00FF00FF);
  241. REG2 = ((REG2 << 4) | REG2) & glm::uint64(0x0F0F0F0F0F0F0F0F);
  242. REG2 = ((REG2 << 2) | REG2) & glm::uint64(0x3333333333333333);
  243. REG2 = ((REG2 << 1) | REG2) & glm::uint64(0x5555555555555555);
  244. return REG1 | (REG2 << 1);
  245. }
  246. inline glm::uint64 interleaveBitfieldInterleave(glm::uint32 x, glm::uint32 y)
  247. {
  248. glm::uint64 REG1;
  249. glm::uint64 REG2;
  250. REG1 = x;
  251. REG2 = y;
  252. REG1 = ((REG1 << 16) | REG1) & glm::uint64(0x0000FFFF0000FFFF);
  253. REG2 = ((REG2 << 16) | REG2) & glm::uint64(0x0000FFFF0000FFFF);
  254. REG1 = ((REG1 << 8) | REG1) & glm::uint64(0x00FF00FF00FF00FF);
  255. REG2 = ((REG2 << 8) | REG2) & glm::uint64(0x00FF00FF00FF00FF);
  256. REG1 = ((REG1 << 4) | REG1) & glm::uint64(0x0F0F0F0F0F0F0F0F);
  257. REG2 = ((REG2 << 4) | REG2) & glm::uint64(0x0F0F0F0F0F0F0F0F);
  258. REG1 = ((REG1 << 2) | REG1) & glm::uint64(0x3333333333333333);
  259. REG2 = ((REG2 << 2) | REG2) & glm::uint64(0x3333333333333333);
  260. REG1 = ((REG1 << 1) | REG1) & glm::uint64(0x5555555555555555);
  261. REG2 = ((REG2 << 1) | REG2) & glm::uint64(0x5555555555555555);
  262. return REG1 | (REG2 << 1);
  263. }
  264. /*
  265. inline glm::uint64 loopBitfieldInterleave(glm::uint32 x, glm::uint32 y)
  266. {
  267. static glm::uint64 const Mask[5] =
  268. {
  269. 0x5555555555555555,
  270. 0x3333333333333333,
  271. 0x0F0F0F0F0F0F0F0F,
  272. 0x00FF00FF00FF00FF,
  273. 0x0000FFFF0000FFFF
  274. };
  275. glm::uint64 REG1 = x;
  276. glm::uint64 REG2 = y;
  277. for(int i = 4; i >= 0; --i)
  278. {
  279. REG1 = ((REG1 << (1 << i)) | REG1) & Mask[i];
  280. REG2 = ((REG2 << (1 << i)) | REG2) & Mask[i];
  281. }
  282. return REG1 | (REG2 << 1);
  283. }
  284. */
  285. #if GLM_ARCH & GLM_ARCH_SSE2_BIT
  286. inline glm::uint64 sseBitfieldInterleave(glm::uint32 x, glm::uint32 y)
  287. {
  288. __m128i const Array = _mm_set_epi32(0, y, 0, x);
  289. __m128i const Mask4 = _mm_set1_epi32(0x0000FFFF);
  290. __m128i const Mask3 = _mm_set1_epi32(0x00FF00FF);
  291. __m128i const Mask2 = _mm_set1_epi32(0x0F0F0F0F);
  292. __m128i const Mask1 = _mm_set1_epi32(0x33333333);
  293. __m128i const Mask0 = _mm_set1_epi32(0x55555555);
  294. __m128i Reg1;
  295. __m128i Reg2;
  296. // REG1 = x;
  297. // REG2 = y;
  298. Reg1 = _mm_load_si128(&Array);
  299. //REG1 = ((REG1 << 16) | REG1) & glm::uint64(0x0000FFFF0000FFFF);
  300. //REG2 = ((REG2 << 16) | REG2) & glm::uint64(0x0000FFFF0000FFFF);
  301. Reg2 = _mm_slli_si128(Reg1, 2);
  302. Reg1 = _mm_or_si128(Reg2, Reg1);
  303. Reg1 = _mm_and_si128(Reg1, Mask4);
  304. //REG1 = ((REG1 << 8) | REG1) & glm::uint64(0x00FF00FF00FF00FF);
  305. //REG2 = ((REG2 << 8) | REG2) & glm::uint64(0x00FF00FF00FF00FF);
  306. Reg2 = _mm_slli_si128(Reg1, 1);
  307. Reg1 = _mm_or_si128(Reg2, Reg1);
  308. Reg1 = _mm_and_si128(Reg1, Mask3);
  309. //REG1 = ((REG1 << 4) | REG1) & glm::uint64(0x0F0F0F0F0F0F0F0F);
  310. //REG2 = ((REG2 << 4) | REG2) & glm::uint64(0x0F0F0F0F0F0F0F0F);
  311. Reg2 = _mm_slli_epi32(Reg1, 4);
  312. Reg1 = _mm_or_si128(Reg2, Reg1);
  313. Reg1 = _mm_and_si128(Reg1, Mask2);
  314. //REG1 = ((REG1 << 2) | REG1) & glm::uint64(0x3333333333333333);
  315. //REG2 = ((REG2 << 2) | REG2) & glm::uint64(0x3333333333333333);
  316. Reg2 = _mm_slli_epi32(Reg1, 2);
  317. Reg1 = _mm_or_si128(Reg2, Reg1);
  318. Reg1 = _mm_and_si128(Reg1, Mask1);
  319. //REG1 = ((REG1 << 1) | REG1) & glm::uint64(0x5555555555555555);
  320. //REG2 = ((REG2 << 1) | REG2) & glm::uint64(0x5555555555555555);
  321. Reg2 = _mm_slli_epi32(Reg1, 1);
  322. Reg1 = _mm_or_si128(Reg2, Reg1);
  323. Reg1 = _mm_and_si128(Reg1, Mask0);
  324. //return REG1 | (REG2 << 1);
  325. Reg2 = _mm_slli_epi32(Reg1, 1);
  326. Reg2 = _mm_srli_si128(Reg2, 8);
  327. Reg1 = _mm_or_si128(Reg1, Reg2);
  328. __m128i Result;
  329. _mm_store_si128(&Result, Reg1);
  330. return *reinterpret_cast<glm::uint64*>(&Result);
  331. }
  332. inline glm::uint64 sseUnalignedBitfieldInterleave(glm::uint32 x, glm::uint32 y)
  333. {
  334. __m128i const Array = _mm_set_epi32(0, y, 0, x);
  335. __m128i const Mask4 = _mm_set1_epi32(0x0000FFFF);
  336. __m128i const Mask3 = _mm_set1_epi32(0x00FF00FF);
  337. __m128i const Mask2 = _mm_set1_epi32(0x0F0F0F0F);
  338. __m128i const Mask1 = _mm_set1_epi32(0x33333333);
  339. __m128i const Mask0 = _mm_set1_epi32(0x55555555);
  340. __m128i Reg1;
  341. __m128i Reg2;
  342. // REG1 = x;
  343. // REG2 = y;
  344. Reg1 = _mm_loadu_si128(&Array);
  345. //REG1 = ((REG1 << 16) | REG1) & glm::uint64(0x0000FFFF0000FFFF);
  346. //REG2 = ((REG2 << 16) | REG2) & glm::uint64(0x0000FFFF0000FFFF);
  347. Reg2 = _mm_slli_si128(Reg1, 2);
  348. Reg1 = _mm_or_si128(Reg2, Reg1);
  349. Reg1 = _mm_and_si128(Reg1, Mask4);
  350. //REG1 = ((REG1 << 8) | REG1) & glm::uint64(0x00FF00FF00FF00FF);
  351. //REG2 = ((REG2 << 8) | REG2) & glm::uint64(0x00FF00FF00FF00FF);
  352. Reg2 = _mm_slli_si128(Reg1, 1);
  353. Reg1 = _mm_or_si128(Reg2, Reg1);
  354. Reg1 = _mm_and_si128(Reg1, Mask3);
  355. //REG1 = ((REG1 << 4) | REG1) & glm::uint64(0x0F0F0F0F0F0F0F0F);
  356. //REG2 = ((REG2 << 4) | REG2) & glm::uint64(0x0F0F0F0F0F0F0F0F);
  357. Reg2 = _mm_slli_epi32(Reg1, 4);
  358. Reg1 = _mm_or_si128(Reg2, Reg1);
  359. Reg1 = _mm_and_si128(Reg1, Mask2);
  360. //REG1 = ((REG1 << 2) | REG1) & glm::uint64(0x3333333333333333);
  361. //REG2 = ((REG2 << 2) | REG2) & glm::uint64(0x3333333333333333);
  362. Reg2 = _mm_slli_epi32(Reg1, 2);
  363. Reg1 = _mm_or_si128(Reg2, Reg1);
  364. Reg1 = _mm_and_si128(Reg1, Mask1);
  365. //REG1 = ((REG1 << 1) | REG1) & glm::uint64(0x5555555555555555);
  366. //REG2 = ((REG2 << 1) | REG2) & glm::uint64(0x5555555555555555);
  367. Reg2 = _mm_slli_epi32(Reg1, 1);
  368. Reg1 = _mm_or_si128(Reg2, Reg1);
  369. Reg1 = _mm_and_si128(Reg1, Mask0);
  370. //return REG1 | (REG2 << 1);
  371. Reg2 = _mm_slli_epi32(Reg1, 1);
  372. Reg2 = _mm_srli_si128(Reg2, 8);
  373. Reg1 = _mm_or_si128(Reg1, Reg2);
  374. __m128i Result;
  375. _mm_store_si128(&Result, Reg1);
  376. return *reinterpret_cast<glm::uint64*>(&Result);
  377. }
  378. #endif//GLM_ARCH & GLM_ARCH_SSE2_BIT
  379. int test()
  380. {
  381. int Error = 0;
  382. /*
  383. {
  384. for(glm::uint32 y = 0; y < (1 << 10); ++y)
  385. for(glm::uint32 x = 0; x < (1 << 10); ++x)
  386. {
  387. glm::uint64 A = glm::bitfieldInterleave(x, y);
  388. glm::uint64 B = fastBitfieldInterleave(x, y);
  389. //glm::uint64 C = loopBitfieldInterleave(x, y);
  390. glm::uint64 D = interleaveBitfieldInterleave(x, y);
  391. assert(A == B);
  392. //assert(A == C);
  393. assert(A == D);
  394. # if GLM_ARCH & GLM_ARCH_SSE2_BIT
  395. glm::uint64 E = sseBitfieldInterleave(x, y);
  396. glm::uint64 F = sseUnalignedBitfieldInterleave(x, y);
  397. assert(A == E);
  398. assert(A == F);
  399. __m128i G = glm_i128_interleave(_mm_set_epi32(0, y, 0, x));
  400. glm::uint64 Result[2];
  401. _mm_storeu_si128((__m128i*)Result, G);
  402. assert(A == Result[0]);
  403. # endif//GLM_ARCH & GLM_ARCH_SSE2_BIT
  404. }
  405. }
  406. */
  407. {
  408. for(glm::uint8 y = 0; y < 127; ++y)
  409. for(glm::uint8 x = 0; x < 127; ++x)
  410. {
  411. glm::uint64 A(glm::bitfieldInterleave(glm::u8vec2(x, y)));
  412. glm::uint64 B(glm::bitfieldInterleave(glm::u16vec2(x, y)));
  413. glm::uint64 C(glm::bitfieldInterleave(glm::u32vec2(x, y)));
  414. Error += A == B ? 0 : 1;
  415. Error += A == C ? 0 : 1;
  416. glm::u32vec2 const& D = glm::bitfieldDeinterleave(C);
  417. Error += D.x == x ? 0 : 1;
  418. Error += D.y == y ? 0 : 1;
  419. }
  420. }
  421. {
  422. for(glm::uint8 y = 0; y < 127; ++y)
  423. for(glm::uint8 x = 0; x < 127; ++x)
  424. {
  425. glm::int64 A(glm::bitfieldInterleave(glm::int8(x), glm::int8(y)));
  426. glm::int64 B(glm::bitfieldInterleave(glm::int16(x), glm::int16(y)));
  427. glm::int64 C(glm::bitfieldInterleave(glm::int32(x), glm::int32(y)));
  428. Error += A == B ? 0 : 1;
  429. Error += A == C ? 0 : 1;
  430. }
  431. }
  432. return Error;
  433. }
  434. int perf()
  435. {
  436. glm::uint32 x_max = 1 << 11;
  437. glm::uint32 y_max = 1 << 10;
  438. // ALU
  439. std::vector<glm::uint64> Data(x_max * y_max);
  440. std::vector<glm::u32vec2> Param(x_max * y_max);
  441. for(glm::uint32 i = 0; i < Param.size(); ++i)
  442. Param[i] = glm::u32vec2(i % x_max, i / y_max);
  443. {
  444. std::clock_t LastTime = std::clock();
  445. for(std::size_t i = 0; i < Data.size(); ++i)
  446. Data[i] = glm::bitfieldInterleave(Param[i].x, Param[i].y);
  447. std::clock_t Time = std::clock() - LastTime;
  448. std::printf("glm::bitfieldInterleave Time %d clocks\n", static_cast<int>(Time));
  449. }
  450. {
  451. std::clock_t LastTime = std::clock();
  452. for(std::size_t i = 0; i < Data.size(); ++i)
  453. Data[i] = fastBitfieldInterleave(Param[i].x, Param[i].y);
  454. std::clock_t Time = std::clock() - LastTime;
  455. std::printf("fastBitfieldInterleave Time %d clocks\n", static_cast<int>(Time));
  456. }
  457. /*
  458. {
  459. std::clock_t LastTime = std::clock();
  460. for(std::size_t i = 0; i < Data.size(); ++i)
  461. Data[i] = loopBitfieldInterleave(Param[i].x, Param[i].y);
  462. std::clock_t Time = std::clock() - LastTime;
  463. std::printf("loopBitfieldInterleave Time %d clocks\n", static_cast<int>(Time));
  464. }
  465. */
  466. {
  467. std::clock_t LastTime = std::clock();
  468. for(std::size_t i = 0; i < Data.size(); ++i)
  469. Data[i] = interleaveBitfieldInterleave(Param[i].x, Param[i].y);
  470. std::clock_t Time = std::clock() - LastTime;
  471. std::printf("interleaveBitfieldInterleave Time %d clocks\n", static_cast<int>(Time));
  472. }
  473. # if GLM_ARCH & GLM_ARCH_SSE2_BIT
  474. {
  475. std::clock_t LastTime = std::clock();
  476. for(std::size_t i = 0; i < Data.size(); ++i)
  477. Data[i] = sseBitfieldInterleave(Param[i].x, Param[i].y);
  478. std::clock_t Time = std::clock() - LastTime;
  479. std::printf("sseBitfieldInterleave Time %d clocks\n", static_cast<int>(Time));
  480. }
  481. {
  482. std::clock_t LastTime = std::clock();
  483. for(std::size_t i = 0; i < Data.size(); ++i)
  484. Data[i] = sseUnalignedBitfieldInterleave(Param[i].x, Param[i].y);
  485. std::clock_t Time = std::clock() - LastTime;
  486. std::printf("sseUnalignedBitfieldInterleave Time %d clocks\n", static_cast<int>(Time));
  487. }
  488. # endif//GLM_ARCH & GLM_ARCH_SSE2_BIT
  489. {
  490. std::clock_t LastTime = std::clock();
  491. for(std::size_t i = 0; i < Data.size(); ++i)
  492. Data[i] = glm::bitfieldInterleave(Param[i].x, Param[i].y, Param[i].x);
  493. std::clock_t Time = std::clock() - LastTime;
  494. std::printf("glm::detail::bitfieldInterleave Time %d clocks\n", static_cast<int>(Time));
  495. }
  496. # if(GLM_ARCH & GLM_ARCH_SSE2_BIT && !(GLM_COMPILER & GLM_COMPILER_GCC))
  497. {
  498. // SIMD
  499. std::vector<__m128i> SimdData;
  500. SimdData.resize(static_cast<std::size_t>(x_max * y_max));
  501. std::vector<__m128i> SimdParam;
  502. SimdParam.resize(static_cast<std::size_t>(x_max * y_max));
  503. for(std::size_t i = 0; i < SimdParam.size(); ++i)
  504. SimdParam[i] = _mm_set_epi32(static_cast<int>(i % static_cast<std::size_t>(x_max)), 0, static_cast<int>(i / static_cast<std::size_t>(y_max)), 0);
  505. std::clock_t LastTime = std::clock();
  506. for(std::size_t i = 0; i < SimdData.size(); ++i)
  507. SimdData[i] = glm_i128_interleave(SimdParam[i]);
  508. std::clock_t Time = std::clock() - LastTime;
  509. std::printf("_mm_bit_interleave_si128 Time %d clocks\n", static_cast<int>(Time));
  510. }
  511. # endif//GLM_ARCH & GLM_ARCH_SSE2_BIT
  512. return 0;
  513. }
  514. }//namespace bitfieldInterleave
  515. namespace bitfieldInterleave5
  516. {
  517. GLM_FUNC_QUALIFIER glm::uint16 bitfieldInterleave_u8vec2(glm::uint8 x, glm::uint8 y)
  518. {
  519. glm::uint32 Result = (glm::uint32(y) << 16) | glm::uint32(x);
  520. Result = ((Result << 4) | Result) & 0x0F0F0F0F;
  521. Result = ((Result << 2) | Result) & 0x33333333;
  522. Result = ((Result << 1) | Result) & 0x55555555;
  523. return static_cast<glm::uint16>((Result & 0x0000FFFF) | (Result >> 15));
  524. }
  525. GLM_FUNC_QUALIFIER glm::u8vec2 bitfieldDeinterleave_u8vec2(glm::uint16 InterleavedBitfield)
  526. {
  527. glm::uint32 Result(InterleavedBitfield);
  528. Result = ((Result << 15) | Result) & 0x55555555;
  529. Result = ((Result >> 1) | Result) & 0x33333333;
  530. Result = ((Result >> 2) | Result) & 0x0F0F0F0F;
  531. Result = ((Result >> 4) | Result) & 0x00FF00FF;
  532. return glm::u8vec2(Result & 0x0000FFFF, Result >> 16);
  533. }
  534. GLM_FUNC_QUALIFIER glm::uint32 bitfieldInterleave_u8vec4(glm::uint8 x, glm::uint8 y, glm::uint8 z, glm::uint8 w)
  535. {
  536. glm::uint64 Result = (glm::uint64(w) << 48) | (glm::uint64(z) << 32) | (glm::uint64(y) << 16) | glm::uint64(x);
  537. Result = ((Result << 12) | Result) & 0x000F000F000F000Full;
  538. Result = ((Result << 6) | Result) & 0x0303030303030303ull;
  539. Result = ((Result << 3) | Result) & 0x1111111111111111ull;
  540. const glm::uint32 a = static_cast<glm::uint32>((Result & 0x000000000000FFFF) >> ( 0 - 0));
  541. const glm::uint32 b = static_cast<glm::uint32>((Result & 0x00000000FFFF0000) >> (16 - 3));
  542. const glm::uint32 c = static_cast<glm::uint32>((Result & 0x0000FFFF00000000) >> (32 - 6));
  543. const glm::uint32 d = static_cast<glm::uint32>((Result & 0xFFFF000000000000) >> (48 - 12));
  544. return a | b | c | d;
  545. }
  546. GLM_FUNC_QUALIFIER glm::u8vec4 bitfieldDeinterleave_u8vec4(glm::uint32 InterleavedBitfield)
  547. {
  548. glm::uint64 Result(InterleavedBitfield);
  549. Result = ((Result << 15) | Result) & 0x9249249249249249ull;
  550. Result = ((Result >> 1) | Result) & 0x30C30C30C30C30C3ull;
  551. Result = ((Result >> 2) | Result) & 0xF00F00F00F00F00Full;
  552. Result = ((Result >> 4) | Result) & 0x00FF0000FF0000FFull;
  553. return glm::u8vec4(
  554. (Result >> 0) & 0x000000000000FFFFull,
  555. (Result >> 16) & 0x00000000FFFF0000ull,
  556. (Result >> 32) & 0x0000FFFF00000000ull,
  557. (Result >> 48) & 0xFFFF000000000000ull);
  558. }
  559. GLM_FUNC_QUALIFIER glm::uint32 bitfieldInterleave_u16vec2(glm::uint16 x, glm::uint16 y)
  560. {
  561. glm::uint64 Result = (glm::uint64(y) << 32) | glm::uint64(x);
  562. Result = ((Result << 8) | Result) & static_cast<glm::uint32>(0x00FF00FF00FF00FFull);
  563. Result = ((Result << 4) | Result) & static_cast<glm::uint32>(0x0F0F0F0F0F0F0F0Full);
  564. Result = ((Result << 2) | Result) & static_cast<glm::uint32>(0x3333333333333333ull);
  565. Result = ((Result << 1) | Result) & static_cast<glm::uint32>(0x5555555555555555ull);
  566. return static_cast<glm::uint32>((Result & 0x00000000FFFFFFFFull) | (Result >> 31));
  567. }
  568. GLM_FUNC_QUALIFIER glm::u16vec2 bitfieldDeinterleave_u16vec2(glm::uint32 InterleavedBitfield)
  569. {
  570. glm::uint64 Result(InterleavedBitfield);
  571. Result = ((Result << 31) | Result) & 0x5555555555555555ull;
  572. Result = ((Result >> 1) | Result) & 0x3333333333333333ull;
  573. Result = ((Result >> 2) | Result) & 0x0F0F0F0F0F0F0F0Full;
  574. Result = ((Result >> 4) | Result) & 0x00FF00FF00FF00FFull;
  575. Result = ((Result >> 8) | Result) & 0x0000FFFF0000FFFFull;
  576. return glm::u16vec2(Result & 0x00000000FFFFFFFFull, Result >> 32);
  577. }
  578. int test()
  579. {
  580. int Error = 0;
  581. for(glm::size_t j = 0; j < 256; ++j)
  582. for(glm::size_t i = 0; i < 256; ++i)
  583. {
  584. glm::uint16 A = bitfieldInterleave_u8vec2(glm::uint8(i), glm::uint8(j));
  585. glm::uint16 B = glm::bitfieldInterleave(glm::uint8(i), glm::uint8(j));
  586. Error += A == B ? 0 : 1;
  587. glm::u8vec2 C = bitfieldDeinterleave_u8vec2(A);
  588. Error += C.x == glm::uint8(i) ? 0 : 1;
  589. Error += C.y == glm::uint8(j) ? 0 : 1;
  590. }
  591. for(glm::size_t j = 0; j < 256; ++j)
  592. for(glm::size_t i = 0; i < 256; ++i)
  593. {
  594. glm::uint32 A = bitfieldInterleave_u8vec4(glm::uint8(i), glm::uint8(j), glm::uint8(i), glm::uint8(j));
  595. glm::uint32 B = glm::bitfieldInterleave(glm::uint8(i), glm::uint8(j), glm::uint8(i), glm::uint8(j));
  596. Error += A == B ? 0 : 1;
  597. /*
  598. glm::u8vec4 C = bitfieldDeinterleave_u8vec4(A);
  599. Error += C.x == glm::uint8(i) ? 0 : 1;
  600. Error += C.y == glm::uint8(j) ? 0 : 1;
  601. Error += C.z == glm::uint8(i) ? 0 : 1;
  602. Error += C.w == glm::uint8(j) ? 0 : 1;
  603. */
  604. }
  605. for(glm::size_t j = 0; j < 256; ++j)
  606. for(glm::size_t i = 0; i < 256; ++i)
  607. {
  608. glm::uint32 A = bitfieldInterleave_u16vec2(glm::uint16(i), glm::uint16(j));
  609. glm::uint32 B = glm::bitfieldInterleave(glm::uint16(i), glm::uint16(j));
  610. Error += A == B ? 0 : 1;
  611. }
  612. return Error;
  613. }
  614. int perf_old_u8vec2(std::vector<glm::uint16>& Result)
  615. {
  616. int Error = 0;
  617. const std::clock_t BeginTime = std::clock();
  618. for(glm::size_t k = 0; k < 10000; ++k)
  619. for(glm::size_t j = 0; j < 256; ++j)
  620. for(glm::size_t i = 0; i < 256; ++i)
  621. Error += Result[j * 256 + i] == glm::bitfieldInterleave(glm::uint8(i), glm::uint8(j)) ? 0 : 1;
  622. const std::clock_t EndTime = std::clock();
  623. std::printf("glm::bitfieldInterleave<u8vec2> Time %d clocks\n", static_cast<int>(EndTime - BeginTime));
  624. return Error;
  625. }
  626. int perf_new_u8vec2(std::vector<glm::uint16>& Result)
  627. {
  628. int Error = 0;
  629. const std::clock_t BeginTime = std::clock();
  630. for(glm::size_t k = 0; k < 10000; ++k)
  631. for(glm::size_t j = 0; j < 256; ++j)
  632. for(glm::size_t i = 0; i < 256; ++i)
  633. Error += Result[j * 256 + i] == bitfieldInterleave_u8vec2(glm::uint8(i), glm::uint8(j)) ? 0 : 1;
  634. const std::clock_t EndTime = std::clock();
  635. std::printf("bitfieldInterleave_u8vec2 Time %d clocks\n", static_cast<int>(EndTime - BeginTime));
  636. return Error;
  637. }
  638. int perf_old_u8vec4(std::vector<glm::uint32>& Result)
  639. {
  640. int Error = 0;
  641. const std::clock_t BeginTime = std::clock();
  642. for(glm::size_t k = 0; k < 10000; ++k)
  643. for(glm::size_t j = 0; j < 256; ++j)
  644. for(glm::size_t i = 0; i < 256; ++i)
  645. Error += Result[j * 256 + i] == glm::bitfieldInterleave(glm::uint8(i), glm::uint8(j), glm::uint8(i), glm::uint8(j)) ? 0 : 1;
  646. const std::clock_t EndTime = std::clock();
  647. std::printf("glm::bitfieldInterleave<u8vec4> Time %d clocks\n", static_cast<int>(EndTime - BeginTime));
  648. return Error;
  649. }
  650. int perf_new_u8vec4(std::vector<glm::uint32>& Result)
  651. {
  652. int Error = 0;
  653. const std::clock_t BeginTime = std::clock();
  654. for(glm::size_t k = 0; k < 10000; ++k)
  655. for(glm::size_t j = 0; j < 256; ++j)
  656. for(glm::size_t i = 0; i < 256; ++i)
  657. Error += Result[j * 256 + i] == bitfieldInterleave_u8vec4(glm::uint8(i), glm::uint8(j), glm::uint8(i), glm::uint8(j)) ? 0 : 1;
  658. const std::clock_t EndTime = std::clock();
  659. std::printf("bitfieldInterleave_u8vec4 Time %d clocks\n", static_cast<int>(EndTime - BeginTime));
  660. return Error;
  661. }
  662. int perf_old_u16vec2(std::vector<glm::uint32>& Result)
  663. {
  664. int Error = 0;
  665. const std::clock_t BeginTime = std::clock();
  666. for(glm::size_t k = 0; k < 10000; ++k)
  667. for(glm::size_t j = 0; j < 256; ++j)
  668. for(glm::size_t i = 0; i < 256; ++i)
  669. Error += Result[j * 256 + i] == glm::bitfieldInterleave(glm::uint16(i), glm::uint16(j)) ? 0 : 1;
  670. const std::clock_t EndTime = std::clock();
  671. std::printf("glm::bitfieldInterleave<u16vec2> Time %d clocks\n", static_cast<int>(EndTime - BeginTime));
  672. return Error;
  673. }
  674. int perf_new_u16vec2(std::vector<glm::uint32>& Result)
  675. {
  676. int Error = 0;
  677. const std::clock_t BeginTime = std::clock();
  678. for(glm::size_t k = 0; k < 10000; ++k)
  679. for(glm::size_t j = 0; j < 256; ++j)
  680. for(glm::size_t i = 0; i < 256; ++i)
  681. Error += Result[j * 256 + i] == bitfieldInterleave_u16vec2(glm::uint16(i), glm::uint16(j)) ? 0 : 1;
  682. const std::clock_t EndTime = std::clock();
  683. std::printf("bitfieldInterleave_u16vec2 Time %d clocks\n", static_cast<int>(EndTime - BeginTime));
  684. return Error;
  685. }
  686. int perf()
  687. {
  688. int Error = 0;
  689. std::printf("bitfieldInterleave perf: init\r");
  690. std::vector<glm::uint16> Result_u8vec2(256 * 256, 0);
  691. for(glm::size_t j = 0; j < 256; ++j)
  692. for(glm::size_t i = 0; i < 256; ++i)
  693. Result_u8vec2[j * 256 + i] = glm::bitfieldInterleave(glm::uint8(i), glm::uint8(j));
  694. Error += perf_old_u8vec2(Result_u8vec2);
  695. Error += perf_new_u8vec2(Result_u8vec2);
  696. std::vector<glm::uint32> Result_u8vec4(256 * 256, 0);
  697. for(glm::size_t j = 0; j < 256; ++j)
  698. for(glm::size_t i = 0; i < 256; ++i)
  699. Result_u8vec4[j * 256 + i] = glm::bitfieldInterleave(glm::uint8(i), glm::uint8(j), glm::uint8(i), glm::uint8(j));
  700. Error += perf_old_u8vec4(Result_u8vec4);
  701. Error += perf_new_u8vec4(Result_u8vec4);
  702. std::vector<glm::uint32> Result_u16vec2(256 * 256, 0);
  703. for(glm::size_t j = 0; j < 256; ++j)
  704. for(glm::size_t i = 0; i < 256; ++i)
  705. Result_u16vec2[j * 256 + i] = glm::bitfieldInterleave(glm::uint16(i), glm::uint16(j));
  706. Error += perf_old_u16vec2(Result_u16vec2);
  707. Error += perf_new_u16vec2(Result_u16vec2);
  708. std::printf("bitfieldInterleave perf: %d Errors\n", Error);
  709. return Error;
  710. }
  711. }//namespace bitfieldInterleave5
  712. int main()
  713. {
  714. int Error = 0;
  715. /* Tests for a faster and to reserve bitfieldInterleave
  716. Error += ::bitfieldInterleave5::test();
  717. Error += ::bitfieldInterleave5::perf();
  718. */
  719. Error += ::mask::test();
  720. Error += ::bitfieldInterleave3::test();
  721. Error += ::bitfieldInterleave4::test();
  722. Error += ::bitfieldInterleave::test();
  723. # ifdef NDEBUG
  724. Error += ::mask::perf();
  725. Error += ::bitfieldInterleave::perf();
  726. # endif//NDEBUG
  727. return Error;
  728. }