[Mesa-dev] [PATCH] mesa: remove SQRTF, use sqrtf. Convert INV_SQRT() to inline function.

Brian Paul brian.e.paul at gmail.com
Mon Sep 3 07:12:26 PDT 2012


From: Brian Paul <brianp at vmware.com>

We were already defining sqrtf where we don't have the C99 version.
---
 src/mesa/main/imports.h        |   22 ++++++++++------------
 src/mesa/main/light.c          |    2 +-
 src/mesa/main/macros.h         |    4 ++--
 src/mesa/math/m_matrix.c       |    2 +-
 src/mesa/swrast/s_aalinetemp.h |    2 +-
 src/mesa/swrast/s_span.c       |    4 ++--
 src/mesa/swrast/s_texfilter.c  |    4 ++--
 src/mesa/tnl/t_rasterpos.c     |    2 +-
 8 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index 3bb7bb1..2635ce5 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -96,18 +96,6 @@ typedef union { GLfloat f; GLint i; } fi_type;
 #define DEG2RAD (M_PI/180.0)
 
 
-/***
- *** SQRTF: single-precision square root
- ***/
-#define SQRTF(X)  (float) sqrt((float) (X))
-
-
-/***
- *** INV_SQRTF: single-precision inverse square root
- ***/
-#define INV_SQRTF(X) (1.0F / SQRTF(X))
-
-
 /**
  * \name Work-arounds for platforms that lack C99 math functions
  */
@@ -156,6 +144,16 @@ static inline int isblank(int ch) { return ch == ' ' || ch == '\t'; }
 #endif
 /*@}*/
 
+
+/** single-precision inverse square root */
+static inline float
+INV_SQRTF(float x)
+{
+   /* XXX we could try Quake's fast inverse square root function here */
+   return 1.0F / sqrtf(x);
+}
+
+
 /***
  *** LOG2: Log base 2 of float
  ***/
diff --git a/src/mesa/main/light.c b/src/mesa/main/light.c
index d6fbcd9..e614574 100644
--- a/src/mesa/main/light.c
+++ b/src/mesa/main/light.c
@@ -1023,7 +1023,7 @@ update_modelview_scale( struct gl_context *ctx )
       if (ctx->_NeedEyeCoords)
 	 ctx->_ModelViewInvScale = (GLfloat) INV_SQRTF(f);
       else
-	 ctx->_ModelViewInvScale = (GLfloat) SQRTF(f);
+	 ctx->_ModelViewInvScale = (GLfloat) sqrtf(f);
    }
 }
 
diff --git a/src/mesa/main/macros.h b/src/mesa/main/macros.h
index 7d0a375..fc6f2a2 100644
--- a/src/mesa/main/macros.h
+++ b/src/mesa/main/macros.h
@@ -665,13 +665,13 @@ LEN_SQUARED_2FV(const GLfloat v[2])
 static inline GLfloat
 LEN_3FV(const GLfloat v[3])
 {
-   return SQRTF(LEN_SQUARED_3FV(v));
+   return sqrtf(LEN_SQUARED_3FV(v));
 }
 
 static inline GLfloat
 LEN_2FV(const GLfloat v[2])
 {
-   return SQRTF(LEN_SQUARED_2FV(v));
+   return sqrtf(LEN_SQUARED_2FV(v));
 }
 
 
diff --git a/src/mesa/math/m_matrix.c b/src/mesa/math/m_matrix.c
index 00a6c81..b9f22d7 100644
--- a/src/mesa/math/m_matrix.c
+++ b/src/mesa/math/m_matrix.c
@@ -856,7 +856,7 @@ _math_matrix_rotate( GLmatrix *mat,
    }
 
    if (!optimized) {
-      const GLfloat mag = SQRTF(x * x + y * y + z * z);
+      const GLfloat mag = sqrtf(x * x + y * y + z * z);
 
       if (mag <= 1.0e-4) {
          /* no rotation, leave mat as-is */
diff --git a/src/mesa/swrast/s_aalinetemp.h b/src/mesa/swrast/s_aalinetemp.h
index a517fb6..6cfd3bc 100644
--- a/src/mesa/swrast/s_aalinetemp.h
+++ b/src/mesa/swrast/s_aalinetemp.h
@@ -118,7 +118,7 @@ NAME(line)(struct gl_context *ctx, const SWvertex *v0, const SWvertex *v1)
    line.y1 = v1->attrib[FRAG_ATTRIB_WPOS][1];
    line.dx = line.x1 - line.x0;
    line.dy = line.y1 - line.y0;
-   line.len = SQRTF(line.dx * line.dx + line.dy * line.dy);
+   line.len = sqrtf(line.dx * line.dx + line.dy * line.dy);
    line.halfWidth = 0.5F * CLAMP(ctx->Line.Width,
                                  ctx->Const.MinLineWidthAA,
                                  ctx->Const.MaxLineWidthAA);
diff --git a/src/mesa/swrast/s_span.c b/src/mesa/swrast/s_span.c
index ef51479..ff653a6 100644
--- a/src/mesa/swrast/s_span.c
+++ b/src/mesa/swrast/s_span.c
@@ -419,8 +419,8 @@ _swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
    GLfloat dvdx = texH * ((t + dtdx) / (q + dqdx) - t * invQ);
    GLfloat dudy = texW * ((s + dsdy) / (q + dqdy) - s * invQ);
    GLfloat dvdy = texH * ((t + dtdy) / (q + dqdy) - t * invQ);
-   GLfloat x = SQRTF(dudx * dudx + dvdx * dvdx);
-   GLfloat y = SQRTF(dudy * dudy + dvdy * dvdy);
+   GLfloat x = sqrtf(dudx * dudx + dvdx * dvdx);
+   GLfloat y = sqrtf(dudy * dudy + dvdy * dvdy);
    GLfloat rho = MAX2(x, y);
    GLfloat lambda = LOG2(rho);
    return lambda;
diff --git a/src/mesa/swrast/s_texfilter.c b/src/mesa/swrast/s_texfilter.c
index d116a05..152b1ff 100644
--- a/src/mesa/swrast/s_texfilter.c
+++ b/src/mesa/swrast/s_texfilter.c
@@ -1795,12 +1795,12 @@ sample_2d_footprint(struct gl_context *ctx,
 
    /*  Calculate the per anisotropic sample offsets in s,t space. */
    if (Px2 > Py2) {
-      numSamples = ceil(SQRTF(Px2));
+      numSamples = ceil(sqrtf(Px2));
       ds = ux / ((GLfloat) img->Width2);
       dt = vx / ((GLfloat) img->Height2);
    }
    else {
-      numSamples = ceil(SQRTF(Py2));
+      numSamples = ceil(sqrtf(Py2));
       ds = uy / ((GLfloat) img->Width2);
       dt = vy / ((GLfloat) img->Height2);
    }
diff --git a/src/mesa/tnl/t_rasterpos.c b/src/mesa/tnl/t_rasterpos.c
index a28ad0d..1673b81 100644
--- a/src/mesa/tnl/t_rasterpos.c
+++ b/src/mesa/tnl/t_rasterpos.c
@@ -429,7 +429,7 @@ _tnl_RasterPos(struct gl_context *ctx, const GLfloat vObj[4])
          ctx->Current.RasterDistance = ctx->Current.Attrib[VERT_ATTRIB_FOG][0];
       else
          ctx->Current.RasterDistance =
-                        SQRTF( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
+                        sqrtf( eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2] );
 
       /* compute transformed normal vector (for lighting or texgen) */
       if (ctx->_NeedEyeCoords) {
-- 
1.7.4.1



More information about the mesa-dev mailing list