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(std::size_t Count)
  78. {
  79. int Error = 0;
  80. {
  81. std::vector<int> Result;
  82. Result.resize(Count);
  83. std::clock_t Begin = clock();
  84. for(int i = 0; i < static_cast<int>(Count); ++i)
  85. Result[i] = glm::log2(static_cast<int>(i));
  86. std::clock_t End = clock();
  87. printf("glm::log2<int>: %ld clocks\n", End - Begin);
  88. }
  89. {
  90. std::vector<glm::ivec4> Result;
  91. Result.resize(Count);
  92. std::clock_t Begin = clock();
  93. for(int i = 0; i < static_cast<int>(Count); ++i)
  94. Result[i] = glm::log2(glm::ivec4(i));
  95. std::clock_t End = clock();
  96. printf("glm::log2<ivec4>: %ld clocks\n", End - Begin);
  97. }
  98. # if GLM_HAS_BITSCAN_WINDOWS
  99. {
  100. std::vector<glm::ivec4> Result;
  101. Result.resize(Count);
  102. std::clock_t Begin = clock();
  103. for(std::size_t i = 0; i < Count; ++i)
  104. {
  105. glm::tvec4<unsigned long, glm::defaultp> Tmp(glm::uninitialize);
  106. _BitScanReverse(&Tmp.x, i);
  107. _BitScanReverse(&Tmp.y, i);
  108. _BitScanReverse(&Tmp.z, i);
  109. _BitScanReverse(&Tmp.w, i);
  110. Result[i] = glm::ivec4(Tmp);
  111. }
  112. std::clock_t End = clock();
  113. printf("glm::log2<ivec4> inlined: %ld clocks\n", End - Begin);
  114. }
  115. {
  116. std::vector<glm::tvec4<unsigned long, glm::defaultp> > Result;
  117. Result.resize(Count);
  118. std::clock_t Begin = clock();
  119. for(std::size_t i = 0; i < Count; ++i)
  120. {
  121. _BitScanReverse(&Result[i].x, i);
  122. _BitScanReverse(&Result[i].y, i);
  123. _BitScanReverse(&Result[i].z, i);
  124. _BitScanReverse(&Result[i].w, i);
  125. }
  126. std::clock_t End = clock();
  127. printf("glm::log2<ivec4> inlined no cast: %ld clocks\n", End - Begin);
  128. }
  129. {
  130. std::vector<glm::ivec4> Result;
  131. Result.resize(Count);
  132. std::clock_t Begin = clock();
  133. for(std::size_t i = 0; i < Count; ++i)
  134. {
  135. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].x), i);
  136. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].y), i);
  137. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].z), i);
  138. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].w), i);
  139. }
  140. std::clock_t End = clock();
  141. printf("glm::log2<ivec4> reinterpret: %ld clocks\n", End - Begin);
  142. }
  143. # endif//GLM_HAS_BITSCAN_WINDOWS
  144. {
  145. std::vector<float> Result;
  146. Result.resize(Count);
  147. std::clock_t Begin = clock();
  148. for(std::size_t i = 0; i < Count; ++i)
  149. Result[i] = glm::log2(static_cast<float>(i));
  150. std::clock_t End = clock();
  151. printf("glm::log2<float>: %ld clocks\n", End - Begin);
  152. }
  153. {
  154. std::vector<glm::vec4> Result;
  155. Result.resize(Count);
  156. std::clock_t Begin = clock();
  157. for(int i = 0; i < static_cast<int>(Count); ++i)
  158. Result[i] = glm::log2(glm::vec4(i));
  159. std::clock_t End = clock();
  160. printf("glm::log2<vec4>: %ld clocks\n", End - Begin);
  161. }
  162. return Error;
  163. }
  164. }//namespace log2_
  165. int main()
  166. {
  167. int Error(0);
  168. Error += ::log2_::test();
  169. # ifdef NDEBUG
  170. std::size_t const Samples(1000);
  171. Error += ::log2_::perf(Samples);
  172. # endif//NDEBUG
  173. return Error;
  174. }