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