core_func_integer.cpp 29 KB

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