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.

148 lines
4.4 KiB

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