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"
55 #include <strings.h> /* for ffs */
60 #define M_SQRT2 1.41421356237309504880
66 #if _MSC_VER < 1400 && !defined(__cplusplus)
68 static INLINE
float cosf( float f
)
70 return (float) cos( (double) f
);
73 static INLINE
float sinf( float f
)
75 return (float) sin( (double) f
);
78 static INLINE
float ceilf( float f
)
80 return (float) ceil( (double) f
);
83 static INLINE
float floorf( float f
)
85 return (float) floor( (double) f
);
88 static INLINE
float powf( float f
, float g
)
90 return (float) pow( (double) f
, (double) g
);
93 static INLINE
float sqrtf( float f
)
95 return (float) sqrt( (double) f
);
98 static INLINE
float fabsf( float f
)
100 return (float) fabs( (double) f
);
103 static INLINE
float logf( float f
)
105 return (float) log( (double) f
);
109 /* Work-around an extra semi-colon in VS 2005 logf definition */
112 #define logf(x) ((float)log((double)(x)))
115 #define isfinite(x) _finite((double)(x))
116 #define isnan(x) _isnan((double)(x))
117 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
119 static INLINE
double log2( double x
)
121 const double invln2
= 1.442695041;
122 return log( x
) * invln2
;
128 return x
>= 0.0 ? floor(x
+ 0.5) : ceil(x
- 0.5);
134 return x
>= 0.0f
? floorf(x
+ 0.5f
) : ceilf(x
- 0.5f
);
137 #endif /* _MSC_VER */
140 #ifdef PIPE_OS_ANDROID
143 double log2(double d
)
145 return log(d
) * (1.0 / M_LN2
);
148 /* workaround a conflict with main/imports.h */
156 return logf(f
) * (float) (1.0 / M_LN2
);
164 #define POW2_TABLE_SIZE_LOG2 9
165 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
166 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
167 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
168 extern float pow2_table
[POW2_TABLE_SIZE
];
172 * Initialize math module. This should be called before using any
173 * other functions in this module.
176 util_init_math(void);
187 * Fast version of 2^x
188 * Identity: exp2(a + b) = exp2(a) * exp2(b)
190 * Let fpart = x - ipart;
191 * So, exp2(x) = exp2(ipart) * exp2(fpart)
192 * Compute exp2(ipart) with i << ipart
193 * Compute exp2(fpart) with lookup table.
196 util_fast_exp2(float x
)
203 return 3.402823466e+38f
;
209 fpart
= x
- (float) ipart
;
212 * epart.f = (float) (1 << ipart)
213 * but faster and without integer overflow for ipart > 31
215 epart
.i
= (ipart
+ 127 ) << 23;
217 mpart
= pow2_table
[POW2_TABLE_OFFSET
+ (int)(fpart
* POW2_TABLE_SCALE
)];
219 return epart
.f
* mpart
;
224 * Fast approximation to exp(x).
227 util_fast_exp(float x
)
229 const float k
= 1.44269f
; /* = log2(e) */
230 return util_fast_exp2(k
* x
);
234 #define LOG2_TABLE_SIZE_LOG2 16
235 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
236 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
237 extern float log2_table
[LOG2_TABLE_SIZE
];
241 * Fast approximation to log2(x).
244 util_fast_log2(float x
)
249 epart
= (float)(((num
.i
& 0x7f800000) >> 23) - 127);
250 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
251 mpart
= log2_table
[((num
.i
& 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2
))) >> (23 - LOG2_TABLE_SIZE_LOG2
)];
252 return epart
+ mpart
;
257 * Fast approximation to x^y.
260 util_fast_pow(float x
, float y
)
262 return util_fast_exp2(util_fast_log2(x
) * y
);
265 /* Note that this counts zero as a power of two.
267 static INLINE boolean
268 util_is_power_of_two( unsigned v
)
270 return (v
& (v
-1)) == 0;
275 * Floor(x), returned as int.
283 af
= (3 << 22) + 0.5 + (double) f
;
284 bf
= (3 << 22) + 0.5 - (double) f
;
285 u
.f
= (float) af
; ai
= u
.i
;
286 u
.f
= (float) bf
; bi
= u
.i
;
287 return (ai
- bi
) >> 1;
292 * Round float to nearest int.
297 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
299 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
301 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
310 return (int) (f
+ 0.5f
);
312 return (int) (f
- 0.5f
);
318 * Approximate floating point comparison
320 static INLINE boolean
321 util_is_approx(float a
, float b
, float tol
)
323 return fabs(b
- a
) <= tol
;
328 * Test if x is NaN or +/- infinity.
330 static INLINE boolean
331 util_is_inf_or_nan(float x
)
335 return !(int)((unsigned int)((tmp
.i
& 0x7fffffff)-0x7f800000) >> 31);
340 * Find first bit set in word. Least significant bit is 1.
341 * Return 0 if no bits set.
344 #define FFS_DEFINED 1
346 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
347 unsigned char _BitScanForward(unsigned long* Index
, unsigned long Mask
);
348 #pragma intrinsic(_BitScanForward)
350 unsigned long ffs( unsigned long u
)
353 if (_BitScanForward(&i
, u
))
358 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
360 unsigned ffs( unsigned u
)
374 #elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID)
375 #define ffs __builtin_ffs
378 #endif /* FFS_DEFINED */
381 /* Destructively loop over all of the bits in a mask as in:
384 * int i = u_bit_scan(&mymask);
385 * ... process element i
389 static INLINE
int u_bit_scan(unsigned *mask
)
391 int i
= ffs(*mask
) - 1;
400 static INLINE
unsigned
410 * Convert ubyte to float in [0, 1].
411 * XXX a 256-entry lookup table would be slightly faster.
414 ubyte_to_float(ubyte ub
)
416 return (float) ub
* (1.0f
/ 255.0f
);
421 * Convert float in [0,1] to ubyte in [0,255] with clamping.
424 float_to_ubyte(float f
)
426 const int ieee_0996
= 0x3f7f0000; /* 0.996 or so */
433 else if (tmp
.i
>= ieee_0996
) {
437 tmp
.f
= tmp
.f
* (255.0f
/256.0f
) + 32768.0f
;
438 return (ubyte
) tmp
.i
;
443 byte_to_float_tex(int8_t b
)
445 return (b
== -128) ? -1.0F
: b
* 1.0F
/ 127.0F
;
449 float_to_byte_tex(float f
)
451 return (int8_t) (127.0F
* f
);
457 static INLINE
unsigned
458 util_logbase2(unsigned n
)
460 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
461 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n
| 1));
464 if (n
>= 1<<16) { n
>>= 16; pos
+= 16; }
465 if (n
>= 1<< 8) { n
>>= 8; pos
+= 8; }
466 if (n
>= 1<< 4) { n
>>= 4; pos
+= 4; }
467 if (n
>= 1<< 2) { n
>>= 2; pos
+= 2; }
468 if (n
>= 1<< 1) { pos
+= 1; }
475 * Returns the smallest power of two >= x
477 static INLINE
unsigned
478 util_next_power_of_two(unsigned x
)
480 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
484 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x
- 1)));
491 if (util_is_power_of_two(x
))
495 val
= (val
>> 1) | val
;
496 val
= (val
>> 2) | val
;
497 val
= (val
>> 4) | val
;
498 val
= (val
>> 8) | val
;
499 val
= (val
>> 16) | val
;
507 * Return number of bits set in n.
509 static INLINE
unsigned
510 util_bitcount(unsigned n
)
512 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
513 return __builtin_popcount(n
);
515 /* K&R classic bitcount.
517 * For each iteration, clear the LSB from the bitfield.
518 * Requires only one iteration per set bit, instead of
519 * one iteration per bit less than highest set bit.
522 for (bits
; n
; bits
++) {
531 * Convert from little endian to CPU byte order.
534 #ifdef PIPE_ARCH_BIG_ENDIAN
535 #define util_le32_to_cpu(x) util_bswap32(x)
536 #define util_le16_to_cpu(x) util_bswap16(x)
538 #define util_le32_to_cpu(x) (x)
539 #define util_le16_to_cpu(x) (x)
544 * Reverse byte order of a 32 bit word.
546 static INLINE
uint32_t
547 util_bswap32(uint32_t n
)
549 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403)
550 return __builtin_bswap32(n
);
553 ((n
>> 8) & 0x0000ff00) |
554 ((n
<< 8) & 0x00ff0000) |
561 * Reverse byte order of a 16 bit word.
563 static INLINE
uint16_t
564 util_bswap16(uint16_t n
)
572 * Clamp X to [MIN, MAX].
573 * This is a macro to allow float, int, uint, etc. types.
575 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
577 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
578 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
580 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
581 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
583 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
584 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
588 * Align a value, only works pot alignemnts.
591 align(int value
, int alignment
)
593 return (value
+ alignment
- 1) & ~(alignment
- 1);
597 * Works like align but on npot alignments.
600 util_align_npot(size_t value
, size_t alignment
)
602 if (value
% alignment
)
603 return value
+ (alignment
- (value
% alignment
));
607 static INLINE
unsigned
608 u_minify(unsigned value
, unsigned levels
)
610 return MAX2(1, value
>> levels
);
614 #define COPY_4V( DST, SRC ) \
616 (DST)[0] = (SRC)[0]; \
617 (DST)[1] = (SRC)[1]; \
618 (DST)[2] = (SRC)[2]; \
619 (DST)[3] = (SRC)[3]; \
625 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
630 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
640 static INLINE
uint32_t util_unsigned_fixed(float value
, unsigned frac_bits
)
642 return value
< 0 ? 0 : (uint32_t)(value
* (1<<frac_bits
));
645 static INLINE
int32_t util_signed_fixed(float value
, unsigned frac_bits
)
647 return (int32_t)(value
* (1<<frac_bits
));
656 #endif /* U_MATH_H */