#! @file roots.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the roots besides the square root: the cube root, and the square #! root of a sum of squares, a point's distance from the origin, worked without #! overflowing or underflowing. #! #! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al., #! under the MIT license. Its `cbrt` is from FreeBSD's s_cbrt.c, of Sun's #! fdlibm, by way of musl, which says: #! #! Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. #! #! Developed at SunPro, a Sun Microsystems, Inc. business. #! Permission to use, copy, modify, and distribute this #! software is freely granted, provided that this notice #! is preserved. #! #! Optimized by Bruce D. Evans. #! #! For `f64`, `cbrt` is within 0.67 ulp of the true value, and `hypot` within #! 1 ulp. An `f32` is worked in `f64` and rounded once more. import core::intrinsics; module std::math; #! The cube root of a real, the real whose cube it is. #! @tparam T The type of real. #! @param x The real. #! @returns Its cube root, of its sign: `±0` for `±0`, `±infinity` for #! `±infinity`, and NaN for NaN. pub def cbrt(x: T): T where T: real { return cbrt_double(x as f64) as T; } #! The square root of the sum of two reals' squares, which is the length of the #! hypotenuse of a right triangle whose legs they are, or a point's distance #! from the origin. It does not overflow or underflow where the squares would. #! @tparam T The type of real. #! @param x One real. #! @param y The other. #! @returns The root: infinity where either is infinite, even with NaN, NaN #! where either is NaN otherwise, and infinity past what `T` holds. pub def hypot(x: T, y: T): T where T: real { return hypot_double(x as f64, y as f64) as T; } # The data of `cbrt`, from musl's cbrt.c. # Added to a third of the high word, these give a cube root to about 5 bits: # (1023 - 1023/3 - 0.03306235651) * 2^20, and that less 54/3 * 2^20, for a # subnormal scaled by 2^54. const cbrt_b1: u32 = 715094163; const cbrt_b2: u32 = 696219795; # A polynomial within 2^-23.5 of 1 / cbrt(r), for r within 1/10 of 1. const cbrt_p0: f64 = 1.87595182427177; const cbrt_p1: f64 = -1.8849797954337717; const cbrt_p2: f64 = 1.6214297201053545; const cbrt_p3: f64 = -0.758397934778766; const cbrt_p4: f64 = 0.14599619288661245; # The cube root, from musl's cbrt.c. def cbrt_double(x: f64): f64 { var hx := high_word(x) & 0x7FFFFFFF; # Infinities and NaN are their own. return x + x if hx >= 0x7FF00000; # t is the cube root to 5 bits: a third of the bits less the exponent's bias, # which the B's add back, of the sign of x. var bits := bits_of(x); if hx < 0x00100000 { # Zero, its own root, or a subnormal, scaled by 2^54 to be normal. bits = bits_of(x * 1.8014398509481984e+16); hx = ((bits >> 32) as u32) & 0x7FFFFFFF; return x if hx == 0; hx = hx / 3 + cbrt_b2; } else { hx = hx / 3 + cbrt_b1; } bits &= 1u << 63; bits |= (hx as u64) << 32; var t := real_of_bits(bits); # To 23 bits: cbrt(x) is t cbrt(x / t^3), and that about P(t^3 / x). var r := (t * t) * (t / x); t = t * ((cbrt_p0 + r * (cbrt_p1 + r * cbrt_p2)) + ((r * r) * r) * (cbrt_p3 + r * cbrt_p4)); # Rounded away from zero to 23 bits, so its square is exact. t = real_of_bits((bits_of(t) + 0x80000000) & 0xFFFFFFFFC0000000); # A step of Newton's method, to 53 bits. val s := t * t; r = x / s; val w := t + t; r = (r - t) / (w + r); t = t + t * r; return t; } # A double's square as a high part and a low part, whose sum is exact. struct hypot_square { var hi: f64; var lo: f64; } # 2^27 + 1, which splits a double into halves of 26 bits. const hypot_split: f64 = 134217729.0; # The square of a double, from musl's hypot.c's `sq`: x is split into xh + xl, # so the square's error is worked exactly. def hypot_squared(x: f64): hypot_square { val xc := x * hypot_split; val xh := x - xc + xc; val xl := x - xh; val hi := x * x; val lo := xh * xh - hi + 2.0 * xh * xl + xl * xl; return hypot_square { hi = hi, lo = lo }; } # sqrt(x^2 + y^2), from musl's hypot.c. def hypot_double(x: f64, y: f64): f64 { # |x| >= |y|, as the bits of each. var ux := bits_of(x) & 0x7FFFFFFFFFFFFFFF; var uy := bits_of(y) & 0x7FFFFFFFFFFFFFFF; if ux < uy { val ut: u64 = ux; ux = uy; uy = ut; } val ex := (ux >> 52) as i32; val ey := (uy >> 52) as i32; var a := real_of_bits(ux); var b := real_of_bits(uy); # Infinity where either is infinite, NaN included, and NaN where either is # NaN otherwise. return b if ey == 0x7FF; return a if ex == 0x7FF || uy == 0; # b is too small to change a: a + b rounds as a + b^2 / 2a would. return a + b if ex - ey > 64; # Scaled by 2^-700 or 2^700, so neither square overflows and the low parts do # not underflow. var z: f64 = 1; if ex > 0x3FF + 510 { z = 5.260135901548374e+210; a *= 1.90109156629516e-211; b *= 1.90109156629516e-211; } else if ey < 0x3FF - 450 { z = 1.90109156629516e-211; a *= 5.260135901548374e+210; b *= 5.260135901548374e+210; } val sa := hypot_squared(a); val sb := hypot_squared(b); return z * square_root(sb.lo + sa.lo + sb.hi + sa.hi); }