1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Math utilities and approximations for common math functions.
31 * Reduced precision is usually acceptable in shaders...
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.
42 #include "pipe/p_compiler.h"
43 #include "util/u_debug.h"
56 #include <strings.h> /* for ffs */
61 #define M_SQRT2 1.41421356237309504880
67 #if _MSC_VER < 1400 && !defined(__cplusplus)
69 static INLINE float cosf( float f )
71 return (float) cos( (double) f );
74 static INLINE float sinf( float f )
76 return (float) sin( (double) f );
79 static INLINE float ceilf( float f )
81 return (float) ceil( (double) f );
84 static INLINE float floorf( float f )
86 return (float) floor( (double) f );
89 static INLINE float powf( float f, float g )
91 return (float) pow( (double) f, (double) g );
94 static INLINE float sqrtf( float f )
96 return (float) sqrt( (double) f );
99 static INLINE float fabsf( float f )
101 return (float) fabs( (double) f );
104 static INLINE float logf( float f )
106 return (float) log( (double) f );
110 /* Work-around an extra semi-colon in VS 2005 logf definition */
113 #define logf(x) ((float)log((double)(x)))
117 #define isfinite(x) _finite((double)(x))
118 #define isnan(x) _isnan((double)(x))
119 #endif /* _MSC_VER < 1800 */
120 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
123 static INLINE double log2( double x )
125 const double invln2 = 1.442695041;
126 return log( x ) * invln2;
132 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
138 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
143 #define INFINITY (DBL_MAX + DBL_MAX)
147 #define NAN (INFINITY - INFINITY)
150 #endif /* _MSC_VER */
153 #if __STDC_VERSION__ < 199901L && (!defined(__cplusplus) || defined(_MSC_VER))
154 static INLINE long int
157 long int rounded = (long int)(d + 0.5);
159 if (d - floor(d) == 0.5) {
160 if (rounded % 2 != 0)
161 rounded += (d > 0) ? -1 : 1;
167 static INLINE long int
170 long int rounded = (long int)(f + 0.5f);
172 if (f - floorf(f) == 0.5f) {
173 if (rounded % 2 != 0)
174 rounded += (f > 0) ? -1 : 1;
180 static INLINE long long int
183 long long int rounded = (long long int)(d + 0.5);
185 if (d - floor(d) == 0.5) {
186 if (rounded % 2 != 0)
187 rounded += (d > 0) ? -1 : 1;
193 static INLINE long long int
196 long long int rounded = (long long int)(f + 0.5f);
198 if (f - floorf(f) == 0.5f) {
199 if (rounded % 2 != 0)
200 rounded += (f > 0) ? -1 : 1;
207 #define POW2_TABLE_SIZE_LOG2 9
208 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
209 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
210 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
211 extern float pow2_table[POW2_TABLE_SIZE];
215 * Initialize math module. This should be called before using any
216 * other functions in this module.
219 util_init_math(void);
237 * Extract the IEEE float32 exponent.
240 util_get_float32_exponent(float x)
246 return ((f.ui >> 23) & 0xff) - 127;
251 * Fast version of 2^x
252 * Identity: exp2(a + b) = exp2(a) * exp2(b)
254 * Let fpart = x - ipart;
255 * So, exp2(x) = exp2(ipart) * exp2(fpart)
256 * Compute exp2(ipart) with i << ipart
257 * Compute exp2(fpart) with lookup table.
260 util_fast_exp2(float x)
267 return 3.402823466e+38f;
273 fpart = x - (float) ipart;
276 * epart.f = (float) (1 << ipart)
277 * but faster and without integer overflow for ipart > 31
279 epart.i = (ipart + 127 ) << 23;
281 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
283 return epart.f * mpart;
288 * Fast approximation to exp(x).
291 util_fast_exp(float x)
293 const float k = 1.44269f; /* = log2(e) */
294 return util_fast_exp2(k * x);
298 #define LOG2_TABLE_SIZE_LOG2 16
299 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
300 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
301 extern float log2_table[LOG2_TABLE_SIZE];
305 * Fast approximation to log2(x).
308 util_fast_log2(float x)
313 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
314 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
315 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
316 return epart + mpart;
321 * Fast approximation to x^y.
324 util_fast_pow(float x, float y)
326 return util_fast_exp2(util_fast_log2(x) * y);
329 /* Note that this counts zero as a power of two.
331 static INLINE boolean
332 util_is_power_of_two( unsigned v )
334 return (v & (v-1)) == 0;
339 * Floor(x), returned as int.
347 af = (3 << 22) + 0.5 + (double) f;
348 bf = (3 << 22) + 0.5 - (double) f;
349 u.f = (float) af; ai = u.i;
350 u.f = (float) bf; bi = u.i;
351 return (ai - bi) >> 1;
356 * Round float to nearest int.
361 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
363 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
365 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
374 return (int) (f + 0.5f);
376 return (int) (f - 0.5f);
382 * Approximate floating point comparison
384 static INLINE boolean
385 util_is_approx(float a, float b, float tol)
387 return fabs(b - a) <= tol;
392 * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
393 * util_is_X_nan = test if x is NaN
394 * util_X_inf_sign = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
396 * NaN can be checked with x != x, however this fails with the fast math flag
403 static INLINE boolean
404 util_is_inf_or_nan(float x)
408 return (tmp.ui & 0x7f800000) == 0x7f800000;
412 static INLINE boolean
417 return (tmp.ui & 0x7fffffff) > 0x7f800000;
422 util_inf_sign(float x)
426 if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
430 return (x < 0) ? -1 : 1;
437 static INLINE boolean
438 util_is_double_inf_or_nan(double x)
442 return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
446 static INLINE boolean
447 util_is_double_nan(double x)
451 return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
456 util_double_inf_sign(double x)
460 if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
464 return (x < 0) ? -1 : 1;
471 static INLINE boolean
472 util_is_half_inf_or_nan(int16_t x)
474 return (x & 0x7c00) == 0x7c00;
478 static INLINE boolean
479 util_is_half_nan(int16_t x)
481 return (x & 0x7fff) > 0x7c00;
486 util_half_inf_sign(int16_t x)
488 if ((x & 0x7fff) != 0x7c00) {
492 return (x < 0) ? -1 : 1;
497 * Find first bit set in word. Least significant bit is 1.
498 * Return 0 if no bits set.
501 #define FFS_DEFINED 1
503 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
504 unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
505 #pragma intrinsic(_BitScanForward)
507 unsigned long ffs( unsigned long u )
510 if (_BitScanForward(&i, u))
515 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
517 unsigned ffs( unsigned u )
531 #elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID)
532 #define ffs __builtin_ffs
535 #endif /* FFS_DEFINED */
538 * Find last bit set in a word. The least significant bit is 1.
539 * Return 0 if no bits are set.
541 static INLINE unsigned
542 util_last_bit(unsigned u)
544 #if defined(__GNUC__)
545 return u == 0 ? 0 : 32 - __builtin_clz(u);
557 * Find last bit in a word that does not match the sign bit. The least
558 * significant bit is 1.
559 * Return 0 if no bits are set.
561 static INLINE unsigned
562 util_last_bit_signed(int i)
565 return util_last_bit(i);
567 return util_last_bit(~(unsigned)i);
570 /* Destructively loop over all of the bits in a mask as in:
573 * int i = u_bit_scan(&mymask);
574 * ... process element i
579 u_bit_scan(unsigned *mask)
581 int i = ffs(*mask) - 1;
590 static INLINE unsigned
608 * Convert ubyte to float in [0, 1].
609 * XXX a 256-entry lookup table would be slightly faster.
612 ubyte_to_float(ubyte ub)
614 return (float) ub * (1.0f / 255.0f);
619 * Convert float in [0,1] to ubyte in [0,255] with clamping.
622 float_to_ubyte(float f)
630 else if (tmp.i >= 0x3f800000 /* 1.0f */) {
634 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
635 return (ubyte) tmp.i;
640 byte_to_float_tex(int8_t b)
642 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
646 float_to_byte_tex(float f)
648 return (int8_t) (127.0F * f);
654 static INLINE unsigned
655 util_logbase2(unsigned n)
657 #if defined(PIPE_CC_GCC)
658 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
661 if (n >= 1<<16) { n >>= 16; pos += 16; }
662 if (n >= 1<< 8) { n >>= 8; pos += 8; }
663 if (n >= 1<< 4) { n >>= 4; pos += 4; }
664 if (n >= 1<< 2) { n >>= 2; pos += 2; }
665 if (n >= 1<< 1) { pos += 1; }
672 * Returns the smallest power of two >= x
674 static INLINE unsigned
675 util_next_power_of_two(unsigned x)
677 #if defined(PIPE_CC_GCC)
681 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
688 if (util_is_power_of_two(x))
692 val = (val >> 1) | val;
693 val = (val >> 2) | val;
694 val = (val >> 4) | val;
695 val = (val >> 8) | val;
696 val = (val >> 16) | val;
704 * Return number of bits set in n.
706 static INLINE unsigned
707 util_bitcount(unsigned n)
709 #if defined(PIPE_CC_GCC)
710 return __builtin_popcount(n);
712 /* K&R classic bitcount.
714 * For each iteration, clear the LSB from the bitfield.
715 * Requires only one iteration per set bit, instead of
716 * one iteration per bit less than highest set bit.
719 for (bits; n; bits++) {
727 static INLINE unsigned
728 util_bitcount64(uint64_t n)
730 #ifdef HAVE___BUILTIN_POPCOUNTLL
731 return __builtin_popcountll(n);
733 return util_bitcount(n) + util_bitcount(n >> 32);
740 * Algorithm taken from:
741 * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
743 static INLINE unsigned
744 util_bitreverse(unsigned n)
746 n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
747 n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
748 n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
749 n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
750 n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
755 * Convert from little endian to CPU byte order.
758 #ifdef PIPE_ARCH_BIG_ENDIAN
759 #define util_le64_to_cpu(x) util_bswap64(x)
760 #define util_le32_to_cpu(x) util_bswap32(x)
761 #define util_le16_to_cpu(x) util_bswap16(x)
763 #define util_le64_to_cpu(x) (x)
764 #define util_le32_to_cpu(x) (x)
765 #define util_le16_to_cpu(x) (x)
768 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
769 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
770 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
773 * Reverse byte order of a 32 bit word.
775 static INLINE uint32_t
776 util_bswap32(uint32_t n)
778 /* We need the gcc version checks for non-autoconf build system */
779 #if defined(HAVE___BUILTIN_BSWAP32) || (defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403))
780 return __builtin_bswap32(n);
783 ((n >> 8) & 0x0000ff00) |
784 ((n << 8) & 0x00ff0000) |
790 * Reverse byte order of a 64bit word.
792 static INLINE uint64_t
793 util_bswap64(uint64_t n)
795 #if defined(HAVE___BUILTIN_BSWAP64)
796 return __builtin_bswap64(n);
798 return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
799 util_bswap32((n >> 32));
805 * Reverse byte order of a 16 bit word.
807 static INLINE uint16_t
808 util_bswap16(uint16_t n)
815 util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
817 #ifdef PIPE_ARCH_BIG_ENDIAN
821 for (i = 0, e = n / 4; i < e; i++) {
822 uint32_t * restrict d = (uint32_t* restrict)dest;
823 const uint32_t * restrict s = (const uint32_t* restrict)src;
824 d[i] = util_bswap32(s[i]);
828 return memcpy(dest, src, n);
833 * Clamp X to [MIN, MAX].
834 * This is a macro to allow float, int, uint, etc. types.
836 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
838 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
839 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
841 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
842 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
844 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
845 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
849 * Align a value, only works pot alignemnts.
852 align(int value, int alignment)
854 return (value + alignment - 1) & ~(alignment - 1);
858 * Works like align but on npot alignments.
861 util_align_npot(size_t value, size_t alignment)
863 if (value % alignment)
864 return value + (alignment - (value % alignment));
868 static INLINE unsigned
869 u_minify(unsigned value, unsigned levels)
871 return MAX2(1, value >> levels);
875 #define COPY_4V( DST, SRC ) \
877 (DST)[0] = (SRC)[0]; \
878 (DST)[1] = (SRC)[1]; \
879 (DST)[2] = (SRC)[2]; \
880 (DST)[3] = (SRC)[3]; \
886 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
891 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
901 static INLINE uint32_t
902 util_unsigned_fixed(float value, unsigned frac_bits)
904 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
907 static INLINE int32_t
908 util_signed_fixed(float value, unsigned frac_bits)
910 return (int32_t)(value * (1<<frac_bits));
914 util_fpstate_get(void);
916 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
918 util_fpstate_set(unsigned fpstate);
926 #endif /* U_MATH_H */