Initial Commit
This commit is contained in:
1625 files changed
+43424
No files matched your search
@@ -0,0 +1,150 @@
|
||||
#! @file bits.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains a real's bits, as IEEE 754 lays them out, and what they say of it:
|
||||
#! whether it is not a number, infinite, finite, or subnormal, and its sign.
|
||||
#! Also the pieces of a double's bits the functions of this module are written
|
||||
#! with, as the C they are ported from reads them.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
#! The bits of an `f64`: its sign, then 11 bits of exponent, then 52 of
|
||||
#! significand.
|
||||
#! @param x The real.
|
||||
#! @returns Its bits.
|
||||
pub def bits_of(x: f64): u64 {
|
||||
var held := x;
|
||||
return ((&mut held) as *mut u64)[0];
|
||||
}
|
||||
|
||||
#! The bits of an `f32`: its sign, then 8 bits of exponent, then 23 of
|
||||
#! significand.
|
||||
#! @param x The real.
|
||||
#! @returns Its bits.
|
||||
pub def bits_of(x: f32): u32 {
|
||||
var held := x;
|
||||
return ((&mut held) as *mut u32)[0];
|
||||
}
|
||||
|
||||
#! The `f64` some bits are.
|
||||
#! @param bits The bits, as `bits_of` gives them.
|
||||
#! @returns The real.
|
||||
pub def real_of_bits(bits: u64): f64 {
|
||||
var held := bits;
|
||||
return ((&mut held) as *mut f64)[0];
|
||||
}
|
||||
|
||||
#! The `f32` some bits are.
|
||||
#! @param bits The bits, as `bits_of` gives them.
|
||||
#! @returns The real.
|
||||
pub def real_of_bits(bits: u32): f32 {
|
||||
var held := bits;
|
||||
return ((&mut held) as *mut f32)[0];
|
||||
}
|
||||
|
||||
#! Whether a real is not a number, which is the only value unequal to itself.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns Whether it is NaN.
|
||||
pub def is_nan<T>(x: T): bool where T: real {
|
||||
return x != x;
|
||||
}
|
||||
|
||||
#! Whether a real is infinite, of either sign.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns Whether it is.
|
||||
pub def is_infinite<T>(x: T): bool where T: real {
|
||||
return (bits_of(x as f64) & 0x7FFFFFFFFFFFFFFF) == 0x7FF0000000000000;
|
||||
}
|
||||
|
||||
#! Whether a real is finite: neither infinite nor NaN.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns Whether it is.
|
||||
pub def is_finite<T>(x: T): bool where T: real {
|
||||
return (bits_of(x as f64) & 0x7FF0000000000000) != 0x7FF0000000000000;
|
||||
}
|
||||
|
||||
#! Whether a real is subnormal: not zero, but closer to it than the least normal
|
||||
#! value of its type, so it holds fewer bits of precision.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns Whether it is.
|
||||
pub def is_subnormal<T>(x: T): bool where T: real {
|
||||
if const size_of::<T>() == 4 {
|
||||
val bits := bits_of(x as f32);
|
||||
return (bits & 0x7F800000) == 0 && (bits & 0x007FFFFF) != 0;
|
||||
}
|
||||
val bits := bits_of(x as f64);
|
||||
return (bits & 0x7FF0000000000000) == 0 && (bits & 0x000FFFFFFFFFFFFF) != 0;
|
||||
}
|
||||
|
||||
#! Whether a real's sign bit is set, as it is for `-0` and some NaNs, which no
|
||||
#! comparison with zero says.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns Whether it is.
|
||||
pub def sign_bit<T>(x: T): bool where T: real {
|
||||
return (bits_of(x as f64) >> 63) != 0;
|
||||
}
|
||||
|
||||
|
||||
# The pieces of a double's bits.
|
||||
|
||||
# The top 12 bits of a double: its sign and exponent.
|
||||
def top12(x: f64): u32 {
|
||||
return (bits_of(x) >> 52) as u32;
|
||||
}
|
||||
|
||||
# The high 32 bits of a double: its sign, exponent, and 20 bits of significand.
|
||||
def high_word(x: f64): u32 {
|
||||
return (bits_of(x) >> 32) as u32;
|
||||
}
|
||||
|
||||
# The low 32 bits of a double, of its significand.
|
||||
def low_word(x: f64): u32 {
|
||||
return (bits_of(x) & 0xFFFFFFFF) as u32;
|
||||
}
|
||||
|
||||
# The double of a high and a low word.
|
||||
def of_words(high: u32, low: u32): f64 {
|
||||
return real_of_bits(((high as u64) << 32) | low as u64);
|
||||
}
|
||||
|
||||
# The double with its high word replaced.
|
||||
def with_high_word(x: f64, high: u32): f64 {
|
||||
return real_of_bits(((high as u64) << 32) | (bits_of(x) & 0xFFFFFFFF));
|
||||
}
|
||||
|
||||
# The double with its low word replaced.
|
||||
def with_low_word(x: f64, low: u32): f64 {
|
||||
return real_of_bits((bits_of(x) & 0xFFFFFFFF00000000) | low as u64);
|
||||
}
|
||||
|
||||
|
||||
# What a function gives where its result is past what a double holds, as musl's
|
||||
# `__math_oflow`, `__math_uflow`, `__math_invalid`, and `__math_divzero` make
|
||||
# them: an infinity, a zero, NaN, or an infinity from dividing by zero, each of
|
||||
# the sign asked for.
|
||||
|
||||
def overflowed(sign: u32): f64 {
|
||||
val y: f64 = 3.105036184601418e+231;
|
||||
return (sign != 0 ? -y : y) * y;
|
||||
}
|
||||
|
||||
def underflowed(sign: u32): f64 {
|
||||
val y: f64 = 1.2882297539194267e-231;
|
||||
return (sign != 0 ? -y : y) * y;
|
||||
}
|
||||
|
||||
def invalid(x: f64): f64 {
|
||||
return (x - x) / (x - x);
|
||||
}
|
||||
|
||||
def divided_by_zero(sign: u32): f64 {
|
||||
return (sign != 0 ? -1.0 : 1.0) / 0.0;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#! @file constants.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the constants of real numbers: those of mathematics, as `f64`s, which
|
||||
#! `as f32` gives the nearest `f32` of, and those of each type of real, its
|
||||
#! largest and least values, epsilon, infinity, and not a number.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
#! π, the ratio of a circle's circumference to its diameter.
|
||||
pub const pi: f64 = 3.141592653589793;
|
||||
|
||||
#! τ, which is 2π, the angle of a whole turn in radians.
|
||||
pub const tau: f64 = 6.283185307179586;
|
||||
|
||||
#! e, the base of the natural logarithm.
|
||||
pub const e: f64 = 2.718281828459045;
|
||||
|
||||
#! The square root of 2.
|
||||
pub const sqrt_2: f64 = 1.4142135623730951;
|
||||
|
||||
#! The natural logarithm of 2.
|
||||
pub const ln_2: f64 = 0.6931471805599453;
|
||||
|
||||
#! The natural logarithm of 10.
|
||||
pub const ln_10: f64 = 2.302585092994046;
|
||||
|
||||
#! The largest finite value of a type of real.
|
||||
#! @tparam T The type of real.
|
||||
#! @returns About 3.4e38 for `f32`, and 1.8e308 for `f64`.
|
||||
pub def largest<T>(): T where T: real {
|
||||
if const size_of::<T>() == 4 {
|
||||
return 3.4028234663852886e38 as T;
|
||||
}
|
||||
return 1.7976931348623157e308 as T;
|
||||
}
|
||||
|
||||
#! The least positive normal value of a type of real. Those closer to zero are
|
||||
#! subnormal, and hold fewer bits of precision.
|
||||
#! @tparam T The type of real.
|
||||
#! @returns About 1.2e-38 for `f32`, and 2.2e-308 for `f64`.
|
||||
pub def smallest<T>(): T where T: real {
|
||||
if const size_of::<T>() == 4 {
|
||||
return 1.1754943508222875e-38 as T;
|
||||
}
|
||||
return 2.2250738585072014e-308 as T;
|
||||
}
|
||||
|
||||
#! The difference between 1 and the next value of a type of real.
|
||||
#! @tparam T The type of real.
|
||||
#! @returns 2^-23 for `f32`, and 2^-52 for `f64`.
|
||||
pub def epsilon<T>(): T where T: real {
|
||||
if const size_of::<T>() == 4 {
|
||||
return 1.1920928955078125e-07 as T;
|
||||
}
|
||||
return 2.220446049250313e-16 as T;
|
||||
}
|
||||
|
||||
#! Positive infinity, of a type of real.
|
||||
#! @tparam T The type of real.
|
||||
#! @returns Infinity; its negation is negative infinity.
|
||||
pub def infinity<T>(): T where T: real {
|
||||
return real_of_bits(0x7FF0000000000000u) as T;
|
||||
}
|
||||
|
||||
#! Not a number, of a type of real: a quiet NaN, of positive sign.
|
||||
#! @tparam T The type of real.
|
||||
#! @returns NaN, which is unequal to everything, itself included.
|
||||
pub def nan<T>(): T where T: real {
|
||||
return real_of_bits(0x7FF8000000000000u) as T;
|
||||
}
|
||||
@@ -0,0 +1,521 @@
|
||||
#! @file exact.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the operations of real numbers IEEE 754 defines to the bit: square
|
||||
#! roots, rounding to integers, absolute values, signs, fused multiplying and
|
||||
#! adding, and the least and greatest of two values. Each gives the one result
|
||||
#! the standard says, in the virtual machine, in an executable, and while
|
||||
#! compiling.
|
||||
#!
|
||||
#! An `f32` is worked in `f64` and rounded back, which gives its exact result
|
||||
#! too: a double holds more than twice a float's bits, so its square root
|
||||
#! rounds once, and a float's integer neighbours are floats. Only `fma` needs
|
||||
#! more, as musl's `fmaf` does.
|
||||
#!
|
||||
#! Where the processor has an instruction for one, as SSE4.1's `roundsd` and
|
||||
#! FMA3's `vfmadd231sd`, an executable works rounding and `fma` with it, of one
|
||||
#! number, as `lanes_apply` of one lane gives it, which gives the same bits, NaN
|
||||
#! included: the code here quiets a NaN it is given, as the instructions do.
|
||||
#!
|
||||
#! Rounding is ported from musl 1.2.5, which is Copyright © 2005-2020 Rich
|
||||
#! Felker, et al., under the MIT license. `fma_single` is from FreeBSD's
|
||||
#! s_fmaf.c, by way of musl, which says:
|
||||
#!
|
||||
#! Copyright (c) 2005-2011 David Schultz <das@FreeBSD.ORG>
|
||||
#! All rights reserved.
|
||||
#!
|
||||
#! Redistribution and use in source and binary forms, with or without
|
||||
#! modification, are permitted provided that the following conditions
|
||||
#! are met:
|
||||
#! 1. Redistributions of source code must retain the above copyright
|
||||
#! notice, this list of conditions and the following disclaimer.
|
||||
#! 2. Redistributions in binary form must reproduce the above copyright
|
||||
#! notice, this list of conditions and the following disclaimer in the
|
||||
#! documentation and/or other materials provided with the distribution.
|
||||
#!
|
||||
#! THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
#! ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
#! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
#! ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
#! FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
#! DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
#! OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
#! HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
#! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
#! OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
#! SUCH DAMAGE.
|
||||
|
||||
import core::intrinsics;
|
||||
module std::math;
|
||||
|
||||
|
||||
#! The square root of a real, correctly rounded.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns Its square root: `-0` for `-0`, infinity for infinity, and NaN for
|
||||
#! what is below zero or NaN.
|
||||
pub def sqrt<T>(x: T): T where T: real {
|
||||
return square_root(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The greatest integer not above a real.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns The integer, as a real: `-0` stays `-0`, and infinities and NaN
|
||||
#! are themselves.
|
||||
pub def floor<T>(x: T): T where T: real {
|
||||
return lane_alone::<T, lanes_floor>(x, x, x) if lanes_have::<T, lanes_floor, 1>();
|
||||
return floor_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The least integer not below a real.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns The integer, as a real: what is above -1 and below 0 gives `-0`.
|
||||
pub def ceil<T>(x: T): T where T: real {
|
||||
return lane_alone::<T, lanes_ceil>(x, x, x) if lanes_have::<T, lanes_ceil, 1>();
|
||||
return ceil_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! A real's integer part, toward zero.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns The integer, as a real, of the real's sign.
|
||||
pub def trunc<T>(x: T): T where T: real {
|
||||
return lane_alone::<T, lanes_trunc>(x, x, x) if lanes_have::<T, lanes_trunc, 1>();
|
||||
return trunc_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The integer nearest a real, halves away from zero, so 2.5 gives 3 and -2.5
|
||||
#! gives -3.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns The integer, as a real, of the real's sign.
|
||||
pub def round<T>(x: T): T where T: real {
|
||||
if lanes_have::<T, lanes_trunc, 1>() {
|
||||
# What truncating leaves is exact, so a half or more rounds away.
|
||||
val whole := lane_alone::<T, lanes_trunc>(x, x, x);
|
||||
val part := (x - whole) as f64;
|
||||
return whole + copysign(1 as T, x) if part >= 0.5 || part <= -0.5;
|
||||
return whole;
|
||||
}
|
||||
return round_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The integer nearest a real, halves to the even one, so 2.5 gives 2 and 3.5
|
||||
#! gives 4, as IEEE 754 rounds by default.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns The integer, as a real, of the real's sign.
|
||||
pub def round_even<T>(x: T): T where T: real {
|
||||
return lane_alone::<T, lanes_round_even>(x, x, x) if lanes_have::<T, lanes_round_even, 1>();
|
||||
return round_even_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! A value's distance from zero, of a type that negates and is ordered: a
|
||||
#! number, or a `duration`. A real's sign bit is cleared, so `-0` gives `0`, and
|
||||
#! NaN a NaN. The most negative integer of a type has no positive, and gives
|
||||
#! itself.
|
||||
#! @tparam T The type.
|
||||
#! @param x The value.
|
||||
#! @returns Its absolute value.
|
||||
pub def abs<T>(x: T): T where T: negatable, ordered {
|
||||
if const T is real {
|
||||
if const size_of::<T>() == 4 {
|
||||
return real_of_bits(bits_of(x as f32) & 0x7FFFFFFF) as T;
|
||||
}
|
||||
return real_of_bits(bits_of(x as f64) & 0x7FFFFFFFFFFFFFFF) as T;
|
||||
}
|
||||
return x < -x ? -x : x;
|
||||
}
|
||||
|
||||
#! A real with the magnitude of one and the sign of another, NaN included.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real giving the magnitude.
|
||||
#! @param sign The real giving the sign.
|
||||
#! @returns The real.
|
||||
pub def copysign<T>(x: T, sign: T): T where T: real {
|
||||
if const size_of::<T>() == 4 {
|
||||
return real_of_bits((bits_of(x as f32) & 0x7FFFFFFF) | (bits_of(sign as f32) & 0x80000000)) as T;
|
||||
}
|
||||
return real_of_bits((bits_of(x as f64) & 0x7FFFFFFFFFFFFFFF) | (bits_of(sign as f64) & 0x8000000000000000)) as T;
|
||||
}
|
||||
|
||||
#! `x * y + z`, rounded once rather than after the multiply too. Where the
|
||||
#! processor has FMA3, its instruction works it, which rounds once too, so it
|
||||
#! gives the same bits, and the same NaN: a factor's, `x`'s before `y`'s, and
|
||||
#! otherwise `z`'s, even where the product is NaN, as `∞ * 0` is.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The first factor.
|
||||
#! @param y The second factor.
|
||||
#! @param z What the product is added to.
|
||||
#! @returns The sum, correctly rounded.
|
||||
pub def fma<T>(x: T, y: T, z: T): T where T: real {
|
||||
return lane_alone::<T, lanes_fma>(x, y, z) if lanes_have::<T, lanes_fma, 1>();
|
||||
if const size_of::<T>() == 4 {
|
||||
return fma_single(x as f32, y as f32, z as f32) as T;
|
||||
}
|
||||
return fma_double(x as f64, y as f64, z as f64) as T;
|
||||
}
|
||||
|
||||
# The operation `OP` of one real, as `lanes_apply` of one lane works it with the
|
||||
# instruction of one number, where `lanes_have` says the processor has it: of
|
||||
# `a`, or of all three for `fma`.
|
||||
def lane_alone<T, const OP: u32>(a: T, b: T, c: T): T where T: real {
|
||||
var made: T = 0;
|
||||
lanes_apply::<T, OP, 1>((&mut made) as *mut u8, (&a) as *u8, (&b) as *u8, (&c) as *u8);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The lesser of two values. Of reals, this is IEEE 754's `minimum`: NaN where
|
||||
#! either is NaN, and `-0` below `0`. Of others, `a` where they are equal.
|
||||
#! @tparam T The type.
|
||||
#! @param a One value.
|
||||
#! @param b The other.
|
||||
#! @returns The lesser.
|
||||
pub def min<T>(a: T, b: T): T where T: ordered {
|
||||
if const T is real {
|
||||
return a + b if a != a || b != b;
|
||||
if a == b {
|
||||
return sign_bit(a) ? a : b;
|
||||
}
|
||||
}
|
||||
return b < a ? b : a;
|
||||
}
|
||||
|
||||
#! The greater of two values. Of reals, this is IEEE 754's `maximum`: NaN where
|
||||
#! either is NaN, and `0` above `-0`. Of others, `a` where they are equal.
|
||||
#! @tparam T The type.
|
||||
#! @param a One value.
|
||||
#! @param b The other.
|
||||
#! @returns The greater.
|
||||
pub def max<T>(a: T, b: T): T where T: ordered {
|
||||
if const T is real {
|
||||
return a + b if a != a || b != b;
|
||||
if a == b {
|
||||
return sign_bit(a) ? b : a;
|
||||
}
|
||||
}
|
||||
return a < b ? b : a;
|
||||
}
|
||||
|
||||
#! A value held between two others, as `max(low, min(x, high))`: `low` where it
|
||||
#! is below, `high` where it is above, and itself between. Of reals, NaN gives
|
||||
#! NaN. Where `low` is above `high`, it gives `low`.
|
||||
#! @tparam T The type.
|
||||
#! @param x The value.
|
||||
#! @param low The least it may be.
|
||||
#! @param high The greatest it may be.
|
||||
#! @returns The value held.
|
||||
pub def clamp<T>(x: T, low: T, high: T): T where T: ordered {
|
||||
return max(low, min(x, high));
|
||||
}
|
||||
|
||||
|
||||
# Doubles.
|
||||
|
||||
# 2^52, past which every double is an integer, and which adding and taking away
|
||||
# rounds a smaller one to an integer.
|
||||
const to_integer: f64 = 4503599627370496.0;
|
||||
|
||||
def floor_double(x: f64): f64 {
|
||||
return x + x if x != x;
|
||||
val bits := bits_of(x);
|
||||
val e := (bits >> 52) & 0x7FF;
|
||||
return x if e >= 0x3FF + 52 || x == 0;
|
||||
# y is the integer nearest x, less x.
|
||||
val y := (bits >> 63) != 0 ? x - to_integer + to_integer - x : x + to_integer - to_integer - x;
|
||||
if e <= 0x3FF - 1 {
|
||||
return (bits >> 63) != 0 ? -1.0 : 0.0;
|
||||
}
|
||||
return x + y - 1.0 if y > 0;
|
||||
return x + y;
|
||||
}
|
||||
|
||||
def ceil_double(x: f64): f64 {
|
||||
return x + x if x != x;
|
||||
val bits := bits_of(x);
|
||||
val e := (bits >> 52) & 0x7FF;
|
||||
return x if e >= 0x3FF + 52 || x == 0;
|
||||
val y := (bits >> 63) != 0 ? x - to_integer + to_integer - x : x + to_integer - to_integer - x;
|
||||
if e <= 0x3FF - 1 {
|
||||
return (bits >> 63) != 0 ? -0.0 : 1.0;
|
||||
}
|
||||
return x + y + 1.0 if y < 0;
|
||||
return x + y;
|
||||
}
|
||||
|
||||
def trunc_double(x: f64): f64 {
|
||||
return x + x if x != x;
|
||||
val bits := bits_of(x);
|
||||
var e := (((bits >> 52) & 0x7FF) as i64) - 0x3FF + 12;
|
||||
return x if e >= 52 + 12;
|
||||
if e < 12 {
|
||||
e = 1;
|
||||
}
|
||||
val m := 0xFFFFFFFFFFFFFFFFu >> (e as u64);
|
||||
return x if (bits & m) == 0;
|
||||
return real_of_bits(bits & ~m);
|
||||
}
|
||||
|
||||
def round_double(x: f64): f64 {
|
||||
return x + x if x != x;
|
||||
val bits := bits_of(x);
|
||||
val e := (bits >> 52) & 0x7FF;
|
||||
return x if e >= 0x3FF + 52;
|
||||
val negative := (bits >> 63) != 0;
|
||||
val a := negative ? -x : x;
|
||||
if e < 0x3FF - 1 {
|
||||
return 0.0 * x;
|
||||
}
|
||||
var y := a + to_integer - to_integer - a;
|
||||
if y > 0.5 {
|
||||
y = y + a - 1.0;
|
||||
} else if y <= -0.5 {
|
||||
y = y + a + 1.0;
|
||||
} else {
|
||||
y = y + a;
|
||||
}
|
||||
return negative ? -y : y;
|
||||
}
|
||||
|
||||
def round_even_double(x: f64): f64 {
|
||||
return x + x if x != x;
|
||||
val bits := bits_of(x);
|
||||
val e := (bits >> 52) & 0x7FF;
|
||||
return x if e >= 0x3FF + 52;
|
||||
val negative := (bits >> 63) != 0;
|
||||
val y := negative ? x - to_integer + to_integer : x + to_integer - to_integer;
|
||||
if y == 0 {
|
||||
return negative ? -0.0 : 0.0;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
# x times 2^n, rounded once: musl's `scalbn`.
|
||||
def scalbn(x: f64, power: i32): f64 {
|
||||
var n := power;
|
||||
var y := x;
|
||||
if n > 1023 {
|
||||
y *= 8.98846567431158e307;
|
||||
n -= 1023;
|
||||
if n > 1023 {
|
||||
y *= 8.98846567431158e307;
|
||||
n -= 1023;
|
||||
if n > 1023 {
|
||||
n = 1023;
|
||||
}
|
||||
}
|
||||
} else if n < -1022 {
|
||||
# The last n is below -53, so a subnormal result rounds once.
|
||||
y *= 2.2250738585072014e-308 * 9007199254740992.0;
|
||||
n += 1022 - 53;
|
||||
if n < -1022 {
|
||||
y *= 2.2250738585072014e-308 * 9007199254740992.0;
|
||||
n += 1022 - 53;
|
||||
if n < -1022 {
|
||||
n = -1022;
|
||||
}
|
||||
}
|
||||
}
|
||||
return y * real_of_bits(((0x3FF + n) as u64) << 52);
|
||||
}
|
||||
|
||||
|
||||
# Fused multiplying and adding, of doubles, from musl's fma.c: the product is
|
||||
# held exactly in 128 bits, the sum is formed in them, and the result is rounded
|
||||
# once converting to a double.
|
||||
|
||||
# A double as its significand, shifted so its top 10 bits and its last are 0,
|
||||
# its exponent, and its sign.
|
||||
struct fma_part {
|
||||
var m: u64;
|
||||
var e: i32;
|
||||
var sign: i32;
|
||||
}
|
||||
|
||||
# Exponents from this one up are of zero, infinity, or NaN.
|
||||
const zero_inf_nan: i32 = 0x7FF - 0x3FF - 52 - 1;
|
||||
|
||||
def fma_normalize(x: f64): fma_part {
|
||||
var ix := bits_of(x);
|
||||
var e := (ix >> 52) as i32;
|
||||
val sign := e & 0x800;
|
||||
e &= 0x7FF;
|
||||
if e == 0 {
|
||||
ix = bits_of(x * 9223372036854775808.0);
|
||||
e = ((ix >> 52) & 0x7FF) as i32;
|
||||
e = e != 0 ? e - 63 : 0x800;
|
||||
}
|
||||
ix &= (1u << 52) - 1;
|
||||
ix |= 1u << 52;
|
||||
ix <<= 1;
|
||||
e -= 0x3FF + 52 + 1;
|
||||
return fma_part { m = ix, e = e, sign = sign };
|
||||
}
|
||||
|
||||
# 1 where a word is not zero, as C's `!!`.
|
||||
def any_of(word: u64): u64 {
|
||||
return word != 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
def fma_double(x: f64, y: f64, z: f64): f64 {
|
||||
# A NaN added to factors that aren't is what it gives, quieted, as FMA3 does,
|
||||
# though their product is NaN.
|
||||
return z + z if z != z && x == x && y == y;
|
||||
val nx := fma_normalize(x);
|
||||
val ny := fma_normalize(y);
|
||||
val nz := fma_normalize(z);
|
||||
return x * y + z if nx.e >= zero_inf_nan || ny.e >= zero_inf_nan;
|
||||
if nz.e >= zero_inf_nan {
|
||||
return x * y + z if nz.e > zero_inf_nan;
|
||||
return z;
|
||||
}
|
||||
|
||||
# r = x * y, of 128 bits.
|
||||
val xlo := nx.m & 0xFFFFFFFF;
|
||||
val xhi := nx.m >> 32;
|
||||
val ylo := ny.m & 0xFFFFFFFF;
|
||||
val yhi := ny.m >> 32;
|
||||
val t1 := xlo * ylo;
|
||||
val t2 := xlo * yhi + xhi * ylo;
|
||||
val t3 := xhi * yhi;
|
||||
var rlo := t1 + (t2 << 32);
|
||||
var rhi := t3 + (t2 >> 32) + (t1 > rlo ? 1u : 0u);
|
||||
|
||||
# The exponents aligned: z shifted left by kz and r right by kr, where kz + kr
|
||||
# is d, and e is then e + kr.
|
||||
var zlo: u64 = 0;
|
||||
var zhi: u64 = 0;
|
||||
var e := nx.e + ny.e;
|
||||
var d := nz.e - e;
|
||||
if d > 0 {
|
||||
if d < 64 {
|
||||
zlo = nz.m << (d as u64);
|
||||
zhi = nz.m >> ((64 - d) as u64);
|
||||
} else {
|
||||
zlo = 0;
|
||||
zhi = nz.m;
|
||||
e = nz.e - 64;
|
||||
d -= 64;
|
||||
if d == 0 {
|
||||
} else if d < 64 {
|
||||
rlo = (rhi << ((64 - d) as u64)) | (rlo >> (d as u64)) | any_of(rlo << ((64 - d) as u64));
|
||||
rhi = rhi >> (d as u64);
|
||||
} else {
|
||||
rlo = 1;
|
||||
rhi = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
zhi = 0;
|
||||
d = -d;
|
||||
if d == 0 {
|
||||
zlo = nz.m;
|
||||
} else if d < 64 {
|
||||
zlo = (nz.m >> (d as u64)) | any_of(nz.m << ((64 - d) as u64));
|
||||
} else {
|
||||
zlo = 1;
|
||||
}
|
||||
}
|
||||
|
||||
# The sum.
|
||||
var sign := nx.sign ^ ny.sign;
|
||||
val same_sign := (sign ^ nz.sign) == 0;
|
||||
var nonzero := true;
|
||||
if same_sign {
|
||||
rlo += zlo;
|
||||
rhi += zhi + (rlo < zlo ? 1u : 0u);
|
||||
} else {
|
||||
val t := rlo;
|
||||
rlo -= zlo;
|
||||
rhi = rhi - zhi - (t < rlo ? 1u : 0u);
|
||||
if (rhi >> 63) != 0 {
|
||||
rlo = 0 - rlo;
|
||||
rhi = (0 - rhi) - any_of(rlo);
|
||||
sign = sign != 0 ? 0 : 1;
|
||||
}
|
||||
nonzero = rhi != 0;
|
||||
}
|
||||
|
||||
# rhi is the top 63 bits of the result, its last bit sticky.
|
||||
if nonzero {
|
||||
e += 64;
|
||||
d = (leading_zeros(rhi) as i32) - 1;
|
||||
rhi = (rhi << (d as u64)) | (rlo >> ((64 - d) as u64)) | any_of(rlo << (d as u64));
|
||||
} else if rlo != 0 {
|
||||
d = (leading_zeros(rlo) as i32) - 1;
|
||||
if d < 0 {
|
||||
rhi = (rlo >> 1) | (rlo & 1);
|
||||
} else {
|
||||
rhi = rlo << (d as u64);
|
||||
}
|
||||
} else {
|
||||
# An exact zero, of the sign the operations give it.
|
||||
return x * y + z;
|
||||
}
|
||||
e -= d;
|
||||
|
||||
# The double, from |r| in [2^62, 2^63].
|
||||
var i := rhi as i64;
|
||||
if sign != 0 {
|
||||
i = -i;
|
||||
}
|
||||
var r := i as f64;
|
||||
if e < -1022 - 62 {
|
||||
# Subnormal before rounding.
|
||||
if e == -1022 - 63 {
|
||||
var c: f64 = 9223372036854775808.0;
|
||||
if sign != 0 {
|
||||
c = -c;
|
||||
}
|
||||
if r == c {
|
||||
# The least normal value, after rounding.
|
||||
return sign != 0 ? -2.2250738585072014e-308 : 2.2250738585072014e-308;
|
||||
}
|
||||
# A bit is lost scaling, so another top bit is added to round once, at
|
||||
# converting, where it is inexact.
|
||||
if (rhi << 53) != 0 {
|
||||
i = ((rhi >> 1) | (rhi & 1) | (1u << 62)) as i64;
|
||||
if sign != 0 {
|
||||
i = -i;
|
||||
}
|
||||
r = i as f64;
|
||||
r = 2.0 * r - c;
|
||||
}
|
||||
} else {
|
||||
# Rounded once, scaling.
|
||||
i = (((rhi >> 10) | any_of(rhi << 54)) << 10) as i64;
|
||||
if sign != 0 {
|
||||
i = -i;
|
||||
}
|
||||
r = i as f64;
|
||||
}
|
||||
}
|
||||
return scalbn(r, e);
|
||||
}
|
||||
|
||||
# Fused multiplying and adding, of floats, from FreeBSD's s_fmaf.c: the product
|
||||
# is exact in a double, and so is the sum but where it rounds to a halfway point
|
||||
# between two floats, which the error then moves off.
|
||||
def fma_single(x: f32, y: f32, z: f32): f32 {
|
||||
return z + z if z != z && x == x && y == y;
|
||||
val xy := (x as f64) * (y as f64);
|
||||
val wide := z as f64;
|
||||
val result := xy + wide;
|
||||
var bits := bits_of(result);
|
||||
val e := (bits >> 52) & 0x7FF;
|
||||
if (bits & 0x1FFFFFFF) != 0x10000000 || e == 0x7FF || (result - xy == wide && result - wide == xy) {
|
||||
return result as f32;
|
||||
}
|
||||
val negative := (bits >> 63) != 0;
|
||||
val error := negative == (wide > xy) ? xy - result + wide : wide - result + xy;
|
||||
if negative == (error < 0) {
|
||||
bits++;
|
||||
} else {
|
||||
bits--;
|
||||
}
|
||||
return real_of_bits(bits) as f32;
|
||||
}
|
||||
+263
@@ -0,0 +1,263 @@
|
||||
#! @file exp.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the exponential functions: e^x and 2^x.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, whose `exp` and `exp2` are from Arm's
|
||||
#! optimized-routines, which says:
|
||||
#!
|
||||
#! Copyright (c) 2018, Arm Limited.
|
||||
#! SPDX-License-Identifier: MIT
|
||||
#!
|
||||
#! Each is within 0.52 ulp of the true value for `f64`. An `f32` is worked in
|
||||
#! `f64` and rounded once more.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
#! e raised to a power.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The power.
|
||||
#! @returns e^x: infinity past about 709.78, and 0 below about -745.13.
|
||||
pub def exp<T>(x: T): T where T: real {
|
||||
return exp_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! 2 raised to a power.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The power.
|
||||
#! @returns 2^x: infinity from 1024 on, and 0 below -1075.
|
||||
pub def exp2<T>(x: T): T where T: real {
|
||||
return exp2_double(x as f64) as T;
|
||||
}
|
||||
|
||||
|
||||
# The data `exp`, `exp2`, and `pow` share, from musl's exp_data.c.
|
||||
|
||||
# How many entries the table has, a power of 2: 2^7.
|
||||
const exp_size: u64 = 128;
|
||||
|
||||
# N / ln 2.
|
||||
const exp_inverse_ln2_n: f64 = 184.6649652337873;
|
||||
|
||||
# -ln 2 / N, as a high part and a low part.
|
||||
const exp_negative_ln2_high_n: f64 = -0.005415212348111709;
|
||||
const exp_negative_ln2_low_n: f64 = -1.2864023111638346e-14;
|
||||
|
||||
# 1.5 * 2^52, which adding rounds to an integer.
|
||||
const exp_shift: f64 = 6755399441055744.0;
|
||||
|
||||
# The polynomial of e^r, for r near 0: its last four coefficients.
|
||||
const exp_c2: f64 = 0.49999999999996786;
|
||||
const exp_c3: f64 = 0.16666666666665886;
|
||||
const exp_c4: f64 = 0.0416666808410674;
|
||||
const exp_c5: f64 = 0.008333335853059549;
|
||||
|
||||
# 1.5 * 2^52 / N, which adding rounds to a multiple of 1 / N.
|
||||
const exp2_shift: f64 = 52776558133248.0;
|
||||
|
||||
# The polynomial of 2^r, for r near 0.
|
||||
const exp2_c1: f64 = 0.6931471805599453;
|
||||
const exp2_c2: f64 = 0.24022650695909065;
|
||||
const exp2_c3: f64 = 0.0555041086686087;
|
||||
const exp2_c4: f64 = 0.009618131975721055;
|
||||
const exp2_c5: f64 = 0.0013332074570119598;
|
||||
|
||||
# 2^(k / N) is about H[k] * (1 + T[k]) for each k from 0 to N: the bits of T[k],
|
||||
# then those of H[k] less (k << 52) / N.
|
||||
const exp_table: u64[256] = [
|
||||
0x0000000000000000, 0x3ff0000000000000, 0x3c9b3b4f1a88bf6e, 0x3feff63da9fb3335,
|
||||
0xbc7160139cd8dc5d, 0x3fefec9a3e778061, 0xbc905e7a108766d1, 0x3fefe315e86e7f85,
|
||||
0x3c8cd2523567f613, 0x3fefd9b0d3158574, 0xbc8bce8023f98efa, 0x3fefd06b29ddf6de,
|
||||
0x3c60f74e61e6c861, 0x3fefc74518759bc8, 0x3c90a3e45b33d399, 0x3fefbe3ecac6f383,
|
||||
0x3c979aa65d837b6d, 0x3fefb5586cf9890f, 0x3c8eb51a92fdeffc, 0x3fefac922b7247f7,
|
||||
0x3c3ebe3d702f9cd1, 0x3fefa3ec32d3d1a2, 0xbc6a033489906e0b, 0x3fef9b66affed31b,
|
||||
0xbc9556522a2fbd0e, 0x3fef9301d0125b51, 0xbc5080ef8c4eea55, 0x3fef8abdc06c31cc,
|
||||
0xbc91c923b9d5f416, 0x3fef829aaea92de0, 0x3c80d3e3e95c55af, 0x3fef7a98c8a58e51,
|
||||
0xbc801b15eaa59348, 0x3fef72b83c7d517b, 0xbc8f1ff055de323d, 0x3fef6af9388c8dea,
|
||||
0x3c8b898c3f1353bf, 0x3fef635beb6fcb75, 0xbc96d99c7611eb26, 0x3fef5be084045cd4,
|
||||
0x3c9aecf73e3a2f60, 0x3fef54873168b9aa, 0xbc8fe782cb86389d, 0x3fef4d5022fcd91d,
|
||||
0x3c8a6f4144a6c38d, 0x3fef463b88628cd6, 0x3c807a05b0e4047d, 0x3fef3f49917ddc96,
|
||||
0x3c968efde3a8a894, 0x3fef387a6e756238, 0x3c875e18f274487d, 0x3fef31ce4fb2a63f,
|
||||
0x3c80472b981fe7f2, 0x3fef2b4565e27cdd, 0xbc96b87b3f71085e, 0x3fef24dfe1f56381,
|
||||
0x3c82f7e16d09ab31, 0x3fef1e9df51fdee1, 0xbc3d219b1a6fbffa, 0x3fef187fd0dad990,
|
||||
0x3c8b3782720c0ab4, 0x3fef1285a6e4030b, 0x3c6e149289cecb8f, 0x3fef0cafa93e2f56,
|
||||
0x3c834d754db0abb6, 0x3fef06fe0a31b715, 0x3c864201e2ac744c, 0x3fef0170fc4cd831,
|
||||
0x3c8fdd395dd3f84a, 0x3feefc08b26416ff, 0xbc86a3803b8e5b04, 0x3feef6c55f929ff1,
|
||||
0xbc924aedcc4b5068, 0x3feef1a7373aa9cb, 0xbc9907f81b512d8e, 0x3feeecae6d05d866,
|
||||
0xbc71d1e83e9436d2, 0x3feee7db34e59ff7, 0xbc991919b3ce1b15, 0x3feee32dc313a8e5,
|
||||
0x3c859f48a72a4c6d, 0x3feedea64c123422, 0xbc9312607a28698a, 0x3feeda4504ac801c,
|
||||
0xbc58a78f4817895b, 0x3feed60a21f72e2a, 0xbc7c2c9b67499a1b, 0x3feed1f5d950a897,
|
||||
0x3c4363ed60c2ac11, 0x3feece086061892d, 0x3c9666093b0664ef, 0x3feeca41ed1d0057,
|
||||
0x3c6ecce1daa10379, 0x3feec6a2b5c13cd0, 0x3c93ff8e3f0f1230, 0x3feec32af0d7d3de,
|
||||
0x3c7690cebb7aafb0, 0x3feebfdad5362a27, 0x3c931dbdeb54e077, 0x3feebcb299fddd0d,
|
||||
0xbc8f94340071a38e, 0x3feeb9b2769d2ca7, 0xbc87deccdc93a349, 0x3feeb6daa2cf6642,
|
||||
0xbc78dec6bd0f385f, 0x3feeb42b569d4f82, 0xbc861246ec7b5cf6, 0x3feeb1a4ca5d920f,
|
||||
0x3c93350518fdd78e, 0x3feeaf4736b527da, 0x3c7b98b72f8a9b05, 0x3feead12d497c7fd,
|
||||
0x3c9063e1e21c5409, 0x3feeab07dd485429, 0x3c34c7855019c6ea, 0x3feea9268a5946b7,
|
||||
0x3c9432e62b64c035, 0x3feea76f15ad2148, 0xbc8ce44a6199769f, 0x3feea5e1b976dc09,
|
||||
0xbc8c33c53bef4da8, 0x3feea47eb03a5585, 0xbc845378892be9ae, 0x3feea34634ccc320,
|
||||
0xbc93cedd78565858, 0x3feea23882552225, 0x3c5710aa807e1964, 0x3feea155d44ca973,
|
||||
0xbc93b3efbf5e2228, 0x3feea09e667f3bcd, 0xbc6a12ad8734b982, 0x3feea012750bdabf,
|
||||
0xbc6367efb86da9ee, 0x3fee9fb23c651a2f, 0xbc80dc3d54e08851, 0x3fee9f7df9519484,
|
||||
0xbc781f647e5a3ecf, 0x3fee9f75e8ec5f74, 0xbc86ee4ac08b7db0, 0x3fee9f9a48a58174,
|
||||
0xbc8619321e55e68a, 0x3fee9feb564267c9, 0x3c909ccb5e09d4d3, 0x3feea0694fde5d3f,
|
||||
0xbc7b32dcb94da51d, 0x3feea11473eb0187, 0x3c94ecfd5467c06b, 0x3feea1ed0130c132,
|
||||
0x3c65ebe1abd66c55, 0x3feea2f336cf4e62, 0xbc88a1c52fb3cf42, 0x3feea427543e1a12,
|
||||
0xbc9369b6f13b3734, 0x3feea589994cce13, 0xbc805e843a19ff1e, 0x3feea71a4623c7ad,
|
||||
0xbc94d450d872576e, 0x3feea8d99b4492ed, 0x3c90ad675b0e8a00, 0x3feeaac7d98a6699,
|
||||
0x3c8db72fc1f0eab4, 0x3feeace5422aa0db, 0xbc65b6609cc5e7ff, 0x3feeaf3216b5448c,
|
||||
0x3c7bf68359f35f44, 0x3feeb1ae99157736, 0xbc93091fa71e3d83, 0x3feeb45b0b91ffc6,
|
||||
0xbc5da9b88b6c1e29, 0x3feeb737b0cdc5e5, 0xbc6c23f97c90b959, 0x3feeba44cbc8520f,
|
||||
0xbc92434322f4f9aa, 0x3feebd829fde4e50, 0xbc85ca6cd7668e4b, 0x3feec0f170ca07ba,
|
||||
0x3c71affc2b91ce27, 0x3feec49182a3f090, 0x3c6dd235e10a73bb, 0x3feec86319e32323,
|
||||
0xbc87c50422622263, 0x3feecc667b5de565, 0x3c8b1c86e3e231d5, 0x3feed09bec4a2d33,
|
||||
0xbc91bbd1d3bcbb15, 0x3feed503b23e255d, 0x3c90cc319cee31d2, 0x3feed99e1330b358,
|
||||
0x3c8469846e735ab3, 0x3feede6b5579fdbf, 0xbc82dfcd978e9db4, 0x3feee36bbfd3f37a,
|
||||
0x3c8c1a7792cb3387, 0x3feee89f995ad3ad, 0xbc907b8f4ad1d9fa, 0x3feeee07298db666,
|
||||
0xbc55c3d956dcaeba, 0x3feef3a2b84f15fb, 0xbc90a40e3da6f640, 0x3feef9728de5593a,
|
||||
0xbc68d6f438ad9334, 0x3feeff76f2fb5e47, 0xbc91eee26b588a35, 0x3fef05b030a1064a,
|
||||
0x3c74ffd70a5fddcd, 0x3fef0c1e904bc1d2, 0xbc91bdfbfa9298ac, 0x3fef12c25bd71e09,
|
||||
0x3c736eae30af0cb3, 0x3fef199bdd85529c, 0x3c8ee3325c9ffd94, 0x3fef20ab5fffd07a,
|
||||
0x3c84e08fd10959ac, 0x3fef27f12e57d14b, 0x3c63cdaf384e1a67, 0x3fef2f6d9406e7b5,
|
||||
0x3c676b2c6c921968, 0x3fef3720dcef9069, 0xbc808a1883ccb5d2, 0x3fef3f0b555dc3fa,
|
||||
0xbc8fad5d3ffffa6f, 0x3fef472d4a07897c, 0xbc900dae3875a949, 0x3fef4f87080d89f2,
|
||||
0x3c74a385a63d07a7, 0x3fef5818dcfba487, 0xbc82919e2040220f, 0x3fef60e316c98398,
|
||||
0x3c8e5a50d5c192ac, 0x3fef69e603db3285, 0x3c843a59ac016b4b, 0x3fef7321f301b460,
|
||||
0xbc82d52107b43e1f, 0x3fef7c97337b9b5f, 0xbc892ab93b470dc9, 0x3fef864614f5a129,
|
||||
0x3c74b604603a88d3, 0x3fef902ee78b3ff6, 0x3c83c5ec519d7271, 0x3fef9a51fbc74c83,
|
||||
0xbc8ff7128fd391f0, 0x3fefa4afa2a490da, 0xbc8dae98e223747d, 0x3fefaf482d8e67f1,
|
||||
0x3c8ec3bc41aa2008, 0x3fefba1bee615a27, 0x3c842b94c3a9eb32, 0x3fefc52b376bba97,
|
||||
0x3c8a64a931d185ee, 0x3fefd0765b6e4540, 0xbc8e37bae43be3ed, 0x3fefdbfdad9cbe14,
|
||||
0x3c77893b4d91cd9d, 0x3fefe7c1819e90d8, 0x3c5305c14160cc89, 0x3feff3c22b8f71f1,
|
||||
];
|
||||
|
||||
|
||||
# e^x and 2^x, from musl's exp.c and exp2.c.
|
||||
|
||||
# Where scale * (1 + tmp) may overflow or underflow on the way. The bits of
|
||||
# scale are `sbits`, whose exponent may have overflowed into the sign, and `ki`
|
||||
# is the k reducing the argument: above zero the result may overflow, and below
|
||||
# it underflow.
|
||||
def exp_special(tmp: f64, scale_bits: u64, ki: u64): f64 {
|
||||
var sbits := scale_bits;
|
||||
if (ki & 0x80000000) == 0 {
|
||||
# k above zero, where the scale's exponent may have overflowed by 460.
|
||||
sbits -= 1009u << 52;
|
||||
val scale := real_of_bits(sbits);
|
||||
return 5.486124068793689e+303 * (scale + scale * tmp);
|
||||
}
|
||||
# k below zero, where the subnormals need care.
|
||||
sbits += 1022u << 52;
|
||||
val scale := real_of_bits(sbits);
|
||||
var y := scale + scale * tmp;
|
||||
if y < 1.0 {
|
||||
# Rounded to the precision it ends with before scaling into the subnormals,
|
||||
# so it rounds once.
|
||||
var lo := scale - y + scale * tmp;
|
||||
val hi := 1.0 + y;
|
||||
lo = 1.0 - hi + y + lo;
|
||||
y = (hi + lo) - 1.0;
|
||||
if y == 0.0 {
|
||||
y = 0.0;
|
||||
}
|
||||
}
|
||||
return 2.2250738585072014e-308 * y;
|
||||
}
|
||||
|
||||
def exp_double(x: f64): f64 {
|
||||
var abstop := top12(x) & 0x7FF;
|
||||
if abstop - 0x3C9 >= 0x408 - 0x3C9 {
|
||||
# Below 2^-54, where e^x is 1 + x.
|
||||
return 1.0 + x if abstop - 0x3C9 >= 0x80000000;
|
||||
if abstop >= 0x409 {
|
||||
return 0.0 if bits_of(x) == 0xFFF0000000000000;
|
||||
return 1.0 + x if abstop >= 0x7FF;
|
||||
return (bits_of(x) >> 63) != 0 ? underflowed(0) : overflowed(0);
|
||||
}
|
||||
# A large x, which `exp_special` works out.
|
||||
abstop = 0;
|
||||
}
|
||||
|
||||
# e^x is 2^(k / N) * e^r, where x is k ln 2 / N + r, k is an integer, and r
|
||||
# is within ln 2 / 2N of 0.
|
||||
val z := exp_inverse_ln2_n * x;
|
||||
var kd := z + exp_shift;
|
||||
val ki := bits_of(kd);
|
||||
kd -= exp_shift;
|
||||
val r := x + kd * exp_negative_ln2_high_n + kd * exp_negative_ln2_low_n;
|
||||
# 2^(k / N) is about scale * (1 + tail).
|
||||
val index := 2 * (ki % exp_size);
|
||||
val top := ki << (52 - 7);
|
||||
val tail := real_of_bits(exp_table[index]);
|
||||
val sbits := exp_table[index + 1] + top;
|
||||
# e^x is about scale + scale * (tail + e^r - 1).
|
||||
val r2 := r * r;
|
||||
val tmp := tail + r + r2 * (exp_c2 + r * exp_c3) + r2 * r2 * (exp_c4 + r * exp_c5);
|
||||
return exp_special(tmp, sbits, ki) if abstop == 0;
|
||||
val scale := real_of_bits(sbits);
|
||||
return scale + scale * tmp;
|
||||
}
|
||||
|
||||
# As `exp_special`, for 2^x, whose scale's exponent may overflow by only 1.
|
||||
def exp2_special(tmp: f64, scale_bits: u64, ki: u64): f64 {
|
||||
var sbits := scale_bits;
|
||||
if (ki & 0x80000000) == 0 {
|
||||
sbits -= 1u << 52;
|
||||
val scale := real_of_bits(sbits);
|
||||
return 2.0 * (scale + scale * tmp);
|
||||
}
|
||||
sbits += 1022u << 52;
|
||||
val scale := real_of_bits(sbits);
|
||||
var y := scale + scale * tmp;
|
||||
if y < 1.0 {
|
||||
var lo := scale - y + scale * tmp;
|
||||
val hi := 1.0 + y;
|
||||
lo = 1.0 - hi + y + lo;
|
||||
y = (hi + lo) - 1.0;
|
||||
if y == 0.0 {
|
||||
y = 0.0;
|
||||
}
|
||||
}
|
||||
return 2.2250738585072014e-308 * y;
|
||||
}
|
||||
|
||||
def exp2_double(x: f64): f64 {
|
||||
var abstop := top12(x) & 0x7FF;
|
||||
if abstop - 0x3C9 >= 0x408 - 0x3C9 {
|
||||
return 1.0 + x if abstop - 0x3C9 >= 0x80000000;
|
||||
if abstop >= 0x409 {
|
||||
return 0.0 if bits_of(x) == 0xFFF0000000000000;
|
||||
return 1.0 + x if abstop >= 0x7FF;
|
||||
if (bits_of(x) >> 63) == 0 {
|
||||
return overflowed(0);
|
||||
}
|
||||
# At -1075 and below.
|
||||
if bits_of(x) >= 0xC090CC0000000000 {
|
||||
return underflowed(0);
|
||||
}
|
||||
}
|
||||
# Past 928 either way.
|
||||
if 2 * bits_of(x) > 2 * 0x408D000000000000u {
|
||||
abstop = 0;
|
||||
}
|
||||
}
|
||||
|
||||
# 2^x is 2^(k / N) * 2^r, where x is k / N + r, k is an integer, and r is
|
||||
# within 1 / 2N of 0.
|
||||
var kd := x + exp2_shift;
|
||||
val ki := bits_of(kd);
|
||||
kd -= exp2_shift;
|
||||
val r := x - kd;
|
||||
val index := 2 * (ki % exp_size);
|
||||
val top := ki << (52 - 7);
|
||||
val tail := real_of_bits(exp_table[index]);
|
||||
val sbits := exp_table[index + 1] + top;
|
||||
val r2 := r * r;
|
||||
val tmp := tail + r * exp2_c1 + r2 * (exp2_c2 + r * exp2_c3) + r2 * r2 * (exp2_c4 + r * exp2_c5);
|
||||
return exp2_special(tmp, sbits, ki) if abstop == 0;
|
||||
val scale := real_of_bits(sbits);
|
||||
return scale + scale * tmp;
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
#! @file expm1.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains e^x - 1 and ln(1 + x), which keep their precision where x is near
|
||||
#! 0, as `exp(x) - 1` and `log(1 + x)` lose it.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
|
||||
#! under the MIT license. Its `expm1` and `log1p` are from FreeBSD's s_expm1.c
|
||||
#! and s_log1p.c, which say:
|
||||
#!
|
||||
#! 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.
|
||||
#!
|
||||
#! Each is within 1 ulp of the true value for `f64`. An `f32` is worked in
|
||||
#! `f64` and rounded once more.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
#! e raised to a power, less 1, which is precise where the power is near 0.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The power.
|
||||
#! @returns e^x - 1: x itself near 0, infinity past about 709.78, and -1 below
|
||||
#! about -37.43 and for negative infinity.
|
||||
pub def expm1<T>(x: T): T where T: real {
|
||||
return expm1_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The natural logarithm of 1 more than a real, which is precise where the real
|
||||
#! is near 0.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns ln(1 + x): x itself near 0, negative infinity at -1, and NaN below
|
||||
#! -1.
|
||||
pub def log1p<T>(x: T): T where T: real {
|
||||
return log1p_double(x as f64) as T;
|
||||
}
|
||||
|
||||
|
||||
# The constants of `expm1` and `log1p`.
|
||||
|
||||
# Past this, about 709.78, e^x - 1 overflows.
|
||||
const expm1_overflow: f64 = 709.782712893384;
|
||||
|
||||
# ln 2, as a high part, whose last 32 bits are 0 so that multiplying it by a
|
||||
# small integer is exact, and a low part. `log1p` has them too.
|
||||
const expm1_ln2_high: f64 = 0.6931471803691238;
|
||||
const expm1_ln2_low: f64 = 1.9082149292705877e-10;
|
||||
|
||||
# 1 / ln 2.
|
||||
const expm1_inverse_ln2: f64 = 1.4426950408889634;
|
||||
|
||||
# The rational function of r near 0: the coefficients of its polynomial in
|
||||
# (r^2) / 2, each the nth scaled by 2^n.
|
||||
const expm1_q1: f64 = -0.03333333333333313;
|
||||
const expm1_q2: f64 = 0.0015873015872548146;
|
||||
const expm1_q3: f64 = -7.93650757867488e-05;
|
||||
const expm1_q4: f64 = 4.008217827329362e-06;
|
||||
const expm1_q5: f64 = -2.0109921818362437e-07;
|
||||
|
||||
# The polynomial of ln(1 + f), in s^2 where s is f / (2 + f).
|
||||
const log1p_lg1: f64 = 0.6666666666666735;
|
||||
const log1p_lg2: f64 = 0.3999999999940942;
|
||||
const log1p_lg3: f64 = 0.2857142874366239;
|
||||
const log1p_lg4: f64 = 0.22222198432149784;
|
||||
const log1p_lg5: f64 = 0.1818357216161805;
|
||||
const log1p_lg6: f64 = 0.15313837699209373;
|
||||
const log1p_lg7: f64 = 0.14798198605116586;
|
||||
|
||||
|
||||
# e^x - 1, from musl's expm1.c.
|
||||
def expm1_double(argument: f64): f64 {
|
||||
var x := argument;
|
||||
val hx := high_word(x) & 0x7FFFFFFF;
|
||||
val negative := (bits_of(x) >> 63) != 0;
|
||||
|
||||
# |x| at least 56 ln 2, where e^x - 1 is -1 below 0 and may overflow above,
|
||||
# or infinite, or NaN.
|
||||
if hx >= 0x4043687A {
|
||||
return x if x != x;
|
||||
return -1.0 if negative;
|
||||
return x * 8.98846567431158e307 if x > expm1_overflow;
|
||||
}
|
||||
|
||||
# x is k ln 2 + r, where r is at most ln 2 / 2 from 0, as hi - lo, and c is
|
||||
# what rounding r lost.
|
||||
var k: i32 = 0;
|
||||
var c := 0.0;
|
||||
if hx > 0x3FD62E42 {
|
||||
# |x| above ln 2 / 2.
|
||||
var hi := 0.0;
|
||||
var lo := 0.0;
|
||||
if hx < 0x3FF0A2B2 {
|
||||
# And below 3 ln 2 / 2, where k is 1 or -1.
|
||||
if !negative {
|
||||
hi = x - expm1_ln2_high;
|
||||
lo = expm1_ln2_low;
|
||||
k = 1;
|
||||
} else {
|
||||
hi = x + expm1_ln2_high;
|
||||
lo = -expm1_ln2_low;
|
||||
k = -1;
|
||||
}
|
||||
} else {
|
||||
k = (expm1_inverse_ln2 * x + (negative ? -0.5 : 0.5)) as i32;
|
||||
val t := k as f64;
|
||||
# Exact, as the high part's last bits are 0.
|
||||
hi = x - t * expm1_ln2_high;
|
||||
lo = t * expm1_ln2_low;
|
||||
}
|
||||
x = hi - lo;
|
||||
c = (hi - x) - lo;
|
||||
} else if hx < 0x3C900000 {
|
||||
# |x| below 2^-54, where e^x - 1 is x.
|
||||
return x;
|
||||
}
|
||||
|
||||
# e^r - 1 is r - e, by a rational function of r^2 / 2.
|
||||
val hfx := 0.5 * x;
|
||||
val hxs := x * hfx;
|
||||
val r1 := 1.0 + hxs * (expm1_q1 + hxs * (expm1_q2 + hxs * (expm1_q3 + hxs * (expm1_q4 + hxs * expm1_q5))));
|
||||
val t := 3.0 - r1 * hfx;
|
||||
var e := hxs * ((r1 - t) / (6.0 - x * t));
|
||||
return x - (x * e - hxs) if k == 0;
|
||||
# c corrected for, e^x is about 2^k (r - e + 1).
|
||||
e = x * (e - c) - c;
|
||||
e -= hxs;
|
||||
return 0.5 * (x - e) - 0.5 if k == -1;
|
||||
if k == 1 {
|
||||
return -2.0 * (e - (x + 0.5)) if x < -0.25;
|
||||
return 1.0 + 2.0 * (x - e);
|
||||
}
|
||||
val twopk := real_of_bits(((0x3FF + k) as u64) << 52);
|
||||
if k < 0 || k > 56 {
|
||||
# k below 0 or past 56, where 2^k (r - e + 1) - 1 is near enough.
|
||||
var y := x - e + 1.0;
|
||||
if k == 1024 {
|
||||
# 2^k overflows, so it is 2 * 2^1023.
|
||||
y = y * 2.0 * 8.98846567431158e307;
|
||||
} else {
|
||||
y = y * twopk;
|
||||
}
|
||||
return y - 1.0;
|
||||
}
|
||||
# Else 2^k (r - e + (1 - 2^-k)), the 1 taken away before scaling.
|
||||
val twomk := real_of_bits(((0x3FF - k) as u64) << 52);
|
||||
return (x - e + (1.0 - twomk)) * twopk if k < 20;
|
||||
return (x - (e + twomk) + 1.0) * twopk;
|
||||
}
|
||||
|
||||
# ln(1 + x), from musl's log1p.c.
|
||||
def log1p_double(x: f64): f64 {
|
||||
val hx := high_word(x);
|
||||
# 1 + x is 2^k (1 + f), where 1 + f is from sqrt(2) / 2 to sqrt 2, and c
|
||||
# corrects for 1 + x being rounded.
|
||||
var k: i32 = 1;
|
||||
var c := 0.0;
|
||||
var f := 0.0;
|
||||
if hx < 0x3FDA827A || (hx >> 31) != 0 {
|
||||
# 1 + x below sqrt 2, a little over.
|
||||
if hx >= 0xBFF00000 {
|
||||
# x at most -1, or NaN of the sign bit.
|
||||
return x / 0.0 if x == -1.0;
|
||||
return (x - x) / 0.0;
|
||||
}
|
||||
# |x| below 2^-53, where ln(1 + x) is x.
|
||||
return x if (hx << 1) < (0x3CA00000 << 1);
|
||||
if hx <= 0xBFD2BEC4 {
|
||||
# 1 + x from sqrt(2) / 2, a little under, where x is f itself.
|
||||
k = 0;
|
||||
c = 0.0;
|
||||
f = x;
|
||||
}
|
||||
} else if hx >= 0x7FF00000 {
|
||||
# Infinite, or NaN.
|
||||
return x;
|
||||
}
|
||||
if k != 0 {
|
||||
var u := 1.0 + x;
|
||||
var hu := high_word(u);
|
||||
hu += 0x3FF00000 - 0x3FE6A09E;
|
||||
k = ((hu >> 20) as i32) - 0x3FF;
|
||||
# ln(1 + x) - ln u is about c / u, which is too small to count past 2^54.
|
||||
if k < 54 {
|
||||
c = k >= 2 ? 1.0 - (u - x) : x - (u - 1.0);
|
||||
c /= u;
|
||||
} else {
|
||||
c = 0.0;
|
||||
}
|
||||
# u scaled into [sqrt(2) / 2, sqrt 2].
|
||||
hu = (hu & 0x000FFFFF) + 0x3FE6A09E;
|
||||
u = with_high_word(u, hu);
|
||||
f = u - 1.0;
|
||||
}
|
||||
val hfsq := 0.5 * f * f;
|
||||
val s := f / (2.0 + f);
|
||||
val z := s * s;
|
||||
val w := z * z;
|
||||
val t1 := w * (log1p_lg2 + w * (log1p_lg4 + w * log1p_lg6));
|
||||
val t2 := z * (log1p_lg1 + w * (log1p_lg3 + w * (log1p_lg5 + w * log1p_lg7)));
|
||||
val r := t2 + t1;
|
||||
val dk := k as f64;
|
||||
return s * (hfsq + r) + (dk * expm1_ln2_low + c) - hfsq + f + dk * expm1_ln2_high;
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
#! @file hyperbolic.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the hyperbolic functions, sinh, cosh, and tanh, and their inverses,
|
||||
#! asinh, acosh, and atanh.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
|
||||
#! under the MIT license.
|
||||
#!
|
||||
#! Each is within 2 ulp of the true value for `f64`, and `cosh` within 1: `tanh`
|
||||
#! comes nearest 2, near 0.2, and `acosh` near 1. An `f32` is worked in `f64`
|
||||
#! and rounded once more.
|
||||
|
||||
import core::intrinsics;
|
||||
module std::math;
|
||||
|
||||
|
||||
#! The hyperbolic sine of a real, (e^x - e^-x) / 2.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns sinh x, of x's sign: x itself near 0, and infinity past about
|
||||
#! 710.48.
|
||||
pub def sinh<T>(x: T): T where T: real {
|
||||
return sinh_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The hyperbolic cosine of a real, (e^x + e^-x) / 2.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns cosh x, at least 1: 1 near 0, and infinity past about 710.48 either
|
||||
#! way.
|
||||
pub def cosh<T>(x: T): T where T: real {
|
||||
return cosh_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The hyperbolic tangent of a real, sinh x / cosh x.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns tanh x, of x's sign: x itself near 0, and 1 past about 19.06, and
|
||||
#! -1 below about -19.06, infinities included.
|
||||
pub def tanh<T>(x: T): T where T: real {
|
||||
return tanh_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The inverse hyperbolic sine of a real, the y whose sinh is x.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns asinh x, of x's sign: x itself near 0, and infinities themselves.
|
||||
pub def asinh<T>(x: T): T where T: real {
|
||||
return asinh_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The inverse hyperbolic cosine of a real, the y from 0 up whose cosh is x.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real, at least 1.
|
||||
#! @returns acosh x: 0 at 1, infinity for infinity, and NaN below 1.
|
||||
pub def acosh<T>(x: T): T where T: real {
|
||||
return acosh_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The inverse hyperbolic tangent of a real, the y whose tanh is x.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real, from -1 to 1.
|
||||
#! @returns atanh x, of x's sign: x itself near 0, infinity of x's sign at 1
|
||||
#! and -1, and NaN beyond them.
|
||||
pub def atanh<T>(x: T): T where T: real {
|
||||
return atanh_double(x as f64) as T;
|
||||
}
|
||||
|
||||
|
||||
# A sign times e^x / 2, for x from about ln(DBL_MAX) up, where e^x overflows,
|
||||
# from musl's __expo2.c: e^(x - k ln 2) times 2^(k - 1). k is odd, so 2^(k - 1)
|
||||
# is the square of a scale, which multiplies twice as the square overflows,
|
||||
# and k ln 2 is a double with little relative error.
|
||||
const hyperbolic_k: i32 = 2043;
|
||||
const hyperbolic_k_ln2: f64 = 1416.0996898839683;
|
||||
|
||||
def hyperbolic_expo2(x: f64, sign: f64): f64 {
|
||||
val scale := of_words(((0x3FF + hyperbolic_k / 2) as u32) << 20, 0);
|
||||
return exp_double(x - hyperbolic_k_ln2) * (sign * scale) * scale;
|
||||
}
|
||||
|
||||
|
||||
# sinh x, from musl's sinh.c, of e^|x| - 1.
|
||||
def sinh_double(x: f64): f64 {
|
||||
var h := 0.5;
|
||||
if (bits_of(x) >> 63) != 0 {
|
||||
h = -h;
|
||||
}
|
||||
val absx := real_of_bits(bits_of(x) & 0x7FFFFFFFFFFFFFFF);
|
||||
val w := high_word(absx);
|
||||
|
||||
# |x| below ln(DBL_MAX).
|
||||
if w < 0x40862E42 {
|
||||
val t := expm1_double(absx);
|
||||
if w < 0x3FF00000 {
|
||||
# Below 2^-26, sinh x is x.
|
||||
return x if w < 0x3FF00000 - (26 << 20);
|
||||
return h * (2.0 * t - t * t / (t + 1.0));
|
||||
}
|
||||
return h * (t + t / (t + 1.0));
|
||||
}
|
||||
|
||||
# |x| above ln(DBL_MAX), or NaN.
|
||||
return hyperbolic_expo2(absx, 2.0 * h);
|
||||
}
|
||||
|
||||
# cosh x, from musl's cosh.c.
|
||||
def cosh_double(argument: f64): f64 {
|
||||
val x := real_of_bits(bits_of(argument) & 0x7FFFFFFFFFFFFFFF);
|
||||
val w := high_word(x);
|
||||
|
||||
# |x| below ln 2, by e^|x| - 1.
|
||||
if w < 0x3FE62E42 {
|
||||
# Below 2^-26, cosh x is 1.
|
||||
return 1.0 if w < 0x3FF00000 - (26 << 20);
|
||||
val t := expm1_double(x);
|
||||
return 1.0 + t * t / (2.0 * (1.0 + t));
|
||||
}
|
||||
|
||||
# Below ln(DBL_MAX), by e^|x|.
|
||||
if w < 0x40862E42 {
|
||||
val t := exp_double(x);
|
||||
return 0.5 * (t + 1.0 / t);
|
||||
}
|
||||
|
||||
# Above ln(DBL_MAX), or NaN.
|
||||
return hyperbolic_expo2(x, 1.0);
|
||||
}
|
||||
|
||||
# tanh x, from musl's tanh.c, of e^2|x| - 1.
|
||||
def tanh_double(argument: f64): f64 {
|
||||
val negative := (bits_of(argument) >> 63) != 0;
|
||||
val x := real_of_bits(bits_of(argument) & 0x7FFFFFFFFFFFFFFF);
|
||||
val w := high_word(x);
|
||||
|
||||
var t := 0.0;
|
||||
if w > 0x3FE193EA {
|
||||
# |x| above ln(3) / 2, about 0.5493, or NaN.
|
||||
if w > 0x40340000 {
|
||||
# Above 20, where tanh x is 1, or NaN, which this keeps.
|
||||
t = 1.0 - 0.0 / x;
|
||||
} else {
|
||||
t = expm1_double(2.0 * x);
|
||||
t = 1.0 - 2.0 / (t + 2.0);
|
||||
}
|
||||
} else if w > 0x3FD058AE {
|
||||
# Above ln(5/3) / 2, about 0.2554.
|
||||
t = expm1_double(2.0 * x);
|
||||
t = t / (t + 2.0);
|
||||
} else if w >= 0x00100000 {
|
||||
# Normal.
|
||||
t = expm1_double(-2.0 * x);
|
||||
t = -t / (t + 2.0);
|
||||
} else {
|
||||
# Subnormal, where tanh x is x.
|
||||
t = x;
|
||||
}
|
||||
return negative ? -t : t;
|
||||
}
|
||||
|
||||
# asinh x, from musl's asinh.c: ln(|x| + sqrt(x^2 + 1)), of x's sign.
|
||||
def asinh_double(argument: f64): f64 {
|
||||
val bits := bits_of(argument);
|
||||
val e := (bits >> 52) & 0x7FF;
|
||||
val negative := (bits >> 63) != 0;
|
||||
var x := real_of_bits(bits & 0x7FFFFFFFFFFFFFFF);
|
||||
|
||||
if e >= 0x3FF + 26 {
|
||||
# |x| at least 2^26, infinite, or NaN, where it is ln 2|x|.
|
||||
x = log_double(x) + ln_2;
|
||||
} else if e >= 0x3FF + 1 {
|
||||
# At least 2.
|
||||
x = log_double(2.0 * x + 1.0 / (square_root(x * x + 1.0) + x));
|
||||
} else if e >= 0x3FF - 26 {
|
||||
# At least 2^-26.
|
||||
x = log1p_double(x + x * x / (square_root(x * x + 1.0) + 1.0));
|
||||
}
|
||||
# Below 2^-26, asinh x is x.
|
||||
return negative ? -x : x;
|
||||
}
|
||||
|
||||
# acosh x, from musl's acosh.c: ln(x + sqrt(x^2 - 1)).
|
||||
def acosh_double(x: f64): f64 {
|
||||
# Below 1, NaN. musl leaves this to the functions it calls, which give it but
|
||||
# for some x from -2^26 to -2, where they give a number; the NaN is the one
|
||||
# they give elsewhere.
|
||||
return invalid(x) if x < 1.0;
|
||||
val e := (bits_of(x) >> 52) & 0x7FF;
|
||||
# Below 2.
|
||||
return log1p_double(x - 1.0 + square_root((x - 1.0) * (x - 1.0) + 2.0 * (x - 1.0))) if e < 0x3FF + 1;
|
||||
# Below 2^26.
|
||||
return log_double(2.0 * x - 1.0 / (x + square_root(x * x - 1.0))) if e < 0x3FF + 26;
|
||||
# At least 2^26, or NaN, where it is ln 2x.
|
||||
return log_double(x) + ln_2;
|
||||
}
|
||||
|
||||
# atanh x, from musl's atanh.c: ln((1 + x) / (1 - x)) / 2, of x's sign.
|
||||
def atanh_double(argument: f64): f64 {
|
||||
val bits := bits_of(argument);
|
||||
val e := (bits >> 52) & 0x7FF;
|
||||
val negative := (bits >> 63) != 0;
|
||||
var y := real_of_bits(bits & 0x7FFFFFFFFFFFFFFF);
|
||||
|
||||
if e < 0x3FF - 1 {
|
||||
# |x| below 1/2, and below 2^-32, where atanh x is x.
|
||||
if e >= 0x3FF - 32 {
|
||||
y = 0.5 * log1p_double(2.0 * y + 2.0 * y * y / (1.0 - y));
|
||||
}
|
||||
} else {
|
||||
# Divided first, so it doesn't overflow.
|
||||
y = 0.5 * log1p_double(2.0 * (y / (1.0 - y)));
|
||||
}
|
||||
return negative ? -y : y;
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
#! @file integers.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the operations of integers processors give but the operators do not:
|
||||
#! rotating; adding, subtracting, and multiplying with a check, with saturation,
|
||||
#! or wrapping where a program checked with `--checks` would stop; powers, greatest common divisors, square roots, and dividing
|
||||
#! toward negative infinity. Each works for every integer type, signed or
|
||||
#! unsigned, of any width. Counting bits and swapping bytes are `core`'s.
|
||||
|
||||
import core::intrinsics;
|
||||
module std::math;
|
||||
|
||||
|
||||
#! An integer's bits turned toward its top, those leaving the top coming back in
|
||||
#! at the bottom.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param x The integer.
|
||||
#! @param by How many places, of which only the remainder dividing by the
|
||||
#! type's width counts.
|
||||
#! @returns The integer turned.
|
||||
pub def rotate_left<T>(x: T, by: u32): T where T: integer {
|
||||
val width := size_of::<T>() * 8;
|
||||
val n := by as u64 % width;
|
||||
return x if n == 0;
|
||||
val mask := width_mask::<T>();
|
||||
val bits := x as u64 & mask;
|
||||
return (((bits << n) | (bits >> (width - n))) & mask) as T;
|
||||
}
|
||||
|
||||
#! An integer's bits turned toward its bottom, those leaving the bottom coming
|
||||
#! back in at the top.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param x The integer.
|
||||
#! @param by How many places, of which only the remainder dividing by the
|
||||
#! type's width counts.
|
||||
#! @returns The integer turned.
|
||||
pub def rotate_right<T>(x: T, by: u32): T where T: integer {
|
||||
val width := size_of::<T>() * 8;
|
||||
val n := by as u64 % width;
|
||||
return x if n == 0;
|
||||
return rotate_left(x, (width - n) as u32);
|
||||
}
|
||||
|
||||
#! `a + b`, wrapping where it overflows, as the operator does unless the
|
||||
#! program is run or built with `--checks`, which stops it there; std is never
|
||||
#! checked so, so this wraps either way.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a One integer.
|
||||
#! @param b The other.
|
||||
#! @returns The sum, its bits past the type's left out.
|
||||
pub def wrapping_add<T>(a: T, b: T): T where T: integer {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
#! `a - b`, wrapping where it overflows, as `wrapping_add` does.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a The integer subtracted from.
|
||||
#! @param b The integer subtracted.
|
||||
#! @returns The difference, its bits past the type's left out.
|
||||
pub def wrapping_sub<T>(a: T, b: T): T where T: integer {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
#! `a * b`, wrapping where it overflows, as `wrapping_add` does.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a One integer.
|
||||
#! @param b The other.
|
||||
#! @returns The product, its bits past the type's left out.
|
||||
pub def wrapping_mul<T>(a: T, b: T): T where T: integer {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
#! `a + b`, where it fits the type.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a One integer.
|
||||
#! @param b The other.
|
||||
#! @returns The sum, or `nothing` where it overflows.
|
||||
pub def checked_add<T>(a: T, b: T): maybe<T> where T: integer {
|
||||
val sum := a + b;
|
||||
if is_signed::<T>() {
|
||||
return nothing {} if (a < 0) == (b < 0) && (sum < 0) != (a < 0);
|
||||
return sum;
|
||||
}
|
||||
return nothing {} if sum < a;
|
||||
return sum;
|
||||
}
|
||||
|
||||
#! `a - b`, where it fits the type.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a The integer subtracted from.
|
||||
#! @param b The integer subtracted.
|
||||
#! @returns The difference, or `nothing` where it overflows.
|
||||
pub def checked_sub<T>(a: T, b: T): maybe<T> where T: integer {
|
||||
val difference := a - b;
|
||||
if is_signed::<T>() {
|
||||
return nothing {} if (a < 0) != (b < 0) && (difference < 0) != (a < 0);
|
||||
return difference;
|
||||
}
|
||||
return nothing {} if a < b;
|
||||
return difference;
|
||||
}
|
||||
|
||||
#! `a * b`, where it fits the type.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a One integer.
|
||||
#! @param b The other.
|
||||
#! @returns The product, or `nothing` where it overflows.
|
||||
pub def checked_mul<T>(a: T, b: T): maybe<T> where T: integer {
|
||||
return 0 as T if a == 0;
|
||||
# -1 times the most negative integer is the one product dividing back cannot
|
||||
# check, since that division overflows too.
|
||||
if is_signed::<T>() && a == ~(0 as T) {
|
||||
return nothing {} if b == least_of::<T>();
|
||||
return -b;
|
||||
}
|
||||
# The product wrapped, which divides back to `b` only where it did not wrap.
|
||||
val product := a * b;
|
||||
return nothing {} if product / a != b;
|
||||
return product;
|
||||
}
|
||||
|
||||
#! An integer raised to a power, where it fits the type.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param x The integer.
|
||||
#! @param exponent The power.
|
||||
#! @returns x^exponent, 1 where the power is 0, or `nothing` where it overflows.
|
||||
pub def checked_pow<T>(x: T, exponent: u32): maybe<T> where T: integer {
|
||||
var result := 1 as T;
|
||||
var base := x;
|
||||
var left := exponent;
|
||||
while left != 0 {
|
||||
if (left & 1) != 0 {
|
||||
result = checked_mul(result, base) else { return nothing {}; };
|
||||
}
|
||||
left >>= 1;
|
||||
if left != 0 {
|
||||
base = checked_mul(base, base) else { return nothing {}; };
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#! `a + b`, held at the type's greatest or least value where it would pass it.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a One integer.
|
||||
#! @param b The other.
|
||||
#! @returns The sum, saturated.
|
||||
pub def saturating_add<T>(a: T, b: T): T where T: integer {
|
||||
if checked_add(a, b) is some(sum) {
|
||||
return sum;
|
||||
}
|
||||
return is_signed::<T>() && b < 0 ? least_of::<T>() : greatest_of::<T>();
|
||||
}
|
||||
|
||||
#! `a - b`, held at the type's greatest or least value where it would pass it.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a The integer subtracted from.
|
||||
#! @param b The integer subtracted.
|
||||
#! @returns The difference, saturated.
|
||||
pub def saturating_sub<T>(a: T, b: T): T where T: integer {
|
||||
if checked_sub(a, b) is some(difference) {
|
||||
return difference;
|
||||
}
|
||||
return is_signed::<T>() && b < 0 ? greatest_of::<T>() : least_of::<T>();
|
||||
}
|
||||
|
||||
#! `a * b`, held at the type's greatest or least value where it would pass it.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a One integer.
|
||||
#! @param b The other.
|
||||
#! @returns The product, saturated.
|
||||
pub def saturating_mul<T>(a: T, b: T): T where T: integer {
|
||||
if checked_mul(a, b) is some(product) {
|
||||
return product;
|
||||
}
|
||||
return is_signed::<T>() && (a < 0) != (b < 0) ? least_of::<T>() : greatest_of::<T>();
|
||||
}
|
||||
|
||||
#! An integer raised to a power, wrapping where it overflows, as `*` does.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param x The integer.
|
||||
#! @param exponent The power.
|
||||
#! @returns x^exponent, as the type holds it; 1 where the power is 0.
|
||||
pub def int_pow<T>(x: T, exponent: u32): T where T: integer {
|
||||
var result := 1 as T;
|
||||
var base := x;
|
||||
var left := exponent;
|
||||
while left != 0 {
|
||||
if (left & 1) != 0 {
|
||||
result = result * base;
|
||||
}
|
||||
left >>= 1;
|
||||
if left != 0 {
|
||||
base = base * base;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#! The greatest common divisor of two integers: the greatest integer dividing
|
||||
#! both, whatever their signs.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a One integer.
|
||||
#! @param b The other.
|
||||
#! @returns The divisor, never below zero, and 0 where both are 0. Where it is
|
||||
#! the magnitude of a signed type's most negative integer, which the
|
||||
#! type cannot hold, it wraps to that integer.
|
||||
pub def gcd<T>(a: T, b: T): T where T: integer {
|
||||
return word_gcd(magnitude_of(a), magnitude_of(b)) as T;
|
||||
}
|
||||
|
||||
#! The least common multiple of two integers: the least integer above zero both
|
||||
#! divide.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a One integer.
|
||||
#! @param b The other.
|
||||
#! @returns The multiple, never below zero, and 0 where either is 0. It wraps
|
||||
#! where the type cannot hold it, as `*` does.
|
||||
pub def lcm<T>(a: T, b: T): T where T: integer {
|
||||
val x := magnitude_of(a);
|
||||
val y := magnitude_of(b);
|
||||
return 0 as T if x == 0 || y == 0;
|
||||
return ((x / word_gcd(x, y)) * y) as T;
|
||||
}
|
||||
|
||||
#! The integer square root: the greatest integer whose square is not above an
|
||||
#! integer.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param x The integer.
|
||||
#! @returns The root, and 0 below zero.
|
||||
pub def isqrt<T>(x: T): T where T: integer {
|
||||
return 0 as T if x <= 0;
|
||||
val n := x as u64 & width_mask::<T>();
|
||||
# The root of the nearest double is within one or two of the root.
|
||||
var root := square_root(n as f64) as u64;
|
||||
while root > 0 && root > n / root {
|
||||
root--;
|
||||
}
|
||||
while root + 1 <= n / (root + 1) {
|
||||
root++;
|
||||
}
|
||||
return root as T;
|
||||
}
|
||||
|
||||
#! `a / b`, rounded toward negative infinity rather than toward zero, so -7 / 2
|
||||
#! gives -4 where `/` gives -3. Of unsigned integers, it is `/`.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a The dividend.
|
||||
#! @param b The divisor, which stops the program where it is zero, as `/`
|
||||
#! does.
|
||||
#! @returns The quotient.
|
||||
pub def div_floor<T>(a: T, b: T): T where T: integer {
|
||||
val quotient := a / b;
|
||||
if is_signed::<T>() && quotient * b != a && (a < 0) != (b < 0) {
|
||||
return quotient - 1;
|
||||
}
|
||||
return quotient;
|
||||
}
|
||||
|
||||
#! What is left dividing toward negative infinity, which has the divisor's sign,
|
||||
#! so -7 and 2 give 1 where `%` gives -1. `div_floor(a, b) * b + rem_floor(a, b)`
|
||||
#! is `a`. Of unsigned integers, it is `%`.
|
||||
#! @tparam T The type of integer.
|
||||
#! @param a The dividend.
|
||||
#! @param b The divisor, which stops the program where it is zero, as `%`
|
||||
#! does.
|
||||
#! @returns The remainder.
|
||||
pub def rem_floor<T>(a: T, b: T): T where T: integer {
|
||||
val rest := a % b;
|
||||
if is_signed::<T>() && rest != 0 && (rest < 0) != (b < 0) {
|
||||
return rest + b;
|
||||
}
|
||||
return rest;
|
||||
}
|
||||
|
||||
|
||||
# What the functions above know of a type of integer.
|
||||
|
||||
# The bits of a type, low ones set, as a word.
|
||||
def width_mask<T>(): u64 where T: integer {
|
||||
if const size_of::<T>() == 8 {
|
||||
return 0xFFFFFFFFFFFFFFFFu;
|
||||
}
|
||||
return (1u << (size_of::<T>() * 8)) - 1;
|
||||
}
|
||||
|
||||
# Whether a type of integer is signed, as its every bit set is below zero.
|
||||
def is_signed<T>(): bool where T: integer {
|
||||
return ~(0 as T) < 0;
|
||||
}
|
||||
|
||||
# The greatest value of a type of integer.
|
||||
def greatest_of<T>(): T where T: integer {
|
||||
return is_signed::<T>() ? (width_mask::<T>() >> 1) as T : ~(0 as T);
|
||||
}
|
||||
|
||||
# The least value of a type of integer.
|
||||
def least_of<T>(): T where T: integer {
|
||||
return is_signed::<T>() ? (1u << (size_of::<T>() * 8 - 1)) as T : 0 as T;
|
||||
}
|
||||
|
||||
# An integer's distance from zero, as a word, which holds it whatever its type.
|
||||
def magnitude_of<T>(x: T): u64 where T: integer {
|
||||
if is_signed::<T>() && x < 0 {
|
||||
return 0 - (x as i64) as u64;
|
||||
}
|
||||
return x as u64 & width_mask::<T>();
|
||||
}
|
||||
|
||||
# Euclid's greatest common divisor, of words.
|
||||
def word_gcd(a: u64, b: u64): u64 {
|
||||
var x := a;
|
||||
var y := b;
|
||||
while y != 0 {
|
||||
val rest := x % y;
|
||||
x = y;
|
||||
y = rest;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
#! @file inverse.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the inverse trigonometric functions: the arcsine, the arccosine,
|
||||
#! and the arctangent, of one real and of a point's two coordinates.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
|
||||
#! under the MIT license. Its `asin`, `acos`, `atan`, and `atan2` are from
|
||||
#! FreeBSD's e_asin.c, e_acos.c, s_atan.c, and e_atan2.c, of Sun's fdlibm, by
|
||||
#! way of musl. e_asin.c, e_acos.c, and e_atan2.c say:
|
||||
#!
|
||||
#! Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
#!
|
||||
#! Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
#! Permission to use, copy, modify, and distribute this
|
||||
#! software is freely granted, provided that this notice
|
||||
#! is preserved.
|
||||
#!
|
||||
#! and s_atan.c 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.
|
||||
#!
|
||||
#! For `f64`, `asin`, `acos`, and `atan` are within 0.9 ulp of the true value,
|
||||
#! and `atan2` within about 1.5 ulp, since it rounds y / x before taking its
|
||||
#! arctangent. An `f32` is worked in `f64` and rounded once more.
|
||||
|
||||
import core::intrinsics;
|
||||
module std::math;
|
||||
|
||||
|
||||
#! The arcsine of a real: the angle, in radians, whose sine it is.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The sine, from -1 to 1.
|
||||
#! @returns The angle, from -pi/2 to pi/2: `-0` for `-0`, and NaN past 1
|
||||
#! either way or for NaN.
|
||||
pub def asin<T>(x: T): T where T: real {
|
||||
return asin_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The arccosine of a real: the angle, in radians, whose cosine it is.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The cosine, from -1 to 1.
|
||||
#! @returns The angle, from 0 to pi: 0 for 1, pi for -1, and NaN past 1 either
|
||||
#! way or for NaN.
|
||||
pub def acos<T>(x: T): T where T: real {
|
||||
return acos_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The arctangent of a real: the angle, in radians, whose tangent it is.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The tangent.
|
||||
#! @returns The angle, from -pi/2 to pi/2: `-0` for `-0`, pi/2 for infinity,
|
||||
#! -pi/2 for negative infinity, and NaN for NaN.
|
||||
pub def atan<T>(x: T): T where T: real {
|
||||
return atan_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The angle, in radians, from the positive x axis to the point (x, y): the
|
||||
#! arctangent of y / x, in the quadrant of the point.
|
||||
#! @tparam T The type of real.
|
||||
#! @param y The point's y coordinate.
|
||||
#! @param x The point's x coordinate.
|
||||
#! @returns The angle, from -pi to pi, of the sign of y, zeros included. A
|
||||
#! `±0` y gives `±0` where x is `0` or above, and `±pi` where x is
|
||||
#! `-0` or below. A zero x, or an infinite y, gives `±pi/2` otherwise,
|
||||
#! and an infinite x `±0` or `±pi`, or `±pi/4` or `±3pi/4` where y is
|
||||
#! infinite too. NaN where either is NaN.
|
||||
pub def atan2<T>(y: T, x: T): T where T: real {
|
||||
return atan2_double(y as f64, x as f64) as T;
|
||||
}
|
||||
|
||||
|
||||
# The data `asin` and `acos` share, from musl's asin.c and acos.c.
|
||||
|
||||
# pi/2, as a high part and a low part.
|
||||
const inverse_pio2_high: f64 = 1.5707963267948966;
|
||||
const inverse_pio2_low: f64 = 6.123233995736766e-17;
|
||||
|
||||
# A rational function near (asin(x) - x) / x^3, of x^2, on [0, 0.5]: the
|
||||
# coefficients of its numerator and of its denominator.
|
||||
const inverse_p0: f64 = 0.16666666666666666;
|
||||
const inverse_p1: f64 = -0.3255658186224009;
|
||||
const inverse_p2: f64 = 0.20121253213486293;
|
||||
const inverse_p3: f64 = -0.04005553450067941;
|
||||
const inverse_p4: f64 = 0.0007915349942898145;
|
||||
const inverse_p5: f64 = 3.479331075960212e-05;
|
||||
const inverse_q1: f64 = -2.403394911734414;
|
||||
const inverse_q2: f64 = 2.0209457602335057;
|
||||
const inverse_q3: f64 = -0.6882839716054533;
|
||||
const inverse_q4: f64 = 0.07703815055590194;
|
||||
|
||||
# The rational function, of z = x^2.
|
||||
def inverse_rational(z: f64): f64 {
|
||||
val p := z * (inverse_p0 + z * (inverse_p1 + z * (inverse_p2 + z * (inverse_p3 + z * (inverse_p4 + z * inverse_p5)))));
|
||||
val q := 1.0 + z * (inverse_q1 + z * (inverse_q2 + z * (inverse_q3 + z * inverse_q4)));
|
||||
return p / q;
|
||||
}
|
||||
|
||||
|
||||
# The arcsine, from musl's asin.c.
|
||||
def asin_double(x: f64): f64 {
|
||||
val hx := high_word(x);
|
||||
val ix := hx & 0x7FFFFFFF;
|
||||
|
||||
# At 1 and past it either way, or NaN: asin(±1) is ±pi/2.
|
||||
if ix >= 0x3FF00000 {
|
||||
return x * inverse_pio2_high if ((ix - 0x3FF00000) | low_word(x)) == 0;
|
||||
return 0.0 / (x - x);
|
||||
}
|
||||
|
||||
# Below 0.5, asin(x) is x + x^3 R(x^2), and x itself below 2^-26.
|
||||
if ix < 0x3FE00000 {
|
||||
return x if ix < 0x3E500000 && ix >= 0x00100000;
|
||||
return x + x * inverse_rational(x * x);
|
||||
}
|
||||
|
||||
# From 0.5 to 1, asin(x) is pi/2 - 2 asin(s), where s is sqrt((1 - |x|) / 2)
|
||||
# and its arcsine is s + s z R(z) for z = s^2.
|
||||
val z := (1.0 - abs(x)) * 0.5;
|
||||
val s := square_root(z);
|
||||
val r := inverse_rational(z);
|
||||
var y: f64 = 0;
|
||||
if ix >= 0x3FEF3333 {
|
||||
# Above 0.975.
|
||||
y = inverse_pio2_high - (2.0 * (s + s * r) - inverse_pio2_low);
|
||||
} else {
|
||||
# f + c is s, where f is s with its low word cleared.
|
||||
val f := with_low_word(s, 0);
|
||||
val c := (z - f * f) / (s + f);
|
||||
y = 0.5 * inverse_pio2_high - (2.0 * s * r - (inverse_pio2_low - 2.0 * c) - (0.5 * inverse_pio2_high - 2.0 * f));
|
||||
}
|
||||
return (hx >> 31) != 0 ? -y : y;
|
||||
}
|
||||
|
||||
|
||||
# The arccosine, from musl's acos.c.
|
||||
def acos_double(x: f64): f64 {
|
||||
val hx := high_word(x);
|
||||
val ix := hx & 0x7FFFFFFF;
|
||||
|
||||
# At 1 and past it either way, or NaN: acos(1) is 0 and acos(-1) is pi.
|
||||
if ix >= 0x3FF00000 {
|
||||
if ((ix - 0x3FF00000) | low_word(x)) == 0 {
|
||||
return 2.0 * inverse_pio2_high if (hx >> 31) != 0;
|
||||
return 0.0;
|
||||
}
|
||||
return 0.0 / (x - x);
|
||||
}
|
||||
|
||||
# Below 0.5, acos(x) is pi/2 - asin(x), and pi/2 itself below 2^-57.
|
||||
if ix < 0x3FE00000 {
|
||||
return inverse_pio2_high if ix <= 0x3C600000;
|
||||
return inverse_pio2_high - (x - (inverse_pio2_low - x * inverse_rational(x * x)));
|
||||
}
|
||||
|
||||
# Below -0.5, acos(x) is pi - 2 asin(s), where s is sqrt((1 + x) / 2).
|
||||
if (hx >> 31) != 0 {
|
||||
val z := (1.0 + x) * 0.5;
|
||||
val s := square_root(z);
|
||||
val w := inverse_rational(z) * s - inverse_pio2_low;
|
||||
return 2.0 * (inverse_pio2_high - (s + w));
|
||||
}
|
||||
|
||||
# Above 0.5, acos(x) is 2 asin(s), where s is sqrt((1 - x) / 2), worked as
|
||||
# df + c, where df is s with its low word cleared.
|
||||
val z := (1.0 - x) * 0.5;
|
||||
val s := square_root(z);
|
||||
val df := with_low_word(s, 0);
|
||||
val c := (z - df * df) / (s + df);
|
||||
val w := inverse_rational(z) * s + c;
|
||||
return 2.0 * (df + w);
|
||||
}
|
||||
|
||||
|
||||
# The data of `atan`, from musl's atan.c.
|
||||
|
||||
# atan(0.5), atan(1), atan(1.5), and atan(infinity), as high parts and low
|
||||
# parts.
|
||||
const atan_high: f64[4] = [
|
||||
0.4636476090008061, 0.7853981633974483, 0.982793723247329, 1.5707963267948966,
|
||||
];
|
||||
const atan_low: f64[4] = [
|
||||
2.2698777452961687e-17, 3.061616997868383e-17, 1.3903311031230998e-17,
|
||||
6.123233995736766e-17,
|
||||
];
|
||||
|
||||
# The polynomial of (x - atan(x)) / x^3, of x^2, on [0, 7/16].
|
||||
const atan_t: f64[11] = [
|
||||
0.3333333333333293, -0.19999999999876483, 0.14285714272503466,
|
||||
-0.11111110405462356, 0.09090887133436507, -0.0769187620504483,
|
||||
0.06661073137387531, -0.058335701337905735, 0.049768779946159324,
|
||||
-0.036531572744216916, 0.016285820115365782,
|
||||
];
|
||||
|
||||
# The arctangent, from musl's atan.c.
|
||||
def atan_double(x: f64): f64 {
|
||||
var ix := high_word(x);
|
||||
val sign := ix >> 31;
|
||||
ix &= 0x7FFFFFFF;
|
||||
|
||||
# From 2^66 up, atan(x) is ±pi/2, or NaN.
|
||||
if ix >= 0x44100000 {
|
||||
return x if x != x;
|
||||
val z := atan_high[3];
|
||||
return sign != 0 ? -z : z;
|
||||
}
|
||||
|
||||
# |x| is reduced to t, near one of 0, 0.5, 1, 1.5, and infinity, whose
|
||||
# arctangent is id's of atan_high and atan_low.
|
||||
var t := x;
|
||||
var id: i32 = -1;
|
||||
if ix < 0x3FDC0000 {
|
||||
# Below 7/16, and x itself below 2^-27.
|
||||
return x if ix < 0x3E400000;
|
||||
} else {
|
||||
t = abs(x);
|
||||
if ix < 0x3FF30000 {
|
||||
if ix < 0x3FE60000 {
|
||||
# From 7/16 to 11/16.
|
||||
id = 0;
|
||||
t = (2.0 * t - 1.0) / (2.0 + t);
|
||||
} else {
|
||||
# From 11/16 to 19/16.
|
||||
id = 1;
|
||||
t = (t - 1.0) / (t + 1.0);
|
||||
}
|
||||
} else {
|
||||
if ix < 0x40038000 {
|
||||
# From 19/16 to 39/16.
|
||||
id = 2;
|
||||
t = (t - 1.5) / (1.0 + 1.5 * t);
|
||||
} else {
|
||||
# From 39/16 to 2^66.
|
||||
id = 3;
|
||||
t = -1.0 / t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# The polynomial, of its odd and even terms apart.
|
||||
val z := t * t;
|
||||
val w := z * z;
|
||||
val s1 := z * (atan_t[0] + w * (atan_t[2] + w * (atan_t[4] + w * (atan_t[6] + w * (atan_t[8] + w * atan_t[10])))));
|
||||
val s2 := w * (atan_t[1] + w * (atan_t[3] + w * (atan_t[5] + w * (atan_t[7] + w * atan_t[9]))));
|
||||
return t - t * (s1 + s2) if id < 0;
|
||||
val k := id as u64;
|
||||
val y := atan_high[k] - (t * (s1 + s2) - atan_low[k] - t);
|
||||
return sign != 0 ? -y : y;
|
||||
}
|
||||
|
||||
|
||||
# The data of `atan2`, from musl's atan2.c: pi, as a high part and a low part.
|
||||
const atan_pi: f64 = 3.141592653589793;
|
||||
const atan_pi_low: f64 = 1.2246467991473532e-16;
|
||||
|
||||
# The arctangent of y / x, in the point's quadrant, from musl's atan2.c.
|
||||
def atan2_double(y: f64, x: f64): f64 {
|
||||
return x + y if x != x || y != y;
|
||||
var ix := high_word(x);
|
||||
val lx := low_word(x);
|
||||
var iy := high_word(y);
|
||||
val ly := low_word(y);
|
||||
# atan2(y, 1) is atan(y).
|
||||
return atan_double(y) if ((ix - 0x3FF00000) | lx) == 0;
|
||||
# Twice x's sign and y's.
|
||||
val m := ((iy >> 31) & 1) | ((ix >> 30) & 2);
|
||||
ix &= 0x7FFFFFFF;
|
||||
iy &= 0x7FFFFFFF;
|
||||
|
||||
# y is zero: ±0 where x's sign is clear, and ±pi where it is set.
|
||||
if (iy | ly) == 0 {
|
||||
return y if m < 2;
|
||||
return m == 2 ? atan_pi : -atan_pi;
|
||||
}
|
||||
# x is zero: ±pi/2.
|
||||
if (ix | lx) == 0 {
|
||||
return (m & 1) != 0 ? -atan_pi / 2.0 : atan_pi / 2.0;
|
||||
}
|
||||
# x is infinite.
|
||||
if ix == 0x7FF00000 {
|
||||
if iy == 0x7FF00000 {
|
||||
return atan_pi / 4.0 if m == 0;
|
||||
return -atan_pi / 4.0 if m == 1;
|
||||
return 3.0 * atan_pi / 4.0 if m == 2;
|
||||
return -3.0 * atan_pi / 4.0;
|
||||
}
|
||||
return 0.0 if m == 0;
|
||||
return -0.0 if m == 1;
|
||||
return atan_pi if m == 2;
|
||||
return -atan_pi;
|
||||
}
|
||||
# |y / x| is above 2^64, or y is infinite: ±pi/2.
|
||||
if ix + 0x04000000 < iy || iy == 0x7FF00000 {
|
||||
return (m & 1) != 0 ? -atan_pi / 2.0 : atan_pi / 2.0;
|
||||
}
|
||||
|
||||
# atan(|y / x|), taken as 0 where it is below 2^-64 and x is negative, so it
|
||||
# does not underflow.
|
||||
var z: f64 = 0;
|
||||
if (m & 2) == 0 || iy + 0x04000000 >= ix {
|
||||
z = atan_double(abs(y / x));
|
||||
}
|
||||
return z if m == 0;
|
||||
return -z if m == 1;
|
||||
return atan_pi - (z - atan_pi_low) if m == 2;
|
||||
return (z - atan_pi_low) - atan_pi;
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
#! @file lanes.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the functions of reals as methods of `simd`, lane by lane: square
|
||||
#! roots, rounding to integers, and fused multiplying and adding. Each gives in
|
||||
#! every lane what the function of the same name gives of one real, worked with
|
||||
#! the processor's vector instructions where it has them, as `simd`'s own
|
||||
#! methods are.
|
||||
|
||||
import core::intrinsics;
|
||||
module std::math;
|
||||
|
||||
|
||||
#! Each real lane's square root, correctly rounded, as `sqrt` gives it.
|
||||
#! @param this The vector.
|
||||
#! @returns The vector.
|
||||
pub def simd<L[N]>::sqrt(&this): simd<L[N]> {
|
||||
var made: simd<L[N]>;
|
||||
if const L is real {
|
||||
if !lanes_done::<L, lanes_sqrt, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
|
||||
var r: L[N] = this.lanes();
|
||||
for i in 0..N {
|
||||
r[i] = sqrt(r[i]);
|
||||
}
|
||||
made = simd::<L[N]>::of(r);
|
||||
}
|
||||
} else {
|
||||
compile_error("`sqrt` takes real lanes, and these are integers.");
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Each real lane's greatest integer not above it, as `floor` gives it.
|
||||
#! @param this The vector.
|
||||
#! @returns The vector.
|
||||
pub def simd<L[N]>::floor(&this): simd<L[N]> {
|
||||
var made: simd<L[N]>;
|
||||
if const L is real {
|
||||
if !lanes_done::<L, lanes_floor, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
|
||||
var r: L[N] = this.lanes();
|
||||
for i in 0..N {
|
||||
r[i] = floor(r[i]);
|
||||
}
|
||||
made = simd::<L[N]>::of(r);
|
||||
}
|
||||
} else {
|
||||
compile_error("`floor` takes real lanes, and these are integers.");
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Each real lane's least integer not below it, as `ceil` gives it.
|
||||
#! @param this The vector.
|
||||
#! @returns The vector.
|
||||
pub def simd<L[N]>::ceil(&this): simd<L[N]> {
|
||||
var made: simd<L[N]>;
|
||||
if const L is real {
|
||||
if !lanes_done::<L, lanes_ceil, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
|
||||
var r: L[N] = this.lanes();
|
||||
for i in 0..N {
|
||||
r[i] = ceil(r[i]);
|
||||
}
|
||||
made = simd::<L[N]>::of(r);
|
||||
}
|
||||
} else {
|
||||
compile_error("`ceil` takes real lanes, and these are integers.");
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Each real lane's integer part, toward zero, as `trunc` gives it.
|
||||
#! @param this The vector.
|
||||
#! @returns The vector.
|
||||
pub def simd<L[N]>::trunc(&this): simd<L[N]> {
|
||||
var made: simd<L[N]>;
|
||||
if const L is real {
|
||||
if !lanes_done::<L, lanes_trunc, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
|
||||
var r: L[N] = this.lanes();
|
||||
for i in 0..N {
|
||||
r[i] = trunc(r[i]);
|
||||
}
|
||||
made = simd::<L[N]>::of(r);
|
||||
}
|
||||
} else {
|
||||
compile_error("`trunc` takes real lanes, and these are integers.");
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Each real lane's nearest integer, halves away from zero, as `round` gives
|
||||
#! it: truncated, then moved one away from zero where a half or more was left,
|
||||
#! with SSE4.1's instructions.
|
||||
#! @param this The vector.
|
||||
#! @returns The vector.
|
||||
pub def simd<L[N]>::round(&this): simd<L[N]> {
|
||||
var made: simd<L[N]>;
|
||||
if const L is real {
|
||||
if !lanes_done::<L, lanes_round, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
|
||||
var r: L[N] = this.lanes();
|
||||
for i in 0..N {
|
||||
r[i] = round(r[i]);
|
||||
}
|
||||
made = simd::<L[N]>::of(r);
|
||||
}
|
||||
} else {
|
||||
compile_error("`round` takes real lanes, and these are integers.");
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Each real lane's nearest integer, halves to the even one, as `round_even`
|
||||
#! gives it.
|
||||
#! @param this The vector.
|
||||
#! @returns The vector.
|
||||
pub def simd<L[N]>::round_even(&this): simd<L[N]> {
|
||||
var made: simd<L[N]>;
|
||||
if const L is real {
|
||||
if !lanes_done::<L, lanes_round_even, N>((&mut made) as *mut u8, this as *u8, null, null, 16, 16) {
|
||||
var r: L[N] = this.lanes();
|
||||
for i in 0..N {
|
||||
r[i] = round_even(r[i]);
|
||||
}
|
||||
made = simd::<L[N]>::of(r);
|
||||
}
|
||||
} else {
|
||||
compile_error("`round_even` takes real lanes, and these are integers.");
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! `a * b + c`, lane by lane: of reals rounded once, as `fma` gives it, and of
|
||||
#! integers wrapping.
|
||||
#! @param this The first factors.
|
||||
#! @param times The second.
|
||||
#! @param plus What the products are added to.
|
||||
#! @returns The vector.
|
||||
pub def simd<L[N]>::fma(&this, times: &simd<L[N]>, plus: &simd<L[N]>): simd<L[N]> {
|
||||
var made: simd<L[N]>;
|
||||
if !lanes_done::<L, lanes_fma, N>((&mut made) as *mut u8, this as *u8, times as *u8, plus as *u8, 16, 16) {
|
||||
val a: L[N] = this.lanes();
|
||||
val b: L[N] = times.lanes();
|
||||
var r: L[N] = plus.lanes();
|
||||
for i in 0..N {
|
||||
if const L is real {
|
||||
r[i] = fma(a[i], b[i], r[i]);
|
||||
} else {
|
||||
r[i] = a[i] * b[i] + r[i];
|
||||
}
|
||||
}
|
||||
made = simd::<L[N]>::of(r);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# Works an operation out with the vector instructions of the processor running
|
||||
# the program, where it has them, as `simd`'s own methods do: the whole vector
|
||||
# at once, or 32 bytes a half at a time where it has them only for 16. Where it
|
||||
# gives false, the caller works the lanes one at a time.
|
||||
def lanes_done<L, const OP: u32, const N: u64>(into: *mut u8, a: *u8, b: *u8, c: *u8, a_half: u64, b_half: u64): bool {
|
||||
if lanes_have::<L, OP, N>() {
|
||||
lanes_apply::<L, OP, N>(into, a, b, c);
|
||||
return true;
|
||||
}
|
||||
if const N * size_of::<L>() == 32 {
|
||||
if lanes_have::<L, OP, N / 2>() {
|
||||
lanes_apply::<L, OP, N / 2>(into, a, b, c);
|
||||
lanes_apply::<L, OP, N / 2>(into + 16, a + a_half, b == null ? b : b + b_half, c == null ? c : c + 16);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# The operations on lanes of these functions, as the compiler numbers them.
|
||||
const lanes_sqrt: u32 = 13;
|
||||
const lanes_floor: u32 = 14;
|
||||
const lanes_ceil: u32 = 15;
|
||||
const lanes_trunc: u32 = 16;
|
||||
const lanes_round_even: u32 = 17;
|
||||
const lanes_fma: u32 = 25;
|
||||
const lanes_round: u32 = 36;
|
||||
+289
@@ -0,0 +1,289 @@
|
||||
#! @file log.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the natural logarithm.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, whose `log` is from Arm's optimized-routines, which
|
||||
#! says:
|
||||
#!
|
||||
#! Copyright (c) 2018, Arm Limited.
|
||||
#! SPDX-License-Identifier: MIT
|
||||
#!
|
||||
#! It is within 0.52 ulp of the true value for `f64`. An `f32` is worked in
|
||||
#! `f64` and rounded once more.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
#! The natural logarithm of a real, the power e is raised to to give it.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns ln x: negative infinity for zero, and NaN below zero.
|
||||
pub def log<T>(x: T): T where T: real {
|
||||
return log_double(x as f64) as T;
|
||||
}
|
||||
|
||||
|
||||
# The data of `log`, from musl's log_data.c.
|
||||
|
||||
# ln 2, as a high part and a low part.
|
||||
const log_ln2_high: f64 = 0.6931471805598903;
|
||||
const log_ln2_low: f64 = 5.497923018708371e-14;
|
||||
|
||||
# The polynomial of ln(1 + r) for r within 2^-4 of 0, from its second
|
||||
# coefficient.
|
||||
const log_b: f64[11] = [
|
||||
-0.5, 0.3333333333333352, -0.24999999999998432,
|
||||
0.19999999999320328, -0.16666666669929706, 0.14285715076560868,
|
||||
-0.12499997863982555, 0.11110712032936046, -0.10000486757818193,
|
||||
0.09181994006195467, -0.08328363062289341,
|
||||
];
|
||||
|
||||
# The polynomial of ln(1 + r) - r for r within 1/2N of 0, from its second
|
||||
# coefficient.
|
||||
const log_a: f64[5] = [
|
||||
-0.5000000000000001, 0.33333333331825593, -0.2499999999622955,
|
||||
0.20000304511814496, -0.16667054827627667,
|
||||
];
|
||||
|
||||
# For each of N subintervals of [0.6875, 1.375), 1/c and ln c, for a c near its
|
||||
# middle.
|
||||
const log_table: f64[256] = [
|
||||
1.4504249240398293, -0.3718565645633589, 1.442253508327276,
|
||||
-0.36620682668944937, 1.4341736174350004, -0.3605888069791945,
|
||||
1.426183816329995, -0.3550022171419869, 1.4182825527052965,
|
||||
-0.34944666968829097, 1.4104682921759335, -0.3439217713603284,
|
||||
1.4027396147468003, -0.3384271921261188, 1.3950954438932313,
|
||||
-0.332962831494342, 1.3875338232485754, -0.32752794345742586,
|
||||
1.3800539211058593, -0.32212257167088865, 1.3726542695419708,
|
||||
-0.3167462884799761, 1.3653332798446802, -0.3113985598928366,
|
||||
1.358090204587874, -0.3060794515165526, 1.3509234892132138,
|
||||
-0.300788424667644, 1.3438320840699889, -0.2955252968476998,
|
||||
1.3368146974742003, -0.29028969275850613, 1.329870114677736,
|
||||
-0.2850812793277555, 1.322997339161106, -0.27989987391470095,
|
||||
1.316195352741367, -0.27474526621870154, 1.3094628125672239,
|
||||
-0.2696169863701243, 1.3027990455471041, -0.26451506180308115,
|
||||
1.2962024229438942, -0.2594387762767383, 1.2896726275815547,
|
||||
-0.2543884090981692, 1.2832080305745537, -0.24936321635129843,
|
||||
1.276807885983376, -0.24436312405975968, 1.2704714060687552,
|
||||
-0.23938801747897287, 1.2641976054949482, -0.23443761696705678,
|
||||
1.257985357514882, -0.22951151871518505, 1.2518337750655457,
|
||||
-0.2246094963439873, 1.2457421919097305, -0.21973149037705753,
|
||||
1.2397094966625508, -0.2148770752847895, 1.2337348463589233,
|
||||
-0.210046029103637, 1.2278176973028803, -0.20523836373934046,
|
||||
1.2219570190618474, -0.20045368751368642, 1.2161519732977757,
|
||||
-0.1956917537758045, 1.2104018095009725, -0.19095237845203883,
|
||||
1.204705805718973, -0.18623539250290833, 1.1990631185441964,
|
||||
-0.18154051731551135, 1.1934733004462308, -0.1768677957431919,
|
||||
1.1879350812847385, -0.17221657406412305, 1.1824481322833125,
|
||||
-0.16758697765942543, 1.1770114976921955, -0.16297859687290384,
|
||||
1.1716248121809465, -0.15839151377804228, 1.1662869231674715,
|
||||
-0.15382513241456763, 1.1609977486762766, -0.14927976358922024,
|
||||
1.1557563220795803, -0.14475495398119165, 1.1505619105480347,
|
||||
-0.14025044090817573, 1.1454138888505974, -0.1357660466685502,
|
||||
1.140311877374656, -0.1313018016355727, 1.1352550225747513,
|
||||
-0.12685731518763532, 1.1302429094831266, -0.1224325737671279,
|
||||
1.1252747693068048, -0.11802724521862729, 1.1203501571039876,
|
||||
-0.11364127671663482, 1.1154683327680124, -0.10927434611278386,
|
||||
1.1106291463292157, -0.10492665324943573, 1.1058315813301596,
|
||||
-0.10059761422644442, 1.1010752177696026, -0.09628717309055901,
|
||||
1.0963597137952512, -0.09199534069557558, 1.0916844827550398,
|
||||
-0.08772190036688698, 1.0870487291277784, -0.08346643613867855,
|
||||
1.082452357388312, -0.07922916827544668, 1.0778948225025884,
|
||||
-0.0750099004750382, 1.0733751731601076, -0.07080805133352897,
|
||||
1.068893585073351, -0.06662408085151128, 1.0644491706655506,
|
||||
-0.06245745471915143, 1.0600414846328305, -0.0583080438042316,
|
||||
1.0556701316181605, -0.05417576112313327, 1.051334750556926,
|
||||
-0.050060547896805474, 1.0470347288442157, -0.045962101199052086,
|
||||
1.0427699229652954, -0.04188056008865715, 1.0385395013738175,
|
||||
-0.03781540056183985, 1.034343418940345, -0.03376684757915882,
|
||||
1.0301811073173315, -0.029734619131772888, 1.026052043621297,
|
||||
-0.025718470239212365, 1.0219561082336197, -0.021718543925430822,
|
||||
1.0178926505784922, -0.01773446126981071, 1.0138614436244586,
|
||||
-0.013766252464051831, 1.0098620186501341, -0.009813706322574944,
|
||||
1.0058938559734134, -0.005876555150052809, 1.00195696235014,
|
||||
-0.0019550499938532084, 0.9961089923088509, 0.0038985973556009412,
|
||||
0.9884170338185201, 0.011650571286395461, 0.9808429191005297,
|
||||
0.019342955478919066, 0.9733840169987446, 0.0269766014846482,
|
||||
0.9660377568876556, 0.034552359728422744, 0.9588014945307369,
|
||||
0.04207121767183253, 0.9516728569073111, 0.049533940950141186,
|
||||
0.9446494635965822, 0.056941358295944156, 0.9377288993026223,
|
||||
0.06429439168346107, 0.9309091073790681, 0.0715936354946507,
|
||||
0.924187681612722, 0.07884010933776153, 0.9175626765599192,
|
||||
0.08603438905970506, 0.9110320403624034, 0.09317721180013905,
|
||||
0.9045935839762024, 0.10026951462748457, 0.8982456375922825,
|
||||
0.10731170956330516, 0.8919860966782501, 0.11430473320717738,
|
||||
0.8858131121185129, 0.12124928503033061, 0.879725075760676,
|
||||
0.12814583422959913, 0.8737201372634685, 0.1349951636851756,
|
||||
0.8677966405782273, 0.1417978768189414, 0.8619528050060739,
|
||||
0.14855476039031146, 0.8561872354420692, 0.1552661937658968,
|
||||
0.8504983927816893, 0.16193275688146969, 0.8448844572790304,
|
||||
0.16855539792220497, 0.8393442741575965, 0.1751343179947753,
|
||||
0.8338762249349438, 0.1816702989864325, 0.8284789320557778,
|
||||
0.18816387146023317, 0.8231510800065832, 0.1946155228479256,
|
||||
0.8178913903778707, 0.20102572579389744, 0.8126984007245374,
|
||||
0.2073952090795501, 0.8075710029460227, 0.21372429840596396,
|
||||
0.8025078881160415, 0.2200135945981856, 0.7975077379364331,
|
||||
0.22626374162859975, 0.792569604966373, 0.23247494747693054,
|
||||
0.7876923641254114, 0.23864766620658884, 0.7828746724940998,
|
||||
0.24478265647405806, 0.7781155388790811, 0.25088025827324145,
|
||||
0.7734139557869777, 0.2569408552510595, 0.7687687179914933,
|
||||
0.26296511155101143, 0.7641790698041854, 0.2689531327189343,
|
||||
0.7596438763692399, 0.27490553924610595, 0.7551621951078668,
|
||||
0.2808227248478943, 0.7507331780216866, 0.286704979267256,
|
||||
0.7463557196361751, 0.29255295645509705, 0.7420289364869653,
|
||||
0.2983670386142876, 0.7377521537065876, 0.30414734587282055,
|
||||
0.7335242966002608, 0.30989455774829366, 0.729344777457841,
|
||||
0.31560871301871884,
|
||||
];
|
||||
|
||||
# The same c, as a double and the rest.
|
||||
const log_table2: f64[256] = [
|
||||
0.6894531274426304, 2.60290652810535e-17, 0.6933593811533166,
|
||||
5.1607448519931415e-17, 0.6972656502972674, 5.290672414887869e-17,
|
||||
0.7011718886092148, 7.801030634806013e-18, 0.7050781228976939,
|
||||
-2.471355337688684e-17, 0.7089843887644558, -4.6413389636862956e-17,
|
||||
0.7128906815542553, 3.3387408065940366e-17, 0.7167968359278303,
|
||||
-2.6454587338050098e-17, 0.7207031520563163, 2.4284096334522285e-17,
|
||||
0.7246093683054673, 4.85782435031982e-17, 0.7285155644718035,
|
||||
-1.3014776598894487e-17, 0.7324219036935506, -1.0402317671225898e-17,
|
||||
0.73632811474659, 3.078291427467431e-17, 0.7402343715130797,
|
||||
-1.4304998976623033e-17, 0.7441405900738403, -2.8625955785571303e-17,
|
||||
0.7480468324364, 5.1609093535635645e-17, 0.7519531335902885,
|
||||
4.423423949536319e-17, 0.7558594189116706, -4.683016762099508e-17,
|
||||
0.7597656365502305, 4.466742007514772e-17, 0.7636719350887737,
|
||||
-4.8134498775190266e-17, 0.767578087670501, -4.3371015350542286e-17,
|
||||
0.7714844396979536, -2.515644472251707e-17, 0.7753905747966752,
|
||||
-4.163636184961329e-17, 0.7792968686084766, 3.3837161384165165e-17,
|
||||
0.783203182701066, 4.379206565056758e-17, 0.7871094109030913,
|
||||
2.7322930976146396e-17, 0.7910155783031153, -5.463661225364046e-17,
|
||||
0.7949218121071572, -1.4320502634189868e-17, 0.7988281031542229,
|
||||
-4.0766731402809024e-17, 0.8027343109146796, -4.0338264352540086e-17,
|
||||
0.8066405901480322, 1.7241428071650815e-18, 0.8105469363625933,
|
||||
-4.5534218930924523e-17, 0.8144531571720115, 1.821216920589142e-17,
|
||||
0.8183593894061396, 4.9864288025894273e-17, 0.8222656559018297,
|
||||
4.335990416726689e-17, 0.8261719308006343, -5.422001471661127e-17,
|
||||
0.830078177803083, -5.333603728929035e-17, 0.8339844538076674,
|
||||
-1.1270648442682386e-17, 0.8378905498984414, -3.165126734800398e-17,
|
||||
0.8417968420618669, 1.1286283062565865e-17, 0.8457030568173808,
|
||||
5.643595964448442e-18, 0.8496093725173733, -2.1687834632368187e-17,
|
||||
0.8535155534462677, -2.2548234217914977e-17, 0.8574219432077146,
|
||||
4.683260556353139e-17, 0.8613281129443706, 2.5148296884122795e-17,
|
||||
0.8652342893532053, -8.672187570949467e-18, 0.8691405397938828,
|
||||
-1.2576268285559751e-17, 0.8730468608194392, -2.9930851265148e-17,
|
||||
0.8769530685783117, 4.814801507681644e-17, 0.8808593488818101,
|
||||
-9.10549533611022e-18, 0.8847655593409667, -3.512414651620766e-17,
|
||||
0.888671840226208, 3.555261815374023e-17, 0.8925780870017613,
|
||||
1.692380503540994e-17, 0.8964844367374554, -1.0837881791715702e-17,
|
||||
0.9003905608862683, 5.2900545724138363e-17, 0.9042968358682077,
|
||||
5.3330721088784517e-17, 0.9082031671057441, 1.8640040753232978e-17,
|
||||
0.9121093993305497, 4.467972832054075e-17, 0.9160155849026457,
|
||||
-5.216213419585033e-18, 0.9199219622862499, -2.7324731032846873e-17,
|
||||
0.9238281880717143, -1.7769263393434193e-17, 0.9277343012727929,
|
||||
4.943363080817441e-17, 0.9316407021562788, 4.164434042024055e-17,
|
||||
0.935546825207466, 1.3011820859795741e-17, 0.9394530312562943,
|
||||
-2.7332907838481398e-17, 0.9433593066844669, 2.342133170803292e-17,
|
||||
0.9472655994038329, 1.6478717480487438e-17, 0.9511718313032721,
|
||||
-5.19608290131499e-18, 0.9550781578218178, -3.5565573220579e-17,
|
||||
0.9589843147339043, 2.0816772146458738e-17, 0.9628906735633685,
|
||||
-3.471962826146841e-18, 0.9667968893972092, -3.68646834472642e-17,
|
||||
0.9707031054025779, -3.4674108076868834e-18, 0.9746094325494931,
|
||||
4.598152624774392e-17, 0.9785156054582722, 4.553145752392568e-17,
|
||||
0.9824218687812282, -7.811259169418978e-18, 0.9863280690752917,
|
||||
2.0375615178047467e-17, 0.9902342909546034, -1.344597043422914e-17,
|
||||
0.9941406780264008, 1.51853723695054e-17, 0.9980468598715558,
|
||||
-1.301926039598001e-17, 1.0039062067717412, -4.596120641744973e-17,
|
||||
1.0117187035281372, -8.152405002665349e-17, 1.0195312424919558,
|
||||
8.239927014757811e-17, 1.0273437641634193, 3.122851005327045e-17,
|
||||
1.0351562274561221, -1.3010212611128995e-17, 1.0429687539123276,
|
||||
-5.984116688633458e-17, 1.0507812561238108, 2.860923854822335e-17,
|
||||
1.0585937308350133, -1.9962944508904598e-17, 1.0664062936992642,
|
||||
5.291469130309099e-17, 1.0742187309945372, 1.0321575006732523e-16,
|
||||
1.082031301537134, 9.021194373419119e-17, 1.0898438063644336,
|
||||
5.202798992357953e-17, 1.0976562356711468, 5.898318482500785e-17,
|
||||
1.1054688179463226, 8.846377690653252e-17, 1.113281220803328,
|
||||
-9.529455965212574e-18, 1.1210937073167315, 6.765080709377787e-17,
|
||||
1.1289062967338532, 1.0929327370933015e-16, 1.1367187631150848,
|
||||
6.938570551230516e-17, 1.1445312490244826, 1.908841015045148e-17,
|
||||
1.1523437096203593, 2.7744504138979832e-17, 1.1601563266482477,
|
||||
-1.5621964097491672e-17, 1.1679688257483505, -6.4191661495975e-17,
|
||||
1.1757811754697645, -4.336465165059268e-17, 1.183593793665613,
|
||||
-3.8159292551356396e-17, 1.1914062331617676, -2.94933042768015e-17,
|
||||
1.1992187450577771, -6.244142916351348e-17, 1.2070312971249755,
|
||||
7.458644622627413e-17, 1.2148438169965134, 2.7760519266147807e-17,
|
||||
1.222656225220801, -7.025106921079665e-17, 1.2304687681290862,
|
||||
3.7292622364618835e-17, 1.2382812116234925, 2.2558949940652082e-17,
|
||||
1.2460936706149877, 8.499368944400665e-17, 1.2539063289686938,
|
||||
1.039515474644361e-17, 1.2617188367227732, 3.297262223275499e-17,
|
||||
1.2695311590462317, 4.509883253594155e-17, 1.277343660657941,
|
||||
-7.546987371769545e-17, 1.2851561882963496, 9.454295598818184e-17,
|
||||
1.2929686521915196, -7.28545609119749e-17, 1.3007813359167737,
|
||||
-8.934357651462802e-17, 1.3085938093753886, 8.15257059458102e-17,
|
||||
1.3164063202609564, 6.946341460765881e-18, 1.3242188320313901,
|
||||
-7.892731380721676e-17, 1.3320311786874468, -4.3384657500713545e-17,
|
||||
1.3398436880573092, 9.627577954598792e-17, 1.3476563390295309,
|
||||
4.2492400590204517e-17, 1.3554687641043626, -1.0843419551446047e-16,
|
||||
1.3632813590971713, -8.50172782472025e-17, 1.3710936595522603,
|
||||
-7.110627206162922e-17,
|
||||
];
|
||||
|
||||
|
||||
# ln x, from musl's log.c.
|
||||
def log_double(x: f64): f64 {
|
||||
var ix := bits_of(x);
|
||||
val top := (ix >> 48) as u32;
|
||||
|
||||
# Near 1, within -2^-4 and 1.03515625 * 2^-4, where the polynomial alone is
|
||||
# near enough.
|
||||
if ix - 0x3FEE000000000000 < 0x3FF1090000000000 - 0x3FEE000000000000 {
|
||||
return 0.0 if ix == 0x3FF0000000000000;
|
||||
val r := x - 1.0;
|
||||
val r2 := r * r;
|
||||
val r3 := r * r2;
|
||||
var y := r3 * (log_b[1] + r * log_b[2] + r2 * log_b[3]
|
||||
+ r3 * (log_b[4] + r * log_b[5] + r2 * log_b[6]
|
||||
+ r3 * (log_b[7] + r * log_b[8] + r2 * log_b[9] + r3 * log_b[10])));
|
||||
var w := r * 134217728.0;
|
||||
val rhi := r + w - w;
|
||||
val rlo := r - rhi;
|
||||
w = rhi * rhi * log_b[0];
|
||||
val hi := r + w;
|
||||
var lo := r - hi + w;
|
||||
lo += log_b[0] * rlo * (rhi + r);
|
||||
y += lo;
|
||||
y += hi;
|
||||
return y;
|
||||
}
|
||||
if top - 0x0010 >= 0x7FF0 - 0x0010 {
|
||||
# Below 2^-1022, infinite, or NaN.
|
||||
return divided_by_zero(1) if ix * 2 == 0;
|
||||
return x if ix == 0x7FF0000000000000;
|
||||
return invalid(x) if (top & 0x8000) != 0 || (top & 0x7FF0) == 0x7FF0;
|
||||
# A subnormal, made normal.
|
||||
ix = bits_of(x * 4503599627370496.0);
|
||||
ix -= 52u << 52;
|
||||
}
|
||||
|
||||
# x is 2^k z, where z is in [0.6875, 1.375) and exact, and falls in the ith
|
||||
# subinterval, whose c is near its middle.
|
||||
val tmp := ix - 0x3FE6000000000000;
|
||||
val i := (tmp >> (52 - 7)) % 128;
|
||||
val k := (tmp as i64) >> 52;
|
||||
val iz := ix - (tmp & (0xFFFu << 52));
|
||||
val invc := log_table[2 * i];
|
||||
val logc := log_table[2 * i + 1];
|
||||
val z := real_of_bits(iz);
|
||||
|
||||
# ln x is ln(1 + (z/c - 1)) + ln c + k ln 2, where r is about z/c - 1.
|
||||
val r := (z - log_table2[2 * i] - log_table2[2 * i + 1]) * invc;
|
||||
val kd := k as f64;
|
||||
# hi + lo is r + ln c + k ln 2.
|
||||
val w := kd * log_ln2_high + logc;
|
||||
val hi := w + r;
|
||||
val lo := w - hi + r + kd * log_ln2_low;
|
||||
# ln x is lo + (ln(1 + r) - r) + hi.
|
||||
val r2 := r * r;
|
||||
return lo + r2 * log_a[0] + r * r2 * (log_a[1] + r * log_a[2] + r2 * (log_a[3] + r * log_a[4])) + hi;
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
#! @file log10.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the base 10 logarithm.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
|
||||
#! under the MIT license. Its `log10` is from FreeBSD's e_log10.c, which says:
|
||||
#!
|
||||
#! Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
#!
|
||||
#! Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
#! Permission to use, copy, modify, and distribute this
|
||||
#! software is freely granted, provided that this notice
|
||||
#! is preserved.
|
||||
#!
|
||||
#! It is within 1 ulp of the true value for `f64`. An `f32` is worked in `f64`
|
||||
#! and rounded once more.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
#! The base 10 logarithm of a real, the power 10 is raised to to give it.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns log10 x: 0 for 1, negative infinity for zero, infinity for
|
||||
#! infinity, and NaN below zero.
|
||||
pub def log10<T>(x: T): T where T: real {
|
||||
return log10_double(x as f64) as T;
|
||||
}
|
||||
|
||||
|
||||
# The data of `log10`, from musl's log10.c.
|
||||
|
||||
# 1 / ln 10, as a high part of 33 bits and a low part.
|
||||
const log10_inverse_ln10_high: f64 = 0.4342944818781689;
|
||||
const log10_inverse_ln10_low: f64 = 2.5082946711645275e-11;
|
||||
|
||||
# log10 2, as a high part and a low part.
|
||||
const log10_log10_2_high: f64 = 0.30102999566361177;
|
||||
const log10_log10_2_low: f64 = 3.694239077158931e-13;
|
||||
|
||||
# The polynomial of (ln(1 + f) - f + f^2 / 2) / s, in s^2, where s is
|
||||
# f / (2 + f): its coefficients from s^2's.
|
||||
const log10_lg1: f64 = 0.6666666666666735;
|
||||
const log10_lg2: f64 = 0.3999999999940942;
|
||||
const log10_lg3: f64 = 0.2857142874366239;
|
||||
const log10_lg4: f64 = 0.22222198432149784;
|
||||
const log10_lg5: f64 = 0.1818357216161805;
|
||||
const log10_lg6: f64 = 0.15313837699209373;
|
||||
const log10_lg7: f64 = 0.14798198605116586;
|
||||
|
||||
|
||||
# log10 x, from musl's log10.c.
|
||||
def log10_double(x: f64): f64 {
|
||||
var v := x;
|
||||
var hx := high_word(v);
|
||||
var k: i32 = 0;
|
||||
if hx < 0x00100000 || (hx >> 31) != 0 {
|
||||
# Zero, below zero, or subnormal.
|
||||
return -1.0 / (v * v) if (bits_of(v) << 1) == 0;
|
||||
return (v - v) / 0.0 if (hx >> 31) != 0;
|
||||
# A subnormal, scaled up by 2^54.
|
||||
k -= 54;
|
||||
v *= 18014398509481984.0;
|
||||
hx = high_word(v);
|
||||
} else if hx >= 0x7FF00000 {
|
||||
return v;
|
||||
} else if hx == 0x3FF00000 && (bits_of(v) << 32) == 0 {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
# x is 2^k (1 + f), where 1 + f is in [sqrt(2) / 2, sqrt(2)).
|
||||
hx += 0x3FF00000 - 0x3FE6A09E;
|
||||
k += ((hx >> 20) as i32) - 0x3FF;
|
||||
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
|
||||
v = with_high_word(v, hx);
|
||||
|
||||
val f := v - 1.0;
|
||||
val hfsq := 0.5 * f * f;
|
||||
val s := f / (2.0 + f);
|
||||
val z := s * s;
|
||||
var w := z * z;
|
||||
val t1 := w * (log10_lg2 + w * (log10_lg4 + w * log10_lg6));
|
||||
val t2 := z * (log10_lg1 + w * (log10_lg3 + w * (log10_lg5 + w * log10_lg7)));
|
||||
val r := t2 + t1;
|
||||
|
||||
# hi + lo is f - hfsq + s * (hfsq + r), about ln(1 + f), hi of 32 bits.
|
||||
val hi := real_of_bits(bits_of(f - hfsq) & 0xFFFFFFFF00000000);
|
||||
val lo := f - hi - hfsq + s * (hfsq + r);
|
||||
|
||||
# high + low is log10(1 + f) + k log10 2.
|
||||
var high := hi * log10_inverse_ln10_high;
|
||||
val dk := k as f64;
|
||||
val y := dk * log10_log10_2_high;
|
||||
var low := dk * log10_log10_2_low + (lo + hi) * log10_inverse_ln10_low + lo * log10_inverse_ln10_high;
|
||||
|
||||
# y added in extra precision.
|
||||
w = y + high;
|
||||
low += (y - w) + high;
|
||||
high = w;
|
||||
return low + high;
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
#! @file log2.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the base 2 logarithm.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
|
||||
#! under the MIT license. Its `log2` is from Arm's optimized-routines, which
|
||||
#! says:
|
||||
#!
|
||||
#! Copyright (c) 2018, Arm Limited.
|
||||
#! SPDX-License-Identifier: MIT
|
||||
#!
|
||||
#! It is within 0.55 ulp of the true value for `f64`. An `f32` is worked in
|
||||
#! `f64` and rounded once more.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
#! The base 2 logarithm of a real, the power 2 is raised to to give it.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The real.
|
||||
#! @returns log2 x: exact at powers of 2, negative infinity for zero, infinity
|
||||
#! for infinity, and NaN below zero.
|
||||
pub def log2<T>(x: T): T where T: real {
|
||||
return log2_double(x as f64) as T;
|
||||
}
|
||||
|
||||
|
||||
# The data of `log2`, from musl's log2_data.c.
|
||||
|
||||
# 1 / ln 2, as a high part of 33 bits and a low part.
|
||||
const log2_inverse_ln2_high: f64 = 1.4426950407214463;
|
||||
const log2_inverse_ln2_low: f64 = 1.6751713164886512e-10;
|
||||
|
||||
# The polynomial of log2(1 + r) for r within 2^-7 of 0, from its second
|
||||
# coefficient, the first being 1 / ln 2.
|
||||
const log2_a: f64[6] = [
|
||||
-0.7213475204444882, 0.4808983469629985, -0.36067375954075914,
|
||||
0.2885390073180969, -0.2404693555628422, 0.2061202382173603,
|
||||
];
|
||||
|
||||
# The polynomial of log2(1 + r) for r near 0, within -0.0424 and 0.0443, from
|
||||
# its second coefficient.
|
||||
const log2_b: f64[10] = [
|
||||
-0.7213475204444817, 0.48089834696298744, -0.360673760222145,
|
||||
0.2885390081805197, -0.24044917405728863, 0.2060992861022954,
|
||||
-0.18033596705327856, 0.1603032746063156, -0.14483316576701266,
|
||||
0.13046826811283835,
|
||||
];
|
||||
|
||||
# For each of N subintervals of [0.6875, 1.375), 1/c and log2 c, for a c near
|
||||
# its middle.
|
||||
const log2_table: f64[128] = [
|
||||
1.4463276106642393, -0.5323943771486483, 1.4301675812282968,
|
||||
-0.516184206053822, 1.4143648676229383, -0.5001543441974263,
|
||||
1.3989068970611538, -0.4842999484799293, 1.3837838957784339,
|
||||
-0.46861865624623533, 1.368983882697786, -0.4531054615783887,
|
||||
1.3544972916324536, -0.43775750882059583, 1.3403139676437443,
|
||||
-0.4225709906127122, 1.3264249304188642, -0.40754302793970965,
|
||||
1.3128203652112156, -0.3926695240384106, 1.299492412370217,
|
||||
-0.3779482100565019, 1.28643198385998, -0.36337518101890964,
|
||||
1.2736319291846832, -0.3489484089425332, 1.2610838968062879,
|
||||
-0.33466425780738973, 1.248780314313572, -0.3205197000629596,
|
||||
1.2367150450059063, -0.3065131231801388, 1.2248804968401878,
|
||||
-0.2926410022678283, 1.2132702353497795, -0.2789009220805383,
|
||||
1.2018780785715362, -0.2652905529862437, 1.190697765061355,
|
||||
-0.2518072602367738, 1.1797235410550593, -0.23844881494437686,
|
||||
1.1689498986205256, -0.22521309705462045, 1.158371137408455,
|
||||
-0.21209756102439314, 1.1479820146386086, -0.1991000395789797,
|
||||
1.1377775576851088, -0.18621852970682085, 1.127753150630225,
|
||||
-0.17345131655361, 1.1179037012282074, -0.16079591649577196,
|
||||
1.1082251967632821, -0.14825107384353942, 1.098712461056607,
|
||||
-0.13581387465410444, 1.0893619091634283, -0.12348332762257996,
|
||||
1.0801689912316736, -0.11125703807351783, 1.071129489697672,
|
||||
-0.09913289918449664, 1.0622406576750423, -0.08711065531497297,
|
||||
1.0534977607927976, -0.07518724771330199, 1.044897877982342,
|
||||
-0.06336194888240243, 1.0364370289499079, -0.0516324649456692,
|
||||
1.0281122132034575, -0.039997735929773626, 1.0199202450036955,
|
||||
-0.028456341769015125, 1.0118577522306755, -0.017006489068080555,
|
||||
1.003921649236766, -0.005646678981520381, 0.9922480467550775,
|
||||
0.011227277611396858, 0.9770993366178549, 0.033422853921138085,
|
||||
0.9624059787315814, 0.05528248992573026, 0.9481480598286369,
|
||||
0.07681573143713649, 0.9343065931261065, 0.09803204623631245,
|
||||
0.9208633936884979, 0.11894094059630334, 0.9078013641840602,
|
||||
0.13955143862290242, 0.8951048181512927, 0.15987146080919956,
|
||||
0.882758574260473, 0.17990916589428707, 0.8707481902241709,
|
||||
0.19967252559081317, 0.8590602881746972, 0.2191687127685782,
|
||||
0.8476822044336688, 0.23840459427242422, 0.8366013284053287,
|
||||
0.25738780610663525, 0.8258064913621258, 0.27612433583180973,
|
||||
0.8152865101579193, 0.2946209507022104, 0.8050314363208223,
|
||||
0.31288297359969874, 0.7950310492640542, 0.33091689015759584,
|
||||
0.7852760432487271, 0.3487282100279572, 0.7757575660314712,
|
||||
0.36632223233368677, 0.7664670566588946, 0.383704309808536,
|
||||
0.7573965608078645, 0.4008792246509074, 0.7485380168514022,
|
||||
0.41785250494945103, 0.7398844213755009, 0.4346281724315304,
|
||||
0.7314286113178112, 0.45121103315341315,
|
||||
];
|
||||
|
||||
# The same c, as a double and the rest.
|
||||
const log2_table2: f64[128] = [
|
||||
0.6914062848739648, 4.339971893418182e-17, 0.6992187580850853,
|
||||
7.805593338712074e-18, 0.7070311366547563, 3.20843320369626e-17,
|
||||
0.7148438556567389, 4.336491946473985e-17, 0.7226561915128084,
|
||||
4.681890319761607e-17, 0.730468789763508, -1.647126642147294e-17,
|
||||
0.7382812842650944, 2.0846837208642192e-17, 0.7460938437864584,
|
||||
-7.82131795361932e-18, 0.7539062159244969, -3.470987810015201e-17,
|
||||
0.761718835645205, 1.2996798345429576e-17, 0.7695312342578777,
|
||||
-5.2071007285622585e-17, 0.7773438569208053, -4.513636731226183e-17,
|
||||
0.7851561955110148, -2.0797044446832382e-17, 0.7929686538163826,
|
||||
-1.9947626198727574e-17, 0.8007813612514213, 1.472064871061342e-17,
|
||||
0.8085937047812207, 5.290804109377209e-17, 0.816406173973453,
|
||||
-1.129659587857177e-17, 0.8242186867064328, 3.901543738154691e-17,
|
||||
0.8320311501051142, -3.212482853127187e-17, 0.8398436860662717,
|
||||
5.220081794706035e-18, 0.8476562221566525, -5.37416378988278e-17,
|
||||
0.8554686571084844, 4.291992377471359e-18, 0.8632811779454657,
|
||||
1.7319616935225806e-17, 0.8710937865301015, 1.8251073579011692e-17,
|
||||
0.8789064200163806, 1.2113671810693101e-17, 0.8867188705623812,
|
||||
-2.431195196496269e-17, 0.894531433164887, -2.258952781769403e-17,
|
||||
0.9023436779100781, 4.3322028821617186e-17, 0.9101562378188762,
|
||||
-5.2414676705491076e-18, 0.9179685755379005, -2.3432856904274634e-17,
|
||||
0.9257810658494647, 3.9914341909758727e-17, 0.9335939394986237,
|
||||
-1.8218382846902827e-17, 0.9414062555172099, 4.423669374431538e-17,
|
||||
0.9492189136192009, -2.6059674623550364e-17, 0.9570313243730211,
|
||||
-4.947019261946595e-17, 0.9648439529540691, -5.4644837229582306e-17,
|
||||
0.9726564738338594, -5.4656321533328614e-17, 0.9804688208697894,
|
||||
-1.782581090537996e-18, 0.98828120632121, -5.2277711573086786e-18,
|
||||
0.9960936700192217, 4.1586638841164565e-17, 1.007812515499802,
|
||||
9.540912389137736e-17, 1.0234373952820537, 3.985821704561347e-17,
|
||||
1.0390625391978199, -5.179495640960355e-18, 1.0546875982436061,
|
||||
-6.074512782488841e-17, 1.07031247275489, -4.853656019321416e-17,
|
||||
1.0859374005459401, -1.0233609026899023e-16, 1.101562565835984,
|
||||
9.714504510281668e-17, 1.1171875960464082, -5.728631491659617e-17,
|
||||
1.1328125595809087, -4.3353271744463366e-17, 1.1484376438870962,
|
||||
5.029455257812864e-17, 1.1640626551656426, -1.3886091944776072e-17,
|
||||
1.179687381390876, 9.535864385063222e-17, 1.19531246968748,
|
||||
2.5970710873556995e-17, 1.2109374417129501, -1.0412863482435496e-16,
|
||||
1.2265626715770166, 1.903080930681457e-17, 1.2421875157698543,
|
||||
9.884933154288451e-17, 1.2578125104996614, 2.605028401046449e-17,
|
||||
1.273437549250769, 3.128070230699857e-17, 1.2890625161616944,
|
||||
-4.506460293510891e-17, 1.3046875156762752, -2.0874781900380847e-17,
|
||||
1.3203123063212308, 8.50535702424751e-17, 1.3359374907988373,
|
||||
-3.99657459893118e-17, 1.351562448282023, 4.1576596545784e-17,
|
||||
1.3671874254389709, -6.072295450780066e-17,
|
||||
];
|
||||
|
||||
|
||||
# log2 x, from musl's log2.c.
|
||||
def log2_double(x: f64): f64 {
|
||||
var ix := bits_of(x);
|
||||
val top := (ix >> 48) as u32;
|
||||
|
||||
# Near 1, within 1 - 0x1.5b51p-5 and 1 + 0x1.6ab2p-5, where the polynomial
|
||||
# alone is near enough.
|
||||
if ix - 0x3FEEA4AF00000000 < 0x3FF0B55900000000 - 0x3FEEA4AF00000000 {
|
||||
return 0.0 if ix == 0x3FF0000000000000;
|
||||
val r := x - 1.0;
|
||||
# r / ln 2 as hi + lo, r split so the high part's product is exact.
|
||||
val rhi := real_of_bits(bits_of(r) & 0xFFFFFFFF00000000);
|
||||
val rlo := r - rhi;
|
||||
val hi := rhi * log2_inverse_ln2_high;
|
||||
var lo := rlo * log2_inverse_ln2_high + r * log2_inverse_ln2_low;
|
||||
val r2 := r * r;
|
||||
val r4 := r2 * r2;
|
||||
val p := r2 * (log2_b[0] + r * log2_b[1]);
|
||||
var y := hi + p;
|
||||
lo += hi - y + p;
|
||||
lo += r4 * (log2_b[2] + r * log2_b[3] + r2 * (log2_b[4] + r * log2_b[5])
|
||||
+ r4 * (log2_b[6] + r * log2_b[7] + r2 * (log2_b[8] + r * log2_b[9])));
|
||||
y += lo;
|
||||
return y;
|
||||
}
|
||||
if top - 0x0010 >= 0x7FF0 - 0x0010 {
|
||||
# Below 2^-1022, infinite, or NaN.
|
||||
return divided_by_zero(1) if ix * 2 == 0;
|
||||
return x if ix == 0x7FF0000000000000;
|
||||
return invalid(x) if (top & 0x8000) != 0 || (top & 0x7FF0) == 0x7FF0;
|
||||
# A subnormal, made normal.
|
||||
ix = bits_of(x * 4503599627370496.0);
|
||||
ix -= 52u << 52;
|
||||
}
|
||||
|
||||
# x is 2^k z, where z is in [0.6875, 1.375) and exact, and falls in the ith
|
||||
# subinterval, whose c is near its middle.
|
||||
val tmp := ix - 0x3FE6000000000000;
|
||||
val i := (tmp >> (52 - 6)) % 64;
|
||||
val k := (tmp as i64) >> 52;
|
||||
val iz := ix - (tmp & (0xFFFu << 52));
|
||||
val invc := log2_table[2 * i];
|
||||
val logc := log2_table[2 * i + 1];
|
||||
val z := real_of_bits(iz);
|
||||
val kd := k as f64;
|
||||
|
||||
# log2 x is log2(1 + (z/c - 1)) + log2 c + k, where r is about z/c - 1, and
|
||||
# r / ln 2 is t1 + t2.
|
||||
val r := (z - log2_table2[2 * i] - log2_table2[2 * i + 1]) * invc;
|
||||
val rhi := real_of_bits(bits_of(r) & 0xFFFFFFFF00000000);
|
||||
val rlo := r - rhi;
|
||||
val t1 := rhi * log2_inverse_ln2_high;
|
||||
val t2 := rlo * log2_inverse_ln2_high + r * log2_inverse_ln2_low;
|
||||
|
||||
# hi + lo is r / ln 2 + log2 c + k.
|
||||
val t3 := kd + logc;
|
||||
val hi := t3 + t1;
|
||||
val lo := t3 - hi + t1 + t2;
|
||||
|
||||
# log2(1 + r) is r / ln 2 + r^2 p.
|
||||
val r2 := r * r;
|
||||
val r4 := r2 * r2;
|
||||
val p := log2_a[0] + r * log2_a[1] + r2 * (log2_a[2] + r * log2_a[3]) + r4 * (log2_a[4] + r * log2_a[5]);
|
||||
return lo + r2 * p + hi;
|
||||
}
|
||||
+384
@@ -0,0 +1,384 @@
|
||||
#! @file pow.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains a real raised to a real power.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
|
||||
#! under the MIT license. Its `pow` is from Arm's optimized-routines, which
|
||||
#! says:
|
||||
#!
|
||||
#! Copyright (c) 2018, Arm Limited.
|
||||
#! SPDX-License-Identifier: MIT
|
||||
#!
|
||||
#! It is within 0.55 ulp of the true value for `f64`. An `f32` is worked in
|
||||
#! `f64` and rounded once more.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
#! A real raised to a power, as C's `pow`.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The base.
|
||||
#! @param y The power.
|
||||
#! @returns x^y: 1 where y is zero or x is 1, even with NaN; NaN where x is
|
||||
#! finite and below zero and y is not an integer; infinity, of x's
|
||||
#! sign where y is an odd integer, for a zero x and y below zero; and
|
||||
#! infinity or 0 past what the type holds.
|
||||
pub def pow<T>(x: T, y: T): T where T: real {
|
||||
return pow_double(x as f64, y as f64) as T;
|
||||
}
|
||||
|
||||
|
||||
# The data of the logarithm `pow` works with, from musl's pow_data.c. Its ln 2
|
||||
# halves are those of `log`.
|
||||
|
||||
# The polynomial of ln(1 + r) for r within 0.0056 of 0, from its second
|
||||
# coefficient, each scaled to undo the factors of -1/2 it is evaluated with.
|
||||
const pow_a: f64[7] = [
|
||||
-0.5, -0.6666666666666679, 0.5000000000000007,
|
||||
0.7999999995323976, -0.6666666663487739, -1.142909628459501,
|
||||
1.0000415263675542,
|
||||
];
|
||||
|
||||
# For each of N subintervals of [0.7057, 1.4114), 1/c, and ln c as a multiple
|
||||
# of 2^-43 and the rest, for a c near its middle whose 1/c has few bits, so
|
||||
# z/c - 1 is exact.
|
||||
const pow_table: f64[384] = [
|
||||
1.4140625, -0.3464667673462145, 5.929407345889625e-15,
|
||||
1.40625, -0.34092658697056777, -2.544157440035963e-14,
|
||||
1.3984375, -0.3353555419211034, -3.443525940775045e-14,
|
||||
1.390625, -0.3297532863724655, -2.500123826022799e-15,
|
||||
1.3828125, -0.32411946865420305, -8.929337133850617e-15,
|
||||
1.375, -0.31845373111855224, 1.7625431312172662e-14,
|
||||
1.3671875, -0.31275571000389846, 1.5688303180062087e-15,
|
||||
1.359375, -0.3070250352949415, 2.9655274673691784e-14,
|
||||
1.3515625, -0.3012613305781997, 3.7923164802093147e-14,
|
||||
1.34375, -0.2954642128938758, 3.993416384387844e-14,
|
||||
1.3359375, -0.28963329258306203, 1.9352855826489123e-14,
|
||||
1.3359375, -0.28963329258306203, 1.9352855826489123e-14,
|
||||
1.328125, -0.28376817313062475, -1.9852665484979036e-14,
|
||||
1.3203125, -0.27786845100342816, -2.814323765595281e-14,
|
||||
1.3125, -0.2719337154836694, 2.7643769993528702e-14,
|
||||
1.3046875, -0.2659635484970977, -4.025092402293806e-14,
|
||||
1.296875, -0.25995752443691345, -1.2621729398885316e-14,
|
||||
1.2890625, -0.25391520998095984, -3.600176732637335e-15,
|
||||
1.2890625, -0.25391520998095984, -3.600176732637335e-15,
|
||||
1.28125, -0.2478361639045943, 1.3029797173308663e-14,
|
||||
1.2734375, -0.2417199368871934, 4.8230289429940886e-14,
|
||||
1.265625, -0.23556607131274632, -2.0592242769647135e-14,
|
||||
1.2578125, -0.22937410106487732, 3.149265065191484e-14,
|
||||
1.25, -0.22314355131425145, 4.169796584527195e-14,
|
||||
1.25, -0.22314355131425145, 4.169796584527195e-14,
|
||||
1.2421875, -0.21687393830063684, 2.2477465222466186e-14,
|
||||
1.234375, -0.21056476910735, 3.6507188831790577e-16,
|
||||
1.2265625, -0.2042155414286526, -3.827767260205414e-14,
|
||||
1.2265625, -0.2042155414286526, -3.827767260205414e-14,
|
||||
1.21875, -0.19782574332987224, -4.7641388950792196e-14,
|
||||
1.2109375, -0.19139485299967873, 4.9278276214647115e-14,
|
||||
1.203125, -0.18492233849406148, 4.9485167661250996e-14,
|
||||
1.203125, -0.18492233849406148, 4.9485167661250996e-14,
|
||||
1.1953125, -0.1784076574728033, -1.5003333854266542e-14,
|
||||
1.1875, -0.17185025692663203, -2.7194441649495324e-14,
|
||||
1.1875, -0.17185025692663203, -2.7194441649495324e-14,
|
||||
1.1796875, -0.1652495728952772, -2.99659267292569e-14,
|
||||
1.171875, -0.15860503017665906, 2.0472357800461955e-14,
|
||||
1.171875, -0.15860503017665906, 2.0472357800461955e-14,
|
||||
1.1640625, -0.15191604202584585, 3.879296723063646e-15,
|
||||
1.15625, -0.1451820098444614, -3.6506824353335045e-14,
|
||||
1.1484375, -0.13840232285906495, -5.4183331379008994e-14,
|
||||
1.1484375, -0.13840232285906495, -5.4183331379008994e-14,
|
||||
1.140625, -0.131576357788731, 1.1729485484531301e-14,
|
||||
1.140625, -0.131576357788731, 1.1729485484531301e-14,
|
||||
1.1328125, -0.12470347850091912, -3.811763084710266e-14,
|
||||
1.125, -0.11778303565643, 4.654729747598445e-14,
|
||||
1.125, -0.11778303565643, 4.654729747598445e-14,
|
||||
1.1171875, -0.11081436634026431, -2.5799991283069902e-14,
|
||||
1.109375, -0.10379679368168127, 3.7700471749674615e-14,
|
||||
1.109375, -0.10379679368168127, 3.7700471749674615e-14,
|
||||
1.1015625, -0.09672962645856842, 1.7306161136093256e-14,
|
||||
1.1015625, -0.09672962645856842, 1.7306161136093256e-14,
|
||||
1.09375, -0.089612158689647, -4.012913552726574e-14,
|
||||
1.0859375, -0.08244366921110213, 2.7541708360737882e-14,
|
||||
1.0859375, -0.08244366921110213, 2.7541708360737882e-14,
|
||||
1.078125, -0.07522342123763792, 5.0396178134370583e-14,
|
||||
1.078125, -0.07522342123763792, 5.0396178134370583e-14,
|
||||
1.0703125, -0.06795066190852594, 1.8195060030168815e-14,
|
||||
1.0625, -0.06062462181648698, 5.213620639136504e-14,
|
||||
1.0625, -0.06062462181648698, 5.213620639136504e-14,
|
||||
1.0546875, -0.053244514518837605, 2.532168943117445e-14,
|
||||
1.0546875, -0.053244514518837605, 2.532168943117445e-14,
|
||||
1.046875, -0.045809536031242715, -5.148849572685811e-14,
|
||||
1.046875, -0.045809536031242715, -5.148849572685811e-14,
|
||||
1.0390625, -0.038318864302141264, 4.6652946995830086e-15,
|
||||
1.0390625, -0.038318864302141264, 4.6652946995830086e-15,
|
||||
1.03125, -0.03077165866670839, -4.529814257790929e-14,
|
||||
1.03125, -0.03077165866670839, -4.529814257790929e-14,
|
||||
1.0234375, -0.023167059281490765, -4.361324067851568e-14,
|
||||
1.015625, -0.015504186535963527, -1.7274567499706107e-15,
|
||||
1.015625, -0.015504186535963527, -1.7274567499706107e-15,
|
||||
1.0078125, -0.0077821404420319595, -2.298941004620351e-14,
|
||||
1.0078125, -0.0077821404420319595, -2.298941004620351e-14,
|
||||
1.0, 0.0, 0.0,
|
||||
1.0, 0.0, 0.0,
|
||||
0.9921875, 0.007843177461040796, -1.4902732911301337e-14,
|
||||
0.984375, 0.01574835696817445, -3.527980389655325e-14,
|
||||
0.9765625, 0.023716526617363343, -4.730054772033249e-14,
|
||||
0.96875, 0.03174869831457272, 7.580310369375161e-15,
|
||||
0.9609375, 0.039845908547249564, -4.9893776716773285e-14,
|
||||
0.953125, 0.048009219186383234, -2.262629393030674e-14,
|
||||
0.9453125, 0.056239718322899535, -2.345674491018699e-14,
|
||||
0.94140625, 0.06038051098892083, -1.3352588834854848e-14,
|
||||
0.93359375, 0.06871389254808946, -3.765296820388875e-14,
|
||||
0.92578125, 0.07711730334438016, 5.1128335719851986e-14,
|
||||
0.91796875, 0.08559193033545398, -5.046674438470119e-14,
|
||||
0.9140625, 0.08985632912185793, 3.1218748807418837e-15,
|
||||
0.90625, 0.09844007281321865, 3.3871241029241416e-14,
|
||||
0.8984375, 0.10709813555638448, -1.7376727386423858e-14,
|
||||
0.89453125, 0.11145544092528326, 3.957125899799804e-14,
|
||||
0.88671875, 0.12022742699821265, -5.2849453521890294e-14,
|
||||
0.8828125, 0.12464244520731427, -3.767012502308738e-14,
|
||||
0.875, 0.13353139262449076, 3.1859736349078334e-14,
|
||||
0.87109375, 0.13800567301939282, 5.0900642926060466e-14,
|
||||
0.86328125, 0.14701474296180095, 8.710783796122478e-15,
|
||||
0.859375, 0.15154989812720032, 6.157896229122976e-16,
|
||||
0.8515625, 0.16068238169043525, 3.821577743916796e-14,
|
||||
0.84765625, 0.16528009093906348, 3.9440046718453496e-14,
|
||||
0.83984375, 0.17453941635187675, 2.2924522154618074e-14,
|
||||
0.8359375, 0.17920142945774842, -3.742530094732263e-14,
|
||||
0.83203125, 0.18388527877016259, -2.5223102140407338e-14,
|
||||
0.82421875, 0.1933193110035063, -1.0320443688698849e-14,
|
||||
0.8203125, 0.19806991376208316, 1.0634128304268335e-14,
|
||||
0.8125, 0.20763936477828793, -4.3425422595242564e-14,
|
||||
0.80859375, 0.21245865121420593, -1.2527395755711364e-14,
|
||||
0.8046875, 0.21730127569003344, -5.204008743405884e-14,
|
||||
0.80078125, 0.22216746534115828, -3.979844515951702e-15,
|
||||
0.79296875, 0.2319714654378231, -4.7955860343296286e-14,
|
||||
0.7890625, 0.2369097470783572, 5.015686013791602e-16,
|
||||
0.78515625, 0.24187253642048745, -7.252318953240293e-16,
|
||||
0.78125, 0.2468600779315011, 2.4688324156011588e-14,
|
||||
0.7734375, 0.2569104137850218, 5.465121253624792e-15,
|
||||
0.76953125, 0.26197371574153294, 4.102651071698446e-14,
|
||||
0.765625, 0.2670627852490952, -4.996736502345936e-14,
|
||||
0.76171875, 0.27217788591576664, 4.903580708156347e-14,
|
||||
0.7578125, 0.27731928541618345, 5.089628039500759e-14,
|
||||
0.75390625, 0.28248725557466514, 1.1782016386565151e-14,
|
||||
0.74609375, 0.29290401643288533, 4.727452940514406e-14,
|
||||
0.7421875, 0.29815337231912054, -4.4204083338755686e-14,
|
||||
0.73828125, 0.3034304294199046, 1.548345993498083e-14,
|
||||
0.734375, 0.30873548164959175, 2.1522127491642888e-14,
|
||||
0.73046875, 0.3140688276249648, 1.1054030169005386e-14,
|
||||
0.7265625, 0.31943077076641657, -5.534326352070679e-14,
|
||||
0.72265625, 0.3248216194012912, -5.351646604259541e-14,
|
||||
0.71875, 0.33024168687052224, 5.4612144489920215e-14,
|
||||
0.71484375, 0.3356912916381134, 2.8136969901227338e-14,
|
||||
0.7109375, 0.3411707574027787, -1.156568624616423e-14,
|
||||
];
|
||||
|
||||
# The sign bias, which `pow_exp` adds to the exponent of its scale to negate
|
||||
# the result.
|
||||
const pow_sign_bias: u32 = 0x800 << 7;
|
||||
|
||||
|
||||
# ln x, as a double and about 15 bits more. ix is the bits of x, whose exponent
|
||||
# is below zero where x is subnormal.
|
||||
struct pow_sum {
|
||||
var hi: f64;
|
||||
var lo: f64;
|
||||
}
|
||||
|
||||
def pow_log(ix: u64): pow_sum {
|
||||
# x is 2^k z, where z is in [0.7057, 1.4114) and exact, and falls in the ith
|
||||
# subinterval, whose c is near its middle.
|
||||
val tmp := ix - 0x3FE6955500000000;
|
||||
val i := (tmp >> (52 - 7)) % 128;
|
||||
val k := (tmp as i64) >> 52;
|
||||
val iz := ix - (tmp & (0xFFFu << 52));
|
||||
val z := real_of_bits(iz);
|
||||
val kd := k as f64;
|
||||
|
||||
# ln x is k ln 2 + ln c + ln(1 + (z/c - 1)).
|
||||
val invc := pow_table[3 * i];
|
||||
val logc := pow_table[3 * i + 1];
|
||||
val logctail := pow_table[3 * i + 2];
|
||||
|
||||
# r is z/c - 1, as rhi + rlo, of z split so rhi, rlo, and rhi^2 are exact.
|
||||
val zhi := real_of_bits((iz + (1u << 31)) & 0xFFFFFFFF00000000);
|
||||
val zlo := z - zhi;
|
||||
val rhi := zhi * invc - 1.0;
|
||||
val rlo := zlo * invc;
|
||||
val r := rhi + rlo;
|
||||
|
||||
# k ln 2 + ln c + r.
|
||||
val t1 := kd * log_ln2_high + logc;
|
||||
val t2 := t1 + r;
|
||||
val lo1 := kd * log_ln2_low + logctail;
|
||||
val lo2 := t1 - t2 + r;
|
||||
|
||||
# k ln 2 + ln c + r - r^2 / 2, the last as arhi2 + lo3.
|
||||
val ar := pow_a[0] * r;
|
||||
val ar2 := r * ar;
|
||||
val ar3 := r * ar2;
|
||||
val arhi := pow_a[0] * rhi;
|
||||
val arhi2 := rhi * arhi;
|
||||
val hi := t2 + arhi2;
|
||||
val lo3 := rlo * (ar + arhi);
|
||||
val lo4 := t2 - hi + arhi2;
|
||||
# ln(1 + r) - r + r^2 / 2.
|
||||
val p := ar3 * (pow_a[1] + r * pow_a[2] + ar2 * (pow_a[3] + r * pow_a[4] + ar2 * (pow_a[5] + r * pow_a[6])));
|
||||
val lo := lo1 + lo2 + lo3 + lo4 + p;
|
||||
val y := hi + lo;
|
||||
return pow_sum { hi = y, lo = hi - y + lo };
|
||||
}
|
||||
|
||||
# As `exp_special`, where the result may be negative.
|
||||
def pow_special(tmp: f64, scale_bits: u64, ki: u64): f64 {
|
||||
var sbits := scale_bits;
|
||||
if (ki & 0x80000000) == 0 {
|
||||
# k above zero, where the scale's exponent may have overflowed by 460.
|
||||
sbits -= 1009u << 52;
|
||||
val scale := real_of_bits(sbits);
|
||||
return 5.486124068793689e+303 * (scale + scale * tmp);
|
||||
}
|
||||
# k below zero, where the subnormals need care.
|
||||
sbits += 1022u << 52;
|
||||
val scale := real_of_bits(sbits);
|
||||
var y := scale + scale * tmp;
|
||||
if abs(y) < 1.0 {
|
||||
# Rounded to the precision it ends with before scaling into the subnormals,
|
||||
# so it rounds once.
|
||||
val one := y < 0.0 ? -1.0 : 1.0;
|
||||
var lo := scale - y + scale * tmp;
|
||||
val hi := one + y;
|
||||
lo = one - hi + y + lo;
|
||||
y = (hi + lo) - one;
|
||||
if y == 0.0 {
|
||||
# A zero of the scale's sign.
|
||||
y = real_of_bits(sbits & 0x8000000000000000);
|
||||
}
|
||||
}
|
||||
return 2.2250738585072014e-308 * y;
|
||||
}
|
||||
|
||||
# e^(x + xtail), negated where sign_bias is `pow_sign_bias`, as `exp_double`
|
||||
# works it, xtail being within 2^-8 / N of 0 and of x.
|
||||
def pow_exp(x: f64, xtail: f64, sign_bias: u32): f64 {
|
||||
var abstop := top12(x) & 0x7FF;
|
||||
if abstop - 0x3C9 >= 0x408 - 0x3C9 {
|
||||
if abstop - 0x3C9 >= 0x80000000 {
|
||||
# Below 2^-54, where e^x is 1 + x.
|
||||
val one := 1.0 + x;
|
||||
return sign_bias != 0 ? -one : one;
|
||||
}
|
||||
if abstop >= 0x409 {
|
||||
return (bits_of(x) >> 63) != 0 ? underflowed(sign_bias) : overflowed(sign_bias);
|
||||
}
|
||||
# A large x, which `pow_special` works out.
|
||||
abstop = 0;
|
||||
}
|
||||
|
||||
# e^x is 2^(k / N) * e^r, where x is k ln 2 / N + r, k is an integer, and r
|
||||
# is within ln 2 / 2N of 0.
|
||||
val z := exp_inverse_ln2_n * x;
|
||||
var kd := z + exp_shift;
|
||||
val ki := bits_of(kd);
|
||||
kd -= exp_shift;
|
||||
var r := x + kd * exp_negative_ln2_high_n + kd * exp_negative_ln2_low_n;
|
||||
r += xtail;
|
||||
# 2^(k / N) is about scale * (1 + tail), the sign bias negating scale.
|
||||
val index := 2 * (ki % exp_size);
|
||||
val top := (ki + (sign_bias as u64)) << (52 - 7);
|
||||
val tail := real_of_bits(exp_table[index]);
|
||||
val sbits := exp_table[index + 1] + top;
|
||||
# e^x is about scale + scale * (tail + e^r - 1).
|
||||
val r2 := r * r;
|
||||
val tmp := tail + r + r2 * (exp_c2 + r * exp_c3) + r2 * r2 * (exp_c4 + r * exp_c5);
|
||||
return pow_special(tmp, sbits, ki) if abstop == 0;
|
||||
val scale := real_of_bits(sbits);
|
||||
return scale + scale * tmp;
|
||||
}
|
||||
|
||||
# Whether the bits of a finite real other than zero are of an integer: 0 where
|
||||
# they are not, 1 where it is odd, and 2 where it is even.
|
||||
def pow_check_integer(iy: u64): i32 {
|
||||
val e := ((iy >> 52) & 0x7FF) as i32;
|
||||
return 0 if e < 0x3FF;
|
||||
return 2 if e > 0x3FF + 52;
|
||||
return 0 if (iy & ((1u << ((0x3FF + 52 - e) as u64)) - 1)) != 0;
|
||||
return 1 if (iy & (1u << ((0x3FF + 52 - e) as u64))) != 0;
|
||||
return 2;
|
||||
}
|
||||
|
||||
# Whether bits are of zero, infinity, or NaN.
|
||||
def pow_zero_inf_nan(i: u64): bool {
|
||||
return 2 * i - 1 >= 2 * 0x7FF0000000000000u - 1;
|
||||
}
|
||||
|
||||
|
||||
# x^y, from musl's pow.c.
|
||||
def pow_double(x: f64, y: f64): f64 {
|
||||
var sign_bias: u32 = 0;
|
||||
var ix := bits_of(x);
|
||||
val iy := bits_of(y);
|
||||
var topx := top12(x);
|
||||
val topy := top12(y);
|
||||
if topx - 0x001 >= 0x7FF - 0x001 || (topy & 0x7FF) - 0x3BE >= 0x43E - 0x3BE {
|
||||
# x below 2^-1022 (or below zero), infinite, or NaN; or |y| below 2^-65,
|
||||
# from 2^63 up, or NaN. Past 2^63, x^y is infinity or 0, and below 2^-65,
|
||||
# it is 1 within an ulp.
|
||||
if pow_zero_inf_nan(iy) {
|
||||
return 1.0 if 2 * iy == 0;
|
||||
return 1.0 if ix == 0x3FF0000000000000;
|
||||
return x + y if 2 * ix > 2 * 0x7FF0000000000000u || 2 * iy > 2 * 0x7FF0000000000000u;
|
||||
return 1.0 if 2 * ix == 2 * 0x3FF0000000000000u;
|
||||
# |x| below 1 and y infinity, or |x| above 1 and y negative infinity.
|
||||
return 0.0 if (2 * ix < 2 * 0x3FF0000000000000u) == ((iy >> 63) == 0);
|
||||
return y * y;
|
||||
}
|
||||
if pow_zero_inf_nan(ix) {
|
||||
var x2 := x * x;
|
||||
if (ix >> 63) != 0 && pow_check_integer(iy) == 1 {
|
||||
x2 = -x2;
|
||||
}
|
||||
return (iy >> 63) != 0 ? 1.0 / x2 : x2;
|
||||
}
|
||||
# x and y finite, and not zero.
|
||||
if (ix >> 63) != 0 {
|
||||
# x below zero.
|
||||
val yint := pow_check_integer(iy);
|
||||
return invalid(x) if yint == 0;
|
||||
if yint == 1 {
|
||||
sign_bias = pow_sign_bias;
|
||||
}
|
||||
ix &= 0x7FFFFFFFFFFFFFFF;
|
||||
topx &= 0x7FF;
|
||||
}
|
||||
if (topy & 0x7FF) - 0x3BE >= 0x43E - 0x3BE {
|
||||
# y is not odd here, being so large or so small.
|
||||
return 1.0 if ix == 0x3FF0000000000000;
|
||||
if (topy & 0x7FF) < 0x3BE {
|
||||
# |y| below 2^-65, where x^y is about 1 + y ln x.
|
||||
return ix > 0x3FF0000000000000 ? 1.0 + y : 1.0 - y;
|
||||
}
|
||||
return (ix > 0x3FF0000000000000) == (topy < 0x800) ? overflowed(0) : underflowed(0);
|
||||
}
|
||||
if topx == 0 {
|
||||
# A subnormal x, made normal with an exponent below zero.
|
||||
ix = bits_of(x * 4503599627370496.0);
|
||||
ix &= 0x7FFFFFFFFFFFFFFF;
|
||||
ix -= 52u << 52;
|
||||
}
|
||||
}
|
||||
|
||||
# x^y is e^(y ln x), y ln x worked as ehi + elo, from halves of y and ln x
|
||||
# whose products are exact.
|
||||
val ln := pow_log(ix);
|
||||
val yhi := real_of_bits(iy & 0xFFFFFFFFF8000000);
|
||||
val ylo := y - yhi;
|
||||
val lhi := real_of_bits(bits_of(ln.hi) & 0xFFFFFFFFF8000000);
|
||||
val llo := ln.hi - lhi + ln.lo;
|
||||
val ehi := yhi * lhi;
|
||||
val elo := ylo * lhi + y * llo;
|
||||
return pow_exp(ehi, elo, sign_bias);
|
||||
}
|
||||
@@ -0,0 +1,417 @@
|
||||
#! @file reduce.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the reduction of a double by multiples of π/2, which the
|
||||
#! trigonometric functions work from: x less n·π/2, as two doubles whose sum
|
||||
#! holds it to well past a double's precision, and n. Arguments up to 2^20·π/2
|
||||
#! are reduced with π/2 in three parts of 33 bits; larger ones with as many bits
|
||||
#! of 2/π as their exponent needs, so the largest double reduces as well as 1e7.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
|
||||
#! under the MIT license. Its `__rem_pio2` and `__rem_pio2_large` are from
|
||||
#! FreeBSD's e_rem_pio2.c, optimized by Bruce D. Evans, and k_rem_pio2.c, which
|
||||
#! say:
|
||||
#!
|
||||
#! Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
#!
|
||||
#! Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
#! Permission to use, copy, modify, and distribute this
|
||||
#! software is freely granted, provided that this notice
|
||||
#! is preserved.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
# A double reduced by n multiples of π/2: x - n·π/2 is about hi + lo, where hi
|
||||
# is the sum rounded and within about π/4 of 0.
|
||||
struct reduce_result {
|
||||
var n: i32;
|
||||
var hi: f64;
|
||||
var lo: f64;
|
||||
}
|
||||
|
||||
|
||||
# The constants of the reduction, from musl's __rem_pio2.c.
|
||||
|
||||
# 1.5 * 2^52, which adding and taking away rounds a smaller double to an
|
||||
# integer.
|
||||
const reduce_to_integer: f64 = 6755399441055744.0;
|
||||
|
||||
# π/4.
|
||||
const reduce_pio4: f64 = 0.7853981633974483;
|
||||
|
||||
# 2/π, to 53 bits.
|
||||
const reduce_inverse_pio2: f64 = 0.6366197723675814;
|
||||
|
||||
# π/2 in three parts of 33 bits, each with what is left of π/2 after it and
|
||||
# those before it.
|
||||
const reduce_pio2_1: f64 = 1.5707963267341256;
|
||||
const reduce_pio2_1_tail: f64 = 6.077100506506192e-11;
|
||||
const reduce_pio2_2: f64 = 6.077100506303966e-11;
|
||||
const reduce_pio2_2_tail: f64 = 2.0222662487959506e-21;
|
||||
const reduce_pio2_3: f64 = 2.0222662487111665e-21;
|
||||
const reduce_pio2_3_tail: f64 = 8.4784276603689e-32;
|
||||
|
||||
|
||||
# x less n·π/2, from musl's __rem_pio2.c. The caller handles |x| up to about
|
||||
# π/4, which needs no reducing. Infinity and NaN give NaN, with n 0.
|
||||
def reduce_pio2(x: f64): reduce_result {
|
||||
val bits := bits_of(x);
|
||||
val sign := (bits >> 63) != 0;
|
||||
val ix := ((bits >> 32) as u32) & 0x7FFFFFFF;
|
||||
|
||||
# Up to 9π/4, x is less 1, 2, 3, or 4 times π/2, but near π/2, π, 3π/2, and
|
||||
# 2π, where too many bits cancel, and the medium case reduces it.
|
||||
var near := false;
|
||||
if ix <= 0x400F6A7A {
|
||||
if (ix & 0xFFFFF) == 0x921FB {
|
||||
near = true;
|
||||
} else if ix <= 0x4002D97C {
|
||||
return reduce_pio2_small(x, sign ? -1 : 1);
|
||||
} else {
|
||||
return reduce_pio2_small(x, sign ? -2 : 2);
|
||||
}
|
||||
} else if ix <= 0x401C463B {
|
||||
if ix <= 0x4015FDBC {
|
||||
if ix == 0x4012D97C {
|
||||
near = true;
|
||||
} else {
|
||||
return reduce_pio2_small(x, sign ? -3 : 3);
|
||||
}
|
||||
} else {
|
||||
if ix == 0x401921FB {
|
||||
near = true;
|
||||
} else {
|
||||
return reduce_pio2_small(x, sign ? -4 : 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
return reduce_pio2_medium(x, ix) if near || ix < 0x413921FB;
|
||||
|
||||
# Infinity and NaN.
|
||||
if ix >= 0x7FF00000 {
|
||||
val nan := x - x;
|
||||
return reduce_result { n = 0, hi = nan, lo = nan };
|
||||
}
|
||||
|
||||
# |x| as 2^e0 times three pieces of 24 bits, the first with its top bit set,
|
||||
# less those that are zero at the end.
|
||||
val z0 := real_of_bits((bits & 0x000FFFFFFFFFFFFF) | ((0x3FF + 23) << 52));
|
||||
var tx: f64[3];
|
||||
var z := z0;
|
||||
var i: i32 = 0;
|
||||
while i < 2 {
|
||||
tx[i] = (z as i32) as f64;
|
||||
z = (z - tx[i]) * 16777216.0;
|
||||
i++;
|
||||
}
|
||||
tx[2] = z;
|
||||
while tx[i] == 0.0 {
|
||||
i--;
|
||||
}
|
||||
val large := reduce_pio2_large(tx, (ix >> 20) as i32 - (0x3FF + 23), i + 1);
|
||||
if sign {
|
||||
return reduce_result { n = -large.n, hi = -large.hi, lo = -large.lo };
|
||||
}
|
||||
return large;
|
||||
}
|
||||
|
||||
# x less k·π/2, for k from -4 to 4 that is not near x / (π/2): a subtraction
|
||||
# of the first part, which cancels to about 85 bits, then its tail. musl writes
|
||||
# x + |k|·π/2 for k below 0, which rounds the same.
|
||||
def reduce_pio2_small(x: f64, k: i32): reduce_result {
|
||||
val m := k as f64;
|
||||
val z := x - m * reduce_pio2_1;
|
||||
val hi := z - m * reduce_pio2_1_tail;
|
||||
val lo := (z - hi) - m * reduce_pio2_1_tail;
|
||||
return reduce_result { n = k, hi = hi, lo = lo };
|
||||
}
|
||||
|
||||
# x less n·π/2 for |x| below 2^20·π/2, where n is x / (π/2) rounded: the first
|
||||
# part of π/2 and its tail, then the second and third where so many bits cancel
|
||||
# that the tail isn't enough.
|
||||
def reduce_pio2_medium(x: f64, ix: u32): reduce_result {
|
||||
var fn := x * reduce_inverse_pio2 + reduce_to_integer - reduce_to_integer;
|
||||
var n := fn as i32;
|
||||
var r := x - fn * reduce_pio2_1;
|
||||
var w := fn * reduce_pio2_1_tail;
|
||||
# Only a rounding mode other than the nearest makes n off by 1.
|
||||
if r - w < -reduce_pio4 {
|
||||
n--;
|
||||
fn--;
|
||||
r = x - fn * reduce_pio2_1;
|
||||
w = fn * reduce_pio2_1_tail;
|
||||
} else if r - w > reduce_pio4 {
|
||||
n++;
|
||||
fn++;
|
||||
r = x - fn * reduce_pio2_1;
|
||||
w = fn * reduce_pio2_1_tail;
|
||||
}
|
||||
var hi := r - w;
|
||||
val ex := (ix >> 20) as i32;
|
||||
var ey := ((bits_of(hi) >> 52) & 0x7FF) as i32;
|
||||
if ex - ey > 16 {
|
||||
# Good to 118 bits.
|
||||
var t := r;
|
||||
w = fn * reduce_pio2_2;
|
||||
r = t - w;
|
||||
w = fn * reduce_pio2_2_tail - ((t - r) - w);
|
||||
hi = r - w;
|
||||
ey = ((bits_of(hi) >> 52) & 0x7FF) as i32;
|
||||
if ex - ey > 49 {
|
||||
# Good to 151 bits, which is enough for every double.
|
||||
t = r;
|
||||
w = fn * reduce_pio2_3;
|
||||
r = t - w;
|
||||
w = fn * reduce_pio2_3_tail - ((t - r) - w);
|
||||
hi = r - w;
|
||||
}
|
||||
}
|
||||
val lo := (r - hi) - w;
|
||||
return reduce_result { n = n, hi = hi, lo = lo };
|
||||
}
|
||||
|
||||
|
||||
# The reduction of large doubles, from musl's __rem_pio2_large.c, at the
|
||||
# precision of a double: x·(2/π) is formed in pieces of 24 bits, from only the
|
||||
# bits of 2/π that reach its last three integer bits and its fraction, so it
|
||||
# takes as long for 1e300 as for 1e7.
|
||||
|
||||
# The bits of 2/π after the point, 24 to an entry: entry i is the bits from
|
||||
# 24i + 1 to 24i + 24, so it is worth `reduce_2_over_pi[i]` · 2^(-24(i + 1)).
|
||||
# These are as many as a double's exponent can need; musl holds more for long
|
||||
# doubles.
|
||||
const reduce_2_over_pi: i32[66] = [
|
||||
0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62,
|
||||
0x95993C, 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A,
|
||||
0x424DD2, 0xE00649, 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129,
|
||||
0xA73EE8, 0x8235F5, 0x2EBB44, 0x84E99C, 0x7026B4, 0x5F7E41,
|
||||
0x3991D6, 0x398353, 0x39F49C, 0x845F8B, 0xBDF928, 0x3B1FF8,
|
||||
0x97FFDE, 0x05980F, 0xEF2F11, 0x8B5A0A, 0x6D1F6D, 0x367ECF,
|
||||
0x27CB09, 0xB74F46, 0x3F669E, 0x5FEA2D, 0x7527BA, 0xC7EBE5,
|
||||
0xF17B3D, 0x0739F7, 0x8A5292, 0xEA6BFB, 0x5FB11F, 0x8D5D08,
|
||||
0x560330, 0x46FC7B, 0x6BABF0, 0xCFBC20, 0x9AF436, 0x1DA9E3,
|
||||
0x91615E, 0xE61B08, 0x659985, 0x5F14A0, 0x68408D, 0xFFD880,
|
||||
0x4D7327, 0x310606, 0x1556CA, 0x73A8C9, 0x60E27B, 0xC08C6B,
|
||||
];
|
||||
|
||||
# π/2 in pieces of 24 bits, as many as a double needs.
|
||||
const reduce_pio2_pieces: f64[5] = [
|
||||
1.570796251296997, 7.549789415861596e-08, 5.390302529957765e-15,
|
||||
3.282003415807913e-22, 1.270655753080676e-29,
|
||||
];
|
||||
|
||||
# 2^24 and 2^-24.
|
||||
const reduce_two_24: f64 = 16777216.0;
|
||||
const reduce_two_minus_24: f64 = 5.9604644775390625e-08;
|
||||
|
||||
# One less than how many pieces of the product are formed at first, for
|
||||
# doubles: musl's jk. Two more than would seem needed, so the test for bits
|
||||
# lost to cancelling works.
|
||||
const reduce_pieces: i32 = 4;
|
||||
|
||||
# The positive x[0]·2^e0 + x[1]·2^(e0 - 24) + ..., of `count` pieces of 24
|
||||
# bits each, less n·π/2, where n is returned modulo 8.
|
||||
def reduce_pio2_large(x: f64[3], e0: i32, count: i32): reduce_result {
|
||||
var iq: i32[20];
|
||||
var f: f64[20];
|
||||
var fq: f64[20];
|
||||
var q: f64[20];
|
||||
val jk := reduce_pieces;
|
||||
val jp := jk;
|
||||
|
||||
# jv is the first piece of 2/π needed: those before it make a multiple of 8
|
||||
# of x·(2/π). q0 is the exponent of q[0].
|
||||
val jx := count - 1;
|
||||
var jv := (e0 - 3) / 24;
|
||||
if jv < 0 {
|
||||
jv = 0;
|
||||
}
|
||||
var q0 := e0 - 24 * (jv + 1);
|
||||
|
||||
# f[0] to f[jx + jk] are the pieces of 2/π from jv - jx to jv + jk.
|
||||
var j := jv - jx;
|
||||
val m := jx + jk;
|
||||
var i: i32 = 0;
|
||||
while i <= m {
|
||||
f[i] = j < 0 ? 0.0 : reduce_2_over_pi[j] as f64;
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
|
||||
# q[0] to q[jk], the pieces of the product.
|
||||
var fw: f64 = 0.0;
|
||||
i = 0;
|
||||
while i <= jk {
|
||||
fw = 0.0;
|
||||
j = 0;
|
||||
while j <= jx {
|
||||
fw += x[j] * f[jx + i - j];
|
||||
j++;
|
||||
}
|
||||
q[i] = fw;
|
||||
i++;
|
||||
}
|
||||
|
||||
var jz := jk;
|
||||
var z: f64 = 0.0;
|
||||
var n: i32 = 0;
|
||||
var ih: i32 = 0;
|
||||
var k: i32 = 0;
|
||||
while true {
|
||||
# q[] into iq[], 24 bits each, from the last.
|
||||
i = 0;
|
||||
j = jz;
|
||||
z = q[jz];
|
||||
while j > 0 {
|
||||
fw = ((reduce_two_minus_24 * z) as i32) as f64;
|
||||
iq[i] = (z - reduce_two_24 * fw) as i32;
|
||||
z = q[j - 1] + fw;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
|
||||
# n is the integer part, modulo 8, and z the fraction.
|
||||
z = scalbn(z, q0);
|
||||
z -= 8.0 * floor_double(z * 0.125);
|
||||
n = z as i32;
|
||||
z -= n as f64;
|
||||
ih = 0;
|
||||
if q0 > 0 {
|
||||
# The last bits of the integer are in iq[jz - 1].
|
||||
i = iq[jz - 1] >> ((24 - q0) as u64);
|
||||
n += i;
|
||||
iq[jz - 1] -= i << ((24 - q0) as u64);
|
||||
ih = iq[jz - 1] >> ((23 - q0) as u64);
|
||||
} else if q0 == 0 {
|
||||
ih = iq[jz - 1] >> 23;
|
||||
} else if z >= 0.5 {
|
||||
ih = 2;
|
||||
}
|
||||
|
||||
# At a half or more, the fraction is 1 less it, and n one more, so the
|
||||
# result is negative.
|
||||
if ih > 0 {
|
||||
n += 1;
|
||||
var carry: i32 = 0;
|
||||
i = 0;
|
||||
while i < jz {
|
||||
j = iq[i];
|
||||
if carry == 0 {
|
||||
if j != 0 {
|
||||
carry = 1;
|
||||
iq[i] = 0x1000000 - j;
|
||||
}
|
||||
} else {
|
||||
iq[i] = 0xFFFFFF - j;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if q0 == 1 {
|
||||
iq[jz - 1] &= 0x7FFFFF;
|
||||
} else if q0 == 2 {
|
||||
iq[jz - 1] &= 0x3FFFFF;
|
||||
}
|
||||
if ih == 2 {
|
||||
z = 1.0 - z;
|
||||
if carry != 0 {
|
||||
z -= scalbn(1.0, q0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Where the fraction's leading bits all cancelled, more pieces of 2/π are
|
||||
# taken, and it is worked again.
|
||||
if z == 0.0 {
|
||||
j = 0;
|
||||
i = jz - 1;
|
||||
while i >= jk {
|
||||
j |= iq[i];
|
||||
i--;
|
||||
}
|
||||
if j == 0 {
|
||||
k = 1;
|
||||
while iq[jk - k] == 0 {
|
||||
k++;
|
||||
}
|
||||
i = jz + 1;
|
||||
while i <= jz + k {
|
||||
f[jx + i] = reduce_2_over_pi[jv + i] as f64;
|
||||
fw = 0.0;
|
||||
j = 0;
|
||||
while j <= jx {
|
||||
fw += x[j] * f[jx + i - j];
|
||||
j++;
|
||||
}
|
||||
q[i] = fw;
|
||||
i++;
|
||||
}
|
||||
jz += k;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
# The pieces of the fraction, less those that are zero at the end.
|
||||
if z == 0.0 {
|
||||
jz -= 1;
|
||||
q0 -= 24;
|
||||
while iq[jz] == 0 {
|
||||
jz--;
|
||||
q0 -= 24;
|
||||
}
|
||||
} else {
|
||||
z = scalbn(z, -q0);
|
||||
if z >= reduce_two_24 {
|
||||
fw = ((reduce_two_minus_24 * z) as i32) as f64;
|
||||
iq[jz] = (z - reduce_two_24 * fw) as i32;
|
||||
jz += 1;
|
||||
q0 += 24;
|
||||
iq[jz] = fw as i32;
|
||||
} else {
|
||||
iq[jz] = z as i32;
|
||||
}
|
||||
}
|
||||
|
||||
# The pieces as doubles.
|
||||
fw = scalbn(1.0, q0);
|
||||
i = jz;
|
||||
while i >= 0 {
|
||||
q[i] = fw * (iq[i] as f64);
|
||||
fw *= reduce_two_minus_24;
|
||||
i--;
|
||||
}
|
||||
|
||||
# The fraction times π/2.
|
||||
i = jz;
|
||||
while i >= 0 {
|
||||
fw = 0.0;
|
||||
k = 0;
|
||||
while k <= jp && k <= jz - i {
|
||||
fw += reduce_pio2_pieces[k] * q[i + k];
|
||||
k++;
|
||||
}
|
||||
fq[jz - i] = fw;
|
||||
i--;
|
||||
}
|
||||
|
||||
# The sum as two doubles.
|
||||
fw = 0.0;
|
||||
i = jz;
|
||||
while i >= 0 {
|
||||
fw += fq[i];
|
||||
i--;
|
||||
}
|
||||
val hi := ih == 0 ? fw : -fw;
|
||||
fw = fq[0] - fw;
|
||||
i = 1;
|
||||
while i <= jz {
|
||||
fw += fq[i];
|
||||
i++;
|
||||
}
|
||||
val lo := ih == 0 ? fw : -fw;
|
||||
return reduce_result { n = n & 7, hi = hi, lo = lo };
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
#! @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<T>(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<T>(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);
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
#! @file trig.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the trigonometric functions: sine, cosine, and tangent, of an angle
|
||||
#! in radians. Each reduces its argument by multiples of π/2 to within about
|
||||
#! π/4 of 0, with `reduce_pio2`, exactly however large it is, and works the
|
||||
#! reduced angle with a polynomial.
|
||||
#!
|
||||
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
|
||||
#! under the MIT license. Its `sin`, `cos`, and `tan` are from FreeBSD's
|
||||
#! s_sin.c, s_cos.c, and s_tan.c, which say:
|
||||
#!
|
||||
#! 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.
|
||||
#!
|
||||
#! Its kernels `__sin` and `__cos` are from k_sin.c and k_cos.c, which say:
|
||||
#!
|
||||
#! Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
||||
#!
|
||||
#! Developed at SunSoft, a Sun Microsystems, Inc. business.
|
||||
#! Permission to use, copy, modify, and distribute this
|
||||
#! software is freely granted, provided that this notice
|
||||
#! is preserved.
|
||||
#!
|
||||
#! And `__tan` is from k_tan.c, which says:
|
||||
#!
|
||||
#! Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
|
||||
#!
|
||||
#! Permission to use, copy, modify, and distribute this
|
||||
#! software is freely granted, provided that this notice
|
||||
#! is preserved.
|
||||
#!
|
||||
#! Each is within 1 ulp of the true value for `f64`. An `f32` is worked in
|
||||
#! `f64` and rounded once more.
|
||||
|
||||
module std::math;
|
||||
|
||||
|
||||
#! The sine of an angle.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The angle, in radians.
|
||||
#! @returns sin x, from -1 to 1: `-0` for `-0`, and NaN for infinity and NaN.
|
||||
pub def sin<T>(x: T): T where T: real {
|
||||
return sin_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The cosine of an angle.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The angle, in radians.
|
||||
#! @returns cos x, from -1 to 1: 1 for `±0`, and NaN for infinity and NaN.
|
||||
pub def cos<T>(x: T): T where T: real {
|
||||
return cos_double(x as f64) as T;
|
||||
}
|
||||
|
||||
#! The tangent of an angle.
|
||||
#! @tparam T The type of real.
|
||||
#! @param x The angle, in radians.
|
||||
#! @returns tan x: `-0` for `-0`, and NaN for infinity and NaN. No double is an
|
||||
#! odd multiple of π/2, so it is always finite, at most about 2.1e18
|
||||
#! in magnitude.
|
||||
pub def tan<T>(x: T): T where T: real {
|
||||
return tan_double(x as f64) as T;
|
||||
}
|
||||
|
||||
|
||||
# sin x, cos x, and tan x, from musl's sin.c, cos.c, and tan.c. Each reduces x
|
||||
# to y = x - n·π/2, and as n modulo 4 is 0, 1, 2, or 3, sin x is sin y, cos y,
|
||||
# -sin y, or -cos y; cos x is cos y, -sin y, -cos y, or sin y; and tan x is
|
||||
# tan y or -1 / tan y.
|
||||
|
||||
def sin_double(x: f64): f64 {
|
||||
val ix := high_word(x) & 0x7FFFFFFF;
|
||||
# Up to about π/4, where x needs no reducing, and below 2^-26, where sin x
|
||||
# rounds to x.
|
||||
if ix <= 0x3FE921FB {
|
||||
return x if ix < 0x3E500000;
|
||||
return trig_sin_kernel(x, 0.0, false);
|
||||
}
|
||||
# Infinity and NaN.
|
||||
return x - x if ix >= 0x7FF00000;
|
||||
val y := reduce_pio2(x);
|
||||
val n := y.n & 3;
|
||||
return trig_sin_kernel(y.hi, y.lo, true) if n == 0;
|
||||
return trig_cos_kernel(y.hi, y.lo) if n == 1;
|
||||
return -trig_sin_kernel(y.hi, y.lo, true) if n == 2;
|
||||
return -trig_cos_kernel(y.hi, y.lo);
|
||||
}
|
||||
|
||||
def cos_double(x: f64): f64 {
|
||||
val ix := high_word(x) & 0x7FFFFFFF;
|
||||
# Up to about π/4, and below 2^-27·√2, where cos x rounds to 1.
|
||||
if ix <= 0x3FE921FB {
|
||||
return 1.0 if ix < 0x3E46A09E;
|
||||
return trig_cos_kernel(x, 0.0);
|
||||
}
|
||||
return x - x if ix >= 0x7FF00000;
|
||||
val y := reduce_pio2(x);
|
||||
val n := y.n & 3;
|
||||
return trig_cos_kernel(y.hi, y.lo) if n == 0;
|
||||
return -trig_sin_kernel(y.hi, y.lo, true) if n == 1;
|
||||
return -trig_cos_kernel(y.hi, y.lo) if n == 2;
|
||||
return trig_sin_kernel(y.hi, y.lo, true);
|
||||
}
|
||||
|
||||
def tan_double(x: f64): f64 {
|
||||
val ix := high_word(x) & 0x7FFFFFFF;
|
||||
# Up to about π/4, and below 2^-27, where tan x rounds to x.
|
||||
if ix <= 0x3FE921FB {
|
||||
return x if ix < 0x3E400000;
|
||||
return trig_tan_kernel(x, 0.0, false);
|
||||
}
|
||||
return x - x if ix >= 0x7FF00000;
|
||||
val y := reduce_pio2(x);
|
||||
return trig_tan_kernel(y.hi, y.lo, (y.n & 1) != 0);
|
||||
}
|
||||
|
||||
|
||||
# The kernels, from musl's __sin.c, __cos.c, and __tan.c: each of x + y, where
|
||||
# |x + y| is at most about π/4 and y is below half an ulp of x.
|
||||
|
||||
# sin x = x + S1·x^3 + ... + S6·x^13, within 2^-58, on [0, π/4].
|
||||
const trig_s1: f64 = -0.16666666666666632;
|
||||
const trig_s2: f64 = 0.00833333333332249;
|
||||
const trig_s3: f64 = -0.0001984126982985795;
|
||||
const trig_s4: f64 = 2.7557313707070068e-06;
|
||||
const trig_s5: f64 = -2.5050760253406863e-08;
|
||||
const trig_s6: f64 = 1.58969099521155e-10;
|
||||
|
||||
# sin(x + y), where y is taken as 0 unless `tail` is. Its polynomial doesn't
|
||||
# keep the sign of `-0`, which callers return themselves.
|
||||
def trig_sin_kernel(x: f64, y: f64, tail: bool): f64 {
|
||||
val z := x * x;
|
||||
val w := z * z;
|
||||
val r := trig_s2 + z * (trig_s3 + z * trig_s4) + z * w * (trig_s5 + z * trig_s6);
|
||||
val v := z * x;
|
||||
return x + v * (trig_s1 + z * r) if !tail;
|
||||
# sin(x + y) is about sin x + (1 - x^2/2)·y.
|
||||
return x - ((z * (0.5 * y - v * r) - y) - v * trig_s1);
|
||||
}
|
||||
|
||||
# cos x = 1 - x^2/2 + C1·x^4 + ... + C6·x^14, within 2^-58, on [0, π/4].
|
||||
const trig_c1: f64 = 0.0416666666666666;
|
||||
const trig_c2: f64 = -0.001388888888887411;
|
||||
const trig_c3: f64 = 2.480158728947673e-05;
|
||||
const trig_c4: f64 = -2.7557314351390663e-07;
|
||||
const trig_c5: f64 = 2.087572321298175e-09;
|
||||
const trig_c6: f64 = -1.1359647557788195e-11;
|
||||
|
||||
# cos(x + y). It is about w + ((1 - w - x^2/2) + (r - x·y)), where w is
|
||||
# 1 - x^2/2 rounded, and the first term in parentheses is what that rounding
|
||||
# lost, and the second the rest of the polynomial and the tail's part, as
|
||||
# cos(x + y) is about cos x - x·y.
|
||||
def trig_cos_kernel(x: f64, y: f64): f64 {
|
||||
val z := x * x;
|
||||
var w := z * z;
|
||||
val r := z * (trig_c1 + z * (trig_c2 + z * trig_c3)) + w * w * (trig_c4 + z * (trig_c5 + z * trig_c6));
|
||||
val hz := 0.5 * z;
|
||||
w = 1.0 - hz;
|
||||
return w + (((1.0 - w) - hz) + (z * r - x * y));
|
||||
}
|
||||
|
||||
# tan x = x + T[0]·x^3 + ... + T[12]·x^27, within 2^-59.2, on [0, 0.67434].
|
||||
const trig_t: f64[13] = [
|
||||
0.3333333333333341, 0.13333333333320124, 0.05396825397622605,
|
||||
0.021869488294859542, 0.0088632398235993, 0.0035920791075913124,
|
||||
0.0014562094543252903, 0.0005880412408202641, 0.0002464631348184699,
|
||||
7.817944429395571e-05, 7.140724913826082e-05, -1.8558637485527546e-05,
|
||||
2.590730518636337e-05,
|
||||
];
|
||||
|
||||
# π/4, as a high part and a low part.
|
||||
const trig_pio4: f64 = 0.7853981633974483;
|
||||
const trig_pio4_low: f64 = 3.061616997868383e-17;
|
||||
|
||||
# tan(x + y), or -1 / tan(x + y) where `odd` is. Its polynomial doesn't keep the
|
||||
# sign of `-0`, which callers return themselves.
|
||||
def trig_tan_kernel(x0: f64, y0: f64, odd: bool): f64 {
|
||||
var x := x0;
|
||||
var y := y0;
|
||||
val hx := high_word(x);
|
||||
# From about 0.6744 up, tan x is worked as tan(π/4 - x'), where x' is
|
||||
# π/4 - |x|, which is (1 - tan x') / (1 + tan x').
|
||||
val big := (hx & 0x7FFFFFFF) >= 0x3FE59428;
|
||||
val sign := (hx >> 31) != 0;
|
||||
if big {
|
||||
if sign {
|
||||
x = -x;
|
||||
y = -y;
|
||||
}
|
||||
x = (trig_pio4 - x) + (trig_pio4_low - y);
|
||||
y = 0.0;
|
||||
}
|
||||
val z := x * x;
|
||||
var w := z * z;
|
||||
# x^5 times the polynomial's terms from T[1], in two halves: the odd ones in
|
||||
# w = x^4, and the even ones, times z = x^2.
|
||||
var r := trig_t[1] + w * (trig_t[3] + w * (trig_t[5] + w * (trig_t[7] + w * (trig_t[9] + w * trig_t[11]))));
|
||||
var v := z * (trig_t[2] + w * (trig_t[4] + w * (trig_t[6] + w * (trig_t[8] + w * (trig_t[10] + w * trig_t[12])))));
|
||||
var s := z * x;
|
||||
# tan(x + y) is about tan x + (1 + x^2)·y.
|
||||
r = y + z * (s * (r + v) + y) + s * trig_t[0];
|
||||
w = x + r;
|
||||
if big {
|
||||
s = odd ? -1.0 : 1.0;
|
||||
v = s - 2.0 * (x + (r - w * w / (w + s)));
|
||||
return sign ? -v : v;
|
||||
}
|
||||
return w if !odd;
|
||||
# -1 / (x + r), which dividing alone would give within 2 ulp: w0 + v is x + r,
|
||||
# and a0 is -1 / w, each with its low word cleared, so the products are exact.
|
||||
val w0 := with_low_word(w, 0);
|
||||
v = r - (w0 - x);
|
||||
val a := -1.0 / w;
|
||||
val a0 := with_low_word(a, 0);
|
||||
return a0 + a * (1.0 + a0 * w0 + a0 * v);
|
||||
}
|
||||
Reference in new issue
Block a user