[Mesa-dev] [PATCH 19/28] replace IROUND with util functions

Dylan Baker dylan at pnwbakers.com
Fri Nov 9 18:40:12 UTC 2018


This adds two new util functions to rounding.h, _mesa_iroundf and
mesa_lround, which are just wrappers around roundf and round, that cast
to int and long int respectively. This is possible since mesa recently
dropped support for VC2013, since 2015 and 2017 support roundf.
---
 src/mesa/main/drawpix.c     | 18 +++++++--------
 src/mesa/main/eval.c        | 15 ++++++-------
 src/mesa/main/get.c         | 44 ++++++++++++++++++-------------------
 src/mesa/main/imports.h     | 17 --------------
 src/mesa/main/light.c       |  9 ++++----
 src/mesa/main/pixel.c       |  3 ++-
 src/mesa/main/pixelstore.c  |  5 +++--
 src/mesa/main/samplerobj.c  |  8 +++----
 src/mesa/main/texparam.c    |  8 +++----
 src/mesa/swrast/s_blit.c    |  2 +-
 src/mesa/swrast/s_context.h |  3 ++-
 src/util/rounding.h         | 10 +++++++++
 12 files changed, 68 insertions(+), 74 deletions(-)

diff --git a/src/mesa/main/drawpix.c b/src/mesa/main/drawpix.c
index b62a22d68f2..a5549d811aa 100644
--- a/src/mesa/main/drawpix.c
+++ b/src/mesa/main/drawpix.c
@@ -23,7 +23,6 @@
  */
 
 #include "glheader.h"
-#include "imports.h"
 #include "draw_validate.h"
 #include "bufferobj.h"
 #include "context.h"
@@ -37,6 +36,7 @@
 #include "glformats.h"
 #include "fbobject.h"
 #include "util/u_math.h"
+#include "util/rounding.h"
 
 
 /*
@@ -58,8 +58,8 @@ _mesa_DrawPixels( GLsizei width, GLsizei height,
                   _mesa_enum_to_string(type),
                   pixels,
                   _mesa_enum_to_string(ctx->DrawBuffer->ColorDrawBuffer[0]),
-                  IROUND(ctx->Current.RasterPos[0]),
-                  IROUND(ctx->Current.RasterPos[1]));
+                  _mesa_iroundf(ctx->Current.RasterPos[0]),
+                  _mesa_iroundf(ctx->Current.RasterPos[1]));
 
 
    if (width < 0 || height < 0) {
@@ -141,8 +141,8 @@ _mesa_DrawPixels( GLsizei width, GLsizei height,
    if (ctx->RenderMode == GL_RENDER) {
       if (width > 0 && height > 0) {
          /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
-         GLint x = IROUND(ctx->Current.RasterPos[0]);
-         GLint y = IROUND(ctx->Current.RasterPos[1]);
+         GLint x = _mesa_iroundf(ctx->Current.RasterPos[0]);
+         GLint y = _mesa_iroundf(ctx->Current.RasterPos[1]);
 
          if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
             /* unpack from PBO */
@@ -202,8 +202,8 @@ _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
                   _mesa_enum_to_string(type),
                   _mesa_enum_to_string(ctx->ReadBuffer->ColorReadBuffer),
                   _mesa_enum_to_string(ctx->DrawBuffer->ColorDrawBuffer[0]),
-                  IROUND(ctx->Current.RasterPos[0]),
-                  IROUND(ctx->Current.RasterPos[1]));
+                  _mesa_iroundf(ctx->Current.RasterPos[0]),
+                  _mesa_iroundf(ctx->Current.RasterPos[1]));
 
    if (width < 0 || height < 0) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
@@ -265,8 +265,8 @@ _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
    if (ctx->RenderMode == GL_RENDER) {
       /* Round to satisfy conformance tests (matches SGI's OpenGL) */
       if (width > 0 && height > 0) {
-         GLint destx = IROUND(ctx->Current.RasterPos[0]);
-         GLint desty = IROUND(ctx->Current.RasterPos[1]);
+         GLint destx = _mesa_iroundf(ctx->Current.RasterPos[0]);
+         GLint desty = _mesa_iroundf(ctx->Current.RasterPos[1]);
          ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
                                  type );
       }
