core_func_integer.cpp 41 KB

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