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.3 2013/11/22 13:37:24 martin 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 #ifdef HAVE_FENV_H 42 #include <fenv.h> 43 #endif 44 45 #ifdef LDBL_IMPLICIT_NBIT 46 #define LDBL_NBIT 0 47 #endif 48 49 /* Return (x + ulp) for normal positive x. Assumes no overflow. */ 50 static inline long double 51 inc(long double x) 52 { 53 union ieee_ext_u ux = { .extu_ld = x, }; 54 55 if (++ux.extu_fracl == 0) { 56 if (++ux.extu_frach == 0) { 57 ux.extu_exp++; 58 ux.extu_frach |= LDBL_NBIT; 59 } 60 } 61 return (ux.extu_ld); 62 } 63 64 /* Return (x - ulp) for normal positive x. Assumes no underflow. */ 65 static inline long double 66 dec(long double x) 67 { 68 union ieee_ext_u ux = { .extu_ld = x, }; 69 70 if (ux.extu_fracl-- == 0) { 71 if (ux.extu_frach-- == LDBL_NBIT) { 72 ux.extu_exp--; 73 ux.extu_frach |= LDBL_NBIT; 74 } 75 } 76 return (ux.extu_ld); 77 } 78 79 /* 80 * This is slow, but simple and portable. You should use hardware sqrt 81 * if possible. 82 */ 83 84 long double 85 __ieee754_sqrtl(long double x) 86 { 87 union ieee_ext_u ux = { .extu_ld = x, }; 88 int k, r; 89 long double lo, xn; 90 #ifdef HAVE_FENV_H 91 fenv_t env; 92 #endif 93 94 /* If x = NaN, then sqrt(x) = NaN. */ 95 /* If x = Inf, then sqrt(x) = Inf. */ 96 /* If x = -Inf, then sqrt(x) = NaN. */ 97 if (ux.extu_exp == LDBL_MAX_EXP * 2 - 1) 98 return (x * x + x); 99 100 /* If x = +-0, then sqrt(x) = +-0. */ 101 if ((ux.extu_frach | ux.extu_fracl | ux.extu_exp) == 0) 102 return (x); 103 104 /* If x < 0, then raise invalid and return NaN */ 105 if (ux.extu_sign) 106 return ((x - x) / (x - x)); 107 108 #ifdef HAVE_FENV_H 109 feholdexcept(&env); 110 #endif 111 112 if (ux.extu_exp == 0) { 113 /* Adjust subnormal numbers. */ 114 ux.extu_ld *= 0x1.0p514; 115 k = -514; 116 } else { 117 k = 0; 118 } 119 /* 120 * ux.extu_ld is a normal number, so break it into ux.extu_ld = e*2^n where 121 * ux.extu_ld = (2*e)*2^2k for odd n and ux.extu_ld = (4*e)*2^2k for even n. 122 */ 123 if ((ux.extu_exp - EXT_EXP_BIAS) & 1) { /* n is even. */ 124 k += ux.extu_exp - EXT_EXP_BIAS - 1; /* 2k = n - 2. */ 125 ux.extu_exp = EXT_EXP_BIAS + 1; /* ux.extu_ld in [2,4). */ 126 } else { 127 k += ux.extu_exp - EXT_EXP_BIAS; /* 2k = n - 1. */ 128 ux.extu_exp = EXT_EXP_BIAS; /* ux.extu_ld in [1,2). */ 129 } 130 131 /* 132 * Newton's iteration. 133 * Split ux.extu_ld into a high and low part to achieve additional precision. 134 */ 135 xn = sqrt(ux.extu_ld); /* 53-bit estimate of sqrtl(x). */ 136 #if LDBL_MANT_DIG > 100 137 xn = (xn + (ux.extu_ld / xn)) * 0.5; /* 106-bit estimate. */ 138 #endif 139 lo = ux.extu_ld; 140 ux.extu_fracl = 0; /* Zero out lower bits. */ 141 lo = (lo - ux.extu_ld) / xn; /* Low bits divided by xn. */ 142 xn = xn + (ux.extu_ld / xn); /* High portion of estimate. */ 143 ux.extu_ld = xn + lo; /* Combine everything. */ 144 ux.extu_exp += (k >> 1) - 1; 145 146 feclearexcept(FE_INEXACT); 147 r = fegetround(); 148 fesetround(FE_TOWARDZERO); /* Set to round-toward-zero. */ 149 xn = x / ux.extu_ld; /* Chopped quotient (inexact?). */ 150 151 if (!fetestexcept(FE_INEXACT)) { /* Quotient is exact. */ 152 if (xn == ux.extu_ld) { 153 #ifdef HAVE_FENV_H 154 fesetenv(&env); 155 #endif 156 return (ux.extu_ld); 157 } 158 /* Round correctly for inputs like x = y**2 - ulp. */ 159 xn = dec(xn); /* xn = xn - ulp. */ 160 } 161 162 if (r == FE_TONEAREST) { 163 xn = inc(xn); /* xn = xn + ulp. */ 164 } else if (r == FE_UPWARD) { 165 ux.extu_ld = inc(ux.extu_ld); /* ux.extu_ld = ux.extu_ld + ulp. */ 166 xn = inc(xn); /* xn = xn + ulp. */ 167 } 168 ux.extu_ld = ux.extu_ld + xn; /* Chopped sum. */ 169 #ifdef HAVE_FENV_H 170 feupdateenv(&env); /* Restore env and raise inexact */ 171 #endif 172 ux.extu_exp--; 173 return (ux.extu_ld); 174 } 175 176 #endif 177