Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / auxiliary / util / u_math.h
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Math utilities and approximations for common math functions.
31 * Reduced precision is usually acceptable in shaders...
32 *
33 * "fast" is used in the names of functions which are low-precision,
34 * or at least lower-precision than the normal C lib functions.
35 */
36
37
38 #ifndef U_MATH_H
39 #define U_MATH_H
40
41
42 #include "pipe/p_compiler.h"
43 #include "pipe/p_debug.h"
44
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50
51 #if defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
52 __inline double ceil(double val)
53 {
54 double ceil_val;
55
56 if((val - (long) val) == 0) {
57 ceil_val = val;
58 }
59 else {
60 if(val > 0) {
61 ceil_val = (long) val + 1;
62 }
63 else {
64 ceil_val = (long) val;
65 }
66 }
67
68 return ceil_val;
69 }
70
71 #ifndef PIPE_SUBSYSTEM_WINDOWS_CE
72 __inline double floor(double val)
73 {
74 double floor_val;
75
76 if((val - (long) val) == 0) {
77 floor_val = val;
78 }
79 else {
80 if(val > 0) {
81 floor_val = (long) val;
82 }
83 else {
84 floor_val = (long) val - 1;
85 }
86 }
87
88 return floor_val;
89 }
90 #endif
91
92 #pragma function(pow)
93 __inline double __cdecl pow(double val, double exponent)
94 {
95 /* XXX */
96 assert(0);
97 return 0;
98 }
99
100 #pragma function(log)
101 __inline double __cdecl log(double val)
102 {
103 /* XXX */
104 assert(0);
105 return 0;
106 }
107
108 #pragma function(atan2)
109 __inline double __cdecl atan2(double val)
110 {
111 /* XXX */
112 assert(0);
113 return 0;
114 }
115 #else
116 #include <math.h>
117 #include <stdarg.h>
118 #endif
119
120
121 #if defined(_MSC_VER)
122 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
123
124 static INLINE float cosf( float f )
125 {
126 return (float) cos( (double) f );
127 }
128
129 static INLINE float sinf( float f )
130 {
131 return (float) sin( (double) f );
132 }
133
134 static INLINE float ceilf( float f )
135 {
136 return (float) ceil( (double) f );
137 }
138
139 static INLINE float floorf( float f )
140 {
141 return (float) floor( (double) f );
142 }
143
144 static INLINE float powf( float f, float g )
145 {
146 return (float) pow( (double) f, (double) g );
147 }
148
149 static INLINE float sqrtf( float f )
150 {
151 return (float) sqrt( (double) f );
152 }
153
154 static INLINE float fabsf( float f )
155 {
156 return (float) fabs( (double) f );
157 }
158
159 static INLINE float logf( float f )
160 {
161 return (float) log( (double) f );
162 }
163
164 #else
165 /* Work-around an extra semi-colon in VS 2005 logf definition */
166 #ifdef logf
167 #undef logf
168 #define logf(x) ((float)log((double)(x)))
169 #endif /* logf */
170 #endif
171 #endif /* _MSC_VER */
172
173
174
175
176
177 #define POW2_TABLE_SIZE 256
178 #define POW2_TABLE_SCALE ((float) (POW2_TABLE_SIZE-1))
179 extern float pow2_table[POW2_TABLE_SIZE];
180
181
182
183 extern void
184 util_init_math(void);
185
186
187 union fi {
188 float f;
189 int i;
190 unsigned ui;
191 };
192
193
194 /**
195 * Fast approximation to exp(x).
196 * Compute with base 2 exponents: exp(x) = exp2(log2(e) * x)
197 * Note: log2(e) is a constant, k = 1.44269
198 * So, exp(x) = exp2(k * x);
199 * Identity: exp2(a + b) = exp2(a) * exp2(b)
200 * Let ipart = int(k*x)
201 * Let fpart = k*x - ipart;
202 * So, exp2(k*x) = exp2(ipart) * exp2(fpart)
203 * Compute exp2(ipart) with i << ipart
204 * Compute exp2(fpart) with lookup table.
205 */
206 static INLINE float
207 util_fast_exp(float x)
208 {
209 if (x >= 0.0f) {
210 float k = 1.44269f; /* = log2(e) */
211 float kx = k * x;
212 int ipart = (int) kx;
213 float fpart = kx - (float) ipart;
214 float y = (float) (1 << ipart)
215 * pow2_table[(int) (fpart * POW2_TABLE_SCALE)];
216 return y;
217 }
218 else {
219 /* exp(-x) = 1.0 / exp(x) */
220 float k = -1.44269f;
221 float kx = k * x;
222 int ipart = (int) kx;
223 float fpart = kx - (float) ipart;
224 float y = (float) (1 << ipart)
225 * pow2_table[(int) (fpart * POW2_TABLE_SCALE)];
226 return 1.0f / y;
227 }
228 }
229
230
231 /**
232 * Fast version of 2^x
233 * XXX the above function could be implemented in terms of this one.
234 */
235 static INLINE float
236 util_fast_exp2(float x)
237 {
238 if (x >= 0.0f) {
239 int ipart = (int) x;
240 float fpart = x - (float) ipart;
241 float y = (float) (1 << ipart)
242 * pow2_table[(int) (fpart * POW2_TABLE_SCALE)];
243 return y;
244 }
245 else {
246 /* exp(-x) = 1.0 / exp(x) */
247 int ipart = (int) -x;
248 float fpart = -x - (float) ipart;
249 float y = (float) (1 << ipart)
250 * pow2_table[(int) (fpart * POW2_TABLE_SCALE)];
251 return 1.0f / y;
252 }
253 }
254
255
256 /**
257 * Based on code from http://www.flipcode.com/totd/
258 */
259 static INLINE float
260 util_fast_log2(float val)
261 {
262 union fi num;
263 int log_2;
264 num.f = val;
265 log_2 = ((num.i >> 23) & 255) - 128;
266 num.i &= ~(255 << 23);
267 num.i += 127 << 23;
268 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
269 return num.f + log_2;
270 }
271
272
273 static INLINE float
274 util_fast_pow(float x, float y)
275 {
276 /* XXX these tests may need adjustment */
277 if (y >= 3.0f && (-0.02f <= x && x <= 0.02f))
278 return 0.0f;
279 if (y >= 50.0f && (-0.9f <= x && x <= 0.9f))
280 return 0.0f;
281 return util_fast_exp2(util_fast_log2(x) * y);
282 }
283
284
285
286 /**
287 * Floor(x), returned as int.
288 */
289 static INLINE int
290 util_ifloor(float f)
291 {
292 int ai, bi;
293 double af, bf;
294 union fi u;
295 af = (3 << 22) + 0.5 + (double)f;
296 bf = (3 << 22) + 0.5 - (double)f;
297 u.f = (float) af; ai = u.i;
298 u.f = (float) bf; bi = u.i;
299 return (ai - bi) >> 1;
300 }
301
302
303 /**
304 * Round float to nearest int.
305 */
306 static INLINE int
307 util_iround(float f)
308 {
309 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
310 int r;
311 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
312 return r;
313 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
314 int r;
315 _asm {
316 fld f
317 fistp r
318 }
319 return r;
320 #else
321 if (f >= 0.0f)
322 return (int) (f + 0.5f);
323 else
324 return (int) (f - 0.5f);
325 #endif
326 }
327
328
329
330 #if defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
331 /**
332 * Find first bit set in word. Least significant bit is 1.
333 * Return 0 if no bits set.
334 */
335 static INLINE
336 unsigned ffs( unsigned u )
337 {
338 unsigned i;
339
340 if( u == 0 ) {
341 return 0;
342 }
343
344 __asm bsf eax, [u]
345 __asm inc eax
346 __asm mov [i], eax
347
348 return i;
349 }
350 #endif
351
352
353 /**
354 * Return float bits.
355 */
356 static INLINE unsigned
357 fui( float f )
358 {
359 union fi fi;
360 fi.f = f;
361 return fi.ui;
362 }
363
364
365
366 static INLINE float
367 ubyte_to_float(ubyte ub)
368 {
369 return (float) ub * (1.0f / 255.0f);
370 }
371
372
373 /**
374 * Convert float in [0,1] to ubyte in [0,255] with clamping.
375 */
376 static INLINE ubyte
377 float_to_ubyte(float f)
378 {
379 const int ieee_0996 = 0x3f7f0000; /* 0.996 or so */
380 union fi tmp;
381
382 tmp.f = f;
383 if (tmp.i < 0) {
384 return (ubyte) 0;
385 }
386 else if (tmp.i >= ieee_0996) {
387 return (ubyte) 255;
388 }
389 else {
390 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
391 return (ubyte) tmp.i;
392 }
393 }
394
395
396
397 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
398
399 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
400 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
401
402
403 static INLINE int
404 align(int value, int alignment)
405 {
406 return (value + alignment - 1) & ~(alignment - 1);
407 }
408
409
410 #ifndef COPY_4V
411 #define COPY_4V( DST, SRC ) \
412 do { \
413 (DST)[0] = (SRC)[0]; \
414 (DST)[1] = (SRC)[1]; \
415 (DST)[2] = (SRC)[2]; \
416 (DST)[3] = (SRC)[3]; \
417 } while (0)
418 #endif
419
420
421 #ifndef COPY_4FV
422 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
423 #endif
424
425
426 #ifndef ASSIGN_4V
427 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
428 do { \
429 (DST)[0] = (V0); \
430 (DST)[1] = (V1); \
431 (DST)[2] = (V2); \
432 (DST)[3] = (V3); \
433 } while (0)
434 #endif
435
436
437 #ifdef __cplusplus
438 }
439 #endif
440
441 #endif /* U_MATH_H */