about summary refs log tree commit diff
path: root/sysdeps/ieee754/dbl-64/dla.h
blob: d6630e309b2ed7c23f6b87d1e7f56f460b89a58d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * IBM Accurate Mathematical Library
 * Copyright (C) 2001-2022 Free Software Foundation, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <math.h>

/***********************************************************************/
/*MODULE_NAME: dla.h                                                   */
/*                                                                     */
/* This file holds C language macros for 'Double Length Floating Point */
/* Arithmetic'. The macros are based on the paper:                     */
/* T.J.Dekker, "A floating-point Technique for extending the           */
/* Available Precision", Number. Math. 18, 224-242 (1971).              */
/* A Double-Length number is defined by a pair (r,s), of IEEE double    */
/* precision floating point numbers that satisfy,                      */
/*                                                                     */
/*              abs(s) <= abs(r+s)*2**(-53)/(1+2**(-53)).              */
/*                                                                     */
/* The computer arithmetic assumed is IEEE double precision in         */
/* round to nearest mode. All variables in the macros must be of type  */
/* IEEE double.                                                        */
/***********************************************************************/

/* CN = 1+2**27 = '41a0000002000000' IEEE double format.  Use it to split a
   double for better accuracy.  */
#define  CN   134217729.0


/* Exact addition of two single-length floating point numbers, Dekker. */
/* The macro produces a double-length number (z,zz) that satisfies     */
/* z+zz = x+y exactly.                                                 */

#define  EADD(x,y,z,zz)  \
	   z=(x)+(y);  zz=(fabs(x)>fabs(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x));


/* Exact subtraction of two single-length floating point numbers, Dekker. */
/* The macro produces a double-length number (z,zz) that satisfies        */
/* z+zz = x-y exactly.                                                    */

#define  ESUB(x,y,z,zz)  \
	   z=(x)-(y);  zz=(fabs(x)>fabs(y)) ? (((x)-(z))-(y)) : ((x)-((y)+(z)));


#ifdef __FP_FAST_FMA
# define DLA_FMS(x, y, z) __builtin_fma (x, y, -(z))
#endif

/* Exact multiplication of two single-length floating point numbers,   */
/* Veltkamp. The macro produces a double-length number (z,zz) that     */
/* satisfies z+zz = x*y exactly. p,hx,tx,hy,ty are temporary           */
/* storage variables of type double.                                   */

#ifdef DLA_FMS
# define  EMULV(x, y, z, zz)          \
  z = x * y; zz = DLA_FMS (x, y, z);
#else
# define  EMULV(x, y, z, zz)          \
    ({  __typeof__ (x) __p, hx, tx, hy, ty;          \
        __p = CN * (x);  hx = ((x) - __p) + __p;  tx = (x) - hx; \
        __p = CN * (y);  hy = ((y) - __p) + __p;  ty = (y) - hy; \
        z = (x) * (y); zz = (((hx * hy - z) + hx * ty) + tx * hy) + tx * ty; \
    })
#endif


/* Exact multiplication of two single-length floating point numbers, Dekker. */
/* The macro produces a nearly double-length number (z,zz) (see Dekker)      */
/* that satisfies z+zz = x*y exactly. p,hx,tx,hy,ty,q are temporary          */
/* storage variables of type double.                                         */

#ifdef DLA_FMS
# define  MUL12(x, y, z, zz)        \
	   EMULV(x, y, z, zz)
#else
# define  MUL12(x, y, z, zz)        \
    ({  __typeof__ (x) __p, hx, tx, hy, ty, __q; \
	   __p=CN*(x);  hx=((x)-__p)+__p;  tx=(x)-hx;  \
	   __p=CN*(y);  hy=((y)-__p)+__p;  ty=(y)-hy;  \
	   __p=hx*hy;  __q=hx*ty+tx*hy; z=__p+__q;  zz=((__p-z)+__q)+tx*ty; \
    })
#endif


/* Double-length addition, Dekker. The macro produces a double-length   */
/* number (z,zz) which satisfies approximately   z+zz = x+xx + y+yy.    */
/* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy)       */
/* are assumed to be double-length numbers. r,s are temporary           */
/* storage variables of type double.                                    */

#define  ADD2(x, xx, y, yy, z, zz, r, s)                   \
  r = (x) + (y);  s = (fabs (x) > fabs (y)) ?                \
		      (((((x) - r) + (y)) + (yy)) + (xx)) : \
		      (((((y) - r) + (x)) + (xx)) + (yy));  \
  z = r + s;  zz = (r - z) + s;


