Log2Estimate.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Math.h>
  9. // Minimax polynomial approximations to log2(x). The polynomial p(x) of
  10. // degree D minimizes the quantity maximum{|log2(x) - p(x)| : x in [1,2]}
  11. // over all polynomials of degree D.
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class Log2Estimate
  16. {
  17. public:
  18. // The input constraint is x in [1,2]. For example,
  19. // float x; // in [1,2]
  20. // float result = Log2Estimate<float>::Degree<3>(x);
  21. template <int D>
  22. inline static Real Degree(Real x)
  23. {
  24. Real t = x - (Real)1; // t in (0,1]
  25. return Evaluate(degree<D>(), t);
  26. }
  27. // The input constraint is x > 0. Range reduction is used to generate
  28. // a value y in (0,1], call Degree(y), and add the exponent for the
  29. // power of two in the binary scientific representation of x. For
  30. // example,
  31. // float x; // x > 0
  32. // float result = Log2Estimate<float>::DegreeRR<3>(x);
  33. template <int D>
  34. inline static Real DegreeRR(Real x)
  35. {
  36. int p;
  37. Real y = std::frexp(x, &p); // y in [1/2,1)
  38. y = ((Real)2) * y; // y in [1,2)
  39. --p;
  40. Real poly = Degree<D>(y);
  41. Real result = poly + (Real)p;
  42. return result;
  43. }
  44. private:
  45. // Metaprogramming and private implementation to allow specialization
  46. // of a template member function.
  47. template <int D> struct degree {};
  48. inline static Real Evaluate(degree<1>, Real t)
  49. {
  50. Real poly;
  51. poly = (Real)GTE_C_LOG2_DEG1_C1;
  52. poly = poly * t;
  53. return poly;
  54. }
  55. inline static Real Evaluate(degree<2>, Real t)
  56. {
  57. Real poly;
  58. poly = (Real)GTE_C_LOG2_DEG2_C2;
  59. poly = (Real)GTE_C_LOG2_DEG2_C1 + poly * t;
  60. poly = poly * t;
  61. return poly;
  62. }
  63. inline static Real Evaluate(degree<3>, Real t)
  64. {
  65. Real poly;
  66. poly = (Real)GTE_C_LOG2_DEG3_C3;
  67. poly = (Real)GTE_C_LOG2_DEG3_C2 + poly * t;
  68. poly = (Real)GTE_C_LOG2_DEG3_C1 + poly * t;
  69. poly = poly * t;
  70. return poly;
  71. }
  72. inline static Real Evaluate(degree<4>, Real t)
  73. {
  74. Real poly;
  75. poly = (Real)GTE_C_LOG2_DEG4_C4;
  76. poly = (Real)GTE_C_LOG2_DEG4_C3 + poly * t;
  77. poly = (Real)GTE_C_LOG2_DEG4_C2 + poly * t;
  78. poly = (Real)GTE_C_LOG2_DEG4_C1 + poly * t;
  79. poly = poly * t;
  80. return poly;
  81. }
  82. inline static Real Evaluate(degree<5>, Real t)
  83. {
  84. Real poly;
  85. poly = (Real)GTE_C_LOG2_DEG5_C5;
  86. poly = (Real)GTE_C_LOG2_DEG5_C4 + poly * t;
  87. poly = (Real)GTE_C_LOG2_DEG5_C3 + poly * t;
  88. poly = (Real)GTE_C_LOG2_DEG5_C2 + poly * t;
  89. poly = (Real)GTE_C_LOG2_DEG5_C1 + poly * t;
  90. poly = poly * t;
  91. return poly;
  92. }
  93. inline static Real Evaluate(degree<6>, Real t)
  94. {
  95. Real poly;
  96. poly = (Real)GTE_C_LOG2_DEG6_C6;
  97. poly = (Real)GTE_C_LOG2_DEG6_C5 + poly * t;
  98. poly = (Real)GTE_C_LOG2_DEG6_C4 + poly * t;
  99. poly = (Real)GTE_C_LOG2_DEG6_C3 + poly * t;
  100. poly = (Real)GTE_C_LOG2_DEG6_C2 + poly * t;
  101. poly = (Real)GTE_C_LOG2_DEG6_C1 + poly * t;
  102. poly = poly * t;
  103. return poly;
  104. }
  105. inline static Real Evaluate(degree<7>, Real t)
  106. {
  107. Real poly;
  108. poly = (Real)GTE_C_LOG2_DEG7_C7;
  109. poly = (Real)GTE_C_LOG2_DEG7_C6 + poly * t;
  110. poly = (Real)GTE_C_LOG2_DEG7_C5 + poly * t;
  111. poly = (Real)GTE_C_LOG2_DEG7_C4 + poly * t;
  112. poly = (Real)GTE_C_LOG2_DEG7_C3 + poly * t;
  113. poly = (Real)GTE_C_LOG2_DEG7_C2 + poly * t;
  114. poly = (Real)GTE_C_LOG2_DEG7_C1 + poly * t;
  115. poly = poly * t;
  116. return poly;
  117. }
  118. inline static Real Evaluate(degree<8>, Real t)
  119. {
  120. Real poly;
  121. poly = (Real)GTE_C_LOG2_DEG8_C8;
  122. poly = (Real)GTE_C_LOG2_DEG8_C7 + poly * t;
  123. poly = (Real)GTE_C_LOG2_DEG8_C6 + poly * t;
  124. poly = (Real)GTE_C_LOG2_DEG8_C5 + poly * t;
  125. poly = (Real)GTE_C_LOG2_DEG8_C4 + poly * t;
  126. poly = (Real)GTE_C_LOG2_DEG8_C3 + poly * t;
  127. poly = (Real)GTE_C_LOG2_DEG8_C2 + poly * t;
  128. poly = (Real)GTE_C_LOG2_DEG8_C1 + poly * t;
  129. poly = poly * t;
  130. return poly;
  131. }
  132. };
  133. }