Skip to content

Numerics, bounary checks, perf, and safety

Motivation

We currently have a number of branches in our boxed arithmetic code; See !5261 (closed) for an example of this. There's a few cases of this:

  • The CPU doesn't raise an exception when we would like it to (ARM divide by zero)
  • The CPU does raise an exception when we wouldn't like it to (x86 signed minBound remainder by -1).

pred and succ also raise exception on overflow, and one might ask whether we we might distinguish between Int and Word as "useful subsets of Z/N" where those shouldn't happen, or cyclic groups where wraparound is allowed. There's also saturating arithmatic.

Proposal

  1. At the bare minimum, let's add some CPP for something so only the ISAs with issues pay for the workarounds they need.

  2. Per Rust (see https://doc.rust-lang.org/std/primitive.u32.html with it's wrapping_ saturating_ and overflowing_), consider adding more high-level operations for the user to better signal their intent.

  3. Consider adding some cabal flags for "fast and loose" turning off branches regardless of intent. But also conversely, with this escape hatch as an "outlet" for those desiring above all else, maybe add more branches for things like detecting and erring on overflow in the cases where wrapping or saturating is not the intent.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information