1 /* Copyright (C) 1989-2024 Free Software Foundation, Inc.
3 This file is part of GCC.
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
24 /* This is a temporary specialization of code from libgcc/libgcc2.c. */
27 #include "quad-float128.h"
29 /* Use the correct built-in function based on whether TFmode is _Float128 or
30 long double. See quad-float128.h for more details. */
31 #ifndef __LONG_DOUBLE_IEEE128__
32 #define COPYSIGN(x,y) __builtin_copysignf128 (x, y)
33 #define INFINITY __builtin_inff128 ()
34 #define FABS __builtin_fabsf128
37 #define COPYSIGN(x,y) __builtin_copysignl (x, y)
38 #define INFINITY __builtin_infl ()
39 #define FABS __builtin_fabsl
42 #define isnan __builtin_isnan
43 #define isinf __builtin_isinf
44 #define isfinite __builtin_isfinite
46 #if defined(FLOAT128_HW_INSNS) && !defined(__divkc3)
47 #define __divkc3 __divkc3_sw
50 #ifndef __LONG_DOUBLE_IEEE128__
51 #define RBIG (__LIBGCC_KF_MAX__ / 2)
52 #define RMIN (__LIBGCC_KF_MIN__)
53 #define RMIN2 (__LIBGCC_KF_EPSILON__)
54 #define RMINSCAL (1 / __LIBGCC_KF_EPSILON__)
55 #define RMAX2 (RBIG * RMIN2)
57 #define RBIG (__LIBGCC_TF_MAX__ / 2)
58 #define RMIN (__LIBGCC_TF_MIN__)
59 #define RMIN2 (__LIBGCC_TF_EPSILON__)
60 #define RMINSCAL (1 / __LIBGCC_TF_EPSILON__)
61 #define RMAX2 (RBIG * RMIN2)
65 __divkc3 (TFtype a
, TFtype b
, TFtype c
, TFtype d
)
67 TFtype denom
, ratio
, x
, y
;
70 /* long double has significant potential underflow/overflow errors that
71 can be greatly reduced with a limited number of tests and adjustments.
74 /* Scale by max(c,d) to reduce chances of denominator overflowing. */
75 if (FABS (c
) < FABS (d
))
77 /* Prevent underflow when denominator is near max representable. */
85 /* Avoid overflow/underflow issues when c and d are small.
86 Scaling up helps avoid some underflows.
87 No new overflow possible since c&d < RMIN2. */
97 if (((FABS (a
) < RMIN
) && (FABS (b
) < RMAX2
) && (FABS (d
) < RMAX2
))
98 || ((FABS (b
) < RMIN
) && (FABS (a
) < RMAX2
)
99 && (FABS (d
) < RMAX2
)))
108 denom
= (c
* ratio
) + d
;
109 /* Choose alternate order of computation if ratio is subnormal. */
110 if (FABS (ratio
) > RMIN
)
112 x
= ((a
* ratio
) + b
) / denom
;
113 y
= ((b
* ratio
) - a
) / denom
;
117 x
= ((c
* (a
/ d
)) + b
) / denom
;
118 y
= ((c
* (b
/ d
)) - a
) / denom
;
123 /* Prevent underflow when denominator is near max representable. */
124 if (FABS (c
) >= RBIG
)
131 /* Avoid overflow/underflow issues when both c and d are small.
132 Scaling up helps avoid some underflows.
133 No new overflow possible since both c&d are less than RMIN2. */
134 if (FABS (c
) < RMIN2
)
143 if (((FABS (a
) < RMIN
) && (FABS (b
) < RMAX2
) && (FABS (c
) < RMAX2
))
144 || ((FABS (b
) < RMIN
) && (FABS (a
) < RMAX2
)
145 && (FABS (c
) < RMAX2
)))
154 denom
= (d
* ratio
) + c
;
155 /* Choose alternate order of computation if ratio is subnormal. */
156 if (FABS (ratio
) > RMIN
)
158 x
= ((b
* ratio
) + a
) / denom
;
159 y
= (b
- (a
* ratio
)) / denom
;
163 x
= (a
+ (d
* (b
/ c
))) / denom
;
164 y
= (b
- (d
* (a
/ c
))) / denom
;
168 /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
169 are nonzero/zero, infinite/finite, and finite/infinite. */
170 if (isnan (x
) && isnan (y
))
172 if (c
== 0.0 && d
== 0.0 && (!isnan (a
) || !isnan (b
)))
174 x
= COPYSIGN (INFINITY
, c
) * a
;
175 y
= COPYSIGN (INFINITY
, c
) * b
;
177 else if ((isinf (a
) || isinf (b
)) && isfinite (c
) && isfinite (d
))
179 a
= COPYSIGN (isinf (a
) ? 1 : 0, a
);
180 b
= COPYSIGN (isinf (b
) ? 1 : 0, b
);
181 x
= INFINITY
* (a
* c
+ b
* d
);
182 y
= INFINITY
* (b
* c
- a
* d
);
184 else if ((isinf (c
) || isinf (d
)) && isfinite (a
) && isfinite (b
))
186 c
= COPYSIGN (isinf (c
) ? 1 : 0, c
);
187 d
= COPYSIGN (isinf (d
) ? 1 : 0, d
);
188 x
= 0.0 * (a
* c
+ b
* d
);
189 y
= 0.0 * (b
* c
- a
* d
);