1 /* origin: FreeBSD /usr/src/lib/msun/src/e_sqrt.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunSoft, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
10 * ====================================================
13 * Return correctly rounded sqrt.
14 * ------------------------------------------
15 * | Use the hardware sqrt if you have one |
16 * ------------------------------------------
18 * Bit by bit method using integer arithmetic. (Slow, but portable)
20 * Scale x to y in [1,4) with even powers of 2:
21 * find an integer k such that 1 <= (y=x*2^(2k)) < 4, then
22 * sqrt(x) = 2^k * sqrt(y)
23 * 2. Bit by bit computation
24 * Let q = sqrt(y) truncated to i bit after binary point (q = 1),
27 * s = 2*q , and y = 2 * ( y - q ). (1)
30 * To compute q from q , one checks whether
37 * If (2) is false, then q = q ; otherwise q = q + 2 .
40 * With some algebraic manipulation, it is not difficult to see
41 * that (2) is equivalent to
46 * The advantage of (3) is that s and y can be computed by
48 * the following recurrence formula:
56 * s = s + 2 , y = y - s - 2 (5)
59 * One may easily use induction to prove (4) and (5).
60 * Note. Since the left hand side of (3) contain only i+2 bits,
61 * it does not necessary to do a full (53-bit) comparison
64 * After generating the 53 bits result, we compute one more bit.
65 * Together with the remainder, we can decide whether the
66 * result is exact, bigger than 1/2ulp, or less than 1/2ulp
67 * (it will never equal to 1/2ulp).
68 * The rounding mode can be detected by checking whether
69 * huge + tiny is equal to huge, and whether huge - tiny is
70 * equal to huge for some floating point number "huge" and "tiny".
73 * sqrt(+-0) = +-0 ... exact
75 * sqrt(-ve) = NaN ... with invalid signal
76 * sqrt(NaN) = NaN ... with invalid signal for signaling NaN
81 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
82 pub fn sqrt(x
: f64) -> f64 {
83 // On wasm32 we know that LLVM's intrinsic will compile to an optimized
84 // `f64.sqrt` native instruction, so we can leverage this for both code size
86 llvm_intrinsically_optimized
! {
87 #[cfg(target_arch = "wasm32")] {
91 unsafe { ::core::intrinsics::sqrtf64(x) }
95 #[cfg(target_feature = "sse2")]
97 // Note: This path is unlikely since LLVM will usually have already
98 // optimized sqrt calls into hardware instructions if sse2 is available,
99 // but if someone does end up here they'll apprected the speed increase.
100 #[cfg(target_arch = "x86")]
101 use core
::arch
::x86
::*;
102 #[cfg(target_arch = "x86_64")]
103 use core
::arch
::x86_64
::*;
105 let m
= _mm_set_sd(x
);
106 let m_sqrt
= _mm_sqrt_pd(m
);
107 _mm_cvtsd_f64(m_sqrt
)
110 #[cfg(not(target_feature = "sse2"))]
112 use core
::num
::Wrapping
;
114 const TINY
: f64 = 1.0e-300;
117 let sign
: Wrapping
<u32> = Wrapping(0x80000000);
124 let mut r
: Wrapping
<u32>;
125 let mut t1
: Wrapping
<u32>;
126 let mut s1
: Wrapping
<u32>;
127 let mut ix1
: Wrapping
<u32>;
128 let mut q1
: Wrapping
<u32>;
130 ix0
= (x
.to_bits() >> 32) as i32;
131 ix1
= Wrapping(x
.to_bits() as u32);
133 /* take care of Inf and NaN */
134 if (ix0
& 0x7ff00000) == 0x7ff00000 {
135 return x
* x
+ x
; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf, sqrt(-inf)=sNaN */
137 /* take care of zero */
139 if ((ix0
& !(sign
.0 as i32)) | ix1
.0
as i32) == 0 {
140 return x
; /* sqrt(+-0) = +-0 */
143 return (x
- x
) / (x
- x
); /* sqrt(-ve) = sNaN */
152 ix0
|= (ix1
>> 11).0 as i32;
156 while (ix0
& 0x00100000) == 0 {
161 ix0
|= (ix1
>> (32 - i
) as usize).0 as i32;
162 ix1
= ix1
<< i
as usize;
164 m
-= 1023; /* unbias exponent */
165 ix0
= (ix0
& 0x000fffff) | 0x00100000;
167 /* odd m, double x to make it even */
168 ix0
+= ix0
+ ((ix1
& sign
) >> 31).0 as i32;
171 m
>>= 1; /* m = [m/2] */
173 /* generate sqrt(x) bit by bit */
174 ix0
+= ix0
+ ((ix1
& sign
) >> 31).0 as i32;
176 q
= 0; /* [q,q1] = sqrt(x) */
180 r
= Wrapping(0x00200000); /* r = moving bit from right to left */
182 while r
!= Wrapping(0) {
189 ix0
+= ix0
+ ((ix1
& sign
) >> 31).0 as i32;
195 while r
!= Wrapping(0) {
198 if t
< ix0
|| (t
== ix0
&& t1
<= ix1
) {
200 if (t1
& sign
) == sign
&& (s1
& sign
) == Wrapping(0) {
210 ix0
+= ix0
+ ((ix1
& sign
) >> 31).0 as i32;
215 /* use floating add to find out rounding direction */
216 if (ix0
as u32 | ix1
.0
) != 0 {
217 z
= 1.0 - TINY
; /* raise inexact flag */
220 if q1
.0
== 0xffffffff {
224 if q1
.0
== 0xfffffffe {
229 q1
+= q1
& Wrapping(1);
233 ix0
= (q
>> 1) + 0x3fe00000;
239 f64::from_bits((ix0
as u64) << 32 | ix1
.0
as u64)
250 assert_eq
!(sqrt(100.0), 10.0);
251 assert_eq
!(sqrt(4.0), 2.0);
254 /// The spec: https://en.cppreference.com/w/cpp/numeric/math/sqrt
257 // Not Asserted: FE_INVALID exception is raised if argument is negative.
258 assert
!(sqrt(-1.0).is_nan());
259 assert
!(sqrt(NAN
).is_nan());
260 for f
in [0.0, -0.0, INFINITY
].iter().copied() {
261 assert_eq
!(sqrt(f
), f
);