core_func_integer.cpp 39 KB

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