typestar

Integer arithmetic in WebAssembly

No operators, only instructions — and division declares its sign.

(module
  ;; Integer division and remainder come signed and unsigned; the
  ;; suffix picks. There is no operator syntax, only instructions.
  (func (export "split_coins") (param $coins i32) (param $ways i32)
        (result i32)
    (i32.div_s (local.get $coins) (local.get $ways)))

  (func (export "left_over") (param $coins i32) (param $ways i32)
        (result i32)
    (i32.rem_s (local.get $coins) (local.get $ways)))

  (func (export "double_less_one") (param $n i32) (result i32)
    (i32.sub (i32.mul (local.get $n) (i32.const 2)) (i32.const 1))))

How it works

  1. i32.div_s divides as signed; _u would read the bits unsigned.
  2. i32.rem_s is the matching remainder.
  3. Compound math nests folded calls: multiply, then subtract.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
520
Tokens
109
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 101 seconds.

Type this snippet

Step 1 of 3 in Numbers, step 4 of 28 in Language basics.

← Previous Next →