core_func_integer.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2014 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()
  407. {
  408. int Error = 0;
  409. glm::uint32 Count = 10000000;
  410. std::vector<glm::uint32> Data;
  411. Data.resize(static_cast<std::size_t>(Count));
  412. std::clock_t Timestamps0 = std::clock();
  413. for(glm::uint32 k = 0; k < Count; ++k)
  414. Data[k] = glm::bitfieldReverse(k);
  415. std::clock_t Timestamps1 = std::clock();
  416. for(glm::uint32 k = 0; k < Count; ++k)
  417. Data[k] = bitfieldReverseLoop(k);
  418. std::clock_t Timestamps2 = std::clock();
  419. for(glm::uint32 k = 0; k < Count; ++k)
  420. Data[k] = bitfieldReverseUint32(k);
  421. std::clock_t Timestamps3 = std::clock();
  422. for(glm::uint32 k = 0; k < Count; ++k)
  423. Data[k] = bitfieldReverseOps(k);
  424. std::clock_t Timestamps4 = std::clock();
  425. std::printf("glm::bitfieldReverse: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
  426. std::printf("bitfieldReverseLoop: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  427. std::printf("bitfieldReverseUint32: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
  428. std::printf("bitfieldReverseOps: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
  429. return Error;
  430. }
  431. int perf64()
  432. {
  433. int Error = 0;
  434. glm::uint64 Count = 10000000;
  435. std::vector<glm::uint64> Data;
  436. Data.resize(static_cast<std::size_t>(Count));
  437. std::clock_t Timestamps0 = std::clock();
  438. for(glm::uint32 k = 0; k < Count; ++k)
  439. Data[k] = glm::bitfieldReverse(k);
  440. std::clock_t Timestamps1 = std::clock();
  441. for(glm::uint64 k = 0; k < Count; ++k)
  442. Data[k] = bitfieldReverseLoop(k);
  443. std::clock_t Timestamps2 = std::clock();
  444. for(glm::uint64 k = 0; k < Count; ++k)
  445. Data[k] = bitfieldReverseUint64(k);
  446. std::clock_t Timestamps3 = std::clock();
  447. for(glm::uint64 k = 0; k < Count; ++k)
  448. Data[k] = bitfieldReverseOps(k);
  449. std::clock_t Timestamps4 = std::clock();
  450. std::printf("glm::bitfieldReverse - 64: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
  451. std::printf("bitfieldReverseLoop - 64: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  452. std::printf("bitfieldReverseUint - 64: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
  453. std::printf("bitfieldReverseOps - 64: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
  454. return Error;
  455. }
  456. int perf()
  457. {
  458. int Error = 0;
  459. Error += perf32();
  460. Error += perf64();
  461. return Error;
  462. }
  463. }//bitfieldReverse
  464. namespace findMSB
  465. {
  466. template <typename genType>
  467. struct type
  468. {
  469. genType Value;
  470. genType Return;
  471. };
  472. # if GLM_HAS_BITSCAN_WINDOWS
  473. template <typename genIUType>
  474. GLM_FUNC_QUALIFIER int findMSB_intrinsic(genIUType Value)
  475. {
  476. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
  477. if(Value == 0)
  478. return -1;
  479. unsigned long Result(0);
  480. _BitScanReverse(&Result, Value);
  481. return int(Result);
  482. }
  483. # endif//GLM_HAS_BITSCAN_WINDOWS
  484. # if GLM_ARCH & GLM_ARCH_AVX && GLM_COMPILER & GLM_COMPILER_VC
  485. template <typename genIUType>
  486. GLM_FUNC_QUALIFIER int findMSB_avx(genIUType Value)
  487. {
  488. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
  489. if(Value == 0)
  490. return -1;
  491. return int(_tzcnt_u32(Value));
  492. }
  493. # endif//GLM_ARCH & GLM_ARCH_AVX && GLM_PLATFORM & GLM_PLATFORM_WINDOWS
  494. template <typename genIUType>
  495. GLM_FUNC_QUALIFIER int findMSB_095(genIUType Value)
  496. {
  497. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
  498. if(Value == genIUType(0) || Value == genIUType(-1))
  499. return -1;
  500. else if(Value > 0)
  501. {
  502. genIUType Bit = genIUType(-1);
  503. for(genIUType tmp = Value; tmp > 0; tmp >>= 1, ++Bit){}
  504. return Bit;
  505. }
  506. else //if(Value < 0)
  507. {
  508. int const BitCount(sizeof(genIUType) * 8);
  509. int MostSignificantBit(-1);
  510. for(int BitIndex(0); BitIndex < BitCount; ++BitIndex)
  511. MostSignificantBit = (Value & (1 << BitIndex)) ? MostSignificantBit : BitIndex;
  512. assert(MostSignificantBit >= 0);
  513. return MostSignificantBit;
  514. }
  515. }
  516. template <typename genIUType>
  517. GLM_FUNC_QUALIFIER int findMSB_nlz1(genIUType x)
  518. {
  519. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
  520. if (x == 0)
  521. return -1;
  522. int n = 0;
  523. if (x <= 0x0000FFFF) {n = n +16; x = x <<16;}
  524. if (x <= 0x00FFFFFF) {n = n + 8; x = x << 8;}
  525. if (x <= 0x0FFFFFFF) {n = n + 4; x = x << 4;}
  526. if (x <= 0x3FFFFFFF) {n = n + 2; x = x << 2;}
  527. if (x <= 0x7FFFFFFF) {n = n + 1;}
  528. return 31 - n;
  529. }
  530. int findMSB_nlz2(unsigned int x)
  531. {
  532. unsigned y;
  533. int n;
  534. n = 32;
  535. y = x >>16; if (y != 0) {n = n -16; x = y;}
  536. y = x >> 8; if (y != 0) {n = n - 8; x = y;}
  537. y = x >> 4; if (y != 0) {n = n - 4; x = y;}
  538. y = x >> 2; if (y != 0) {n = n - 2; x = y;}
  539. y = x >> 1; if (y != 0) return n - 2;
  540. return 32 - (n - x);
  541. }
  542. int findMSB_pop(unsigned int x)
  543. {
  544. x = x | (x >> 1);
  545. x = x | (x >> 2);
  546. x = x | (x >> 4);
  547. x = x | (x >> 8);
  548. x = x | (x >>16);
  549. return 31 - glm::bitCount(~x);
  550. }
  551. int perf_int()
  552. {
  553. type<int> const Data[] =
  554. {
  555. {0x00000000, -1},
  556. {0x00000001, 0},
  557. {0x00000002, 1},
  558. {0x00000003, 1},
  559. {0x00000004, 2},
  560. {0x00000005, 2},
  561. {0x00000007, 2},
  562. {0x00000008, 3},
  563. {0x00000010, 4},
  564. {0x00000020, 5},
  565. {0x00000040, 6},
  566. {0x00000080, 7},
  567. {0x00000100, 8},
  568. {0x00000200, 9},
  569. {0x00000400, 10},
  570. {0x00000800, 11},
  571. {0x00001000, 12},
  572. {0x00002000, 13},
  573. {0x00004000, 14},
  574. {0x00008000, 15},
  575. {0x00010000, 16},
  576. {0x00020000, 17},
  577. {0x00040000, 18},
  578. {0x00080000, 19},
  579. {0x00100000, 20},
  580. {0x00200000, 21},
  581. {0x00400000, 22},
  582. {0x00800000, 23},
  583. {0x01000000, 24},
  584. {0x02000000, 25},
  585. {0x04000000, 26},
  586. {0x08000000, 27},
  587. {0x10000000, 28},
  588. {0x20000000, 29},
  589. {0x40000000, 30}
  590. };
  591. int Error(0);
  592. std::size_t const Count(10000000);
  593. std::clock_t Timestamps0 = std::clock();
  594. for(std::size_t k = 0; k < Count; ++k)
  595. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  596. {
  597. int Result = glm::findMSB(Data[i].Value);
  598. Error += Data[i].Return == Result ? 0 : 1;
  599. }
  600. std::clock_t Timestamps1 = std::clock();
  601. for(std::size_t k = 0; k < Count; ++k)
  602. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  603. {
  604. int Result = findMSB_nlz1(Data[i].Value);
  605. Error += Data[i].Return == Result ? 0 : 1;
  606. }
  607. std::clock_t Timestamps2 = std::clock();
  608. for(std::size_t k = 0; k < Count; ++k)
  609. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  610. {
  611. int Result = findMSB_nlz2(Data[i].Value);
  612. Error += Data[i].Return == Result ? 0 : 1;
  613. }
  614. std::clock_t Timestamps3 = std::clock();
  615. for(std::size_t k = 0; k < Count; ++k)
  616. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  617. {
  618. int Result = findMSB_095(Data[i].Value);
  619. Error += Data[i].Return == Result ? 0 : 1;
  620. }
  621. std::clock_t Timestamps4 = std::clock();
  622. # if GLM_HAS_BITSCAN_WINDOWS
  623. for(std::size_t k = 0; k < Count; ++k)
  624. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  625. {
  626. int Result = findMSB_intrinsic(Data[i].Value);
  627. Error += Data[i].Return == Result ? 0 : 1;
  628. }
  629. # endif//GLM_HAS_BITSCAN_WINDOWS
  630. std::clock_t Timestamps5 = std::clock();
  631. for(std::size_t k = 0; k < Count; ++k)
  632. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  633. {
  634. int Result = findMSB_pop(Data[i].Value);
  635. Error += Data[i].Return == Result ? 0 : 1;
  636. }
  637. std::clock_t Timestamps6 = std::clock();
  638. # if GLM_ARCH & GLM_ARCH_AVX && GLM_COMPILER & GLM_COMPILER_VC
  639. for(std::size_t k = 0; k < Count; ++k)
  640. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  641. {
  642. int Result = findMSB_avx(Data[i].Value);
  643. Error += Data[i].Return == Result ? 0 : 1;
  644. }
  645. # endif
  646. std::clock_t Timestamps7 = std::clock();
  647. std::printf("glm::findMSB: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
  648. std::printf("findMSB - nlz1: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  649. std::printf("findMSB - nlz2: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
  650. std::printf("findMSB - 0.9.5: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
  651. # if GLM_HAS_BITSCAN_WINDOWS
  652. std::printf("findMSB - intrinsics: %d clocks\n", static_cast<unsigned int>(Timestamps5 - Timestamps4));
  653. # endif//GLM_HAS_BITSCAN_WINDOWS
  654. std::printf("findMSB - pop: %d clocks\n", static_cast<unsigned int>(Timestamps6 - Timestamps5));
  655. # if GLM_ARCH & GLM_ARCH_AVX && GLM_COMPILER & GLM_COMPILER_VC
  656. std::printf("findMSB - avx tzcnt: %d clocks\n", static_cast<unsigned int>(Timestamps7 - Timestamps6));
  657. # endif//GLM_ARCH & GLM_ARCH_AVX && GLM_PLATFORM & GLM_PLATFORM_WINDOWS
  658. return Error;
  659. }
  660. int test_ivec4()
  661. {
  662. type<glm::ivec4> const Data[] =
  663. {
  664. {glm::ivec4(0x00000000), glm::ivec4(-1)},
  665. {glm::ivec4(0x00000001), glm::ivec4( 0)},
  666. {glm::ivec4(0x00000002), glm::ivec4( 1)},
  667. {glm::ivec4(0x00000003), glm::ivec4( 1)},
  668. {glm::ivec4(0x00000004), glm::ivec4( 2)},
  669. {glm::ivec4(0x00000005), glm::ivec4( 2)},
  670. {glm::ivec4(0x00000007), glm::ivec4( 2)},
  671. {glm::ivec4(0x00000008), glm::ivec4( 3)},
  672. {glm::ivec4(0x00000010), glm::ivec4( 4)},
  673. {glm::ivec4(0x00000020), glm::ivec4( 5)},
  674. {glm::ivec4(0x00000040), glm::ivec4( 6)},
  675. {glm::ivec4(0x00000080), glm::ivec4( 7)},
  676. {glm::ivec4(0x00000100), glm::ivec4( 8)},
  677. {glm::ivec4(0x00000200), glm::ivec4( 9)},
  678. {glm::ivec4(0x00000400), glm::ivec4(10)},
  679. {glm::ivec4(0x00000800), glm::ivec4(11)},
  680. {glm::ivec4(0x00001000), glm::ivec4(12)},
  681. {glm::ivec4(0x00002000), glm::ivec4(13)},
  682. {glm::ivec4(0x00004000), glm::ivec4(14)},
  683. {glm::ivec4(0x00008000), glm::ivec4(15)},
  684. {glm::ivec4(0x00010000), glm::ivec4(16)},
  685. {glm::ivec4(0x00020000), glm::ivec4(17)},
  686. {glm::ivec4(0x00040000), glm::ivec4(18)},
  687. {glm::ivec4(0x00080000), glm::ivec4(19)},
  688. {glm::ivec4(0x00100000), glm::ivec4(20)},
  689. {glm::ivec4(0x00200000), glm::ivec4(21)},
  690. {glm::ivec4(0x00400000), glm::ivec4(22)},
  691. {glm::ivec4(0x00800000), glm::ivec4(23)},
  692. {glm::ivec4(0x01000000), glm::ivec4(24)},
  693. {glm::ivec4(0x02000000), glm::ivec4(25)},
  694. {glm::ivec4(0x04000000), glm::ivec4(26)},
  695. {glm::ivec4(0x08000000), glm::ivec4(27)},
  696. {glm::ivec4(0x10000000), glm::ivec4(28)},
  697. {glm::ivec4(0x20000000), glm::ivec4(29)},
  698. {glm::ivec4(0x40000000), glm::ivec4(30)}
  699. };
  700. int Error(0);
  701. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::ivec4>); ++i)
  702. {
  703. glm::ivec4 Result0 = glm::findMSB(Data[i].Value);
  704. Error += glm::all(glm::equal(Data[i].Return, Result0)) ? 0 : 1;
  705. }
  706. return Error;
  707. }
  708. int test_int()
  709. {
  710. type<glm::uint> const Data[] =
  711. {
  712. {0x00000000, -1},
  713. {0x00000001, 0},
  714. {0x00000002, 1},
  715. {0x00000003, 1},
  716. {0x00000004, 2},
  717. {0x00000005, 2},
  718. {0x00000007, 2},
  719. {0x00000008, 3},
  720. {0x00000010, 4},
  721. {0x00000020, 5},
  722. {0x00000040, 6},
  723. {0x00000080, 7},
  724. {0x00000100, 8},
  725. {0x00000200, 9},
  726. {0x00000400, 10},
  727. {0x00000800, 11},
  728. {0x00001000, 12},
  729. {0x00002000, 13},
  730. {0x00004000, 14},
  731. {0x00008000, 15},
  732. {0x00010000, 16},
  733. {0x00020000, 17},
  734. {0x00040000, 18},
  735. {0x00080000, 19},
  736. {0x00100000, 20},
  737. {0x00200000, 21},
  738. {0x00400000, 22},
  739. {0x00800000, 23},
  740. {0x01000000, 24},
  741. {0x02000000, 25},
  742. {0x04000000, 26},
  743. {0x08000000, 27},
  744. {0x10000000, 28},
  745. {0x20000000, 29},
  746. {0x40000000, 30}
  747. };
  748. int Error(0);
  749. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  750. {
  751. int Result0 = glm::findMSB(Data[i].Value);
  752. Error += Data[i].Return == Result0 ? 0 : 1;
  753. }
  754. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  755. {
  756. int Result0 = findMSB_nlz1(Data[i].Value);
  757. Error += Data[i].Return == Result0 ? 0 : 1;
  758. }
  759. /*
  760. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  761. {
  762. int Result0 = findMSB_nlz2(Data[i].Value);
  763. Error += Data[i].Return == Result0 ? 0 : 1;
  764. }
  765. */
  766. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  767. {
  768. int Result0 = findMSB_095(Data[i].Value);
  769. Error += Data[i].Return == Result0 ? 0 : 1;
  770. }
  771. # if GLM_HAS_BITSCAN_WINDOWS
  772. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  773. {
  774. int Result0 = findMSB_intrinsic(Data[i].Value);
  775. Error += Data[i].Return == Result0 ? 0 : 1;
  776. }
  777. # endif//GLM_HAS_BITSCAN_WINDOWS
  778. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int>); ++i)
  779. {
  780. int Result0 = findMSB_pop(Data[i].Value);
  781. Error += Data[i].Return == Result0 ? 0 : 1;
  782. }
  783. return Error;
  784. }
  785. int test()
  786. {
  787. int Error(0);
  788. Error += test_ivec4();
  789. Error += test_int();
  790. return Error;
  791. }
  792. int perf()
  793. {
  794. int Error(0);
  795. Error += perf_int();
  796. return Error;
  797. }
  798. }//findMSB
  799. namespace findLSB
  800. {
  801. template <typename genType>
  802. struct type
  803. {
  804. genType Value;
  805. genType Return;
  806. };
  807. type<int> const DataI32[] =
  808. {
  809. {0x00000001, 0},
  810. {0x00000003, 0},
  811. {0x00000002, 1},
  812. {0x80000000, 31},
  813. {0x00010000, 16},
  814. {0xFFFF0000, 16},
  815. {0xFF000000, 24},
  816. {0xFF00FF00, 8},
  817. {0x00000000, -1}
  818. };
  819. # if GLM_HAS_BITSCAN_WINDOWS
  820. template <typename genIUType>
  821. GLM_FUNC_QUALIFIER int findLSB_intrinsic(genIUType Value)
  822. {
  823. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findLSB' only accept integer values");
  824. if(Value == 0)
  825. return -1;
  826. unsigned long Result(0);
  827. _BitScanForward(&Result, Value);
  828. return int(Result);
  829. }
  830. # endif
  831. template <typename genIUType>
  832. GLM_FUNC_QUALIFIER int findLSB_095(genIUType Value)
  833. {
  834. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findLSB' only accept integer values");
  835. if(Value == 0)
  836. return -1;
  837. genIUType Bit;
  838. for(Bit = genIUType(0); !(Value & (1 << Bit)); ++Bit){}
  839. return Bit;
  840. }
  841. template <typename genIUType>
  842. GLM_FUNC_QUALIFIER int findLSB_ntz2(genIUType x)
  843. {
  844. if(x == 0)
  845. return -1;
  846. return glm::bitCount(~x & (x - static_cast<genIUType>(1)));
  847. }
  848. template <typename genIUType>
  849. GLM_FUNC_QUALIFIER int findLSB_branchfree(genIUType x)
  850. {
  851. bool IsNull(x == 0);
  852. int const Keep(!IsNull);
  853. int const Discard(IsNull);
  854. return static_cast<int>(glm::bitCount(~x & (x - static_cast<genIUType>(1)))) * Keep + Discard * -1;
  855. }
  856. int test_int()
  857. {
  858. int Error(0);
  859. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  860. {
  861. int Result = glm::findLSB(DataI32[i].Value);
  862. Error += DataI32[i].Return == Result ? 0 : 1;
  863. }
  864. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  865. {
  866. int Result = findLSB_095(DataI32[i].Value);
  867. Error += DataI32[i].Return == Result ? 0 : 1;
  868. }
  869. # if GLM_HAS_BITSCAN_WINDOWS
  870. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  871. {
  872. int Result = findLSB_intrinsic(DataI32[i].Value);
  873. Error += DataI32[i].Return == Result ? 0 : 1;
  874. }
  875. # endif
  876. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  877. {
  878. int Result = findLSB_ntz2(DataI32[i].Value);
  879. Error += DataI32[i].Return == Result ? 0 : 1;
  880. }
  881. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  882. {
  883. int Result = findLSB_branchfree(DataI32[i].Value);
  884. Error += DataI32[i].Return == Result ? 0 : 1;
  885. }
  886. return Error;
  887. }
  888. int test()
  889. {
  890. int Error(0);
  891. Error += test_int();
  892. return Error;
  893. }
  894. int perf_int()
  895. {
  896. int Error(0);
  897. std::size_t const Count(10000000);
  898. std::clock_t Timestamps0 = std::clock();
  899. for(std::size_t k = 0; k < Count; ++k)
  900. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  901. {
  902. int Result = glm::findLSB(DataI32[i].Value);
  903. Error += DataI32[i].Return == Result ? 0 : 1;
  904. }
  905. std::clock_t Timestamps1 = std::clock();
  906. for(std::size_t k = 0; k < Count; ++k)
  907. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  908. {
  909. int Result = findLSB_095(DataI32[i].Value);
  910. Error += DataI32[i].Return == Result ? 0 : 1;
  911. }
  912. std::clock_t Timestamps2 = std::clock();
  913. # if GLM_HAS_BITSCAN_WINDOWS
  914. for(std::size_t k = 0; k < Count; ++k)
  915. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  916. {
  917. int Result = findLSB_intrinsic(DataI32[i].Value);
  918. Error += DataI32[i].Return == Result ? 0 : 1;
  919. }
  920. # endif
  921. std::clock_t Timestamps3 = std::clock();
  922. for(std::size_t k = 0; k < Count; ++k)
  923. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  924. {
  925. int Result = findLSB_ntz2(DataI32[i].Value);
  926. Error += DataI32[i].Return == Result ? 0 : 1;
  927. }
  928. std::clock_t Timestamps4 = std::clock();
  929. for(std::size_t k = 0; k < Count; ++k)
  930. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(type<int>); ++i)
  931. {
  932. int Result = findLSB_branchfree(DataI32[i].Value);
  933. Error += DataI32[i].Return == Result ? 0 : 1;
  934. }
  935. std::clock_t Timestamps5 = std::clock();
  936. std::printf("glm::findLSB: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
  937. std::printf("findLSB - 0.9.5: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  938. # if GLM_HAS_BITSCAN_WINDOWS
  939. std::printf("findLSB - intrinsics: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
  940. # endif
  941. std::printf("findLSB - ntz2: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
  942. std::printf("findLSB - branchfree: %d clocks\n", static_cast<unsigned int>(Timestamps5 - Timestamps4));
  943. return Error;
  944. }
  945. int perf()
  946. {
  947. int Error(0);
  948. Error += perf_int();
  949. return Error;
  950. }
  951. }//findLSB
  952. namespace uaddCarry
  953. {
  954. int test()
  955. {
  956. int Error(0);
  957. {
  958. glm::uint x = std::numeric_limits<glm::uint>::max();
  959. glm::uint y = 0;
  960. glm::uint Carry = 0;
  961. glm::uint Result = glm::uaddCarry(x, y, Carry);
  962. Error += Carry == 0 ? 0 : 1;
  963. Error += Result == std::numeric_limits<glm::uint>::max() ? 0 : 1;
  964. }
  965. {
  966. glm::uint x = std::numeric_limits<glm::uint>::max();
  967. glm::uint y = 1;
  968. glm::uint Carry = 0;
  969. glm::uint Result = glm::uaddCarry(x, y, Carry);
  970. Error += Carry == 1 ? 0 : 1;
  971. Error += Result == 0 ? 0 : 1;
  972. }
  973. {
  974. glm::uvec1 x(std::numeric_limits<glm::uint>::max());
  975. glm::uvec1 y(0);
  976. glm::uvec1 Carry(0);
  977. glm::uvec1 Result(glm::uaddCarry(x, y, Carry));
  978. Error += glm::all(glm::equal(Carry, glm::uvec1(0))) ? 0 : 1;
  979. Error += glm::all(glm::equal(Result, glm::uvec1(std::numeric_limits<glm::uint>::max()))) ? 0 : 1;
  980. }
  981. {
  982. glm::uvec1 x(std::numeric_limits<glm::uint>::max());
  983. glm::uvec1 y(1);
  984. glm::uvec1 Carry(0);
  985. glm::uvec1 Result(glm::uaddCarry(x, y, Carry));
  986. Error += glm::all(glm::equal(Carry, glm::uvec1(1))) ? 0 : 1;
  987. Error += glm::all(glm::equal(Result, glm::uvec1(0))) ? 0 : 1;
  988. }
  989. return Error;
  990. }
  991. }//namespace uaddCarry
  992. namespace usubBorrow
  993. {
  994. int test()
  995. {
  996. int Error(0);
  997. {
  998. glm::uint x = 16;
  999. glm::uint y = 17;
  1000. glm::uint Borrow = 0;
  1001. glm::uint Result = glm::usubBorrow(x, y, Borrow);
  1002. Error += Borrow == 1 ? 0 : 1;
  1003. Error += Result == 1 ? 0 : 1;
  1004. }
  1005. {
  1006. glm::uvec1 x(16);
  1007. glm::uvec1 y(17);
  1008. glm::uvec1 Borrow(0);
  1009. glm::uvec1 Result(glm::usubBorrow(x, y, Borrow));
  1010. Error += glm::all(glm::equal(Borrow, glm::uvec1(1))) ? 0 : 1;
  1011. Error += glm::all(glm::equal(Result, glm::uvec1(1))) ? 0 : 1;
  1012. }
  1013. {
  1014. glm::uvec2 x(16);
  1015. glm::uvec2 y(17);
  1016. glm::uvec2 Borrow(0);
  1017. glm::uvec2 Result(glm::usubBorrow(x, y, Borrow));
  1018. Error += glm::all(glm::equal(Borrow, glm::uvec2(1))) ? 0 : 1;
  1019. Error += glm::all(glm::equal(Result, glm::uvec2(1))) ? 0 : 1;
  1020. }
  1021. {
  1022. glm::uvec3 x(16);
  1023. glm::uvec3 y(17);
  1024. glm::uvec3 Borrow(0);
  1025. glm::uvec3 Result(glm::usubBorrow(x, y, Borrow));
  1026. Error += glm::all(glm::equal(Borrow, glm::uvec3(1))) ? 0 : 1;
  1027. Error += glm::all(glm::equal(Result, glm::uvec3(1))) ? 0 : 1;
  1028. }
  1029. {
  1030. glm::uvec4 x(16);
  1031. glm::uvec4 y(17);
  1032. glm::uvec4 Borrow(0);
  1033. glm::uvec4 Result(glm::usubBorrow(x, y, Borrow));
  1034. Error += glm::all(glm::equal(Borrow, glm::uvec4(1))) ? 0 : 1;
  1035. Error += glm::all(glm::equal(Result, glm::uvec4(1))) ? 0 : 1;
  1036. }
  1037. return Error;
  1038. }
  1039. }//namespace usubBorrow
  1040. namespace umulExtended
  1041. {
  1042. int test()
  1043. {
  1044. int Error(0);
  1045. {
  1046. glm::uint x = 2;
  1047. glm::uint y = 3;
  1048. glm::uint msb = 0;
  1049. glm::uint lsb = 0;
  1050. glm::umulExtended(x, y, msb, lsb);
  1051. Error += msb == 0 ? 0 : 1;
  1052. Error += lsb == 6 ? 0 : 1;
  1053. }
  1054. {
  1055. glm::uvec1 x(2);
  1056. glm::uvec1 y(3);
  1057. glm::uvec1 msb(0);
  1058. glm::uvec1 lsb(0);
  1059. glm::umulExtended(x, y, msb, lsb);
  1060. Error += glm::all(glm::equal(msb, glm::uvec1(0))) ? 0 : 1;
  1061. Error += glm::all(glm::equal(lsb, glm::uvec1(6))) ? 0 : 1;
  1062. }
  1063. {
  1064. glm::uvec2 x(2);
  1065. glm::uvec2 y(3);
  1066. glm::uvec2 msb(0);
  1067. glm::uvec2 lsb(0);
  1068. glm::umulExtended(x, y, msb, lsb);
  1069. Error += glm::all(glm::equal(msb, glm::uvec2(0))) ? 0 : 1;
  1070. Error += glm::all(glm::equal(lsb, glm::uvec2(6))) ? 0 : 1;
  1071. }
  1072. {
  1073. glm::uvec3 x(2);
  1074. glm::uvec3 y(3);
  1075. glm::uvec3 msb(0);
  1076. glm::uvec3 lsb(0);
  1077. glm::umulExtended(x, y, msb, lsb);
  1078. Error += glm::all(glm::equal(msb, glm::uvec3(0))) ? 0 : 1;
  1079. Error += glm::all(glm::equal(lsb, glm::uvec3(6))) ? 0 : 1;
  1080. }
  1081. {
  1082. glm::uvec4 x(2);
  1083. glm::uvec4 y(3);
  1084. glm::uvec4 msb(0);
  1085. glm::uvec4 lsb(0);
  1086. glm::umulExtended(x, y, msb, lsb);
  1087. Error += glm::all(glm::equal(msb, glm::uvec4(0))) ? 0 : 1;
  1088. Error += glm::all(glm::equal(lsb, glm::uvec4(6))) ? 0 : 1;
  1089. }
  1090. return Error;
  1091. }
  1092. }//namespace umulExtended
  1093. namespace imulExtended
  1094. {
  1095. int test()
  1096. {
  1097. int Error(0);
  1098. {
  1099. int x = 2;
  1100. int y = 3;
  1101. int msb = 0;
  1102. int lsb = 0;
  1103. glm::imulExtended(x, y, msb, lsb);
  1104. Error += msb == 0 ? 0 : 1;
  1105. Error += lsb == 6 ? 0 : 1;
  1106. }
  1107. {
  1108. glm::ivec1 x(2);
  1109. glm::ivec1 y(3);
  1110. glm::ivec1 msb(0);
  1111. glm::ivec1 lsb(0);
  1112. glm::imulExtended(x, y, msb, lsb);
  1113. Error += glm::all(glm::equal(msb, glm::ivec1(0))) ? 0 : 1;
  1114. Error += glm::all(glm::equal(lsb, glm::ivec1(6))) ? 0 : 1;
  1115. }
  1116. {
  1117. glm::ivec2 x(2);
  1118. glm::ivec2 y(3);
  1119. glm::ivec2 msb(0);
  1120. glm::ivec2 lsb(0);
  1121. glm::imulExtended(x, y, msb, lsb);
  1122. Error += glm::all(glm::equal(msb, glm::ivec2(0))) ? 0 : 1;
  1123. Error += glm::all(glm::equal(lsb, glm::ivec2(6))) ? 0 : 1;
  1124. }
  1125. {
  1126. glm::ivec3 x(2);
  1127. glm::ivec3 y(3);
  1128. glm::ivec3 msb(0);
  1129. glm::ivec3 lsb(0);
  1130. glm::imulExtended(x, y, msb, lsb);
  1131. Error += glm::all(glm::equal(msb, glm::ivec3(0))) ? 0 : 1;
  1132. Error += glm::all(glm::equal(lsb, glm::ivec3(6))) ? 0 : 1;
  1133. }
  1134. {
  1135. glm::ivec4 x(2);
  1136. glm::ivec4 y(3);
  1137. glm::ivec4 msb(0);
  1138. glm::ivec4 lsb(0);
  1139. glm::imulExtended(x, y, msb, lsb);
  1140. Error += glm::all(glm::equal(msb, glm::ivec4(0))) ? 0 : 1;
  1141. Error += glm::all(glm::equal(lsb, glm::ivec4(6))) ? 0 : 1;
  1142. }
  1143. return Error;
  1144. }
  1145. }//namespace imulExtended
  1146. namespace bitCount
  1147. {
  1148. template <typename genType>
  1149. struct type
  1150. {
  1151. genType Value;
  1152. genType Return;
  1153. };
  1154. type<int> const DataI32[] =
  1155. {
  1156. {0x00000001, 1},
  1157. {0x00000003, 2},
  1158. {0x00000002, 1},
  1159. {0x7fffffff, 31},
  1160. {0x00000000, 0}
  1161. };
  1162. template <typename T>
  1163. inline int bitCount_if(T v)
  1164. {
  1165. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitCount' only accept integer values");
  1166. int Count(0);
  1167. for(T i = 0, n = static_cast<T>(sizeof(T) * 8); i < n; ++i)
  1168. {
  1169. if(v & static_cast<T>(1 << i))
  1170. ++Count;
  1171. }
  1172. return Count;
  1173. }
  1174. template <typename T>
  1175. inline int bitCount_vec(T v)
  1176. {
  1177. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitCount' only accept integer values");
  1178. int Count(0);
  1179. for(T i = 0, n = static_cast<T>(sizeof(T) * 8); i < n; ++i)
  1180. {
  1181. Count += static_cast<int>((v >> i) & static_cast<T>(1));
  1182. }
  1183. return Count;
  1184. }
  1185. template <bool EXEC = false>
  1186. struct compute_bitfieldBitCountStep
  1187. {
  1188. template <typename T, glm::precision P, template <class, glm::precision> class vecType>
  1189. GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & v, T, T)
  1190. {
  1191. return v;
  1192. }
  1193. };
  1194. template <>
  1195. struct compute_bitfieldBitCountStep<true>
  1196. {
  1197. template <typename T, glm::precision P, template <class, glm::precision> class vecType>
  1198. GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & v, T Mask, T Shift)
  1199. {
  1200. return (v & Mask) + ((v >> Shift) & Mask);
  1201. }
  1202. };
  1203. template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
  1204. GLM_FUNC_QUALIFIER vecType<int, P> bitCount_bitfield(vecType<T, P> const & v)
  1205. {
  1206. vecType<typename glm::detail::make_unsigned<T>::type, P> x(*reinterpret_cast<vecType<typename glm::detail::make_unsigned<T>::type, P> const *>(&v));
  1207. 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));
  1208. 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));
  1209. 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));
  1210. 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));
  1211. 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));
  1212. 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));
  1213. return vecType<int, P>(x);
  1214. }
  1215. template <typename genType>
  1216. GLM_FUNC_QUALIFIER int bitCount_bitfield(genType x)
  1217. {
  1218. return bitCount_bitfield(glm::tvec1<genType, glm::defaultp>(x)).x;
  1219. }
  1220. int perf()
  1221. {
  1222. int Error(0);
  1223. std::size_t Size = 10000000;
  1224. std::vector<int> v;
  1225. v.resize(Size);
  1226. std::vector<glm::ivec4> w;
  1227. w.resize(Size);
  1228. std::clock_t TimestampsA = std::clock();
  1229. // bitCount - TimeIf
  1230. {
  1231. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1232. v[i] = bitCount_if(i);
  1233. }
  1234. std::clock_t TimestampsB = std::clock();
  1235. // bitCount - TimeVec
  1236. {
  1237. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1238. v[i] = bitCount_vec(i);
  1239. }
  1240. std::clock_t TimestampsC = std::clock();
  1241. // bitCount - TimeDefault
  1242. {
  1243. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1244. v[i] = glm::bitCount(i);
  1245. }
  1246. std::clock_t TimestampsD = std::clock();
  1247. // bitCount - TimeVec4
  1248. {
  1249. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1250. w[i] = glm::bitCount(glm::ivec4(static_cast<int>(i)));
  1251. }
  1252. std::clock_t TimestampsE = std::clock();
  1253. {
  1254. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1255. v[i] = bitCount_bitfield(static_cast<int>(i));
  1256. }
  1257. std::clock_t TimestampsF = std::clock();
  1258. std::printf("bitCount - TimeIf %d\n", static_cast<unsigned int>(TimestampsB - TimestampsA));
  1259. std::printf("bitCount - TimeVec %d\n", static_cast<unsigned int>(TimestampsC - TimestampsB));
  1260. std::printf("bitCount - TimeDefault %d\n", static_cast<unsigned int>(TimestampsD - TimestampsC));
  1261. std::printf("bitCount - TimeVec4 %d\n", static_cast<unsigned int>(TimestampsE - TimestampsD));
  1262. std::printf("bitCount - bitfield %d\n", static_cast<unsigned int>(TimestampsF - TimestampsE));
  1263. return Error;
  1264. }
  1265. int test()
  1266. {
  1267. int Error(0);
  1268. for(std::size_t i = 0, n = sizeof(DataI32) / sizeof(type<int>); i < n; ++i)
  1269. {
  1270. int ResultA = glm::bitCount(DataI32[i].Value);
  1271. int ResultB = bitCount_if(DataI32[i].Value);
  1272. int ResultC = bitCount_vec(DataI32[i].Value);
  1273. int ResultE = bitCount_bitfield(DataI32[i].Value);
  1274. Error += DataI32[i].Return == ResultA ? 0 : 1;
  1275. Error += DataI32[i].Return == ResultB ? 0 : 1;
  1276. Error += DataI32[i].Return == ResultC ? 0 : 1;
  1277. Error += DataI32[i].Return == ResultE ? 0 : 1;
  1278. assert(!Error);
  1279. }
  1280. return Error;
  1281. }
  1282. }//bitCount
  1283. int main()
  1284. {
  1285. int Error = 0;
  1286. Error += ::bitCount::test();
  1287. Error += ::bitfieldReverse::test();
  1288. Error += ::findMSB::test();
  1289. Error += ::findLSB::test();
  1290. Error += ::umulExtended::test();
  1291. Error += ::imulExtended::test();
  1292. Error += ::uaddCarry::test();
  1293. Error += ::usubBorrow::test();
  1294. Error += ::bitfieldInsert::test();
  1295. Error += ::bitfieldExtract::test();
  1296. Error += ::findMSB::perf();
  1297. # ifdef GLM_TEST_ENABLE_PERF
  1298. Error += ::bitCount::perf();
  1299. Error += ::bitfieldReverse::perf();
  1300. Error += ::findMSB::perf();
  1301. Error += ::findLSB::perf();
  1302. # endif
  1303. return Error;
  1304. }