]>
Commit | Line | Data |
---|---|---|
e4d82761 UD |
1 | /* |
2 | * IBM Accurate Mathematical Library | |
c6c6dd48 | 3 | * Written by International Business Machines Corp. |
688903eb | 4 | * Copyright (C) 2001-2018 Free Software Foundation, Inc. |
e4d82761 UD |
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 | |
cc7375ce | 8 | * the Free Software Foundation; either version 2.1 of the License, or |
e4d82761 | 9 | * (at your option) any later version. |
c6c6dd48 | 10 | * |
e4d82761 UD |
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 | |
c6c6dd48 | 14 | * GNU Lesser General Public License for more details. |
e4d82761 UD |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public License | |
59ba27a6 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
e4d82761 | 18 | */ |
c6c6dd48 | 19 | |
0e9be4db WD |
20 | #include <math.h> |
21 | ||
e4d82761 UD |
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 | ||
085ec079 SP |
39 | /* CN = 1+2**27 = '41a0000002000000' IEEE double format. Use it to split a |
40 | double for better accuracy. */ | |
e4d82761 UD |
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) \ | |
0e9be4db | 49 | z=(x)+(y); zz=(fabs(x)>fabs(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x)); |
e4d82761 UD |
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) \ | |
0e9be4db | 57 | z=(x)-(y); zz=(fabs(x)>fabs(y)) ? (((x)-(z))-(y)) : ((x)-((y)+(z))); |
e4d82761 UD |
58 | |
59 | ||
f280fa6d JM |
60 | #ifdef __FP_FAST_FMA |
61 | # define DLA_FMS(x, y, z) __builtin_fma (x, y, -(z)) | |
62 | #endif | |
63 | ||
e4d82761 UD |
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 | ||
774a2669 | 69 | #ifdef DLA_FMS |
c5d5d574 OB |
70 | # define EMULV(x, y, z, zz, p, hx, tx, hy, ty) \ |
71 | z = x * y; zz = DLA_FMS (x, y, z); | |
a1a87169 | 72 | #else |
c5d5d574 OB |
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; | |
a1a87169 | 77 | #endif |
e4d82761 UD |
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 | ||
774a2669 | 85 | #ifdef DLA_FMS |
a1a87169 UD |
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 | |
e4d82761 UD |
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 | ||
c5d5d574 | 102 | #define ADD2(x, xx, y, yy, z, zz, r, s) \ |
0e9be4db | 103 | r = (x) + (y); s = (fabs (x) > fabs (y)) ? \ |
c5d5d574 OB |
104 | (((((x) - r) + (y)) + (yy)) + (xx)) : \ |
105 | (((((y) - r) + (x)) + (xx)) + (yy)); \ | |
106 | z = r + s; zz = (r - z) + s; | |
e4d82761 UD |
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 | ||
c5d5d574 | 115 | #define SUB2(x, xx, y, yy, z, zz, r, s) \ |
0e9be4db | 116 | r = (x) - (y); s = (fabs (x) > fabs (y)) ? \ |
c5d5d574 OB |
117 | (((((x) - r) - (y)) - (yy)) + (xx)) : \ |
118 | ((((x) - ((y) + r)) + (xx)) - (yy)); \ | |
119 | z = r + s; zz = (r - z) + s; | |
e4d82761 UD |
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 | ||
c5d5d574 OB |
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; | |
e4d82761 UD |
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) \ | |
a1a87169 UD |
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; | |
e4d82761 UD |
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 | ||
c5d5d574 OB |
151 | #define ADD2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w) \ |
152 | r = (x) + (y); \ | |
0e9be4db | 153 | if (fabs (x) > fabs (y)) { rr = ((x) - r) + (y); s = (rr + (yy)) + (xx); } \ |
c5d5d574 OB |
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 { \ | |
0e9be4db | 158 | ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) + (yy)) : (((yy) - s) + (xx));\ |
c5d5d574 | 159 | u = r + s; \ |
0e9be4db | 160 | uu = (fabs (r) > fabs (s)) ? ((r - u) + s) : ((s - u) + r); \ |
c5d5d574 | 161 | w = uu + ss; z = u + w; \ |
0e9be4db | 162 | zz = (fabs (u) > fabs (w)) ? ((u - z) + w) : ((w - z) + u); } |
e4d82761 UD |
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 | ||
c5d5d574 OB |
172 | #define SUB2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w) \ |
173 | r = (x) - (y); \ | |
0e9be4db | 174 | if (fabs (x) > fabs (y)) { rr = ((x) - r) - (y); s = (rr - (yy)) + (xx); } \ |
c5d5d574 OB |
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 { \ | |
0e9be4db | 179 | ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) - (yy)) : ((xx) - ((yy) + s)); \ |
c5d5d574 | 180 | u = r + s; \ |
0e9be4db | 181 | uu = (fabs (r) > fabs (s)) ? ((r - u) + s) : ((s - u) + r); \ |
c5d5d574 | 182 | w = uu + ss; z = u + w; \ |
0e9be4db | 183 | zz = (fabs (u) > fabs (w)) ? ((u - z) + w) : ((w - z) + u); } |