]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/ieee754/dbl-64/dla.h
Demystify the magic number 134217729.0
[thirdparty/glibc.git] / sysdeps / ieee754 / dbl-64 / dla.h
1 /*
2 * IBM Accurate Mathematical Library
3 * Written by International Business Machines Corp.
4 * Copyright (C) 2001-2012 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 /***********************************************************************/
21 /*MODULE_NAME: dla.h */
22 /* */
23 /* This file holds C language macros for 'Double Length Floating Point */
24 /* Arithmetic'. The macros are based on the paper: */
25 /* T.J.Dekker, "A floating-point Technique for extending the */
26 /* Available Precision", Number. Math. 18, 224-242 (1971). */
27 /* A Double-Length number is defined by a pair (r,s), of IEEE double */
28 /* precision floating point numbers that satisfy, */
29 /* */
30 /* abs(s) <= abs(r+s)*2**(-53)/(1+2**(-53)). */
31 /* */
32 /* The computer arithmetic assumed is IEEE double precision in */
33 /* round to nearest mode. All variables in the macros must be of type */
34 /* IEEE double. */
35 /***********************************************************************/
36
37 /* CN = 1+2**27 = '41a0000002000000' IEEE double format. Use it to split a
38 double for better accuracy. */
39 #define CN 134217729.0
40
41
42 /* Exact addition of two single-length floating point numbers, Dekker. */
43 /* The macro produces a double-length number (z,zz) that satisfies */
44 /* z+zz = x+y exactly. */
45
46 #define EADD(x,y,z,zz) \
47 z=(x)+(y); zz=(ABS(x)>ABS(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x));
48
49
50 /* Exact subtraction of two single-length floating point numbers, Dekker. */
51 /* The macro produces a double-length number (z,zz) that satisfies */
52 /* z+zz = x-y exactly. */
53
54 #define ESUB(x,y,z,zz) \
55 z=(x)-(y); zz=(ABS(x)>ABS(y)) ? (((x)-(z))-(y)) : ((x)-((y)+(z)));
56
57
58 /* Exact multiplication of two single-length floating point numbers, */
59 /* Veltkamp. The macro produces a double-length number (z,zz) that */
60 /* satisfies z+zz = x*y exactly. p,hx,tx,hy,ty are temporary */
61 /* storage variables of type double. */
62
63 #ifdef DLA_FMS
64 # define EMULV(x,y,z,zz,p,hx,tx,hy,ty) \
65 z=x*y; zz=DLA_FMS(x,y,z);
66 #else
67 # define EMULV(x,y,z,zz,p,hx,tx,hy,ty) \
68 p=CN*(x); hx=((x)-p)+p; tx=(x)-hx; \
69 p=CN*(y); hy=((y)-p)+p; ty=(y)-hy; \
70 z=(x)*(y); zz=(((hx*hy-z)+hx*ty)+tx*hy)+tx*ty;
71 #endif
72
73
74 /* Exact multiplication of two single-length floating point numbers, Dekker. */
75 /* The macro produces a nearly double-length number (z,zz) (see Dekker) */
76 /* that satisfies z+zz = x*y exactly. p,hx,tx,hy,ty,q are temporary */
77 /* storage variables of type double. */
78
79 #ifdef DLA_FMS
80 # define MUL12(x,y,z,zz,p,hx,tx,hy,ty,q) \
81 EMULV(x,y,z,zz,p,hx,tx,hy,ty)
82 #else
83 # define MUL12(x,y,z,zz,p,hx,tx,hy,ty,q) \
84 p=CN*(x); hx=((x)-p)+p; tx=(x)-hx; \
85 p=CN*(y); hy=((y)-p)+p; ty=(y)-hy; \
86 p=hx*hy; q=hx*ty+tx*hy; z=p+q; zz=((p-z)+q)+tx*ty;
87 #endif
88
89
90 /* Double-length addition, Dekker. The macro produces a double-length */
91 /* number (z,zz) which satisfies approximately z+zz = x+xx + y+yy. */
92 /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */
93 /* are assumed to be double-length numbers. r,s are temporary */
94 /* storage variables of type double. */
95
96 #define ADD2(x,xx,y,yy,z,zz,r,s) \
97 r=(x)+(y); s=(ABS(x)>ABS(y)) ? \
98 (((((x)-r)+(y))+(yy))+(xx)) : \
99 (((((y)-r)+(x))+(xx))+(yy)); \
100 z=r+s; zz=(r-z)+s;
101
102
103 /* Double-length subtraction, Dekker. The macro produces a double-length */
104 /* number (z,zz) which satisfies approximately z+zz = x+xx - (y+yy). */
105 /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */
106 /* are assumed to be double-length numbers. r,s are temporary */
107 /* storage variables of type double. */
108
109 #define SUB2(x,xx,y,yy,z,zz,r,s) \
110 r=(x)-(y); s=(ABS(x)>ABS(y)) ? \
111 (((((x)-r)-(y))-(yy))+(xx)) : \
112 ((((x)-((y)+r))+(xx))-(yy)); \
113 z=r+s; zz=(r-z)+s;
114
115
116 /* Double-length multiplication, Dekker. The macro produces a double-length */
117 /* number (z,zz) which satisfies approximately z+zz = (x+xx)*(y+yy). */
118 /* An error bound: abs((x+xx)*(y+yy))*1.24e-31. (x,xx), (y,yy) */
119 /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc are */
120 /* temporary storage variables of type double. */
121
122 #define MUL2(x,xx,y,yy,z,zz,p,hx,tx,hy,ty,q,c,cc) \
123 MUL12(x,y,c,cc,p,hx,tx,hy,ty,q) \
124 cc=((x)*(yy)+(xx)*(y))+cc; z=c+cc; zz=(c-z)+cc;
125
126
127 /* Double-length division, Dekker. The macro produces a double-length */
128 /* number (z,zz) which satisfies approximately z+zz = (x+xx)/(y+yy). */
129 /* An error bound: abs((x+xx)/(y+yy))*1.50e-31. (x,xx), (y,yy) */
130 /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc,u,uu */
131 /* are temporary storage variables of type double. */
132
133 #define DIV2(x,xx,y,yy,z,zz,p,hx,tx,hy,ty,q,c,cc,u,uu) \
134 c=(x)/(y); MUL12(c,y,u,uu,p,hx,tx,hy,ty,q) \
135 cc=(((((x)-u)-uu)+(xx))-c*(yy))/(y); z=c+cc; zz=(c-z)+cc;
136
137
138 /* Double-length addition, slower but more accurate than ADD2. */
139 /* The macro produces a double-length */
140 /* number (z,zz) which satisfies approximately z+zz = (x+xx)+(y+yy). */
141 /* An error bound: abs(x+xx + y+yy)*1.50e-31. (x,xx), (y,yy) */
142 /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w */
143 /* are temporary storage variables of type double. */
144
145 #define ADD2A(x,xx,y,yy,z,zz,r,rr,s,ss,u,uu,w) \
146 r=(x)+(y); \
147 if (ABS(x)>ABS(y)) { rr=((x)-r)+(y); s=(rr+(yy))+(xx); } \
148 else { rr=((y)-r)+(x); s=(rr+(xx))+(yy); } \
149 if (rr!=0.0) { \
150 z=r+s; zz=(r-z)+s; } \
151 else { \
152 ss=(ABS(xx)>ABS(yy)) ? (((xx)-s)+(yy)) : (((yy)-s)+(xx)); \
153 u=r+s; \
154 uu=(ABS(r)>ABS(s)) ? ((r-u)+s) : ((s-u)+r) ; \
155 w=uu+ss; z=u+w; \
156 zz=(ABS(u)>ABS(w)) ? ((u-z)+w) : ((w-z)+u) ; }
157
158
159 /* Double-length subtraction, slower but more accurate than SUB2. */
160 /* The macro produces a double-length */
161 /* number (z,zz) which satisfies approximately z+zz = (x+xx)-(y+yy). */
162 /* An error bound: abs(x+xx - (y+yy))*1.50e-31. (x,xx), (y,yy) */
163 /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w */
164 /* are temporary storage variables of type double. */
165
166 #define SUB2A(x,xx,y,yy,z,zz,r,rr,s,ss,u,uu,w) \
167 r=(x)-(y); \
168 if (ABS(x)>ABS(y)) { rr=((x)-r)-(y); s=(rr-(yy))+(xx); } \
169 else { rr=(x)-((y)+r); s=(rr+(xx))-(yy); } \
170 if (rr!=0.0) { \
171 z=r+s; zz=(r-z)+s; } \
172 else { \
173 ss=(ABS(xx)>ABS(yy)) ? (((xx)-s)-(yy)) : ((xx)-((yy)+s)); \
174 u=r+s; \
175 uu=(ABS(r)>ABS(s)) ? ((r-u)+s) : ((s-u)+r) ; \
176 w=uu+ss; z=u+w; \
177 zz=(ABS(u)>ABS(w)) ? ((u-z)+w) : ((w-z)+u) ; }