]>
Commit | Line | Data |
---|---|---|
e4d82761 UD |
1 | /* |
2 | * IBM Accurate Mathematical Library | |
c6c6dd48 | 3 | * Written by International Business Machines Corp. |
f7a9f785 | 4 | * Copyright (C) 2001-2016 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 | ||
60 | /* Exact multiplication of two single-length floating point numbers, */ | |
61 | /* Veltkamp. The macro produces a double-length number (z,zz) that */ | |
62 | /* satisfies z+zz = x*y exactly. p,hx,tx,hy,ty are temporary */ | |
63 | /* storage variables of type double. */ | |
64 | ||
774a2669 | 65 | #ifdef DLA_FMS |
c5d5d574 OB |
66 | # define EMULV(x, y, z, zz, p, hx, tx, hy, ty) \ |
67 | z = x * y; zz = DLA_FMS (x, y, z); | |
a1a87169 | 68 | #else |
c5d5d574 OB |
69 | # define EMULV(x, y, z, zz, p, hx, tx, hy, ty) \ |
70 | p = CN * (x); hx = ((x) - p) + p; tx = (x) - hx; \ | |
71 | p = CN * (y); hy = ((y) - p) + p; ty = (y) - hy; \ | |
72 | z = (x) * (y); zz = (((hx * hy - z) + hx * ty) + tx * hy) + tx * ty; | |
a1a87169 | 73 | #endif |
e4d82761 UD |
74 | |
75 | ||
76 | /* Exact multiplication of two single-length floating point numbers, Dekker. */ | |
77 | /* The macro produces a nearly double-length number (z,zz) (see Dekker) */ | |
78 | /* that satisfies z+zz = x*y exactly. p,hx,tx,hy,ty,q are temporary */ | |
79 | /* storage variables of type double. */ | |
80 | ||
774a2669 | 81 | #ifdef DLA_FMS |
a1a87169 UD |
82 | # define MUL12(x,y,z,zz,p,hx,tx,hy,ty,q) \ |
83 | EMULV(x,y,z,zz,p,hx,tx,hy,ty) | |
84 | #else | |
85 | # define MUL12(x,y,z,zz,p,hx,tx,hy,ty,q) \ | |
86 | p=CN*(x); hx=((x)-p)+p; tx=(x)-hx; \ | |
87 | p=CN*(y); hy=((y)-p)+p; ty=(y)-hy; \ | |
88 | p=hx*hy; q=hx*ty+tx*hy; z=p+q; zz=((p-z)+q)+tx*ty; | |
89 | #endif | |
e4d82761 UD |
90 | |
91 | ||
92 | /* Double-length addition, Dekker. The macro produces a double-length */ | |
93 | /* number (z,zz) which satisfies approximately z+zz = x+xx + y+yy. */ | |
94 | /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */ | |
95 | /* are assumed to be double-length numbers. r,s are temporary */ | |
96 | /* storage variables of type double. */ | |
97 | ||
c5d5d574 | 98 | #define ADD2(x, xx, y, yy, z, zz, r, s) \ |
0e9be4db | 99 | r = (x) + (y); s = (fabs (x) > fabs (y)) ? \ |
c5d5d574 OB |
100 | (((((x) - r) + (y)) + (yy)) + (xx)) : \ |
101 | (((((y) - r) + (x)) + (xx)) + (yy)); \ | |
102 | z = r + s; zz = (r - z) + s; | |
e4d82761 UD |
103 | |
104 | ||
105 | /* Double-length subtraction, Dekker. The macro produces a double-length */ | |
106 | /* number (z,zz) which satisfies approximately z+zz = x+xx - (y+yy). */ | |
107 | /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */ | |
108 | /* are assumed to be double-length numbers. r,s are temporary */ | |
109 | /* storage variables of type double. */ | |
110 | ||
c5d5d574 | 111 | #define SUB2(x, xx, y, yy, z, zz, r, s) \ |
0e9be4db | 112 | r = (x) - (y); s = (fabs (x) > fabs (y)) ? \ |
c5d5d574 OB |
113 | (((((x) - r) - (y)) - (yy)) + (xx)) : \ |
114 | ((((x) - ((y) + r)) + (xx)) - (yy)); \ | |
115 | z = r + s; zz = (r - z) + s; | |
e4d82761 UD |
116 | |
117 | ||
118 | /* Double-length multiplication, Dekker. The macro produces a double-length */ | |
119 | /* number (z,zz) which satisfies approximately z+zz = (x+xx)*(y+yy). */ | |
120 | /* An error bound: abs((x+xx)*(y+yy))*1.24e-31. (x,xx), (y,yy) */ | |
121 | /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc are */ | |
122 | /* temporary storage variables of type double. */ | |
123 | ||
c5d5d574 OB |
124 | #define MUL2(x, xx, y, yy, z, zz, p, hx, tx, hy, ty, q, c, cc) \ |
125 | MUL12 (x, y, c, cc, p, hx, tx, hy, ty, q) \ | |
126 | cc = ((x) * (yy) + (xx) * (y)) + cc; z = c + cc; zz = (c - z) + cc; | |
e4d82761 UD |
127 | |
128 | ||
129 | /* Double-length division, Dekker. The macro produces a double-length */ | |
130 | /* number (z,zz) which satisfies approximately z+zz = (x+xx)/(y+yy). */ | |
131 | /* An error bound: abs((x+xx)/(y+yy))*1.50e-31. (x,xx), (y,yy) */ | |
132 | /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc,u,uu */ | |
133 | /* are temporary storage variables of type double. */ | |
134 | ||
135 | #define DIV2(x,xx,y,yy,z,zz,p,hx,tx,hy,ty,q,c,cc,u,uu) \ | |
a1a87169 UD |
136 | c=(x)/(y); MUL12(c,y,u,uu,p,hx,tx,hy,ty,q) \ |
137 | cc=(((((x)-u)-uu)+(xx))-c*(yy))/(y); z=c+cc; zz=(c-z)+cc; | |
e4d82761 UD |
138 | |
139 | ||
140 | /* Double-length addition, slower but more accurate than ADD2. */ | |
141 | /* The macro produces a double-length */ | |
142 | /* number (z,zz) which satisfies approximately z+zz = (x+xx)+(y+yy). */ | |
143 | /* An error bound: abs(x+xx + y+yy)*1.50e-31. (x,xx), (y,yy) */ | |
144 | /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w */ | |
145 | /* are temporary storage variables of type double. */ | |
146 | ||
c5d5d574 OB |
147 | #define ADD2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w) \ |
148 | r = (x) + (y); \ | |
0e9be4db | 149 | if (fabs (x) > fabs (y)) { rr = ((x) - r) + (y); s = (rr + (yy)) + (xx); } \ |
c5d5d574 OB |
150 | else { rr = ((y) - r) + (x); s = (rr + (xx)) + (yy); } \ |
151 | if (rr != 0.0) { \ | |
152 | z = r + s; zz = (r - z) + s; } \ | |
153 | else { \ | |
0e9be4db | 154 | ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) + (yy)) : (((yy) - s) + (xx));\ |
c5d5d574 | 155 | u = r + s; \ |
0e9be4db | 156 | uu = (fabs (r) > fabs (s)) ? ((r - u) + s) : ((s - u) + r); \ |
c5d5d574 | 157 | w = uu + ss; z = u + w; \ |
0e9be4db | 158 | zz = (fabs (u) > fabs (w)) ? ((u - z) + w) : ((w - z) + u); } |
e4d82761 UD |
159 | |
160 | ||
161 | /* Double-length subtraction, slower but more accurate than SUB2. */ | |
162 | /* The macro produces a double-length */ | |
163 | /* number (z,zz) which satisfies approximately z+zz = (x+xx)-(y+yy). */ | |
164 | /* An error bound: abs(x+xx - (y+yy))*1.50e-31. (x,xx), (y,yy) */ | |
165 | /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w */ | |
166 | /* are temporary storage variables of type double. */ | |
167 | ||
c5d5d574 OB |
168 | #define SUB2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w) \ |
169 | r = (x) - (y); \ | |
0e9be4db | 170 | if (fabs (x) > fabs (y)) { rr = ((x) - r) - (y); s = (rr - (yy)) + (xx); } \ |
c5d5d574 OB |
171 | else { rr = (x) - ((y) + r); s = (rr + (xx)) - (yy); } \ |
172 | if (rr != 0.0) { \ | |
173 | z = r + s; zz = (r - z) + s; } \ | |
174 | else { \ | |
0e9be4db | 175 | ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) - (yy)) : ((xx) - ((yy) + s)); \ |
c5d5d574 | 176 | u = r + s; \ |
0e9be4db | 177 | uu = (fabs (r) > fabs (s)) ? ((r - u) + s) : ((s - u) + r); \ |
c5d5d574 | 178 | w = uu + ss; z = u + w; \ |
0e9be4db | 179 | zz = (fabs (u) > fabs (w)) ? ((u - z) + w) : ((w - z) + u); } |