1*2fe8fb19SBen Gras /* e_sqrtf.c -- float version of e_sqrt.c.
2*2fe8fb19SBen Gras * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3*2fe8fb19SBen Gras */
4*2fe8fb19SBen Gras
5*2fe8fb19SBen Gras /*
6*2fe8fb19SBen Gras * ====================================================
7*2fe8fb19SBen Gras * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*2fe8fb19SBen Gras *
9*2fe8fb19SBen Gras * Developed at SunPro, a Sun Microsystems, Inc. business.
10*2fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this
11*2fe8fb19SBen Gras * software is freely granted, provided that this notice
12*2fe8fb19SBen Gras * is preserved.
13*2fe8fb19SBen Gras * ====================================================
14*2fe8fb19SBen Gras */
15*2fe8fb19SBen Gras
16*2fe8fb19SBen Gras #include <sys/cdefs.h>
17*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
18*2fe8fb19SBen Gras __RCSID("$NetBSD: e_sqrtf.c,v 1.7 2002/05/26 22:01:53 wiz Exp $");
19*2fe8fb19SBen Gras #endif
20*2fe8fb19SBen Gras
21*2fe8fb19SBen Gras #include "math.h"
22*2fe8fb19SBen Gras #include "math_private.h"
23*2fe8fb19SBen Gras
24*2fe8fb19SBen Gras static const float one = 1.0, tiny=1.0e-30;
25*2fe8fb19SBen Gras
26*2fe8fb19SBen Gras float
__ieee754_sqrtf(float x)27*2fe8fb19SBen Gras __ieee754_sqrtf(float x)
28*2fe8fb19SBen Gras {
29*2fe8fb19SBen Gras float z;
30*2fe8fb19SBen Gras int32_t sign = (int)0x80000000;
31*2fe8fb19SBen Gras int32_t ix,s,q,m,t,i;
32*2fe8fb19SBen Gras u_int32_t r;
33*2fe8fb19SBen Gras
34*2fe8fb19SBen Gras GET_FLOAT_WORD(ix,x);
35*2fe8fb19SBen Gras
36*2fe8fb19SBen Gras /* take care of Inf and NaN */
37*2fe8fb19SBen Gras if((ix&0x7f800000)==0x7f800000) {
38*2fe8fb19SBen Gras return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
39*2fe8fb19SBen Gras sqrt(-inf)=sNaN */
40*2fe8fb19SBen Gras }
41*2fe8fb19SBen Gras /* take care of zero */
42*2fe8fb19SBen Gras if(ix<=0) {
43*2fe8fb19SBen Gras if((ix&(~sign))==0) return x;/* sqrt(+-0) = +-0 */
44*2fe8fb19SBen Gras else if(ix<0)
45*2fe8fb19SBen Gras return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
46*2fe8fb19SBen Gras }
47*2fe8fb19SBen Gras /* normalize x */
48*2fe8fb19SBen Gras m = (ix>>23);
49*2fe8fb19SBen Gras if(m==0) { /* subnormal x */
50*2fe8fb19SBen Gras for(i=0;(ix&0x00800000)==0;i++) ix<<=1;
51*2fe8fb19SBen Gras m -= i-1;
52*2fe8fb19SBen Gras }
53*2fe8fb19SBen Gras m -= 127; /* unbias exponent */
54*2fe8fb19SBen Gras ix = (ix&0x007fffff)|0x00800000;
55*2fe8fb19SBen Gras if(m&1) /* odd m, double x to make it even */
56*2fe8fb19SBen Gras ix += ix;
57*2fe8fb19SBen Gras m >>= 1; /* m = [m/2] */
58*2fe8fb19SBen Gras
59*2fe8fb19SBen Gras /* generate sqrt(x) bit by bit */
60*2fe8fb19SBen Gras ix += ix;
61*2fe8fb19SBen Gras q = s = 0; /* q = sqrt(x) */
62*2fe8fb19SBen Gras r = 0x01000000; /* r = moving bit from right to left */
63*2fe8fb19SBen Gras
64*2fe8fb19SBen Gras while(r!=0) {
65*2fe8fb19SBen Gras t = s+r;
66*2fe8fb19SBen Gras if(t<=ix) {
67*2fe8fb19SBen Gras s = t+r;
68*2fe8fb19SBen Gras ix -= t;
69*2fe8fb19SBen Gras q += r;
70*2fe8fb19SBen Gras }
71*2fe8fb19SBen Gras ix += ix;
72*2fe8fb19SBen Gras r>>=1;
73*2fe8fb19SBen Gras }
74*2fe8fb19SBen Gras
75*2fe8fb19SBen Gras /* use floating add to find out rounding direction */
76*2fe8fb19SBen Gras if(ix!=0) {
77*2fe8fb19SBen Gras z = one-tiny; /* trigger inexact flag */
78*2fe8fb19SBen Gras if (z>=one) {
79*2fe8fb19SBen Gras z = one+tiny;
80*2fe8fb19SBen Gras if (z>one)
81*2fe8fb19SBen Gras q += 2;
82*2fe8fb19SBen Gras else
83*2fe8fb19SBen Gras q += (q&1);
84*2fe8fb19SBen Gras }
85*2fe8fb19SBen Gras }
86*2fe8fb19SBen Gras ix = (q>>1)+0x3f000000;
87*2fe8fb19SBen Gras ix += (m <<23);
88*2fe8fb19SBen Gras SET_FLOAT_WORD(z,ix);
89*2fe8fb19SBen Gras return z;
90*2fe8fb19SBen Gras }
91