1 // 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.
7 // Sqrt returns the square root of x.
14 func libc_sqrt(float64) float64 __asm__("sqrt")
15 func Sqrt(x float64) float64 {
19 // The original C code and the long comment below are
20 // from FreeBSD's /usr/src/lib/msun/src/e_sqrt.c and
21 // came with this notice. The go code is a simplified
22 // version of the original C.
24 // ====================================================
25 // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
27 // Developed at SunPro, a Sun Microsystems, Inc. business.
28 // Permission to use, copy, modify, and distribute this
29 // software is freely granted, provided that this notice
31 // ====================================================
34 // Return correctly rounded sqrt.
35 // -----------------------------------------
36 // | Use the hardware sqrt if you have one |
37 // -----------------------------------------
39 // Bit by bit method using integer arithmetic. (Slow, but portable)
41 // Scale x to y in [1,4) with even powers of 2:
42 // find an integer k such that 1 <= (y=x*2**(2k)) < 4, then
43 // sqrt(x) = 2**k * sqrt(y)
44 // 2. Bit by bit computation
45 // Let q = sqrt(y) truncated to i bit after binary point (q = 1),
48 // s = 2*q , and y = 2 * ( y - q ). (1)
51 // To compute q from q , one checks whether
58 // If (2) is false, then q = q ; otherwise q = q + 2 .
61 // With some algebraic manipulation, it is not difficult to see
62 // that (2) is equivalent to
67 // The advantage of (3) is that s and y can be computed by
69 // the following recurrence formula:
72 // s = s , y = y ; (4)
77 // s = s + 2 , y = y - s - 2 (5)
80 // One may easily use induction to prove (4) and (5).
81 // Note. Since the left hand side of (3) contain only i+2 bits,
82 // it does not necessary to do a full (53-bit) comparison
85 // After generating the 53 bits result, we compute one more bit.
86 // Together with the remainder, we can decide whether the
87 // result is exact, bigger than 1/2ulp, or less than 1/2ulp
88 // (it will never equal to 1/2ulp).
89 // The rounding mode can be detected by checking whether
90 // huge + tiny is equal to huge, and whether huge - tiny is
91 // equal to huge for some floating point number "huge" and "tiny".
94 // Notes: Rounding mode detection omitted. The constants "mask", "shift",
95 // and "bias" are found in src/pkg/math/bits.go
97 // Sqrt returns the square root of x.
104 func sqrt(x float64) float64 {
106 // TODO(rsc): Remove manual inlining of IsNaN, IsInf
107 // when compiler does it for us
109 case x == 0 || x != x || x > MaxFloat64: // x == 0 || IsNaN(x) || IsInf(x, 1):
116 exp := int((ix >> shift) & mask)
117 if exp == 0 { // subnormal x
118 for ix&1<<shift == 0 {
124 exp -= bias // unbias exponent
127 if exp&1 == 1 { // odd exp, double x to make it even
130 exp >>= 1 // exp = exp/2, exponent of square root
131 // generate sqrt(x) bit by bit
133 var q, s uint64 // q = sqrt(x)
134 r := uint64(1 << (shift + 1)) // r = moving bit from MSB to LSB
146 if ix != 0 { // remainder, result not exact
147 q += q & 1 // round according to extra bit
149 ix = q>>1 + uint64(exp-1+bias)<<shift // significand + biased exponent
150 return Float64frombits(ix)
153 func sqrtC(f float64, r *float64) {