Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.1KB

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