gtc_integer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /// 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/gtc/gtc_integer.cpp
  28. /// @date 2014-11-17 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #define GLM_FORCE_INLINE
  32. #include <glm/gtc/epsilon.hpp>
  33. #include <glm/gtc/integer.hpp>
  34. #include <glm/gtc/type_precision.hpp>
  35. #include <glm/gtc/vec1.hpp>
  36. #include <glm/gtx/type_aligned.hpp>
  37. #include <glm/vector_relational.hpp>
  38. #include <glm/vec2.hpp>
  39. #include <glm/vec3.hpp>
  40. #include <glm/vec4.hpp>
  41. #include <ctime>
  42. #include <cstdio>
  43. #include <vector>
  44. #include <cmath>
  45. namespace log2_
  46. {
  47. int test()
  48. {
  49. int Error = 0;
  50. int A0 = static_cast<int>(glm::log2(16.f));
  51. glm::ivec1 B0(glm::log2(glm::vec1(16.f)));
  52. glm::ivec2 C0(glm::log2(glm::vec2(16.f)));
  53. glm::ivec3 D0(glm::log2(glm::vec3(16.f)));
  54. glm::ivec4 E0(glm::log2(glm::vec4(16.f)));
  55. int A1 = glm::log2(int(16));
  56. glm::ivec1 B1 = glm::log2(glm::ivec1(16));
  57. glm::ivec2 C1 = glm::log2(glm::ivec2(16));
  58. glm::ivec3 D1 = glm::log2(glm::ivec3(16));
  59. glm::ivec4 E1 = glm::log2(glm::ivec4(16));
  60. Error += A0 == A1 ? 0 : 1;
  61. Error += glm::all(glm::equal(B0, B1)) ? 0 : 1;
  62. Error += glm::all(glm::equal(C0, C1)) ? 0 : 1;
  63. Error += glm::all(glm::equal(D0, D1)) ? 0 : 1;
  64. Error += glm::all(glm::equal(E0, E1)) ? 0 : 1;
  65. glm::uint64 A2 = glm::log2(glm::uint64(16));
  66. glm::u64vec1 B2 = glm::log2(glm::u64vec1(16));
  67. glm::u64vec2 C2 = glm::log2(glm::u64vec2(16));
  68. glm::u64vec3 D2 = glm::log2(glm::u64vec3(16));
  69. glm::u64vec4 E2 = glm::log2(glm::u64vec4(16));
  70. Error += A2 == glm::uint64(4) ? 0 : 1;
  71. Error += glm::all(glm::equal(B2, glm::u64vec1(4))) ? 0 : 1;
  72. Error += glm::all(glm::equal(C2, glm::u64vec2(4))) ? 0 : 1;
  73. Error += glm::all(glm::equal(D2, glm::u64vec3(4))) ? 0 : 1;
  74. Error += glm::all(glm::equal(E2, glm::u64vec4(4))) ? 0 : 1;
  75. return Error;
  76. }
  77. int perf()
  78. {
  79. int Error = 0;
  80. std::size_t const Count(100000000);
  81. {
  82. std::vector<int> Result;
  83. Result.resize(Count);
  84. std::clock_t Begin = clock();
  85. for(int i = 0; i < static_cast<int>(Count); ++i)
  86. Result[i] = glm::log2(static_cast<int>(i));
  87. std::clock_t End = clock();
  88. printf("glm::log2<int>: %d clocks\n", End - Begin);
  89. }
  90. {
  91. std::vector<glm::ivec4> Result;
  92. Result.resize(Count);
  93. std::clock_t Begin = clock();
  94. for(int i = 0; i < static_cast<int>(Count); ++i)
  95. Result[i] = glm::log2(glm::ivec4(i));
  96. std::clock_t End = clock();
  97. printf("glm::log2<ivec4>: %d clocks\n", End - Begin);
  98. }
  99. # if GLM_HAS_BITSCAN_WINDOWS
  100. {
  101. std::vector<glm::ivec4> Result;
  102. Result.resize(Count);
  103. std::clock_t Begin = clock();
  104. for(std::size_t i = 0; i < Count; ++i)
  105. {
  106. glm::tvec4<unsigned long, glm::defaultp> Tmp(glm::uninitialize);
  107. _BitScanReverse(&Tmp.x, i);
  108. _BitScanReverse(&Tmp.y, i);
  109. _BitScanReverse(&Tmp.z, i);
  110. _BitScanReverse(&Tmp.w, i);
  111. Result[i] = glm::ivec4(Tmp);
  112. }
  113. std::clock_t End = clock();
  114. printf("glm::log2<ivec4> inlined: %d clocks\n", End - Begin);
  115. }
  116. {
  117. std::vector<glm::tvec4<unsigned long, glm::defaultp> > Result;
  118. Result.resize(Count);
  119. std::clock_t Begin = clock();
  120. for(std::size_t i = 0; i < Count; ++i)
  121. {
  122. _BitScanReverse(&Result[i].x, i);
  123. _BitScanReverse(&Result[i].y, i);
  124. _BitScanReverse(&Result[i].z, i);
  125. _BitScanReverse(&Result[i].w, i);
  126. }
  127. std::clock_t End = clock();
  128. printf("glm::log2<ivec4> inlined no cast: %d clocks\n", End - Begin);
  129. }
  130. {
  131. std::vector<glm::ivec4> Result;
  132. Result.resize(Count);
  133. std::clock_t Begin = clock();
  134. for(std::size_t i = 0; i < Count; ++i)
  135. {
  136. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].x), i);
  137. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].y), i);
  138. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].z), i);
  139. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].w), i);
  140. }
  141. std::clock_t End = clock();
  142. printf("glm::log2<ivec4> reinterpret: %d clocks\n", End - Begin);
  143. }
  144. # endif//GLM_HAS_BITSCAN_WINDOWS
  145. {
  146. std::vector<float> Result;
  147. Result.resize(Count);
  148. std::clock_t Begin = clock();
  149. for(std::size_t i = 0; i < Count; ++i)
  150. Result[i] = glm::log2(static_cast<float>(i));
  151. std::clock_t End = clock();
  152. printf("glm::log2<float>: %d clocks\n", End - Begin);
  153. }
  154. {
  155. std::vector<glm::vec4> Result;
  156. Result.resize(Count);
  157. std::clock_t Begin = clock();
  158. for(int i = 0; i < static_cast<int>(Count); ++i)
  159. Result[i] = glm::log2(glm::vec4(i));
  160. std::clock_t End = clock();
  161. printf("glm::log2<vec4>: %d clocks\n", End - Begin);
  162. }
  163. return Error;
  164. }
  165. }//namespace log2_
  166. int main()
  167. {
  168. int Error(0);
  169. Error += ::log2_::test();
  170. # ifdef NDEBUG
  171. Error += ::log2_::perf();
  172. # endif//NDEBUG
  173. return Error;
  174. }