]>
Commit | Line | Data |
---|---|---|
e4d82761 UD |
1 | /* |
2 | * IBM Accurate Mathematical Library | |
6d7e8eda | 3 | * Copyright (C) 2001-2023 Free Software Foundation, Inc. |
e4d82761 UD |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU Lesser General Public License as published by | |
cc7375ce | 7 | * the Free Software Foundation; either version 2.1 of the License, or |
e4d82761 | 8 | * (at your option) any later version. |
c6c6dd48 | 9 | * |
e4d82761 UD |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
c6c6dd48 | 13 | * GNU Lesser General Public License for more details. |
e4d82761 UD |
14 | * |
15 | * You should have received a copy of the GNU Lesser General Public License | |
5a82c748 | 16 | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
e4d82761 | 17 | */ |
c6c6dd48 | 18 | |
0e9be4db WD |
19 | #include <math.h> |
20 | ||
e4d82761 UD |
21 | /***********************************************************************/ |
22 | /*MODULE_NAME: dla.h */ | |
23 | /* */ | |
24 | /* This file holds C language macros for 'Double Length Floating Point */ | |
25 | /* Arithmetic'. The macros are based on the paper: */ | |
26 | /* T.J.Dekker, "A floating-point Technique for extending the */ | |
27 | /* Available Precision", Number. Math. 18, 224-242 (1971). */ | |
28 | /* A Double-Length number is defined by a pair (r,s), of IEEE double */ | |
29 | /* precision floating point numbers that satisfy, */ | |
30 | /* */ | |
31 | /* abs(s) <= abs(r+s)*2**(-53)/(1+2**(-53)). */ | |
32 | /* */ | |
33 | /* The computer arithmetic assumed is IEEE double precision in */ | |
34 | /* round to nearest mode. All variables in the macros must be of type */ | |
35 | /* IEEE double. */ | |
36 | /***********************************************************************/ | |
37 | ||
085ec079 SP |
38 | /* CN = 1+2**27 = '41a0000002000000' IEEE double format. Use it to split a |
39 | double for better accuracy. */ | |
e4d82761 UD |
40 | #define CN 134217729.0 |
41 | ||
42 | ||
43 | /* Exact addition of two single-length floating point numbers, Dekker. */ | |
44 | /* The macro produces a double-length number (z,zz) that satisfies */ | |
45 | /* z+zz = x+y exactly. */ | |
46 | ||
47 | #define EADD(x,y,z,zz) \ | |
0e9be4db | 48 | z=(x)+(y); zz=(fabs(x)>fabs(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x)); |
e4d82761 UD |
49 | |
50 | ||
51 | /* Exact subtraction of two single-length floating point numbers, Dekker. */ | |
52 | /* The macro produces a double-length number (z,zz) that satisfies */ | |
53 | /* z+zz = x-y exactly. */ | |
54 | ||
55 | #define ESUB(x,y,z,zz) \ | |
0e9be4db | 56 | z=(x)-(y); zz=(fabs(x)>fabs(y)) ? (((x)-(z))-(y)) : ((x)-((y)+(z))); |
e4d82761 UD |
57 | |
58 | ||
f280fa6d JM |
59 | #ifdef __FP_FAST_FMA |
60 | # define DLA_FMS(x, y, z) __builtin_fma (x, y, -(z)) | |
61 | #endif | |
62 | ||
e4d82761 UD |
63 | /* Exact multiplication of two single-length floating point numbers, */ |
64 | /* Veltkamp. The macro produces a double-length number (z,zz) that */ | |
65 | /* satisfies z+zz = x*y exactly. p,hx,tx,hy,ty are temporary */ | |
66 | /* storage variables of type double. */ | |
67 | ||
774a2669 | 68 | #ifdef DLA_FMS |
e93c2643 | 69 | # define EMULV(x, y, z, zz) \ |
c5d5d574 | 70 | z = x * y; zz = DLA_FMS (x, y, z); |
a1a87169 | 71 | #else |
e93c2643 VG |
72 | # define EMULV(x, y, z, zz) \ |
73 | ({ __typeof__ (x) __p, hx, tx, hy, ty; \ | |
74 | __p = CN * (x); hx = ((x) - __p) + __p; tx = (x) - hx; \ | |
75 | __p = CN * (y); hy = ((y) - __p) + __p; ty = (y) - hy; \ | |
76 | z = (x) * (y); zz = (((hx * hy - z) + hx * ty) + tx * hy) + tx * ty; \ | |
77 | }) | |
a1a87169 | 78 | #endif |
e4d82761 UD |
79 | |
80 | ||
81 | /* Exact multiplication of two single-length floating point numbers, Dekker. */ | |
82 | /* The macro produces a nearly double-length number (z,zz) (see Dekker) */ | |
83 | /* that satisfies z+zz = x*y exactly. p,hx,tx,hy,ty,q are temporary */ | |
84 | /* storage variables of type double. */ | |
85 | ||
774a2669 | 86 | #ifdef DLA_FMS |
e93c2643 VG |
87 | # define MUL12(x, y, z, zz) \ |
88 | EMULV(x, y, z, zz) | |
a1a87169 | 89 | #else |
e93c2643 VG |
90 | # define MUL12(x, y, z, zz) \ |
91 | ({ __typeof__ (x) __p, hx, tx, hy, ty, __q; \ | |
92 | __p=CN*(x); hx=((x)-__p)+__p; tx=(x)-hx; \ | |
93 | __p=CN*(y); hy=((y)-__p)+__p; ty=(y)-hy; \ | |
94 | __p=hx*hy; __q=hx*ty+tx*hy; z=__p+__q; zz=((__p-z)+__q)+tx*ty; \ | |
95 | }) | |
a1a87169 | 96 | #endif |
e4d82761 UD |
97 | |
98 | ||
99 | /* Double-length addition, Dekker. The macro produces a double-length */ | |
100 | /* number (z,zz) which satisfies approximately z+zz = x+xx + y+yy. */ | |
101 | /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */ | |
102 | /* are assumed to be double-length numbers. r,s are temporary */ | |
103 | /* storage variables of type double. */ | |
104 | ||
c5d5d574 | 105 | #define ADD2(x, xx, y, yy, z, zz, r, s) \ |
0e9be4db | 106 | r = (x) + (y); s = (fabs (x) > fabs (y)) ? \ |
c5d5d574 OB |
107 | (((((x) - r) + (y)) + (yy)) + (xx)) : \ |
108 | (((((y) - r) + (x)) + (xx)) + (yy)); \ | |
109 | z = r + s; zz = (r - z) + s; | |
e4d82761 UD |
110 | |
111 | ||
112 | /* Double-length subtraction, Dekker. The macro produces a double-length */ | |
113 | /* number (z,zz) which satisfies approximately z+zz = x+xx - (y+yy). */ | |
114 | /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */ | |
115 | /* are assumed to be double-length numbers. r,s are temporary */ | |
116 | /* storage variables of type double. */ | |
117 | ||
c5d5d574 | 118 | #define SUB2(x, xx, y, yy, z, zz, r, s) \ |
0e9be4db | 119 | r = (x) - (y); s = (fabs (x) > fabs (y)) ? \ |
c5d5d574 OB |
120 | (((((x) - r) - (y)) - (yy)) + (xx)) : \ |
121 | ((((x) - ((y) + r)) + (xx)) - (yy)); \ | |
122 | z = r + s; zz = (r - z) + s; | |
e4d82761 UD |
123 | |
124 | ||
125 | /* Double-length multiplication, Dekker. The macro produces a double-length */ | |
126 | /* number (z,zz) which satisfies approximately z+zz = (x+xx)*(y+yy). */ | |
127 | /* An error bound: abs((x+xx)*(y+yy))*1.24e-31. (x,xx), (y,yy) */ | |
128 | /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc are */ | |
129 | /* temporary storage variables of type double. */ | |
130 | ||
e93c2643 VG |
131 | #define MUL2(x, xx, y, yy, z, zz, c, cc) \ |
132 | MUL12 (x, y, c, cc); \ | |
c5d5d574 | 133 | cc = ((x) * (yy) + (xx) * (y)) + cc; z = c + cc; zz = (c - z) + cc; |
e4d82761 UD |
134 | |
135 | ||
136 | /* Double-length division, Dekker. The macro produces a double-length */ | |
137 | /* number (z,zz) which satisfies approximately z+zz = (x+xx)/(y+yy). */ | |
138 | /* An error bound: abs((x+xx)/(y+yy))*1.50e-31. (x,xx), (y,yy) */ | |
139 | /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc,u,uu */ | |
140 | /* are temporary storage variables of type double. */ | |
141 | ||
e93c2643 VG |
142 | #define DIV2(x, xx, y, yy, z, zz, c, cc, u, uu) \ |
143 | c=(x)/(y); MUL12(c,y,u,uu); \ | |
a1a87169 | 144 | cc=(((((x)-u)-uu)+(xx))-c*(yy))/(y); z=c+cc; zz=(c-z)+cc; |
e4d82761 UD |
145 | |
146 | ||
147 | /* Double-length addition, slower but more accurate than ADD2. */ | |
148 | /* The macro produces a double-length */ | |
149 | /* number (z,zz) which satisfies approximately z+zz = (x+xx)+(y+yy). */ | |
150 | /* An error bound: abs(x+xx + y+yy)*1.50e-31. (x,xx), (y,yy) */ | |
151 | /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w */ | |
152 | /* are temporary storage variables of type double. */ | |
153 | ||
c5d5d574 OB |
154 | #define ADD2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w) \ |
155 | r = (x) + (y); \ | |
0e9be4db | 156 | if (fabs (x) > fabs (y)) { rr = ((x) - r) + (y); s = (rr + (yy)) + (xx); } \ |
c5d5d574 OB |
157 | else { rr = ((y) - r) + (x); s = (rr + (xx)) + (yy); } \ |
158 | if (rr != 0.0) { \ | |
159 | z = r + s; zz = (r - z) + s; } \ | |
160 | else { \ | |
0e9be4db | 161 | ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) + (yy)) : (((yy) - s) + (xx));\ |
c5d5d574 | 162 | u = r + s; \ |
0e9be4db | 163 | uu = (fabs (r) > fabs (s)) ? ((r - u) + s) : ((s - u) + r); \ |
c5d5d574 | 164 | w = uu + ss; z = u + w; \ |
0e9be4db | 165 | zz = (fabs (u) > fabs (w)) ? ((u - z) + w) : ((w - z) + u); } |
e4d82761 UD |
166 | |
167 | ||
168 | /* Double-length subtraction, slower but more accurate than SUB2. */ | |
169 | /* The macro produces a double-length */ | |
170 | /* number (z,zz) which satisfies approximately z+zz = (x+xx)-(y+yy). */ | |
171 | /* An error bound: abs(x+xx - (y+yy))*1.50e-31. (x,xx), (y,yy) */ | |
172 | /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w */ | |
173 | /* are temporary storage variables of type double. */ | |
174 | ||
c5d5d574 OB |
175 | #define SUB2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w) \ |
176 | r = (x) - (y); \ | |
0e9be4db | 177 | if (fabs (x) > fabs (y)) { rr = ((x) - r) - (y); s = (rr - (yy)) + (xx); } \ |
c5d5d574 OB |
178 | else { rr = (x) - ((y) + r); s = (rr + (xx)) - (yy); } \ |
179 | if (rr != 0.0) { \ | |
180 | z = r + s; zz = (r - z) + s; } \ | |
181 | else { \ | |
0e9be4db | 182 | ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) - (yy)) : ((xx) - ((yy) + s)); \ |
c5d5d574 | 183 | u = r + s; \ |
0e9be4db | 184 | uu = (fabs (r) > fabs (s)) ? ((r - u) + s) : ((s - u) + r); \ |
c5d5d574 | 185 | w = uu + ss; z = u + w; \ |
0e9be4db | 186 | zz = (fabs (u) > fabs (w)) ? ((u - z) + w) : ((w - z) + u); } |