Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-std

Source of release 0.7.5

0.7.5 / src / math / pow.lucb

#==============================================================================================
#
#   pow - x raised to the power y
#
#   DESCRIPTION:
#       Finite y and a normal x first try the fast path at the end of this file, which
#       returns only results it proves correctly rounded. The method is ARM optimized-routines' pow (as in musl): x^y = e^(y·log x) with
#       log x as a pair good to about 2^-68 (log.lucb), y times it as an exact product plus
#       the low part's, and e^(that pair) from exp.lucb's pair, which keeps the low part
#       of its argument. An error of 2^-63 in y·log x moves the result 2^-63 relative, so a
#       result is within 0.52 ulp and an exactly representable result is exact. The special
#       cases are C99 Annex F.9.4.4's. Written anew from the method.
#
#==============================================================================================

## 0 when `y` (finite, nonzero) is not an integer, 1 when it is odd, 2 when even.
func integer_kind(y: f64) -> u32:
    let exponent = (i64)((y.bits() >> 52) & 0x7ff) - 1023
    if exponent < 0:
        return 0
    if exponent > 52:
        return 2
    let fraction_bits = (u64)(52 - exponent)
    let mantissa = y.bits() & 0x000fffffffffffff
    if fraction_bits > 0 and (mantissa & (((u64)1 << fraction_bits) - 1)) != 0:
        return 0
    if exponent == 0:
        return 1
    return 1 if (mantissa >> fraction_bits) & 1 == 1 else 2

## `x` raised to `y`, every special value as Annex F gives it.
func pow_accurate(x: f64, y: f64) -> f64:
    let ix = x.bits()
    let iy = y.bits()
    let ax = ix & 0x7fffffffffffffff
    let ay = iy & 0x7fffffffffffffff
    if ay == 0 or ix == 0x3ff0000000000000:
        # anything to the zeroth power, and 1 to any power, NaN included, is 1
        return 1.0
    if ax > 0x7ff0000000000000 or ay > 0x7ff0000000000000:
        return x + y
    if ay == 0x7ff0000000000000:
        if ax == 0x3ff0000000000000:
            return 1.0
        return 0.0 if (ax < 0x3ff0000000000000) == (iy >> 63 == 0) else infinity
    let kind = integer_kind(y)
    let negative = ix >> 63 == 1 and kind == 1
    if ax == 0 or ax == 0x7ff0000000000000:
        # zero and infinity: the magnitude is 0 or infinity, by the signs of y and log x
        let large = (ax == 0) == (iy >> 63 == 1)
        let magnitude = infinity if large else 0.0
        return -magnitude if negative else magnitude
    if ix >> 63 == 1 and kind == 0:
        return f64.bits(0x7ff8000000000000)
    let ab = f64.bits(ax)
    var magnitude: f64
    if ay >= 0x43f0000000000000:
        # |y| >= 2^64 against |log x| >= 2^-54: the exponent is past either end
        magnitude = infinity if (ax > 0x3ff0000000000000) == (iy >> 63 == 0) else 0.0
    else:
        let (lh, ll) = log_pair(ab)
        let (ph, pe) = two_product(y, lh)
        let (th, tl) = fast_two_sum(ph, pe + y * ll)
        if th > 709.8:
            magnitude = infinity
        elif th < -746.0:
            magnitude = 0.0
        else:
            let (vh, vl, m) = exp_pair(th, tl)
            magnitude = scale_pair(vh, vl, m)
    return -magnitude if negative else magnitude

# mark: The fast path =========================================================================

# For a normal x (negative only with an integer y), finite y and |y·log x| < 708, so that
# the result is normal: log.lucb's fast log |x| = lh + ll, within 2^-70 of itself, t = y·lh
# exactly as a pair plus y·ll, and e^t by exp.lucb's table pair with t's low part folded
# into the reduction (two_sum again), so that r's low part stays below 2^-62 and exp's
# budget holds. An error of |t|·2^-69 in t moves e^t by as much relatively, and exp's own
# is under 2^-67 of its value in [1, 2]; the test allows 2^-62 + |t|·2^-65 there, so a
# fast return is the correctly rounded value. An exact result is never a halfway case and
# passes; a halfway one never does and takes the method above, as do the special values.

func pow_value(x: f64, y: f64) -> f64:
    let ax = abs(x)
    if log_is_normal(ax) and (y.bits() & 0x7fffffffffffffff) < 0x7ff0000000000000:
        let kind = integer_kind(y) if x < 0.0 else 2
        if kind != 0:
            let (lh, ll) = log_fast(ax)
            let (th, te) = two_product(y, lh)
            if abs(th) < 708.0:
                let tl = te + y * ll
                let kd = (th * f64.bits(inverse_ln2_128) + round_shift) - round_shift
                let p = kd * f64.bits(ln2_128_low)
                let (s, se) = two_sum(tl, -p)
                let (rh, re) = two_sum(th - kd * f64.bits(ln2_128_high), s)
                let k = (i64)kd
                let (h, l) = exp_table_pair(k, rh, (re + se) - kd.mul_add(f64.bits(ln2_128_low), -p))
                let bound = 2.168404344971009e-19 + abs(th) * 2.710505431213761e-20
                let low = h + (l - bound)
                if low == h + (l + bound):
                    let magnitude = low * power_of_two(k >> 7)
                    return -magnitude if kind == 1 else magnitude
    return pow_accurate(x, y)