Blame sysdeps/ieee754/dbl-64/dla.h

Packit 6c4009
/*
Packit 6c4009
 * IBM Accurate Mathematical Library
Packit 6c4009
 * Written by International Business Machines Corp.
Packit 6c4009
 * Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
 *
Packit 6c4009
 * This program is free software; you can redistribute it and/or modify
Packit 6c4009
 * it under the terms of the GNU Lesser General Public License as published by
Packit 6c4009
 * the Free Software Foundation; either version 2.1 of the License, or
Packit 6c4009
 * (at your option) any later version.
Packit 6c4009
 *
Packit 6c4009
 * This program is distributed in the hope that it will be useful,
Packit 6c4009
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
 * GNU Lesser General Public License for more details.
Packit 6c4009
 *
Packit 6c4009
 * You should have received a copy of the GNU Lesser General Public License
Packit 6c4009
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
Packit 6c4009
/***********************************************************************/
Packit 6c4009
/*MODULE_NAME: dla.h                                                   */
Packit 6c4009
/*                                                                     */
Packit 6c4009
/* This file holds C language macros for 'Double Length Floating Point */
Packit 6c4009
/* Arithmetic'. The macros are based on the paper:                     */
Packit 6c4009
/* T.J.Dekker, "A floating-point Technique for extending the           */
Packit 6c4009
/* Available Precision", Number. Math. 18, 224-242 (1971).              */
Packit 6c4009
/* A Double-Length number is defined by a pair (r,s), of IEEE double    */
Packit 6c4009
/* precision floating point numbers that satisfy,                      */
Packit 6c4009
/*                                                                     */
Packit 6c4009
/*              abs(s) <= abs(r+s)*2**(-53)/(1+2**(-53)).              */
Packit 6c4009
/*                                                                     */
Packit 6c4009
/* The computer arithmetic assumed is IEEE double precision in         */
Packit 6c4009
/* round to nearest mode. All variables in the macros must be of type  */
Packit 6c4009
/* IEEE double.                                                        */
Packit 6c4009
/***********************************************************************/
Packit 6c4009
Packit 6c4009
/* CN = 1+2**27 = '41a0000002000000' IEEE double format.  Use it to split a
Packit 6c4009
   double for better accuracy.  */
Packit 6c4009
#define  CN   134217729.0
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Exact addition of two single-length floating point numbers, Dekker. */
Packit 6c4009
/* The macro produces a double-length number (z,zz) that satisfies     */
Packit 6c4009
/* z+zz = x+y exactly.                                                 */
Packit 6c4009
Packit 6c4009
#define  EADD(x,y,z,zz)  \
Packit 6c4009
	   z=(x)+(y);  zz=(fabs(x)>fabs(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x));
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Exact subtraction of two single-length floating point numbers, Dekker. */
Packit 6c4009
/* The macro produces a double-length number (z,zz) that satisfies        */
Packit 6c4009
/* z+zz = x-y exactly.                                                    */
Packit 6c4009
Packit 6c4009
#define  ESUB(x,y,z,zz)  \
Packit 6c4009
	   z=(x)-(y);  zz=(fabs(x)>fabs(y)) ? (((x)-(z))-(y)) : ((x)-((y)+(z)));
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef __FP_FAST_FMA
Packit 6c4009
# define DLA_FMS(x, y, z) __builtin_fma (x, y, -(z))
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Exact multiplication of two single-length floating point numbers,   */
Packit 6c4009
/* Veltkamp. The macro produces a double-length number (z,zz) that     */
Packit 6c4009
/* satisfies z+zz = x*y exactly. p,hx,tx,hy,ty are temporary           */
Packit 6c4009
/* storage variables of type double.                                   */
Packit 6c4009
Packit 6c4009
#ifdef DLA_FMS
Packit 6c4009
# define  EMULV(x, y, z, zz, p, hx, tx, hy, ty)          \
Packit 6c4009
  z = x * y; zz = DLA_FMS (x, y, z);
Packit 6c4009
#else
Packit 6c4009
# define  EMULV(x, y, z, zz, p, hx, tx, hy, ty)          \
Packit 6c4009
  p = CN * (x);  hx = ((x) - p) + p;  tx = (x) - hx; \
