mesa: Autogenerate format_unpack.c
[mesa.git] / src / mesa / main / macros.h
index 1052f756074b10bc7274e5c3fa115d7e2b530efb..cd5f2d6f28b9b1fa38ff3f56abc5f85c2cb9cc58 100644 (file)
@@ -140,14 +140,14 @@ extern GLfloat _mesa_ubyte_to_float_color_tab[256];
  *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
  *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
  ***/
-#if defined(USE_IEEE) && !defined(DEBUG)
+#ifndef DEBUG
 /* This function/macro is sensitive to precision.  Test very carefully
  * if you change it!
  */
-#define UNCLAMPED_FLOAT_TO_UBYTE(UB, F)                                        \
+#define UNCLAMPED_FLOAT_TO_UBYTE(UB, FLT)                              \
         do {                                                           \
            fi_type __tmp;                                              \
-           __tmp.f = (F);                                              \
+           __tmp.f = (FLT);                                            \
            if (__tmp.i < 0)                                            \
               UB = (GLubyte) 0;                                                \
            else if (__tmp.i >= IEEE_ONE)                               \
@@ -157,10 +157,10 @@ extern GLfloat _mesa_ubyte_to_float_color_tab[256];
               UB = (GLubyte) __tmp.i;                                  \
            }                                                           \
         } while (0)
-#define CLAMPED_FLOAT_TO_UBYTE(UB, F)                                  \
+#define CLAMPED_FLOAT_TO_UBYTE(UB, FLT)                                        \
         do {                                                           \
            fi_type __tmp;                                              \
-           __tmp.f = (F) * (255.0F/256.0F) + 32768.0F;                 \
+           __tmp.f = (FLT) * (255.0F/256.0F) + 32768.0F;               \
            UB = (GLubyte) __tmp.i;                                     \
         } while (0)
 #else
@@ -184,6 +184,35 @@ static inline GLfloat UINT_AS_FLT(GLuint u)
    return tmp.f;
 }
 
+static inline unsigned FLT_AS_UINT(float f)
+{
+   fi_type tmp;
+   tmp.f = f;
+   return tmp.u;
+}
+
+/**
+ * Convert a floating point value to an unsigned fixed point value.
+ *
+ * \param frac_bits   The number of bits used to store the fractional part.
+ */
+static inline uint32_t
+U_FIXED(float value, uint32_t frac_bits)
+{
+   value *= (1 << frac_bits);
+   return value < 0.0f ? 0 : (uint32_t) value;
+}
+
+/**
+ * Convert a floating point value to an signed fixed point value.
+ *
+ * \param frac_bits   The number of bits used to store the fractional part.
+ */
+static inline int32_t
+S_FIXED(float value, uint32_t frac_bits)
+{
+   return (int32_t) (value * (1 << frac_bits));
+}
 /*@}*/
 
 
@@ -662,6 +691,17 @@ minify(unsigned value, unsigned levels)
     return MAX2(1, value >> levels);
 }
 
+/**
+ * Return true if the given value is a power of two.
+ *
+ * Note that this considers 0 a power of two.
+ */
+static inline bool
+is_power_of_two(unsigned value)
+{
+   return (value & (value - 1)) == 0;
+}
+
 /**
  * Align a value up to an alignment value
  *
@@ -673,8 +713,20 @@ minify(unsigned value, unsigned levels)
  *
  * \sa ROUND_DOWN_TO()
  */
-#define ALIGN(value, alignment)  (((value) + alignment - 1) & ~(alignment - 1))
+#define ALIGN(value, alignment)  (((value) + (alignment) - 1) & ~((alignment) - 1))
 
+/**
+ * Align a value down to an alignment value
+ *
+ * If \c value is not already aligned to the requested alignment value, it
+ * will be rounded down.
+ *
+ * \param value  Value to be rounded
+ * \param alignment  Alignment value to be used.  This must be a power of two.
+ *
+ * \sa ALIGN()
+ */
+#define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1))
 
 
 /** Cross product of two 3-element vectors */
@@ -747,13 +799,6 @@ NORMALIZE_3FV(GLfloat v[3])
 }
 
 
-/** Is float value negative? */
-static inline GLboolean
-IS_NEGATIVE(float x)
-{
-   return signbit(x) != 0;
-}
-
 /** Test two floats have opposite signs */
 static inline GLboolean
 DIFFERENT_SIGNS(GLfloat x, GLfloat y)
@@ -773,7 +818,11 @@ DIFFERENT_SIGNS(GLfloat x, GLfloat y)
 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
 
 /* Compute the size of an array */
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+#ifndef ARRAY_SIZE
+#  define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+#endif
 
+/* Stringify */
+#define STRINGIFY(x) #x
 
 #endif