typestar

i64 and width changes in WebAssembly

Doubling the width is explicit in both directions.

(module
  ;; i64 doubles the width; extend and wrap cross between the sizes.
  (func (export "square_wide") (param $n i32) (result i64)
    (local $wide i64)
    (local.set $wide (i64.extend_i32_s (local.get $n)))
    (i64.mul (local.get $wide) (local.get $wide)))

  ;; Wrapping keeps the low 32 bits and drops the rest.
  (func (export "low_bits") (param $big i64) (result i32)
    (i32.wrap_i64 (local.get $big)))

  (func (export "millis_to_secs") (param $ms i64) (result i64)
    (i64.div_u (local.get $ms) (i64.const 1000))))

How it works

  1. i64.extend_i32_s widens with sign; the math then runs in i64.
  2. i32.wrap_i64 keeps the low 32 bits and drops the rest.
  3. i64.div_u shows the wide type carries the full instruction set.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
501
Tokens
104
Three-star pace
60 tpm

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

Type this snippet

Step 1 of 3 in Wider types, step 23 of 28 in Language basics.

← Previous Next →