Improve DLA_FMA for FMA4
[glibc.git] / sysdeps / ieee754 / dbl-64 / dla.h
blob0ca87620cb6177cb0b4b58dde4fd66e33420a6ae
1 /*
2 * IBM Accurate Mathematical Library
3 * Written by International Business Machines Corp.
4 * Copyright (C) 2001, 2011 Free Software Foundation, Inc.
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.
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.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 /***********************************************************************/
22 /*MODULE_NAME: dla.h */
23 /* */
24 /* This file holds C language macros for 'Double Length Floating Point */
25 /* Arithmetic'. The macros are based on the paper: */
26 /* T.J.Dekker, "A floating-point Technique for extending the */
27 /* Available Precision", Number. Math. 18, 224-242 (1971). */
28 /* A Double-Length number is defined by a pair (r,s), of IEEE double */
29 /* precision floating point numbers that satisfy, */
30 /* */
31 /* abs(s) <= abs(r+s)*2**(-53)/(1+2**(-53)). */
32 /* */
33 /* The computer arithmetic assumed is IEEE double precision in */
34 /* round to nearest mode. All variables in the macros must be of type */
35 /* IEEE double. */
36 /***********************************************************************/
38 /* We can use fma instructions if available. */
39 #if defined __x86_64__ || (defined __i386__ && defined __SSE2_MATH__)
40 # ifdef __FMA4__
41 # define DLA_FMA(x,y,z) \
42 ({ double __zz; \
43 asm ("vfmsubsd %3, %2, %1, %0" \
44 : "=x" (__zz) : "x" (x), "xm" (y), "x" (z)); \
45 __zz; })
46 # endif
47 #endif
50 /* CN = 1+2**27 = '41a0000002000000' IEEE double format */
51 #define CN 134217729.0
54 /* Exact addition of two single-length floating point numbers, Dekker. */
55 /* The macro produces a double-length number (z,zz) that satisfies */
56 /* z+zz = x+y exactly. */
58 #define EADD(x,y,z,zz) \
59 z=(x)+(y); zz=(ABS(x)>ABS(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x));
62 /* Exact subtraction of two single-length floating point numbers, Dekker. */
63 /* The macro produces a double-length number (z,zz) that satisfies */
64 /* z+zz = x-y exactly. */
66 #define ESUB(x,y,z,zz) \
67 z=(x)-(y); zz=(ABS(x)>ABS(y)) ? (((x)-(z))-(y)) : ((x)-((y)+(z)));
70 /* Exact multiplication of two single-length floating point numbers, */
71 /* Veltkamp. The macro produces a double-length number (z,zz) that */
72 /* satisfies z+zz = x*y exactly. p,hx,tx,hy,ty are temporary */
73 /* storage variables of type double. */
75 #ifdef DLA_FMA
76 # define EMULV(x,y,z,zz,p,hx,tx,hy,ty) \
77 z=x*y; zz=DLA_FMA(x,y,z);
78 #else
79 # define EMULV(x,y,z,zz,p,hx,tx,hy,ty) \
80 p=CN*(x); hx=((x)-p)+p; tx=(x)-hx; \
81 p=CN*(y); hy=((y)-p)+p; ty=(y)-hy; \
82 z=(x)*(y); zz=(((hx*hy-z)+hx*ty)+tx*hy)+tx*ty;
83 #endif
86 /* Exact multiplication of two single-length floating point numbers, Dekker. */
87 /* The macro produces a nearly double-length number (z,zz) (see Dekker) */
88 /* that satisfies z+zz = x*y exactly. p,hx,tx,hy,ty,q are temporary */
89 /* storage variables of type double. */
91 #ifdef DLA_FMA
92 # define MUL12(x,y,z,zz,p,hx,tx,hy,ty,q) \
93 EMULV(x,y,z,zz,p,hx,tx,hy,ty)
94 #else
95 # define MUL12(x,y,z,zz,p,hx,tx,hy,ty,q) \
96 p=CN*(x); hx=((x)-p)+p; tx=(x)-hx; \
97 p=CN*(y); hy=((y)-p)+p; ty=(y)-hy; \
98 p=hx*hy; q=hx*ty+tx*hy; z=p+q; zz=((p-z)+q)+tx*ty;
99 #endif
102 /* Double-length addition, Dekker. The macro produces a double-length */
103 /* number (z,zz) which satisfies approximately z+zz = x+xx + y+yy. */
104 /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */
105 /* are assumed to be double-length numbers. r,s are temporary */
106 /* storage variables of type double. */
108 #define ADD2(x,xx,y,yy,z,zz,r,s) \
109 r=(x)+(y); s=(ABS(x)>ABS(y)) ? \
110 (((((x)-r)+(y))+(yy))+(xx)) : \
111 (((((y)-r)+(x))+(xx))+(yy)); \
112 z=r+s; zz=(r-z)+s;
115 /* Double-length subtraction, Dekker. The macro produces a double-length */
116 /* number (z,zz) which satisfies approximately z+zz = x+xx - (y+yy). */
117 /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */
118 /* are assumed to be double-length numbers. r,s are temporary */
119 /* storage variables of type double. */
121 #define SUB2(x,xx,y,yy,z,zz,r,s) \
122 r=(x)-(y); s=(ABS(x)>ABS(y)) ? \
123 (((((x)-r)-(y))-(yy))+(xx)) : \
124 ((((x)-((y)+r))+(xx))-(yy)); \
125 z=r+s; zz=(r-z)+s;
128 /* Double-length multiplication, Dekker. The macro produces a double-length */
129 /* number (z,zz) which satisfies approximately z+zz = (x+xx)*(y+yy). */
130 /* An error bound: abs((x+xx)*(y+yy))*1.24e-31. (x,xx), (y,yy) */
131 /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc are */
132 /* temporary storage variables of type double. */
134 #define MUL2(x,xx,y,yy,z,zz,p,hx,tx,hy,ty,q,c,cc) \
135 MUL12(x,y,c,cc,p,hx,tx,hy,ty,q) \
136 cc=((x)*(yy)+(xx)*(y))+cc; z=c+cc; zz=(c-z)+cc;
139 /* Double-length division, Dekker. 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. p,hx,tx,hy,ty,q,c,cc,u,uu */
143 /* are temporary storage variables of type double. */
145 #define DIV2(x,xx,y,yy,z,zz,p,hx,tx,hy,ty,q,c,cc,u,uu) \
146 c=(x)/(y); MUL12(c,y,u,uu,p,hx,tx,hy,ty,q) \
147 cc=(((((x)-u)-uu)+(xx))-c*(yy))/(y); z=c+cc; zz=(c-z)+cc;
150 /* Double-length addition, slower but more accurate than ADD2. */
151 /* The macro produces a double-length */
152 /* number (z,zz) which satisfies approximately z+zz = (x+xx)+(y+yy). */
153 /* An error bound: abs(x+xx + y+yy)*1.50e-31. (x,xx), (y,yy) */
154 /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w */
155 /* are temporary storage variables of type double. */
157 #define ADD2A(x,xx,y,yy,z,zz,r,rr,s,ss,u,uu,w) \
158 r=(x)+(y); \
159 if (ABS(x)>ABS(y)) { rr=((x)-r)+(y); s=(rr+(yy))+(xx); } \
160 else { rr=((y)-r)+(x); s=(rr+(xx))+(yy); } \
161 if (rr!=0.0) { \
162 z=r+s; zz=(r-z)+s; } \
163 else { \
164 ss=(ABS(xx)>ABS(yy)) ? (((xx)-s)+(yy)) : (((yy)-s)+(xx)); \
165 u=r+s; \
166 uu=(ABS(r)>ABS(s)) ? ((r-u)+s) : ((s-u)+r) ; \
167 w=uu+ss; z=u+w; \
168 zz=(ABS(u)>ABS(w)) ? ((u-z)+w) : ((w-z)+u) ; }
171 /* Double-length subtraction, slower but more accurate than SUB2. */
172 /* The macro produces a double-length */
173 /* number (z,zz) which satisfies approximately z+zz = (x+xx)-(y+yy). */
174 /* An error bound: abs(x+xx - (y+yy))*1.50e-31. (x,xx), (y,yy) */
175 /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w */
176 /* are temporary storage variables of type double. */
178 #define SUB2A(x,xx,y,yy,z,zz,r,rr,s,ss,u,uu,w) \
179 r=(x)-(y); \
180 if (ABS(x)>ABS(y)) { rr=((x)-r)-(y); s=(rr-(yy))+(xx); } \
181 else { rr=(x)-((y)+r); s=(rr+(xx))-(yy); } \
182 if (rr!=0.0) { \
183 z=r+s; zz=(r-z)+s; } \
184 else { \
185 ss=(ABS(xx)>ABS(yy)) ? (((xx)-s)-(yy)) : ((xx)-((yy)+s)); \
186 u=r+s; \
187 uu=(ABS(r)>ABS(s)) ? ((r-u)+s) : ((s-u)+r) ; \
188 w=uu+ss; z=u+w; \
189 zz=(ABS(u)>ABS(w)) ? ((u-z)+w) : ((w-z)+u) ; }