1 /*
2 * Copyright © 2021 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <math.h>
25 #include <gtest/gtest.h>
26
27 #include "util/half_float.h"
28 #include "util/u_math.h"
29
30 /* math.h has some defines for these, but they have some compiler dependencies
31 * and can potentially raise exceptions.
32 */
33 #define TEST_POS_INF (uif(0x7f800000))
34 #define TEST_NEG_INF (uif(0xff800000))
35 #define TEST_NAN (uif(0x7fc00000))
36
37 #define HALF_POS_INF 0x7c00
38 #define HALF_NEG_INF 0xfc00
39 #define HALF_NAN 0x7e00
40
41 #ifndef HAVE_ISSIGNALING
issignaling(float x)42 static bool issignaling(float x)
43 {
44 uint32_t ui = fui(x);
45 return (((ui >> 23) & 0xff) == 0xff) && !(ui & (1 << 22));
46 }
47 #endif
48
49 /* The sign of the bit for signaling is different on some old processors
50 * (PA-RISC, old MIPS without IEEE-754-2008 support).
51 *
52 * Disable the tests on those platforms, because it's not clear how to
53 * correctly handle NaNs when the CPU and GPU differ in their convention.
54 */
55 #if DETECT_ARCH_HPPA || ((DETECT_ARCH_MIPS || DETECT_ARCH_MIPS64) && !defined __mips_nan2008)
56 #define IEEE754_2008_NAN 0
57 #else
58 #define IEEE754_2008_NAN 1
59 #endif
60
61 /* Sanity test our inf test values */
TEST(half_to_float_test,inf_test)62 TEST(half_to_float_test, inf_test)
63 {
64 EXPECT_TRUE(isinf(TEST_POS_INF));
65 EXPECT_TRUE(isinf(TEST_NEG_INF));
66 }
67
68 /* Make sure that our 32-bit float nan test value we're using is a
69 * non-signaling NaN.
70 */
71 #if IEEE754_2008_NAN
TEST(half_to_float_test,nan_test)72 TEST(half_to_float_test, nan_test)
73 #else
74 TEST(half_to_float_test, DISABLED_nan_test)
75 #endif
76 {
77 EXPECT_TRUE(isnan(TEST_NAN));
78 EXPECT_FALSE(issignaling(TEST_NAN));
79 }
80
81 static void
test_half_to_float_limits(float (* func)(uint16_t))82 test_half_to_float_limits(float (*func)(uint16_t))
83 {
84 /* Positive and negative 0. */
85 EXPECT_EQ(func(0), 0.0f);
86 EXPECT_EQ(fui(func(0x8000)), fui(-0.0f));
87
88 /* Max normal number */
89 EXPECT_EQ(func(0x7bff), 65504.0f);
90
91 float nan = func(HALF_NAN);
92 EXPECT_TRUE(isnan(nan));
93 EXPECT_FALSE(issignaling(nan));
94
95 /* inf */
96 EXPECT_EQ(func(HALF_POS_INF), TEST_POS_INF);
97 /* -inf */
98 EXPECT_EQ(func(HALF_NEG_INF), TEST_NEG_INF);
99 }
100
101 /* Test the optionally HW instruction-using path. */
102 #if IEEE754_2008_NAN
TEST(half_to_float_test,half_to_float_test)103 TEST(half_to_float_test, half_to_float_test)
104 #else
105 TEST(half_to_float_test, DISABLED_half_to_float_test)
106 #endif
107 {
108 test_half_to_float_limits(_mesa_half_to_float);
109 }
110
111 #if IEEE754_2008_NAN
TEST(half_to_float_test,half_to_float_slow_test)112 TEST(half_to_float_test, half_to_float_slow_test)
113 #else
114 TEST(half_to_float_test, DISABLED_half_to_float_slow_test)
115 #endif
116 {
117 test_half_to_float_limits(_mesa_half_to_float_slow);
118 }
119
120 static void
test_float_to_half_limits(uint16_t (* func)(float))121 test_float_to_half_limits(uint16_t (*func)(float))
122 {
123 /* Positive and negative 0. */
124 EXPECT_EQ(func(0.0f), 0);
125 EXPECT_EQ(func(-0.0f), 0x8000);
126
127 /* Max normal number */
128 EXPECT_EQ(func(65504.0f), 0x7bff);
129
130 uint16_t nan = func(TEST_NAN);
131 EXPECT_EQ((nan & 0xfc00), 0x7c00); /* exponent is all 1s */
132 EXPECT_TRUE(nan & (1 << 9)); /* mantissa is quiet nan */
133
134 EXPECT_EQ(func(TEST_POS_INF), HALF_POS_INF);
135 EXPECT_EQ(func(TEST_NEG_INF), HALF_NEG_INF);
136 }
137
TEST(float_to_half_test,float_to_half_test)138 TEST(float_to_half_test, float_to_half_test)
139 {
140 test_float_to_half_limits(_mesa_float_to_half);
141 }
142
TEST(float_to_float16_rtne_test,float_to_float16_rtne_test)143 TEST(float_to_float16_rtne_test, float_to_float16_rtne_test)
144 {
145 test_float_to_half_limits(_mesa_float_to_float16_rtne);
146 }
147
148 /* no rtne_slow variant -- rtne is just _mesa_float_to_half(). */
149
TEST(float_to_float16_rtz_test,float_to_float16_rtz_test)150 TEST(float_to_float16_rtz_test, float_to_float16_rtz_test)
151 {
152 test_float_to_half_limits(_mesa_float_to_float16_rtz);
153 }
154
TEST(float_to_float16_rtz_slow_test,float_to_float16_rtz_test)155 TEST(float_to_float16_rtz_slow_test, float_to_float16_rtz_test)
156 {
157 test_float_to_half_limits(_mesa_float_to_float16_rtz_slow);
158 }
159