Merge branch 'fp16-tests' into 'master'

ngl: Make the C half-float implementation accessible

See merge request GNOME/gtk!3748
This commit is contained in:
Matthias Clasen
2021-07-13 13:38:21 +00:00
4 changed files with 82 additions and 3 deletions

View File

@@ -54,7 +54,7 @@ float_to_half (const float x)
return (b&0x80000000)>>16 | (e>112)*((((e-112)<<10)&0x7C00)|m>>13) | ((e<113)&(e>101))*((((0x007FF000+m)>>(125-e))+1)>>1) | (e>143)*0x7FFF; // sign : normalized : denormalized : saturate
}
static void
void
float_to_half4_c (const float f[4],
guint16 h[4])
{
@@ -64,7 +64,7 @@ float_to_half4_c (const float f[4],
h[3] = float_to_half (f[3]);
}
static void
void
half_to_float4_c (const guint16 h[4],
float f[4])
{

View File

@@ -41,6 +41,12 @@ void float_to_half4_f16c (const float f[4],
void half_to_float4_f16c (const guint16 h[4],
float f[4]);
void float_to_half4_c (const float f[4],
guint16 h[4]);
void half_to_float4_c (const guint16 h[4],
float f[4]);
G_END_DECLS
#endif

View File

@@ -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 ();
}

View File

@@ -234,7 +234,8 @@ foreach t : tests
endforeach
internal_tests = [
['diff']
[ 'diff' ],
[ 'half-float' ],
]
foreach t : internal_tests