1 /* Compute x * y + z as ternary operation.
2 Copyright (C) 2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2010.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 /* This implementation uses rounding to odd to avoid problems with
27 double rounding. See a paper by Boldo and Melquiond:
28 http://www.lri.fr/~melquion/doc/08-tc.pdf */
31 __fmal (long double x
, long double y
, long double z
)
33 union ieee854_long_double u
, v
, w
;
38 if (__builtin_expect (u
.ieee
.exponent
+ v
.ieee
.exponent
39 >= 0x7fff + IEEE854_LONG_DOUBLE_BIAS
41 || __builtin_expect (u
.ieee
.exponent
>= 0x7fff - LDBL_MANT_DIG
, 0)
42 || __builtin_expect (v
.ieee
.exponent
>= 0x7fff - LDBL_MANT_DIG
, 0)
43 || __builtin_expect (w
.ieee
.exponent
>= 0x7fff - LDBL_MANT_DIG
, 0)
44 || __builtin_expect (u
.ieee
.exponent
+ v
.ieee
.exponent
45 <= IEEE854_LONG_DOUBLE_BIAS
+ LDBL_MANT_DIG
, 0))
47 /* If z is Inf, but x and y are finite, the result should be
49 if (w
.ieee
.exponent
== 0x7fff
50 && u
.ieee
.exponent
!= 0x7fff
51 && v
.ieee
.exponent
!= 0x7fff)
53 /* If x or y or z is Inf/NaN, or if fma will certainly overflow,
54 or if x * y is less than half of LDBL_DENORM_MIN,
55 compute as x * y + z. */
56 if (u
.ieee
.exponent
== 0x7fff
57 || v
.ieee
.exponent
== 0x7fff
58 || w
.ieee
.exponent
== 0x7fff
59 || u
.ieee
.exponent
+ v
.ieee
.exponent
60 > 0x7fff + IEEE854_LONG_DOUBLE_BIAS
61 || u
.ieee
.exponent
+ v
.ieee
.exponent
62 < IEEE854_LONG_DOUBLE_BIAS
- LDBL_MANT_DIG
- 2)
64 if (u
.ieee
.exponent
+ v
.ieee
.exponent
65 >= 0x7fff + IEEE854_LONG_DOUBLE_BIAS
- LDBL_MANT_DIG
)
67 /* Compute 1p-113 times smaller result and multiply
69 if (u
.ieee
.exponent
> v
.ieee
.exponent
)
70 u
.ieee
.exponent
-= LDBL_MANT_DIG
;
72 v
.ieee
.exponent
-= LDBL_MANT_DIG
;
73 /* If x + y exponent is very large and z exponent is very small,
74 it doesn't matter if we don't adjust it. */
75 if (w
.ieee
.exponent
> LDBL_MANT_DIG
)
76 w
.ieee
.exponent
-= LDBL_MANT_DIG
;
79 else if (w
.ieee
.exponent
>= 0x7fff - LDBL_MANT_DIG
)
82 If z exponent is very large and x and y exponents are
83 very small, it doesn't matter if we don't adjust it. */
84 if (u
.ieee
.exponent
> v
.ieee
.exponent
)
86 if (u
.ieee
.exponent
> LDBL_MANT_DIG
)
87 u
.ieee
.exponent
-= LDBL_MANT_DIG
;
89 else if (v
.ieee
.exponent
> LDBL_MANT_DIG
)
90 v
.ieee
.exponent
-= LDBL_MANT_DIG
;
91 w
.ieee
.exponent
-= LDBL_MANT_DIG
;
94 else if (u
.ieee
.exponent
>= 0x7fff - LDBL_MANT_DIG
)
96 u
.ieee
.exponent
-= LDBL_MANT_DIG
;
98 v
.ieee
.exponent
+= LDBL_MANT_DIG
;
102 else if (v
.ieee
.exponent
>= 0x7fff - LDBL_MANT_DIG
)
104 v
.ieee
.exponent
-= LDBL_MANT_DIG
;
106 u
.ieee
.exponent
+= LDBL_MANT_DIG
;
110 else /* if (u.ieee.exponent + v.ieee.exponent
111 <= IEEE854_LONG_DOUBLE_BIAS + LDBL_MANT_DIG) */
113 if (u
.ieee
.exponent
> v
.ieee
.exponent
)
114 u
.ieee
.exponent
+= 2 * LDBL_MANT_DIG
;
116 v
.ieee
.exponent
+= 2 * LDBL_MANT_DIG
;
117 if (w
.ieee
.exponent
<= 4 * LDBL_MANT_DIG
+ 4)
120 w
.ieee
.exponent
+= 2 * LDBL_MANT_DIG
;
125 /* Otherwise x * y should just affect inexact
132 /* Multiplication m1 + m2 = x * y using Dekker's algorithm. */
133 #define C ((1LL << (LDBL_MANT_DIG + 1) / 2) + 1)
134 long double x1
= x
* C
;
135 long double y1
= y
* C
;
136 long double m1
= x
* y
;
139 long double x2
= x
- x1
;
140 long double y2
= y
- y1
;
141 long double m2
= (((x1
* y1
- m1
) + x1
* y2
) + x2
* y1
) + x2
* y2
;
143 /* Addition a1 + a2 = z + m1 using Knuth's algorithm. */
144 long double a1
= z
+ m1
;
145 long double t1
= a1
- z
;
146 long double t2
= a1
- t1
;
149 long double a2
= t1
+ t2
;
153 fesetround (FE_TOWARDZERO
);
154 /* Perform m2 + a2 addition with round to odd. */
157 if (__builtin_expect (adjust
== 0, 1))
159 if ((u
.ieee
.mantissa3
& 1) == 0 && u
.ieee
.exponent
!= 0x7fff)
160 u
.ieee
.mantissa3
|= fetestexcept (FE_INEXACT
) != 0;
162 /* Result is a1 + u.d. */
165 else if (__builtin_expect (adjust
> 0, 1))
167 if ((u
.ieee
.mantissa3
& 1) == 0 && u
.ieee
.exponent
!= 0x7fff)
168 u
.ieee
.mantissa3
|= fetestexcept (FE_INEXACT
) != 0;
170 /* Result is a1 + u.d, scaled up. */
171 return (a1
+ u
.d
) * 0x1p
113L;
175 if ((u
.ieee
.mantissa3
& 1) == 0)
176 u
.ieee
.mantissa3
|= fetestexcept (FE_INEXACT
) != 0;
178 int j
= fetestexcept (FE_INEXACT
) != 0;
180 /* Ensure the following computations are performed in default rounding
181 mode instead of just reusing the round to zero computation. */
182 asm volatile ("" : "=m" (u
) : "m" (u
));
183 /* If a1 + u.d is exact, the only rounding happens during
186 return v
.d
* 0x1p
-226L;
187 /* If result rounded to zero is not subnormal, no double
188 rounding will occur. */
189 if (v
.ieee
.exponent
> 226)
190 return (a1
+ u
.d
) * 0x1p
-226L;
191 /* If v.d * 0x1p-226L with round to zero is a subnormal above
192 or equal to LDBL_MIN / 2, then v.d * 0x1p-226L shifts mantissa
193 down just by 1 bit, which means v.ieee.mantissa3 |= j would
194 change the round bit, not sticky or guard bit.
195 v.d * 0x1p-226L never normalizes by shifting up,
196 so round bit plus sticky bit should be already enough
197 for proper rounding. */
198 if (v
.ieee
.exponent
== 226)
200 /* v.ieee.mantissa3 & 2 is LSB bit of the result before rounding,
201 v.ieee.mantissa3 & 1 is the round bit and j is our sticky
202 bit. In round-to-nearest 001 rounds down like 00,
203 011 rounds up, even though 01 rounds down (thus we need
204 to adjust), 101 rounds down like 10 and 111 rounds up
206 if ((v
.ieee
.mantissa3
& 3) == 1)
210 return v
.d
- 0x1p
-16493L /* __LDBL_DENORM_MIN__ */;
212 return v
.d
+ 0x1p
-16493L /* __LDBL_DENORM_MIN__ */;
215 return v
.d
* 0x1p
-226L;
217 v
.ieee
.mantissa3
|= j
;
218 return v
.d
* 0x1p
-226L;
221 weak_alias (__fmal
, fmal
)