Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / builtins / Unit / fp_test.h
blob4eb54b7425c107b41f3691c967274aa57144f521
1 #include <stdlib.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <stdint.h>
6 #include "int_types.h"
8 #ifdef COMPILER_RT_HAS_FLOAT16
9 #define TYPE_FP16 _Float16
10 #else
11 #define TYPE_FP16 uint16_t
12 #endif
14 enum EXPECTED_RESULT {
15 LESS_0, LESS_EQUAL_0, EQUAL_0, GREATER_0, GREATER_EQUAL_0, NEQUAL_0
18 static inline TYPE_FP16 fromRep16(uint16_t x)
20 #ifdef COMPILER_RT_HAS_FLOAT16
21 TYPE_FP16 ret;
22 memcpy(&ret, &x, sizeof(ret));
23 return ret;
24 #else
25 return x;
26 #endif
29 static inline float fromRep32(uint32_t x)
31 float ret;
32 memcpy(&ret, &x, 4);
33 return ret;
36 static inline double fromRep64(uint64_t x)
38 double ret;
39 memcpy(&ret, &x, 8);
40 return ret;
43 #if defined(CRT_HAS_TF_MODE)
44 static inline tf_float fromRep128(uint64_t hi, uint64_t lo) {
45 __uint128_t x = ((__uint128_t)hi << 64) + lo;
46 tf_float ret;
47 memcpy(&ret, &x, 16);
48 return ret;
50 #endif
52 static inline uint16_t toRep16(TYPE_FP16 x)
54 #ifdef COMPILER_RT_HAS_FLOAT16
55 uint16_t ret;
56 memcpy(&ret, &x, sizeof(ret));
57 return ret;
58 #else
59 return x;
60 #endif
63 static inline uint32_t toRep32(float x)
65 uint32_t ret;
66 memcpy(&ret, &x, 4);
67 return ret;
70 static inline uint64_t toRep64(double x)
72 uint64_t ret;
73 memcpy(&ret, &x, 8);
74 return ret;
77 #if defined(CRT_HAS_TF_MODE)
78 static inline __uint128_t toRep128(tf_float x) {
79 __uint128_t ret;
80 memcpy(&ret, &x, 16);
81 return ret;
83 #endif
85 static inline int compareResultH(TYPE_FP16 result,
86 uint16_t expected)
88 uint16_t rep = toRep16(result);
90 if (rep == expected){
91 return 0;
93 // test other possible NaN representation(signal NaN)
94 else if (expected == 0x7e00U){
95 if ((rep & 0x7c00U) == 0x7c00U &&
96 (rep & 0x3ffU) > 0){
97 return 0;
100 return 1;
103 static inline int compareResultF(float result,
104 uint32_t expected)
106 uint32_t rep = toRep32(result);
108 if (rep == expected){
109 return 0;
111 // test other possible NaN representation(signal NaN)
112 else if (expected == 0x7fc00000U){
113 if ((rep & 0x7f800000U) == 0x7f800000U &&
114 (rep & 0x7fffffU) > 0){
115 return 0;
118 return 1;
121 static inline int compareResultD(double result,
122 uint64_t expected)
124 uint64_t rep = toRep64(result);
126 if (rep == expected){
127 return 0;
129 // test other possible NaN representation(signal NaN)
130 else if (expected == 0x7ff8000000000000UL){
131 if ((rep & 0x7ff0000000000000UL) == 0x7ff0000000000000UL &&
132 (rep & 0xfffffffffffffUL) > 0){
133 return 0;
136 return 1;
139 #if defined(CRT_HAS_TF_MODE)
140 // return 0 if equal
141 // use two 64-bit integers instead of one 128-bit integer
142 // because 128-bit integer constant can't be assigned directly
143 static inline int compareResultF128(tf_float result, uint64_t expectedHi,
144 uint64_t expectedLo) {
145 __uint128_t rep = toRep128(result);
146 uint64_t hi = rep >> 64;
147 uint64_t lo = rep;
149 if (hi == expectedHi && lo == expectedLo) {
150 return 0;
152 // test other possible NaN representation(signal NaN)
153 else if (expectedHi == 0x7fff800000000000UL && expectedLo == 0x0UL) {
154 if ((hi & 0x7fff000000000000UL) == 0x7fff000000000000UL &&
155 ((hi & 0xffffffffffffUL) > 0 || lo > 0)) {
156 return 0;
159 return 1;
161 #endif
163 static inline int compareResultCMP(int result,
164 enum EXPECTED_RESULT expected)
166 switch(expected){
167 case LESS_0:
168 if (result < 0)
169 return 0;
170 break;
171 case LESS_EQUAL_0:
172 if (result <= 0)
173 return 0;
174 break;
175 case EQUAL_0:
176 if (result == 0)
177 return 0;
178 break;
179 case NEQUAL_0:
180 if (result != 0)
181 return 0;
182 break;
183 case GREATER_EQUAL_0:
184 if (result >= 0)
185 return 0;
186 break;
187 case GREATER_0:
188 if (result > 0)
189 return 0;
190 break;
191 default:
192 return 1;
194 return 1;
197 static inline char *expectedStr(enum EXPECTED_RESULT expected)
199 switch(expected){
200 case LESS_0:
201 return "<0";
202 case LESS_EQUAL_0:
203 return "<=0";
204 case EQUAL_0:
205 return "=0";
206 case NEQUAL_0:
207 return "!=0";
208 case GREATER_EQUAL_0:
209 return ">=0";
210 case GREATER_0:
211 return ">0";
212 default:
213 return "";
215 return "";
218 static inline TYPE_FP16 makeQNaN16(void)
220 return fromRep16(0x7e00U);
223 static inline float makeQNaN32(void)
225 return fromRep32(0x7fc00000U);
228 static inline double makeQNaN64(void)
230 return fromRep64(0x7ff8000000000000UL);
233 #if __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
234 static inline long double F80FromRep128(uint64_t hi, uint64_t lo) {
235 __uint128_t x = ((__uint128_t)hi << 64) + lo;
236 long double ret;
237 memcpy(&ret, &x, 16);
238 return ret;
241 static inline __uint128_t F80ToRep128(long double x) {
242 __uint128_t ret;
243 memcpy(&ret, &x, 16);
244 return ret;
247 static inline int compareResultF80(long double result, uint64_t expectedHi,
248 uint64_t expectedLo) {
249 __uint128_t rep = F80ToRep128(result);
250 // F80 occupies the lower 80 bits of __uint128_t.
251 uint64_t hi = (rep >> 64) & ((1UL << (80 - 64)) - 1);
252 uint64_t lo = rep;
253 return !(hi == expectedHi && lo == expectedLo);
256 static inline long double makeQNaN80(void) {
257 return F80FromRep128(0x7fffUL, 0xc000000000000000UL);
260 static inline long double makeNaN80(uint64_t rand) {
261 return F80FromRep128(0x7fffUL,
262 0x8000000000000000 | (rand & 0x3fffffffffffffff));
265 static inline long double makeInf80(void) {
266 return F80FromRep128(0x7fffUL, 0x8000000000000000UL);
268 #endif
270 #if defined(CRT_HAS_TF_MODE)
271 static inline tf_float makeQNaN128(void) {
272 return fromRep128(0x7fff800000000000UL, 0x0UL);
274 #endif
276 static inline TYPE_FP16 makeNaN16(uint16_t rand)
278 return fromRep16(0x7c00U | (rand & 0x7fffU));
281 static inline float makeNaN32(uint32_t rand)
283 return fromRep32(0x7f800000U | (rand & 0x7fffffU));
286 static inline double makeNaN64(uint64_t rand)
288 return fromRep64(0x7ff0000000000000UL | (rand & 0xfffffffffffffUL));
291 #if defined(CRT_HAS_TF_MODE)
292 static inline tf_float makeNaN128(uint64_t rand) {
293 return fromRep128(0x7fff000000000000UL | (rand & 0xffffffffffffUL), 0x0UL);
295 #endif
297 static inline TYPE_FP16 makeInf16(void)
299 return fromRep16(0x7c00U);
302 static inline float makeInf32(void)
304 return fromRep32(0x7f800000U);
307 static inline float makeNegativeInf32(void)
309 return fromRep32(0xff800000U);
312 static inline double makeInf64(void)
314 return fromRep64(0x7ff0000000000000UL);
317 static inline double makeNegativeInf64(void)
319 return fromRep64(0xfff0000000000000UL);
322 #if defined(CRT_HAS_TF_MODE)
323 static inline tf_float makeInf128(void) {
324 return fromRep128(0x7fff000000000000UL, 0x0UL);
327 static inline tf_float makeNegativeInf128(void) {
328 return fromRep128(0xffff000000000000UL, 0x0UL);
330 #endif