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