OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libm/] [math/] [ef_sqrt.c] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1010 ivang
/* ef_sqrtf.c -- float version of e_sqrt.c.
2
 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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
#include "fdlibm.h"
17
 
18
#ifdef __STDC__
19
static  const float     one     = 1.0, tiny=1.0e-30;
20
#else
21
static  float   one     = 1.0, tiny=1.0e-30;
22
#endif
23
 
24
#ifdef __STDC__
25
        float __ieee754_sqrtf(float x)
26
#else
27
        float __ieee754_sqrtf(x)
28
        float x;
29
#endif
30
{
31
        float z;
32
        __int32_t       sign = (__int32_t)0x80000000;
33
        __uint32_t r,hx;
34
        __int32_t ix,s,q,m,t,i;
35
 
36
        GET_FLOAT_WORD(ix,x);
37
        hx = ix&0x7fffffff;
38
 
39
    /* take care of Inf and NaN */
40
        if(!FLT_UWORD_IS_FINITE(hx))
41
            return x*x+x;               /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
42
                                           sqrt(-inf)=sNaN */
43
    /* take care of zero and -ves */
44
        if(FLT_UWORD_IS_ZERO(hx)) return x;/* sqrt(+-0) = +-0 */
45
        if(ix<0) return (x-x)/(x-x);             /* sqrt(-ve) = sNaN */
46
 
47
    /* normalize x */
48
        m = (ix>>23);
49
        if(FLT_UWORD_IS_SUBNORMAL(hx)) {                /* subnormal x */
50
            for(i=0;(ix&0x00800000L)==0;i++) ix<<=1;
51
            m -= i-1;
52
        }
53
        m -= 127;       /* unbias exponent */
54
        ix = (ix&0x007fffffL)|0x00800000L;
55
        if(m&1) /* odd m, double x to make it even */
56
            ix += ix;
57
        m >>= 1;        /* m = [m/2] */
58
 
59
    /* generate sqrt(x) bit by bit */
60
        ix += ix;
61
        q = s = 0;               /* q = sqrt(x) */
62
        r = 0x01000000L;                /* r = moving bit from right to left */
63
 
64
        while(r!=0) {
65
            t = s+r;
66
            if(t<=ix) {
67
                s    = t+r;
68
                ix  -= t;
69
                q   += r;
70
            }
71
            ix += ix;
72
            r>>=1;
73
        }
74
 
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);
84
            }
85
        }
86
        ix = (q>>1)+0x3f000000L;
87
        ix += (m <<23);
88
        SET_FLOAT_WORD(z,ix);
89
        return z;
90
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.