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