551463f75cd1d7db3df73a2accb2b2515e7fb6ee
[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 #ifndef M_SQRT2
122 #define M_SQRT2 1.41421356237309504880
123 #endif
124
125
126 #if defined(_MSC_VER)
127
128 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
129
130 static INLINE float cosf( float f )
131 {
132 return (float) cos( (double) f );
133 }
134
135 static INLINE float sinf( float f )
136 {
137 return (float) sin( (double) f );
138 }
139
140 static INLINE float ceilf( float f )
141 {
142 return (float) ceil( (double) f );
143 }
144
145 static INLINE float floorf( float f )
146 {
147 return (float) floor( (double) f );
148 }
149
150 static INLINE float powf( float f, float g )
151 {
152 return (float) pow( (double) f, (double) g );
153 }
154
155 static INLINE float sqrtf( float f )
156 {
157 return (float) sqrt( (double) f );
158 }
159
160 static INLINE float fabsf( float f )
161 {
162 return (float) fabs( (double) f );
163 }
164
165 static INLINE float logf( float f )
166 {
167 return (float) log( (double) f );
168 }
169
170 #else
171 /* Work-around an extra semi-colon in VS 2005 logf definition */
172 #ifdef logf
173 #undef logf
174 #define logf(x) ((float)log((double)(x)))
175 #endif /* logf */
176
177 #define isfinite(x) _finite((double)(x))
178 #define isnan(x) _isnan((double)(x))
179 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
180
181 static INLINE double log2( double x )
182 {
183 const double invln2 = 1.442695041;
184 return log( x ) * invln2;
185 }
186
187 static INLINE double
188 round(double x)
189 {
190 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
191 }
192
193 static INLINE float
194 roundf(float x)
195 {
196 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
197 }
198
199 #endif /* _MSC_VER */
200
201
202 #ifdef PIPE_OS_ANDROID
203
204 static INLINE
205 double log2(double d)
206 {
207 return log(d) * (1.0 / M_LN2);
208 }
209
210 /* workaround a conflict with main/imports.h */
211 #ifdef log2f
212 #undef log2f
213 #endif
214
215 static INLINE
216 float log2f(float f)
217 {
218 return logf(f) * (float) (1.0 / M_LN2);
219 }
220
221 #endif
222
223
224
225
226 #define POW2_TABLE_SIZE_LOG2 9
227 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
228 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
229 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
230 extern float pow2_table[POW2_TABLE_SIZE];
231
232
233 /**
234 * Initialize math module. This should be called before using any
235 * other functions in this module.
236 */
237 extern void
238 util_init_math(void);
239
240
241 union fi {
242 float f;
243 int32_t i;
244 uint32_t ui;
245 };
246
247
248 /**
249 * Fast version of 2^x
250 * Identity: exp2(a + b) = exp2(a) * exp2(b)
251 * Let ipart = int(x)
252 * Let fpart = x - ipart;
253 * So, exp2(x) = exp2(ipart) * exp2(fpart)
254 * Compute exp2(ipart) with i << ipart
255 * Compute exp2(fpart) with lookup table.
256 */
257 static INLINE float
258 util_fast_exp2(float x)
259 {
260 int32_t ipart;
261 float fpart, mpart;
262 union fi epart;
263
264 if(x > 129.00000f)
265 return 3.402823466e+38f;
266
267 if (x < -126.99999f)
268 return 0.0f;
269
270 ipart = (int32_t) x;
271 fpart = x - (float) ipart;
272
273 /* same as
274 * epart.f = (float) (1 << ipart)
275 * but faster and without integer overflow for ipart > 31
276 */
277 epart.i = (ipart + 127 ) << 23;
278
279 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
280
281 return epart.f * mpart;
282 }
283
284
285 /**
286 * Fast approximation to exp(x).
287 */
288 static INLINE float
289 util_fast_exp(float x)
290 {
291 const float k = 1.44269f; /* = log2(e) */
292 return util_fast_exp2(k * x);
293 }
294
295
296 #define LOG2_TABLE_SIZE_LOG2 16
297 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
298 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
299 extern float log2_table[LOG2_TABLE_SIZE];
300
301
302 /**
303 * Fast approximation to log2(x).
304 */
305 static INLINE float
306 util_fast_log2(float x)
307 {
308 union fi num;
309 float epart, mpart;
310 num.f = x;
311 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
312 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
313 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
314 return epart + mpart;
315 }
316
317
318 /**
319 * Fast approximation to x^y.
320 */
321 static INLINE float
322 util_fast_pow(float x, float y)
323 {
324 return util_fast_exp2(util_fast_log2(x) * y);
325 }
326
327 /* Note that this counts zero as a power of two.
328 */
329 static INLINE boolean
330 util_is_power_of_two( unsigned v )
331 {
332 return (v & (v-1)) == 0;
333 }
334
335
336 /**
337 * Floor(x), returned as int.
338 */
339 static INLINE int
340 util_ifloor(float f)
341 {
342 int ai, bi;
343 double af, bf;
344 union fi u;
345 af = (3 << 22) + 0.5 + (double) f;
346 bf = (3 << 22) + 0.5 - (double) f;
347 u.f = (float) af; ai = u.i;
348 u.f = (float) bf; bi = u.i;
349 return (ai - bi) >> 1;
350 }
351
352
353 /**
354 * Round float to nearest int.
355 */
356 static INLINE int
357 util_iround(float f)
358 {
359 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
360 int r;
361 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
362 return r;
363 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
364 int r;
365 _asm {
366 fld f
367 fistp r
368 }
369 return r;
370 #else
371 if (f >= 0.0f)
372 return (int) (f + 0.5f);
373 else
374 return (int) (f - 0.5f);
375 #endif
376 }
377
378
379 /**
380 * Approximate floating point comparison
381 */
382 static INLINE boolean
383 util_is_approx(float a, float b, float tol)
384 {
385 return fabs(b - a) <= tol;
386 }
387
388
389 /**
390 * Test if x is NaN or +/- infinity.
391 */
392 static INLINE boolean
393 util_is_inf_or_nan(float x)
394 {
395 union fi tmp;
396 tmp.f = x;
397 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
398 }
399
400
401 /**
402 * Find first bit set in word. Least significant bit is 1.
403 * Return 0 if no bits set.
404 */
405 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
406 unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
407 #pragma intrinsic(_BitScanForward)
408 static INLINE
409 unsigned long ffs( unsigned long u )
410 {
411 unsigned long i;
412 if (_BitScanForward(&i, u))
413 return i + 1;
414 else
415 return 0;
416 }
417 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
418 static INLINE
419 unsigned ffs( unsigned u )
420 {
421 unsigned i;
422
423 if (u == 0) {
424 return 0;
425 }
426
427 __asm bsf eax, [u]
428 __asm inc eax
429 __asm mov [i], eax
430
431 return i;
432 }
433 #elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID)
434 #define ffs __builtin_ffs
435 #endif
436
437
438 /* Destructively loop over all of the bits in a mask as in:
439 *
440 * while (mymask) {
441 * int i = u_bit_scan(&mymask);
442 * ... process element i
443 * }
444 *
445 */
446 static INLINE int u_bit_scan(unsigned *mask)
447 {
448 int i = ffs(*mask) - 1;
449 *mask &= ~(1 << i);
450 return i;
451 }
452
453
454 /**
455 * Return float bits.
456 */
457 static INLINE unsigned
458 fui( float f )
459 {
460 union fi fi;
461 fi.f = f;
462 return fi.ui;
463 }
464
465
466 /**
467 * Convert ubyte to float in [0, 1].
468 * XXX a 256-entry lookup table would be slightly faster.
469 */
470 static INLINE float
471 ubyte_to_float(ubyte ub)
472 {
473 return (float) ub * (1.0f / 255.0f);
474 }
475
476
477 /**
478 * Convert float in [0,1] to ubyte in [0,255] with clamping.
479 */
480 static INLINE ubyte
481 float_to_ubyte(float f)
482 {
483 const int ieee_0996 = 0x3f7f0000; /* 0.996 or so */
484 union fi tmp;
485
486 tmp.f = f;
487 if (tmp.i < 0) {
488 return (ubyte) 0;
489 }
490 else if (tmp.i >= ieee_0996) {
491 return (ubyte) 255;
492 }
493 else {
494 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
495 return (ubyte) tmp.i;
496 }
497 }
498
499 static INLINE float
500 byte_to_float_tex(int8_t b)
501 {
502 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
503 }
504
505 static INLINE int8_t
506 float_to_byte_tex(float f)
507 {
508 return (int8_t) (127.0F * f);
509 }
510
511 /**
512 * Calc log base 2
513 */
514 static INLINE unsigned
515 util_logbase2(unsigned n)
516 {
517 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
518 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
519 #else
520 unsigned pos = 0;
521 if (n >= 1<<16) { n >>= 16; pos += 16; }
522 if (n >= 1<< 8) { n >>= 8; pos += 8; }
523 if (n >= 1<< 4) { n >>= 4; pos += 4; }
524 if (n >= 1<< 2) { n >>= 2; pos += 2; }
525 if (n >= 1<< 1) { pos += 1; }
526 return pos;
527 #endif
528 }
529
530
531 /**
532 * Returns the smallest power of two >= x
533 */
534 static INLINE unsigned
535 util_next_power_of_two(unsigned x)
536 {
537 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
538 if (x <= 1)
539 return 1;
540
541 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
542 #else
543 unsigned val = x;
544
545 if (x <= 1)
546 return 1;
547
548 if (util_is_power_of_two(x))
549 return x;
550
551 val--;
552 val = (val >> 1) | val;
553 val = (val >> 2) | val;
554 val = (val >> 4) | val;
555 val = (val >> 8) | val;
556 val = (val >> 16) | val;
557 val++;
558 return val;
559 #endif
560 }
561
562
563 /**
564 * Return number of bits set in n.
565 */
566 static INLINE unsigned
567 util_bitcount(unsigned n)
568 {
569 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
570 return __builtin_popcount(n);
571 #else
572 /* K&R classic bitcount.
573 *
574 * For each iteration, clear the LSB from the bitfield.
575 * Requires only one iteration per set bit, instead of
576 * one iteration per bit less than highest set bit.
577 */
578 unsigned bits = 0;
579 for (bits; n; bits++) {
580 n &= n - 1;
581 }
582 return bits;
583 #endif
584 }
585
586
587 /**
588 * Convert from little endian to CPU byte order.
589 */
590
591 #ifdef PIPE_ARCH_BIG_ENDIAN
592 #define util_le32_to_cpu(x) util_bswap32(x)
593 #define util_le16_to_cpu(x) util_bswap16(x)
594 #else
595 #define util_le32_to_cpu(x) (x)
596 #define util_le16_to_cpu(x) (x)
597 #endif
598
599
600 /**
601 * Reverse byte order of a 32 bit word.
602 */
603 static INLINE uint32_t
604 util_bswap32(uint32_t n)
605 {
606 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403)
607 return __builtin_bswap32(n);
608 #else
609 return (n >> 24) |
610 ((n >> 8) & 0x0000ff00) |
611 ((n << 8) & 0x00ff0000) |
612 (n << 24);
613 #endif
614 }
615
616
617 /**
618 * Reverse byte order of a 16 bit word.
619 */
620 static INLINE uint16_t
621 util_bswap16(uint16_t n)
622 {
623 return (n >> 8) |
624 (n << 8);
625 }
626
627
628 /**
629 * Clamp X to [MIN, MAX].
630 * This is a macro to allow float, int, uint, etc. types.
631 */
632 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
633
634 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
635 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
636
637 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
638 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
639
640 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
641 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
642
643
644 /**
645 * Align a value, only works pot alignemnts.
646 */
647 static INLINE int
648 align(int value, int alignment)
649 {
650 return (value + alignment - 1) & ~(alignment - 1);
651 }
652
653 /**
654 * Works like align but on npot alignments.
655 */
656 static INLINE size_t
657 util_align_npot(size_t value, size_t alignment)
658 {
659 if (value % alignment)
660 return value + (alignment - (value % alignment));
661 return value;
662 }
663
664 static INLINE unsigned
665 u_minify(unsigned value, unsigned levels)
666 {
667 return MAX2(1, value >> levels);
668 }
669
670 #ifndef COPY_4V
671 #define COPY_4V( DST, SRC ) \
672 do { \
673 (DST)[0] = (SRC)[0]; \
674 (DST)[1] = (SRC)[1]; \
675 (DST)[2] = (SRC)[2]; \
676 (DST)[3] = (SRC)[3]; \
677 } while (0)
678 #endif
679
680
681 #ifndef COPY_4FV
682 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
683 #endif
684
685
686 #ifndef ASSIGN_4V
687 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
688 do { \
689 (DST)[0] = (V0); \
690 (DST)[1] = (V1); \
691 (DST)[2] = (V2); \
692 (DST)[3] = (V3); \
693 } while (0)
694 #endif
695
696
697 static INLINE uint32_t util_unsigned_fixed(float value, unsigned frac_bits)
698 {
699 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
700 }
701
702 static INLINE int32_t util_signed_fixed(float value, unsigned frac_bits)
703 {
704 return (int32_t)(value * (1<<frac_bits));
705 }
706
707
708
709 #ifdef __cplusplus
710 }
711 #endif
712
713 #endif /* U_MATH_H */