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"
54 #include <strings.h> /* for ffs */
59 #define M_SQRT2 1.41421356237309504880
65 #if _MSC_VER < 1400 && !defined(__cplusplus)
67 static INLINE
float cosf( float f
)
69 return (float) cos( (double) f
);
72 static INLINE
float sinf( float f
)
74 return (float) sin( (double) f
);
77 static INLINE
float ceilf( float f
)
79 return (float) ceil( (double) f
);
82 static INLINE
float floorf( float f
)
84 return (float) floor( (double) f
);
87 static INLINE
float powf( float f
, float g
)
89 return (float) pow( (double) f
, (double) g
);
92 static INLINE
float sqrtf( float f
)
94 return (float) sqrt( (double) f
);
97 static INLINE
float fabsf( float f
)
99 return (float) fabs( (double) f
);
102 static INLINE
float logf( float f
)
104 return (float) log( (double) f
);
108 /* Work-around an extra semi-colon in VS 2005 logf definition */
111 #define logf(x) ((float)log((double)(x)))
114 #define isfinite(x) _finite((double)(x))
115 #define isnan(x) _isnan((double)(x))
116 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
118 static INLINE
double log2( double x
)
120 const double invln2
= 1.442695041;
121 return log( x
) * invln2
;
127 return x
>= 0.0 ? floor(x
+ 0.5) : ceil(x
- 0.5);
133 return x
>= 0.0f
? floorf(x
+ 0.5f
) : ceilf(x
- 0.5f
);
136 #endif /* _MSC_VER */
139 #ifdef PIPE_OS_ANDROID
142 double log2(double d
)
144 return log(d
) * (1.0 / M_LN2
);
147 /* workaround a conflict with main/imports.h */
155 return logf(f
) * (float) (1.0 / M_LN2
);
163 #define POW2_TABLE_SIZE_LOG2 9
164 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
165 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
166 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
167 extern float pow2_table
[POW2_TABLE_SIZE
];
171 * Initialize math module. This should be called before using any
172 * other functions in this module.
175 util_init_math(void);
193 * Fast version of 2^x
194 * Identity: exp2(a + b) = exp2(a) * exp2(b)
196 * Let fpart = x - ipart;
197 * So, exp2(x) = exp2(ipart) * exp2(fpart)
198 * Compute exp2(ipart) with i << ipart
199 * Compute exp2(fpart) with lookup table.
202 util_fast_exp2(float x
)
209 return 3.402823466e+38f
;
215 fpart
= x
- (float) ipart
;
218 * epart.f = (float) (1 << ipart)
219 * but faster and without integer overflow for ipart > 31
221 epart
.i
= (ipart
+ 127 ) << 23;
223 mpart
= pow2_table
[POW2_TABLE_OFFSET
+ (int)(fpart
* POW2_TABLE_SCALE
)];
225 return epart
.f
* mpart
;
230 * Fast approximation to exp(x).
233 util_fast_exp(float x
)
235 const float k
= 1.44269f
; /* = log2(e) */
236 return util_fast_exp2(k
* x
);
240 #define LOG2_TABLE_SIZE_LOG2 16
241 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
242 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
243 extern float log2_table
[LOG2_TABLE_SIZE
];
247 * Fast approximation to log2(x).
250 util_fast_log2(float x
)
255 epart
= (float)(((num
.i
& 0x7f800000) >> 23) - 127);
256 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
257 mpart
= log2_table
[((num
.i
& 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2
))) >> (23 - LOG2_TABLE_SIZE_LOG2
)];
258 return epart
+ mpart
;
263 * Fast approximation to x^y.
266 util_fast_pow(float x
, float y
)
268 return util_fast_exp2(util_fast_log2(x
) * y
);
271 /* Note that this counts zero as a power of two.
273 static INLINE boolean
274 util_is_power_of_two( unsigned v
)
276 return (v
& (v
-1)) == 0;
281 * Floor(x), returned as int.
289 af
= (3 << 22) + 0.5 + (double) f
;
290 bf
= (3 << 22) + 0.5 - (double) f
;
291 u
.f
= (float) af
; ai
= u
.i
;
292 u
.f
= (float) bf
; bi
= u
.i
;
293 return (ai
- bi
) >> 1;
298 * Round float to nearest int.
303 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
305 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
307 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
316 return (int) (f
+ 0.5f
);
318 return (int) (f
- 0.5f
);
324 * Approximate floating point comparison
326 static INLINE boolean
327 util_is_approx(float a
, float b
, float tol
)
329 return fabs(b
- a
) <= tol
;
334 * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
335 * util_is_X_nan = test if x is NaN
336 * util_X_inf_sign = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
338 * NaN can be checked with x != x, however this fails with the fast math flag
345 static INLINE boolean
346 util_is_inf_or_nan(float x
)
350 return (tmp
.ui
& 0x7f800000) == 0x7f800000;
354 static INLINE boolean
359 return (tmp
.ui
& 0x7fffffff) > 0x7f800000;
364 util_inf_sign(float x
)
368 if ((tmp
.ui
& 0x7fffffff) != 0x7f800000) {
372 return (x
< 0) ? -1 : 1;
379 static INLINE boolean
380 util_is_double_inf_or_nan(double x
)
384 return (tmp
.ui
& 0x7ff0000000000000ULL
) == 0x7ff0000000000000ULL
;
388 static INLINE boolean
389 util_is_double_nan(double x
)
393 return (tmp
.ui
& 0x7fffffffffffffffULL
) > 0x7ff0000000000000ULL
;
398 util_double_inf_sign(double x
)
402 if ((tmp
.ui
& 0x7fffffffffffffffULL
) != 0x7ff0000000000000ULL
) {
406 return (x
< 0) ? -1 : 1;
413 static INLINE boolean
414 util_is_half_inf_or_nan(int16_t x
)
416 return (x
& 0x7c00) == 0x7c00;
420 static INLINE boolean
421 util_is_half_nan(int16_t x
)
423 return (x
& 0x7fff) > 0x7c00;
428 util_half_inf_sign(int16_t x
)
430 if ((x
& 0x7fff) != 0x7c00) {
434 return (x
< 0) ? -1 : 1;
439 * Find first bit set in word. Least significant bit is 1.
440 * Return 0 if no bits set.
443 #define FFS_DEFINED 1
445 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
446 unsigned char _BitScanForward(unsigned long* Index
, unsigned long Mask
);
447 #pragma intrinsic(_BitScanForward)
449 unsigned long ffs( unsigned long u
)
452 if (_BitScanForward(&i
, u
))
457 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
459 unsigned ffs( unsigned u
)
473 #elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID)
474 #define ffs __builtin_ffs
477 #endif /* FFS_DEFINED */
480 * Find last bit set in a word. The least significant bit is 1.
481 * Return 0 if no bits are set.
483 static INLINE
unsigned util_last_bit(unsigned u
)
485 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304)
486 return u
== 0 ? 0 : 32 - __builtin_clz(u
);
498 /* Destructively loop over all of the bits in a mask as in:
501 * int i = u_bit_scan(&mymask);
502 * ... process element i
506 static INLINE
int u_bit_scan(unsigned *mask
)
508 int i
= ffs(*mask
) - 1;
517 static INLINE
unsigned
527 * Convert ubyte to float in [0, 1].
528 * XXX a 256-entry lookup table would be slightly faster.
531 ubyte_to_float(ubyte ub
)
533 return (float) ub
* (1.0f
/ 255.0f
);
538 * Convert float in [0,1] to ubyte in [0,255] with clamping.
541 float_to_ubyte(float f
)
543 const int ieee_0996
= 0x3f7f0000; /* 0.996 or so */
550 else if (tmp
.i
>= ieee_0996
) {
554 tmp
.f
= tmp
.f
* (255.0f
/256.0f
) + 32768.0f
;
555 return (ubyte
) tmp
.i
;
560 byte_to_float_tex(int8_t b
)
562 return (b
== -128) ? -1.0F
: b
* 1.0F
/ 127.0F
;
566 float_to_byte_tex(float f
)
568 return (int8_t) (127.0F
* f
);
574 static INLINE
unsigned
575 util_logbase2(unsigned n
)
577 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
578 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n
| 1));
581 if (n
>= 1<<16) { n
>>= 16; pos
+= 16; }
582 if (n
>= 1<< 8) { n
>>= 8; pos
+= 8; }
583 if (n
>= 1<< 4) { n
>>= 4; pos
+= 4; }
584 if (n
>= 1<< 2) { n
>>= 2; pos
+= 2; }
585 if (n
>= 1<< 1) { pos
+= 1; }
592 * Returns the smallest power of two >= x
594 static INLINE
unsigned
595 util_next_power_of_two(unsigned x
)
597 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
601 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x
- 1)));
608 if (util_is_power_of_two(x
))
612 val
= (val
>> 1) | val
;
613 val
= (val
>> 2) | val
;
614 val
= (val
>> 4) | val
;
615 val
= (val
>> 8) | val
;
616 val
= (val
>> 16) | val
;
624 * Return number of bits set in n.
626 static INLINE
unsigned
627 util_bitcount(unsigned n
)
629 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
630 return __builtin_popcount(n
);
632 /* K&R classic bitcount.
634 * For each iteration, clear the LSB from the bitfield.
635 * Requires only one iteration per set bit, instead of
636 * one iteration per bit less than highest set bit.
639 for (bits
; n
; bits
++) {
648 * Convert from little endian to CPU byte order.
651 #ifdef PIPE_ARCH_BIG_ENDIAN
652 #define util_le32_to_cpu(x) util_bswap32(x)
653 #define util_le16_to_cpu(x) util_bswap16(x)
655 #define util_le32_to_cpu(x) (x)
656 #define util_le16_to_cpu(x) (x)
661 * Reverse byte order of a 32 bit word.
663 static INLINE
uint32_t
664 util_bswap32(uint32_t n
)
666 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403)
667 return __builtin_bswap32(n
);
670 ((n
>> 8) & 0x0000ff00) |
671 ((n
<< 8) & 0x00ff0000) |
678 * Reverse byte order of a 16 bit word.
680 static INLINE
uint16_t
681 util_bswap16(uint16_t n
)
689 * Clamp X to [MIN, MAX].
690 * This is a macro to allow float, int, uint, etc. types.
692 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
694 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
695 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
697 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
698 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
700 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
701 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
705 * Align a value, only works pot alignemnts.
708 align(int value
, int alignment
)
710 return (value
+ alignment
- 1) & ~(alignment
- 1);
714 * Works like align but on npot alignments.
717 util_align_npot(size_t value
, size_t alignment
)
719 if (value
% alignment
)
720 return value
+ (alignment
- (value
% alignment
));
724 static INLINE
unsigned
725 u_minify(unsigned value
, unsigned levels
)
727 return MAX2(1, value
>> levels
);
731 #define COPY_4V( DST, SRC ) \
733 (DST)[0] = (SRC)[0]; \
734 (DST)[1] = (SRC)[1]; \
735 (DST)[2] = (SRC)[2]; \
736 (DST)[3] = (SRC)[3]; \
742 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
747 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
757 static INLINE
uint32_t util_unsigned_fixed(float value
, unsigned frac_bits
)
759 return value
< 0 ? 0 : (uint32_t)(value
* (1<<frac_bits
));
762 static INLINE
int32_t util_signed_fixed(float value
, unsigned frac_bits
)
764 return (int32_t)(value
* (1<<frac_bits
));
773 #endif /* U_MATH_H */