#! @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(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); }