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