/* Double-length subtraction, Dekker. The macro produces a double-length  */
/* number (z,zz) which satisfies approximately   z+zz = x+xx - (y+yy).    */
/* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy)         */
/* are assumed to be double-length numbers. r,s are temporary             */
/* storage variables of type double.                                      */

#define  SUB2(x, xx, y, yy, z, zz, r, s)                   \
  r = (x) - (y);  s = (fabs (x) > fabs (y)) ?                \
		      (((((x) - r) - (y)) - (yy)) + (xx)) : \
		      ((((x) - ((y) + r)) + (xx)) - (yy));  \
  z = r + s;  zz = (r - z) + s;


/* Double-length multiplication, Dekker. The macro produces a double-length  */
/* number (z,zz) which satisfies approximately   z+zz = (x+xx)*(y+yy).       */
/* An error bound: abs((x+xx)*(y+yy))*1.24e-31. (x,xx), (y,yy)               */
/* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc are         */
/* temporary storage variables of type double.                               */

#define  MUL2(x, xx, y, yy, z, zz, c, cc)  \
  MUL12 (x, y, c, cc);                     \
  cc = ((x) * (yy) + (xx) * (y)) + cc;   z = c + cc;   zz = (c - z) + cc;


/* Double-length division, Dekker. The macro produces a double-length        */
/* number (z,zz) which satisfies approximately   z+zz = (x+xx)/(y+yy).       */
/* An error bound: abs((x+xx)/(y+yy))*1.50e-31. (x,xx), (y,yy)               */
/* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc,u,uu        */
/* are temporary storage variables of type double.                           */

#define  DIV2(x, xx, y, yy, z, zz, c, cc, u, uu)  \
	   c=(x)/(y);   MUL12(c,y,u,uu);          \
	   cc=(((((x)-u)-uu)+(xx))-c*(yy))/(y);   z=c+cc;   zz=(c-z)+cc;


/* Double-length addition, slower but more accurate than ADD2.               */
/* The macro produces a double-length                                        */
/* number (z,zz) which satisfies approximately   z+zz = (x+xx)+(y+yy).       */
/* An error bound: abs(x+xx + y+yy)*1.50e-31. (x,xx), (y,yy)                 */
/* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w                 */
/* are temporary storage variables of type double.                           */

#define  ADD2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w)                 \
  r = (x) + (y);                                                            \
  if (fabs (x) > fabs (y)) { rr = ((x) - r) + (y);  s = (rr + (yy)) + (xx); } \
  else               { rr = ((y) - r) + (x);  s = (rr + (xx)) + (yy); }     \
  if (rr != 0.0) {                                                          \
      z = r + s;  zz = (r - z) + s; }                                       \
  else {                                                                    \
      ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) + (yy)) : (((yy) - s) + (xx));\
      u = r + s;                                                            \
      uu = (fabs (r) > fabs (s))   ? ((r - u) + s)   : ((s - u) + r);         \
      w = uu + ss;  z = u + w;                                              \
      zz = (fabs (u) > fabs (w))   ? ((u - z) + w)   : ((w - z) + u); }


/* Double-length subtraction, slower but more accurate than SUB2.            */
/* The macro produces a double-length                                        */
/* number (z,zz) which satisfies approximately   z+zz = (x+xx)-(y+yy).       */
/* An error bound: abs(x+xx - (y+yy))*1.50e-31. (x,xx), (y,yy)               */
/* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w                 */
/* are temporary storage variables of type double.                           */

#define  SUB2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w)                   \
  r = (x) - (y);                                                              \
  if (fabs (x) > fabs (y)) { rr = ((x) - r) - (y);  s = (rr - (yy)) + (xx); }   \
  else               { rr = (x) - ((y) + r);  s = (rr + (xx)) - (yy); }       \
  if (rr != 0.0) {                                                            \
      z = r + s;  zz = (r - z) + s; }                                         \
  else {                                                                      \
      ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) - (yy)) : ((xx) - ((yy) + s)); \
      u = r + s;                                                              \
      uu = (fabs (r) > fabs (s))   ? ((r - u) + s)   : ((s - u) + r);           \
      w = uu + ss;  z = u + w;                                                \
      zz = (fabs (u) > fabs (w))   ? ((u - z) + w)   : ((w - z) + u); }