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