Packit 6c4009
  p = CN * (y);  hy = ((y) - p) + p;  ty = (y) - hy; \
Packit 6c4009
  z = (x) * (y); zz = (((hx * hy - z) + hx * ty) + tx * hy) + tx * ty;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Exact multiplication of two single-length floating point numbers, Dekker. */
Packit 6c4009
/* The macro produces a nearly double-length number (z,zz) (see Dekker)      */
Packit 6c4009
/* that satisfies z+zz = x*y exactly. p,hx,tx,hy,ty,q are temporary          */
Packit 6c4009
/* storage variables of type double.                                         */
Packit 6c4009
Packit 6c4009
#ifdef DLA_FMS
Packit 6c4009
# define  MUL12(x,y,z,zz,p,hx,tx,hy,ty,q)        \
Packit 6c4009
	   EMULV(x,y,z,zz,p,hx,tx,hy,ty)
Packit 6c4009
#else
Packit 6c4009
# define  MUL12(x,y,z,zz,p,hx,tx,hy,ty,q)        \
Packit 6c4009
	   p=CN*(x);  hx=((x)-p)+p;  tx=(x)-hx; \
Packit 6c4009
	   p=CN*(y);  hy=((y)-p)+p;  ty=(y)-hy; \
Packit 6c4009
	   p=hx*hy;  q=hx*ty+tx*hy; z=p+q;  zz=((p-z)+q)+tx*ty;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Double-length addition, Dekker. The macro produces a double-length   */
Packit 6c4009
/* number (z,zz) which satisfies approximately   z+zz = x+xx + y+yy.    */
Packit 6c4009
/* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy)       */
Packit 6c4009
/* are assumed to be double-length numbers. r,s are temporary           */
Packit 6c4009
/* storage variables of type double.                                    */
Packit 6c4009
Packit 6c4009
#define  ADD2(x, xx, y, yy, z, zz, r, s)                   \
Packit 6c4009
  r = (x) + (y);  s = (fabs (x) > fabs (y)) ?                \
Packit 6c4009
		      (((((x) - r) + (y)) + (yy)) + (xx)) : \
Packit 6c4009
		      (((((y) - r) + (x)) + (xx)) + (yy));  \
Packit 6c4009
  z = r + s;  zz = (r - z) + s;
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Double-length subtraction, Dekker. The macro produces a double-length  */
Packit 6c4009
/* number (z,zz) which satisfies approximately   z+zz = x+xx - (y+yy).    */
Packit 6c4009
/* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy)         */
Packit 6c4009
/* are assumed to be double-length numbers. r,s are temporary             */
Packit 6c4009
/* storage variables of type double.                                      */
Packit 6c4009
Packit 6c4009
#define  SUB2(x, xx, y, yy, z, zz, r, s)                   \
Packit 6c4009
  r = (x) - (y);  s = (fabs (x) > fabs (y)) ?                \
Packit 6c4009
		      (((((x) - r) - (y)) - (yy)) + (xx)) : \
Packit 6c4009
		      ((((x) - ((y) + r)) + (xx)) - (yy));  \
Packit 6c4009
  z = r + s;  zz = (r - z) + s;
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Double-length multiplication, Dekker. The macro produces a double-length  */
Packit 6c4009
/* number (z,zz) which satisfies approximately   z+zz = (x+xx)*(y+yy).       */
Packit 6c4009
/* An error bound: abs((x+xx)*(y+yy))*1.24e-31. (x,xx), (y,yy)               */
Packit 6c4009
/* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc are         */
Packit 6c4009
/* temporary storage variables of type double.                               */
Packit 6c4009
Packit 6c4009
#define  MUL2(x, xx, y, yy, z, zz, p, hx, tx, hy, ty, q, c, cc)  \
Packit 6c4009
  MUL12 (x, y, c, cc, p, hx, tx, hy, ty, q)                      \
