2.9
[glibc/nacl-glibc.git] / sysdeps / ieee754 / ldbl-128ibm / e_sqrtl.c
blob1f533cae42257fe527019bd330ffef9f749b5752
1 /*
2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001, 2004, 2006 Free Software Foundation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /*********************************************************************/
21 /* MODULE_NAME: uroot.c */
22 /* */
23 /* FUNCTION: usqrt */
24 /* */
25 /* FILES NEEDED: dla.h endian.h mydefs.h uroot.h */
26 /* uroot.tbl */
27 /* */
28 /* An ultimate sqrt routine. Given an IEEE double machine number x */
29 /* it computes the correctly rounded (to nearest) value of square */
30 /* root of x. */
31 /* Assumption: Machine arithmetic operations are performed in */
32 /* round to nearest mode of IEEE 754 standard. */
33 /* */
34 /*********************************************************************/
36 #include <math_private.h>
38 typedef unsigned int int4;
39 typedef union {int4 i[4]; long double x; double d[2]; } mynumber;
41 static const mynumber
42 t512 = {{0x5ff00000, 0x00000000, 0x00000000, 0x00000000 }}, /* 2^512 */
43 tm256 = {{0x2ff00000, 0x00000000, 0x00000000, 0x00000000 }}; /* 2^-256 */
44 static const double
45 two54 = 1.80143985094819840000e+16, /* 0x4350000000000000 */
46 twom54 = 5.55111512312578270212e-17; /* 0x3C90000000000000 */
48 /*********************************************************************/
49 /* An ultimate sqrt routine. Given an IEEE double machine number x */
50 /* it computes the correctly rounded (to nearest) value of square */
51 /* root of x. */
52 /*********************************************************************/
53 long double __ieee754_sqrtl(long double x)
55 static const long double big = 134217728.0, big1 = 134217729.0;
56 long double t,s,i;
57 mynumber a,c;
58 int4 k, l, m;
59 int n;
60 double d;
62 a.x=x;
63 k=a.i[0] & 0x7fffffff;
64 /*----------------- 2^-1022 <= | x |< 2^1024 -----------------*/
65 if (k>0x000fffff && k<0x7ff00000) {
66 if (x < 0) return (big1-big1)/(big-big);
67 l = (k&0x001fffff)|0x3fe00000;
68 if (((a.i[2] & 0x7fffffff) | a.i[3]) != 0) {
69 n = (int) ((l - k) * 2) >> 21;
70 m = (a.i[2] >> 20) & 0x7ff;
71 if (m == 0) {
72 a.d[1] *= two54;
73 m = ((a.i[2] >> 20) & 0x7ff) - 54;
75 m += n;
76 if (m > 0)
77 a.i[2] = (a.i[2] & 0x800fffff) | (m << 20);
78 else if (m <= -54) {
79 a.i[2] &= 0x80000000;
80 a.i[3] = 0;
81 } else {
82 m += 54;
83 a.i[2] = (a.i[2] & 0x800fffff) | (m << 20);
84 a.d[1] *= twom54;
87 a.i[0] = l;
88 s = a.x;
89 d = __ieee754_sqrt (a.d[0]);
90 c.i[0] = 0x20000000+((k&0x7fe00000)>>1);
91 c.i[1] = 0;
92 c.i[2] = 0;
93 c.i[3] = 0;
94 i = d;
95 t = 0.5L * (i + s / i);
96 i = 0.5L * (t + s / t);
97 return c.x * i;
99 else {
100 if (k>=0x7ff00000) {
101 if (a.i[0] == 0xfff00000 && a.i[1] == 0)
102 return (big1-big1)/(big-big); /* sqrt (-Inf) = NaN. */
103 return x; /* sqrt (NaN) = NaN, sqrt (+Inf) = +Inf. */
105 if (x == 0) return x;
106 if (x < 0) return (big1-big1)/(big-big);
107 return tm256.x*__ieee754_sqrtl(x*t512.x);