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
)
121 #if defined(_MSC_VER)
123 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
125 static INLINE
float cosf( float f
)
127 return (float) cos( (double) f
);
130 static INLINE
float sinf( float f
)
132 return (float) sin( (double) f
);
135 static INLINE
float ceilf( float f
)
137 return (float) ceil( (double) f
);
140 static INLINE
float floorf( float f
)
142 return (float) floor( (double) f
);
145 static INLINE
float powf( float f
, float g
)
147 return (float) pow( (double) f
, (double) g
);
150 static INLINE
float sqrtf( float f
)
152 return (float) sqrt( (double) f
);
155 static INLINE
float fabsf( float f
)
157 return (float) fabs( (double) f
);
160 static INLINE
float logf( float f
)
162 return (float) log( (double) f
);
166 /* Work-around an extra semi-colon in VS 2005 logf definition */
169 #define logf(x) ((float)log((double)(x)))
173 static INLINE
double log2( double x
)
175 const double invln2
= 1.442695041;
176 return log( x
) * invln2
;
179 #endif /* _MSC_VER */
185 #define POW2_TABLE_SIZE_LOG2 9
186 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
187 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
188 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
189 extern float pow2_table
[POW2_TABLE_SIZE
];
193 * Initialize math module. This should be called before using any
194 * other functions in this module.
197 util_init_math(void);
208 * Fast version of 2^x
209 * Identity: exp2(a + b) = exp2(a) * exp2(b)
211 * Let fpart = x - ipart;
212 * So, exp2(x) = exp2(ipart) * exp2(fpart)
213 * Compute exp2(ipart) with i << ipart
214 * Compute exp2(fpart) with lookup table.
217 util_fast_exp2(float x
)
224 return 3.402823466e+38f
;
230 fpart
= x
- (float) ipart
;
233 * epart.f = (float) (1 << ipart)
234 * but faster and without integer overflow for ipart > 31
236 epart
.i
= (ipart
+ 127 ) << 23;
238 mpart
= pow2_table
[POW2_TABLE_OFFSET
+ (int)(fpart
* POW2_TABLE_SCALE
)];
240 return epart
.f
* mpart
;
245 * Fast approximation to exp(x).
248 util_fast_exp(float x
)
250 const float k
= 1.44269f
; /* = log2(e) */
251 return util_fast_exp2(k
* x
);
255 #define LOG2_TABLE_SIZE_LOG2 16
256 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
257 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
258 extern float log2_table
[LOG2_TABLE_SIZE
];
262 * Fast approximation to log2(x).
265 util_fast_log2(float x
)
270 epart
= (float)(((num
.i
& 0x7f800000) >> 23) - 127);
271 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
272 mpart
= log2_table
[((num
.i
& 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2
))) >> (23 - LOG2_TABLE_SIZE_LOG2
)];
273 return epart
+ mpart
;
278 * Fast approximation to x^y.
281 util_fast_pow(float x
, float y
)
283 return util_fast_exp2(util_fast_log2(x
) * y
);
286 /* Note that this counts zero as a power of two.
288 static INLINE boolean
289 util_is_power_of_two( unsigned v
)
291 return (v
& (v
-1)) == 0;
296 * Floor(x), returned as int.
304 af
= (3 << 22) + 0.5 + (double) f
;
305 bf
= (3 << 22) + 0.5 - (double) f
;
306 u
.f
= (float) af
; ai
= u
.i
;
307 u
.f
= (float) bf
; bi
= u
.i
;
308 return (ai
- bi
) >> 1;
313 * Round float to nearest int.
318 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
320 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
322 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
331 return (int) (f
+ 0.5f
);
333 return (int) (f
- 0.5f
);
340 * Test if x is NaN or +/- infinity.
342 static INLINE boolean
343 util_is_inf_or_nan(float x
)
347 return !(int)((unsigned int)((tmp
.i
& 0x7fffffff)-0x7f800000) >> 31);
352 * Test whether x is a power of two.
354 static INLINE boolean
355 util_is_pot(unsigned x
)
357 return (x
& (x
- 1)) == 0;
362 * Find first bit set in word. Least significant bit is 1.
363 * Return 0 if no bits set.
365 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
366 unsigned char _BitScanForward(unsigned long* Index
, unsigned long Mask
);
367 #pragma intrinsic(_BitScanForward)
369 unsigned long ffs( unsigned long u
)
372 if (_BitScanForward(&i
, u
))
377 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
379 unsigned ffs( unsigned u
)
393 #elif defined(__MINGW32__)
394 #define ffs __builtin_ffs
398 #define ffs __builtin_ffs
402 /* Could also binary search for the highest bit.
404 static INLINE
unsigned
405 util_unsigned_logbase2(unsigned n
)
417 static INLINE
unsigned
427 * Convert ubyte to float in [0, 1].
428 * XXX a 256-entry lookup table would be slightly faster.
431 ubyte_to_float(ubyte ub
)
433 return (float) ub
* (1.0f
/ 255.0f
);
438 * Convert float in [0,1] to ubyte in [0,255] with clamping.
441 float_to_ubyte(float f
)
443 const int ieee_0996
= 0x3f7f0000; /* 0.996 or so */
450 else if (tmp
.i
>= ieee_0996
) {
454 tmp
.f
= tmp
.f
* (255.0f
/256.0f
) + 32768.0f
;
455 return (ubyte
) tmp
.i
;
463 static INLINE
unsigned
464 util_logbase2(unsigned n
)
474 * Returns the smallest power of two >= x
476 static INLINE
unsigned
477 util_next_power_of_two(unsigned x
)
486 for (i
= 1; i
< sizeof(unsigned) * 8; i
<<= 1)
494 * Return number of bits set in n.
496 static INLINE
unsigned
497 util_bitcount(unsigned n
)
499 #if defined(PIPE_CC_GCC)
500 return __builtin_popcount(n
);
502 /* K&R classic bitcount.
504 * For each iteration, clear the LSB from the bitfield.
505 * Requires only one iteration per set bit, instead of
506 * one iteration per bit less than highest set bit.
509 for (bits
; n
; bits
++) {
518 * Reverse byte order of a 32 bit word.
520 static INLINE
uint32_t
521 util_bswap32(uint32_t n
)
523 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403)
524 return __builtin_bswap32(n
);
527 ((n
>> 8) & 0x0000ff00) |
528 ((n
<< 8) & 0x00ff0000) |
535 * Reverse byte order of a 16 bit word.
537 static INLINE
uint16_t
538 util_bswap16(uint16_t n
)
546 * Clamp X to [MIN, MAX].
547 * This is a macro to allow float, int, uint, etc. types.
549 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
551 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
552 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
554 #define MIN3( A, B, C ) MIN2( MIN2( A, B ), C )
555 #define MAX3( A, B, C ) MAX2( MAX2( A, B ), C )
559 align(int value
, int alignment
)
561 return (value
+ alignment
- 1) & ~(alignment
- 1);
564 static INLINE
unsigned
565 u_minify(unsigned value
, unsigned levels
)
567 return MAX2(1, value
>> levels
);
571 #define COPY_4V( DST, SRC ) \
573 (DST)[0] = (SRC)[0]; \
574 (DST)[1] = (SRC)[1]; \
575 (DST)[2] = (SRC)[2]; \
576 (DST)[3] = (SRC)[3]; \
582 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
587 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
597 static INLINE
uint32_t util_unsigned_fixed(float value
, unsigned frac_bits
)
599 return value
< 0 ? 0 : (uint32_t)(value
* (1<<frac_bits
));
602 static INLINE
int32_t util_signed_fixed(float value
, unsigned frac_bits
)
604 return (int32_t)(value
* (1<<frac_bits
));
613 #endif /* U_MATH_H */