Packit 6c4009
  cc = ((x) * (yy) + (xx) * (y)) + cc;   z = c + cc;   zz = (c - z) + cc;
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Double-length division, Dekker. The macro produces a double-length        */
Packit 6c4009
/* number (z,zz) which satisfies approximately   z+zz = (x+xx)/(y+yy).       */
Packit 6c4009
/* An error bound: abs((x+xx)/(y+yy))*1.50e-31. (x,xx), (y,yy)               */
Packit 6c4009
/* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc,u,uu        */
Packit 6c4009
/* are temporary storage variables of type double.                           */
Packit 6c4009
Packit 6c4009
#define  DIV2(x,xx,y,yy,z,zz,p,hx,tx,hy,ty,q,c,cc,u,uu)  \
Packit 6c4009
	   c=(x)/(y);   MUL12(c,y,u,uu,p,hx,tx,hy,ty,q)  \
Packit 6c4009
	   cc=(((((x)-u)-uu)+(xx))-c*(yy))/(y);   z=c+cc;   zz=(c-z)+cc;
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Double-length addition, slower but more accurate than ADD2.               */
Packit 6c4009
/* The macro produces a double-length                                        */
Packit 6c4009
/* number (z,zz) which satisfies approximately   z+zz = (x+xx)+(y+yy).       */
Packit 6c4009
/* An error bound: abs(x+xx + y+yy)*1.50e-31. (x,xx), (y,yy)                 */
Packit 6c4009
/* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w                 */
Packit 6c4009
/* are temporary storage variables of type double.                           */
Packit 6c4009
Packit 6c4009
#define  ADD2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w)                 \
Packit 6c4009
  r = (x) + (y);                                                            \
Packit 6c4009
  if (fabs (x) > fabs (y)) { rr = ((x) - r) + (y);  s = (rr + (yy)) + (xx); } \
Packit 6c4009
  else               { rr = ((y) - r) + (x);  s = (rr + (xx)) + (yy); }     \
Packit 6c4009
  if (rr != 0.0) {                                                          \
Packit 6c4009
      z = r + s;  zz = (r - z) + s; }                                       \
Packit 6c4009
  else {                                                                    \
Packit 6c4009
      ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) + (yy)) : (((yy) - s) + (xx));\
Packit 6c4009
      u = r + s;                                                            \
Packit 6c4009
      uu = (fabs (r) > fabs (s))   ? ((r - u) + s)   : ((s - u) + r);         \
Packit 6c4009
      w = uu + ss;  z = u + w;                                              \
Packit 6c4009
      zz = (fabs (u) > fabs (w))   ? ((u - z) + w)   : ((w - z) + u); }
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Double-length subtraction, slower but more accurate than SUB2.            */
Packit 6c4009
/* The macro produces a double-length                                        */
Packit 6c4009
/* number (z,zz) which satisfies approximately   z+zz = (x+xx)-(y+yy).       */
Packit 6c4009
/* An error bound: abs(x+xx - (y+yy))*1.50e-31. (x,xx), (y,yy)               */
Packit 6c4009
/* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w                 */
Packit 6c4009
/* are temporary storage variables of type double.                           */
Packit 6c4009
Packit 6c4009
#define  SUB2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w)                   \
Packit 6c4009
  r = (x) - (y);                                                              \
Packit 6c4009
  if (fabs (x) > fabs (y)) { rr = ((x) - r) - (y);  s = (rr - (yy)) + (xx); }   \
Packit 6c4009
  else               { rr = (x) - ((y) + r);  s = (rr + (xx)) - (yy); }       \
Packit 6c4009
  if (rr != 0.0) {                                                            \
Packit 6c4009
      z = r + s;  zz = (r - z) + s; }                                         \
Packit 6c4009
  else {                                                                      \
Packit 6c4009
      ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) - (yy)) : ((xx) - ((yy) + s)); \
Packit 6c4009
      u = r + s;                                                              \
Packit 6c4009
      uu = (fabs (r) > fabs (s))   ? ((r - u) + s)   : ((s - u) + r);           \
Packit 6c4009
      w = uu + ss;  z = u + w;                                                \
Packit 6c4009
      zz = (fabs (u) > fabs (w))   ? ((u - z) + w)   : ((w - z) + u); }