core_func_integer.cpp 38 KB

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