gtx_integer.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2012 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2011-10-11
  5. // Updated : 2011-10-11
  6. // Licence : This source is under MIT licence
  7. // File : test/gtx/gtx_integer.cpp
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. #include <glm/glm.hpp>
  10. #include <glm/gtx/integer.hpp>
  11. #include <glm/gtx/epsilon.hpp>
  12. #include <cstdio>
  13. int test_floor_log2()
  14. {
  15. int Error = 0;
  16. for(std::size_t i = 1; i < 1000000; ++i)
  17. {
  18. glm::uint A = glm::floor_log2(glm::uint(i));
  19. glm::uint B = glm::uint(glm::floor(glm::log2(double(i)))); // Will fail with float, lack of accuracy
  20. Error += A == B ? 0 : 1;
  21. assert(!Error);
  22. }
  23. return Error;
  24. }
  25. int test_log2()
  26. {
  27. int Error = 0;
  28. for(std::size_t i = 1; i < 24; ++i)
  29. {
  30. glm::uint A = glm::log2(glm::uint(1 << i));
  31. glm::uint B = glm::uint(glm::log2(double(1 << i)));
  32. //Error += glm::equalEpsilon(double(A), B, 1.0) ? 0 : 1;
  33. Error += glm::abs(double(A) - B) <= 24 ? 0 : 1;
  34. assert(!Error);
  35. printf("Log2(%d) Error: %d, %d\n", 1 << i, A, B);
  36. }
  37. printf("log2 error: %d\n", Error);
  38. return Error;
  39. }
  40. int test_nlz()
  41. {
  42. int Error = 0;
  43. for(glm::uint i = 1; i < glm::uint(33); ++i)
  44. Error += glm::nlz(i) == glm::uint(31u) - glm::findMSB(i) ? 0 : 1;
  45. //printf("%d, %d\n", glm::nlz(i), 31u - glm::findMSB(i));
  46. return Error;
  47. }
  48. int main()
  49. {
  50. int Error = 0;
  51. Error += test_nlz();
  52. Error += test_floor_log2();
  53. Error += test_log2();
  54. return Error;
  55. }