darwin-ldouble.c revision 1.1.1.1
1/* 128-bit long double support routines for Darwin. 2 Copyright (C) 1993, 2003, 2004, 2005, 2006 3 Free Software Foundation, Inc. 4 5This file is part of GCC. 6 7GCC is free software; you can redistribute it and/or modify it under 8the terms of the GNU General Public License as published by the Free 9Software Foundation; either version 2, or (at your option) any later 10version. 11 12In addition to the permissions in the GNU General Public License, the 13Free Software Foundation gives you unlimited permission to link the 14compiled version of this file into combinations with other programs, 15and to distribute those combinations without any restriction coming 16from the use of this file. (The General Public License restrictions 17do apply in other respects; for example, they cover modification of 18the file, and distribution when not linked into a combine 19executable.) 20 21GCC is distributed in the hope that it will be useful, but WITHOUT ANY 22WARRANTY; without even the implied warranty of MERCHANTABILITY or 23FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 24for more details. 25 26You should have received a copy of the GNU General Public License 27along with GCC; see the file COPYING. If not, write to the Free 28Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 2902110-1301, USA. */ 30 31/* Implementations of floating-point long double basic arithmetic 32 functions called by the IBM C compiler when generating code for 33 PowerPC platforms. In particular, the following functions are 34 implemented: __gcc_qadd, __gcc_qsub, __gcc_qmul, and __gcc_qdiv. 35 Double-double algorithms are based on the paper "Doubled-Precision 36 IEEE Standard 754 Floating-Point Arithmetic" by W. Kahan, February 26, 37 1987. An alternative published reference is "Software for 38 Doubled-Precision Floating-Point Computations", by Seppo Linnainmaa, 39 ACM TOMS vol 7 no 3, September 1981, pages 272-283. */ 40 41/* Each long double is made up of two IEEE doubles. The value of the 42 long double is the sum of the values of the two parts. The most 43 significant part is required to be the value of the long double 44 rounded to the nearest double, as specified by IEEE. For Inf 45 values, the least significant part is required to be one of +0.0 or 46 -0.0. No other requirements are made; so, for example, 1.0 may be 47 represented as (1.0, +0.0) or (1.0, -0.0), and the low part of a 48 NaN is don't-care. 49 50 This code currently assumes big-endian. */ 51 52#if (!defined (__NO_FPRS__) && !defined (__LITTLE_ENDIAN__) \ 53 && (defined (__MACH__) || defined (__powerpc__) || defined (_AIX))) 54 55#define fabs(x) __builtin_fabs(x) 56#define isless(x, y) __builtin_isless (x, y) 57#define inf() __builtin_inf() 58 59#define unlikely(x) __builtin_expect ((x), 0) 60 61#define nonfinite(a) unlikely (! isless (fabs (a), inf ())) 62 63/* All these routines actually take two long doubles as parameters, 64 but GCC currently generates poor code when a union is used to turn 65 a long double into a pair of doubles. */ 66 67extern long double __gcc_qadd (double, double, double, double); 68extern long double __gcc_qsub (double, double, double, double); 69extern long double __gcc_qmul (double, double, double, double); 70extern long double __gcc_qdiv (double, double, double, double); 71 72#if defined __ELF__ && defined SHARED \ 73 && (defined __powerpc64__ || !(defined __linux__ || defined __gnu_hurd__)) 74/* Provide definitions of the old symbol names to satisfy apps and 75 shared libs built against an older libgcc. To access the _xlq 76 symbols an explicit version reference is needed, so these won't 77 satisfy an unadorned reference like _xlqadd. If dot symbols are 78 not needed, the assembler will remove the aliases from the symbol 79 table. */ 80__asm__ (".symver __gcc_qadd,_xlqadd@GCC_3.4\n\t" 81 ".symver __gcc_qsub,_xlqsub@GCC_3.4\n\t" 82 ".symver __gcc_qmul,_xlqmul@GCC_3.4\n\t" 83 ".symver __gcc_qdiv,_xlqdiv@GCC_3.4\n\t" 84 ".symver .__gcc_qadd,._xlqadd@GCC_3.4\n\t" 85 ".symver .__gcc_qsub,._xlqsub@GCC_3.4\n\t" 86 ".symver .__gcc_qmul,._xlqmul@GCC_3.4\n\t" 87 ".symver .__gcc_qdiv,._xlqdiv@GCC_3.4"); 88#endif 89 90typedef union 91{ 92 long double ldval; 93 double dval[2]; 94} longDblUnion; 95 96/* Add two 'long double' values and return the result. */ 97long double 98__gcc_qadd (double a, double aa, double c, double cc) 99{ 100 longDblUnion x; 101 double z, q, zz, xh; 102 103 z = a + c; 104 105 if (nonfinite (z)) 106 { 107 z = cc + aa + c + a; 108 if (nonfinite (z)) 109 return z; 110 x.dval[0] = z; /* Will always be DBL_MAX. */ 111 zz = aa + cc; 112 if (fabs(a) > fabs(c)) 113 x.dval[1] = a - z + c + zz; 114 else 115 x.dval[1] = c - z + a + zz; 116 } 117 else 118 { 119 q = a - z; 120 zz = q + c + (a - (q + z)) + aa + cc; 121 122 /* Keep -0 result. */ 123 if (zz == 0.0) 124 return z; 125 126 xh = z + zz; 127 if (nonfinite (xh)) 128 return xh; 129 130 x.dval[0] = xh; 131 x.dval[1] = z - xh + zz; 132 } 133 return x.ldval; 134} 135 136long double 137__gcc_qsub (double a, double b, double c, double d) 138{ 139 return __gcc_qadd (a, b, -c, -d); 140} 141 142long double 143__gcc_qmul (double a, double b, double c, double d) 144{ 145 longDblUnion z; 146 double t, tau, u, v, w; 147 148 t = a * c; /* Highest order double term. */ 149 150 if (unlikely (t == 0) /* Preserve -0. */ 151 || nonfinite (t)) 152 return t; 153 154 /* Sum terms of two highest orders. */ 155 156 /* Use fused multiply-add to get low part of a * c. */ 157 asm ("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t)); 158 v = a*d; 159 w = b*c; 160 tau += v + w; /* Add in other second-order terms. */ 161 u = t + tau; 162 163 /* Construct long double result. */ 164 if (nonfinite (u)) 165 return u; 166 z.dval[0] = u; 167 z.dval[1] = (t - u) + tau; 168 return z.ldval; 169} 170 171long double 172__gcc_qdiv (double a, double b, double c, double d) 173{ 174 longDblUnion z; 175 double s, sigma, t, tau, u, v, w; 176 177 t = a / c; /* highest order double term */ 178 179 if (unlikely (t == 0) /* Preserve -0. */ 180 || nonfinite (t)) 181 return t; 182 183 /* Finite nonzero result requires corrections to the highest order term. */ 184 185 s = c * t; /* (s,sigma) = c*t exactly. */ 186 w = -(-b + d * t); /* Written to get fnmsub for speed, but not 187 numerically necessary. */ 188 189 /* Use fused multiply-add to get low part of c * t. */ 190 asm ("fmsub %0,%1,%2,%3" : "=f"(sigma) : "f"(c), "f"(t), "f"(s)); 191 v = a - s; 192 193 tau = ((v-sigma)+w)/c; /* Correction to t. */ 194 u = t + tau; 195 196 /* Construct long double result. */ 197 if (nonfinite (u)) 198 return u; 199 z.dval[0] = u; 200 z.dval[1] = (t - u) + tau; 201 return z.ldval; 202} 203 204#endif 205