(root)/
glibc-2.38/
sysdeps/
ieee754/
dbl-64/
dla.h
       1  /*
       2   * IBM Accurate Mathematical Library
       3   * Copyright (C) 2001-2023 Free Software Foundation, Inc.
       4   *
       5   * This program is free software; you can redistribute it and/or modify
       6   * it under the terms of the GNU Lesser General Public License as published by
       7   * the Free Software Foundation; either version 2.1 of the License, or
       8   * (at your option) any later version.
       9   *
      10   * This program is distributed in the hope that it will be useful,
      11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13   * GNU Lesser General Public License for more details.
      14   *
      15   * You should have received a copy of the GNU Lesser General Public License
      16   * along with this program; if not, see <https://www.gnu.org/licenses/>.
      17   */
      18  
      19  #include <math.h>
      20  
      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  /***********************************************************************/
      37  
      38  /* CN = 1+2**27 = '41a0000002000000' IEEE double format.  Use it to split a
      39     double for better accuracy.  */
      40  #define  CN   134217729.0
      41  
      42  
      43  /* Exact addition of two single-length floating point numbers, Dekker. */
      44  /* The macro produces a double-length number (z,zz) that satisfies     */
      45  /* z+zz = x+y exactly.                                                 */
      46  
      47  #define  EADD(x,y,z,zz)  \
      48  	   z=(x)+(y);  zz=(fabs(x)>fabs(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x));
      49  
      50  
      51  /* Exact subtraction of two single-length floating point numbers, Dekker. */
      52  /* The macro produces a double-length number (z,zz) that satisfies        */
      53  /* z+zz = x-y exactly.                                                    */
      54  
      55  #define  ESUB(x,y,z,zz)  \
      56  	   z=(x)-(y);  zz=(fabs(x)>fabs(y)) ? (((x)-(z))-(y)) : ((x)-((y)+(z)));
      57  
      58  
      59  #ifdef __FP_FAST_FMA
      60  # define DLA_FMS(x, y, z) __builtin_fma (x, y, -(z))
      61  #endif
      62  
      63  /* Exact multiplication of two single-length floating point numbers,   */
      64  /* Veltkamp. The macro produces a double-length number (z,zz) that     */
      65  /* satisfies z+zz = x*y exactly. p,hx,tx,hy,ty are temporary           */
      66  /* storage variables of type double.                                   */
      67  
      68  #ifdef DLA_FMS
      69  # define  EMULV(x, y, z, zz)          \
      70    z = x * y; zz = DLA_FMS (x, y, z);
      71  #else
      72  # define  EMULV(x, y, z, zz)          \
      73      ({  __typeof__ (x) __p, hx, tx, hy, ty;          \
      74          __p = CN * (x);  hx = ((x) - __p) + __p;  tx = (x) - hx; \
      75          __p = CN * (y);  hy = ((y) - __p) + __p;  ty = (y) - hy; \
      76          z = (x) * (y); zz = (((hx * hy - z) + hx * ty) + tx * hy) + tx * ty; \
      77      })
      78  #endif
      79  
      80  
      81  /* Exact multiplication of two single-length floating point numbers, Dekker. */
      82  /* The macro produces a nearly double-length number (z,zz) (see Dekker)      */
      83  /* that satisfies z+zz = x*y exactly. p,hx,tx,hy,ty,q are temporary          */
      84  /* storage variables of type double.                                         */
      85  
      86  #ifdef DLA_FMS
      87  # define  MUL12(x, y, z, zz)        \
      88  	   EMULV(x, y, z, zz)
      89  #else
      90  # define  MUL12(x, y, z, zz)        \
      91      ({  __typeof__ (x) __p, hx, tx, hy, ty, __q; \
      92  	   __p=CN*(x);  hx=((x)-__p)+__p;  tx=(x)-hx;  \
      93  	   __p=CN*(y);  hy=((y)-__p)+__p;  ty=(y)-hy;  \
      94  	   __p=hx*hy;  __q=hx*ty+tx*hy; z=__p+__q;  zz=((__p-z)+__q)+tx*ty; \
      95      })
      96  #endif
      97  
      98  
      99  /* Double-length addition, Dekker. The macro produces a double-length   */
     100  /* number (z,zz) which satisfies approximately   z+zz = x+xx + y+yy.    */
     101  /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy)       */
     102  /* are assumed to be double-length numbers. r,s are temporary           */
     103  /* storage variables of type double.                                    */
     104  
     105  #define  ADD2(x, xx, y, yy, z, zz, r, s)                   \
     106    r = (x) + (y);  s = (fabs (x) > fabs (y)) ?                \
     107  		      (((((x) - r) + (y)) + (yy)) + (xx)) : \
     108  		      (((((y) - r) + (x)) + (xx)) + (yy));  \
     109    z = r + s;  zz = (r - z) + s;
     110  
     111  
     112  /* Double-length subtraction, Dekker. The macro produces a double-length  */
     113  /* number (z,zz) which satisfies approximately   z+zz = x+xx - (y+yy).    */
     114  /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy)         */
     115  /* are assumed to be double-length numbers. r,s are temporary             */
     116  /* storage variables of type double.                                      */
     117  
     118  #define  SUB2(x, xx, y, yy, z, zz, r, s)                   \
     119    r = (x) - (y);  s = (fabs (x) > fabs (y)) ?                \
     120  		      (((((x) - r) - (y)) - (yy)) + (xx)) : \
     121  		      ((((x) - ((y) + r)) + (xx)) - (yy));  \
     122    z = r + s;  zz = (r - z) + s;
     123  
     124  
     125  /* Double-length multiplication, Dekker. The macro produces a double-length  */
     126  /* number (z,zz) which satisfies approximately   z+zz = (x+xx)*(y+yy).       */
     127  /* An error bound: abs((x+xx)*(y+yy))*1.24e-31. (x,xx), (y,yy)               */
     128  /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc are         */
     129  /* temporary storage variables of type double.                               */
     130  
     131  #define  MUL2(x, xx, y, yy, z, zz, c, cc)  \
     132    MUL12 (x, y, c, cc);                     \
     133    cc = ((x) * (yy) + (xx) * (y)) + cc;   z = c + cc;   zz = (c - z) + cc;
     134  
     135  
     136  /* Double-length division, Dekker. The macro produces a double-length        */
     137  /* number (z,zz) which satisfies approximately   z+zz = (x+xx)/(y+yy).       */
     138  /* An error bound: abs((x+xx)/(y+yy))*1.50e-31. (x,xx), (y,yy)               */
     139  /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc,u,uu        */
     140  /* are temporary storage variables of type double.                           */
     141  
     142  #define  DIV2(x, xx, y, yy, z, zz, c, cc, u, uu)  \
     143  	   c=(x)/(y);   MUL12(c,y,u,uu);          \
     144  	   cc=(((((x)-u)-uu)+(xx))-c*(yy))/(y);   z=c+cc;   zz=(c-z)+cc;
     145  
     146  
     147  /* Double-length addition, slower but more accurate than ADD2.               */
     148  /* The macro produces a double-length                                        */
     149  /* number (z,zz) which satisfies approximately   z+zz = (x+xx)+(y+yy).       */
     150  /* An error bound: abs(x+xx + y+yy)*1.50e-31. (x,xx), (y,yy)                 */
     151  /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w                 */
     152  /* are temporary storage variables of type double.                           */
     153  
     154  #define  ADD2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w)                 \
     155    r = (x) + (y);                                                            \
     156    if (fabs (x) > fabs (y)) { rr = ((x) - r) + (y);  s = (rr + (yy)) + (xx); } \
     157    else               { rr = ((y) - r) + (x);  s = (rr + (xx)) + (yy); }     \
     158    if (rr != 0.0) {                                                          \
     159        z = r + s;  zz = (r - z) + s; }                                       \
     160    else {                                                                    \
     161        ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) + (yy)) : (((yy) - s) + (xx));\
     162        u = r + s;                                                            \
     163        uu = (fabs (r) > fabs (s))   ? ((r - u) + s)   : ((s - u) + r);         \
     164        w = uu + ss;  z = u + w;                                              \
     165        zz = (fabs (u) > fabs (w))   ? ((u - z) + w)   : ((w - z) + u); }
     166  
     167  
     168  /* Double-length subtraction, slower but more accurate than SUB2.            */
     169  /* The macro produces a double-length                                        */
     170  /* number (z,zz) which satisfies approximately   z+zz = (x+xx)-(y+yy).       */
     171  /* An error bound: abs(x+xx - (y+yy))*1.50e-31. (x,xx), (y,yy)               */
     172  /* are assumed to be double-length numbers. r,rr,s,ss,u,uu,w                 */
     173  /* are temporary storage variables of type double.                           */
     174  
     175  #define  SUB2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w)                   \
     176    r = (x) - (y);                                                              \
     177    if (fabs (x) > fabs (y)) { rr = ((x) - r) - (y);  s = (rr - (yy)) + (xx); }   \
     178    else               { rr = (x) - ((y) + r);  s = (rr + (xx)) - (yy); }       \
     179    if (rr != 0.0) {                                                            \
     180        z = r + s;  zz = (r - z) + s; }                                         \
     181    else {                                                                      \
     182        ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) - (yy)) : ((xx) - ((yy) + s)); \
     183        u = r + s;                                                              \
     184        uu = (fabs (r) > fabs (s))   ? ((r - u) + s)   : ((s - u) + r);           \
     185        w = uu + ss;  z = u + w;                                                \
     186        zz = (fabs (u) > fabs (w))   ? ((u - z) + w)   : ((w - z) + u); }