diff --git a/src/mesa/main/eval.c b/src/mesa/main/eval.c
index 86c8f75a1d2..fd24cb541fd 100644
--- a/src/mesa/main/eval.c
+++ b/src/mesa/main/eval.c
@@ -38,7 +38,6 @@
 
 
 #include "glheader.h"
-#include "imports.h"
 #include "context.h"
 #include "eval.h"
 #include "macros.h"
@@ -704,7 +703,7 @@ _mesa_GetnMapivARB( GLenum target, GLenum query, GLsizei bufSize, GLint *v )
             if (bufSize < numBytes)
                goto overflow;
 	    for (i=0;i<n;i++) {
-	       v[i] = IROUND(data[i]);
+	       v[i] = _mesa_iroundf(data[i]);
 	    }
 	 }
          break;
@@ -728,17 +727,17 @@ _mesa_GetnMapivARB( GLenum target, GLenum query, GLsizei bufSize, GLint *v )
             numBytes = 2 * sizeof *v;
             if (bufSize < numBytes)
                goto overflow;
-            v[0] = IROUND(map1d->u1);
-            v[1] = IROUND(map1d->u2);
+            v[0] = _mesa_iroundf(map1d->u1);
+            v[1] = _mesa_iroundf(map1d->u2);
          }
          else {
             numBytes = 4 * sizeof *v;
             if (bufSize < numBytes)
                goto overflow;
-            v[0] = IROUND(map2d->u1);
-            v[1] = IROUND(map2d->u2);
-            v[2] = IROUND(map2d->v1);
-            v[3] = IROUND(map2d->v2);
+            v[0] = _mesa_iroundf(map2d->u1);
+            v[1] = _mesa_iroundf(map2d->u2);
+            v[2] = _mesa_iroundf(map2d->v1);
+            v[3] = _mesa_iroundf(map2d->v2);
          }
          break;
       default:
diff --git a/src/mesa/main/get.c b/src/mesa/main/get.c
index 1b1679e8bf7..3761b2db999 100644
--- a/src/mesa/main/get.c
+++ b/src/mesa/main/get.c
@@ -1853,18 +1853,18 @@ _mesa_GetIntegerv(GLenum pname, GLint *params)
       break;
 
    case TYPE_FLOAT_8:
-      params[7] = IROUND(((GLfloat *) p)[7]);
-      params[6] = IROUND(((GLfloat *) p)[6]);
-      params[5] = IROUND(((GLfloat *) p)[5]);
-      params[4] = IROUND(((GLfloat *) p)[4]);
+      params[7] = _mesa_iroundf(((GLfloat *) p)[7]);
+      params[6] = _mesa_iroundf(((GLfloat *) p)[6]);
+      params[5] = _mesa_iroundf(((GLfloat *) p)[5]);
+      params[4] = _mesa_iroundf(((GLfloat *) p)[4]);
    case TYPE_FLOAT_4:
-      params[3] = IROUND(((GLfloat *) p)[3]);
+      params[3] = _mesa_iroundf(((GLfloat *) p)[3]);
    case TYPE_FLOAT_3:
-      params[2] = IROUND(((GLfloat *) p)[2]);
+      params[2] = _mesa_iroundf(((GLfloat *) p)[2]);
    case TYPE_FLOAT_2:
-      params[1] = IROUND(((GLfloat *) p)[1]);
+      params[1] = _mesa_iroundf(((GLfloat *) p)[1]);
    case TYPE_FLOAT:
-      params[0] = IROUND(((GLfloat *) p)[0]);
+      params[0] = _mesa_iroundf(((GLfloat *) p)[0]);
       break;
 
    case TYPE_FLOATN_4:
@@ -1968,18 +1968,18 @@ _mesa_GetInteger64v(GLenum pname, GLint64 *params)
       break;
 
    case TYPE_FLOAT_8:
-      params[7] = IROUND64(((GLfloat *) p)[7]);
-      params[6] = IROUND64(((GLfloat *) p)[6]);
-      params[5] = IROUND64(((GLfloat *) p)[5]);
-      params[4] = IROUND64(((GLfloat *) p)[4]);
+      params[7] = lround(((GLfloat *) p)[7]);
+      params[6] = lround(((GLfloat *) p)[6]);
+      params[5] = lround(((GLfloat *) p)[5]);
+      params[4] = lround(((GLfloat *) p)[4]);
    case TYPE_FLOAT_4:
-      params[3] = IROUND64(((GLfloat *) p)[3]);
+      params[3] = lround(((GLfloat *) p)[3]);
    case TYPE_FLOAT_3:
