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