Skip to content

Commit 99c457d

Browse files
Unbreak *tf builtins for hexfloat (#82208)
This re-lands cc0065a in a way that keeps existing targets working. --------- Original commit message: #68132 ended up removing __multc3 & __divtc3 from compiler-rt library builds that have QUAD_PRECISION but not TF_MODE due to missing int128 support. I added support for QUAD_PRECISION to use the native hex float long double representation. --------- Co-authored-by: Sean Perry <[email protected]>
1 parent cd160a6 commit 99c457d

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

compiler-rt/lib/builtins/divtc3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#define QUAD_PRECISION
1414
#include "fp_lib.h"
1515

16-
#if defined(CRT_HAS_TF_MODE)
16+
#if defined(CRT_HAS_F128)
1717

1818
// Returns: the quotient of (a + ib) / (c + id)
1919

compiler-rt/lib/builtins/fp_lib.h

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "int_lib.h"
2424
#include "int_math.h"
25+
#include "int_types.h"
2526
#include <limits.h>
2627
#include <stdbool.h>
2728
#include <stdint.h>
@@ -93,13 +94,14 @@ static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
9394
COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
9495

9596
#elif defined QUAD_PRECISION
96-
#if defined(CRT_HAS_TF_MODE)
97+
#if defined(CRT_HAS_F128) && defined(CRT_HAS_128BIT)
9798
typedef uint64_t half_rep_t;
9899
typedef __uint128_t rep_t;
99100
typedef __int128_t srep_t;
100101
typedef tf_float fp_t;
101102
#define HALF_REP_C UINT64_C
102103
#define REP_C (__uint128_t)
104+
#if defined(CRT_HAS_IEEE_TF)
103105
// Note: Since there is no explicit way to tell compiler the constant is a
104106
// 128-bit integer, we let the constant be casted to 128-bit integer
105107
#define significandBits 112
@@ -188,27 +190,17 @@ static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
188190
#undef Word_HiMask
189191
#undef Word_LoMask
190192
#undef Word_FullMask
191-
#endif // defined(CRT_HAS_TF_MODE)
193+
#endif // defined(CRT_HAS_IEEE_TF)
194+
#else
195+
typedef long double fp_t;
196+
#endif // defined(CRT_HAS_F128) && defined(CRT_HAS_128BIT)
192197
#else
193198
#error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
194199
#endif
195200

196201
#if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || \
197202
(defined(QUAD_PRECISION) && defined(CRT_HAS_TF_MODE))
198203
#define typeWidth (sizeof(rep_t) * CHAR_BIT)
199-
#define exponentBits (typeWidth - significandBits - 1)
200-
#define maxExponent ((1 << exponentBits) - 1)
201-
#define exponentBias (maxExponent >> 1)
202-
203-
#define implicitBit (REP_C(1) << significandBits)
204-
#define significandMask (implicitBit - 1U)
205-
#define signBit (REP_C(1) << (significandBits + exponentBits))
206-
#define absMask (signBit - 1U)
207-
#define exponentMask (absMask ^ significandMask)
208-
#define oneRep ((rep_t)exponentBias << significandBits)
209-
#define infRep exponentMask
210-
#define quietBit (implicitBit >> 1)
211-
#define qnanRep (exponentMask | quietBit)
212204

213205
static __inline rep_t toRep(fp_t x) {
214206
const union {
@@ -226,6 +218,21 @@ static __inline fp_t fromRep(rep_t x) {
226218
return rep.f;
227219
}
228220

221+
#if !defined(QUAD_PRECISION) || defined(CRT_HAS_IEEE_TF)
222+
#define exponentBits (typeWidth - significandBits - 1)
223+
#define maxExponent ((1 << exponentBits) - 1)
224+
#define exponentBias (maxExponent >> 1)
225+
226+
#define implicitBit (REP_C(1) << significandBits)
227+
#define significandMask (implicitBit - 1U)
228+
#define signBit (REP_C(1) << (significandBits + exponentBits))
229+
#define absMask (signBit - 1U)
230+
#define exponentMask (absMask ^ significandMask)
231+
#define oneRep ((rep_t)exponentBias << significandBits)
232+
#define infRep exponentMask
233+
#define quietBit (implicitBit >> 1)
234+
#define qnanRep (exponentMask | quietBit)
235+
229236
static __inline int normalize(rep_t *significand) {
230237
const int shift = rep_clz(*significand) - rep_clz(implicitBit);
231238
*significand <<= shift;
@@ -328,6 +335,8 @@ static __inline fp_t __compiler_rt_scalbnX(fp_t x, int y) {
328335
return fromRep(sign | ((rep_t)exp << significandBits) | sig);
329336
}
330337

338+
#endif // !defined(QUAD_PRECISION) || defined(CRT_HAS_IEEE_TF)
339+
331340
// Avoid using fmax from libm.
332341
static __inline fp_t __compiler_rt_fmaxX(fp_t x, fp_t y) {
333342
// If either argument is NaN, return the other argument. If both are NaN,
@@ -405,6 +414,8 @@ static __inline tf_float __compiler_rt_fmaxtf(tf_float x, tf_float y) {
405414
#define __compiler_rt_logbl crt_logbl
406415
#define __compiler_rt_scalbnl crt_scalbnl
407416
#define __compiler_rt_fmaxl crt_fmaxl
417+
#define crt_fabstf crt_fabsl
418+
#define crt_copysigntf crt_copysignl
408419
#else
409420
#error Unsupported TF mode type
410421
#endif

compiler-rt/lib/builtins/int_types.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,16 @@ typedef long double tf_float;
189189
#define CRT_LDBL_IEEE_F128
190190
#endif
191191
#define TF_C(x) x##L
192-
#elif __LDBL_MANT_DIG__ == 113
193-
// Use long double instead of __float128 if it matches the IEEE 128-bit format.
192+
#elif __LDBL_MANT_DIG__ == 113 || \
193+
(__FLT_RADIX__ == 16 && __LDBL_MANT_DIG__ == 28)
194+
// Use long double instead of __float128 if it matches the IEEE 128-bit format
195+
// or the IBM hexadecimal format.
194196
#define CRT_LDBL_128BIT
195197
#define CRT_HAS_F128
198+
#if __LDBL_MANT_DIG__ == 113
196199
#define CRT_HAS_IEEE_TF
197200
#define CRT_LDBL_IEEE_F128
201+
#endif
198202
typedef long double tf_float;
199203
#define TF_C(x) x##L
200204
#elif defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__)

compiler-rt/lib/builtins/multc3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "int_lib.h"
1616
#include "int_math.h"
1717

18-
#if defined(CRT_HAS_TF_MODE)
18+
#if defined(CRT_HAS_F128)
1919

2020
// Returns: the product of a + ib and c + id
2121

0 commit comments

Comments
 (0)