#include "pipe/p_compiler.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
#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
/**
* 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
+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(HAVE___BUILTIN_CLZ)
- 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;
}
}
-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(HAVE___BUILTIN_CLZ)
#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);
+}
/**
* Returns the smallest power of two >= x
*/
-static INLINE unsigned
+static inline unsigned
util_next_power_of_two(unsigned x)
{
#if defined(HAVE___BUILTIN_CLZ)
/**
* Return number of bits set in n.
*/
-static INLINE unsigned
+static inline unsigned
util_bitcount(unsigned n)
{
#if defined(HAVE___BUILTIN_POPCOUNT)
* 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)
{
#if defined(HAVE___BUILTIN_BSWAP32)
/**
* 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
/**
* 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));