-      params[2] = IROUND64(((GLfloat *) p)[2]);
+      params[2] = lround(((GLfloat *) p)[2]);
    case TYPE_FLOAT_2:
-      params[1] = IROUND64(((GLfloat *) p)[1]);
+      params[1] = lround(((GLfloat *) p)[1]);
    case TYPE_FLOAT:
-      params[0] = IROUND64(((GLfloat *) p)[0]);
+      params[0] = lround(((GLfloat *) p)[0]);
       break;
 
    case TYPE_FLOATN_4:
@@ -2745,22 +2745,22 @@ _mesa_GetIntegeri_v( GLenum pname, GLuint index, GLint *params )
    switch (type) {
    case TYPE_FLOAT_4:
    case TYPE_FLOATN_4:
-      params[3] = IROUND(v.value_float_4[3]);
+      params[3] = _mesa_iroundf(v.value_float_4[3]);
    case TYPE_FLOAT_3:
    case TYPE_FLOATN_3:
-      params[2] = IROUND(v.value_float_4[2]);
+      params[2] = _mesa_iroundf(v.value_float_4[2]);
    case TYPE_FLOAT_2:
    case TYPE_FLOATN_2:
-      params[1] = IROUND(v.value_float_4[1]);
+      params[1] = _mesa_iroundf(v.value_float_4[1]);
    case TYPE_FLOAT:
    case TYPE_FLOATN:
-      params[0] = IROUND(v.value_float_4[0]);
+      params[0] = _mesa_iroundf(v.value_float_4[0]);
       break;
 
    case TYPE_DOUBLEN_2:
-      params[1] = IROUND(v.value_double_2[1]);
+      params[1] = _mesa_iroundf(v.value_double_2[1]);
    case TYPE_DOUBLEN:
-      params[0] = IROUND(v.value_double_2[0]);
+      params[0] = _mesa_iroundf(v.value_double_2[0]);
       break;
 
    case TYPE_INT:
diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index 7d210fd752e..b2f6fe22bc9 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -107,23 +107,6 @@ typedef union { GLfloat f; GLint i; GLuint u; } fi_type;
 #endif
 
 
-/**
- * Convert float to int by rounding to nearest integer, away from zero.
- */
-static inline int IROUND(float f)
-{
-   return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
-}
-
-/**
- * Convert float to int64 by rounding to nearest integer.
- */
-static inline GLint64 IROUND64(float f)
-{
-   return (GLint64) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
-}
-
-
 /**********************************************************************
  * Functions
  */
diff --git a/src/mesa/main/light.c b/src/mesa/main/light.c
index 67faf8a1452..38cc4b1d48c 100644
--- a/src/mesa/main/light.c
+++ b/src/mesa/main/light.c
@@ -26,7 +26,6 @@
 
 #include "c99_math.h"
 #include "glheader.h"
-#include "imports.h"
 #include "context.h"
 #include "enums.h"
 #include "light.h"
@@ -857,12 +856,12 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
          params[3] = FLOAT_TO_INT( mat[MAT_ATTRIB_EMISSION(f)][3] );
 	 break;
       case GL_SHININESS:
-         *params = IROUND( mat[MAT_ATTRIB_SHININESS(f)][0] );
+         *params = _mesa_iroundf( mat[MAT_ATTRIB_SHININESS(f)][0] );
 	 break;
       case GL_COLOR_INDEXES:
-	 params[0] = IROUND( mat[MAT_ATTRIB_INDEXES(f)][0] );
-	 params[1] = IROUND( mat[MAT_ATTRIB_INDEXES(f)][1] );
-	 params[2] = IROUND( mat[MAT_ATTRIB_INDEXES(f)][2] );
+	 params[0] = _mesa_iroundf( mat[MAT_ATTRIB_INDEXES(f)][0] );
+	 params[1] = _mesa_iroundf( mat[MAT_ATTRIB_INDEXES(f)][1] );
+	 params[2] = _mesa_iroundf( mat[MAT_ATTRIB_INDEXES(f)][2] );
 	 break;
       default:
          _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
diff --git a/src/mesa/main/pixel.c b/src/mesa/main/pixel.c
index 0c707361364..1a04be16e92 100644
--- a/src/mesa/main/pixel.c
+++ b/src/mesa/main/pixel.c
@@ -28,6 +28,7 @@
  * Pixel transfer functions (glPixelZoom, glPixelMap, glPixelTransfer)
  */
 
+#include "c99_math.h"
 #include "glheader.h"
 #include "bufferobj.h"
 #include "context.h"
@@ -113,7 +114,7 @@ store_pixelmap(struct gl_context *ctx, GLenum map, GLsizei mapsize,
       /* special case */
       ctx->PixelMaps.StoS.Size = mapsize;
       for (i = 0; i < mapsize; i++) {
-         ctx->PixelMaps.StoS.Map[i] = (GLfloat)IROUND(values[i]);
+         ctx->PixelMaps.StoS.Map[i] = roundf(values[i]);
       }
       break;
    case GL_PIXEL_MAP_I_TO_I:
diff --git a/src/mesa/main/pixelstore.c b/src/mesa/main/pixelstore.c
index 6123da92278..033a6f7740b 100644
--- a/src/mesa/main/pixelstore.c
+++ b/src/mesa/main/pixelstore.c
@@ -33,6 +33,7 @@
 #include "context.h"
 #include "pixelstore.h"
 #include "mtypes.h"
+#include "util/rounding.h"
 
 
 static ALWAYS_INLINE void
@@ -234,7 +235,7 @@ _mesa_PixelStorei(GLenum pname, GLint param)
 void GLAPIENTRY
 _mesa_PixelStoref(GLenum pname, GLfloat param)
 {
-   _mesa_PixelStorei(pname, IROUND(param));
+   _mesa_PixelStorei(pname, _mesa_iroundf(param));
 }
 
 
@@ -248,7 +249,7 @@ _mesa_PixelStorei_no_error(GLenum pname, GLint param)
 void GLAPIENTRY
 _mesa_PixelStoref_no_error(GLenum pname, GLfloat param)
 {
-   _mesa_PixelStorei_no_error(pname, IROUND(param));
+   _mesa_PixelStorei_no_error(pname, _mesa_iroundf(param));
 }
 
 
diff --git a/src/mesa/main/samplerobj.c b/src/mesa/main/samplerobj.c
index 6656cf5022a..be7e8b39142 100644
--- a/src/mesa/main/samplerobj.c
+++ b/src/mesa/main/samplerobj.c
@@ -1421,19 +1421,19 @@ _mesa_GetSamplerParameteriv(GLuint sampler, GLenum pname, GLint *params)
       /* GL spec 'Data Conversions' section specifies that floating-point
        * value in integer Get function is rounded to nearest integer
        */
-      *params = IROUND(sampObj->MinLod);
+      *params = _mesa_iroundf(sampObj->MinLod);
       break;
    case GL_TEXTURE_MAX_LOD:
       /* GL spec 'Data Conversions' section specifies that floating-point
        * value in integer Get function is rounded to nearest integer
        */
-      *params = IROUND(sampObj->MaxLod);
+      *params = _mesa_iroundf(sampObj->MaxLod);
       break;
    case GL_TEXTURE_LOD_BIAS:
       /* GL spec 'Data Conversions' section specifies that floating-point
        * value in integer Get function is rounded to nearest integer
        */
-      *params = IROUND(sampObj->LodBias);
+      *params = _mesa_iroundf(sampObj->LodBias);
       break;
    case GL_TEXTURE_COMPARE_MODE:
       *params = sampObj->CompareMode;
@@ -1445,7 +1445,7 @@ _mesa_GetSamplerParameteriv(GLuint sampler, GLenum pname, GLint *params)
       /* GL spec 'Data Conversions' section specifies that floating-point
        * value in integer Get function is rounded to nearest integer
        */
-      *params = IROUND(sampObj->MaxAnisotropy);
+      *params = _mesa_iroundf(sampObj->MaxAnisotropy);
       break;
    case GL_TEXTURE_BORDER_COLOR:
       params[0] = FLOAT_TO_INT(sampObj->BorderColor.f[0]);
diff --git a/src/mesa/main/texparam.c b/src/mesa/main/texparam.c
index a3ec7241986..b759d35ee75 100644
--- a/src/mesa/main/texparam.c
+++ b/src/mesa/main/texparam.c
@@ -2116,7 +2116,7 @@ get_tex_parameteriv(struct gl_context *ctx,
          /* GL spec 'Data Conversions' section specifies that floating-point
           * value in integer Get function is rounded to nearest integer
           */
-         *params = IROUND(obj->Sampler.MinLod);
+         *params = _mesa_iroundf(obj->Sampler.MinLod);
          break;
       case GL_TEXTURE_MAX_LOD:
          if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
@@ -2124,7 +2124,7 @@ get_tex_parameteriv(struct gl_context *ctx,
          /* GL spec 'Data Conversions' section specifies that floating-point
           * value in integer Get function is rounded to nearest integer
           */
-         *params = IROUND(obj->Sampler.MaxLod);
+         *params = _mesa_iroundf(obj->Sampler.MaxLod);
          break;
       case GL_TEXTURE_BASE_LEVEL:
          if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
@@ -2141,7 +2141,7 @@ get_tex_parameteriv(struct gl_context *ctx,
          /* GL spec 'Data Conversions' section specifies that floating-point
           * value in integer Get function is rounded to nearest integer
           */
-         *params = IROUND(obj->Sampler.MaxAnisotropy);
+         *params = _mesa_iroundf(obj->Sampler.MaxAnisotropy);
          break;
       case GL_GENERATE_MIPMAP_SGIS:
          if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
@@ -2179,7 +2179,7 @@ get_tex_parameteriv(struct gl_context *ctx,
          /* GL spec 'Data Conversions' section specifies that floating-point
           * value in integer Get function is rounded to nearest integer
           */
-         *params = IROUND(obj->Sampler.LodBias);
+         *params = _mesa_iroundf(obj->Sampler.LodBias);
          break;
       case GL_TEXTURE_CROP_RECT_OES:
          if (ctx->API != API_OPENGLES || !ctx->Extensions.OES_draw_texture)
diff --git a/src/mesa/swrast/s_blit.c b/src/mesa/swrast/s_blit.c
index 8c06013d785..fa03f7830f0 100644
--- a/src/mesa/swrast/s_blit.c
+++ b/src/mesa/swrast/s_blit.c
@@ -298,7 +298,7 @@ blit_nearest(struct gl_context *ctx,
 
       for (dstRow = 0; dstRow < dstHeight; dstRow++) {
          GLfloat srcRowF = (dstRow + 0.5F) / dstHeight * srcHeight - 0.5F;
-         GLint srcRow = IROUND(srcRowF);
+         GLint srcRow = _mesa_iroundf(srcRowF);
          GLubyte *dstRowStart = dstMap + dstRowStride * dstRow;
 
          assert(srcRow >= 0);
diff --git a/src/mesa/swrast/s_context.h b/src/mesa/swrast/s_context.h
index 49c30a3de3b..7b7b43daf08 100644
--- a/src/mesa/swrast/s_context.h
+++ b/src/mesa/swrast/s_context.h
@@ -49,6 +49,7 @@
 #include "swrast.h"
 #include "s_fragprog.h"
 #include "s_span.h"
+#include "util/rounding.h"
 
 
 typedef void (*texture_sample_func)(struct gl_context *ctx,
@@ -433,7 +434,7 @@ _swrast_unmap_renderbuffers(struct gl_context *ctx);
 #define FIXED_EPSILON   1
 #define FIXED_SCALE     ((float) FIXED_ONE)
 #define FIXED_DBL_SCALE ((double) FIXED_ONE)
-#define FloatToFixed(X) (IROUND((X) * FIXED_SCALE))
+#define FloatToFixed(X) (_mesa_iroundf((X) * FIXED_SCALE))
 #define FixedToDouble(X) ((X) * (1.0 / FIXED_DBL_SCALE))
 #define IntToFixed(I)   ((I) << FIXED_SHIFT)
 #define FixedToInt(X)   ((X) >> FIXED_SHIFT)
diff --git a/src/util/rounding.h b/src/util/rounding.h
index dfc691eaf13..3ffefacb54e 100644
--- a/src/util/rounding.h
+++ b/src/util/rounding.h
@@ -109,6 +109,16 @@ _mesa_lroundevenf(float x)
 #endif
 }
 
+/**
+ * \brief Rounds \c x to away from zero and returns the value as an int.
+ */
+static inline int
+_mesa_iroundf(float x)
+{
+   return (int) roundf(x);
+}
+
+
 /**
  * \brief Rounds \c x to the nearest integer, with ties to the even integer,
  * and returns the value as a long int.
-- 
2.19.1



More information about the mesa-dev mailing list