[Piglit] [PATCH 4/8] util: Move the half float functions from util-gl to util

Neil Roberts nroberts at igalia.com
Wed Apr 4 22:56:51 UTC 2018


They will be equally useful on Vulkan.
---
 tests/util/piglit-util-gl.c | 90 ---------------------------------------------
 tests/util/piglit-util-gl.h | 29 ---------------
 tests/util/piglit-util.c    | 89 ++++++++++++++++++++++++++++++++++++++++++++
 tests/util/piglit-util.h    | 28 ++++++++++++++
 4 files changed, 117 insertions(+), 119 deletions(-)

diff --git a/tests/util/piglit-util-gl.c b/tests/util/piglit-util-gl.c
index 3a41a5eb0..1b978422f 100644
--- a/tests/util/piglit-util-gl.c
+++ b/tests/util/piglit-util-gl.c
@@ -296,96 +296,6 @@ piglit_set_tolerance_for_bits(int rbits, int gbits, int bbits, int abits)
 	}
 }
 
-typedef union { GLfloat f; GLint i; } fi_type;
-
-/**
- * Convert a 4-byte float to a 2-byte half float.
- * Based on code from:
- * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
- *
- * Taken over from Mesa.
- */
-unsigned short
-piglit_half_from_float(float val)
-{
-	const fi_type fi = {val};
-	const int flt_m = fi.i & 0x7fffff;
-	const int flt_e = (fi.i >> 23) & 0xff;
-	const int flt_s = (fi.i >> 31) & 0x1;
-	int s, e, m = 0;
-	unsigned short result;
-
-	/* sign bit */
-	s = flt_s;
-
-	/* handle special cases */
-	if ((flt_e == 0) && (flt_m == 0)) {
-		/* zero */
-		/* m = 0; - already set */
-		e = 0;
-	}
-	else if ((flt_e == 0) && (flt_m != 0)) {
-		/* denorm -- denorm float maps to 0 half */
-		/* m = 0; - already set */
-		e = 0;
-	}
-	else if ((flt_e == 0xff) && (flt_m == 0)) {
-		/* infinity */
-		/* m = 0; - already set */
-		e = 31;
-	}
-	else if ((flt_e == 0xff) && (flt_m != 0)) {
-		/* NaN */
-		m = 1;
-		e = 31;
-	}
-	else {
-		/* regular number */
-		const int new_exp = flt_e - 127;
-		if (new_exp < -24) {
-			/* this maps to 0 */
-			/* m = 0; - already set */
-			e = 0;
-		}
-		else if (new_exp < -14) {
-			/* this maps to a denorm */
-			/* 2^-exp_val*/
-			unsigned int exp_val = (unsigned int) (-14 - new_exp);
-
-			e = 0;
-			switch (exp_val) {
-			case 0:
-				/* m = 0; - already set */
-				break;
-			case 1: m = 512 + (flt_m >> 14); break;
-			case 2: m = 256 + (flt_m >> 15); break;
-			case 3: m = 128 + (flt_m >> 16); break;
-			case 4: m = 64 + (flt_m >> 17); break;
-			case 5: m = 32 + (flt_m >> 18); break;
-			case 6: m = 16 + (flt_m >> 19); break;
-			case 7: m = 8 + (flt_m >> 20); break;
-			case 8: m = 4 + (flt_m >> 21); break;
-			case 9: m = 2 + (flt_m >> 22); break;
-			case 10: m = 1; break;
-			}
-		}
-		else if (new_exp > 15) {
-			/* map this value to infinity */
-			/* m = 0; - already set */
-			e = 31;
-		}
-		else {
-			/* regular */
-			e = new_exp + 15;
-			m = flt_m >> 13;
-		}
-	}
-
-	result = (s << 15) | (e << 10) | m;
-	return result;
-}
-
-
 /**
  * Return block size info for a specific texture compression format.
  * \param  bw returns the block width, in pixels
diff --git a/tests/util/piglit-util-gl.h b/tests/util/piglit-util-gl.h
index 7b1cee564..db0b1b60d 100644
--- a/tests/util/piglit-util-gl.h
+++ b/tests/util/piglit-util-gl.h
@@ -29,9 +29,6 @@
 extern "C" {
 #endif
 
-#include <errno.h>
-#include <limits.h>
-
 #include "piglit-util.h"
 
 #include <piglit/gl_wrap.h>
@@ -248,32 +245,6 @@ GLvoid piglit_draw_rect_back(float x, float y, float w, float h);
 void piglit_draw_rect_from_arrays(const void *verts, const void *tex,
 				  bool use_patches, unsigned instance_count);
 
-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,
diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index a79db3fb7..b5587ea65 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -883,3 +883,92 @@ piglit_fatal(const char *format, ...)
 
 	abort();
 }
+
+typedef union { float f; int32_t i; } fi_type;
+
+/**
+ * Convert a 4-byte float to a 2-byte half float.
+ * Based on code from:
+ * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
+ *
+ * Taken over from Mesa.
+ */
+unsigned short
+piglit_half_from_float(float val)
+{
+	const fi_type fi = {val};
+	const int flt_m = fi.i & 0x7fffff;
+	const int flt_e = (fi.i >> 23) & 0xff;
+	const int flt_s = (fi.i >> 31) & 0x1;
+	int s, e, m = 0;
+	unsigned short result;
+
+	/* sign bit */
+	s = flt_s;
+
+	/* handle special cases */
+	if ((flt_e == 0) && (flt_m == 0)) {
+		/* zero */
+		/* m = 0; - already set */
+		e = 0;
+	}
+	else if ((flt_e == 0) && (flt_m != 0)) {
+		/* denorm -- denorm float maps to 0 half */
+		/* m = 0; - already set */
+		e = 0;
+	}
+	else if ((flt_e == 0xff) && (flt_m == 0)) {
+		/* infinity */
+		/* m = 0; - already set */
+		e = 31;
+	}
+	else if ((flt_e == 0xff) && (flt_m != 0)) {
+		/* NaN */
+		m = 1;
+		e = 31;
+	}
+	else {
+		/* regular number */
+		const int new_exp = flt_e - 127;
+		if (new_exp < -24) {
+			/* this maps to 0 */
+			/* m = 0; - already set */
+			e = 0;
+		}
+		else if (new_exp < -14) {
+			/* this maps to a denorm */
+			/* 2^-exp_val*/
+			unsigned int exp_val = (unsigned int) (-14 - new_exp);
+
+			e = 0;
+			switch (exp_val) {
+			case 0:
+				/* m = 0; - already set */
+				break;
+			case 1: m = 512 + (flt_m >> 14); break;
+			case 2: m = 256 + (flt_m >> 15); break;
+			case 3: m = 128 + (flt_m >> 16); break;
+			case 4: m = 64 + (flt_m >> 17); break;
+			case 5: m = 32 + (flt_m >> 18); break;
+			case 6: m = 16 + (flt_m >> 19); break;
+			case 7: m = 8 + (flt_m >> 20); break;
+			case 8: m = 4 + (flt_m >> 21); break;
+			case 9: m = 2 + (flt_m >> 22); break;
+			case 10: m = 1; break;
+			}
+		}
+		else if (new_exp > 15) {
+			/* map this value to infinity */
+			/* m = 0; - already set */
+			e = 31;
+		}
+		else {
+			/* regular */
+			e = new_exp + 15;
+			m = flt_m >> 13;
+		}
+	}
+
+	result = (s << 15) | (e << 10) | m;
+	return result;
+}
diff --git a/tests/util/piglit-util.h b/tests/util/piglit-util.h
index 3c9ca9fce..a148402f0 100644
--- a/tests/util/piglit-util.h
+++ b/tests/util/piglit-util.h
@@ -50,6 +50,8 @@ extern "C" {
 #include <stdarg.h>
 #include <math.h>
 #include <float.h>
+#include <limits.h>
+#include <errno.h>
 
 #if defined(__APPLE__) || defined(__MINGW32__)
 #  include "libgen.h" // for basename
@@ -189,6 +191,8 @@ do { \
 				     __VA_ARGS__);		       \
 } while (0)
 
+unsigned short piglit_half_from_float(float val);
+
 static inline unsigned
 log2u(unsigned v)
 {
@@ -306,6 +310,30 @@ strtol_hex(const char *nptr, char **endptr)
 	}
 }
 
+/**
+ * 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));
+	}
+}
+
 #ifndef HAVE_STRCHRNUL
 static inline char *
 strchrnul(const char *s, int c)
-- 
2.14.3



More information about the Piglit mailing list