core_func_integer.cpp 41 KB

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