gtc_round.cpp 10.0 KB

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