Debian Codegraph @

modules/fdlibm/src/e_sqrtf.cpp

raw
1 /* e_sqrtf.c -- float version of e_sqrt.c.
2 * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
3 */
4
5 /*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 *
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 * ====================================================
14 */
15
16 //#ifndef lint
17 //static char rcsid[] = "$FreeBSD$";
18 //#endif
19
20 #include "math_private.h"
21
22 #ifdef USE_BUILTIN_SQRTF
23 float
24 __ieee754_sqrtf(float x)
25 {
26 return (__builtin_sqrtf(x));
27 }
28 #else
29 static const float one = 1.0, tiny=1.0e-30;
30
31 float
32 __ieee754_sqrtf(float x)
33 {
34 float z;
35 int32_t sign = (int)0x80000000;
36 int32_t ix,s,q,m,t,i;
37 u_int32_t r;
38
39 GET_FLOAT_WORD(ix,x);
40
41 /* take care of Inf and NaN */
42 if((ix&0x7f800000)==0x7f800000) {
43 return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
44 sqrt(-inf)=sNaN */
45 }
46 /* take care of zero */
47 if(ix<=0) {
48 if((ix&(~sign))==0) return x;/* sqrt(+-0) = +-0 */
49 else if(ix<0)
50 return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
51 }
52 /* normalize x */
53 m = (ix>>23);
54 if(m==0) { /* subnormal x */
55 for(i=0;(ix&0x00800000)==0;i++) ix<<=1;
56 m -= i-1;
57 }
58 m -= 127; /* unbias exponent */
59 ix = (ix&0x007fffff)|0x00800000;
60 if(m&1) /* odd m, double x to make it even */
61 ix += ix;
62 m >>= 1; /* m = [m/2] */
63
64 /* generate sqrt(x) bit by bit */
65 ix += ix;
66 q = s = 0; /* q = sqrt(x) */
67 r = 0x01000000; /* r = moving bit from right to left */
68
69 while(r!=0) {
70 t = s+r;
71 if(t<=ix) {
72 s = t+r;
73 ix -= t;
74 q += r;
75 }
76 ix += ix;
77 r>>=1;
78 }
79
80 /* use floating add to find out rounding direction */
81 if(ix!=0) {
82 z = one-tiny; /* trigger inexact flag */
83 if (z>=one) {
84 z = one+tiny;
85 if (z>one)
86 q += 2;
87 else
88 q += (q&1);
89 }
90 }
91 ix = (q>>1)+0x3f000000;
92 ix += (m <<23);
93 SET_FLOAT_WORD(z,ix);
94 return z;
95 }
96 #endif