Skip to content

Commit d33dc14

Browse files
authored
[compiler-rt] Allow running extendhfxf2_test without int128 support
We don't need 128-bit integers here, instead rewrite the fp_test.h helpers to avoid the need for __int128. Also change the high argument for compareResultF80() and F80FromRep80() to be uint16_t since values greater than this do not make any sense. This should allow the compiler to detect accidentally swapping the arguments. Reviewed By: biabbas, alexander-shaposhnikov Pull Request: #117818
1 parent 1b03747 commit d33dc14

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

compiler-rt/test/builtins/Unit/extendhfxf2_test.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,58 @@
77

88
#include "fp_test.h"
99

10-
#if __LDBL_MANT_DIG__ == 64 && defined(__x86_64__) && \
11-
defined(COMPILER_RT_HAS_FLOAT16)
10+
#if HAS_80_BIT_LONG_DOUBLE
1211

1312
xf_float __extendhfxf2(TYPE_FP16 f);
1413

15-
int test_extendhfxf2(TYPE_FP16 a, uint64_t expectedHi, uint64_t expectedLo) {
14+
int test_extendhfxf2(TYPE_FP16 a, uint16_t expectedHi, uint64_t expectedLo) {
1615
xf_float x = __extendhfxf2(a);
1716
int ret = compareResultF80(x, expectedHi, expectedLo);
1817
if (ret) {
1918
printf("error in test__extendhfxf2(%#.4x) = %.20Lf, "
2019
"expected %.20Lf\n",
21-
toRep16(a), x, F80FromRep128(expectedHi, expectedLo));
20+
toRep16(a), x, F80FromRep80(expectedHi, expectedLo));
2221
}
2322
return ret;
2423
}
2524

2625
int main() {
2726
// Small positive value
28-
if (test_extendhfxf2(fromRep16(0x2e66), UINT64_C(0x3ffb),
27+
if (test_extendhfxf2(fromRep16(0x2e66), UINT16_C(0x3ffb),
2928
UINT64_C(0xccc0000000000000)))
3029
return 1;
3130

3231
// Small negative value
33-
if (test_extendhfxf2(fromRep16(0xae66), UINT64_C(0xbffb),
32+
if (test_extendhfxf2(fromRep16(0xae66), UINT16_C(0xbffb),
3433
UINT64_C(0xccc0000000000000)))
3534
return 1;
3635

3736
// Zero
38-
if (test_extendhfxf2(fromRep16(0), UINT64_C(0x0), UINT64_C(0x0)))
37+
if (test_extendhfxf2(fromRep16(0), UINT16_C(0x0), UINT64_C(0x0)))
3938
return 1;
4039

4140
// Smallest positive non-zero value
42-
if (test_extendhfxf2(fromRep16(0x0100), UINT64_C(0x3fef),
41+
if (test_extendhfxf2(fromRep16(0x0100), UINT16_C(0x3fef),
4342
UINT64_C(0x8000000000000000)))
4443
return 1;
4544

4645
// Smallest negative non-zero value
47-
if (test_extendhfxf2(fromRep16(0x8100), UINT64_C(0xbfef),
46+
if (test_extendhfxf2(fromRep16(0x8100), UINT16_C(0xbfef),
4847
UINT64_C(0x8000000000000000)))
4948
return 1;
5049

5150
// Positive infinity
52-
if (test_extendhfxf2(makeInf16(), UINT64_C(0x7fff),
51+
if (test_extendhfxf2(makeInf16(), UINT16_C(0x7fff),
5352
UINT64_C(0x8000000000000000)))
5453
return 1;
5554

5655
// Negative infinity
57-
if (test_extendhfxf2(makeNegativeInf16(), UINT64_C(0xffff),
56+
if (test_extendhfxf2(makeNegativeInf16(), UINT16_C(0xffff),
5857
UINT64_C(0x8000000000000000)))
5958
return 1;
6059

6160
// NaN
62-
if (test_extendhfxf2(makeQNaN16(), UINT64_C(0x7fff),
61+
if (test_extendhfxf2(makeQNaN16(), UINT16_C(0x7fff),
6362
UINT64_C(0xc000000000000000)))
6463
return 1;
6564

compiler-rt/test/builtins/Unit/fp_test.h

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#include <stdlib.h>
1+
#include <assert.h>
22
#include <limits.h>
3-
#include <string.h>
43
#include <stdint.h>
4+
#include <stdlib.h>
5+
#include <string.h>
56

67
#include "int_types.h"
78

@@ -230,44 +231,49 @@ static inline double makeQNaN64(void)
230231
return fromRep64(0x7ff8000000000000UL);
231232
}
232233

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;
234+
#if HAS_80_BIT_LONG_DOUBLE
235+
static inline xf_float F80FromRep80(uint16_t hi, uint64_t lo) {
236+
uqwords bits;
237+
bits.high.all = hi;
238+
bits.low.all = lo;
239+
xf_float ret;
240+
static_assert(sizeof(xf_float) <= sizeof(uqwords), "wrong representation");
241+
memcpy(&ret, &bits, sizeof(ret));
242+
return ret;
239243
}
240244

241-
static inline __uint128_t F80ToRep128(long double x) {
242-
__uint128_t ret;
243-
memcpy(&ret, &x, 16);
244-
return ret;
245+
static inline uqwords F80ToRep80(xf_float x) {
246+
uqwords ret;
247+
memset(&ret, 0, sizeof(ret));
248+
memcpy(&ret, &x, sizeof(x));
249+
// Any bits beyond the first 16 in high are undefined.
250+
ret.high.all = (uint16_t)ret.high.all;
251+
return ret;
245252
}
246253

247-
static inline int compareResultF80(long double result, uint64_t expectedHi,
254+
static inline int compareResultF80(xf_float result, uint16_t expectedHi,
248255
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+
uqwords rep = F80ToRep80(result);
257+
// F80 high occupies the lower 16 bits of high.
258+
assert((uint64_t)(uint16_t)rep.high.all == rep.high.all);
259+
return !(rep.high.all == expectedHi && rep.low.all == expectedLo);
254260
}
255261

256-
static inline long double makeQNaN80(void) {
257-
return F80FromRep128(0x7fffUL, 0xc000000000000000UL);
262+
static inline xf_float makeQNaN80(void) {
263+
return F80FromRep80(0x7fffu, 0xc000000000000000UL);
258264
}
259265

260-
static inline long double makeNaN80(uint64_t rand) {
261-
return F80FromRep128(0x7fffUL,
262-
0x8000000000000000 | (rand & 0x3fffffffffffffff));
266+
static inline xf_float makeNaN80(uint64_t rand) {
267+
return F80FromRep80(0x7fffu,
268+
0x8000000000000000 | (rand & 0x3fffffffffffffff));
263269
}
264270

265-
static inline long double makeInf80(void) {
266-
return F80FromRep128(0x7fffUL, 0x8000000000000000UL);
271+
static inline xf_float makeInf80(void) {
272+
return F80FromRep80(0x7fffu, 0x8000000000000000UL);
267273
}
268274

269-
static inline long double makeNegativeInf80(void) {
270-
return F80FromRep128(0xffffUL, 0x8000000000000000UL);
275+
static inline xf_float makeNegativeInf80(void) {
276+
return F80FromRep80(0xffffu, 0x8000000000000000UL);
271277
}
272278
#endif
273279

0 commit comments

Comments
 (0)