1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
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"
51 #if defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
52 __inline double ceil(double val)
56 if ((val - (long) val) == 0) {
61 ceil_val = (long) val + 1;
64 ceil_val = (long) val;
71 #ifndef PIPE_SUBSYSTEM_WINDOWS_CE_OGL
72 __inline double floor(double val)
76 if ((val - (long) val) == 0) {
81 floor_val = (long) val;
84 floor_val = (long) val - 1;
93 __inline double __cdecl pow(double val, double exponent)
100 #pragma function(log)
101 __inline double __cdecl log(double val)
108 #pragma function(atan2)
109 __inline double __cdecl atan2(double val)
122 #define M_SQRT2 1.41421356237309504880
126 #if defined(_MSC_VER)
128 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
130 static INLINE float cosf( float f )
132 return (float) cos( (double) f );
135 static INLINE float sinf( float f )
137 return (float) sin( (double) f );
140 static INLINE float ceilf( float f )
142 return (float) ceil( (double) f );
145 static INLINE float floorf( float f )
147 return (float) floor( (double) f );
150 static INLINE float powf( float f, float g )
152 return (float) pow( (double) f, (double) g );
155 static INLINE float sqrtf( float f )
157 return (float) sqrt( (double) f );
160 static INLINE float fabsf( float f )
162 return (float) fabs( (double) f );
165 static INLINE float logf( float f )
167 return (float) log( (double) f );
171 /* Work-around an extra semi-colon in VS 2005 logf definition */
174 #define logf(x) ((float)log((double)(x)))
177 #define isfinite(x) _finite((double)(x))
178 #define isnan(x) _isnan((double)(x))
179 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
181 static INLINE double log2( double x )
183 const double invln2 = 1.442695041;
184 return log( x ) * invln2;
190 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
196 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
199 #endif /* _MSC_VER */
205 #define POW2_TABLE_SIZE_LOG2 9
206 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
207 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
208 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
209 extern float pow2_table[POW2_TABLE_SIZE];
213 * Initialize math module. This should be called before using any
214 * other functions in this module.
217 util_init_math(void);
228 * Fast version of 2^x
229 * Identity: exp2(a + b) = exp2(a) * exp2(b)
231 * Let fpart = x - ipart;
232 * So, exp2(x) = exp2(ipart) * exp2(fpart)
233 * Compute exp2(ipart) with i << ipart
234 * Compute exp2(fpart) with lookup table.
237 util_fast_exp2(float x)
244 return 3.402823466e+38f;
250 fpart = x - (float) ipart;
253 * epart.f = (float) (1 << ipart)
254 * but faster and without integer overflow for ipart > 31
256 epart.i = (ipart + 127 ) << 23;
258 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
260 return epart.f * mpart;
265 * Fast approximation to exp(x).
268 util_fast_exp(float x)
270 const float k = 1.44269f; /* = log2(e) */
271 return util_fast_exp2(k * x);
275 #define LOG2_TABLE_SIZE_LOG2 16
276 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
277 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
278 extern float log2_table[LOG2_TABLE_SIZE];
282 * Fast approximation to log2(x).
285 util_fast_log2(float x)
290 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
291 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
292 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
293 return epart + mpart;
298 * Fast approximation to x^y.
301 util_fast_pow(float x, float y)
303 return util_fast_exp2(util_fast_log2(x) * y);
306 /* Note that this counts zero as a power of two.
308 static INLINE boolean
309 util_is_power_of_two( unsigned v )
311 return (v & (v-1)) == 0;
316 * Floor(x), returned as int.
324 af = (3 << 22) + 0.5 + (double) f;
325 bf = (3 << 22) + 0.5 - (double) f;
326 u.f = (float) af; ai = u.i;
327 u.f = (float) bf; bi = u.i;
328 return (ai - bi) >> 1;
333 * Round float to nearest int.
338 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
340 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
342 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
351 return (int) (f + 0.5f);
353 return (int) (f - 0.5f);
359 * Approximate floating point comparison
361 static INLINE boolean
362 util_is_approx(float a, float b, float tol)
364 return fabs(b - a) <= tol;
369 * Test if x is NaN or +/- infinity.
371 static INLINE boolean
372 util_is_inf_or_nan(float x)
376 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
381 * Find first bit set in word. Least significant bit is 1.
382 * Return 0 if no bits set.
384 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
385 unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
386 #pragma intrinsic(_BitScanForward)
388 unsigned long ffs( unsigned long u )
391 if (_BitScanForward(&i, u))
396 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
398 unsigned ffs( unsigned u )
412 #elif defined(__MINGW32__)
413 #define ffs __builtin_ffs
420 static INLINE unsigned
430 * Convert ubyte to float in [0, 1].
431 * XXX a 256-entry lookup table would be slightly faster.
434 ubyte_to_float(ubyte ub)
436 return (float) ub * (1.0f / 255.0f);
441 * Convert float in [0,1] to ubyte in [0,255] with clamping.
444 float_to_ubyte(float f)
446 const int ieee_0996 = 0x3f7f0000; /* 0.996 or so */
453 else if (tmp.i >= ieee_0996) {
457 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
458 return (ubyte) tmp.i;
463 byte_to_float_tex(int8_t b)
465 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
469 float_to_byte_tex(float f)
471 return (int8_t) (127.0F * f);
477 static INLINE unsigned
478 util_logbase2(unsigned n)
480 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
481 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
484 if (n >= 1<<16) { n >>= 16; pos += 16; }
485 if (n >= 1<< 8) { n >>= 8; pos += 8; }
486 if (n >= 1<< 4) { n >>= 4; pos += 4; }
487 if (n >= 1<< 2) { n >>= 2; pos += 2; }
488 if (n >= 1<< 1) { pos += 1; }
495 * Returns the smallest power of two >= x
497 static INLINE unsigned
498 util_next_power_of_two(unsigned x)
500 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
504 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
511 if (util_is_power_of_two(x))
515 val = (val >> 1) | val;
516 val = (val >> 2) | val;
517 val = (val >> 4) | val;
518 val = (val >> 8) | val;
519 val = (val >> 16) | val;
527 * Return number of bits set in n.
529 static INLINE unsigned
530 util_bitcount(unsigned n)
532 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
533 return __builtin_popcount(n);
535 /* K&R classic bitcount.
537 * For each iteration, clear the LSB from the bitfield.
538 * Requires only one iteration per set bit, instead of
539 * one iteration per bit less than highest set bit.
542 for (bits; n; bits++) {
551 * Convert from little endian to CPU byte order.
554 #ifdef PIPE_ARCH_BIG_ENDIAN
555 #define util_le32_to_cpu(x) util_bswap32(x)
556 #define util_le16_to_cpu(x) util_bswap16(x)
558 #define util_le32_to_cpu(x) (x)
559 #define util_le16_to_cpu(x) (x)
564 * Reverse byte order of a 32 bit word.
566 static INLINE uint32_t
567 util_bswap32(uint32_t n)
569 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403)
570 return __builtin_bswap32(n);
573 ((n >> 8) & 0x0000ff00) |
574 ((n << 8) & 0x00ff0000) |
581 * Reverse byte order of a 16 bit word.
583 static INLINE uint16_t
584 util_bswap16(uint16_t n)
592 * Clamp X to [MIN, MAX].
593 * This is a macro to allow float, int, uint, etc. types.
595 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
597 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
598 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
600 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
601 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
603 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
604 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
608 * Align a value, only works pot alignemnts.
611 align(int value, int alignment)
613 return (value + alignment - 1) & ~(alignment - 1);
617 * Works like align but on npot alignments.
620 util_align_npot(size_t value, size_t alignment)
622 if (value % alignment)
623 return value + (alignment - (value % alignment));
627 static INLINE unsigned
628 u_minify(unsigned value, unsigned levels)
630 return MAX2(1, value >> levels);
634 #define COPY_4V( DST, SRC ) \
636 (DST)[0] = (SRC)[0]; \
637 (DST)[1] = (SRC)[1]; \
638 (DST)[2] = (SRC)[2]; \
639 (DST)[3] = (SRC)[3]; \
645 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
650 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
660 static INLINE uint32_t util_unsigned_fixed(float value, unsigned frac_bits)
662 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
665 static INLINE int32_t util_signed_fixed(float value, unsigned frac_bits)
667 return (int32_t)(value * (1<<frac_bits));
676 #endif /* U_MATH_H */