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

Submitted by Andres Gomez on June 9, 2016, 9:27 p.m.

Details

Message ID 1465507625-10599-3-git-send-email-agomez@igalia.com
State Accepted
Commit f49a92d0291dc939dbab83e22f82cac6145c10d2
Headers show
Series "More additional tests for ARB_vertex_attrib_64bit extension" ( rev: 1 ) in Piglit

Not browsing as part of any series.

Commit Message

Andres Gomez June 9, 2016, 9:27 p.m.
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@igalia.com>
---
 tests/util/piglit-util-gl.h | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

Patch hide | download patch | download mbox

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,