593 lines
15 KiB
Plaintext
593 lines
15 KiB
Plaintext
#! @file float.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains how an executable formats a float, as the virtual machine does.
|
|
#!
|
|
#! Formats a float as `machine__format_float` of `sources/machine.c` does with
|
|
#! the C library: whole numbers below 1e15 as integers, and otherwise the
|
|
#! shortest `%.*g` text, from 1 to 17 digits, that reads back as the same value.
|
|
#! The digits are exact, from arithmetic on big integers, rounded half to even
|
|
#! as `printf` rounds them, and reading back rounds to the nearest double as
|
|
#! `strtod` does.
|
|
|
|
module runtime when backend == native;
|
|
|
|
# Big integers are words in memory: their number of limbs, then the limbs, each
|
|
# holding 32 bits from the least significant.
|
|
val limbs := 128u;
|
|
val limb := 4294967296u;
|
|
|
|
var big_n: *mut u64;
|
|
var big_d: *mut u64;
|
|
var big_c: *mut u64;
|
|
var big_t: *mut u64;
|
|
var big_u: *mut u64; # The midpoint above the value, scaled as it is.
|
|
var big_l: *mut u64; # The midpoint below it.
|
|
var big_m: *mut u64; # A margin around the midpoints, for single precision.
|
|
var big_q: *mut u64;
|
|
|
|
# Room for rounding a decimal exactly, apart from the digits being checked.
|
|
var exact_n: *mut u64;
|
|
var exact_d: *mut u64;
|
|
var exact_c: *mut u64;
|
|
var exact_t: *mut u64;
|
|
|
|
# The digits of the value, which are 18 so any precision can be rounded, and
|
|
# whether any digit after them would not be zero.
|
|
var digits: *mut u8;
|
|
var sticky: bool;
|
|
|
|
var float_bits: u64;
|
|
|
|
def big(): *mut u64 {
|
|
return acquire((limbs + 1) * 8) as *mut u64;
|
|
}
|
|
|
|
def big_trim(a: *mut u64): void {
|
|
while a[0] > 0 && a[a[0]] == 0 {
|
|
a[0] = a[0] - 1;
|
|
}
|
|
}
|
|
|
|
def big_set(a: *mut u64, value: u64): void {
|
|
a[0] = 2;
|
|
a[1] = value % limb;
|
|
a[2] = value / limb;
|
|
big_trim(a);
|
|
}
|
|
|
|
def big_copy(a: *mut u64, b: *mut u64): void {
|
|
for i in 0..b[0] + 1 {
|
|
a[i] = b[i];
|
|
}
|
|
}
|
|
|
|
def big_mul_small(a: *mut u64, factor: u64): void {
|
|
var carry := 0u;
|
|
for i in 1..a[0] + 1 {
|
|
val product := a[i] * factor + carry;
|
|
a[i] = product % limb;
|
|
carry = product / limb;
|
|
}
|
|
while carry > 0 {
|
|
a[0] = a[0] + 1;
|
|
a[a[0]] = carry % limb;
|
|
carry = carry / limb;
|
|
}
|
|
}
|
|
|
|
def big_mul_ten(a: *mut u64, power: u64): void {
|
|
var left := power;
|
|
while left >= 9 {
|
|
big_mul_small(a, 1000000000);
|
|
left = left - 9;
|
|
}
|
|
while left > 0 {
|
|
big_mul_small(a, 10);
|
|
left--;
|
|
}
|
|
}
|
|
|
|
def big_shl(a: *mut u64, bits: u64): void {
|
|
if a[0] == 0 {
|
|
return;
|
|
}
|
|
val words := bits / 32;
|
|
val shift := bits % 32;
|
|
var i := a[0] + words + 1;
|
|
while i > words {
|
|
val index := i - words;
|
|
val high := index <= a[0] ? a[index] : 0;
|
|
val low := index - 1 >= 1 && index - 1 <= a[0] ? a[index - 1] : 0;
|
|
a[i] = shift == 0 ? high : ((high << shift) | (low >> (32 - shift))) % limb;
|
|
i--;
|
|
}
|
|
while i > 0 {
|
|
a[i] = 0;
|
|
i--;
|
|
}
|
|
a[0] = a[0] + words + 1;
|
|
big_trim(a);
|
|
}
|
|
|
|
def big_shr1(a: *mut u64): void {
|
|
for i in 1..a[0] + 1 {
|
|
val high := i < a[0] ? a[i + 1] : 0;
|
|
a[i] = (a[i] >> 1) | ((high & 1) << 31);
|
|
}
|
|
big_trim(a);
|
|
}
|
|
|
|
def big_compare(a: *mut u64, b: *mut u64): i64 {
|
|
if a[0] != b[0] {
|
|
return a[0] < b[0] ? -1 : 1;
|
|
}
|
|
var i := a[0];
|
|
while i > 0 {
|
|
if a[i] != b[i] {
|
|
return a[i] < b[i] ? -1 : 1;
|
|
}
|
|
i--;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
# Subtracts `b` from `a`, which is at least `b`.
|
|
def big_sub(a: *mut u64, b: *mut u64): void {
|
|
var borrow := 0u;
|
|
for i in 1..a[0] + 1 {
|
|
val take := (i <= b[0] ? b[i] : 0) + borrow;
|
|
if a[i] >= take {
|
|
a[i] = a[i] - take;
|
|
borrow = 0;
|
|
}
|
|
else {
|
|
a[i] = a[i] + limb - take;
|
|
borrow = 1;
|
|
}
|
|
}
|
|
big_trim(a);
|
|
}
|
|
|
|
def big_bits(a: *mut u64): u64 {
|
|
if a[0] == 0 {
|
|
return 0;
|
|
}
|
|
var top := a[a[0]];
|
|
var count := (a[0] - 1) * 32;
|
|
while top > 0 {
|
|
count++;
|
|
top = top >> 1;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
def bits_of(value: f64): u64 {
|
|
((&mut float_bits) as *mut f64)[0] = value;
|
|
return float_bits;
|
|
}
|
|
|
|
def float_of(bits: u64): f64 {
|
|
float_bits = bits;
|
|
return ((&mut float_bits) as *mut f64)[0];
|
|
}
|
|
|
|
def start_float(): void {
|
|
if big_n != null {
|
|
return;
|
|
}
|
|
big_n = big();
|
|
big_d = big();
|
|
big_c = big();
|
|
big_t = big();
|
|
big_u = big();
|
|
big_l = big();
|
|
big_m = big();
|
|
big_q = big();
|
|
exact_n = big();
|
|
exact_d = big();
|
|
exact_c = big();
|
|
exact_t = big();
|
|
digits = acquire(32);
|
|
}
|
|
|
|
# Writes the first 18 digits of a positive finite value, `n` units of two to
|
|
# `unit`, giving the decimal exponent of the first, so the value is `0.d1d2...`
|
|
# times ten to it plus one. The midpoints `u` and `l` and the margin `m`, in the
|
|
# same units, are scaled with the value, so they compare with it as it is written.
|
|
def decimal_digits(unit: i64, binary: i64): i64 {
|
|
big_set(big_d, 1);
|
|
if unit >= 0 {
|
|
big_shl(big_n, unit as u64);
|
|
big_shl(big_u, unit as u64);
|
|
big_shl(big_l, unit as u64);
|
|
big_shl(big_m, unit as u64);
|
|
}
|
|
else {
|
|
big_shl(big_d, (0 - unit) as u64);
|
|
}
|
|
|
|
# An estimate of the decimal exponent, corrected so `1 <= n / d < 10`.
|
|
var power := ((binary as f64) * 0.30102999566398114) as i64;
|
|
if (binary as f64) * 0.30102999566398114 < power as f64 {
|
|
power--;
|
|
}
|
|
if power >= 0 {
|
|
big_mul_ten(big_d, power as u64);
|
|
}
|
|
else {
|
|
scale_value(0 - power);
|
|
}
|
|
|
|
while big_compare(big_n, big_d) < 0 {
|
|
scale_value(1);
|
|
power--;
|
|
}
|
|
big_copy(big_t, big_d);
|
|
big_mul_small(big_t, 10);
|
|
while big_compare(big_n, big_t) >= 0 {
|
|
big_mul_small(big_d, 10);
|
|
big_copy(big_t, big_d);
|
|
big_mul_small(big_t, 10);
|
|
power++;
|
|
}
|
|
|
|
big_copy(big_t, big_n);
|
|
for i in 0..18 {
|
|
var digit: u8 = 0;
|
|
while big_compare(big_t, big_d) >= 0 {
|
|
big_sub(big_t, big_d);
|
|
digit++;
|
|
}
|
|
digits[i] = digit;
|
|
big_mul_small(big_t, 10);
|
|
}
|
|
sticky = big_t[0] != 0;
|
|
return power;
|
|
}
|
|
|
|
# Multiplies the value, its midpoints, and the margin by a power of ten.
|
|
def scale_value(power: i64): void {
|
|
big_mul_ten(big_n, power as u64);
|
|
big_mul_ten(big_u, power as u64);
|
|
big_mul_ten(big_l, power as u64);
|
|
big_mul_ten(big_m, power as u64);
|
|
}
|
|
|
|
# Rounds the digits to a precision half to even, into `rounded`, giving whether
|
|
# they carried into a new first digit.
|
|
var rounded: *mut u8;
|
|
|
|
def round_digits(precision: u64): bool {
|
|
for i in 0..precision {
|
|
rounded[i] = digits[i];
|
|
}
|
|
|
|
val next := digits[precision];
|
|
var rest := sticky;
|
|
for i in precision + 1..18 {
|
|
rest = rest || digits[i] != 0;
|
|
}
|
|
val up := next > 5 || (next == 5 && (rest || rounded[precision - 1] % 2 == 1));
|
|
if !up {
|
|
return false;
|
|
}
|
|
|
|
var i := precision;
|
|
while i > 0 {
|
|
i--;
|
|
if rounded[i] < 9 {
|
|
rounded[i] = rounded[i] + 1;
|
|
return false;
|
|
}
|
|
rounded[i] = 0;
|
|
}
|
|
rounded[0] = 1;
|
|
return true;
|
|
}
|
|
|
|
# The double nearest the decimal `0.r1r2...rp` times ten to `power` plus one,
|
|
# rounding half to even as `strtod` does.
|
|
def nearest_double(precision: u64, power: i64): f64 {
|
|
big_set(exact_n, 0);
|
|
for i in 0..precision {
|
|
big_mul_small(exact_n, 10);
|
|
val add := exact_c;
|
|
big_set(add, rounded[i] as u64);
|
|
add_into(exact_n, add);
|
|
}
|
|
big_set(exact_d, 1);
|
|
val scale := power - (precision as i64) + 1;
|
|
if scale >= 0 {
|
|
big_mul_ten(exact_n, scale as u64);
|
|
}
|
|
else {
|
|
big_mul_ten(exact_d, (0 - scale) as u64);
|
|
}
|
|
|
|
# The binary exponent puts 53 bits in the quotient, unless it would pass the
|
|
# least exponent, which leaves a subnormal with fewer.
|
|
var binary := big_bits(exact_n) as i64 - big_bits(exact_d) as i64 - 53;
|
|
if binary < -1074 {
|
|
binary = -1074;
|
|
}
|
|
while true {
|
|
big_copy(exact_t, exact_d);
|
|
if binary >= 0 {
|
|
big_shl(exact_t, binary as u64);
|
|
}
|
|
val numerator := exact_c;
|
|
big_copy(numerator, exact_n);
|
|
if binary < 0 {
|
|
big_shl(numerator, (0 - binary) as u64);
|
|
}
|
|
|
|
# The quotient must be below 2^53, and at least 2^52 unless subnormal.
|
|
val top := big();
|
|
big_copy(top, exact_t);
|
|
big_shl(top, 53);
|
|
if big_compare(numerator, top) >= 0 {
|
|
dispose(top as *mut u8, (limbs + 1) * 8);
|
|
binary++;
|
|
continue;
|
|
}
|
|
big_shr1(top);
|
|
if big_compare(numerator, top) < 0 && binary > -1074 {
|
|
dispose(top as *mut u8, (limbs + 1) * 8);
|
|
binary--;
|
|
continue;
|
|
}
|
|
|
|
# Divides a bit at a time, then rounds by the remainder.
|
|
big_shl(top, 1);
|
|
var quotient := 0u;
|
|
for i in 0..53 {
|
|
big_shr1(top);
|
|
quotient = quotient * 2;
|
|
if big_compare(numerator, top) >= 0 {
|
|
big_sub(numerator, top);
|
|
quotient++;
|
|
}
|
|
}
|
|
big_shl(numerator, 1);
|
|
val order := big_compare(numerator, exact_t);
|
|
dispose(top as *mut u8, (limbs + 1) * 8);
|
|
if order > 0 || (order == 0 && quotient % 2 == 1) {
|
|
quotient++;
|
|
}
|
|
if quotient == 9007199254740992 {
|
|
quotient = 4503599627370496;
|
|
binary++;
|
|
}
|
|
|
|
if quotient < 4503599627370496 {
|
|
return float_of(quotient);
|
|
}
|
|
val biased := binary + 1075;
|
|
if biased >= 2047 {
|
|
return float_of(0x7FF0000000000000);
|
|
}
|
|
return float_of(((biased as u64) << 52) | (quotient - 4503599627370496));
|
|
}
|
|
return 0.0;
|
|
}
|
|
|
|
def add_into(a: *mut u64, b: *mut u64): void {
|
|
var carry := 0u;
|
|
var i := 1u;
|
|
while i <= a[0] || i <= b[0] || carry > 0 {
|
|
val sum := (i <= a[0] ? a[i] : 0) + (i <= b[0] ? b[i] : 0) + carry;
|
|
a[i] = sum % limb;
|
|
carry = sum / limb;
|
|
if i > a[0] {
|
|
a[0] = i;
|
|
}
|
|
i++;
|
|
}
|
|
big_trim(a);
|
|
}
|
|
|
|
# Writes the rounded digits in the style `%g` chooses for the precision and the
|
|
# decimal exponent, without trailing zeros.
|
|
def put_general(into: *mut builder, precision: u64, power: i64): void {
|
|
var used := precision;
|
|
while used > 1 && rounded[used - 1] == 0 {
|
|
used--;
|
|
}
|
|
|
|
if power < (precision as i64) && power >= -4 {
|
|
if power < 0 {
|
|
put(into, '0');
|
|
put(into, '.');
|
|
for i in 0..(0 - power - 1) as u64 {
|
|
put(into, '0');
|
|
}
|
|
for i in 0..used {
|
|
put(into, '0' + rounded[i]);
|
|
}
|
|
return;
|
|
}
|
|
|
|
val whole := power as u64 + 1;
|
|
for i in 0..whole {
|
|
put(into, i < used ? '0' + rounded[i] : '0');
|
|
}
|
|
if used > whole {
|
|
put(into, '.');
|
|
for i in whole..used {
|
|
put(into, '0' + rounded[i]);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
put(into, '0' + rounded[0]);
|
|
if used > 1 {
|
|
put(into, '.');
|
|
for i in 1..used {
|
|
put(into, '0' + rounded[i]);
|
|
}
|
|
}
|
|
put(into, 'e');
|
|
put(into, power < 0 ? '-' : '+');
|
|
val magnitude := power < 0 ? (0 - power) as u64 : power as u64;
|
|
if magnitude < 10 {
|
|
put(into, '0');
|
|
}
|
|
put_unsigned(into, magnitude);
|
|
}
|
|
|
|
def put_float(into: *mut builder, value: f64, single: bool): void {
|
|
if value != value {
|
|
put(into, 'n');
|
|
put(into, 'a');
|
|
put(into, 'n');
|
|
return;
|
|
}
|
|
|
|
val bits := bits_of(value);
|
|
val sign := (bits >> 63) != 0;
|
|
val field := (bits >> 52) & 2047;
|
|
if field == 2047 {
|
|
if sign {
|
|
put(into, '-');
|
|
}
|
|
put(into, 'i');
|
|
put(into, 'n');
|
|
put(into, 'f');
|
|
return;
|
|
}
|
|
|
|
# Whole numbers are written without an exponent while they are exact.
|
|
if value > -1000000000000000.0 && value < 1000000000000000.0 && value == (value as i64) as f64 {
|
|
if sign && value == 0.0 {
|
|
put(into, '-');
|
|
}
|
|
put_signed(into, value as i64);
|
|
return;
|
|
}
|
|
|
|
start_float();
|
|
if rounded == null {
|
|
rounded = acquire(32);
|
|
}
|
|
if sign {
|
|
put(into, '-');
|
|
}
|
|
|
|
# The value in units a quarter of its gap, with the midpoints to the values
|
|
# beside it, where the one below is nearer at a power of two. A single is
|
|
# measured in its own gaps, with a margin of the gaps of doubles beside it.
|
|
val magnitude := float_of(bits & 0x7FFFFFFFFFFFFFFF);
|
|
var unit: i64 = 0;
|
|
var binary: i64 = 0;
|
|
var even: bool = false;
|
|
if single {
|
|
((&mut float_bits) as *mut f32)[0] = magnitude as f32;
|
|
val narrow := float_bits & 0xFFFFFFFF;
|
|
val exponent := (narrow >> 23) & 255;
|
|
val mantissa := exponent == 0 ? narrow & 0x7FFFFF : (narrow & 0x7FFFFF) | 0x800000;
|
|
val scale := exponent == 0 ? -149 : (exponent as i64) - 150;
|
|
big_set(big_n, mantissa);
|
|
big_shl(big_n, 30);
|
|
big_set(big_u, mantissa * 2 + 1);
|
|
big_shl(big_u, 29);
|
|
big_set(big_l, mantissa == 0x800000 && exponent > 1 ? mantissa * 4 - 1 : mantissa * 2 - 1);
|
|
big_shl(big_l, mantissa == 0x800000 && exponent > 1 ? 28 : 29);
|
|
big_set(big_m, 2);
|
|
unit = scale - 30;
|
|
binary = scale + big_bits(big_n) as i64 - 31;
|
|
even = mantissa % 2 == 0;
|
|
}
|
|
else {
|
|
val mantissa := field == 0 ? bits & 0xFFFFFFFFFFFFF : (bits & 0xFFFFFFFFFFFFF) | 0x10000000000000;
|
|
val scale := field == 0 ? -1074 : (field as i64) - 1075;
|
|
big_set(big_n, mantissa * 4);
|
|
big_set(big_u, mantissa * 4 + 2);
|
|
big_set(big_l, mantissa == 0x10000000000000 && field > 1 ? mantissa * 4 - 1 : mantissa * 4 - 2);
|
|
big_set(big_m, 0);
|
|
unit = scale - 2;
|
|
binary = scale + big_bits(big_n) as i64 - 3;
|
|
even = mantissa % 2 == 0;
|
|
}
|
|
val power := decimal_digits(unit, binary);
|
|
|
|
# The shortest precision whose digits read back as the value: those strictly
|
|
# between the midpoints, or on one when ties go to the value, being even.
|
|
var precision := 1u;
|
|
while precision <= 17 {
|
|
val carried := round_digits(precision);
|
|
val shifted := carried ? power + 1 : power;
|
|
var digits_value := 0u;
|
|
for i in 0..precision {
|
|
digits_value = digits_value * 10 + rounded[i] as u64;
|
|
}
|
|
if carried {
|
|
digits_value = digits_value * 10;
|
|
}
|
|
big_set(big_q, digits_value);
|
|
big_copy(big_t, big_d);
|
|
big_mul_u64(big_t, big_q);
|
|
|
|
val above := big_compare(big_t, big_u);
|
|
val below := big_compare(big_t, big_l);
|
|
var same := (above < 0 || (above == 0 && even)) && (below > 0 || (below == 0 && even));
|
|
|
|
# A single reads back through a double, whose rounding matters near a midpoint.
|
|
if single && !(above < 0 && below > 0 && clear_of_midpoints()) && !(above > 0 || below < 0) || single && near_outside() {
|
|
val back := nearest_double(precision, shifted);
|
|
same = (back as f32) == (magnitude as f32);
|
|
}
|
|
|
|
if same || precision == 17 {
|
|
put_general(into, precision, shifted);
|
|
return;
|
|
}
|
|
|
|
big_mul_small(big_u, 10);
|
|
big_mul_small(big_l, 10);
|
|
big_mul_small(big_m, 10);
|
|
precision++;
|
|
}
|
|
}
|
|
|
|
# Whether the candidate in `t` is inside the midpoints by more than the margin.
|
|
def clear_of_midpoints(): bool {
|
|
big_copy(big_c, big_t);
|
|
add_into(big_c, big_m);
|
|
if big_compare(big_c, big_u) >= 0 {
|
|
return false;
|
|
}
|
|
big_copy(big_c, big_l);
|
|
add_into(big_c, big_m);
|
|
return big_compare(big_t, big_c) > 0;
|
|
}
|
|
|
|
# Whether the candidate in `t` is outside the midpoints, but within the margin.
|
|
def near_outside(): bool {
|
|
big_copy(big_c, big_u);
|
|
add_into(big_c, big_m);
|
|
if big_compare(big_t, big_u) > 0 && big_compare(big_t, big_c) <= 0 {
|
|
return true;
|
|
}
|
|
big_copy(big_c, big_t);
|
|
add_into(big_c, big_m);
|
|
return big_compare(big_t, big_l) < 0 && big_compare(big_c, big_l) >= 0;
|
|
}
|
|
|
|
# Multiplies by a number of at most 64 bits, held in `factor`.
|
|
def big_mul_u64(a: *mut u64, factor: *mut u64): void {
|
|
if factor[0] <= 1 {
|
|
big_mul_small(a, factor[0] == 0 ? 0 : factor[1]);
|
|
big_trim(a);
|
|
return;
|
|
}
|
|
big_copy(big_c, a);
|
|
big_mul_small(a, factor[1]);
|
|
big_mul_small(big_c, factor[2]);
|
|
big_shl(big_c, 32);
|
|
add_into(a, big_c);
|
|
}
|