2
0

core_func_integer.cpp 32 KB

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