[Mesa-dev] [PATCH 4/6] mesa: convert a bunch of math macros to inline functions

Brian Paul brian.e.paul at gmail.com
Mon Aug 27 20:53:11 PDT 2012


From: Brian Paul <brianp at vmware.com>

---
 src/mesa/main/macros.h |  142 +++++++++++++++++++++++++++++-------------------
 1 files changed, 86 insertions(+), 56 deletions(-)

diff --git a/src/mesa/main/macros.h b/src/mesa/main/macros.h
index d1df2ce..411b16d 100644
--- a/src/mesa/main/macros.h
+++ b/src/mesa/main/macros.h
@@ -584,34 +584,31 @@ do {				\
 /*@}*/
 
 
-/** \name Linear interpolation macros */
+/** \name Linear interpolation functions */
 /*@{*/
 
-/**
- * Linear interpolation
- *
- * \note \p OUT argument is evaluated twice!
- * \note Be wary of using *coord++ as an argument to any of these macros!
- */
-#define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
-
-#define INTERP_F( t, dstf, outf, inf )      \
-   dstf = LINTERP( t, outf, inf )
-
-#define INTERP_4F( t, dst, out, in )        \
-do {                        \
-   dst[0] = LINTERP( (t), (out)[0], (in)[0] );  \
-   dst[1] = LINTERP( (t), (out)[1], (in)[1] );  \
-   dst[2] = LINTERP( (t), (out)[2], (in)[2] );  \
-   dst[3] = LINTERP( (t), (out)[3], (in)[3] );  \
-} while (0)
-
-#define INTERP_3F( t, dst, out, in )        \
-do {                        \
-   dst[0] = LINTERP( (t), (out)[0], (in)[0] );  \
-   dst[1] = LINTERP( (t), (out)[1], (in)[1] );  \
-   dst[2] = LINTERP( (t), (out)[2], (in)[2] );  \
-} while (0)
+static inline GLfloat
+LINTERP(GLfloat t, GLfloat out, GLfloat in)
+{
+   return out + t * (in - out);
+}
+
+static inline void
+INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3])
+{
+   dst[0] = LINTERP( t, out[0], in[0] );
+   dst[1] = LINTERP( t, out[1], in[1] );
+   dst[2] = LINTERP( t, out[2], in[2] );
+}
+
+static inline void
+INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4])
+{
+   dst[0] = LINTERP( t, out[0], in[0] );
+   dst[1] = LINTERP( t, out[1], in[1] );
+   dst[2] = LINTERP( t, out[2], in[2] );
+   dst[3] = LINTERP( t, out[3], in[3] );
+}
 
 /*@}*/
 
@@ -630,43 +627,76 @@ do {                        \
 #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))
 
-/** Dot product of two 2-element vectors */
-#define DOT2( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
-
-/** Dot product of two 3-element vectors */
-#define DOT3( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
-
-/** Dot product of two 4-element vectors */
-#define DOT4( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
-            (a)[2]*(b)[2] + (a)[3]*(b)[3] )
 
 
 /** Cross product of two 3-element vectors */
-#define CROSS3(n, u, v)             \
-do {                        \
-   (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1];  \
-   (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2];  \
-   (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0];  \
-} while (0)
+static inline void
+CROSS3(GLfloat n[3], const GLfloat u[3], const GLfloat v[3])
+{
+   n[0] = u[1] * v[2] - u[2] * v[1];
+   n[1] = u[2] * v[0] - u[0] * v[2];
+   n[2] = u[0] * v[1] - u[1] * v[0];
+}
 
 
-/* Normalize a 3-element vector to unit length. */
-#define NORMALIZE_3FV( V )          \
-do {                        \
-   GLfloat len = (GLfloat) LEN_SQUARED_3FV(V);  \
-   if (len) {                   \
-      len = INV_SQRTF(len);         \
-      (V)[0] = (GLfloat) ((V)[0] * len);    \
-      (V)[1] = (GLfloat) ((V)[1] * len);    \
-      (V)[2] = (GLfloat) ((V)[2] * len);    \
-   }                        \
-} while(0)
+/** Dot product of two 2-element vectors */
+static inline GLfloat
+DOT2(const GLfloat a[2], const GLfloat b[2])
+{
+   return a[0] * b[0] + a[1] * b[1];
+}
+
+static inline GLfloat
+DOT3(const GLfloat a[3], const GLfloat b[3])
+{
+   return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
+}
+
+static inline GLfloat
+DOT4(const GLfloat a[4], const GLfloat b[4])
+{
+   return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
+}
+
+
+static inline GLfloat
+LEN_SQUARED_3FV(const GLfloat v[3])
+{
+   return DOT3(v, v);
+}
+
+static inline GLfloat
+LEN_SQUARED_2FV(const GLfloat v[2])
+{
+   return DOT2(v, v);
+}
+
+
+static inline GLfloat
+LEN_3FV(const GLfloat v[3])
+{
+   return SQRTF(LEN_SQUARED_3FV(v));
+}
+
+static inline GLfloat
+LEN_2FV(const GLfloat v[2])
+{
+   return SQRTF(LEN_SQUARED_2FV(v));
+}
 
-#define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
-#define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
 
-#define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
-#define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
+/* Normalize a 3-element vector to unit length. */
+static inline void
+NORMALIZE_3FV(GLfloat v[3])
+{
+   GLfloat len = (GLfloat) LEN_SQUARED_3FV(v);
+   if (len) {
+      len = INV_SQRTF(len);
+      v[0] *= len;
+      v[1] *= len;
+      v[2] *= len;
+   }
+}
 
 
 /** Compute ceiling of integer quotient of A divided by B. */
-- 
1.7.4.1



More information about the mesa-dev mailing list