gtx_integer.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/gtx/gtx_integer.cpp
  28. /// @date 2011-10-11 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/exponential.hpp>
  32. #include <glm/gtc/epsilon.hpp>
  33. #include <glm/gtx/integer.hpp>
  34. #include <cstdio>
  35. /*
  36. int test_floor_log2()
  37. {
  38. int Error = 0;
  39. for(std::size_t i = 1; i < 1000000; ++i)
  40. {
  41. glm::uint A = glm::floor_log2(glm::uint(i));
  42. glm::uint B = glm::uint(glm::floor(glm::log2(double(i)))); // Will fail with float, lack of accuracy
  43. Error += A == B ? 0 : 1;
  44. assert(!Error);
  45. }
  46. return Error;
  47. }
  48. */
  49. int test_log2()
  50. {
  51. int Error = 0;
  52. for(std::size_t i = 1; i < 24; ++i)
  53. {
  54. glm::uint A = glm::log2(glm::uint(1 << i));
  55. glm::uint B = glm::uint(glm::log2(double(1 << i)));
  56. //Error += glm::equalEpsilon(double(A), B, 1.0) ? 0 : 1;
  57. Error += glm::abs(double(A) - B) <= 24 ? 0 : 1;
  58. assert(!Error);
  59. printf("Log2(%d) Error: %d, %d\n", 1 << i, A, B);
  60. }
  61. printf("log2 error: %d\n", Error);
  62. return Error;
  63. }
  64. int test_nlz()
  65. {
  66. int Error = 0;
  67. for(glm::uint i = 1; i < glm::uint(33); ++i)
  68. Error += glm::nlz(i) == glm::uint(31u) - glm::findMSB(i) ? 0 : 1;
  69. //printf("%d, %d\n", glm::nlz(i), 31u - glm::findMSB(i));
  70. return Error;
  71. }
  72. int main()
  73. {
  74. int Error = 0;
  75. Error += test_nlz();
  76. // Error += test_floor_log2();
  77. Error += test_log2();
  78. return Error;
  79. }