core_func_integer.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2011-05-03
  5. // Updated : 2011-05-03
  6. // Licence : This source is under MIT licence
  7. // File : test/core/func_integer.cpp
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. #include <glm/integer.hpp>
  10. #include <glm/gtc/vec1.hpp>
  11. #include <vector>
  12. #include <ctime>
  13. #include <cstdio>
  14. enum result
  15. {
  16. SUCCESS,
  17. FAIL,
  18. ASSERT,
  19. STATIC_ASSERT
  20. };
  21. namespace bitfieldInsert
  22. {
  23. template <typename genType, typename sizeType>
  24. struct type
  25. {
  26. genType Base;
  27. genType Insert;
  28. sizeType Offset;
  29. sizeType Bits;
  30. genType Return;
  31. };
  32. typedef type<glm::uint, glm::uint> typeU32;
  33. typeU32 const Data32[] =
  34. {
  35. {0x00000000, 0xffffffff, 0, 32, 0xffffffff},
  36. {0x00000000, 0xffffffff, 0, 31, 0x7fffffff},
  37. {0x00000000, 0xffffffff, 0, 0, 0x00000000},
  38. {0xff000000, 0x0000ff00, 8, 8, 0xff00ff00},
  39. {0xffff0000, 0x0000ffff, 16, 16, 0x00000000},
  40. {0x0000ffff, 0xffff0000, 16, 16, 0xffffffff}
  41. };
  42. int test()
  43. {
  44. int Error = 0;
  45. glm::uint count = sizeof(Data32) / sizeof(typeU32);
  46. for(glm::uint i = 0; i < count; ++i)
  47. {
  48. glm::uint Return = glm::bitfieldInsert(
  49. Data32[i].Base,
  50. Data32[i].Insert,
  51. Data32[i].Offset,
  52. Data32[i].Bits);
  53. Error += Data32[i].Return == Return ? 0 : 1;
  54. }
  55. return Error;
  56. }
  57. }//bitfieldInsert
  58. namespace bitfieldExtract
  59. {
  60. template <typename genType, typename sizeType>
  61. struct type
  62. {
  63. genType Value;
  64. sizeType Offset;
  65. sizeType Bits;
  66. genType Return;
  67. result Result;
  68. };
  69. typedef type<glm::uint, glm::uint> typeU32;
  70. typeU32 const Data32[] =
  71. {
  72. {0xffffffff, 0,32, 0xffffffff, SUCCESS},
  73. {0xffffffff, 8, 0, 0x00000000, SUCCESS},
  74. {0x00000000, 0,32, 0x00000000, SUCCESS},
  75. {0x0f0f0f0f, 0,32, 0x0f0f0f0f, SUCCESS},
  76. {0x00000000, 8, 0, 0x00000000, SUCCESS},
  77. {0x80000000,31, 1, 0x00000001, SUCCESS},
  78. {0x7fffffff,31, 1, 0x00000000, SUCCESS},
  79. {0x00000300, 8, 8, 0x00000003, SUCCESS},
  80. {0x0000ff00, 8, 8, 0x000000ff, SUCCESS},
  81. {0xfffffff0, 0, 5, 0x00000010, SUCCESS},
  82. {0x000000ff, 1, 3, 0x00000007, SUCCESS},
  83. {0x000000ff, 0, 3, 0x00000007, SUCCESS},
  84. {0x00000000, 0, 2, 0x00000000, SUCCESS},
  85. {0xffffffff, 0, 8, 0x000000ff, SUCCESS},
  86. {0xffff0000,16,16, 0x0000ffff, SUCCESS},
  87. {0xfffffff0, 0, 8, 0x00000000, FAIL},
  88. {0xffffffff,16,16, 0x00000000, FAIL},
  89. //{0xffffffff,32, 1, 0x00000000, ASSERT}, // Throw an assert
  90. //{0xffffffff, 0,33, 0x00000000, ASSERT}, // Throw an assert
  91. //{0xffffffff,16,16, 0x00000000, ASSERT}, // Throw an assert
  92. };
  93. int test()
  94. {
  95. int Error = 0;
  96. glm::uint count = sizeof(Data32) / sizeof(typeU32);
  97. for(glm::uint i = 0; i < count; ++i)
  98. {
  99. glm::uint Return = glm::bitfieldExtract(
  100. Data32[i].Value,
  101. Data32[i].Offset,
  102. Data32[i].Bits);
  103. bool Compare = Data32[i].Return == Return;
  104. if(Data32[i].Result == SUCCESS && Compare)
  105. continue;
  106. else if(Data32[i].Result == FAIL && !Compare)
  107. continue;
  108. Error += 1;
  109. }
  110. return Error;
  111. }
  112. }//extractField
  113. namespace bitfieldReverse
  114. {
  115. /*
  116. GLM_FUNC_QUALIFIER unsigned int bitfieldReverseLoop(unsigned int v)
  117. {
  118. unsigned int Result(0);
  119. unsigned int const BitSize = static_cast<unsigned int>(sizeof(unsigned int) * 8);
  120. for(unsigned int i = 0; i < BitSize; ++i)
  121. {
  122. unsigned int const BitSet(v & (static_cast<unsigned int>(1) << i));
  123. unsigned int const BitFirst(BitSet >> i);
  124. Result |= BitFirst << (BitSize - 1 - i);
  125. }
  126. return Result;
  127. }
  128. GLM_FUNC_QUALIFIER glm::uint64_t bitfieldReverseLoop(glm::uint64_t v)
  129. {
  130. glm::uint64_t Result(0);
  131. glm::uint64_t const BitSize = static_cast<glm::uint64_t>(sizeof(unsigned int) * 8);
  132. for(glm::uint64_t i = 0; i < BitSize; ++i)
  133. {
  134. glm::uint64_t const BitSet(v & (static_cast<glm::uint64_t>(1) << i));
  135. glm::uint64_t const BitFirst(BitSet >> i);
  136. Result |= BitFirst << (BitSize - 1 - i);
  137. }
  138. return Result;
  139. }
  140. */
  141. template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
  142. GLM_FUNC_QUALIFIER vecType<T, P> bitfieldReverseLoop(vecType<T, P> const & v)
  143. {
  144. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldReverse' only accept integer values");
  145. vecType<T, P> Result(0);
  146. T const BitSize = static_cast<T>(sizeof(T) * 8);
  147. for(T i = 0; i < BitSize; ++i)
  148. {
  149. vecType<T, P> const BitSet(v & (static_cast<T>(1) << i));
  150. vecType<T, P> const BitFirst(BitSet >> i);
  151. Result |= BitFirst << (BitSize - 1 - i);
  152. }
  153. return Result;
  154. }
  155. template <typename T>
  156. GLM_FUNC_QUALIFIER T bitfieldReverseLoop(T v)
  157. {
  158. return bitfieldReverseLoop(glm::tvec1<T>(v)).x;
  159. }
  160. GLM_FUNC_QUALIFIER glm::uint32_t bitfieldReverseUint32(glm::uint32_t x)
  161. {
  162. x = (x & 0x55555555) << 1 | (x & 0xAAAAAAAA) >> 1;
  163. x = (x & 0x33333333) << 2 | (x & 0xCCCCCCCC) >> 2;
  164. x = (x & 0x0F0F0F0F) << 4 | (x & 0xF0F0F0F0) >> 4;
  165. x = (x & 0x00FF00FF) << 8 | (x & 0xFF00FF00) >> 8;
  166. x = (x & 0x0000FFFF) << 16 | (x & 0xFFFF0000) >> 16;
  167. return x;
  168. }
  169. GLM_FUNC_QUALIFIER glm::uint64_t bitfieldReverseUint64(glm::uint64_t x)
  170. {
  171. x = (x & 0x5555555555555555) << 1 | (x & 0xAAAAAAAAAAAAAAAA) >> 1;
  172. x = (x & 0x3333333333333333) << 2 | (x & 0xCCCCCCCCCCCCCCCC) >> 2;
  173. x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x & 0xF0F0F0F0F0F0F0F0) >> 4;
  174. x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
  175. x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
  176. x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
  177. return x;
  178. }
  179. template <bool EXEC = false>
  180. struct compute_bitfieldReverseStep
  181. {
  182. template <typename T, glm::precision P, template <class, glm::precision> class vecType>
  183. GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & v, T, T)
  184. {
  185. return v;
  186. }
  187. };
  188. template <>
  189. struct compute_bitfieldReverseStep<true>
  190. {
  191. template <typename T, glm::precision P, template <class, glm::precision> class vecType>
  192. GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & v, T Mask, T Shift)
  193. {
  194. return (v & Mask) << Shift | (v & (~Mask)) >> Shift;
  195. }
  196. };
  197. template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
  198. GLM_FUNC_QUALIFIER vecType<T, P> bitfieldReverseOps(vecType<T, P> const & v)
  199. {
  200. vecType<T, P> x(v);
  201. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 2>::call(x, T(0x5555555555555555ull), static_cast<T>( 1));
  202. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 4>::call(x, T(0x3333333333333333ull), static_cast<T>( 2));
  203. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 8>::call(x, T(0x0F0F0F0F0F0F0F0Full), static_cast<T>( 4));
  204. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 16>::call(x, T(0x00FF00FF00FF00FFull), static_cast<T>( 8));
  205. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 32>::call(x, T(0x0000FFFF0000FFFFull), static_cast<T>(16));
  206. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 64>::call(x, T(0x00000000FFFFFFFFull), static_cast<T>(32));
  207. return x;
  208. }
  209. template <typename genType>
  210. GLM_FUNC_QUALIFIER genType bitfieldReverseOps(genType x)
  211. {
  212. return bitfieldReverseOps(glm::tvec1<genType, glm::defaultp>(x)).x;
  213. }
  214. template <typename genType>
  215. struct type
  216. {
  217. genType Value;
  218. genType Return;
  219. result Result;
  220. };
  221. typedef type<glm::uint> typeU32;
  222. typeU32 const Data32[] =
  223. {
  224. {0x00000001, 0x80000000, SUCCESS},
  225. {0x0000000f, 0xf0000000, SUCCESS},
  226. {0x000000ff, 0xff000000, SUCCESS},
  227. {0xf0000000, 0x0000000f, SUCCESS},
  228. {0xff000000, 0x000000ff, SUCCESS},
  229. {0xffffffff, 0xffffffff, SUCCESS},
  230. {0x00000000, 0x00000000, SUCCESS}
  231. };
  232. typedef type<glm::uint64> typeU64;
  233. #if(((GLM_COMPILER & GLM_COMPILER_GCC) == GLM_COMPILER_GCC) && (GLM_COMPILER < GLM_COMPILER_GCC44))
  234. typeU64 const Data64[] =
  235. {
  236. {0xf000000000000000LLU, 0x000000000000000fLLU, SUCCESS},
  237. {0xffffffffffffffffLLU, 0xffffffffffffffffLLU, SUCCESS},
  238. {0x0000000000000000LLU, 0x0000000000000000LLU, SUCCESS}
  239. };
  240. #else
  241. typeU64 const Data64[] =
  242. {
  243. {0x00000000000000ff, 0xff00000000000000, SUCCESS},
  244. {0x000000000000000f, 0xf000000000000000, SUCCESS},
  245. {0xf000000000000000, 0x000000000000000f, SUCCESS},
  246. {0xffffffffffffffff, 0xffffffffffffffff, SUCCESS},
  247. {0x0000000000000000, 0x0000000000000000, SUCCESS}
  248. };
  249. #endif
  250. int test32_bitfieldReverse()
  251. {
  252. int Error = 0;
  253. std::size_t const Count = sizeof(Data32) / sizeof(typeU32);
  254. for(std::size_t i = 0; i < Count; ++i)
  255. {
  256. glm::uint Return = glm::bitfieldReverse(Data32[i].Value);
  257. bool Compare = Data32[i].Return == Return;
  258. if(Data32[i].Result == SUCCESS)
  259. Error += Compare ? 0 : 1;
  260. else
  261. Error += Compare ? 1 : 0;
  262. }
  263. return Error;
  264. }
  265. int test32_bitfieldReverseLoop()
  266. {
  267. int Error = 0;
  268. std::size_t const Count = sizeof(Data32) / sizeof(typeU32);
  269. for(std::size_t i = 0; i < Count; ++i)
  270. {
  271. glm::uint Return = bitfieldReverseLoop(Data32[i].Value);
  272. bool Compare = Data32[i].Return == Return;
  273. if(Data32[i].Result == SUCCESS)
  274. Error += Compare ? 0 : 1;
  275. else
  276. Error += Compare ? 1 : 0;
  277. }
  278. return Error;
  279. }
  280. int test32_bitfieldReverseUint32()
  281. {
  282. int Error = 0;
  283. std::size_t const Count = sizeof(Data32) / sizeof(typeU32);
  284. for(std::size_t i = 0; i < Count; ++i)
  285. {
  286. glm::uint Return = bitfieldReverseUint32(Data32[i].Value);
  287. bool Compare = Data32[i].Return == Return;
  288. if(Data32[i].Result == SUCCESS)
  289. Error += Compare ? 0 : 1;
  290. else
  291. Error += Compare ? 1 : 0;
  292. }
  293. return Error;
  294. }
  295. int test32_bitfieldReverseOps()
  296. {
  297. int Error = 0;
  298. std::size_t const Count = sizeof(Data32) / sizeof(typeU32);
  299. for(std::size_t i = 0; i < Count; ++i)
  300. {
  301. glm::uint Return = bitfieldReverseOps(Data32[i].Value);
  302. bool Compare = Data32[i].Return == Return;
  303. if(Data32[i].Result == SUCCESS)
  304. Error += Compare ? 0 : 1;
  305. else
  306. Error += Compare ? 1 : 0;
  307. }
  308. return Error;
  309. }
  310. int test64_bitfieldReverse()
  311. {
  312. int Error = 0;
  313. std::size_t const Count = sizeof(Data64) / sizeof(typeU64);
  314. for(std::size_t i = 0; i < Count; ++i)
  315. {
  316. glm::uint64 Return = glm::bitfieldReverse(Data64[i].Value);
  317. bool Compare = Data64[i].Return == Return;
  318. if(Data64[i].Result == SUCCESS)
  319. Error += Compare ? 0 : 1;
  320. else
  321. Error += Compare ? 1 : 0;
  322. }
  323. return Error;
  324. }
  325. int test64_bitfieldReverseLoop()
  326. {
  327. int Error = 0;
  328. std::size_t const Count = sizeof(Data64) / sizeof(typeU64);
  329. for(std::size_t i = 0; i < Count; ++i)
  330. {
  331. glm::uint64 Return = bitfieldReverseLoop(Data64[i].Value);
  332. bool Compare = Data64[i].Return == Return;
  333. if(Data32[i].Result == SUCCESS)
  334. Error += Compare ? 0 : 1;
  335. else
  336. Error += Compare ? 1 : 0;
  337. }
  338. return Error;
  339. }
  340. int test64_bitfieldReverseUint64()
  341. {
  342. int Error = 0;
  343. std::size_t const Count = sizeof(Data64) / sizeof(typeU64);
  344. for(std::size_t i = 0; i < Count; ++i)
  345. {
  346. glm::uint64 Return = bitfieldReverseUint64(Data64[i].Value);
  347. bool Compare = Data64[i].Return == Return;
  348. if(Data64[i].Result == SUCCESS)
  349. Error += Compare ? 0 : 1;
  350. else
  351. Error += Compare ? 1 : 0;
  352. }
  353. return Error;
  354. }
  355. int test64_bitfieldReverseOps()
  356. {
  357. int Error = 0;
  358. std::size_t const Count = sizeof(Data64) / sizeof(typeU64);
  359. for(std::size_t i = 0; i < Count; ++i)
  360. {
  361. glm::uint64 Return = bitfieldReverseOps(Data64[i].Value);
  362. bool Compare = Data64[i].Return == Return;
  363. if(Data64[i].Result == SUCCESS)
  364. Error += Compare ? 0 : 1;
  365. else
  366. Error += Compare ? 1 : 0;
  367. }
  368. return Error;
  369. }
  370. int test()
  371. {
  372. int Error = 0;
  373. Error += test32_bitfieldReverse();
  374. Error += test32_bitfieldReverseLoop();
  375. Error += test32_bitfieldReverseUint32();
  376. Error += test32_bitfieldReverseOps();
  377. Error += test64_bitfieldReverse();
  378. Error += test64_bitfieldReverseLoop();
  379. Error += test64_bitfieldReverseUint64();
  380. Error += test64_bitfieldReverseOps();
  381. return Error;
  382. }
  383. int perf32()
  384. {
  385. int Error = 0;
  386. glm::uint32 Count = 10000000;
  387. std::vector<glm::uint32> Data;
  388. Data.resize(static_cast<std::size_t>(Count));
  389. std::clock_t Timestamps0 = std::clock();
  390. for(glm::uint32 k = 0; k < Count; ++k)
  391. Data[k] = glm::bitfieldReverse(k);
  392. std::clock_t Timestamps1 = std::clock();
  393. for(glm::uint32 k = 0; k < Count; ++k)
  394. Data[k] = bitfieldReverseLoop(k);
  395. std::clock_t Timestamps2 = std::clock();
  396. for(glm::uint32 k = 0; k < Count; ++k)
  397. Data[k] = bitfieldReverseUint32(k);
  398. std::clock_t Timestamps3 = std::clock();
  399. for(glm::uint32 k = 0; k < Count; ++k)
  400. Data[k] = bitfieldReverseOps(k);
  401. std::clock_t Timestamps4 = std::clock();
  402. std::printf("glm::bitfieldReverse: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
  403. std::printf("bitfieldReverseLoop: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  404. std::printf("bitfieldReverseUint32: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
  405. std::printf("bitfieldReverseOps: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
  406. return Error;
  407. }
  408. int perf64()
  409. {
  410. int Error = 0;
  411. glm::uint64 Count = 10000000;
  412. std::vector<glm::uint64> Data;
  413. Data.resize(static_cast<std::size_t>(Count));
  414. std::clock_t Timestamps0 = std::clock();
  415. for(glm::uint32 k = 0; k < Count; ++k)
  416. Data[k] = glm::bitfieldReverse(k);
  417. std::clock_t Timestamps1 = std::clock();
  418. for(glm::uint64 k = 0; k < Count; ++k)
  419. Data[k] = bitfieldReverseLoop(k);
  420. std::clock_t Timestamps2 = std::clock();
  421. for(glm::uint64 k = 0; k < Count; ++k)
  422. Data[k] = bitfieldReverseUint64(k);
  423. std::clock_t Timestamps3 = std::clock();
  424. for(glm::uint64 k = 0; k < Count; ++k)
  425. Data[k] = bitfieldReverseOps(k);
  426. std::clock_t Timestamps4 = std::clock();
  427. std::printf("glm::bitfieldReverse - 64: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
  428. std::printf("bitfieldReverseLoop - 64: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  429. std::printf("bitfieldReverseUint - 64: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
  430. std::printf("bitfieldReverseOps - 64: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
  431. return Error;
  432. }
  433. int perf()
  434. {
  435. int Error = 0;
  436. Error += perf32();
  437. Error += perf64();
  438. return Error;
  439. }
  440. }//bitfieldReverse
  441. namespace findMSB
  442. {
  443. template <typename genType>
  444. struct type
  445. {
  446. genType Value;
  447. genType Return;
  448. };
  449. template <typename genIUType>
  450. GLM_FUNC_QUALIFIER int findMSB_095(genIUType Value)
  451. {
  452. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
  453. if(Value == genIUType(0) || Value == genIUType(-1))
  454. return -1;
  455. else if(Value > 0)
  456. {
  457. genIUType Bit = genIUType(-1);
  458. for(genIUType tmp = Value; tmp > 0; tmp >>= 1, ++Bit){}
  459. return Bit;
  460. }
  461. else //if(Value < 0)
  462. {
  463. int const BitCount(sizeof(genIUType) * 8);
  464. int MostSignificantBit(-1);
  465. for(int BitIndex(0); BitIndex < BitCount; ++BitIndex)
  466. MostSignificantBit = (Value & (1 << BitIndex)) ? MostSignificantBit : BitIndex;
  467. assert(MostSignificantBit >= 0);
  468. return MostSignificantBit;
  469. }
  470. }
  471. template <typename genIUType>
  472. GLM_FUNC_QUALIFIER int findMSB_nlz1(genIUType x)
  473. {
  474. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
  475. /*
  476. int Result = 0;
  477. for(std::size_t i = 0, n = sizeof(genIUType) * 8; i < n; ++i)
  478. Result = Value & static_cast<genIUType>(1 << i) ? static_cast<int>(i) : Result;
  479. return Result;
  480. */
  481. /*
  482. genIUType Bit = genIUType(-1);
  483. for(genIUType tmp = Value; tmp > 0; tmp >>= 1, ++Bit){}
  484. return Bit;
  485. */
  486. int n;
  487. if (x == 0) return(32);
  488. n = 0;
  489. if (x <= 0x0000FFFF) {n = n +16; x = x <<16;}
  490. if (x <= 0x00FFFFFF) {n = n + 8; x = x << 8;}
  491. if (x <= 0x0FFFFFFF) {n = n + 4; x = x << 4;}
  492. if (x <= 0x3FFFFFFF) {n = n + 2; x = x << 2;}
  493. if (x <= 0x7FFFFFFF) {n = n + 1;}
  494. return n;
  495. }
  496. int findMSB_nlz2(unsigned int x)
  497. {
  498. unsigned y;
  499. int n;
  500. n = 32;
  501. y = x >>16; if (y != 0) {n = n -16; x = y;}
  502. y = x >> 8; if (y != 0) {n = n - 8; x = y;}
  503. y = x >> 4; if (y != 0) {n = n - 4; x = y;}
  504. y = x >> 2; if (y != 0) {n = n - 2; x = y;}
  505. y = x >> 1; if (y != 0) return n - 2;
  506. return n - x;
  507. }
  508. int perf_950()
  509. {
  510. type<glm::uint> const Data[] =
  511. {
  512. //{0x00000000, -1},
  513. {0x00000001, 0},
  514. {0x00000002, 1},
  515. {0x00000003, 1},
  516. {0x00000004, 2},
  517. {0x00000005, 2},
  518. {0x00000007, 2},
  519. {0x00000008, 3},
  520. {0x00000010, 4},
  521. {0x00000020, 5},
  522. {0x00000040, 6},
  523. {0x00000080, 7},
  524. {0x00000100, 8},
  525. {0x00000200, 9},
  526. {0x00000400, 10},
  527. {0x00000800, 11},
  528. {0x00001000, 12},
  529. {0x00002000, 13},
  530. {0x00004000, 14},
  531. {0x00008000, 15},
  532. {0x00010000, 16},
  533. {0x00020000, 17},
  534. {0x00040000, 18},
  535. {0x00080000, 19},
  536. {0x00100000, 20},
  537. {0x00200000, 21},
  538. {0x00400000, 22},
  539. {0x00800000, 23},
  540. {0x01000000, 24},
  541. {0x02000000, 25},
  542. {0x04000000, 26},
  543. {0x08000000, 27},
  544. {0x10000000, 28},
  545. {0x20000000, 29},
  546. {0x40000000, 30}
  547. };
  548. int Error(0);
  549. std::clock_t Timestamps1 = std::clock();
  550. for(std::size_t k = 0; k < 1000000; ++k)
  551. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  552. {
  553. int Result = findMSB_095(Data[i].Value);
  554. Error += Data[i].Return == Result ? 0 : 1;
  555. }
  556. std::clock_t Timestamps2 = std::clock();
  557. std::printf("findMSB - 0.9.5: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  558. return Error;
  559. }
  560. int perf_ops()
  561. {
  562. type<int> const Data[] =
  563. {
  564. {0x00000000, -1},
  565. {0x00000001, 0},
  566. {0x00000002, 1},
  567. {0x00000003, 1},
  568. {0x00000004, 2},
  569. {0x00000005, 2},
  570. {0x00000007, 2},
  571. {0x00000008, 3},
  572. {0x00000010, 4},
  573. {0x00000020, 5},
  574. {0x00000040, 6},
  575. {0x00000080, 7},
  576. {0x00000100, 8},
  577. {0x00000200, 9},
  578. {0x00000400, 10},
  579. {0x00000800, 11},
  580. {0x00001000, 12},
  581. {0x00002000, 13},
  582. {0x00004000, 14},
  583. {0x00008000, 15},
  584. {0x00010000, 16},
  585. {0x00020000, 17},
  586. {0x00040000, 18},
  587. {0x00080000, 19},
  588. {0x00100000, 20},
  589. {0x00200000, 21},
  590. {0x00400000, 22},
  591. {0x00800000, 23},
  592. {0x01000000, 24},
  593. {0x02000000, 25},
  594. {0x04000000, 26},
  595. {0x08000000, 27},
  596. {0x10000000, 28},
  597. {0x20000000, 29},
  598. {0x40000000, 30}
  599. };
  600. int Error(0);
  601. std::clock_t Timestamps1 = std::clock();
  602. for(std::size_t k = 0; k < 1000000; ++k)
  603. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  604. {
  605. int Result = findMSB_nlz1(Data[i].Value);
  606. Error += Data[i].Return == Result ? 0 : 1;
  607. }
  608. std::clock_t Timestamps2 = std::clock();
  609. std::printf("findMSB - nlz1: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  610. return Error;
  611. }
  612. int test_findMSB()
  613. {
  614. type<glm::uint> const Data[] =
  615. {
  616. //{0x00000000, -1},
  617. {0x00000001, 0},
  618. {0x00000002, 1},
  619. {0x00000003, 1},
  620. {0x00000004, 2},
  621. {0x00000005, 2},
  622. {0x00000007, 2},
  623. {0x00000008, 3},
  624. {0x00000010, 4},
  625. {0x00000020, 5},
  626. {0x00000040, 6},
  627. {0x00000080, 7},
  628. {0x00000100, 8},
  629. {0x00000200, 9},
  630. {0x00000400, 10},
  631. {0x00000800, 11},
  632. {0x00001000, 12},
  633. {0x00002000, 13},
  634. {0x00004000, 14},
  635. {0x00008000, 15},
  636. {0x00010000, 16},
  637. {0x00020000, 17},
  638. {0x00040000, 18},
  639. {0x00080000, 19},
  640. {0x00100000, 20},
  641. {0x00200000, 21},
  642. {0x00400000, 22},
  643. {0x00800000, 23},
  644. {0x01000000, 24},
  645. {0x02000000, 25},
  646. {0x04000000, 26},
  647. {0x08000000, 27},
  648. {0x10000000, 28},
  649. {0x20000000, 29},
  650. {0x40000000, 30}
  651. };
  652. int Error(0);
  653. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  654. {
  655. int Result = glm::findMSB(Data[i].Value);
  656. Error += Data[i].Return == Result ? 0 : 1;
  657. assert(!Error);
  658. }
  659. return Error;
  660. }
  661. int test_nlz1()
  662. {
  663. type<glm::uint> const Data[] =
  664. {
  665. //{0x00000000, -1},
  666. {0x00000001, 0},
  667. {0x00000002, 1},
  668. {0x00000003, 1},
  669. {0x00000004, 2},
  670. {0x00000005, 2},
  671. {0x00000007, 2},
  672. {0x00000008, 3},
  673. {0x00000010, 4},
  674. {0x00000020, 5},
  675. {0x00000040, 6},
  676. {0x00000080, 7},
  677. {0x00000100, 8},
  678. {0x00000200, 9},
  679. {0x00000400, 10},
  680. {0x00000800, 11},
  681. {0x00001000, 12},
  682. {0x00002000, 13},
  683. {0x00004000, 14},
  684. {0x00008000, 15},
  685. {0x00010000, 16},
  686. {0x00020000, 17},
  687. {0x00040000, 18},
  688. {0x00080000, 19},
  689. {0x00100000, 20},
  690. {0x00200000, 21},
  691. {0x00400000, 22},
  692. {0x00800000, 23},
  693. {0x01000000, 24},
  694. {0x02000000, 25},
  695. {0x04000000, 26},
  696. {0x08000000, 27},
  697. {0x10000000, 28},
  698. {0x20000000, 29},
  699. {0x40000000, 30}
  700. };
  701. int Error(0);
  702. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  703. {
  704. int Result = findMSB_nlz2(Data[i].Value);
  705. Error += Data[i].Return == Result ? 0 : 1;
  706. }
  707. return Error;
  708. }
  709. int test()
  710. {
  711. int Error(0);
  712. Error += test_findMSB();
  713. //Error += test_nlz1();
  714. return Error;
  715. }
  716. int perf()
  717. {
  718. int Error(0);
  719. Error += perf_950();
  720. //Error += perf_ops();
  721. return Error;
  722. }
  723. }//findMSB
  724. namespace findLSB
  725. {
  726. template <typename genType>
  727. struct type
  728. {
  729. genType Value;
  730. genType Return;
  731. };
  732. type<int> const DataI32[] =
  733. {
  734. {0x00000001, 0},
  735. {0x00000003, 0},
  736. {0x00000002, 1}
  737. };
  738. int test()
  739. {
  740. int Error(0);
  741. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  742. {
  743. int Result = glm::findLSB(DataI32[i].Value);
  744. Error += DataI32[i].Return == Result ? 0 : 1;
  745. assert(!Error);
  746. }
  747. return Error;
  748. }
  749. }//findLSB
  750. namespace uaddCarry
  751. {
  752. int test()
  753. {
  754. int Error(0);
  755. {
  756. glm::uint x = std::numeric_limits<glm::uint>::max();
  757. glm::uint y = 0;
  758. glm::uint Carry = 0;
  759. glm::uint Result = glm::uaddCarry(x, y, Carry);
  760. Error += Carry == 0 ? 0 : 1;
  761. Error += Result == std::numeric_limits<glm::uint>::max() ? 0 : 1;
  762. }
  763. {
  764. glm::uint x = std::numeric_limits<glm::uint>::max();
  765. glm::uint y = 1;
  766. glm::uint Carry = 0;
  767. glm::uint Result = glm::uaddCarry(x, y, Carry);
  768. Error += Carry == 1 ? 0 : 1;
  769. Error += Result == 0 ? 0 : 1;
  770. }
  771. {
  772. glm::uvec1 x(std::numeric_limits<glm::uint>::max());
  773. glm::uvec1 y(0);
  774. glm::uvec1 Carry(0);
  775. glm::uvec1 Result(glm::uaddCarry(x, y, Carry));
  776. Error += glm::all(glm::equal(Carry, glm::uvec1(0))) ? 0 : 1;
  777. Error += glm::all(glm::equal(Result, glm::uvec1(std::numeric_limits<glm::uint>::max()))) ? 0 : 1;
  778. }
  779. {
  780. glm::uvec1 x(std::numeric_limits<glm::uint>::max());
  781. glm::uvec1 y(1);
  782. glm::uvec1 Carry(0);
  783. glm::uvec1 Result(glm::uaddCarry(x, y, Carry));
  784. Error += glm::all(glm::equal(Carry, glm::uvec1(1))) ? 0 : 1;
  785. Error += glm::all(glm::equal(Result, glm::uvec1(0))) ? 0 : 1;
  786. }
  787. return Error;
  788. }
  789. }//namespace uaddCarry
  790. namespace usubBorrow
  791. {
  792. int test()
  793. {
  794. int Error(0);
  795. {
  796. glm::uint x = 16;
  797. glm::uint y = 17;
  798. glm::uint Borrow = 0;
  799. glm::uint Result = glm::usubBorrow(x, y, Borrow);
  800. Error += Borrow == 1 ? 0 : 1;
  801. Error += Result == 1 ? 0 : 1;
  802. }
  803. {
  804. glm::uvec1 x(16);
  805. glm::uvec1 y(17);
  806. glm::uvec1 Borrow(0);
  807. glm::uvec1 Result(glm::usubBorrow(x, y, Borrow));
  808. Error += glm::all(glm::equal(Borrow, glm::uvec1(1))) ? 0 : 1;
  809. Error += glm::all(glm::equal(Result, glm::uvec1(1))) ? 0 : 1;
  810. }
  811. {
  812. glm::uvec2 x(16);
  813. glm::uvec2 y(17);
  814. glm::uvec2 Borrow(0);
  815. glm::uvec2 Result(glm::usubBorrow(x, y, Borrow));
  816. Error += glm::all(glm::equal(Borrow, glm::uvec2(1))) ? 0 : 1;
  817. Error += glm::all(glm::equal(Result, glm::uvec2(1))) ? 0 : 1;
  818. }
  819. {
  820. glm::uvec3 x(16);
  821. glm::uvec3 y(17);
  822. glm::uvec3 Borrow(0);
  823. glm::uvec3 Result(glm::usubBorrow(x, y, Borrow));
  824. Error += glm::all(glm::equal(Borrow, glm::uvec3(1))) ? 0 : 1;
  825. Error += glm::all(glm::equal(Result, glm::uvec3(1))) ? 0 : 1;
  826. }
  827. {
  828. glm::uvec4 x(16);
  829. glm::uvec4 y(17);
  830. glm::uvec4 Borrow(0);
  831. glm::uvec4 Result(glm::usubBorrow(x, y, Borrow));
  832. Error += glm::all(glm::equal(Borrow, glm::uvec4(1))) ? 0 : 1;
  833. Error += glm::all(glm::equal(Result, glm::uvec4(1))) ? 0 : 1;
  834. }
  835. return Error;
  836. }
  837. }//namespace usubBorrow
  838. namespace umulExtended
  839. {
  840. int test()
  841. {
  842. int Error(0);
  843. {
  844. glm::uint x = 2;
  845. glm::uint y = 3;
  846. glm::uint msb = 0;
  847. glm::uint lsb = 0;
  848. glm::umulExtended(x, y, msb, lsb);
  849. Error += msb == 0 ? 0 : 1;
  850. Error += lsb == 6 ? 0 : 1;
  851. }
  852. {
  853. glm::uvec1 x(2);
  854. glm::uvec1 y(3);
  855. glm::uvec1 msb(0);
  856. glm::uvec1 lsb(0);
  857. glm::umulExtended(x, y, msb, lsb);
  858. Error += glm::all(glm::equal(msb, glm::uvec1(0))) ? 0 : 1;
  859. Error += glm::all(glm::equal(lsb, glm::uvec1(6))) ? 0 : 1;
  860. }
  861. {
  862. glm::uvec2 x(2);
  863. glm::uvec2 y(3);
  864. glm::uvec2 msb(0);
  865. glm::uvec2 lsb(0);
  866. glm::umulExtended(x, y, msb, lsb);
  867. Error += glm::all(glm::equal(msb, glm::uvec2(0))) ? 0 : 1;
  868. Error += glm::all(glm::equal(lsb, glm::uvec2(6))) ? 0 : 1;
  869. }
  870. {
  871. glm::uvec3 x(2);
  872. glm::uvec3 y(3);
  873. glm::uvec3 msb(0);
  874. glm::uvec3 lsb(0);
  875. glm::umulExtended(x, y, msb, lsb);
  876. Error += glm::all(glm::equal(msb, glm::uvec3(0))) ? 0 : 1;
  877. Error += glm::all(glm::equal(lsb, glm::uvec3(6))) ? 0 : 1;
  878. }
  879. {
  880. glm::uvec4 x(2);
  881. glm::uvec4 y(3);
  882. glm::uvec4 msb(0);
  883. glm::uvec4 lsb(0);
  884. glm::umulExtended(x, y, msb, lsb);
  885. Error += glm::all(glm::equal(msb, glm::uvec4(0))) ? 0 : 1;
  886. Error += glm::all(glm::equal(lsb, glm::uvec4(6))) ? 0 : 1;
  887. }
  888. return Error;
  889. }
  890. }//namespace umulExtended
  891. namespace imulExtended
  892. {
  893. int test()
  894. {
  895. int Error(0);
  896. {
  897. int x = 2;
  898. int y = 3;
  899. int msb = 0;
  900. int lsb = 0;
  901. glm::imulExtended(x, y, msb, lsb);
  902. Error += msb == 0 ? 0 : 1;
  903. Error += lsb == 6 ? 0 : 1;
  904. }
  905. {
  906. glm::ivec1 x(2);
  907. glm::ivec1 y(3);
  908. glm::ivec1 msb(0);
  909. glm::ivec1 lsb(0);
  910. glm::imulExtended(x, y, msb, lsb);
  911. Error += glm::all(glm::equal(msb, glm::ivec1(0))) ? 0 : 1;
  912. Error += glm::all(glm::equal(lsb, glm::ivec1(6))) ? 0 : 1;
  913. }
  914. {
  915. glm::ivec2 x(2);
  916. glm::ivec2 y(3);
  917. glm::ivec2 msb(0);
  918. glm::ivec2 lsb(0);
  919. glm::imulExtended(x, y, msb, lsb);
  920. Error += glm::all(glm::equal(msb, glm::ivec2(0))) ? 0 : 1;
  921. Error += glm::all(glm::equal(lsb, glm::ivec2(6))) ? 0 : 1;
  922. }
  923. {
  924. glm::ivec3 x(2);
  925. glm::ivec3 y(3);
  926. glm::ivec3 msb(0);
  927. glm::ivec3 lsb(0);
  928. glm::imulExtended(x, y, msb, lsb);
  929. Error += glm::all(glm::equal(msb, glm::ivec3(0))) ? 0 : 1;
  930. Error += glm::all(glm::equal(lsb, glm::ivec3(6))) ? 0 : 1;
  931. }
  932. {
  933. glm::ivec4 x(2);
  934. glm::ivec4 y(3);
  935. glm::ivec4 msb(0);
  936. glm::ivec4 lsb(0);
  937. glm::imulExtended(x, y, msb, lsb);
  938. Error += glm::all(glm::equal(msb, glm::ivec4(0))) ? 0 : 1;
  939. Error += glm::all(glm::equal(lsb, glm::ivec4(6))) ? 0 : 1;
  940. }
  941. return Error;
  942. }
  943. }//namespace imulExtended
  944. namespace bitCount
  945. {
  946. template <typename genType>
  947. struct type
  948. {
  949. genType Value;
  950. genType Return;
  951. };
  952. type<int> const DataI32[] =
  953. {
  954. {0x00000001, 1},
  955. {0x00000003, 2},
  956. {0x00000002, 1},
  957. {0x7fffffff, 31},
  958. {0x00000000, 0}
  959. };
  960. template <typename T>
  961. inline int bitCount_if(T v)
  962. {
  963. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitCount' only accept integer values");
  964. int Count(0);
  965. for(T i = 0, n = static_cast<T>(sizeof(T) * 8); i < n; ++i)
  966. {
  967. if(v & static_cast<T>(1 << i))
  968. ++Count;
  969. }
  970. return Count;
  971. }
  972. template <typename T>
  973. inline int bitCount_vec(T v)
  974. {
  975. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitCount' only accept integer values");
  976. int Count(0);
  977. for(T i = 0, n = static_cast<T>(sizeof(T) * 8); i < n; ++i)
  978. {
  979. Count += static_cast<int>((v >> i) & static_cast<T>(1));
  980. }
  981. return Count;
  982. }
  983. template <bool EXEC = false>
  984. struct compute_bitfieldBitCountStep
  985. {
  986. template <typename T, glm::precision P, template <class, glm::precision> class vecType>
  987. GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & v, T, T)
  988. {
  989. return v;
  990. }
  991. };
  992. template <>
  993. struct compute_bitfieldBitCountStep<true>
  994. {
  995. template <typename T, glm::precision P, template <class, glm::precision> class vecType>
  996. GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & v, T Mask, T Shift)
  997. {
  998. return (v & Mask) + ((v >> Shift) & Mask);
  999. }
  1000. };
  1001. template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
  1002. GLM_FUNC_QUALIFIER vecType<int, P> bitCount_bitfield(vecType<T, P> const & v)
  1003. {
  1004. vecType<typename glm::detail::make_unsigned<T>::type, P> x(*reinterpret_cast<vecType<typename glm::detail::make_unsigned<T>::type, P> const *>(&v));
  1005. x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 2>::call(x, typename glm::detail::make_unsigned<T>::type(0x5555555555555555ull), typename glm::detail::make_unsigned<T>::type( 1));
  1006. x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 4>::call(x, typename glm::detail::make_unsigned<T>::type(0x3333333333333333ull), typename glm::detail::make_unsigned<T>::type( 2));
  1007. x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 8>::call(x, typename glm::detail::make_unsigned<T>::type(0x0F0F0F0F0F0F0F0Full), typename glm::detail::make_unsigned<T>::type( 4));
  1008. x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 16>::call(x, typename glm::detail::make_unsigned<T>::type(0x00FF00FF00FF00FFull), typename glm::detail::make_unsigned<T>::type( 8));
  1009. x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 32>::call(x, typename glm::detail::make_unsigned<T>::type(0x0000FFFF0000FFFFull), typename glm::detail::make_unsigned<T>::type(16));
  1010. x = compute_bitfieldBitCountStep<sizeof(T) * 8 >= 64>::call(x, typename glm::detail::make_unsigned<T>::type(0x00000000FFFFFFFFull), typename glm::detail::make_unsigned<T>::type(32));
  1011. return vecType<int, P>(x);
  1012. }
  1013. template <typename genType>
  1014. GLM_FUNC_QUALIFIER int bitCount_bitfield(genType x)
  1015. {
  1016. return bitCount_bitfield(glm::tvec1<genType, glm::defaultp>(x)).x;
  1017. }
  1018. int perf()
  1019. {
  1020. int Error(0);
  1021. std::size_t Size = 10000000;
  1022. std::vector<int> v;
  1023. v.resize(Size);
  1024. std::vector<glm::ivec4> w;
  1025. w.resize(Size);
  1026. std::clock_t TimestampsA = std::clock();
  1027. // bitCount - TimeIf
  1028. {
  1029. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1030. v[i] = bitCount_if(i);
  1031. }
  1032. std::clock_t TimestampsB = std::clock();
  1033. // bitCount - TimeVec
  1034. {
  1035. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1036. v[i] = bitCount_vec(i);
  1037. }
  1038. std::clock_t TimestampsC = std::clock();
  1039. // bitCount - TimeDefault
  1040. {
  1041. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1042. v[i] = glm::bitCount(i);
  1043. }
  1044. std::clock_t TimestampsD = std::clock();
  1045. // bitCount - TimeVec4
  1046. {
  1047. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1048. w[i] = glm::bitCount(glm::ivec4(static_cast<int>(i)));
  1049. }
  1050. std::clock_t TimestampsE = std::clock();
  1051. {
  1052. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1053. v[i] = bitCount_bitfield(static_cast<int>(i));
  1054. }
  1055. std::clock_t TimestampsF = std::clock();
  1056. std::printf("bitCount - TimeIf %d\n", static_cast<unsigned int>(TimestampsB - TimestampsA));
  1057. std::printf("bitCount - TimeVec %d\n", static_cast<unsigned int>(TimestampsC - TimestampsB));
  1058. std::printf("bitCount - TimeDefault %d\n", static_cast<unsigned int>(TimestampsD - TimestampsC));
  1059. std::printf("bitCount - TimeVec4 %d\n", static_cast<unsigned int>(TimestampsE - TimestampsD));
  1060. std::printf("bitCount - bitfield %d\n", static_cast<unsigned int>(TimestampsF - TimestampsE));
  1061. return Error;
  1062. }
  1063. int test()
  1064. {
  1065. int Error(0);
  1066. for(std::size_t i = 0, n = sizeof(DataI32) / sizeof(type<int>); i < n; ++i)
  1067. {
  1068. int ResultA = glm::bitCount(DataI32[i].Value);
  1069. int ResultB = bitCount_if(DataI32[i].Value);
  1070. int ResultC = bitCount_vec(DataI32[i].Value);
  1071. int ResultE = bitCount_bitfield(DataI32[i].Value);
  1072. Error += DataI32[i].Return == ResultA ? 0 : 1;
  1073. Error += DataI32[i].Return == ResultB ? 0 : 1;
  1074. Error += DataI32[i].Return == ResultC ? 0 : 1;
  1075. Error += DataI32[i].Return == ResultE ? 0 : 1;
  1076. assert(!Error);
  1077. }
  1078. return Error;
  1079. }
  1080. }//bitCount
  1081. int main()
  1082. {
  1083. int Error = 0;
  1084. Error += ::bitCount::test();
  1085. Error += ::bitfieldReverse::test();
  1086. Error += ::findMSB::test();
  1087. Error += ::findLSB::test();
  1088. Error += ::umulExtended::test();
  1089. Error += ::imulExtended::test();
  1090. Error += ::uaddCarry::test();
  1091. Error += ::usubBorrow::test();
  1092. Error += ::bitfieldInsert::test();
  1093. Error += ::bitfieldExtract::test();
  1094. # ifdef GLM_TEST_ENABLE_PERF
  1095. Error += ::bitCount::perf();
  1096. Error += ::bitfieldReverse::perf();
  1097. Error += ::findMSB::perf();
  1098. # endif
  1099. return Error;
  1100. }