gtc_round.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #include <glm/gtc/round.hpp>
  2. #include <glm/gtc/type_precision.hpp>
  3. #include <glm/gtc/vec1.hpp>
  4. #include <glm/gtc/epsilon.hpp>
  5. #include <vector>
  6. #include <ctime>
  7. #include <cstdio>
  8. namespace isPowerOfTwo
  9. {
  10. #if GLM_COMPILER & GLM_COMPILER_CLANG
  11. # pragma clang diagnostic push
  12. # pragma clang diagnostic ignored "-Wpadded"
  13. #endif
  14. template<typename genType>
  15. struct type
  16. {
  17. genType Value;
  18. bool Return;
  19. };
  20. #if GLM_COMPILER & GLM_COMPILER_CLANG
  21. # pragma clang diagnostic pop
  22. #endif
  23. static int test_int16()
  24. {
  25. type<glm::int16> const Data[] =
  26. {
  27. {0x0001, true},
  28. {0x0002, true},
  29. {0x0004, true},
  30. {0x0080, true},
  31. {0x0000, true},
  32. {0x0003, false}
  33. };
  34. int Error(0);
  35. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::int16>); i < n; ++i)
  36. {
  37. bool Result = glm::isPowerOfTwo(Data[i].Value);
  38. Error += Data[i].Return == Result ? 0 : 1;
  39. }
  40. return Error;
  41. }
  42. static int test_uint16()
  43. {
  44. type<glm::uint16> const Data[] =
  45. {
  46. {0x0001, true},
  47. {0x0002, true},
  48. {0x0004, true},
  49. {0x0000, true},
  50. {0x0000, true},
  51. {0x0003, false}
  52. };
  53. int Error(0);
  54. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint16>); i < n; ++i)
  55. {
  56. bool Result = glm::isPowerOfTwo(Data[i].Value);
  57. Error += Data[i].Return == Result ? 0 : 1;
  58. }
  59. return Error;
  60. }
  61. static int test_int32()
  62. {
  63. type<int> const Data[] =
  64. {
  65. {0x00000001, true},
  66. {0x00000002, true},
  67. {0x00000004, true},
  68. {0x0000000f, false},
  69. {0x00000000, true},
  70. {0x00000003, false}
  71. };
  72. int Error(0);
  73. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  74. {
  75. bool Result = glm::isPowerOfTwo(Data[i].Value);
  76. Error += Data[i].Return == Result ? 0 : 1;
  77. }
  78. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  79. {
  80. glm::bvec1 Result = glm::isPowerOfTwo(glm::ivec1(Data[i].Value));
  81. Error += glm::all(glm::equal(glm::bvec1(Data[i].Return), Result)) ? 0 : 1;
  82. }
  83. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  84. {
  85. glm::bvec2 Result = glm::isPowerOfTwo(glm::ivec2(Data[i].Value));
  86. Error += glm::all(glm::equal(glm::bvec2(Data[i].Return), Result)) ? 0 : 1;
  87. }
  88. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  89. {
  90. glm::bvec3 Result = glm::isPowerOfTwo(glm::ivec3(Data[i].Value));
  91. Error += glm::all(glm::equal(glm::bvec3(Data[i].Return), Result)) ? 0 : 1;
  92. }
  93. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  94. {
  95. glm::bvec4 Result = glm::isPowerOfTwo(glm::ivec4(Data[i].Value));
  96. Error += glm::all(glm::equal(glm::bvec4(Data[i].Return), Result)) ? 0 : 1;
  97. }
  98. return Error;
  99. }
  100. static int test_uint32()
  101. {
  102. type<glm::uint> const Data[] =
  103. {
  104. {0x00000001, true},
  105. {0x00000002, true},
  106. {0x00000004, true},
  107. {0x80000000, true},
  108. {0x00000000, true},
  109. {0x00000003, false}
  110. };
  111. int Error(0);
  112. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint>); i < n; ++i)
  113. {
  114. bool Result = glm::isPowerOfTwo(Data[i].Value);
  115. Error += Data[i].Return == Result ? 0 : 1;
  116. }
  117. return Error;
  118. }
  119. static int test()
  120. {
  121. int Error(0);
  122. Error += test_int16();
  123. Error += test_uint16();
  124. Error += test_int32();
  125. Error += test_uint32();
  126. return Error;
  127. }
  128. }//isPowerOfTwo
  129. namespace ceilPowerOfTwo_advanced
  130. {
  131. template<typename genIUType>
  132. GLM_FUNC_QUALIFIER
  133. static genIUType highestBitValue(genIUType Value)
  134. {
  135. genIUType tmp = Value;
  136. genIUType result = genIUType(0);
  137. while(tmp)
  138. {
  139. result = (tmp & (~tmp + 1)); // grab lowest bit
  140. tmp &= ~result; // clear lowest bit
  141. }
  142. return result;
  143. }
  144. template<typename genType>
  145. GLM_FUNC_QUALIFIER
  146. static genType ceilPowerOfTwo_loop(genType value)
  147. {
  148. return glm::isPowerOfTwo(value) ? value : highestBitValue(value) << 1;
  149. }
  150. template<typename genType>
  151. struct type
  152. {
  153. genType Value;
  154. genType Return;
  155. };
  156. static int test_int32()
  157. {
  158. type<glm::int32> const Data[] =
  159. {
  160. {0x0000ffff, 0x00010000},
  161. {-3, -4},
  162. {-8, -8},
  163. {0x00000001, 0x00000001},
  164. {0x00000002, 0x00000002},
  165. {0x00000004, 0x00000004},
  166. {0x00000007, 0x00000008},
  167. {0x0000fff0, 0x00010000},
  168. {0x0000f000, 0x00010000},
  169. {0x08000000, 0x08000000},
  170. {0x00000000, 0x00000000},
  171. {0x00000003, 0x00000004}
  172. };
  173. int Error(0);
  174. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::int32>); i < n; ++i)
  175. {
  176. glm::int32 Result = glm::ceilPowerOfTwo(Data[i].Value);
  177. Error += Data[i].Return == Result ? 0 : 1;
  178. }
  179. return Error;
  180. }
  181. static int test_uint32()
  182. {
  183. type<glm::uint32> const Data[] =
  184. {
  185. {0x00000001, 0x00000001},
  186. {0x00000002, 0x00000002},
  187. {0x00000004, 0x00000004},
  188. {0x00000007, 0x00000008},
  189. {0x0000ffff, 0x00010000},
  190. {0x0000fff0, 0x00010000},
  191. {0x0000f000, 0x00010000},
  192. {0x80000000, 0x80000000},
  193. {0x00000000, 0x00000000},
  194. {0x00000003, 0x00000004}
  195. };
  196. int Error(0);
  197. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint32>); i < n; ++i)
  198. {
  199. glm::uint32 Result = glm::ceilPowerOfTwo(Data[i].Value);
  200. Error += Data[i].Return == Result ? 0 : 1;
  201. }
  202. return Error;
  203. }
  204. static int perf()
  205. {
  206. int Error(0);
  207. std::vector<glm::uint> v;
  208. v.resize(10000);
  209. std::clock_t Timestramp0 = std::clock();
  210. for(glm::uint32 i = 0, n = static_cast<glm::uint>(v.size()); i < n; ++i)
  211. v[i] = ceilPowerOfTwo_loop(i);
  212. std::clock_t Timestramp1 = std::clock();
  213. for(glm::uint32 i = 0, n = static_cast<glm::uint>(v.size()); i < n; ++i)
  214. v[i] = glm::ceilPowerOfTwo(i);
  215. std::clock_t Timestramp2 = std::clock();
  216. std::printf("ceilPowerOfTwo_loop: %d clocks\n", static_cast<int>(Timestramp1 - Timestramp0));
  217. std::printf("glm::ceilPowerOfTwo: %d clocks\n", static_cast<int>(Timestramp2 - Timestramp1));
  218. return Error;
  219. }
  220. static int test()
  221. {
  222. int Error(0);
  223. Error += test_int32();
  224. Error += test_uint32();
  225. return Error;
  226. }
  227. }//namespace ceilPowerOfTwo_advanced
  228. namespace roundPowerOfTwo
  229. {
  230. static int test()
  231. {
  232. int Error = 0;
  233. glm::uint32 const A = glm::roundPowerOfTwo(7u);
  234. Error += A == 8u ? 0 : 1;
  235. glm::uint32 const B = glm::roundPowerOfTwo(15u);
  236. Error += B == 16u ? 0 : 1;
  237. glm::uint32 const C = glm::roundPowerOfTwo(31u);
  238. Error += C == 32u ? 0 : 1;
  239. glm::uint32 const D = glm::roundPowerOfTwo(9u);
  240. Error += D == 8u ? 0 : 1;
  241. glm::uint32 const E = glm::roundPowerOfTwo(17u);
  242. Error += E == 16u ? 0 : 1;
  243. glm::uint32 const F = glm::roundPowerOfTwo(33u);
  244. Error += F == 32u ? 0 : 1;
  245. return Error;
  246. }
  247. }//namespace roundPowerOfTwo
  248. namespace floorPowerOfTwo
  249. {
  250. static int test()
  251. {
  252. int Error = 0;
  253. glm::uint32 const A = glm::floorPowerOfTwo(7u);
  254. Error += A == 4u ? 0 : 1;
  255. glm::uint32 const B = glm::floorPowerOfTwo(15u);
  256. Error += B == 8u ? 0 : 1;
  257. glm::uint32 const C = glm::floorPowerOfTwo(31u);
  258. Error += C == 16u ? 0 : 1;
  259. return Error;
  260. }
  261. }//namespace floorPowerOfTwo
  262. namespace ceilPowerOfTwo
  263. {
  264. static int test()
  265. {
  266. int Error = 0;
  267. glm::uint32 const A = glm::ceilPowerOfTwo(7u);
  268. Error += A == 8u ? 0 : 1;
  269. glm::uint32 const B = glm::ceilPowerOfTwo(15u);
  270. Error += B == 16u ? 0 : 1;
  271. glm::uint32 const C = glm::ceilPowerOfTwo(31u);
  272. Error += C == 32u ? 0 : 1;
  273. return Error;
  274. }
  275. }//namespace ceilPowerOfTwo
  276. namespace floorMultiple
  277. {
  278. template<typename genType>
  279. struct type
  280. {
  281. genType Source;
  282. genType Multiple;
  283. genType Return;
  284. genType Epsilon;
  285. };
  286. static int test_float()
  287. {
  288. type<glm::float64> const Data[] =
  289. {
  290. {3.4, 0.3, 3.3, 0.0001},
  291. {-1.4, 0.3, -1.5, 0.0001},
  292. };
  293. int Error(0);
  294. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::float64>); i < n; ++i)
  295. {
  296. glm::float64 Result = glm::floorMultiple(Data[i].Source, Data[i].Multiple);
  297. Error += glm::epsilonEqual(Data[i].Return, Result, Data[i].Epsilon) ? 0 : 1;
  298. }
  299. return Error;
  300. }
  301. static int test()
  302. {
  303. int Error(0);
  304. Error += test_float();
  305. return Error;
  306. }
  307. }//namespace floorMultiple
  308. namespace ceilMultiple
  309. {
  310. template<typename genType>
  311. struct type
  312. {
  313. genType Source;
  314. genType Multiple;
  315. genType Return;
  316. genType Epsilon;
  317. };
  318. static int test_float()
  319. {
  320. type<glm::float64> const Data[] =
  321. {
  322. {3.4, 0.3, 3.6, 0.0001},
  323. {-1.4, 0.3, -1.2, 0.0001},
  324. };
  325. int Error(0);
  326. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::float64>); i < n; ++i)
  327. {
  328. glm::float64 Result = glm::ceilMultiple(Data[i].Source, Data[i].Multiple);
  329. Error += glm::epsilonEqual(Data[i].Return, Result, Data[i].Epsilon) ? 0 : 1;
  330. }
  331. return Error;
  332. }
  333. static int test_int()
  334. {
  335. type<int> const Data[] =
  336. {
  337. {3, 4, 4, 0},
  338. {7, 4, 8, 0},
  339. {5, 4, 8, 0},
  340. {1, 4, 4, 0},
  341. {1, 3, 3, 0},
  342. {4, 3, 6, 0},
  343. {4, 1, 4, 0},
  344. {1, 1, 1, 0},
  345. {7, 1, 7, 0},
  346. };
  347. int Error(0);
  348. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  349. {
  350. int Result = glm::ceilMultiple(Data[i].Source, Data[i].Multiple);
  351. Error += Data[i].Return == Result ? 0 : 1;
  352. }
  353. return Error;
  354. }
  355. static int test()
  356. {
  357. int Error(0);
  358. Error += test_int();
  359. Error += test_float();
  360. return Error;
  361. }
  362. }//namespace ceilMultiple
  363. int main()
  364. {
  365. int Error(0);
  366. Error += isPowerOfTwo::test();
  367. Error += floorPowerOfTwo::test();
  368. Error += roundPowerOfTwo::test();
  369. Error += ceilPowerOfTwo::test();
  370. Error += ceilPowerOfTwo_advanced::test();
  371. Error += ceilPowerOfTwo_advanced::perf();
  372. Error += floorMultiple::test();
  373. Error += ceilMultiple::test();
  374. return Error;
  375. }