summaryrefslogtreecommitdiff
path: root/src/glm/gtx/fast_exponential.inl
blob: 59c0397a87f319b027567bc95bb3ac74124d3590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
///////////////////////////////////////////////////////////////////////////////////////////////////
// OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2006-01-09
// Updated : 2006-01-09
// Licence : This source is under MIT License
// File    : glm/gtx/fast_exponential.h
///////////////////////////////////////////////////////////////////////////////////////////////////

namespace glm{
namespace gtx{
namespace fast_exponential
{
    // fastPow:
	template <typename T>
    GLM_FUNC_QUALIFIER T fastPow(const T x, const T y)
    {
        return exp(y * log(x));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec2<T> fastPow(
		const detail::tvec2<T>& x, 
		const detail::tvec2<T>& y)
    {
        return detail::tvec2<T>(
            fastPow(x.x, y.x),
            fastPow(x.y, y.y));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec3<T> fastPow(
		const detail::tvec3<T>& x, 
		const detail::tvec3<T>& y)
    {
        return detail::tvec3<T>(
            fastPow(x.x, y.x),
            fastPow(x.y, y.y),
            fastPow(x.z, y.z));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec4<T> fastPow(
		const detail::tvec4<T>& x, 
		const detail::tvec4<T>& y)
    {
        return detail::tvec4<T>(
            fastPow(x.x, y.x),
            fastPow(x.y, y.y),
            fastPow(x.z, y.z),
            fastPow(x.w, y.w));
    }

	template <typename T>
	GLM_FUNC_QUALIFIER T fastPow(const T x, int y)
    {
        T f = T(1);
        for(int i = 0; i < y; ++i)
            f *= x;
        return f;
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec2<T> fastPow(
		const detail::tvec2<T>& x, 
		const detail::tvec2<int>& y)
    {
        return detail::tvec2<T>(
            fastPow(x.x, y.x),
            fastPow(x.y, y.y));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec3<T> fastPow(
		const detail::tvec3<T>& x, 
		const detail::tvec3<int>& y)
    {
        return detail::tvec3<T>(
            fastPow(x.x, y.x),
            fastPow(x.y, y.y),
            fastPow(x.z, y.z));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec4<T> fastPow(
		const detail::tvec4<T>& x, 
		const detail::tvec4<int>& y)
    {
        return detail::tvec4<T>(
            fastPow(x.x, y.x),
            fastPow(x.y, y.y),
            fastPow(x.z, y.z),
            fastPow(x.w, y.w));
    }

    // fastExp
    // Note: This function provides accurate results only for value between -1 and 1, else avoid it.
	template <typename T>
    GLM_FUNC_QUALIFIER T fastExp(const T x)
    {
        // This has a better looking and same performance in release mode than the following code. However, in debug mode it's slower.
        // return 1.0f + x * (1.0f + x * 0.5f * (1.0f + x * 0.3333333333f * (1.0f + x * 0.25 * (1.0f + x * 0.2f))));
        T x2 = x * x;
        T x3 = x2 * x;
        T x4 = x3 * x;
        T x5 = x4 * x;
        return T(1) + x + (x2 * T(0.5)) + (x3 * T(0.1666666667)) + (x4 * T(0.041666667)) + (x5 * T(0.008333333333));
    }
/*  // Try to handle all values of float... but often shower than std::exp, glm::floor and the loop kill the performance
    GLM_FUNC_QUALIFIER float fastExp(float x)
    {
        const float e = 2.718281828f;
        const float IntegerPart = floor(x);
        const float FloatPart = x - IntegerPart;
        float z = 1.f;

        for(int i = 0; i < int(IntegerPart); ++i)
            z *= e;

        const float x2 = FloatPart * FloatPart;
        const float x3 = x2 * FloatPart;
        const float x4 = x3 * FloatPart;
        const float x5 = x4 * FloatPart;
        return z * (1.0f + FloatPart + (x2 * 0.5f) + (x3 * 0.1666666667f) + (x4 * 0.041666667f) + (x5 * 0.008333333333f));
    }

    // Increase accuracy on number bigger that 1 and smaller than -1 but it's not enough for high and negative numbers
    GLM_FUNC_QUALIFIER float fastExp(float x)
    {
        // This has a better looking and same performance in release mode than the following code. However, in debug mode it's slower.
        // return 1.0f + x * (1.0f + x * 0.5f * (1.0f + x * 0.3333333333f * (1.0f + x * 0.25 * (1.0f + x * 0.2f))));
        float x2 = x * x;
        float x3 = x2 * x;
        float x4 = x3 * x;
        float x5 = x4 * x;
        float x6 = x5 * x;
        float x7 = x6 * x;
        float x8 = x7 * x;
        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);;
    }
*/
    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec2<T> fastExp(
		const detail::tvec2<T>& x)
    {
        return detail::tvec2<T>(
            fastExp(x.x),
            fastExp(x.y));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec3<T> fastExp(
		const detail::tvec3<T>& x)
    {
        return detail::tvec3<T>(
            fastExp(x.x),
            fastExp(x.y),
            fastExp(x.z));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec4<T> fastExp(
		const detail::tvec4<T>& x)
    {
        return detail::tvec4<T>(
            fastExp(x.x),
            fastExp(x.y),
            fastExp(x.z),
            fastExp(x.w));
    }

    // fastLog
	template <typename T>
    GLM_FUNC_QUALIFIER T fastLog(const T x)
    {
        return std::log(x);
    }

    /* Slower than the VC7.1 function...
    GLM_FUNC_QUALIFIER float fastLog(float x)
    {
        float y1 = (x - 1.0f) / (x + 1.0f);
        float y2 = y1 * y1;
        return 2.0f * y1 * (1.0f + y2 * (0.3333333333f + y2 * (0.2f + y2 * 0.1428571429f)));
    }
    */

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec2<T> fastLog(
		const detail::tvec2<T>& x)
    {
        return detail::tvec2<T>(
            fastLog(x.x),
            fastLog(x.y));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec3<T> fastLog(
		const detail::tvec3<T>& x)
    {
        return detail::tvec3<T>(
            fastLog(x.x),
            fastLog(x.y),
            fastLog(x.z));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec4<T> fastLog(
		const detail::tvec4<T>& x)
    {
        return detail::tvec4<T>(
            fastLog(x.x),
            fastLog(x.y),
            fastLog(x.z),
            fastLog(x.w));
    }

    //fastExp2, ln2 = 0.69314718055994530941723212145818f
	template <typename T>
    GLM_FUNC_QUALIFIER T fastExp2(const T x)
    {
        return fastExp(0.69314718055994530941723212145818f * x);
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec2<T> fastExp2(
		const detail::tvec2<T>& x)
    {
        return detail::tvec2<T>(
            fastExp2(x.x),
            fastExp2(x.y));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec3<T> fastExp2(
		const detail::tvec3<T>& x)
    {
        return detail::tvec3<T>(
            fastExp2(x.x),
            fastExp2(x.y),
            fastExp2(x.z));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec4<T> fastExp2(
		const detail::tvec4<T>& x)
    {
        return detail::tvec4<T>(
            fastExp2(x.x),
            fastExp2(x.y),
            fastExp2(x.z),
            fastExp2(x.w));
    }

    // fastLog2, ln2 = 0.69314718055994530941723212145818f
	template <typename T>
    GLM_FUNC_QUALIFIER T fastLog2(const T x)
    {
        return fastLog(x) / 0.69314718055994530941723212145818f;
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec2<T> fastLog2(
		const detail::tvec2<T>& x)
    {
        return detail::tvec2<T>(
            fastLog2(x.x),
            fastLog2(x.y));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec3<T> fastLog2(
		const detail::tvec3<T>& x)
    {
        return detail::tvec3<T>(
            fastLog2(x.x),
            fastLog2(x.y),
            fastLog2(x.z));
    }

    template <typename T>
    GLM_FUNC_QUALIFIER detail::tvec4<T> fastLog2(
		const detail::tvec4<T>& x)
    {
        return detail::tvec4<T>(
            fastLog2(x.x),
            fastLog2(x.y),
            fastLog2(x.z),
            fastLog2(x.w));
    }

}//namespace fast_exponential
}//namespace gtx
}//namespace glm