[Piglit] [PATCH] util: Wrappers to load hex values for floats

Andres Gomez agomez at igalia.com
Mon Apr 25 13:53:13 UTC 2016


For some cases we want to have shaders where we load an exact bit
pattern into a float or double. We already have this in place for
uniforms. Now, the methods have been refactorized so they can be used in
VBOs too.

Signed-off-by: Andres Gomez <agomez at igalia.com>
---
 tests/shaders/shader_runner.c | 35 +++++--------------------------
 tests/util/piglit-util.h      | 48 +++++++++++++++++++++++++++++++++++++++++++
 tests/util/piglit-vbo.cpp     |  9 ++++----
 3 files changed, 58 insertions(+), 34 deletions(-)

diff --git a/tests/shaders/shader_runner.c b/tests/shaders/shader_runner.c
index 239735c..e0d3416 100644
--- a/tests/shaders/shader_runner.c
+++ b/tests/shaders/shader_runner.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <ctype.h>
 
+#include "piglit-util.h"
 #include "piglit-util-gl.h"
 #include "piglit-vbo.h"
 
@@ -1371,21 +1372,8 @@ get_floats(const char *line, float *f, unsigned count)
 {
 	unsigned i;
 
-	for (i = 0; i < count; i++) {
-		line = eat_whitespace(line);
-
-		if (strncmp(line, "0x", 2) == 0) {
-			union {
-				uint32_t u;
-				float f;
-			} x;
-
-			x.u = strtoul(line, (char **) &line, 16);
-			f[i] = x.f;
-		} else {
-			f[i] = strtod_inf(line, (char **) &line);
-		}
-	}
+	for (i = 0; i < count; i++)
+		f[i] = strtof_hex(line, (char **) &line);
 }
 
 static void
@@ -1393,21 +1381,8 @@ get_doubles(const char *line, double *d, unsigned count)
 {
 	unsigned i;
 
-	for (i = 0; i < count; i++) {
-		line = eat_whitespace(line);
-
-		if (strncmp(line, "0x", 2) == 0) {
-			union {
-				uint64_t u64;
-				double d;
-			} x;
-
-			x.u64 = strtoull(line, (char **) &line, 16);
-			d[i] = x.d;
-		} else {
-			d[i] = strtod_inf(line, (char **) &line);
-		}
-	}
+	for (i = 0; i < count; i++)
+		d[i] = strtod_hex(line, (char **) &line);
 }
 
 
diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
index 985ebbd..1e57215 100644
--- a/tests/util/piglit-util.h
+++ b/tests/util/piglit-util.h
@@ -221,6 +221,54 @@ strtod_inf(const char *nptr, char **endptr)
 	return strtod(nptr, endptr);
 }
 
+/**
+ * Wrapper for strtod_inf() which allows using an exact hex bit
+ * pattern to generate a float value.
+ */
+static inline float
+strtof_hex(const char *nptr, char **endptr)
+{
+	/* skip spaces and tabs */
+	while (*nptr == ' ' || *nptr == '\t')
+		nptr++;
+
+	if (strncmp(nptr, "0x", 2) == 0) {
+		union {
+			uint32_t u;
+			float f;
+		} x;
+
+		x.u = strtoul(nptr, endptr, 16);
+		return x.f;
+	} else {
+		return strtod_inf(nptr, endptr);
+	}
+}
+
+/**
+ * Wrapper for strtod_inf() which allows using an exact hex bit
+ * pattern to generate a double value.
+ */
+static inline double
+strtod_hex(const char *nptr, char **endptr)
+{
+	/* skip spaces and tabs */
+	while (*nptr == ' ' || *nptr == '\t')
+		nptr++;
+
+	if (strncmp(nptr, "0x", 2) == 0) {
+		union {
+			uint64_t u64;
+			double d;
+		} x;
+
+		x.u64 = strtoull(nptr, endptr, 16);
+		return x.d;
+	} else {
+		return strtod_inf(nptr, endptr);
+	}
+}
+
 #ifndef HAVE_STRCHRNUL
 static inline char *
 strchrnul(const char *s, int c)
diff --git a/tests/util/piglit-vbo.cpp b/tests/util/piglit-vbo.cpp
index 11a4adc..5147234 100644
--- a/tests/util/piglit-vbo.cpp
+++ b/tests/util/piglit-vbo.cpp
@@ -102,6 +102,7 @@
 #include <errno.h>
 #include <ctype.h>
 
+#include "piglit-util.h"
 #include "piglit-util-gl.h"
 #include "piglit-vbo.h"
 
@@ -271,16 +272,16 @@ vertex_attrib_description::parse_datum(const char **text, void *data) const
 	errno = 0;
 	switch (this->data_type) {
 	case GL_FLOAT: {
-		double value = strtod(*text, &endptr);
+		float value = strtof_hex(*text, &endptr);
 		if (errno == ERANGE) {
-			printf("Could not parse as double\n");
+			printf("Could not parse as float\n");
 			return false;
 		}
-		*((GLfloat *) data) = (float) value;
+		*((GLfloat *) data) = value;
 		break;
 	}
 	case GL_DOUBLE: {
-		double value = strtod(*text, &endptr);
+		double value = strtod_hex(*text, &endptr);
 		if (errno == ERANGE) {
 			printf("Could not parse as double\n");
 			return false;
-- 
2.8.0.rc3



More information about the Piglit mailing list