e_sqrtl.c revision 1.5
1/*- 2 * Copyright (c) 2007 Steven G. Kargl 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28#if 0 29__FBSDID("$FreeBSD: head/lib/msun/src/e_sqrtl.c 176720 2008-03-02 01:47:58Z das $"); 30#endif 31__RCSID("$NetBSD: e_sqrtl.c,v 1.5 2016/08/26 08:31:17 christos Exp $"); 32 33#include <machine/ieee.h> 34#include <float.h> 35 36#include "math.h" 37#include "math_private.h" 38 39#ifdef __HAVE_LONG_DOUBLE 40 41#define __TEST_FENV 42#include <fenv.h> 43 44#ifdef LDBL_IMPLICIT_NBIT 45#define LDBL_NBIT 0 46#endif 47 48#ifdef __HAVE_FENV 49 50/* Return (x + ulp) for normal positive x. Assumes no overflow. */ 51static inline long double 52inc(long double x) 53{ 54 union ieee_ext_u ux = { .extu_ld = x, }; 55 56 if (++ux.extu_fracl == 0) { 57 if (++ux.extu_frach == 0) { 58 ux.extu_exp++; 59 ux.extu_frach |= LDBL_NBIT; 60 } 61 } 62 return (ux.extu_ld); 63} 64 65/* Return (x - ulp) for normal positive x. Assumes no underflow. */ 66static inline long double 67dec(long double x) 68{ 69 union ieee_ext_u ux = { .extu_ld = x, }; 70 71 if (ux.extu_fracl-- == 0) { 72 if (ux.extu_frach-- == LDBL_NBIT) { 73 ux.extu_exp--; 74 ux.extu_frach |= LDBL_NBIT; 75 } 76 } 77 return (ux.extu_ld); 78} 79 80/* 81 * This is slow, but simple and portable. You should use hardware sqrt 82 * if possible. 83 */ 84 85long double 86__ieee754_sqrtl(long double x) 87{ 88 union ieee_ext_u ux = { .extu_ld = x, }; 89 int k, r; 90 long double lo, xn; 91 fenv_t env; 92 93 /* If x = NaN, then sqrt(x) = NaN. */ 94 /* If x = Inf, then sqrt(x) = Inf. */ 95 /* If x = -Inf, then sqrt(x) = NaN. */ 96 if (ux.extu_exp == LDBL_MAX_EXP * 2 - 1) 97 return (x * x + x); 98 99 /* If x = +-0, then sqrt(x) = +-0. */ 100 if ((ux.extu_frach | ux.extu_fracl | ux.extu_exp) == 0) 101 return (x); 102 103 /* If x < 0, then raise invalid and return NaN */ 104 if (ux.extu_sign) 105 return ((x - x) / (x - x)); 106 107 feholdexcept(&env); 108 109 if (ux.extu_exp == 0) { 110 /* Adjust subnormal numbers. */ 111 ux.extu_ld *= 0x1.0p514; 112 k = -514; 113 } else { 114 k = 0; 115 } 116 /* 117 * ux.extu_ld is a normal number, so break it into ux.extu_ld = e*2^n where 118 * ux.extu_ld = (2*e)*2^2k for odd n and ux.extu_ld = (4*e)*2^2k for even n. 119 */ 120 if ((ux.extu_exp - EXT_EXP_BIAS) & 1) { /* n is even. */ 121 k += ux.extu_exp - EXT_EXP_BIAS - 1; /* 2k = n - 2. */ 122 ux.extu_exp = EXT_EXP_BIAS + 1; /* ux.extu_ld in [2,4). */ 123 } else { 124 k += ux.extu_exp - EXT_EXP_BIAS; /* 2k = n - 1. */ 125 ux.extu_exp = EXT_EXP_BIAS; /* ux.extu_ld in [1,2). */ 126 } 127 128 /* 129 * Newton's iteration. 130 * Split ux.extu_ld into a high and low part to achieve additional precision. 131 */ 132 xn = sqrt(ux.extu_ld); /* 53-bit estimate of sqrtl(x). */ 133#if LDBL_MANT_DIG > 100 134 xn = (xn + (ux.extu_ld / xn)) * 0.5; /* 106-bit estimate. */ 135#endif 136 lo = ux.extu_ld; 137 ux.extu_fracl = 0; /* Zero out lower bits. */ 138 lo = (lo - ux.extu_ld) / xn; /* Low bits divided by xn. */ 139 xn = xn + (ux.extu_ld / xn); /* High portion of estimate. */ 140 ux.extu_ld = xn + lo; /* Combine everything. */ 141 ux.extu_exp += (k >> 1) - 1; 142 143 feclearexcept(FE_INEXACT); 144 r = fegetround(); 145 fesetround(FE_TOWARDZERO); /* Set to round-toward-zero. */ 146 xn = x / ux.extu_ld; /* Chopped quotient (inexact?). */ 147 148 if (!fetestexcept(FE_INEXACT)) { /* Quotient is exact. */ 149 if (xn == ux.extu_ld) { 150 fesetenv(&env); 151 return (ux.extu_ld); 152 } 153 /* Round correctly for inputs like x = y**2 - ulp. */ 154 xn = dec(xn); /* xn = xn - ulp. */ 155 } 156 157 if (r == FE_TONEAREST) { 158 xn = inc(xn); /* xn = xn + ulp. */ 159 } else if (r == FE_UPWARD) { 160 ux.extu_ld = inc(ux.extu_ld); /* ux.extu_ld = ux.extu_ld + ulp. */ 161 xn = inc(xn); /* xn = xn + ulp. */ 162 } 163 ux.extu_ld = ux.extu_ld + xn; /* Chopped sum. */ 164 feupdateenv(&env); /* Restore env and raise inexact */ 165 ux.extu_exp--; 166 return (ux.extu_ld); 167} 168 169#else /* !__HAVE_FENV */ 170 171/* 172 * No fenv support: 173 * poor man's version: just use double 174 */ 175long double 176__ieee754_sqrtl(long double x) 177{ 178 return __ieee754_sqrt((double)x); 179} 180 181#endif /* __HAVE_FENV */ 182 183#endif /* __HAVE_LONG_DOUBLE */ 184