gtx_integer.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2011 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::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 < 1000000; ++i)
  29. {
  30. glm::uint A = glm::log2(glm::uint(i));
  31. double B = glm::log2(double(i));
  32. Error += glm::equalEpsilon(double(A), B, 1.0) ? 0 : 1;
  33. //assert(!Error);
  34. }
  35. return Error;
  36. }
  37. int test_nlz()
  38. {
  39. int Error = 0;
  40. for(std::size_t i = 1; i < 33; ++i)
  41. printf("%d, %d\n", glm::nlz(i), 31u - glm::findMSB(i));
  42. return Error;
  43. }
  44. int main()
  45. {
  46. int Error = 0;
  47. Error += test_nlz();
  48. Error += test_floor_log2();
  49. Error += test_log2();
  50. return Error;
  51. }