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