core_func_integer.cpp 39 KB

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