gtc_integer.cpp 5.1 KB

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