core_func_integer.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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>
  17. struct type
  18. {
  19. genType Base;
  20. genType Insert;
  21. int Offset;
  22. int Bits;
  23. genType Return;
  24. };
  25. typedef type<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. static 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>
  54. struct type
  55. {
  56. genType Value;
  57. int Offset;
  58. int Bits;
  59. genType Return;
  60. result Result;
  61. };
  62. typedef type<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. static 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::qualifier Q>
  135. GLM_FUNC_QUALIFIER glm::vec<L, T, Q> bitfieldReverseLoop(glm::vec<L, T, Q> const& v)
  136. {
  137. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldReverse' only accept integer values");
  138. glm::vec<L, T, Q> Result(0);
  139. T const BitSize = static_cast<T>(sizeof(T) * 8);
  140. for(T i = 0; i < BitSize; ++i)
  141. {
  142. glm::vec<L, T, Q> const BitSet(v & (static_cast<T>(1) << i));
  143. glm::vec<L, T, Q> 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::qualifier Q>
  176. GLM_FUNC_QUALIFIER static glm::vec<L, T, Q> call(glm::vec<L, T, Q> 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::qualifier Q>
  185. GLM_FUNC_QUALIFIER static glm::vec<L, T, Q> call(glm::vec<L, T, Q> 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::qualifier Q>
  191. GLM_FUNC_QUALIFIER glm::vec<L, T, Q> bitfieldReverseOps(glm::vec<L, T, Q> const& v)
  192. {
  193. glm::vec<L, T, Q> x(v);
  194. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 2>::call(x, static_cast<T>(0x5555555555555555ull), static_cast<T>( 1));
  195. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 4>::call(x, static_cast<T>(0x3333333333333333ull), static_cast<T>( 2));
  196. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 8>::call(x, static_cast<T>(0x0F0F0F0F0F0F0F0Full), static_cast<T>( 4));
  197. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 16>::call(x, static_cast<T>(0x00FF00FF00FF00FFull), static_cast<T>( 8));
  198. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 32>::call(x, static_cast<T>(0x0000FFFF0000FFFFull), static_cast<T>(16));
  199. x = compute_bitfieldReverseStep<sizeof(T) * 8 >= 64>::call(x, static_cast<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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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. static 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 static_cast<int>(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. static 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. static int findMSB_nlz2(unsigned int x)
  490. {
  491. unsigned int y;
  492. int n = 32;
  493. y = x >>16; if (y != 0) {n = n -16; x = y;}
  494. y = x >> 8; if (y != 0) {n = n - 8; x = y;}
  495. y = x >> 4; if (y != 0) {n = n - 4; x = y;}
  496. y = x >> 2; if (y != 0) {n = n - 2; x = y;}
  497. y = x >> 1; if (y != 0) return n - 2;
  498. return 32 - (n - static_cast<int>(x));
  499. }
  500. static int findMSB_pop(unsigned int x)
  501. {
  502. x = x | (x >> 1);
  503. x = x | (x >> 2);
  504. x = x | (x >> 4);
  505. x = x | (x >> 8);
  506. x = x | (x >>16);
  507. return 31 - glm::bitCount(~x);
  508. }
  509. static int perf_int(std::size_t Count)
  510. {
  511. type<int, int> const Data[] =
  512. {
  513. {0x00000000, -1},
  514. {0x00000001, 0},
  515. {0x00000002, 1},
  516. {0x00000003, 1},
  517. {0x00000004, 2},
  518. {0x00000005, 2},
  519. {0x00000007, 2},
  520. {0x00000008, 3},
  521. {0x00000010, 4},
  522. {0x00000020, 5},
  523. {0x00000040, 6},
  524. {0x00000080, 7},
  525. {0x00000100, 8},
  526. {0x00000200, 9},
  527. {0x00000400, 10},
  528. {0x00000800, 11},
  529. {0x00001000, 12},
  530. {0x00002000, 13},
  531. {0x00004000, 14},
  532. {0x00008000, 15},
  533. {0x00010000, 16},
  534. {0x00020000, 17},
  535. {0x00040000, 18},
  536. {0x00080000, 19},
  537. {0x00100000, 20},
  538. {0x00200000, 21},
  539. {0x00400000, 22},
  540. {0x00800000, 23},
  541. {0x01000000, 24},
  542. {0x02000000, 25},
  543. {0x04000000, 26},
  544. {0x08000000, 27},
  545. {0x10000000, 28},
  546. {0x20000000, 29},
  547. {0x40000000, 30}
  548. };
  549. int Error(0);
  550. std::clock_t Timestamps0 = std::clock();
  551. for(std::size_t k = 0; k < Count; ++k)
  552. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
  553. {
  554. int Result = glm::findMSB(Data[i].Value);
  555. Error += Data[i].Return == Result ? 0 : 1;
  556. }
  557. std::clock_t Timestamps1 = std::clock();
  558. for(std::size_t k = 0; k < Count; ++k)
  559. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
  560. {
  561. int Result = findMSB_nlz1(Data[i].Value);
  562. Error += Data[i].Return == Result ? 0 : 1;
  563. }
  564. std::clock_t Timestamps2 = std::clock();
  565. for(std::size_t k = 0; k < Count; ++k)
  566. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
  567. {
  568. int Result = findMSB_nlz2(static_cast<unsigned int>(Data[i].Value));
  569. Error += Data[i].Return == Result ? 0 : 1;
  570. }
  571. std::clock_t Timestamps3 = std::clock();
  572. for(std::size_t k = 0; k < Count; ++k)
  573. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
  574. {
  575. int Result = findMSB_095(static_cast<unsigned int>(Data[i].Value));
  576. Error += Data[i].Return == Result ? 0 : 1;
  577. }
  578. std::clock_t Timestamps4 = std::clock();
  579. # if GLM_HAS_BITSCAN_WINDOWS
  580. for(std::size_t k = 0; k < Count; ++k)
  581. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
  582. {
  583. int Result = findMSB_intrinsic(Data[i].Value);
  584. Error += Data[i].Return == Result ? 0 : 1;
  585. }
  586. # endif//GLM_HAS_BITSCAN_WINDOWS
  587. std::clock_t Timestamps5 = std::clock();
  588. for(std::size_t k = 0; k < Count; ++k)
  589. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
  590. {
  591. int Result = findMSB_pop(static_cast<unsigned int>(Data[i].Value));
  592. Error += Data[i].Return == Result ? 0 : 1;
  593. }
  594. std::clock_t Timestamps6 = std::clock();
  595. # if GLM_ARCH & GLM_ARCH_AVX && GLM_COMPILER & GLM_COMPILER_VC
  596. for(std::size_t k = 0; k < Count; ++k)
  597. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<int, int>); ++i)
  598. {
  599. int Result = findMSB_avx(Data[i].Value);
  600. Error += Data[i].Return == Result ? 0 : 1;
  601. }
  602. std::clock_t Timestamps7 = std::clock();
  603. # endif
  604. std::printf("glm::findMSB: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
  605. std::printf("findMSB - nlz1: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  606. std::printf("findMSB - nlz2: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
  607. std::printf("findMSB - 0.9.5: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
  608. # if GLM_HAS_BITSCAN_WINDOWS
  609. std::printf("findMSB - intrinsics: %d clocks\n", static_cast<unsigned int>(Timestamps5 - Timestamps4));
  610. # endif//GLM_HAS_BITSCAN_WINDOWS
  611. std::printf("findMSB - pop: %d clocks\n", static_cast<unsigned int>(Timestamps6 - Timestamps5));
  612. # if GLM_ARCH & GLM_ARCH_AVX && GLM_COMPILER & GLM_COMPILER_VC
  613. std::printf("findMSB - avx tzcnt: %d clocks\n", static_cast<unsigned int>(Timestamps7 - Timestamps6));
  614. # endif//GLM_ARCH & GLM_ARCH_AVX && GLM_PLATFORM & GLM_PLATFORM_WINDOWS
  615. return Error;
  616. }
  617. static int test_ivec4()
  618. {
  619. type<glm::ivec4, glm::ivec4> const Data[] =
  620. {
  621. {glm::ivec4(0x00000000), glm::ivec4(-1)},
  622. {glm::ivec4(0x00000001), glm::ivec4( 0)},
  623. {glm::ivec4(0x00000002), glm::ivec4( 1)},
  624. {glm::ivec4(0x00000003), glm::ivec4( 1)},
  625. {glm::ivec4(0x00000004), glm::ivec4( 2)},
  626. {glm::ivec4(0x00000005), glm::ivec4( 2)},
  627. {glm::ivec4(0x00000007), glm::ivec4( 2)},
  628. {glm::ivec4(0x00000008), glm::ivec4( 3)},
  629. {glm::ivec4(0x00000010), glm::ivec4( 4)},
  630. {glm::ivec4(0x00000020), glm::ivec4( 5)},
  631. {glm::ivec4(0x00000040), glm::ivec4( 6)},
  632. {glm::ivec4(0x00000080), glm::ivec4( 7)},
  633. {glm::ivec4(0x00000100), glm::ivec4( 8)},
  634. {glm::ivec4(0x00000200), glm::ivec4( 9)},
  635. {glm::ivec4(0x00000400), glm::ivec4(10)},
  636. {glm::ivec4(0x00000800), glm::ivec4(11)},
  637. {glm::ivec4(0x00001000), glm::ivec4(12)},
  638. {glm::ivec4(0x00002000), glm::ivec4(13)},
  639. {glm::ivec4(0x00004000), glm::ivec4(14)},
  640. {glm::ivec4(0x00008000), glm::ivec4(15)},
  641. {glm::ivec4(0x00010000), glm::ivec4(16)},
  642. {glm::ivec4(0x00020000), glm::ivec4(17)},
  643. {glm::ivec4(0x00040000), glm::ivec4(18)},
  644. {glm::ivec4(0x00080000), glm::ivec4(19)},
  645. {glm::ivec4(0x00100000), glm::ivec4(20)},
  646. {glm::ivec4(0x00200000), glm::ivec4(21)},
  647. {glm::ivec4(0x00400000), glm::ivec4(22)},
  648. {glm::ivec4(0x00800000), glm::ivec4(23)},
  649. {glm::ivec4(0x01000000), glm::ivec4(24)},
  650. {glm::ivec4(0x02000000), glm::ivec4(25)},
  651. {glm::ivec4(0x04000000), glm::ivec4(26)},
  652. {glm::ivec4(0x08000000), glm::ivec4(27)},
  653. {glm::ivec4(0x10000000), glm::ivec4(28)},
  654. {glm::ivec4(0x20000000), glm::ivec4(29)},
  655. {glm::ivec4(0x40000000), glm::ivec4(30)}
  656. };
  657. int Error(0);
  658. for(std::size_t i = 0; i < sizeof(Data) / sizeof(type<glm::ivec4, glm::ivec4>); ++i)
  659. {
  660. glm::ivec4 Result0 = glm::findMSB(Data[i].Value);
  661. Error += glm::all(glm::equal(Data[i].Return, Result0)) ? 0 : 1;
  662. }
  663. return Error;
  664. }
  665. static int test_int()
  666. {
  667. typedef type<glm::uint, int> entry;
  668. entry const Data[] =
  669. {
  670. {0x00000000, -1},
  671. {0x00000001, 0},
  672. {0x00000002, 1},
  673. {0x00000003, 1},
  674. {0x00000004, 2},
  675. {0x00000005, 2},
  676. {0x00000007, 2},
  677. {0x00000008, 3},
  678. {0x00000010, 4},
  679. {0x00000020, 5},
  680. {0x00000040, 6},
  681. {0x00000080, 7},
  682. {0x00000100, 8},
  683. {0x00000200, 9},
  684. {0x00000400, 10},
  685. {0x00000800, 11},
  686. {0x00001000, 12},
  687. {0x00002000, 13},
  688. {0x00004000, 14},
  689. {0x00008000, 15},
  690. {0x00010000, 16},
  691. {0x00020000, 17},
  692. {0x00040000, 18},
  693. {0x00080000, 19},
  694. {0x00100000, 20},
  695. {0x00200000, 21},
  696. {0x00400000, 22},
  697. {0x00800000, 23},
  698. {0x01000000, 24},
  699. {0x02000000, 25},
  700. {0x04000000, 26},
  701. {0x08000000, 27},
  702. {0x10000000, 28},
  703. {0x20000000, 29},
  704. {0x40000000, 30}
  705. };
  706. int Error(0);
  707. for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
  708. {
  709. int Result0 = glm::findMSB(Data[i].Value);
  710. Error += Data[i].Return == Result0 ? 0 : 1;
  711. }
  712. for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
  713. {
  714. int Result0 = findMSB_nlz1(Data[i].Value);
  715. Error += Data[i].Return == Result0 ? 0 : 1;
  716. }
  717. /*
  718. for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
  719. {
  720. int Result0 = findMSB_nlz2(Data[i].Value);
  721. Error += Data[i].Return == Result0 ? 0 : 1;
  722. }
  723. */
  724. for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
  725. {
  726. int Result0 = findMSB_095(Data[i].Value);
  727. Error += Data[i].Return == Result0 ? 0 : 1;
  728. }
  729. # if GLM_HAS_BITSCAN_WINDOWS
  730. for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
  731. {
  732. int Result0 = findMSB_intrinsic(Data[i].Value);
  733. Error += Data[i].Return == Result0 ? 0 : 1;
  734. }
  735. # endif//GLM_HAS_BITSCAN_WINDOWS
  736. for(std::size_t i = 0; i < sizeof(Data) / sizeof(entry); ++i)
  737. {
  738. int Result0 = findMSB_pop(Data[i].Value);
  739. Error += Data[i].Return == Result0 ? 0 : 1;
  740. }
  741. return Error;
  742. }
  743. static int test()
  744. {
  745. int Error(0);
  746. Error += test_ivec4();
  747. Error += test_int();
  748. return Error;
  749. }
  750. static int perf(std::size_t Samples)
  751. {
  752. int Error(0);
  753. Error += perf_int(Samples);
  754. return Error;
  755. }
  756. }//findMSB
  757. namespace findLSB
  758. {
  759. template<typename genType, typename retType>
  760. struct type
  761. {
  762. genType Value;
  763. retType Return;
  764. };
  765. typedef type<int, int> entry;
  766. entry const DataI32[] =
  767. {
  768. {0x00000001, 0},
  769. {0x00000003, 0},
  770. {0x00000002, 1},
  771. // {0x80000000, 31}, // Clang generates an error with this
  772. {0x00010000, 16},
  773. {0x7FFF0000, 16},
  774. {0x7F000000, 24},
  775. {0x7F00FF00, 8},
  776. {0x00000000, -1}
  777. };
  778. # if GLM_HAS_BITSCAN_WINDOWS
  779. template<typename genIUType>
  780. static int findLSB_intrinsic(genIUType Value)
  781. {
  782. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findLSB' only accept integer values");
  783. if(Value == 0)
  784. return -1;
  785. unsigned long Result(0);
  786. _BitScanForward(&Result, Value);
  787. return int(Result);
  788. }
  789. # endif
  790. template<typename genIUType>
  791. static int findLSB_095(genIUType Value)
  792. {
  793. GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findLSB' only accept integer values");
  794. if(Value == 0)
  795. return -1;
  796. genIUType Bit;
  797. for(Bit = genIUType(0); !(Value & (1 << Bit)); ++Bit){}
  798. return Bit;
  799. }
  800. template<typename genIUType>
  801. static int findLSB_ntz2(genIUType x)
  802. {
  803. if(x == 0)
  804. return -1;
  805. return glm::bitCount(~x & (x - static_cast<genIUType>(1)));
  806. }
  807. template<typename genIUType>
  808. static int findLSB_branchfree(genIUType x)
  809. {
  810. bool IsNull(x == 0);
  811. int const Keep(!IsNull);
  812. int const Discard(IsNull);
  813. return static_cast<int>(glm::bitCount(~x & (x - static_cast<genIUType>(1)))) * Keep + Discard * -1;
  814. }
  815. static int test_int()
  816. {
  817. int Error(0);
  818. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  819. {
  820. int Result = glm::findLSB(DataI32[i].Value);
  821. Error += DataI32[i].Return == Result ? 0 : 1;
  822. }
  823. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  824. {
  825. int Result = findLSB_095(DataI32[i].Value);
  826. Error += DataI32[i].Return == Result ? 0 : 1;
  827. }
  828. # if GLM_HAS_BITSCAN_WINDOWS
  829. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  830. {
  831. int Result = findLSB_intrinsic(DataI32[i].Value);
  832. Error += DataI32[i].Return == Result ? 0 : 1;
  833. }
  834. # endif
  835. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  836. {
  837. int Result = findLSB_ntz2(DataI32[i].Value);
  838. Error += DataI32[i].Return == Result ? 0 : 1;
  839. }
  840. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  841. {
  842. int Result = findLSB_branchfree(DataI32[i].Value);
  843. Error += DataI32[i].Return == Result ? 0 : 1;
  844. }
  845. return Error;
  846. }
  847. static int test()
  848. {
  849. int Error(0);
  850. Error += test_int();
  851. return Error;
  852. }
  853. static int perf_int(std::size_t Count)
  854. {
  855. int Error(0);
  856. std::clock_t Timestamps0 = std::clock();
  857. for(std::size_t k = 0; k < Count; ++k)
  858. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  859. {
  860. int Result = glm::findLSB(DataI32[i].Value);
  861. Error += DataI32[i].Return == Result ? 0 : 1;
  862. }
  863. std::clock_t Timestamps1 = std::clock();
  864. for(std::size_t k = 0; k < Count; ++k)
  865. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  866. {
  867. int Result = findLSB_095(DataI32[i].Value);
  868. Error += DataI32[i].Return == Result ? 0 : 1;
  869. }
  870. std::clock_t Timestamps2 = std::clock();
  871. # if GLM_HAS_BITSCAN_WINDOWS
  872. for(std::size_t k = 0; k < Count; ++k)
  873. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  874. {
  875. int Result = findLSB_intrinsic(DataI32[i].Value);
  876. Error += DataI32[i].Return == Result ? 0 : 1;
  877. }
  878. # endif
  879. std::clock_t Timestamps3 = std::clock();
  880. for(std::size_t k = 0; k < Count; ++k)
  881. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  882. {
  883. int Result = findLSB_ntz2(DataI32[i].Value);
  884. Error += DataI32[i].Return == Result ? 0 : 1;
  885. }
  886. std::clock_t Timestamps4 = std::clock();
  887. for(std::size_t k = 0; k < Count; ++k)
  888. for(std::size_t i = 0; i < sizeof(DataI32) / sizeof(entry); ++i)
  889. {
  890. int Result = findLSB_branchfree(DataI32[i].Value);
  891. Error += DataI32[i].Return == Result ? 0 : 1;
  892. }
  893. std::clock_t Timestamps5 = std::clock();
  894. std::printf("glm::findLSB: %d clocks\n", static_cast<unsigned int>(Timestamps1 - Timestamps0));
  895. std::printf("findLSB - 0.9.5: %d clocks\n", static_cast<unsigned int>(Timestamps2 - Timestamps1));
  896. # if GLM_HAS_BITSCAN_WINDOWS
  897. std::printf("findLSB - intrinsics: %d clocks\n", static_cast<unsigned int>(Timestamps3 - Timestamps2));
  898. # endif
  899. std::printf("findLSB - ntz2: %d clocks\n", static_cast<unsigned int>(Timestamps4 - Timestamps3));
  900. std::printf("findLSB - branchfree: %d clocks\n", static_cast<unsigned int>(Timestamps5 - Timestamps4));
  901. return Error;
  902. }
  903. static int perf(std::size_t Samples)
  904. {
  905. int Error(0);
  906. Error += perf_int(Samples);
  907. return Error;
  908. }
  909. }//findLSB
  910. namespace uaddCarry
  911. {
  912. static int test()
  913. {
  914. int Error(0);
  915. {
  916. glm::uint x = std::numeric_limits<glm::uint>::max();
  917. glm::uint y = 0;
  918. glm::uint Carry = 0;
  919. glm::uint Result = glm::uaddCarry(x, y, Carry);
  920. Error += Carry == 0 ? 0 : 1;
  921. Error += Result == std::numeric_limits<glm::uint>::max() ? 0 : 1;
  922. }
  923. {
  924. glm::uint x = std::numeric_limits<glm::uint>::max();
  925. glm::uint y = 1;
  926. glm::uint Carry = 0;
  927. glm::uint Result = glm::uaddCarry(x, y, Carry);
  928. Error += Carry == 1 ? 0 : 1;
  929. Error += Result == 0 ? 0 : 1;
  930. }
  931. {
  932. glm::uvec1 x(std::numeric_limits<glm::uint>::max());
  933. glm::uvec1 y(0);
  934. glm::uvec1 Carry(0);
  935. glm::uvec1 Result(glm::uaddCarry(x, y, Carry));
  936. Error += glm::all(glm::equal(Carry, glm::uvec1(0))) ? 0 : 1;
  937. Error += glm::all(glm::equal(Result, glm::uvec1(std::numeric_limits<glm::uint>::max()))) ? 0 : 1;
  938. }
  939. {
  940. glm::uvec1 x(std::numeric_limits<glm::uint>::max());
  941. glm::uvec1 y(1);
  942. glm::uvec1 Carry(0);
  943. glm::uvec1 Result(glm::uaddCarry(x, y, Carry));
  944. Error += glm::all(glm::equal(Carry, glm::uvec1(1))) ? 0 : 1;
  945. Error += glm::all(glm::equal(Result, glm::uvec1(0))) ? 0 : 1;
  946. }
  947. return Error;
  948. }
  949. }//namespace uaddCarry
  950. namespace usubBorrow
  951. {
  952. static int test()
  953. {
  954. int Error(0);
  955. {
  956. glm::uint x = 16;
  957. glm::uint y = 17;
  958. glm::uint Borrow = 0;
  959. glm::uint Result = glm::usubBorrow(x, y, Borrow);
  960. Error += Borrow == 1 ? 0 : 1;
  961. Error += Result == 1 ? 0 : 1;
  962. }
  963. {
  964. glm::uvec1 x(16);
  965. glm::uvec1 y(17);
  966. glm::uvec1 Borrow(0);
  967. glm::uvec1 Result(glm::usubBorrow(x, y, Borrow));
  968. Error += glm::all(glm::equal(Borrow, glm::uvec1(1))) ? 0 : 1;
  969. Error += glm::all(glm::equal(Result, glm::uvec1(1))) ? 0 : 1;
  970. }
  971. {
  972. glm::uvec2 x(16);
  973. glm::uvec2 y(17);
  974. glm::uvec2 Borrow(0);
  975. glm::uvec2 Result(glm::usubBorrow(x, y, Borrow));
  976. Error += glm::all(glm::equal(Borrow, glm::uvec2(1))) ? 0 : 1;
  977. Error += glm::all(glm::equal(Result, glm::uvec2(1))) ? 0 : 1;
  978. }
  979. {
  980. glm::uvec3 x(16);
  981. glm::uvec3 y(17);
  982. glm::uvec3 Borrow(0);
  983. glm::uvec3 Result(glm::usubBorrow(x, y, Borrow));
  984. Error += glm::all(glm::equal(Borrow, glm::uvec3(1))) ? 0 : 1;
  985. Error += glm::all(glm::equal(Result, glm::uvec3(1))) ? 0 : 1;
  986. }
  987. {
  988. glm::uvec4 x(16);
  989. glm::uvec4 y(17);
  990. glm::uvec4 Borrow(0);
  991. glm::uvec4 Result(glm::usubBorrow(x, y, Borrow));
  992. Error += glm::all(glm::equal(Borrow, glm::uvec4(1))) ? 0 : 1;
  993. Error += glm::all(glm::equal(Result, glm::uvec4(1))) ? 0 : 1;
  994. }
  995. return Error;
  996. }
  997. }//namespace usubBorrow
  998. namespace umulExtended
  999. {
  1000. static int test()
  1001. {
  1002. int Error(0);
  1003. {
  1004. glm::uint x = 2;
  1005. glm::uint y = 3;
  1006. glm::uint msb = 0;
  1007. glm::uint lsb = 0;
  1008. glm::umulExtended(x, y, msb, lsb);
  1009. Error += msb == 0 ? 0 : 1;
  1010. Error += lsb == 6 ? 0 : 1;
  1011. }
  1012. {
  1013. glm::uvec1 x(2);
  1014. glm::uvec1 y(3);
  1015. glm::uvec1 msb(0);
  1016. glm::uvec1 lsb(0);
  1017. glm::umulExtended(x, y, msb, lsb);
  1018. Error += glm::all(glm::equal(msb, glm::uvec1(0))) ? 0 : 1;
  1019. Error += glm::all(glm::equal(lsb, glm::uvec1(6))) ? 0 : 1;
  1020. }
  1021. {
  1022. glm::uvec2 x(2);
  1023. glm::uvec2 y(3);
  1024. glm::uvec2 msb(0);
  1025. glm::uvec2 lsb(0);
  1026. glm::umulExtended(x, y, msb, lsb);
  1027. Error += glm::all(glm::equal(msb, glm::uvec2(0))) ? 0 : 1;
  1028. Error += glm::all(glm::equal(lsb, glm::uvec2(6))) ? 0 : 1;
  1029. }
  1030. {
  1031. glm::uvec3 x(2);
  1032. glm::uvec3 y(3);
  1033. glm::uvec3 msb(0);
  1034. glm::uvec3 lsb(0);
  1035. glm::umulExtended(x, y, msb, lsb);
  1036. Error += glm::all(glm::equal(msb, glm::uvec3(0))) ? 0 : 1;
  1037. Error += glm::all(glm::equal(lsb, glm::uvec3(6))) ? 0 : 1;
  1038. }
  1039. {
  1040. glm::uvec4 x(2);
  1041. glm::uvec4 y(3);
  1042. glm::uvec4 msb(0);
  1043. glm::uvec4 lsb(0);
  1044. glm::umulExtended(x, y, msb, lsb);
  1045. Error += glm::all(glm::equal(msb, glm::uvec4(0))) ? 0 : 1;
  1046. Error += glm::all(glm::equal(lsb, glm::uvec4(6))) ? 0 : 1;
  1047. }
  1048. return Error;
  1049. }
  1050. }//namespace umulExtended
  1051. namespace imulExtended
  1052. {
  1053. static int test()
  1054. {
  1055. int Error(0);
  1056. {
  1057. int x = 2;
  1058. int y = 3;
  1059. int msb = 0;
  1060. int lsb = 0;
  1061. glm::imulExtended(x, y, msb, lsb);
  1062. Error += msb == 0 ? 0 : 1;
  1063. Error += lsb == 6 ? 0 : 1;
  1064. }
  1065. {
  1066. glm::ivec1 x(2);
  1067. glm::ivec1 y(3);
  1068. glm::ivec1 msb(0);
  1069. glm::ivec1 lsb(0);
  1070. glm::imulExtended(x, y, msb, lsb);
  1071. Error += glm::all(glm::equal(msb, glm::ivec1(0))) ? 0 : 1;
  1072. Error += glm::all(glm::equal(lsb, glm::ivec1(6))) ? 0 : 1;
  1073. }
  1074. {
  1075. glm::ivec2 x(2);
  1076. glm::ivec2 y(3);
  1077. glm::ivec2 msb(0);
  1078. glm::ivec2 lsb(0);
  1079. glm::imulExtended(x, y, msb, lsb);
  1080. Error += glm::all(glm::equal(msb, glm::ivec2(0))) ? 0 : 1;
  1081. Error += glm::all(glm::equal(lsb, glm::ivec2(6))) ? 0 : 1;
  1082. }
  1083. {
  1084. glm::ivec3 x(2);
  1085. glm::ivec3 y(3);
  1086. glm::ivec3 msb(0);
  1087. glm::ivec3 lsb(0);
  1088. glm::imulExtended(x, y, msb, lsb);
  1089. Error += glm::all(glm::equal(msb, glm::ivec3(0))) ? 0 : 1;
  1090. Error += glm::all(glm::equal(lsb, glm::ivec3(6))) ? 0 : 1;
  1091. }
  1092. {
  1093. glm::ivec4 x(2);
  1094. glm::ivec4 y(3);
  1095. glm::ivec4 msb(0);
  1096. glm::ivec4 lsb(0);
  1097. glm::imulExtended(x, y, msb, lsb);
  1098. Error += glm::all(glm::equal(msb, glm::ivec4(0))) ? 0 : 1;
  1099. Error += glm::all(glm::equal(lsb, glm::ivec4(6))) ? 0 : 1;
  1100. }
  1101. return Error;
  1102. }
  1103. }//namespace imulExtended
  1104. namespace bitCount
  1105. {
  1106. template<typename genType>
  1107. struct type
  1108. {
  1109. genType Value;
  1110. genType Return;
  1111. };
  1112. type<int> const DataI32[] =
  1113. {
  1114. {0x00000001, 1},
  1115. {0x00000003, 2},
  1116. {0x00000002, 1},
  1117. {0x7fffffff, 31},
  1118. {0x00000000, 0}
  1119. };
  1120. template<typename T>
  1121. inline int bitCount_if(T v)
  1122. {
  1123. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitCount' only accept integer values");
  1124. int Count(0);
  1125. for(T i = 0, n = static_cast<T>(sizeof(T) * 8); i < n; ++i)
  1126. {
  1127. if(v & static_cast<T>(1 << i))
  1128. ++Count;
  1129. }
  1130. return Count;
  1131. }
  1132. template<typename T>
  1133. inline int bitCount_vec(T v)
  1134. {
  1135. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitCount' only accept integer values");
  1136. int Count(0);
  1137. for(T i = 0, n = static_cast<T>(sizeof(T) * 8); i < n; ++i)
  1138. {
  1139. Count += static_cast<int>((v >> i) & static_cast<T>(1));
  1140. }
  1141. return Count;
  1142. }
  1143. template<bool EXEC = false>
  1144. struct compute_bitfieldBitCountStep
  1145. {
  1146. template<glm::length_t L, typename T, glm::qualifier Q>
  1147. GLM_FUNC_QUALIFIER static glm::vec<L, T, Q> call(glm::vec<L, T, Q> const& v, T, T)
  1148. {
  1149. return v;
  1150. }
  1151. };
  1152. template<>
  1153. struct compute_bitfieldBitCountStep<true>
  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 Mask, T Shift)
  1157. {
  1158. return (v & Mask) + ((v >> Shift) & Mask);
  1159. }
  1160. };
  1161. template<glm::length_t L, typename T, glm::qualifier Q>
  1162. static glm::vec<L, int, Q> bitCount_bitfield(glm::vec<L, T, Q> const& v)
  1163. {
  1164. 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));
  1165. 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));
  1166. 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));
  1167. 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));
  1168. 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));
  1169. 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));
  1170. 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));
  1171. return glm::vec<L, int, Q>(x);
  1172. }
  1173. template<typename genType>
  1174. static int bitCount_bitfield(genType x)
  1175. {
  1176. return bitCount_bitfield(glm::vec<1, genType, glm::defaultp>(x)).x;
  1177. }
  1178. static int perf(std::size_t Size)
  1179. {
  1180. int Error(0);
  1181. std::vector<int> v;
  1182. v.resize(Size);
  1183. std::vector<glm::ivec4> w;
  1184. w.resize(Size);
  1185. std::clock_t TimestampsA = std::clock();
  1186. // bitCount - TimeIf
  1187. {
  1188. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1189. v[i] = bitCount_if(static_cast<int>(i));
  1190. }
  1191. std::clock_t TimestampsB = std::clock();
  1192. // bitCount - TimeVec
  1193. {
  1194. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1195. v[i] = bitCount_vec(i);
  1196. }
  1197. std::clock_t TimestampsC = std::clock();
  1198. // bitCount - TimeDefault
  1199. {
  1200. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1201. v[i] = glm::bitCount(i);
  1202. }
  1203. std::clock_t TimestampsD = std::clock();
  1204. // bitCount - TimeVec4
  1205. {
  1206. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1207. w[i] = glm::bitCount(glm::ivec4(static_cast<int>(i)));
  1208. }
  1209. std::clock_t TimestampsE = std::clock();
  1210. {
  1211. for(std::size_t i = 0, n = v.size(); i < n; ++i)
  1212. v[i] = bitCount_bitfield(static_cast<int>(i));
  1213. }
  1214. std::clock_t TimestampsF = std::clock();
  1215. std::printf("bitCount - TimeIf %d\n", static_cast<unsigned int>(TimestampsB - TimestampsA));
  1216. std::printf("bitCount - TimeVec %d\n", static_cast<unsigned int>(TimestampsC - TimestampsB));
  1217. std::printf("bitCount - TimeDefault %d\n", static_cast<unsigned int>(TimestampsD - TimestampsC));
  1218. std::printf("bitCount - TimeVec4 %d\n", static_cast<unsigned int>(TimestampsE - TimestampsD));
  1219. std::printf("bitCount - bitfield %d\n", static_cast<unsigned int>(TimestampsF - TimestampsE));
  1220. return Error;
  1221. }
  1222. static int test()
  1223. {
  1224. int Error(0);
  1225. for(std::size_t i = 0, n = sizeof(DataI32) / sizeof(type<int>); i < n; ++i)
  1226. {
  1227. int ResultA = glm::bitCount(DataI32[i].Value);
  1228. int ResultB = bitCount_if(DataI32[i].Value);
  1229. int ResultC = bitCount_vec(DataI32[i].Value);
  1230. int ResultE = bitCount_bitfield(DataI32[i].Value);
  1231. Error += DataI32[i].Return == ResultA ? 0 : 1;
  1232. Error += DataI32[i].Return == ResultB ? 0 : 1;
  1233. Error += DataI32[i].Return == ResultC ? 0 : 1;
  1234. Error += DataI32[i].Return == ResultE ? 0 : 1;
  1235. assert(!Error);
  1236. }
  1237. return Error;
  1238. }
  1239. }//bitCount
  1240. int main()
  1241. {
  1242. int Error = 0;
  1243. Error += ::bitCount::test();
  1244. Error += ::bitfieldReverse::test();
  1245. Error += ::findMSB::test();
  1246. Error += ::findLSB::test();
  1247. Error += ::umulExtended::test();
  1248. Error += ::imulExtended::test();
  1249. Error += ::uaddCarry::test();
  1250. Error += ::usubBorrow::test();
  1251. Error += ::bitfieldInsert::test();
  1252. Error += ::bitfieldExtract::test();
  1253. # ifdef NDEBUG
  1254. std::size_t const Samples = 1000;
  1255. # else
  1256. std::size_t const Samples = 1;
  1257. # endif
  1258. ::bitCount::perf(Samples);
  1259. ::bitfieldReverse::perf(Samples);
  1260. ::findMSB::perf(Samples);
  1261. ::findLSB::perf(Samples);
  1262. return Error;
  1263. }