s/Tungsten Graphics/VMware/
[mesa.git] / src / gallium / auxiliary / util / u_math.h
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49
50 #include <math.h>
51 #include <float.h>
52 #include <stdarg.h>
53
54 #ifdef PIPE_OS_UNIX
55 #include <strings.h> /* for ffs */
56 #endif
57
58
59 #ifndef M_SQRT2
60 #define M_SQRT2 1.41421356237309504880
61 #endif
62
63
64 #if defined(_MSC_VER)
65
66 #if _MSC_VER < 1400 && !defined(__cplusplus)
67
68 static INLINE float cosf( float f )
69 {
70 return (float) cos( (double) f );
71 }
72
73 static INLINE float sinf( float f )
74 {
75 return (float) sin( (double) f );
76 }
77
78 static INLINE float ceilf( float f )
79 {
80 return (float) ceil( (double) f );
81 }
82
83 static INLINE float floorf( float f )
84 {
85 return (float) floor( (double) f );
86 }
87
88 static INLINE float powf( float f, float g )
89 {
90 return (float) pow( (double) f, (double) g );
91 }
92
93 static INLINE float sqrtf( float f )
94 {
95 return (float) sqrt( (double) f );
96 }
97
98 static INLINE float fabsf( float f )
99 {
100 return (float) fabs( (double) f );
101 }
102
103 static INLINE float logf( float f )
104 {
105 return (float) log( (double) f );
106 }
107
108 #else
109 /* Work-around an extra semi-colon in VS 2005 logf definition */
110 #ifdef logf
111 #undef logf
112 #define logf(x) ((float)log((double)(x)))
113 #endif /* logf */
114
115 #define isfinite(x) _finite((double)(x))
116 #define isnan(x) _isnan((double)(x))
117 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
118
119 static INLINE double log2( double x )
120 {
121 const double invln2 = 1.442695041;
122 return log( x ) * invln2;
123 }
124
125 static INLINE double
126 round(double x)
127 {
128 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
129 }
130
131 static INLINE float
132 roundf(float x)
133 {
134 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
135 }
136
137 #define INFINITY (DBL_MAX + DBL_MAX)
138 #define NAN (INFINITY - INFINITY)
139
140 #endif /* _MSC_VER */
141
142
143 #ifdef PIPE_OS_ANDROID
144
145 static INLINE
146 double log2(double d)
147 {
148 return log(d) * (1.0 / M_LN2);
149 }
150
151 /* workaround a conflict with main/imports.h */
152 #ifdef log2f
153 #undef log2f
154 #endif
155
156 static INLINE
157 float log2f(float f)
158 {
159 return logf(f) * (float) (1.0 / M_LN2);
160 }
161
162 #endif
163
164
165 #if __STDC_VERSION__ < 199901L && (!defined(__cplusplus) || defined(_MSC_VER))
166 static INLINE long int
167 lrint(double d)
168 {
169 long int rounded = (long int)(d + 0.5);
170
171 if (d - floor(d) == 0.5) {
172 if (rounded % 2 != 0)
173 rounded += (d > 0) ? -1 : 1;
174 }
175
176 return rounded;
177 }
178
179 static INLINE long int
180 lrintf(float f)
181 {
182 long int rounded = (long int)(f + 0.5f);
183
184 if (f - floorf(f) == 0.5f) {
185 if (rounded % 2 != 0)
186 rounded += (f > 0) ? -1 : 1;
187 }
188
189 return rounded;
190 }
191
192 static INLINE long long int
193 llrint(double d)
194 {
195 long long int rounded = (long long int)(d + 0.5);
196
197 if (d - floor(d) == 0.5) {
198 if (rounded % 2 != 0)
199 rounded += (d > 0) ? -1 : 1;
200 }
201
202 return rounded;
203 }
204
205 static INLINE long long int
206 llrintf(float f)
207 {
208 long long int rounded = (long long int)(f + 0.5f);
209
210 if (f - floorf(f) == 0.5f) {
211 if (rounded % 2 != 0)
212 rounded += (f > 0) ? -1 : 1;
213 }
214
215 return rounded;
216 }
217 #endif /* C99 */
218
219 #define POW2_TABLE_SIZE_LOG2 9
220 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
221 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
222 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
223 extern float pow2_table[POW2_TABLE_SIZE];
224
225
226 /**
227 * Initialize math module. This should be called before using any
228 * other functions in this module.
229 */
230 extern void
231 util_init_math(void);
232
233
234 union fi {
235 float f;
236 int32_t i;
237 uint32_t ui;
238 };
239
240
241 union di {
242 double d;
243 int64_t i;
244 uint64_t ui;
245 };
246
247
248 /**
249 * Extract the IEEE float32 exponent.
250 */
251 static INLINE signed
252 util_get_float32_exponent(float x) {
253 union fi f;
254
255 f.f = x;
256
257 return ((f.ui >> 23) & 0xff) - 127;
258 }
259
260
261 /**
262 * Fast version of 2^x
263 * Identity: exp2(a + b) = exp2(a) * exp2(b)
264 * Let ipart = int(x)
265 * Let fpart = x - ipart;
266 * So, exp2(x) = exp2(ipart) * exp2(fpart)
267 * Compute exp2(ipart) with i << ipart
268 * Compute exp2(fpart) with lookup table.
269 */
270 static INLINE float
271 util_fast_exp2(float x)
272 {
273 int32_t ipart;
274 float fpart, mpart;
275 union fi epart;
276
277 if(x > 129.00000f)
278 return 3.402823466e+38f;
279
280 if (x < -126.99999f)
281 return 0.0f;
282
283 ipart = (int32_t) x;
284 fpart = x - (float) ipart;
285
286 /* same as
287 * epart.f = (float) (1 << ipart)
288 * but faster and without integer overflow for ipart > 31
289 */
290 epart.i = (ipart + 127 ) << 23;
291
292 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
293
294 return epart.f * mpart;
295 }
296
297
298 /**
299 * Fast approximation to exp(x).
300 */
301 static INLINE float
302 util_fast_exp(float x)
303 {
304 const float k = 1.44269f; /* = log2(e) */
305 return util_fast_exp2(k * x);
306 }
307
308
309 #define LOG2_TABLE_SIZE_LOG2 16
310 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
311 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
312 extern float log2_table[LOG2_TABLE_SIZE];
313
314
315 /**
316 * Fast approximation to log2(x).
317 */
318 static INLINE float
319 util_fast_log2(float x)
320 {
321 union fi num;
322 float epart, mpart;
323 num.f = x;
324 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
325 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
326 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
327 return epart + mpart;
328 }
329
330
331 /**
332 * Fast approximation to x^y.
333 */
334 static INLINE float
335 util_fast_pow(float x, float y)
336 {
337 return util_fast_exp2(util_fast_log2(x) * y);
338 }
339
340 /* Note that this counts zero as a power of two.
341 */
342 static INLINE boolean
343 util_is_power_of_two( unsigned v )
344 {
345 return (v & (v-1)) == 0;
346 }
347
348
349 /**
350 * Floor(x), returned as int.
351 */
352 static INLINE int
353 util_ifloor(float f)
354 {
355 int ai, bi;
356 double af, bf;
357 union fi u;
358 af = (3 << 22) + 0.5 + (double) f;
359 bf = (3 << 22) + 0.5 - (double) f;
360 u.f = (float) af; ai = u.i;
361 u.f = (float) bf; bi = u.i;
362 return (ai - bi) >> 1;
363 }
364
365
366 /**
367 * Round float to nearest int.
368 */
369 static INLINE int
370 util_iround(float f)
371 {
372 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
373 int r;
374 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
375 return r;
376 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
377 int r;
378 _asm {
379 fld f
380 fistp r
381 }
382 return r;
383 #else
384 if (f >= 0.0f)
385 return (int) (f + 0.5f);
386 else
387 return (int) (f - 0.5f);
388 #endif
389 }
390
391
392 /**
393 * Approximate floating point comparison
394 */
395 static INLINE boolean
396 util_is_approx(float a, float b, float tol)
397 {
398 return fabs(b - a) <= tol;
399 }
400
401
402 /**
403 * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
404 * util_is_X_nan = test if x is NaN
405 * util_X_inf_sign = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
406 *
407 * NaN can be checked with x != x, however this fails with the fast math flag
408 **/
409
410
411 /**
412 * Single-float
413 */
414 static INLINE boolean
415 util_is_inf_or_nan(float x)
416 {
417 union fi tmp;
418 tmp.f = x;
419 return (tmp.ui & 0x7f800000) == 0x7f800000;
420 }
421
422
423 static INLINE boolean
424 util_is_nan(float x)
425 {
426 union fi tmp;
427 tmp.f = x;
428 return (tmp.ui & 0x7fffffff) > 0x7f800000;
429 }
430
431
432 static INLINE int
433 util_inf_sign(float x)
434 {
435 union fi tmp;
436 tmp.f = x;
437 if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
438 return 0;
439 }
440
441 return (x < 0) ? -1 : 1;
442 }
443
444
445 /**
446 * Double-float
447 */
448 static INLINE boolean
449 util_is_double_inf_or_nan(double x)
450 {
451 union di tmp;
452 tmp.d = x;
453 return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
454 }
455
456
457 static INLINE boolean
458 util_is_double_nan(double x)
459 {
460 union di tmp;
461 tmp.d = x;
462 return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
463 }
464
465
466 static INLINE int
467 util_double_inf_sign(double x)
468 {
469 union di tmp;
470 tmp.d = x;
471 if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
472 return 0;
473 }
474
475 return (x < 0) ? -1 : 1;
476 }
477
478
479 /**
480 * Half-float
481 */
482 static INLINE boolean
483 util_is_half_inf_or_nan(int16_t x)
484 {
485 return (x & 0x7c00) == 0x7c00;
486 }
487
488
489 static INLINE boolean
490 util_is_half_nan(int16_t x)
491 {
492 return (x & 0x7fff) > 0x7c00;
493 }
494
495
496 static INLINE int
497 util_half_inf_sign(int16_t x)
498 {
499 if ((x & 0x7fff) != 0x7c00) {
500 return 0;
501 }
502
503 return (x < 0) ? -1 : 1;
504 }
505
506
507 /**
508 * Find first bit set in word. Least significant bit is 1.
509 * Return 0 if no bits set.
510 */
511 #ifndef FFS_DEFINED
512 #define FFS_DEFINED 1
513
514 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
515 unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
516 #pragma intrinsic(_BitScanForward)
517 static INLINE
518 unsigned long ffs( unsigned long u )
519 {
520 unsigned long i;
521 if (_BitScanForward(&i, u))
522 return i + 1;
523 else
524 return 0;
525 }
526 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
527 static INLINE
528 unsigned ffs( unsigned u )
529 {
530 unsigned i;
531
532 if (u == 0) {
533 return 0;
534 }
535
536 __asm bsf eax, [u]
537 __asm inc eax
538 __asm mov [i], eax
539
540 return i;
541 }
542 #elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID)
543 #define ffs __builtin_ffs
544 #endif
545
546 #endif /* FFS_DEFINED */
547
548 /**
549 * Find last bit set in a word. The least significant bit is 1.
550 * Return 0 if no bits are set.
551 */
552 static INLINE unsigned util_last_bit(unsigned u)
553 {
554 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304)
555 return u == 0 ? 0 : 32 - __builtin_clz(u);
556 #else
557 unsigned r = 0;
558 while (u) {
559 r++;
560 u >>= 1;
561 }
562 return r;
563 #endif
564 }
565
566
567 /* Destructively loop over all of the bits in a mask as in:
568 *
569 * while (mymask) {
570 * int i = u_bit_scan(&mymask);
571 * ... process element i
572 * }
573 *
574 */
575 static INLINE int u_bit_scan(unsigned *mask)
576 {
577 int i = ffs(*mask) - 1;
578 *mask &= ~(1 << i);
579 return i;
580 }
581
582
583 /**
584 * Return float bits.
585 */
586 static INLINE unsigned
587 fui( float f )
588 {
589 union fi fi;
590 fi.f = f;
591 return fi.ui;
592 }
593
594
595 /**
596 * Convert ubyte to float in [0, 1].
597 * XXX a 256-entry lookup table would be slightly faster.
598 */
599 static INLINE float
600 ubyte_to_float(ubyte ub)
601 {
602 return (float) ub * (1.0f / 255.0f);
603 }
604
605
606 /**
607 * Convert float in [0,1] to ubyte in [0,255] with clamping.
608 */
609 static INLINE ubyte
610 float_to_ubyte(float f)
611 {
612 union fi tmp;
613
614 tmp.f = f;
615 if (tmp.i < 0) {
616 return (ubyte) 0;
617 }
618 else if (tmp.i >= 0x3f800000 /* 1.0f */) {
619 return (ubyte) 255;
620 }
621 else {
622 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
623 return (ubyte) tmp.i;
624 }
625 }
626
627 static INLINE float
628 byte_to_float_tex(int8_t b)
629 {
630 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
631 }
632
633 static INLINE int8_t
634 float_to_byte_tex(float f)
635 {
636 return (int8_t) (127.0F * f);
637 }
638
639 /**
640 * Calc log base 2
641 */
642 static INLINE unsigned
643 util_logbase2(unsigned n)
644 {
645 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
646 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
647 #else
648 unsigned pos = 0;
649 if (n >= 1<<16) { n >>= 16; pos += 16; }
650 if (n >= 1<< 8) { n >>= 8; pos += 8; }
651 if (n >= 1<< 4) { n >>= 4; pos += 4; }
652 if (n >= 1<< 2) { n >>= 2; pos += 2; }
653 if (n >= 1<< 1) { pos += 1; }
654 return pos;
655 #endif
656 }
657
658
659 /**
660 * Returns the smallest power of two >= x
661 */
662 static INLINE unsigned
663 util_next_power_of_two(unsigned x)
664 {
665 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
666 if (x <= 1)
667 return 1;
668
669 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
670 #else
671 unsigned val = x;
672
673 if (x <= 1)
674 return 1;
675
676 if (util_is_power_of_two(x))
677 return x;
678
679 val--;
680 val = (val >> 1) | val;
681 val = (val >> 2) | val;
682 val = (val >> 4) | val;
683 val = (val >> 8) | val;
684 val = (val >> 16) | val;
685 val++;
686 return val;
687 #endif
688 }
689
690
691 /**
692 * Return number of bits set in n.
693 */
694 static INLINE unsigned
695 util_bitcount(unsigned n)
696 {
697 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
698 return __builtin_popcount(n);
699 #else
700 /* K&R classic bitcount.
701 *
702 * For each iteration, clear the LSB from the bitfield.
703 * Requires only one iteration per set bit, instead of
704 * one iteration per bit less than highest set bit.
705 */
706 unsigned bits = 0;
707 for (bits; n; bits++) {
708 n &= n - 1;
709 }
710 return bits;
711 #endif
712 }
713
714
715 /**
716 * Convert from little endian to CPU byte order.
717 */
718
719 #ifdef PIPE_ARCH_BIG_ENDIAN
720 #define util_le32_to_cpu(x) util_bswap32(x)
721 #define util_le16_to_cpu(x) util_bswap16(x)
722 #else
723 #define util_le32_to_cpu(x) (x)
724 #define util_le16_to_cpu(x) (x)
725 #endif
726
727
728 /**
729 * Reverse byte order of a 32 bit word.
730 */
731 static INLINE uint32_t
732 util_bswap32(uint32_t n)
733 {
734 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403)
735 return __builtin_bswap32(n);
736 #else
737 return (n >> 24) |
738 ((n >> 8) & 0x0000ff00) |
739 ((n << 8) & 0x00ff0000) |
740 (n << 24);
741 #endif
742 }
743
744
745 /**
746 * Reverse byte order of a 16 bit word.
747 */
748 static INLINE uint16_t
749 util_bswap16(uint16_t n)
750 {
751 return (n >> 8) |
752 (n << 8);
753 }
754
755
756 /**
757 * Clamp X to [MIN, MAX].
758 * This is a macro to allow float, int, uint, etc. types.
759 */
760 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
761
762 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
763 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
764
765 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
766 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
767
768 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
769 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
770
771
772 /**
773 * Align a value, only works pot alignemnts.
774 */
775 static INLINE int
776 align(int value, int alignment)
777 {
778 return (value + alignment - 1) & ~(alignment - 1);
779 }
780
781 /**
782 * Works like align but on npot alignments.
783 */
784 static INLINE size_t
785 util_align_npot(size_t value, size_t alignment)
786 {
787 if (value % alignment)
788 return value + (alignment - (value % alignment));
789 return value;
790 }
791
792 static INLINE unsigned
793 u_minify(unsigned value, unsigned levels)
794 {
795 return MAX2(1, value >> levels);
796 }
797
798 #ifndef COPY_4V
799 #define COPY_4V( DST, SRC ) \
800 do { \
801 (DST)[0] = (SRC)[0]; \
802 (DST)[1] = (SRC)[1]; \
803 (DST)[2] = (SRC)[2]; \
804 (DST)[3] = (SRC)[3]; \
805 } while (0)
806 #endif
807
808
809 #ifndef COPY_4FV
810 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
811 #endif
812
813
814 #ifndef ASSIGN_4V
815 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
816 do { \
817 (DST)[0] = (V0); \
818 (DST)[1] = (V1); \
819 (DST)[2] = (V2); \
820 (DST)[3] = (V3); \
821 } while (0)
822 #endif
823
824
825 static INLINE uint32_t util_unsigned_fixed(float value, unsigned frac_bits)
826 {
827 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
828 }
829
830 static INLINE int32_t util_signed_fixed(float value, unsigned frac_bits)
831 {
832 return (int32_t)(value * (1<<frac_bits));
833 }
834
835 unsigned
836 util_fpstate_get(void);
837 unsigned
838 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
839 void
840 util_fpstate_set(unsigned fpstate);
841
842
843
844 #ifdef __cplusplus
845 }
846 #endif
847
848 #endif /* U_MATH_H */