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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [math/] [sqrt.go] - Blame information for rev 867

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

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package math
6
 
7
// Sqrt returns the square root of x.
8
//
9
// Special cases are:
10
//      Sqrt(+Inf) = +Inf
11
//      Sqrt(±0) = ±0
12
//      Sqrt(x < 0) = NaN
13
//      Sqrt(NaN) = NaN
14
 
15
//extern sqrt
16
func libc_sqrt(float64) float64
17
 
18
func Sqrt(x float64) float64 {
19
        return libc_sqrt(x)
20
}
21
 
22
// The original C code and the long comment below are
23
// from FreeBSD's /usr/src/lib/msun/src/e_sqrt.c and
24
// came with this notice.  The go code is a simplified
25
// version of the original C.
26
//
27
// ====================================================
28
// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
29
//
30
// Developed at SunPro, a Sun Microsystems, Inc. business.
31
// Permission to use, copy, modify, and distribute this
32
// software is freely granted, provided that this notice
33
// is preserved.
34
// ====================================================
35
//
36
// __ieee754_sqrt(x)
37
// Return correctly rounded sqrt.
38
//           -----------------------------------------
39
//           | Use the hardware sqrt if you have one |
40
//           -----------------------------------------
41
// Method:
42
//   Bit by bit method using integer arithmetic. (Slow, but portable)
43
//   1. Normalization
44
//      Scale x to y in [1,4) with even powers of 2:
45
//      find an integer k such that  1 <= (y=x*2**(2k)) < 4, then
46
//              sqrt(x) = 2**k * sqrt(y)
47
//   2. Bit by bit computation
48
//      Let q  = sqrt(y) truncated to i bit after binary point (q = 1),
49
//           i                                                   0
50
//                                     i+1         2
51
//          s  = 2*q , and      y  =  2   * ( y - q  ).          (1)
52
//           i      i            i                 i
53
//
54
//      To compute q    from q , one checks whether
55
//                  i+1       i
56
//
57
//                            -(i+1) 2
58
//                      (q + 2      )  <= y.                     (2)
59
//                        i
60
//                                                            -(i+1)
61
//      If (2) is false, then q   = q ; otherwise q   = q  + 2      .
62
//                             i+1   i             i+1   i
63
//
64
//      With some algebraic manipulation, it is not difficult to see
65
//      that (2) is equivalent to
66
//                             -(i+1)
67
//                      s  +  2       <= y                       (3)
68
//                       i                i
69
//
70
//      The advantage of (3) is that s  and y  can be computed by
71
//                                    i      i
72
//      the following recurrence formula:
73
//          if (3) is false
74
//
75
//          s     =  s  ,       y    = y   ;                     (4)
76
//           i+1      i          i+1    i
77
//
78
//      otherwise,
79
//                         -i                      -(i+1)
80
//          s     =  s  + 2  ,  y    = y  -  s  - 2              (5)
81
//           i+1      i          i+1    i     i
82
//
83
//      One may easily use induction to prove (4) and (5).
84
//      Note. Since the left hand side of (3) contain only i+2 bits,
85
//            it does not necessary to do a full (53-bit) comparison
86
//            in (3).
87
//   3. Final rounding
88
//      After generating the 53 bits result, we compute one more bit.
89
//      Together with the remainder, we can decide whether the
90
//      result is exact, bigger than 1/2ulp, or less than 1/2ulp
91
//      (it will never equal to 1/2ulp).
92
//      The rounding mode can be detected by checking whether
93
//      huge + tiny is equal to huge, and whether huge - tiny is
94
//      equal to huge for some floating point number "huge" and "tiny".
95
//
96
//
97
// Notes:  Rounding mode detection omitted.  The constants "mask", "shift",
98
// and "bias" are found in src/pkg/math/bits.go
99
 
100
// Sqrt returns the square root of x.
101
//
102
// Special cases are:
103
//      Sqrt(+Inf) = +Inf
104
//      Sqrt(±0) = ±0
105
//      Sqrt(x < 0) = NaN
106
//      Sqrt(NaN) = NaN
107
func sqrt(x float64) float64 {
108
        // special cases
109
        switch {
110
        case x == 0 || IsNaN(x) || IsInf(x, 1):
111
                return x
112
        case x < 0:
113
                return NaN()
114
        }
115
        ix := Float64bits(x)
116
        // normalize x
117
        exp := int((ix >> shift) & mask)
118
        if exp == 0 { // subnormal x
119
                for ix&1<
120
                        ix <<= 1
121
                        exp--
122
                }
123
                exp++
124
        }
125
        exp -= bias // unbias exponent
126
        ix &^= mask << shift
127
        ix |= 1 << shift
128
        if exp&1 == 1 { // odd exp, double x to make it even
129
                ix <<= 1
130
        }
131
        exp >>= 1 // exp = exp/2, exponent of square root
132
        // generate sqrt(x) bit by bit
133
        ix <<= 1
134
        var q, s uint64               // q = sqrt(x)
135
        r := uint64(1 << (shift + 1)) // r = moving bit from MSB to LSB
136
        for r != 0 {
137
                t := s + r
138
                if t <= ix {
139
                        s = t + r
140
                        ix -= t
141
                        q += r
142
                }
143
                ix <<= 1
144
                r >>= 1
145
        }
146
        // final rounding
147
        if ix != 0 { // remainder, result not exact
148
                q += q & 1 // round according to extra bit
149
        }
150
        ix = q>>1 + uint64(exp-1+bias)<
151
        return Float64frombits(ix)
152
}
153
 
154
func sqrtC(f float64, r *float64) {
155
        *r = sqrt(f)
156
}

powered by: WebSVN 2.1.0

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