Merge branch 'mesa_7_6_branch'
[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 "util/u_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_OGL
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
123 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
124
125 static INLINE float cosf( float f )
126 {
127 return (float) cos( (double) f );
128 }
129
130 static INLINE float sinf( float f )
131 {
132 return (float) sin( (double) f );
133 }
134
135 static INLINE float ceilf( float f )
136 {
137 return (float) ceil( (double) f );
138 }
139
140 static INLINE float floorf( float f )
141 {
142 return (float) floor( (double) f );
143 }
144
145 static INLINE float powf( float f, float g )
146 {
147 return (float) pow( (double) f, (double) g );
148 }
149
150 static INLINE float sqrtf( float f )
151 {
152 return (float) sqrt( (double) f );
153 }
154
155 static INLINE float fabsf( float f )
156 {
157 return (float) fabs( (double) f );
158 }
159
160 static INLINE float logf( float f )
161 {
162 return (float) log( (double) f );
163 }
164
165 #else
166 /* Work-around an extra semi-colon in VS 2005 logf definition */
167 #ifdef logf
168 #undef logf
169 #define logf(x) ((float)log((double)(x)))
170 #endif /* logf */
171 #endif
172
173 static INLINE double log2( double x )
174 {
175 const double invln2 = 1.442695041;
176 return log( x ) * invln2;
177 }
178
179 #endif /* _MSC_VER */
180
181
182
183
184
185 #define POW2_TABLE_SIZE_LOG2 9
186 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
187 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
188 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
189 extern float pow2_table[POW2_TABLE_SIZE];
190
191
192 /**
193 * Initialize math module. This should be called before using any
194 * other functions in this module.
195 */
196 extern void
197 util_init_math(void);
198
199
200 union fi {
201 float f;
202 int32_t i;
203 uint32_t ui;
204 };
205
206
207 /**
208 * Fast version of 2^x
209 * Identity: exp2(a + b) = exp2(a) * exp2(b)
210 * Let ipart = int(x)
211 * Let fpart = x - ipart;
212 * So, exp2(x) = exp2(ipart) * exp2(fpart)
213 * Compute exp2(ipart) with i << ipart
214 * Compute exp2(fpart) with lookup table.
215 */
216 static INLINE float
217 util_fast_exp2(float x)
218 {
219 int32_t ipart;
220 float fpart, mpart;
221 union fi epart;
222
223 if(x > 129.00000f)
224 return 3.402823466e+38f;
225
226 if (x < -126.99999f)
227 return 0.0f;
228
229 ipart = (int32_t) x;
230 fpart = x - (float) ipart;
231
232 /* same as
233 * epart.f = (float) (1 << ipart)
234 * but faster and without integer overflow for ipart > 31
235 */
236 epart.i = (ipart + 127 ) << 23;
237
238 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
239
240 return epart.f * mpart;
241 }
242
243
244 /**
245 * Fast approximation to exp(x).
246 */
247 static INLINE float
248 util_fast_exp(float x)
249 {
250 const float k = 1.44269f; /* = log2(e) */
251 return util_fast_exp2(k * x);
252 }
253
254
255 #define LOG2_TABLE_SIZE_LOG2 16
256 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
257 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
258 extern float log2_table[LOG2_TABLE_SIZE];
259
260
261 /**
262 * Fast approximation to log2(x).
263 */
264 static INLINE float
265 util_fast_log2(float x)
266 {
267 union fi num;
268 float epart, mpart;
269 num.f = x;
270 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
271 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
272 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
273 return epart + mpart;
274 }
275
276
277 /**
278 * Fast approximation to x^y.
279 */
280 static INLINE float
281 util_fast_pow(float x, float y)
282 {
283 return util_fast_exp2(util_fast_log2(x) * y);
284 }
285
286 /* Note that this counts zero as a power of two.
287 */
288 static INLINE boolean
289 util_is_power_of_two( unsigned v )
290 {
291 return (v & (v-1)) == 0;
292 }
293
294
295 /**
296 * Floor(x), returned as int.
297 */
298 static INLINE int
299 util_ifloor(float f)
300 {
301 int ai, bi;
302 double af, bf;
303 union fi u;
304 af = (3 << 22) + 0.5 + (double) f;
305 bf = (3 << 22) + 0.5 - (double) f;
306 u.f = (float) af; ai = u.i;
307 u.f = (float) bf; bi = u.i;
308 return (ai - bi) >> 1;
309 }
310
311
312 /**
313 * Round float to nearest int.
314 */
315 static INLINE int
316 util_iround(float f)
317 {
318 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
319 int r;
320 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
321 return r;
322 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
323 int r;
324 _asm {
325 fld f
326 fistp r
327 }
328 return r;
329 #else
330 if (f >= 0.0f)
331 return (int) (f + 0.5f);
332 else
333 return (int) (f - 0.5f);
334 #endif
335 }
336
337
338
339 /**
340 * Test if x is NaN or +/- infinity.
341 */
342 static INLINE boolean
343 util_is_inf_or_nan(float x)
344 {
345 union fi tmp;
346 tmp.f = x;
347 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
348 }
349
350
351 /**
352 * Test whether x is a power of two.
353 */
354 static INLINE boolean
355 util_is_pot(unsigned x)
356 {
357 return (x & (x - 1)) == 0;
358 }
359
360
361 /**
362 * Find first bit set in word. Least significant bit is 1.
363 * Return 0 if no bits set.
364 */
365 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
366 unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
367 #pragma intrinsic(_BitScanForward)
368 static INLINE
369 unsigned long ffs( unsigned long u )
370 {
371 unsigned long i;
372 if (_BitScanForward(&i, u))
373 return i + 1;
374 else
375 return 0;
376 }
377 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
378 static INLINE
379 unsigned ffs( unsigned u )
380 {
381 unsigned i;
382
383 if (u == 0) {
384 return 0;
385 }
386
387 __asm bsf eax, [u]
388 __asm inc eax
389 __asm mov [i], eax
390
391 return i;
392 }
393 #elif defined(__MINGW32__)
394 #define ffs __builtin_ffs
395 #endif
396
397 #ifdef __MINGW32__
398 #define ffs __builtin_ffs
399 #endif
400
401
402 /* Could also binary search for the highest bit.
403 */
404 static INLINE unsigned
405 util_unsigned_logbase2(unsigned n)
406 {
407 unsigned log2 = 0;
408 while (n >>= 1)
409 ++log2;
410 return log2;
411 }
412
413
414 /**
415 * Return float bits.
416 */
417 static INLINE unsigned
418 fui( float f )
419 {
420 union fi fi;
421 fi.f = f;
422 return fi.ui;
423 }
424
425
426 /**
427 * Convert ubyte to float in [0, 1].
428 * XXX a 256-entry lookup table would be slightly faster.
429 */
430 static INLINE float
431 ubyte_to_float(ubyte ub)
432 {
433 return (float) ub * (1.0f / 255.0f);
434 }
435
436
437 /**
438 * Convert float in [0,1] to ubyte in [0,255] with clamping.
439 */
440 static INLINE ubyte
441 float_to_ubyte(float f)
442 {
443 const int ieee_0996 = 0x3f7f0000; /* 0.996 or so */
444 union fi tmp;
445
446 tmp.f = f;
447 if (tmp.i < 0) {
448 return (ubyte) 0;
449 }
450 else if (tmp.i >= ieee_0996) {
451 return (ubyte) 255;
452 }
453 else {
454 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
455 return (ubyte) tmp.i;
456 }
457 }
458
459
460 /**
461 * Calc log base 2
462 */
463 static INLINE unsigned
464 util_logbase2(unsigned n)
465 {
466 unsigned log2 = 0;
467 while (n >>= 1)
468 ++log2;
469 return log2;
470 }
471
472
473 /**
474 * Returns the smallest power of two >= x
475 */
476 static INLINE unsigned
477 util_next_power_of_two(unsigned x)
478 {
479 unsigned i;
480
481 if (x == 0)
482 return 1;
483
484 --x;
485
486 for (i = 1; i < sizeof(unsigned) * 8; i <<= 1)
487 x |= x >> i;
488
489 return x + 1;
490 }
491
492
493 /**
494 * Clamp X to [MIN, MAX].
495 * This is a macro to allow float, int, uint, etc. types.
496 */
497 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
498
499 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
500 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
501
502
503 static INLINE int
504 align(int value, int alignment)
505 {
506 return (value + alignment - 1) & ~(alignment - 1);
507 }
508
509 static INLINE unsigned
510 minify(unsigned value)
511 {
512 return MAX2(1, value >> 1);
513 }
514
515 #ifndef COPY_4V
516 #define COPY_4V( DST, SRC ) \
517 do { \
518 (DST)[0] = (SRC)[0]; \
519 (DST)[1] = (SRC)[1]; \
520 (DST)[2] = (SRC)[2]; \
521 (DST)[3] = (SRC)[3]; \
522 } while (0)
523 #endif
524
525
526 #ifndef COPY_4FV
527 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
528 #endif
529
530
531 #ifndef ASSIGN_4V
532 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
533 do { \
534 (DST)[0] = (V0); \
535 (DST)[1] = (V1); \
536 (DST)[2] = (V2); \
537 (DST)[3] = (V3); \
538 } while (0)
539 #endif
540
541
542 #ifdef __cplusplus
543 }
544 #endif
545
546 #endif /* U_MATH_H */