sqrt.c revision 25c28e83beb90e7c80452a7c818c5e6f73a07dc8
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "libm.h"
#ifdef __INLINE
extern double __inline_sqrt(double);
double
sqrt(double x) {
double z = __inline_sqrt(x);
if (isnan(x))
return (z);
}
#else /* defined(__INLINE) */
/*
* Warning: This correctly rounded sqrt is extremely slow because it computes
* the sqrt bit by bit using integer arithmetic.
*/
double
sqrt(double x)
{
double z;
return (_SVID_libm_err(x, x, 26));
return (x + x);
}
return (x);
/* extract exponent and significand */
m = ilogb(x);
z = scalbn(x, -m);
n = m >> 1;
if (n + n != m) {
ix1 <<= 1;
m -= 1;
}
/* generate sqrt(x) bit by bit */
ix1 <<= 1;
r = 0x00200000;
for (j = 1; j <= 22; j++) {
t = s0 + r;
if (t <= ix0) {
s0 = t + r;
ix0 -= t;
q += r;
}
ix1 <<= 1;
r >>= 1;
}
r = 0x80000000;
for (j = 1; j <= 32; j++) {
t = s0;
(s1 & 0x80000000) == 0)
s0 += 1;
ix0 -= t;
ix0 -= 1;
q1 += r;
}
ix1 <<= 1;
r >>= 1;
}
/* round */
goto done;
if (z < big)
goto done;
if (q1 == 0xffffffff) {
q1 = 0;
q += 1;
goto done;
}
if (z > big) {
if (q1 == 0xfffffffe)
q += 1;
q1 += 2;
goto done;
}
done:
if ((q & 1) == 1)
return (scalbn(z, n));
}
#endif /* defined(__INLINE) */