#! @file numbers.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the operations of one value the machine gives that no operator of the #! language writes: a square root, and counting and reversing a word's bits and #! bytes. The machine runs them, and the optimizer folds them, through these, so #! the two agree with each other and with an executable, whose instructions give #! the same. `std::math` reaches them through its intrinsics, which a compiler #! building this one need not know. #! #! `root_of` is musl's `sqrt`, from src/math/sqrt.c of musl 1.2.5, which is #! Copyright © 2005-2020 Rich Felker, et al., under the MIT license. module sakura::compiler; # The reciprocal square roots `root_of` begins from, by the lowest bit of the # exponent and the top six of the significand: musl's `__rsqrt_tab`. const reciprocal_roots: u16[128] = [ 0xb451, 0xb2f0, 0xb196, 0xb044, 0xaef9, 0xadb6, 0xac79, 0xab43, 0xaa14, 0xa8eb, 0xa7c8, 0xa6aa, 0xa592, 0xa480, 0xa373, 0xa26b, 0xa168, 0xa06a, 0x9f70, 0x9e7b, 0x9d8a, 0x9c9d, 0x9bb5, 0x9ad1, 0x99f0, 0x9913, 0x983a, 0x9765, 0x9693, 0x95c4, 0x94f8, 0x9430, 0x936b, 0x92a9, 0x91ea, 0x912e, 0x9075, 0x8fbe, 0x8f0a, 0x8e59, 0x8daa, 0x8cfe, 0x8c54, 0x8bac, 0x8b07, 0x8a64, 0x89c4, 0x8925, 0x8889, 0x87ee, 0x8756, 0x86c0, 0x862b, 0x8599, 0x8508, 0x8479, 0x83ec, 0x8361, 0x82d8, 0x8250, 0x81c9, 0x8145, 0x80c2, 0x8040, 0xff02, 0xfd0e, 0xfb25, 0xf947, 0xf773, 0xf5aa, 0xf3ea, 0xf234, 0xf087, 0xeee3, 0xed47, 0xebb3, 0xea27, 0xe8a3, 0xe727, 0xe5b2, 0xe443, 0xe2dc, 0xe17a, 0xe020, 0xdecb, 0xdd7d, 0xdc34, 0xdaf1, 0xd9b3, 0xd87b, 0xd748, 0xd61a, 0xd4f1, 0xd3cd, 0xd2ad, 0xd192, 0xd07b, 0xcf69, 0xce5b, 0xcd51, 0xcc4a, 0xcb48, 0xca4a, 0xc94f, 0xc858, 0xc764, 0xc674, 0xc587, 0xc49d, 0xc3b7, 0xc2d4, 0xc1f4, 0xc116, 0xc03c, 0xbf65, 0xbe90, 0xbdbe, 0xbcef, 0xbc23, 0xbb59, 0xba91, 0xb9cc, 0xb90a, 0xb84a, 0xb78c, 0xb6d0, 0xb617, 0xb560, ]; # `a * b / 2^32` of the low 32 bits of each, rounded down. def high_of_32(a: u64, b: u64): u64 { return ((a & 0xFFFFFFFF) * (b & 0xFFFFFFFF)) >> 32; } # `a * b / 2^64`, short of it by less than 3. def high_of_64(a: u64, b: u64): u64 { val ahi := a >> 32; val alo := a & 0xFFFFFFFF; val bhi := b >> 32; val blo := b & 0xFFFFFFFF; return ahi * bhi + ((ahi * blo) >> 32) + ((alo * bhi) >> 32); } #! The square root of a real, correctly rounded, as IEEE 754 defines it and #! `sqrtsd` gives it: the root of `-0` is `-0`, of infinity infinity, and of #! what is below zero, or not a number, not a number. #! @param x The real. #! @returns Its square root. pub def root_of(x: f64): f64 { var ix := bits_of_real(x); var top := ix >> 52; # Below the least normal, infinite, not a number, or below zero. if top - 1 >= 0x7FE { return x if ix * 2 == 0 || ix == 0x7FF0000000000000; if ix > 0x7FF0000000000000 { return (x - x) / (x - x); } # A subnormal, made normal. ix = bits_of_real(x * 4503599627370496.0); top = (ix >> 52) - 52; } # x is 4^e times m, with m from 1 to 4 held as 2.62 bits, and 2^e is the # exponent of the root. val even := top & 1; var m := (ix << 11) | 0x8000000000000000; if even != 0 { m >>= 1; } top = (top + 0x3FF) >> 1; # r is near 1 / sqrt(m) and s near sqrt(m): from the table, then two steps of # Goldschmidt's iteration in 32 bits, and one in 64. val three: u64 = 0xC0000000; var r := (reciprocal_roots[(ix >> 46) % 128] as u64) << 16; var s := high_of_32(m >> 32, r); var d := high_of_32(s, r); var u := three - d; r = (high_of_32(r, u) << 1) & 0xFFFFFFFF; s = (high_of_32(s, u) << 1) & 0xFFFFFFFF; d = high_of_32(s, r); u = three - d; r = (high_of_32(r, u) << 1) & 0xFFFFFFFF; r = r << 32; s = high_of_64(m, r); d = high_of_64(s, r); u = (three << 32) - d; s = high_of_64(s, u); s = (s - 2) >> 9; # s is below sqrt(m) by less than 2^-52 and a little more, so the root is s or # the next, as (2^52 s + 1/2)^2 compares with 2^104 m. val d0 := (m << 42) - s * s; val d1 := s - d0; s += d1 >> 63; s &= 0x000FFFFFFFFFFFFF; s |= top << 52; return real_of(s); } #! How many bits of a word are 1. #! @param word The word. #! @returns How many. pub def ones_of(word: u64): u64 { var x := word - ((word >> 1) & 0x5555555555555555); x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F; return (x * 0x0101010101010101) >> 56; } #! How many 0 bits lead a word, from its top. #! @param word The word. #! @returns How many: 64 where it is zero. pub def leading_zeros_of(word: u64): u64 { return 64 if word == 0; var x := word; var count: u64 = 0; if (x & 0xFFFFFFFF00000000) == 0 { count += 32; x <<= 32; } if (x & 0xFFFF000000000000) == 0 { count += 16; x <<= 16; } if (x & 0xFF00000000000000) == 0 { count += 8; x <<= 8; } if (x & 0xF000000000000000) == 0 { count += 4; x <<= 4; } if (x & 0xC000000000000000) == 0 { count += 2; x <<= 2; } if (x & 0x8000000000000000) == 0 { count += 1; } return count; } #! How many 0 bits trail a word, from its bottom. #! @param word The word. #! @returns How many: 64 where it is zero. pub def trailing_zeros_of(word: u64): u64 { return 64 if word == 0; return ones_of((word & (0 - word)) - 1); } #! A word with its bytes in the reverse order. #! @param word The word. #! @returns The word reversed. pub def bytes_swapped(word: u64): u64 { var x := ((word >> 8) & 0x00FF00FF00FF00FF) | ((word & 0x00FF00FF00FF00FF) << 8); x = ((x >> 16) & 0x0000FFFF0000FFFF) | ((x & 0x0000FFFF0000FFFF) << 16); return (x >> 32) | (x << 32); }