summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2021-07-13 09:02:16 -0400
committerMatthias Clasen <mclasen@redhat.com>2021-07-13 09:03:49 -0400
commitfab82a7c252504a4520711b40aeb259d678309ae (patch)
tree4dbb418bf7cf3afd979387cec529d0127f27414b
parentfc9c34897a1d27eef50cf6ff52276e9be8073019 (diff)
downloadgtk+-fab82a7c252504a4520711b40aeb259d678309ae.tar.gz
tests: Add some fp16 tests
-rw-r--r--testsuite/gsk/half-float.c72
-rw-r--r--testsuite/gsk/meson.build3
2 files changed, 74 insertions, 1 deletions
diff --git a/testsuite/gsk/half-float.c b/testsuite/gsk/half-float.c
new file mode 100644
index 0000000000..35dce9c1c8
--- /dev/null
+++ b/testsuite/gsk/half-float.c
@@ -0,0 +1,72 @@
+#include <gtk/gtk.h>
+
+#include "gsk/ngl/fp16private.h"
+
+static void
+test_constants (void)
+{
+ struct {
+ float f;
+ guint16 h;
+ } tests[] = {
+ { 0.0, FP16_ZERO },
+ { 1.0, FP16_ONE },
+ { -1.0, FP16_MINUS_ONE },
+ };
+
+ for (int i = 0; i < G_N_ELEMENTS (tests); i++)
+ {
+ float f[4];
+ guint16 h[4];
+
+ memset (f, 0, sizeof (f));
+ f[0] = tests[i].f;
+ float_to_half4 (f, h);
+ g_assert_cmpuint (h[0], ==, tests[i].h);
+
+
+ memset (h, 0, sizeof (h));
+ h[0] = tests[i].h;
+ half_to_float4 (h, f);
+ g_assert_cmpfloat (f[0], ==, tests[i].f);
+ }
+}
+
+static void
+test_roundtrip (void)
+{
+ for (int i = 0; i < 100; i++)
+ {
+ float f[4];
+ float f2[4];
+ guint16 h[4];
+
+ do
+ {
+ /* generate a random float thats representable as fp16 */
+ memset (h, 0, sizeof (h));
+ h[0] = g_random_int_range (G_MININT16, G_MAXINT16);
+ half_to_float4 (h, f2);
+ }
+ while (!isnormal (f2[0])); /* skip nans and infs since they don't compare well */
+
+ memset (f, 0, sizeof (f));
+ f[0] = f2[0];
+
+ float_to_half4 (f, h);
+ half_to_float4 (h, f2);
+
+ g_assert_cmpfloat (f[0], ==, f2[0]);
+ }
+}
+
+int
+main (int argc, char *argv[])
+{
+ (g_test_init) (&argc, &argv, NULL);
+
+ g_test_add_func ("/half-float/constants", test_constants);
+ g_test_add_func ("/half-float/roundtrip", test_roundtrip);
+
+ return g_test_run ();
+}
diff --git a/testsuite/gsk/meson.build b/testsuite/gsk/meson.build
index 8a15261ba0..4bdfc8ceaf 100644
--- a/testsuite/gsk/meson.build
+++ b/testsuite/gsk/meson.build
@@ -234,7 +234,8 @@ foreach t : tests
endforeach
internal_tests = [
- ['diff']
+ [ 'diff' ],
+ [ 'half-float' ],
]
foreach t : internal_tests