[Piglit] [PATCH 2/4] util/gl: Wrapper to load hex values for half floats

Andres Gomez agomez at igalia.com
Thu Jun 9 21:27:03 UTC 2016


For some cases we want to have shaders where we load an exact bit
pattern into a half float.

Signed-off-by: Andres Gomez <agomez at igalia.com>
---
 tests/util/piglit-util-gl.h | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/tests/util/piglit-util-gl.h b/tests/util/piglit-util-gl.h
index 0954ef5..b416e50 100644
--- a/tests/util/piglit-util-gl.h
+++ b/tests/util/piglit-util-gl.h
@@ -29,6 +29,9 @@
 extern "C" {
 #endif
 
+#include <errno.h>
+#include <limits.h>
+
 #include "piglit-util.h"
 
 #include <piglit/gl_wrap.h>
@@ -244,6 +247,30 @@ void piglit_draw_rect_from_arrays(const void *verts, const void *tex,
 
 unsigned short piglit_half_from_float(float val);
 
+/**
+ * Wrapper for piglit_half_from_float() which allows using an exact
+ * hex bit pattern to generate a half float value.
+ */
+static inline unsigned short
+strtohf_hex(const char *nptr, char **endptr)
+{
+	/* skip spaces and tabs */
+	while (*nptr == ' ' || *nptr == '\t')
+		nptr++;
+
+	if (strncmp(nptr, "0x", 2) == 0) {
+		uint32_t u = strtoul(nptr, endptr, 16);
+		if (u > USHRT_MAX) {
+			errno = ERANGE;
+			return USHRT_MAX;
+		} else {
+			return u;
+		}
+	} else {
+		return piglit_half_from_float(strtod_inf(nptr, endptr));
+	}
+}
+
 void piglit_escape_exit_key(unsigned char key, int x, int y);
 
 void piglit_gen_ortho_projection(double left, double right, double bottom,
-- 
2.8.1



More information about the Piglit mailing list