summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan A. Suarez Romero <jasuarez@igalia.com>2024-04-02 13:56:48 +0200
committerMarge Bot <emma+marge@anholt.net>2024-04-19 10:11:06 +0000
commit7f40f35a34010675ccbdf5bc583920f0d10968e6 (patch)
tree70401a44be71e1f469554581141538adeaff0e5d
parentf7ece74a107a2f99b2f494d978c84f8d51faa703 (diff)
Add float from half function
Converts a 16-bit float to 32-bit float. Reviewed-by: Jose Maria Casanova Crespo <jmcasanova@igalia.com> Signed-off-by: Juan A. Suarez Romero <jasuarez@igalia.com> Part-of: <https://gitlab.freedesktop.org/mesa/piglit/-/merge_requests/899>
-rw-r--r--tests/util/piglit-util-gl.c36
-rw-r--r--tests/util/piglit-util-gl.h1
2 files changed, 36 insertions, 1 deletions
diff --git a/tests/util/piglit-util-gl.c b/tests/util/piglit-util-gl.c
index 5b665d874..03e6dde05 100644
--- a/tests/util/piglit-util-gl.c
+++ b/tests/util/piglit-util-gl.c
@@ -307,7 +307,7 @@ piglit_set_tolerance_for_bits(int rbits, int gbits, int bbits, int abits)
}
}
-typedef union { GLfloat f; GLint i; } fi_type;
+typedef union { GLfloat f; GLint i; GLuint ui; } fi_type;
/**
* Convert a 4-byte float to a 2-byte half float.
@@ -396,6 +396,40 @@ piglit_half_from_float(float val)
return result;
}
+/**
+ * Convert a 2-byte half float to a 4-byte float.
+ * Based on code from:
+ * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
+ *
+ * Taken from Mesa.
+ */
+float
+piglit_float_from_half(unsigned short val)
+{
+ fi_type infnan;
+ fi_type magic;
+ fi_type f32;
+
+ infnan.ui = 0x8f << 23;
+ infnan.f = 65536.0f;
+ magic.ui = 0xef << 23;
+
+ /* Exponent / Mantissa */
+ f32.ui = (val & 0x7fff) << 13;
+
+ /* Adjust */
+ f32.f *= magic.f;
+ /* XXX: The magic mul relies on denorms being available */
+
+ /* Inf / NaN */
+ if (f32.f >= infnan.f)
+ f32.ui |= 0xff << 23;
+
+ /* Sign */
+ f32.ui |= (uint32_t)(val & 0x8000) << 16;
+
+ return f32.f;
+}
/**
* Return block size info for a specific texture compression format.
diff --git a/tests/util/piglit-util-gl.h b/tests/util/piglit-util-gl.h
index e53f3a3c8..d76eb18df 100644
--- a/tests/util/piglit-util-gl.h
+++ b/tests/util/piglit-util-gl.h
@@ -260,6 +260,7 @@ 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);
+float piglit_float_from_half(unsigned short val);
/**
* Wrapper for piglit_half_from_float() which allows using an exact