Back to index

glibc  2.9
e_sqrtl.c
Go to the documentation of this file.
00001 /*
00002  * IBM Accurate Mathematical Library
00003  * written by International Business Machines Corp.
00004  * Copyright (C) 2001, 2004, 2006 Free Software Foundation
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2.1 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 /*********************************************************************/
00021 /* MODULE_NAME: uroot.c                                              */
00022 /*                                                                   */
00023 /* FUNCTION:    usqrt                                                */
00024 /*                                                                   */
00025 /* FILES NEEDED: dla.h endian.h mydefs.h uroot.h                     */
00026 /*               uroot.tbl                                           */
00027 /*                                                                   */
00028 /* An ultimate sqrt routine. Given an IEEE double machine number x   */
00029 /* it computes the correctly rounded (to nearest) value of square    */
00030 /* root of x.                                                        */
00031 /* Assumption: Machine arithmetic operations are performed in        */
00032 /* round to nearest mode of IEEE 754 standard.                       */
00033 /*                                                                   */
00034 /*********************************************************************/
00035 
00036 #include <math_private.h>
00037 
00038 typedef unsigned int int4;
00039 typedef union {int4 i[4]; long double x; double d[2]; } mynumber;
00040 
00041 static const  mynumber
00042   t512 = {{0x5ff00000, 0x00000000, 0x00000000, 0x00000000 }},  /* 2^512  */
00043   tm256 = {{0x2ff00000, 0x00000000, 0x00000000, 0x00000000 }};  /* 2^-256 */
00044 static const double
00045 two54 = 1.80143985094819840000e+16, /* 0x4350000000000000 */
00046 twom54 = 5.55111512312578270212e-17; /* 0x3C90000000000000 */
00047 
00048 /*********************************************************************/
00049 /* An ultimate sqrt routine. Given an IEEE double machine number x   */
00050 /* it computes the correctly rounded (to nearest) value of square    */
00051 /* root of x.                                                        */
00052 /*********************************************************************/
00053 long double __ieee754_sqrtl(long double x) 
00054 {
00055   static const long double big = 134217728.0, big1 = 134217729.0;
00056   long double t,s,i;
00057   mynumber a,c;
00058   int4 k, l, m;
00059   int n;
00060   double d;
00061 
00062   a.x=x;
00063   k=a.i[0] & 0x7fffffff;
00064   /*----------------- 2^-1022  <= | x |< 2^1024  -----------------*/
00065   if (k>0x000fffff && k<0x7ff00000) {
00066     if (x < 0) return (big1-big1)/(big-big);
00067     l = (k&0x001fffff)|0x3fe00000;
00068     if (((a.i[2] & 0x7fffffff) | a.i[3]) != 0) {
00069       n = (int) ((l - k) * 2) >> 21;
00070       m = (a.i[2] >> 20) & 0x7ff;
00071       if (m == 0) {
00072        a.d[1] *= two54;
00073        m = ((a.i[2] >> 20) & 0x7ff) - 54;
00074       }
00075       m += n;
00076       if (m > 0)
00077        a.i[2] = (a.i[2] & 0x800fffff) | (m << 20);
00078       else if (m <= -54) {
00079        a.i[2] &= 0x80000000;
00080        a.i[3] = 0;
00081       } else {
00082        m += 54;
00083        a.i[2] = (a.i[2] & 0x800fffff) | (m << 20);
00084        a.d[1] *= twom54;
00085       }
00086     }
00087     a.i[0] = l;
00088     s = a.x;
00089     d = __ieee754_sqrt (a.d[0]);
00090     c.i[0] = 0x20000000+((k&0x7fe00000)>>1);
00091     c.i[1] = 0;
00092     c.i[2] = 0;
00093     c.i[3] = 0;
00094     i = d;
00095     t = 0.5L * (i + s / i);
00096     i = 0.5L * (t + s / t);
00097     return c.x * i;
00098   }
00099   else {
00100     if (k>=0x7ff00000) {
00101       if (a.i[0] == 0xfff00000 && a.i[1] == 0)
00102        return (big1-big1)/(big-big); /* sqrt (-Inf) = NaN.  */
00103       return x; /* sqrt (NaN) = NaN, sqrt (+Inf) = +Inf.  */
00104     }
00105     if (x == 0) return x;
00106     if (x < 0) return (big1-big1)/(big-big);
00107     return tm256.x*__ieee754_sqrtl(x*t512.x);
00108   }
00109 }