#include "pipe/p_compiler.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-#include <math.h>
+#include "c99_math.h"
+#include <assert.h>
#include <float.h>
#include <stdarg.h>
-#ifdef PIPE_OS_UNIX
-#include <strings.h> /* for ffs */
+#include "util/bitscan.h"
+
+#ifdef __cplusplus
+extern "C" {
#endif
#define M_SQRT2 1.41421356237309504880
#endif
-
-#if defined(_MSC_VER)
-
-#if _MSC_VER < 1400 && !defined(__cplusplus)
-
-static INLINE float cosf( float f )
-{
- return (float) cos( (double) f );
-}
-
-static INLINE float sinf( float f )
-{
- return (float) sin( (double) f );
-}
-
-static INLINE float ceilf( float f )
-{
- return (float) ceil( (double) f );
-}
-
-static INLINE float floorf( float f )
-{
- return (float) floor( (double) f );
-}
-
-static INLINE float powf( float f, float g )
-{
- return (float) pow( (double) f, (double) g );
-}
-
-static INLINE float sqrtf( float f )
-{
- return (float) sqrt( (double) f );
-}
-
-static INLINE float fabsf( float f )
-{
- return (float) fabs( (double) f );
-}
-
-static INLINE float logf( float f )
-{
- return (float) log( (double) f );
-}
-
-#else
-/* Work-around an extra semi-colon in VS 2005 logf definition */
-#ifdef logf
-#undef logf
-#define logf(x) ((float)log((double)(x)))
-#endif /* logf */
-
-#if _MSC_VER < 1800
-#define isfinite(x) _finite((double)(x))
-#define isnan(x) _isnan((double)(x))
-#endif /* _MSC_VER < 1800 */
-#endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
-
-#if _MSC_VER < 1800
-static INLINE double log2( double x )
-{
- const double invln2 = 1.442695041;
- return log( x ) * invln2;
-}
-
-static INLINE double
-round(double x)
-{
- return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
-}
-
-static INLINE float
-roundf(float x)
-{
- return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
-}
-#endif
-
-#ifndef INFINITY
-#define INFINITY (DBL_MAX + DBL_MAX)
-#endif
-
-#ifndef NAN
-#define NAN (INFINITY - INFINITY)
-#endif
-
-#endif /* _MSC_VER */
-
-
-#if __STDC_VERSION__ < 199901L && (!defined(__cplusplus) || defined(_MSC_VER))
-static INLINE long int
-lrint(double d)
-{
- long int rounded = (long int)(d + 0.5);
-
- if (d - floor(d) == 0.5) {
- if (rounded % 2 != 0)
- rounded += (d > 0) ? -1 : 1;
- }
-
- return rounded;
-}
-
-static INLINE long int
-lrintf(float f)
-{
- long int rounded = (long int)(f + 0.5f);
-
- if (f - floorf(f) == 0.5f) {
- if (rounded % 2 != 0)
- rounded += (f > 0) ? -1 : 1;
- }
-
- return rounded;
-}
-
-static INLINE long long int
-llrint(double d)
-{
- long long int rounded = (long long int)(d + 0.5);
-
- if (d - floor(d) == 0.5) {
- if (rounded % 2 != 0)
- rounded += (d > 0) ? -1 : 1;
- }
-
- return rounded;
-}
-
-static INLINE long long int
-llrintf(float f)
-{
- long long int rounded = (long long int)(f + 0.5f);
-
- if (f - floorf(f) == 0.5f) {
- if (rounded % 2 != 0)
- rounded += (f > 0) ? -1 : 1;
- }
-
- return rounded;
-}
-#endif /* C99 */
-
#define POW2_TABLE_SIZE_LOG2 9
#define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
#define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
/**
* Extract the IEEE float32 exponent.
*/
-static INLINE signed
+static inline signed
util_get_float32_exponent(float x)
{
union fi f;
* Compute exp2(ipart) with i << ipart
* Compute exp2(fpart) with lookup table.
*/
-static INLINE float
+static inline float
util_fast_exp2(float x)
{
int32_t ipart;
/**
* Fast approximation to exp(x).
*/
-static INLINE float
+static inline float
util_fast_exp(float x)
{
const float k = 1.44269f; /* = log2(e) */
/**
* Fast approximation to log2(x).
*/
-static INLINE float
+static inline float
util_fast_log2(float x)
{
union fi num;
/**
* Fast approximation to x^y.
*/
-static INLINE float
+static inline float
util_fast_pow(float x, float y)
{
return util_fast_exp2(util_fast_log2(x) * y);
}
-/* Note that this counts zero as a power of two.
- */
-static INLINE boolean
-util_is_power_of_two( unsigned v )
-{
- return (v & (v-1)) == 0;
-}
-
/**
* Floor(x), returned as int.
*/
-static INLINE int
+static inline int
util_ifloor(float f)
{
int ai, bi;
/**
* Round float to nearest int.
*/
-static INLINE int
+static inline int
util_iround(float f)
{
#if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
/**
* Approximate floating point comparison
*/
-static INLINE boolean
+static inline boolean
util_is_approx(float a, float b, float tol)
{
- return fabs(b - a) <= tol;
+ return fabsf(b - a) <= tol;
}
/**
* Single-float
*/
-static INLINE boolean
+static inline boolean
util_is_inf_or_nan(float x)
{
union fi tmp;
}
-static INLINE boolean
+static inline boolean
util_is_nan(float x)
{
union fi tmp;
}
-static INLINE int
+static inline int
util_inf_sign(float x)
{
union fi tmp;
/**
* Double-float
*/
-static INLINE boolean
+static inline boolean
util_is_double_inf_or_nan(double x)
{
union di tmp;
}
-static INLINE boolean
+static inline boolean
util_is_double_nan(double x)
{
union di tmp;
}
-static INLINE int
+static inline int
util_double_inf_sign(double x)
{
union di tmp;
/**
* Half-float
*/
-static INLINE boolean
+static inline boolean
util_is_half_inf_or_nan(int16_t x)
{
return (x & 0x7c00) == 0x7c00;
}
-static INLINE boolean
+static inline boolean
util_is_half_nan(int16_t x)
{
return (x & 0x7fff) > 0x7c00;
}
-static INLINE int
+static inline int
util_half_inf_sign(int16_t x)
{
if ((x & 0x7fff) != 0x7c00) {
}
-/**
- * Find first bit set in word. Least significant bit is 1.
- * Return 0 if no bits set.
- */
-#ifndef FFS_DEFINED
-#define FFS_DEFINED 1
-
-#if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
-unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
-#pragma intrinsic(_BitScanForward)
-static INLINE
-unsigned long ffs( unsigned long u )
-{
- unsigned long i;
- if (_BitScanForward(&i, u))
- return i + 1;
- else
- return 0;
-}
-#elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
-static INLINE
-unsigned ffs( unsigned u )
-{
- unsigned i;
-
- if (u == 0) {
- return 0;
- }
-
- __asm bsf eax, [u]
- __asm inc eax
- __asm mov [i], eax
-
- return i;
-}
-#elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID)
-#define ffs __builtin_ffs
-#define ffsll __builtin_ffsll
-#endif
-
-#endif /* FFS_DEFINED */
-
-/**
- * Find last bit set in a word. The least significant bit is 1.
- * Return 0 if no bits are set.
- */
-static INLINE unsigned
-util_last_bit(unsigned u)
-{
-#if defined(__GNUC__)
- return u == 0 ? 0 : 32 - __builtin_clz(u);
-#else
- unsigned r = 0;
- while (u) {
- r++;
- u >>= 1;
- }
- return r;
-#endif
-}
-
-/**
- * Find last bit in a word that does not match the sign bit. The least
- * significant bit is 1.
- * Return 0 if no bits are set.
- */
-static INLINE unsigned
-util_last_bit_signed(int i)
-{
- if (i >= 0)
- return util_last_bit(i);
- else
- return util_last_bit(~(unsigned)i);
-}
-
-/* Destructively loop over all of the bits in a mask as in:
- *
- * while (mymask) {
- * int i = u_bit_scan(&mymask);
- * ... process element i
- * }
- *
- */
-static INLINE int
-u_bit_scan(unsigned *mask)
-{
- int i = ffs(*mask) - 1;
- *mask &= ~(1 << i);
- return i;
-}
-
-#ifndef _MSC_VER
-static INLINE int
-u_bit_scan64(uint64_t *mask)
-{
- int i = ffsll(*mask) - 1;
- *mask &= ~(1llu << i);
- return i;
-}
-#endif
-
/**
* Return float bits.
*/
-static INLINE unsigned
+static inline unsigned
fui( float f )
{
union fi fi;
return fi.ui;
}
-static INLINE float
+static inline float
uif(uint32_t ui)
{
union fi fi;
/**
* Convert ubyte to float in [0, 1].
- * XXX a 256-entry lookup table would be slightly faster.
*/
-static INLINE float
+static inline float
ubyte_to_float(ubyte ub)
{
return (float) ub * (1.0f / 255.0f);
/**
* Convert float in [0,1] to ubyte in [0,255] with clamping.
*/
-static INLINE ubyte
+static inline ubyte
float_to_ubyte(float f)
{
- union fi tmp;
-
- tmp.f = f;
- if (tmp.i < 0) {
+ /* return 0 for NaN too */
+ if (!(f > 0.0f)) {
return (ubyte) 0;
}
- else if (tmp.i >= 0x3f800000 /* 1.0f */) {
+ else if (f >= 1.0f) {
return (ubyte) 255;
}
else {
+ union fi tmp;
+ tmp.f = f;
tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
return (ubyte) tmp.i;
}
}
-static INLINE float
+static inline float
byte_to_float_tex(int8_t b)
{
return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
}
-static INLINE int8_t
+static inline int8_t
float_to_byte_tex(float f)
{
return (int8_t) (127.0F * f);
/**
* Calc log base 2
*/
-static INLINE unsigned
+static inline unsigned
util_logbase2(unsigned n)
{
-#if defined(PIPE_CC_GCC)
+#if defined(HAVE___BUILTIN_CLZ)
return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
#else
unsigned pos = 0;
#endif
}
+static inline uint64_t
+util_logbase2_64(uint64_t n)
+{
+#if defined(HAVE___BUILTIN_CLZLL)
+ return ((sizeof(uint64_t) * 8 - 1) - __builtin_clzll(n | 1));
+#else
+ uint64_t pos = 0ull;
+ if (n >= 1ull<<32) { n >>= 32; pos += 32; }
+ if (n >= 1ull<<16) { n >>= 16; pos += 16; }
+ if (n >= 1ull<< 8) { n >>= 8; pos += 8; }
+ if (n >= 1ull<< 4) { n >>= 4; pos += 4; }
+ if (n >= 1ull<< 2) { n >>= 2; pos += 2; }
+ if (n >= 1ull<< 1) { pos += 1; }
+ return pos;
+#endif
+}
+
+/**
+ * Returns the ceiling of log n base 2, and 0 when n == 0. Equivalently,
+ * returns the smallest x such that n <= 2**x.
+ */
+static inline unsigned
+util_logbase2_ceil(unsigned n)
+{
+ if (n <= 1)
+ return 0;
+
+ return 1 + util_logbase2(n - 1);
+}
+
+static inline uint64_t
+util_logbase2_ceil64(uint64_t n)
+{
+ if (n <= 1)
+ return 0;
+
+ return 1ull + util_logbase2_64(n - 1);
+}
/**
* Returns the smallest power of two >= x
*/
-static INLINE unsigned
+static inline unsigned
util_next_power_of_two(unsigned x)
{
-#if defined(PIPE_CC_GCC)
+#if defined(HAVE___BUILTIN_CLZ)
if (x <= 1)
return 1;
if (x <= 1)
return 1;
- if (util_is_power_of_two(x))
+ if (util_is_power_of_two_or_zero(x))
return x;
val--;
#endif
}
+static inline uint64_t
+util_next_power_of_two64(uint64_t x)
+{
+#if defined(HAVE___BUILTIN_CLZLL)
+ if (x <= 1)
+ return 1;
+
+ return (1ull << ((sizeof(uint64_t) * 8) - __builtin_clzll(x - 1)));
+#else
+ uint64_t val = x;
+
+ if (x <= 1)
+ return 1;
+
+ if (util_is_power_of_two_or_zero64(x))
+ return x;
+
+ val--;
+ val = (val >> 1) | val;
+ val = (val >> 2) | val;
+ val = (val >> 4) | val;
+ val = (val >> 8) | val;
+ val = (val >> 16) | val;
+ val = (val >> 32) | val;
+ val++;
+ return val;
+#endif
+}
+
/**
* Return number of bits set in n.
*/
-static INLINE unsigned
+static inline unsigned
util_bitcount(unsigned n)
{
-#if defined(PIPE_CC_GCC)
+#if defined(HAVE___BUILTIN_POPCOUNT)
return __builtin_popcount(n);
#else
/* K&R classic bitcount.
* Requires only one iteration per set bit, instead of
* one iteration per bit less than highest set bit.
*/
- unsigned bits = 0;
- for (bits; n; bits++) {
+ unsigned bits;
+ for (bits = 0; n; bits++) {
n &= n - 1;
}
return bits;
}
-static INLINE unsigned
+static inline unsigned
util_bitcount64(uint64_t n)
{
#ifdef HAVE___BUILTIN_POPCOUNTLL
* Algorithm taken from:
* http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
*/
-static INLINE unsigned
+static inline unsigned
util_bitreverse(unsigned n)
{
n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
/**
* Reverse byte order of a 32 bit word.
*/
-static INLINE uint32_t
+static inline uint32_t
util_bswap32(uint32_t n)
{
-/* We need the gcc version checks for non-autoconf build system */
-#if defined(HAVE___BUILTIN_BSWAP32) || (defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403))
+#if defined(HAVE___BUILTIN_BSWAP32)
return __builtin_bswap32(n);
#else
return (n >> 24) |
/**
* Reverse byte order of a 64bit word.
*/
-static INLINE uint64_t
+static inline uint64_t
util_bswap64(uint64_t n)
{
#if defined(HAVE___BUILTIN_BSWAP64)
/**
* Reverse byte order of a 16 bit word.
*/
-static INLINE uint16_t
+static inline uint16_t
util_bswap16(uint16_t n)
{
return (n >> 8) |
(n << 8);
}
-static INLINE void*
+static inline void*
util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
{
#ifdef PIPE_ARCH_BIG_ENDIAN
/**
* Clamp X to [MIN, MAX].
* This is a macro to allow float, int, uint, etc. types.
+ * We arbitrarily turn NaN into MIN.
*/
-#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
+#define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) )
#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
/**
* Align a value, only works pot alignemnts.
*/
-static INLINE int
+static inline int
align(int value, int alignment)
{
return (value + alignment - 1) & ~(alignment - 1);
}
+static inline uint64_t
+align64(uint64_t value, unsigned alignment)
+{
+ return (value + alignment - 1) & ~((uint64_t)alignment - 1);
+}
+
/**
* Works like align but on npot alignments.
*/
-static INLINE size_t
+static inline size_t
util_align_npot(size_t value, size_t alignment)
{
if (value % alignment)
return value;
}
-static INLINE unsigned
+static inline unsigned
u_minify(unsigned value, unsigned levels)
{
return MAX2(1, value >> levels);
#endif
-static INLINE uint32_t
+static inline uint32_t
util_unsigned_fixed(float value, unsigned frac_bits)
{
return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
}
-static INLINE int32_t
+static inline int32_t
util_signed_fixed(float value, unsigned frac_bits)
{
return (int32_t)(value * (1<<frac_bits));