gtc_integer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2014-10-25
  5. // Updated : 2014-10-25
  6. // Licence : This source is under MIT licence
  7. // File : test/gtc/integer.cpp
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. #include <glm/gtc/integer.hpp>
  10. #include <glm/gtc/type_precision.hpp>
  11. #include <glm/gtc/vec1.hpp>
  12. #include <vector>
  13. #include <ctime>
  14. #include <cstdio>
  15. namespace isPowerOfTwo
  16. {
  17. template <typename genType>
  18. struct type
  19. {
  20. genType Value;
  21. bool Return;
  22. };
  23. 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. 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. 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. 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. 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
  130. {
  131. template <typename genIUType>
  132. GLM_FUNC_QUALIFIER genIUType highestBitValue(genIUType Value)
  133. {
  134. genIUType tmp = Value;
  135. genIUType result = genIUType(0);
  136. while(tmp)
  137. {
  138. result = (tmp & (~tmp + 1)); // grab lowest bit
  139. tmp &= ~result; // clear lowest bit
  140. }
  141. return result;
  142. }
  143. template <typename genType>
  144. GLM_FUNC_QUALIFIER genType ceilPowerOfTwo_loop(genType value)
  145. {
  146. return glm::isPowerOfTwo(value) ? value : highestBitValue(value) << 1;
  147. }
  148. template <typename genType>
  149. struct type
  150. {
  151. genType Value;
  152. genType Return;
  153. };
  154. int test_int32()
  155. {
  156. type<glm::int32> const Data[] =
  157. {
  158. {0x0000ffff, 0x00010000},
  159. {-3, -4},
  160. {-8, -8},
  161. {0x00000001, 0x00000001},
  162. {0x00000002, 0x00000002},
  163. {0x00000004, 0x00000004},
  164. {0x00000007, 0x00000008},
  165. {0x0000fff0, 0x00010000},
  166. {0x0000f000, 0x00010000},
  167. {0x08000000, 0x08000000},
  168. {0x00000000, 0x00000000},
  169. {0x00000003, 0x00000004}
  170. };
  171. int Error(0);
  172. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::int32>); i < n; ++i)
  173. {
  174. glm::int32 Result = glm::ceilPowerOfTwo(Data[i].Value);
  175. Error += Data[i].Return == Result ? 0 : 1;
  176. }
  177. return Error;
  178. }
  179. int test_uint32()
  180. {
  181. type<glm::uint32> const Data[] =
  182. {
  183. {0x00000001, 0x00000001},
  184. {0x00000002, 0x00000002},
  185. {0x00000004, 0x00000004},
  186. {0x00000007, 0x00000008},
  187. {0x0000ffff, 0x00010000},
  188. {0x0000fff0, 0x00010000},
  189. {0x0000f000, 0x00010000},
  190. {0x80000000, 0x80000000},
  191. {0x00000000, 0x00000000},
  192. {0x00000003, 0x00000004}
  193. };
  194. int Error(0);
  195. for(std::size_t i = 0, n = sizeof(Data) / sizeof(type<glm::uint32>); i < n; ++i)
  196. {
  197. glm::uint32 Result = glm::ceilPowerOfTwo(Data[i].Value);
  198. Error += Data[i].Return == Result ? 0 : 1;
  199. }
  200. return Error;
  201. }
  202. int perf()
  203. {
  204. int Error(0);
  205. std::vector<glm::uint> v;
  206. v.resize(100000000);
  207. std::clock_t Timestramp0 = std::clock();
  208. for(glm::uint32 i = 0, n = static_cast<glm::uint>(v.size()); i < n; ++i)
  209. v[i] = ceilPowerOfTwo_loop(i);
  210. std::clock_t Timestramp1 = std::clock();
  211. for(glm::uint32 i = 0, n = static_cast<glm::uint>(v.size()); i < n; ++i)
  212. v[i] = glm::ceilPowerOfTwo(i);
  213. std::clock_t Timestramp2 = std::clock();
  214. std::printf("ceilPowerOfTwo_loop: %d clocks\n", static_cast<unsigned int>(Timestramp1 - Timestramp0));
  215. std::printf("glm::ceilPowerOfTwo: %d clocks\n", static_cast<unsigned int>(Timestramp2 - Timestramp1));
  216. return Error;
  217. }
  218. int test()
  219. {
  220. int Error(0);
  221. Error += test_int32();
  222. Error += test_uint32();
  223. return Error;
  224. }
  225. }//namespace ceilPowerOfTwo
  226. int main()
  227. {
  228. int Error(0);
  229. Error += isPowerOfTwo::test();
  230. Error += ceilPowerOfTwo::test();
  231. Error += ceilPowerOfTwo::perf();
  232. return Error;
  233. }