TExcelTAD
Home › WebAssembly › Numeric Instructions

Numeric Instructions

Most WebAssembly instructions are arithmetic. They are named <type>.<operation>, so the same operation exists for each type where it makes sense: i32.add, i64.add, f32.add, f64.add.

Integer arithmetic

Instruction Meaning Notes
add sub mul + − × wrap around on overflow
div_s div_u division, truncating toward zero traps on division by zero; div_s also traps on MIN / -1
rem_s rem_u remainder result of rem_s has the sign of the dividend; traps on x % 0
wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (func (export "main")
    (call $log (i32.div_s (i32.const 17) (i32.const 5)))    ;; 3
    (call $log (i32.rem_s (i32.const 17) (i32.const 5)))    ;; 2
    (call $log (i32.div_s (i32.const -17) (i32.const 5)))   ;; truncates toward zero
    (call $log (i32.rem_s (i32.const -17) (i32.const 5)))   ;; sign follows -17
    (call $log (i32.rem_u (i32.const -17) (i32.const 5)))))  ;; 4294967279 % 5
3
2
-3
-2
4

Integer division by zero is one of the few operations that traps:

wat · try it yourself
(module
  (func (export "main") (result i32)
    (i32.div_u (i32.const 10) (i32.const 0))))
RuntimeError: divide by zero

Bitwise operations

Instruction Meaning
and or xor bitwise AND, OR, XOR
shl shift left
shr_s arithmetic shift right (copies the sign bit)
shr_u logical shift right (fills with zeros)
rotl rotr rotate left/right
clz ctz popcnt count leading zeros, trailing zeros, set bits

The shift count is taken modulo the bit width (so i32.shl by 33 shifts by 1).

WAT has no binary literals (0b1100 is a syntax error), so the example uses hexadecimal: 0xC is 1100 in binary and 0xA is 1010.

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (func (export "main")
    (call $log (i32.and (i32.const 0xC) (i32.const 0xA)))
    (call $log (i32.or  (i32.const 0xC) (i32.const 0xA)))
    (call $log (i32.xor (i32.const 0xC) (i32.const 0xA)))
    (call $log (i32.shl (i32.const 1) (i32.const 10)))
    (call $log (i32.shl (i32.const 1) (i32.const 33)))
    (call $log (i32.shr_s (i32.const -16) (i32.const 2)))
    (call $log (i32.shr_u (i32.const -16) (i32.const 28)))
    (call $log (i32.popcnt (i32.const 255)))
    (call $log (i32.clz (i32.const 1)))
    (call $log (i32.ctz (i32.const 40)))))
8
14
6
1024
2
-4
15
8
31
3

-16 is 0xFFFFFFF0: shifting right arithmetically by 2 gives -4, while shifting logically by 28 leaves 0xF = 15. 40 is 101000 in binary, so it has 3 trailing zeros.

Floating-point arithmetic

Floats have add sub mul div plus several unary operations and min/max/copysign:

Instruction Meaning
abs neg absolute value, negation
sqrt square root
ceil floor trunc nearest rounding (nearest rounds half to even)
min max minimum/maximum (NaN if either operand is NaN)
copysign magnitude of the first operand, sign of the second
wat · try it yourself
(module
  (import "console" "log" (func $log (param f64)))
  (func (export "main")
    (call $log (f64.sqrt (f64.const 2)))
    (call $log (f64.floor (f64.const -2.5)))
    (call $log (f64.ceil (f64.const -2.5)))
    (call $log (f64.trunc (f64.const -2.5)))
    (call $log (f64.nearest (f64.const 2.5)))      ;; ties go to the even neighbour
    (call $log (f64.nearest (f64.const 3.5)))
    (call $log (f64.max (f64.const 4) (f64.const 9)))
    (call $log (f64.copysign (f64.const 7) (f64.const -1)))))
1.4142135623730951
-3
-2
-2
2
4
9
-7

There is no built-in sin, exp or pow: compilers either include a small math library in the module or import those functions from the host (Math.sin).

Putting it together: a distance function

The Euclidean distance between two points (x1, y1) and (x2, y2) is sqrt(dx² + dy²):

wat · try it yourself
(module
  (import "console" "log" (func $log (param f64)))
  (func $distance (param $x1 f64) (param $y1 f64) (param $x2 f64) (param $y2 f64) (result f64)
    (local $dx f64) (local $dy f64)
    (local.set $dx (f64.sub (local.get $x2) (local.get $x1)))
    (local.set $dy (f64.sub (local.get $y2) (local.get $y1)))
    (f64.sqrt
      (f64.add
        (f64.mul (local.get $dx) (local.get $dx))
        (f64.mul (local.get $dy) (local.get $dy)))))
  (func (export "main")
    (call $log (call $distance (f64.const 0) (f64.const 0) (f64.const 3) (f64.const 4)))
    (call $log (call $distance (f64.const 1) (f64.const 1) (f64.const 2) (f64.const 2)))))
5
1.4142135623730951

Test yourself

What is (i32.rem_s (i32.const -7) (i32.const 2))?

  1. 1
  2. -1
  3. 3
  4. It traps

Which operation traps?

  1. f64.div by zero
  2. i32.add overflow
  3. i32.div_s by zero
  4. i32.shl by 40

What does f64.nearest return for 2.5?

  1. 2
  2. 3
  3. 2.5
  4. NaN