r600g: Use the actual Evergreen functions to query format support on Evergreen.
[mesa.git] / src / gallium / auxiliary / util / u_math.h
index b2969a210a7878f0daed5df6d715981139e2dc3d..0b5284428ebbd471eb2b0b50afe9c982da04eb81 100644 (file)
@@ -118,6 +118,11 @@ __inline double __cdecl atan2(double val)
 #endif
 
 
+#ifndef M_SQRT2
+#define M_SQRT2 1.41421356237309504880
+#endif
+
+
 #if defined(_MSC_VER) 
 
 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
@@ -168,7 +173,10 @@ static INLINE float logf( float f )
 #undef logf
 #define logf(x) ((float)log((double)(x)))
 #endif /* logf */
-#endif
+
+#define isfinite(x) _finite((double)(x))
+#define isnan(x) _isnan((double)(x))
+#endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
 
 static INLINE double log2( double x )
 {
@@ -176,6 +184,18 @@ static INLINE double log2( double x )
    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 /* _MSC_VER */
 
 
@@ -335,26 +355,25 @@ util_iround(float f)
 }
 
 
-
 /**
- * Test if x is NaN or +/- infinity.
+ * Approximate floating point comparison
  */
 static INLINE boolean
-util_is_inf_or_nan(float x)
+util_is_approx(float a, float b, float tol)
 {
-   union fi tmp;
-   tmp.f = x;
-   return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
+   return fabs(b - a) <= tol;
 }
 
 
 /**
- * Test whether x is a power of two.
+ * Test if x is NaN or +/- infinity.
  */
 static INLINE boolean
-util_is_pot(unsigned x)
+util_is_inf_or_nan(float x)
 {
-   return (x & (x - 1)) == 0;
+   union fi tmp;
+   tmp.f = x;
+   return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
 }
 
 
@@ -394,22 +413,6 @@ unsigned ffs( unsigned u )
 #define ffs __builtin_ffs
 #endif
 
-#ifdef __MINGW32__
-#define ffs __builtin_ffs
-#endif
-
-
-/* Could also binary search for the highest bit.
- */
-static INLINE unsigned
-util_unsigned_logbase2(unsigned n)
-{
-   unsigned log2 = 0;
-   while (n >>= 1)
-      ++log2;
-   return log2;
-}
-
 
 /**
  * Return float bits.
@@ -456,6 +459,17 @@ float_to_ubyte(float f)
    }
 }
 
+static INLINE float
+byte_to_float_tex(int8_t b)
+{
+   return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
+}
+
+static INLINE int8_t
+float_to_byte_tex(float f)
+{
+   return (int8_t) (127.0F * f);
+}
 
 /**
  * Calc log base 2
@@ -463,10 +477,17 @@ float_to_ubyte(float f)
 static INLINE unsigned
 util_logbase2(unsigned n)
 {
-   unsigned log2 = 0;
-   while (n >>= 1)
-      ++log2;
-   return log2;
+#if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
+   return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
+#else
+   unsigned pos = 0;
+   if (n >= 1<<16) { n >>= 16; pos += 16; }
+   if (n >= 1<< 8) { n >>=  8; pos +=  8; }
+   if (n >= 1<< 4) { n >>=  4; pos +=  4; }
+   if (n >= 1<< 2) { n >>=  2; pos +=  2; }
+   if (n >= 1<< 1) {           pos +=  1; }
+   return pos;
+#endif
 }
 
 
@@ -476,17 +497,29 @@ util_logbase2(unsigned n)
 static INLINE unsigned
 util_next_power_of_two(unsigned x)
 {
-   unsigned i;
+#if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
+   if (x <= 1)
+       return 1;
 
-   if (x == 0)
-      return 1;
-
-   --x;
+   return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
+#else
+   unsigned val = x;
 
-   for (i = 1; i < sizeof(unsigned) * 8; i <<= 1)
-      x |= x >> i;
+   if (x <= 1)
+      return 1;
 
-   return x + 1;
+   if (util_is_power_of_two(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++;
+   return val;
+#endif
 }
 
 
@@ -496,7 +529,7 @@ util_next_power_of_two(unsigned x)
 static INLINE unsigned
 util_bitcount(unsigned n)
 {
-#if defined(PIPE_CC_GCC)
+#if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
    return __builtin_popcount(n);
 #else
    /* K&R classic bitcount.
@@ -531,6 +564,17 @@ util_bswap32(uint32_t n)
 }
 
 
+/**
+ * Reverse byte order of a 16 bit word.
+ */
+static INLINE uint16_t
+util_bswap16(uint16_t n)
+{
+   return (n >> 8) |
+          (n << 8);
+}
+
+
 /**
  * Clamp X to [MIN, MAX].
  * This is a macro to allow float, int, uint, etc. types.
@@ -540,16 +584,33 @@ util_bswap32(uint32_t n)
 #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
 #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
 
-#define MIN3( A, B, C ) MIN2( MIN2( A, B ), C )
-#define MAX3( A, B, C ) MAX2( MAX2( A, B ), C )
+#define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
+#define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
+
+#define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
+#define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
 
 
+/**
+ * Align a value, only works pot alignemnts.
+ */
 static INLINE int
 align(int value, int alignment)
 {
    return (value + alignment - 1) & ~(alignment - 1);
 }
 
+/**
+ * Works like align but on npot alignments.
+ */
+static INLINE size_t
+util_align_npot(size_t value, size_t alignment)
+{
+   if (value % alignment)
+      return value + (alignment - (value % alignment));
+   return value;
+}
+
 static INLINE unsigned
 u_minify(unsigned value, unsigned levels)
 {