Lines
100 %
Functions
Branches
//! Ratio arithmetic and comparison that never wraps (ISS-086). Operands are
//! cross-reduced by their GCDs before any product, so a result that fits in
//! 64 bits is computed exactly even when the naive cross products would not;
//! every remaining product, sum and difference goes through a checked helper
//! that throws `ARITHMETIC-OVERFLOW` instead of wrapping.
use super::CompileContext;
use super::exception::ConditionTrap;
use wasm_encoder::{BlockType, Function, Instruction, ValType};
const NUM: u32 = 0;
const DEN: u32 = 1;
/// The checked i64 operations, in their registration order.
pub(super) const CHECKED_OPS: [&str; 3] = ["i64_mul_checked", "i64_add_checked", "i64_sub_checked"];
impl CompileContext {
/// `i64_mul_checked` / `i64_add_checked` / `i64_sub_checked(a, b) -> i64`.
pub(super) fn build_checked_i64_bodies(&mut self, overflow: &ConditionTrap) {
self.pending_helpers.push(checked_mul_body(overflow));
self.pending_helpers
.push(checked_sum_body(overflow, &Instruction::I64Add, (1, 2)));
.push(checked_sum_body(overflow, &Instruction::I64Sub, (0, 1)));
}
/// `ratio_add` / `ratio_sub` / `ratio_mul` / `ratio_div(a, b) -> ratio`.
pub(super) fn build_ratio_binop_bodies(&mut self, div_trap: &ConditionTrap) {
let ops = self.ratio_ops();
self.pending_helpers.push(ops.sum_body(ops.add_checked));
self.pending_helpers.push(ops.sum_body(ops.sub_checked));
self.pending_helpers.push(ops.mul_body());
self.pending_helpers.push(ops.div_body(div_trap));
/// `ratio_eq` / `ratio_lt(a, b) -> i32`.
pub(super) fn build_ratio_cmp_bodies(&mut self) {
self.pending_helpers.push(ops.eq_body());
self.pending_helpers.push(ops.lt_body());
fn ratio_ops(&self) -> RatioOps {
RatioOps {
ratio: self.ids.ty_ratio,
gcd: self.ids.gcd,
ratio_new: self.ids.ratio_new,
mul_checked: self.ids.i64_mul_checked,
add_checked: self.ids.i64_add_checked,
sub_checked: self.ids.i64_sub_checked,
/// The function indices the ratio bodies call.
struct RatioOps {
ratio: u32,
gcd: u32,
ratio_new: u32,
mul_checked: u32,
add_checked: u32,
sub_checked: u32,
impl RatioOps {
fn field(&self, f: &mut Function, local: u32, field: u32) {
f.instruction(&Instruction::LocalGet(local));
f.instruction(&Instruction::StructGet {
struct_type_index: self.ratio,
field_index: field,
});
/// Pushes `field(local) / g`, `g` held in local `g`.
fn reduced(&self, f: &mut Function, local: u32, field: u32, g: u32) {
self.field(f, local, field);
f.instruction(&Instruction::LocalGet(g));
f.instruction(&Instruction::I64DivS);
/// Sets local `into` to `gcd(field_a(a), field_b(b))`.
fn gcd_into(&self, f: &mut Function, field_a: (u32, u32), field_b: (u32, u32), into: u32) {
self.field(f, field_a.0, field_a.1);
self.field(f, field_b.0, field_b.1);
f.instruction(&Instruction::Call(self.gcd));
f.instruction(&Instruction::LocalSet(into));
/// `a ± b` over the least common denominator `a.den * (b.den / g)`.
fn sum_body(&self, combine: u32) -> Function {
let mut f = Function::new([(1, ValType::I64)]);
let g = 2;
self.gcd_into(&mut f, (0, DEN), (1, DEN), g);
self.field(&mut f, 0, NUM);
self.reduced(&mut f, 1, DEN, g);
f.instruction(&Instruction::Call(self.mul_checked));
self.field(&mut f, 1, NUM);
self.reduced(&mut f, 0, DEN, g);
f.instruction(&Instruction::Call(combine));
self.field(&mut f, 0, DEN);
f.instruction(&Instruction::Call(self.ratio_new));
f.instruction(&Instruction::End);
f
/// `a * b`, each numerator first cancelled against the other denominator.
fn mul_body(&self) -> Function {
let mut f = Function::new([(2, ValType::I64)]);
let (g1, g2) = (2, 3);
self.gcd_into(&mut f, (0, NUM), (1, DEN), g1);
self.gcd_into(&mut f, (1, NUM), (0, DEN), g2);
self.product(&mut f, (0, NUM, g1), (1, NUM, g2));
self.product(&mut f, (0, DEN, g2), (1, DEN, g1));
/// `a / b = a * (b.den / b.num)`; a zero divisor throws before any gcd.
fn div_body(&self, div_trap: &ConditionTrap) -> Function {
f.instruction(&Instruction::I64Eqz);
f.instruction(&Instruction::If(BlockType::Empty));
div_trap.emit_throw(&mut f);
self.gcd_into(&mut f, (0, NUM), (1, NUM), g1);
self.gcd_into(&mut f, (0, DEN), (1, DEN), g2);
self.product(&mut f, (0, NUM, g1), (1, DEN, g2));
self.product(&mut f, (0, DEN, g2), (1, NUM, g1));
/// Pushes the checked product of two reduced fields.
fn product(&self, f: &mut Function, left: (u32, u32, u32), right: (u32, u32, u32)) {
self.reduced(f, left.0, left.1, left.2);
self.reduced(f, right.0, right.1, right.2);
/// Equal in lowest terms: exact, with no product to overflow.
fn eq_body(&self) -> Function {
let (ga, gb) = (2, 3);
self.gcd_into(&mut f, (0, NUM), (0, DEN), ga);
self.gcd_into(&mut f, (1, NUM), (1, DEN), gb);
for field in [NUM, DEN] {
self.reduced(&mut f, 0, field, ga);
self.reduced(&mut f, 1, field, gb);
f.instruction(&Instruction::I64Eq);
f.instruction(&Instruction::I32And);
/// `a.num * (b.den / g) < b.num * (a.den / g)`, `g` the denominators' GCD.
fn lt_body(&self) -> Function {
f.instruction(&Instruction::I64LtS);
/// `a * b`, throwing when the product leaves i64: it overflowed exactly when
/// dividing it back by `a` does not give `b`, or for `i64::MIN * -1`, whose
/// division would itself trap.
fn checked_mul_body(overflow: &ConditionTrap) -> Function {
for (minus_one, min) in [(0, 1), (1, 0)] {
f.instruction(&Instruction::LocalGet(minus_one));
f.instruction(&Instruction::I64Const(-1));
f.instruction(&Instruction::LocalGet(min));
f.instruction(&Instruction::I64Const(i64::MIN));
throw_if(&mut f, overflow);
f.instruction(&Instruction::LocalGet(0));
f.instruction(&Instruction::If(BlockType::Result(ValType::I64)));
f.instruction(&Instruction::I64Const(0));
f.instruction(&Instruction::Else);
f.instruction(&Instruction::LocalGet(1));
f.instruction(&Instruction::I64Mul);
f.instruction(&Instruction::LocalTee(2));
f.instruction(&Instruction::I64Ne);
f.instruction(&Instruction::LocalGet(2));
/// `r = a + b` or `r = a - b`, throwing on signed overflow, which is when
/// `(a ^ r) & (x ^ y)` is negative: `(x, y)` is `(b, r)` for a sum (r's sign
/// differs from both operands') and `(a, b)` for a difference (the operands'
/// signs differ and r's differs from a's).
fn checked_sum_body(
overflow: &ConditionTrap,
op: &Instruction<'_>,
(x, y): (u32, u32),
) -> Function {
f.instruction(op);
f.instruction(&Instruction::LocalSet(2));
f.instruction(&Instruction::I64Xor);
f.instruction(&Instruction::LocalGet(x));
f.instruction(&Instruction::LocalGet(y));
f.instruction(&Instruction::I64And);
fn throw_if(f: &mut Function, trap: &ConditionTrap) {
trap.emit_throw(f);