[PowerPC] Collect some CallLowering arguments into a struct. [NFC]
[llvm-project.git] / compiler-rt / test / builtins / Unit / fp_test.h
blob49f7197242a28f81853e682ff7d0773c62cc2722
1 //===--------------------------- fp_test.h - ------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines shared functions for the test.
11 //===----------------------------------------------------------------------===//
13 #include <stdlib.h>
14 #include <limits.h>
15 #include <string.h>
16 #include <stdint.h>
18 enum EXPECTED_RESULT {
19 LESS_0, LESS_EQUAL_0, EQUAL_0, GREATER_0, GREATER_EQUAL_0, NEQUAL_0
22 static inline uint16_t fromRep16(uint16_t x)
24 return x;
27 static inline float fromRep32(uint32_t x)
29 float ret;
30 memcpy(&ret, &x, 4);
31 return ret;
34 static inline double fromRep64(uint64_t x)
36 double ret;
37 memcpy(&ret, &x, 8);
38 return ret;
41 #if __LDBL_MANT_DIG__ == 113
42 static inline long double fromRep128(uint64_t hi, uint64_t lo)
44 __uint128_t x = ((__uint128_t)hi << 64) + lo;
45 long double ret;
46 memcpy(&ret, &x, 16);
47 return ret;
49 #endif
51 static inline uint16_t toRep16(uint16_t x)
53 return x;
56 static inline uint32_t toRep32(float x)
58 uint32_t ret;
59 memcpy(&ret, &x, 4);
60 return ret;
63 static inline uint64_t toRep64(double x)
65 uint64_t ret;
66 memcpy(&ret, &x, 8);
67 return ret;
70 #if __LDBL_MANT_DIG__ == 113
71 static inline __uint128_t toRep128(long double x)
73 __uint128_t ret;
74 memcpy(&ret, &x, 16);
75 return ret;
77 #endif
79 static inline int compareResultH(uint16_t result,
80 uint16_t expected)
82 uint16_t rep = toRep16(result);
84 if (rep == expected){
85 return 0;
87 // test other possible NaN representation(signal NaN)
88 else if (expected == 0x7e00U){
89 if ((rep & 0x7c00U) == 0x7c00U &&
90 (rep & 0x3ffU) > 0){
91 return 0;
94 return 1;
97 static inline int compareResultF(float result,
98 uint32_t expected)
100 uint32_t rep = toRep32(result);
102 if (rep == expected){
103 return 0;
105 // test other possible NaN representation(signal NaN)
106 else if (expected == 0x7fc00000U){
107 if ((rep & 0x7f800000U) == 0x7f800000U &&
108 (rep & 0x7fffffU) > 0){
109 return 0;
112 return 1;
115 static inline int compareResultD(double result,
116 uint64_t expected)
118 uint64_t rep = toRep64(result);
120 if (rep == expected){
121 return 0;
123 // test other possible NaN representation(signal NaN)
124 else if (expected == 0x7ff8000000000000UL){
125 if ((rep & 0x7ff0000000000000UL) == 0x7ff0000000000000UL &&
126 (rep & 0xfffffffffffffUL) > 0){
127 return 0;
130 return 1;
133 #if __LDBL_MANT_DIG__ == 113
134 // return 0 if equal
135 // use two 64-bit integers intead of one 128-bit integer
136 // because 128-bit integer constant can't be assigned directly
137 static inline int compareResultLD(long double result,
138 uint64_t expectedHi,
139 uint64_t expectedLo)
141 __uint128_t rep = toRep128(result);
142 uint64_t hi = rep >> 64;
143 uint64_t lo = rep;
145 if (hi == expectedHi && lo == expectedLo){
146 return 0;
148 // test other possible NaN representation(signal NaN)
149 else if (expectedHi == 0x7fff800000000000UL && expectedLo == 0x0UL){
150 if ((hi & 0x7fff000000000000UL) == 0x7fff000000000000UL &&
151 ((hi & 0xffffffffffffUL) > 0 || lo > 0)){
152 return 0;
155 return 1;
157 #endif
159 static inline int compareResultCMP(int result,
160 enum EXPECTED_RESULT expected)
162 switch(expected){
163 case LESS_0:
164 if (result < 0)
165 return 0;
166 break;
167 case LESS_EQUAL_0:
168 if (result <= 0)
169 return 0;
170 break;
171 case EQUAL_0:
172 if (result == 0)
173 return 0;
174 break;
175 case NEQUAL_0:
176 if (result != 0)
177 return 0;
178 break;
179 case GREATER_EQUAL_0:
180 if (result >= 0)
181 return 0;
182 break;
183 case GREATER_0:
184 if (result > 0)
185 return 0;
186 break;
187 default:
188 return 1;
190 return 1;
193 static inline char *expectedStr(enum EXPECTED_RESULT expected)
195 switch(expected){
196 case LESS_0:
197 return "<0";
198 case LESS_EQUAL_0:
199 return "<=0";
200 case EQUAL_0:
201 return "=0";
202 case NEQUAL_0:
203 return "!=0";
204 case GREATER_EQUAL_0:
205 return ">=0";
206 case GREATER_0:
207 return ">0";
208 default:
209 return "";
211 return "";
214 static inline uint16_t makeQNaN16()
216 return fromRep16(0x7e00U);
219 static inline float makeQNaN32()
221 return fromRep32(0x7fc00000U);
224 static inline double makeQNaN64()
226 return fromRep64(0x7ff8000000000000UL);
229 #if __LDBL_MANT_DIG__ == 113
230 static inline long double makeQNaN128()
232 return fromRep128(0x7fff800000000000UL, 0x0UL);
234 #endif
236 static inline uint16_t makeNaN16(uint16_t rand)
238 return fromRep16(0x7c00U | (rand & 0x7fffU));
241 static inline float makeNaN32(uint32_t rand)
243 return fromRep32(0x7f800000U | (rand & 0x7fffffU));
246 static inline double makeNaN64(uint64_t rand)
248 return fromRep64(0x7ff0000000000000UL | (rand & 0xfffffffffffffUL));
251 #if __LDBL_MANT_DIG__ == 113
252 static inline long double makeNaN128(uint64_t rand)
254 return fromRep128(0x7fff000000000000UL | (rand & 0xffffffffffffUL), 0x0UL);
256 #endif
258 static inline uint16_t makeInf16()
260 return fromRep16(0x7c00U);
263 static inline float makeInf32()
265 return fromRep32(0x7f800000U);
268 static inline double makeInf64()
270 return fromRep64(0x7ff0000000000000UL);
273 #if __LDBL_MANT_DIG__ == 113
274 static inline long double makeInf128()
276 return fromRep128(0x7fff000000000000UL, 0x0UL);
278 #endif