Merge remote branch 'origin/master' into nv50-compiler
[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
172 #define isfinite(x) _finite((double)(x))
173 #define isnan(x) _isnan((double)(x))
174 #endif
175
176 static INLINE double log2( double x )
177 {
178 const double invln2 = 1.442695041;
179 return log( x ) * invln2;
180 }
181
182 #endif /* _MSC_VER */
183
184
185
186
187
188 #define POW2_TABLE_SIZE_LOG2 9
189 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
190 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
191 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
192 extern float pow2_table[POW2_TABLE_SIZE];
193
194
195 /**
196 * Initialize math module. This should be called before using any
197 * other functions in this module.
198 */
199 extern void
200 util_init_math(void);
201
202
203 union fi {
204 float f;
205 int32_t i;
206 uint32_t ui;
207 };
208
209
210 /**
211 * Fast version of 2^x
212 * Identity: exp2(a + b) = exp2(a) * exp2(b)
213 * Let ipart = int(x)
214 * Let fpart = x - ipart;
215 * So, exp2(x) = exp2(ipart) * exp2(fpart)
216 * Compute exp2(ipart) with i << ipart
217 * Compute exp2(fpart) with lookup table.
218 */
219 static INLINE float
220 util_fast_exp2(float x)
221 {
222 int32_t ipart;
223 float fpart, mpart;
224 union fi epart;
225
226 if(x > 129.00000f)
227 return 3.402823466e+38f;
228
229 if (x < -126.99999f)
230 return 0.0f;
231
232 ipart = (int32_t) x;
233 fpart = x - (float) ipart;
234
235 /* same as
236 * epart.f = (float) (1 << ipart)
237 * but faster and without integer overflow for ipart > 31
238 */
239 epart.i = (ipart + 127 ) << 23;
240
241 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
242
243 return epart.f * mpart;
244 }
245
246
247 /**
248 * Fast approximation to exp(x).
249 */
250 static INLINE float
251 util_fast_exp(float x)
252 {
253 const float k = 1.44269f; /* = log2(e) */
254 return util_fast_exp2(k * x);
255 }
256
257
258 #define LOG2_TABLE_SIZE_LOG2 16
259 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
260 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
261 extern float log2_table[LOG2_TABLE_SIZE];
262
263
264 /**
265 * Fast approximation to log2(x).
266 */
267 static INLINE float
268 util_fast_log2(float x)
269 {
270 union fi num;
271 float epart, mpart;
272 num.f = x;
273 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
274 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
275 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
276 return epart + mpart;
277 }
278
279
280 /**
281 * Fast approximation to x^y.
282 */
283 static INLINE float
284 util_fast_pow(float x, float y)
285 {
286 return util_fast_exp2(util_fast_log2(x) * y);
287 }
288
289 /* Note that this counts zero as a power of two.
290 */
291 static INLINE boolean
292 util_is_power_of_two( unsigned v )
293 {
294 return (v & (v-1)) == 0;
295 }
296
297
298 /**
299 * Floor(x), returned as int.
300 */
301 static INLINE int
302 util_ifloor(float f)
303 {
304 int ai, bi;
305 double af, bf;
306 union fi u;
307 af = (3 << 22) + 0.5 + (double) f;
308 bf = (3 << 22) + 0.5 - (double) f;
309 u.f = (float) af; ai = u.i;
310 u.f = (float) bf; bi = u.i;
311 return (ai - bi) >> 1;
312 }
313
314
315 /**
316 * Round float to nearest int.
317 */
318 static INLINE int
319 util_iround(float f)
320 {
321 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
322 int r;
323 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
324 return r;
325 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
326 int r;
327 _asm {
328 fld f
329 fistp r
330 }
331 return r;
332 #else
333 if (f >= 0.0f)
334 return (int) (f + 0.5f);
335 else
336 return (int) (f - 0.5f);
337 #endif
338 }
339
340
341 /**
342 * Approximate floating point comparison
343 */
344 static INLINE boolean
345 util_is_approx(float a, float b, float tol)
346 {
347 return fabs(b - a) <= tol;
348 }
349
350
351 /**
352 * Test if x is NaN or +/- infinity.
353 */
354 static INLINE boolean
355 util_is_inf_or_nan(float x)
356 {
357 union fi tmp;
358 tmp.f = x;
359 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
360 }
361
362
363 /**
364 * Find first bit set in word. Least significant bit is 1.
365 * Return 0 if no bits set.
366 */
367 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
368 unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
369 #pragma intrinsic(_BitScanForward)
370 static INLINE
371 unsigned long ffs( unsigned long u )
372 {
373 unsigned long i;
374 if (_BitScanForward(&i, u))
375 return i + 1;
376 else
377 return 0;
378 }
379 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
380 static INLINE
381 unsigned ffs( unsigned u )
382 {
383 unsigned i;
384
385 if (u == 0) {
386 return 0;
387 }
388
389 __asm bsf eax, [u]
390 __asm inc eax
391 __asm mov [i], eax
392
393 return i;
394 }
395 #elif defined(__MINGW32__)
396 #define ffs __builtin_ffs
397 #endif
398
399 #ifdef __MINGW32__
400 #define ffs __builtin_ffs
401 #endif
402
403
404 /* Could also binary search for the highest bit.
405 */
406 static INLINE unsigned
407 util_unsigned_logbase2(unsigned n)
408 {
409 unsigned log2 = 0;
410 while (n >>= 1)
411 ++log2;
412 return log2;
413 }
414
415
416 /**
417 * Return float bits.
418 */
419 static INLINE unsigned
420 fui( float f )
421 {
422 union fi fi;
423 fi.f = f;
424 return fi.ui;
425 }
426
427
428 /**
429 * Convert ubyte to float in [0, 1].
430 * XXX a 256-entry lookup table would be slightly faster.
431 */
432 static INLINE float
433 ubyte_to_float(ubyte ub)
434 {
435 return (float) ub * (1.0f / 255.0f);
436 }
437
438
439 /**
440 * Convert float in [0,1] to ubyte in [0,255] with clamping.
441 */
442 static INLINE ubyte
443 float_to_ubyte(float f)
444 {
445 const int ieee_0996 = 0x3f7f0000; /* 0.996 or so */
446 union fi tmp;
447
448 tmp.f = f;
449 if (tmp.i < 0) {
450 return (ubyte) 0;
451 }
452 else if (tmp.i >= ieee_0996) {
453 return (ubyte) 255;
454 }
455 else {
456 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
457 return (ubyte) tmp.i;
458 }
459 }
460
461
462 /**
463 * Calc log base 2
464 */
465 static INLINE unsigned
466 util_logbase2(unsigned n)
467 {
468 unsigned log2 = 0;
469 while (n >>= 1)
470 ++log2;
471 return log2;
472 }
473
474
475 /**
476 * Returns the smallest power of two >= x
477 */
478 static INLINE unsigned
479 util_next_power_of_two(unsigned x)
480 {
481 unsigned i;
482
483 if (x == 0)
484 return 1;
485
486 --x;
487
488 for (i = 1; i < sizeof(unsigned) * 8; i <<= 1)
489 x |= x >> i;
490
491 return x + 1;
492 }
493
494
495 /**
496 * Return number of bits set in n.
497 */
498 static INLINE unsigned
499 util_bitcount(unsigned n)
500 {
501 #if defined(PIPE_CC_GCC)
502 return __builtin_popcount(n);
503 #else
504 /* K&R classic bitcount.
505 *
506 * For each iteration, clear the LSB from the bitfield.
507 * Requires only one iteration per set bit, instead of
508 * one iteration per bit less than highest set bit.
509 */
510 unsigned bits = 0;
511 for (bits; n; bits++) {
512 n &= n - 1;
513 }
514 return bits;
515 #endif
516 }
517
518
519 /**
520 * Reverse byte order of a 32 bit word.
521 */
522 static INLINE uint32_t
523 util_bswap32(uint32_t n)
524 {
525 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403)
526 return __builtin_bswap32(n);
527 #else
528 return (n >> 24) |
529 ((n >> 8) & 0x0000ff00) |
530 ((n << 8) & 0x00ff0000) |
531 (n << 24);
532 #endif
533 }
534
535
536 /**
537 * Reverse byte order of a 16 bit word.
538 */
539 static INLINE uint16_t
540 util_bswap16(uint16_t n)
541 {
542 return (n >> 8) |
543 (n << 8);
544 }
545
546
547 /**
548 * Clamp X to [MIN, MAX].
549 * This is a macro to allow float, int, uint, etc. types.
550 */
551 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
552
553 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
554 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
555
556 #define MIN3( A, B, C ) MIN2( MIN2( A, B ), C )
557 #define MAX3( A, B, C ) MAX2( MAX2( A, B ), C )
558
559 #define MIN4( A, B, C, D ) MIN2( MIN2( A, B ), MIN2(C, D) )
560 #define MAX4( A, B, C, D ) MAX2( MAX2( A, B ), MAX2(C, D) )
561
562
563 /**
564 * Align a value, only works pot alignemnts.
565 */
566 static INLINE int
567 align(int value, int alignment)
568 {
569 return (value + alignment - 1) & ~(alignment - 1);
570 }
571
572 /**
573 * Works like align but on npot alignments.
574 */
575 static INLINE size_t
576 util_align_npot(size_t value, size_t alignment)
577 {
578 if (value % alignment)
579 return value + (alignment - (value % alignment));
580 return value;
581 }
582
583 static INLINE unsigned
584 u_minify(unsigned value, unsigned levels)
585 {
586 return MAX2(1, value >> levels);
587 }
588
589 #ifndef COPY_4V
590 #define COPY_4V( DST, SRC ) \
591 do { \
592 (DST)[0] = (SRC)[0]; \
593 (DST)[1] = (SRC)[1]; \
594 (DST)[2] = (SRC)[2]; \
595 (DST)[3] = (SRC)[3]; \
596 } while (0)
597 #endif
598
599
600 #ifndef COPY_4FV
601 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
602 #endif
603
604
605 #ifndef ASSIGN_4V
606 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
607 do { \
608 (DST)[0] = (V0); \
609 (DST)[1] = (V1); \
610 (DST)[2] = (V2); \
611 (DST)[3] = (V3); \
612 } while (0)
613 #endif
614
615
616 static INLINE uint32_t util_unsigned_fixed(float value, unsigned frac_bits)
617 {
618 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
619 }
620
621 static INLINE int32_t util_signed_fixed(float value, unsigned frac_bits)
622 {
623 return (int32_t)(value * (1<<frac_bits));
624 }
625
626
627
628 #ifdef __cplusplus
629 }
630 #endif
631
632 #endif /* U_MATH_H */