gtc_round.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <vector>
  34. #include <ctime>
  35. #include <cstdio>
  36. namespace isPowerOfTwo
  37. {
  38. template <typename genType>
  39. struct type
  40. {
  41. genType Value;
  42. bool Return;
  43. };
  44. int test_int16()
  45. {
  46. type<glm::int16> const Data[] =
  47. {
  48. {0x0001, true},
  49. {0x0002, true},
  50. {0x0004, true},
  51. {0x0080, true},
  52. {0x0000, true},
  53. {0x0003, false}
  54. };
  55. int Error(0);
  56. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::int16>); i < n; ++i)
  57. {
  58. bool Result = glm::isPowerOfTwo(Data[i].Value);
  59. Error += Data[i].Return == Result ? 0 : 1;
  60. }
  61. return Error;
  62. }
  63. int test_uint16()
  64. {
  65. type<glm::uint16> const Data[] =
  66. {
  67. {0x0001, true},
  68. {0x0002, true},
  69. {0x0004, true},
  70. {0x0000, true},
  71. {0x0000, true},
  72. {0x0003, false}
  73. };
  74. int Error(0);
  75. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint16>); i < n; ++i)
  76. {
  77. bool Result = glm::isPowerOfTwo(Data[i].Value);
  78. Error += Data[i].Return == Result ? 0 : 1;
  79. }
  80. return Error;
  81. }
  82. int test_int32()
  83. {
  84. type<int> const Data[] =
  85. {
  86. {0x00000001, true},
  87. {0x00000002, true},
  88. {0x00000004, true},
  89. {0x0000000f, false},
  90. {0x00000000, true},
  91. {0x00000003, false}
  92. };
  93. int Error(0);
  94. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  95. {
  96. bool Result = glm::isPowerOfTwo(Data[i].Value);
  97. Error += Data[i].Return == Result ? 0 : 1;
  98. }
  99. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  100. {
  101. glm::bvec1 Result = glm::isPowerOfTwo(glm::ivec1(Data[i].Value));
  102. Error += glm::all(glm::equal(glm::bvec1(Data[i].Return), Result)) ? 0 : 1;
  103. }
  104. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  105. {
  106. glm::bvec2 Result = glm::isPowerOfTwo(glm::ivec2(Data[i].Value));
  107. Error += glm::all(glm::equal(glm::bvec2(Data[i].Return), Result)) ? 0 : 1;
  108. }
  109. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  110. {
  111. glm::bvec3 Result = glm::isPowerOfTwo(glm::ivec3(Data[i].Value));
  112. Error += glm::all(glm::equal(glm::bvec3(Data[i].Return), Result)) ? 0 : 1;
  113. }
  114. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<int>); i < n; ++i)
  115. {
  116. glm::bvec4 Result = glm::isPowerOfTwo(glm::ivec4(Data[i].Value));
  117. Error += glm::all(glm::equal(glm::bvec4(Data[i].Return), Result)) ? 0 : 1;
  118. }
  119. return Error;
  120. }
  121. int test_uint32()
  122. {
  123. type<glm::uint> const Data[] =
  124. {
  125. {0x00000001, true},
  126. {0x00000002, true},
  127. {0x00000004, true},
  128. {0x80000000, true},
  129. {0x00000000, true},
  130. {0x00000003, false}
  131. };
  132. int Error(0);
  133. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint>); i < n; ++i)
  134. {
  135. bool Result = glm::isPowerOfTwo(Data[i].Value);
  136. Error += Data[i].Return == Result ? 0 : 1;
  137. }
  138. return Error;
  139. }
  140. int test()
  141. {
  142. int Error(0);
  143. Error += test_int16();
  144. Error += test_uint16();
  145. Error += test_int32();
  146. Error += test_uint32();
  147. return Error;
  148. }
  149. }//isPowerOfTwo
  150. namespace ceilPowerOfTwo
  151. {
  152. template <typename genIUType>
  153. GLM_FUNC_QUALIFIER genIUType highestBitValue(genIUType Value)
  154. {
  155. genIUType tmp = Value;
  156. genIUType result = genIUType(0);
  157. while(tmp)
  158. {
  159. result = (tmp & (~tmp + 1)); // grab lowest bit
  160. tmp &= ~result; // clear lowest bit
  161. }
  162. return result;
  163. }
  164. template <typename genType>
  165. GLM_FUNC_QUALIFIER genType ceilPowerOfTwo_loop(genType value)
  166. {
  167. return glm::isPowerOfTwo(value) ? value : highestBitValue(value) << 1;
  168. }
  169. template <typename genType>
  170. struct type
  171. {
  172. genType Value;
  173. genType Return;
  174. };
  175. int test_int32()
  176. {
  177. type<glm::int32> const Data[] =
  178. {
  179. {0x0000ffff, 0x00010000},
  180. {-3, -4},
  181. {-8, -8},
  182. {0x00000001, 0x00000001},
  183. {0x00000002, 0x00000002},
  184. {0x00000004, 0x00000004},
  185. {0x00000007, 0x00000008},
  186. {0x0000fff0, 0x00010000},
  187. {0x0000f000, 0x00010000},
  188. {0x08000000, 0x08000000},
  189. {0x00000000, 0x00000000},
  190. {0x00000003, 0x00000004}
  191. };
  192. int Error(0);
  193. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::int32>); i < n; ++i)
  194. {
  195. glm::int32 Result = glm::ceilPowerOfTwo(Data[i].Value);
  196. Error += Data[i].Return == Result ? 0 : 1;
  197. }
  198. return Error;
  199. }
  200. int test_uint32()
  201. {
  202. type<glm::uint32> const Data[] =
  203. {
  204. {0x00000001, 0x00000001},
  205. {0x00000002, 0x00000002},
  206. {0x00000004, 0x00000004},
  207. {0x00000007, 0x00000008},
  208. {0x0000ffff, 0x00010000},
  209. {0x0000fff0, 0x00010000},
  210. {0x0000f000, 0x00010000},
  211. {0x80000000, 0x80000000},
  212. {0x00000000, 0x00000000},
  213. {0x00000003, 0x00000004}
  214. };
  215. int Error(0);
  216. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint32>); i < n; ++i)
  217. {
  218. glm::uint32 Result = glm::ceilPowerOfTwo(Data[i].Value);
  219. Error += Data[i].Return == Result ? 0 : 1;
  220. }
  221. return Error;
  222. }
  223. int perf()
  224. {
  225. int Error(0);
  226. std::vector<glm::uint> v;
  227. v.resize(100000000);
  228. std::clock_t Timestramp0 = std::clock();
  229. for(glm::uint32 i = 0, n = static_cast<glm::uint>(v.size()); i < n; ++i)
  230. v[i] = ceilPowerOfTwo_loop(i);
  231. std::clock_t Timestramp1 = std::clock();
  232. for(glm::uint32 i = 0, n = static_cast<glm::uint>(v.size()); i < n; ++i)
  233. v[i] = glm::ceilPowerOfTwo(i);
  234. std::clock_t Timestramp2 = std::clock();
  235. std::printf("ceilPowerOfTwo_loop: %d clocks\n", static_cast<unsigned int>(Timestramp1 - Timestramp0));
  236. std::printf("glm::ceilPowerOfTwo: %d clocks\n", static_cast<unsigned int>(Timestramp2 - Timestramp1));
  237. return Error;
  238. }
  239. int test()
  240. {
  241. int Error(0);
  242. Error += test_int32();
  243. Error += test_uint32();
  244. return Error;
  245. }
  246. }//namespace ceilPowerOfTwo
  247. int main()
  248. {
  249. int Error(0);
  250. Error += isPowerOfTwo::test();
  251. Error += ceilPowerOfTwo::test();
  252. # ifdef NDEBUG
  253. Error += ceilPowerOfTwo::perf();
  254. # endif//NDEBUG
  255. return Error;
  256. }