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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 39 lampret
/* 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;
34
        __int32_t ix,s,q,m,t,i;
35
 
36
        GET_FLOAT_WORD(ix,x);
37
 
38
    /* take care of Inf and NaN */
39
        if((ix&0x7f800000L)==0x7f800000L) {
40
            return x*x+x;               /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
41
                                           sqrt(-inf)=sNaN */
42
        }
43
    /* take care of zero */
44
        if(ix<=0) {
45
            if((ix&(~sign))==0) return x;/* sqrt(+-0) = +-0 */
46
            else if(ix<0)
47
                return (x-x)/(x-x);             /* sqrt(-ve) = sNaN */
48
        }
49
    /* normalize x */
50
        m = (ix>>23);
51
        if(m==0) {                               /* subnormal x */
52
            for(i=0;(ix&0x00800000L)==0;i++) ix<<=1;
53
            m -= i-1;
54
        }
55
        m -= 127;       /* unbias exponent */
56
        ix = (ix&0x007fffffL)|0x00800000L;
57
        if(m&1) /* odd m, double x to make it even */
58
            ix += ix;
59
        m >>= 1;        /* m = [m/2] */
60
 
61
    /* generate sqrt(x) bit by bit */
62
        ix += ix;
63
        q = s = 0;               /* q = sqrt(x) */
64
        r = 0x01000000L;                /* r = moving bit from right to left */
65
 
66
        while(r!=0) {
67
            t = s+r;
68
            if(t<=ix) {
69
                s    = t+r;
70
                ix  -= t;
71
                q   += r;
72
            }
73
            ix += ix;
74
            r>>=1;
75
        }
76
 
77
    /* use floating add to find out rounding direction */
78
        if(ix!=0) {
79
            z = one-tiny; /* trigger inexact flag */
80
            if (z>=one) {
81
                z = one+tiny;
82
                if (z>one)
83
                    q += 2;
84
                else
85
                    q += (q&1);
86
            }
87
        }
88
        ix = (q>>1)+0x3f000000L;
89
        ix += (m <<23);
90
        SET_FLOAT_WORD(z,ix);
91
        return z;
92
}

powered by: WebSVN 2.1.0

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