fast_exponential.inl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /// @ref gtx_fast_exponential
  2. namespace glm
  3. {
  4. // fastPow:
  5. template<typename genType>
  6. GLM_FUNC_QUALIFIER genType fastPow(genType x, genType y)
  7. {
  8. return exp(y * log(x));
  9. }
  10. template<length_t L, typename T, qualifier Q>
  11. GLM_FUNC_QUALIFIER vec<L, T, Q> fastPow(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
  12. {
  13. return exp(y * log(x));
  14. }
  15. template<typename T>
  16. GLM_FUNC_QUALIFIER T fastPow(T x, int y)
  17. {
  18. T f = static_cast<T>(1);
  19. for(int i = 0; i < y; ++i)
  20. f *= x;
  21. return f;
  22. }
  23. template<length_t L, typename T, qualifier Q>
  24. GLM_FUNC_QUALIFIER vec<L, T, Q> fastPow(vec<L, T, Q> const& x, vec<L, int, Q> const& y)
  25. {
  26. vec<L, T, Q> Result;
  27. for(length_t i = 0, n = x.length(); i < n; ++i)
  28. Result[i] = fastPow(x[i], y[i]);
  29. return Result;
  30. }
  31. // fastExp
  32. // Note: This function provides accurate results only for value between -1 and 1, else avoid it.
  33. template<typename T>
  34. GLM_FUNC_QUALIFIER T fastExp(T x)
  35. {
  36. // This has a better looking and same performance in release mode than the following code. However, in debug mode it's slower.
  37. // return 1.0f + x * (1.0f + x * 0.5f * (1.0f + x * 0.3333333333f * (1.0f + x * 0.25 * (1.0f + x * 0.2f))));
  38. T x2 = x * x;
  39. T x3 = x2 * x;
  40. T x4 = x3 * x;
  41. T x5 = x4 * x;
  42. return T(1) + x + (x2 * T(0.5)) + (x3 * T(0.1666666667)) + (x4 * T(0.041666667)) + (x5 * T(0.008333333333));
  43. }
  44. /* // Try to handle all values of float... but often shower than std::exp, glm::floor and the loop kill the performance
  45. GLM_FUNC_QUALIFIER float fastExp(float x)
  46. {
  47. const float e = 2.718281828f;
  48. const float IntegerPart = floor(x);
  49. const float FloatPart = x - IntegerPart;
  50. float z = 1.f;
  51. for(int i = 0; i < int(IntegerPart); ++i)
  52. z *= e;
  53. const float x2 = FloatPart * FloatPart;
  54. const float x3 = x2 * FloatPart;
  55. const float x4 = x3 * FloatPart;
  56. const float x5 = x4 * FloatPart;
  57. return z * (1.0f + FloatPart + (x2 * 0.5f) + (x3 * 0.1666666667f) + (x4 * 0.041666667f) + (x5 * 0.008333333333f));
  58. }
  59. // Increase accuracy on number bigger that 1 and smaller than -1 but it's not enough for high and negative numbers
  60. GLM_FUNC_QUALIFIER float fastExp(float x)
  61. {
  62. // This has a better looking and same performance in release mode than the following code. However, in debug mode it's slower.
  63. // return 1.0f + x * (1.0f + x * 0.5f * (1.0f + x * 0.3333333333f * (1.0f + x * 0.25 * (1.0f + x * 0.2f))));
  64. float x2 = x * x;
  65. float x3 = x2 * x;
  66. float x4 = x3 * x;
  67. float x5 = x4 * x;
  68. float x6 = x5 * x;
  69. float x7 = x6 * x;
  70. float x8 = x7 * x;
  71. return 1.0f + x + (x2 * 0.5f) + (x3 * 0.1666666667f) + (x4 * 0.041666667f) + (x5 * 0.008333333333f)+ (x6 * 0.00138888888888f) + (x7 * 0.000198412698f) + (x8 * 0.0000248015873f);;
  72. }
  73. */
  74. template<length_t L, typename T, qualifier Q>
  75. GLM_FUNC_QUALIFIER vec<L, T, Q> fastExp(vec<L, T, Q> const& x)
  76. {
  77. return detail::functor1<vec, L, T, T, Q>::call(fastExp, x);
  78. }
  79. // fastLog
  80. template<typename genType>
  81. GLM_FUNC_QUALIFIER genType fastLog(genType x)
  82. {
  83. return std::log(x);
  84. }
  85. /* Slower than the VC7.1 function...
  86. GLM_FUNC_QUALIFIER float fastLog(float x)
  87. {
  88. float y1 = (x - 1.0f) / (x + 1.0f);
  89. float y2 = y1 * y1;
  90. return 2.0f * y1 * (1.0f + y2 * (0.3333333333f + y2 * (0.2f + y2 * 0.1428571429f)));
  91. }
  92. */
  93. template<length_t L, typename T, qualifier Q>
  94. GLM_FUNC_QUALIFIER vec<L, T, Q> fastLog(vec<L, T, Q> const& x)
  95. {
  96. return detail::functor1<vec, L, T, T, Q>::call(fastLog, x);
  97. }
  98. //fastExp2, ln2 = 0.69314718055994530941723212145818f
  99. template<typename genType>
  100. GLM_FUNC_QUALIFIER genType fastExp2(genType x)
  101. {
  102. return fastExp(0.69314718055994530941723212145818f * x);
  103. }
  104. template<length_t L, typename T, qualifier Q>
  105. GLM_FUNC_QUALIFIER vec<L, T, Q> fastExp2(vec<L, T, Q> const& x)
  106. {
  107. return detail::functor1<vec, L, T, T, Q>::call(fastExp2, x);
  108. }
  109. // fastLog2, ln2 = 0.69314718055994530941723212145818f
  110. template<typename genType>
  111. GLM_FUNC_QUALIFIER genType fastLog2(genType x)
  112. {
  113. return fastLog(x) / 0.69314718055994530941723212145818f;
  114. }
  115. template<length_t L, typename T, qualifier Q>
  116. GLM_FUNC_QUALIFIER vec<L, T, Q> fastLog2(vec<L, T, Q> const& x)
  117. {
  118. return detail::functor1<vec, L, T, T, Q>::call(fastLog2, x);
  119. }
  120. }//namespace glm