core_func_integer.cpp 39 KB

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