Skip to content

Commit 05a4212

Browse files
authored
[builtins] Avoid using long double in generic sources (#69754)
Use of long double can be error-prone since it could be one of 80-bit extended precision float, IEEE 128-bit float, or IBM 128-bit float. Instead use an explicit xf_float typedef for the remaining cases where long double is being used in the implementation. This patch does not touch the PPC specializations which still use long double.
1 parent 2282af2 commit 05a4212

22 files changed

+48
-48
lines changed

compiler-rt/lib/builtins/divxc3.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717

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

20-
COMPILER_RT_ABI Lcomplex __divxc3(long double __a, long double __b,
21-
long double __c, long double __d) {
20+
COMPILER_RT_ABI Lcomplex __divxc3(xf_float __a, xf_float __b, xf_float __c,
21+
xf_float __d) {
2222
int __ilogbw = 0;
23-
long double __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
23+
xf_float __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
2424
if (crt_isfinite(__logbw)) {
2525
__ilogbw = (int)__logbw;
2626
__c = crt_scalbnl(__c, -__ilogbw);
2727
__d = crt_scalbnl(__d, -__ilogbw);
2828
}
29-
long double __denom = __c * __c + __d * __d;
29+
xf_float __denom = __c * __c + __d * __d;
3030
Lcomplex z;
3131
COMPLEX_REAL(z) = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
3232
COMPLEX_IMAGINARY(z) =

compiler-rt/lib/builtins/extendxftf2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define DST_QUAD
1818
#include "fp_extend_impl.inc"
1919

20-
COMPILER_RT_ABI tf_float __extendxftf2(long double a) {
20+
COMPILER_RT_ABI tf_float __extendxftf2(xf_float a) {
2121
return __extendXfYf2__(a);
2222
}
2323

compiler-rt/lib/builtins/fixunsxfdi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
#pragma warning(disable : 4700)
3333
#endif
3434

35-
COMPILER_RT_ABI du_int __fixunsxfdi(long double a) {
36-
long_double_bits fb;
35+
COMPILER_RT_ABI du_int __fixunsxfdi(xf_float a) {
36+
xf_bits fb;
3737
fb.f = a;
3838
int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
3939
if (e < 0 || (fb.u.high.s.low & 0x00008000))

compiler-rt/lib/builtins/fixunsxfsi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
#pragma warning(disable : 4700)
3333
#endif
3434

35-
COMPILER_RT_ABI su_int __fixunsxfsi(long double a) {
36-
long_double_bits fb;
35+
COMPILER_RT_ABI su_int __fixunsxfsi(xf_float a) {
36+
xf_bits fb;
3737
fb.f = a;
3838
int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
3939
if (e < 0 || (fb.u.high.s.low & 0x00008000))

compiler-rt/lib/builtins/fixunsxfti.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
// eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
2626
// mmmm mmmm mmmm
2727

28-
COMPILER_RT_ABI tu_int __fixunsxfti(long double a) {
29-
long_double_bits fb;
28+
COMPILER_RT_ABI tu_int __fixunsxfti(xf_float a) {
29+
xf_bits fb;
3030
fb.f = a;
3131
int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
3232
if (e < 0 || (fb.u.high.s.low & 0x00008000))

compiler-rt/lib/builtins/fixxfdi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
#pragma warning(disable : 4700)
3232
#endif
3333

34-
COMPILER_RT_ABI di_int __fixxfdi(long double a) {
34+
COMPILER_RT_ABI di_int __fixxfdi(xf_float a) {
3535
const di_int di_max = (di_int)((~(du_int)0) / 2);
3636
const di_int di_min = -di_max - 1;
37-
long_double_bits fb;
37+
xf_bits fb;
3838
fb.f = a;
3939
int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
4040
if (e < 0)

compiler-rt/lib/builtins/fixxfti.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
// eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
2525
// mmmm mmmm mmmm
2626

27-
COMPILER_RT_ABI ti_int __fixxfti(long double a) {
27+
COMPILER_RT_ABI ti_int __fixxfti(xf_float a) {
2828
const ti_int ti_max = (ti_int)((~(tu_int)0) / 2);
2929
const ti_int ti_min = -ti_max - 1;
30-
long_double_bits fb;
30+
xf_bits fb;
3131
fb.f = a;
3232
int e = (fb.u.high.s.low & 0x00007FFF) - 16383;
3333
if (e < 0)

compiler-rt/lib/builtins/floatdixf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
// eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
2424
// mmmm mmmm mmmm
2525

26-
COMPILER_RT_ABI long double __floatdixf(di_int a) {
26+
COMPILER_RT_ABI xf_float __floatdixf(di_int a) {
2727
if (a == 0)
2828
return 0.0;
2929
const unsigned N = sizeof(di_int) * CHAR_BIT;
3030
const di_int s = a >> (N - 1);
3131
a = (a ^ s) - s;
3232
int clz = __builtin_clzll(a);
3333
int e = (N - 1) - clz; // exponent
34-
long_double_bits fb;
34+
xf_bits fb;
3535
fb.u.high.s.low = ((su_int)s & 0x00008000) | // sign
3636
(e + 16383); // exponent
3737
fb.u.low.all = a << clz; // mantissa

compiler-rt/lib/builtins/floattixf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
2424
// mmmm mmmm mmmm
2525

26-
COMPILER_RT_ABI long double __floattixf(ti_int a) {
26+
COMPILER_RT_ABI xf_float __floattixf(ti_int a) {
2727
if (a == 0)
2828
return 0.0;
2929
const unsigned N = sizeof(ti_int) * CHAR_BIT;
@@ -63,7 +63,7 @@ COMPILER_RT_ABI long double __floattixf(ti_int a) {
6363
a <<= (LDBL_MANT_DIG - sd);
6464
// a is now rounded to LDBL_MANT_DIG bits
6565
}
66-
long_double_bits fb;
66+
xf_bits fb;
6767
fb.u.high.s.low = ((su_int)s & 0x8000) | // sign
6868
(e + 16383); // exponent
6969
fb.u.low.all = (du_int)a; // mantissa

compiler-rt/lib/builtins/floatundixf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
// gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee
2323
// eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
2424
// mmmm mmmm mmmm
25-
COMPILER_RT_ABI long double __floatundixf(du_int a) {
25+
COMPILER_RT_ABI xf_float __floatundixf(du_int a) {
2626
if (a == 0)
2727
return 0.0;
2828
const unsigned N = sizeof(du_int) * CHAR_BIT;
2929
int clz = __builtin_clzll(a);
3030
int e = (N - 1) - clz; // exponent
31-
long_double_bits fb;
31+
xf_bits fb;
3232
fb.u.high.s.low = (e + 16383); // exponent
3333
fb.u.low.all = a << clz; // mantissa
3434
return fb.f;

compiler-rt/lib/builtins/floatuntixf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
2424
// mmmm mmmm mmmm
2525

26-
COMPILER_RT_ABI long double __floatuntixf(tu_int a) {
26+
COMPILER_RT_ABI xf_float __floatuntixf(tu_int a) {
2727
if (a == 0)
2828
return 0.0;
2929
const unsigned N = sizeof(tu_int) * CHAR_BIT;
@@ -61,7 +61,7 @@ COMPILER_RT_ABI long double __floatuntixf(tu_int a) {
6161
a <<= (LDBL_MANT_DIG - sd);
6262
// a is now rounded to LDBL_MANT_DIG bits
6363
}
64-
long_double_bits fb;
64+
xf_bits fb;
6565
fb.u.high.s.low = (e + 16383); // exponent
6666
fb.u.low.all = (du_int)a; // mantissa
6767
return fb.f;

compiler-rt/lib/builtins/fp_extend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static inline int src_rep_t_clz_impl(src_rep_t a) {
5050
#define src_rep_t_clz src_rep_t_clz_impl
5151

5252
#elif defined SRC_80
53-
typedef long double src_t;
53+
typedef xf_float src_t;
5454
typedef __uint128_t src_rep_t;
5555
#define SRC_REP_C (__uint128_t)
5656
// sign bit, exponent and significand occupy the lower 80 bits.

compiler-rt/lib/builtins/fp_trunc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static const int dstSigFracBits = 52;
6060
static const int dstExpBits = 11;
6161

6262
#elif defined DST_80
63-
typedef long double dst_t;
63+
typedef xf_float dst_t;
6464
typedef __uint128_t dst_rep_t;
6565
#define DST_REP_C (__uint128_t)
6666
static const int dstBits = 80;

compiler-rt/lib/builtins/i386/floatdixf.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "../assembly.h"
66

7-
// long double __floatdixf(di_int a);
7+
// xf_float __floatdixf(di_int a);
88

99
#ifdef __i386__
1010

compiler-rt/lib/builtins/i386/floatundixf.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "../assembly.h"
66

7-
// long double __floatundixf(du_int a);16
7+
// xf_float __floatundixf(du_int a);16
88

99
#ifdef __i386__
1010

compiler-rt/lib/builtins/int_types.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ typedef struct {
165165
#define HAS_80_BIT_LONG_DOUBLE 0
166166
#endif
167167

168+
#if HAS_80_BIT_LONG_DOUBLE
169+
typedef long double xf_float;
170+
typedef union {
171+
uqwords u;
172+
xf_float f;
173+
} xf_bits;
174+
#endif
175+
168176
#ifdef __powerpc64__
169177
// From https://gcc.gnu.org/wiki/Ieee128PowerPC:
170178
// PowerPC64 uses the following suffixes:
@@ -213,11 +221,6 @@ typedef union {
213221
#endif
214222

215223
#if CRT_HAS_FLOATING_POINT
216-
typedef union {
217-
uqwords u;
218-
long double f;
219-
} long_double_bits;
220-
221224
#if __STDC_VERSION__ >= 199901L
222225
typedef float _Complex Fcomplex;
223226
typedef double _Complex Dcomplex;

compiler-rt/lib/builtins/mulxc3.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
// Returns: the product of a + ib and c + id
1919

20-
COMPILER_RT_ABI Lcomplex __mulxc3(long double __a, long double __b,
21-
long double __c, long double __d) {
22-
long double __ac = __a * __c;
23-
long double __bd = __b * __d;
24-
long double __ad = __a * __d;
25-
long double __bc = __b * __c;
20+
COMPILER_RT_ABI Lcomplex __mulxc3(xf_float __a, xf_float __b, xf_float __c,
21+
xf_float __d) {
22+
xf_float __ac = __a * __c;
23+
xf_float __bd = __b * __d;
24+
xf_float __ad = __a * __d;
25+
xf_float __bc = __b * __c;
2626
Lcomplex z;
2727
COMPLEX_REAL(z) = __ac - __bd;
2828
COMPLEX_IMAGINARY(z) = __ad + __bc;

compiler-rt/lib/builtins/powixf2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
// Returns: a ^ b
1818

19-
COMPILER_RT_ABI long double __powixf2(long double a, int b) {
19+
COMPILER_RT_ABI xf_float __powixf2(xf_float a, int b) {
2020
const int recip = b < 0;
21-
long double r = 1;
21+
xf_float r = 1;
2222
while (1) {
2323
if (b & 1)
2424
r *= a;

compiler-rt/lib/builtins/ppc/multc3.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
} \
2020
}
2121

22-
long double _Complex __multc3(long double a, long double b, long double c,
23-
long double d) {
22+
xf_float _Complex __multc3(xf_float a, xf_float b, xf_float c, xf_float d) {
2423
long double ac = __gcc_qmul(a, c);
2524
long double bd = __gcc_qmul(b, d);
2625
long double ad = __gcc_qmul(a, d);

compiler-rt/lib/builtins/trunctfxf2.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#define DST_80
1919
#include "fp_trunc_impl.inc"
2020

21-
COMPILER_RT_ABI long double __trunctfxf2(tf_float a) {
22-
return __truncXfYf2__(a);
23-
}
21+
COMPILER_RT_ABI xf_float __trunctfxf2(tf_float a) { return __truncXfYf2__(a); }
2422

2523
#endif

compiler-rt/lib/builtins/x86_64/floatdixf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// See https://llvm.org/LICENSE.txt for license information.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5-
// long double __floatdixf(di_int a);
5+
// xf_float __floatdixf(di_int a);
66

77
#ifdef __x86_64__
88

99
#include "../int_lib.h"
1010

11-
long double __floatdixf(int64_t a) { return (long double)a; }
11+
xf_float __floatdixf(int64_t a) { return (xf_float)a; }
1212

1313
#endif // __i386__

compiler-rt/lib/builtins/x86_64/floatundixf.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "../assembly.h"
66

7-
// long double __floatundixf(du_int a);
7+
// xf_float __floatundixf(du_int a);
88

99
#ifdef __x86_64__
1010

0 commit comments

Comments
 (0)