core_func_integer.cpp 39 KB

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