TExcelTAD
Home › WebAssembly › Type Conversions

Type Conversions

WebAssembly never converts types implicitly. To use an i32 where an f64 is expected, you must convert it with an explicit instruction. The names always read result type . operation _ source type: f64.convert_i32_s means "make an f64 from a signed i32".

Integer ↔ integer

Instruction Effect
i32.wrap_i64 keep the low 32 bits of an i64
i64.extend_i32_s sign-extend an i32 to 64 bits
i64.extend_i32_u zero-extend an i32 to 64 bits
i32.extend8_s i32.extend16_s sign-extend the low 8/16 bits in place
i64.extend8_s i64.extend16_s i64.extend32_s the same for i64
wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (import "console" "log" (func $log64 (param i64)))
  (func (export "main")
    (call $log64 (i64.extend_i32_s (i32.const -1)))       ;; stays -1
    (call $log64 (i64.extend_i32_u (i32.const -1)))       ;; 0x00000000FFFFFFFF
    (call $log (i32.wrap_i64 (i64.const 0x1_0000_0005)))  ;; high bits dropped
    (call $log (i32.extend8_s (i32.const 200)))           ;; byte 200 as a signed byte
    (call $log (i32.extend16_s (i32.const 0x8000)))))
-1
4294967295
5
-56
-32768

200 is 0xC8; read as a signed byte, it is -56. The extendN_s instructions are handy when you load a value of one width and need to interpret it as another.

Integer → float

Instruction Effect
f32.convert_i32_s / _u i32 → f32 (may round for large values)
f64.convert_i32_s / _u i32 → f64 (always exact)
f64.convert_i64_s / _u i64 → f64 (may round above 2^53)
wat · try it yourself
(module
  (import "console" "log" (func $log (param f64)))
  (import "console" "logf" (func $unused (param f32)))
  (func (export "main")
    (call $log (f64.convert_i32_s (i32.const -3)))
    (call $log (f64.convert_i32_u (i32.const -3)))))
LinkError: Import #1 "console" "logf": function import requires a callable

That failed at instantiation: the playground's host has no console.logf, and a missing import is a LinkError. Every import must be satisfied, even if it is never called. Remove it and try again:

wat · try it yourself
(module
  (import "console" "log" (func $log (param f64)))
  (func (export "main")
    (call $log (f64.convert_i32_s (i32.const -3)))
    (call $log (f64.convert_i32_u (i32.const -3)))
    (call $log (f64.convert_i64_s (i64.const 9007199254740993)))))   ;; 2^53 + 1 rounds
-3
4294967293
9007199254740992

Float → integer

Converting a float to an integer is trickier: what should 1e20 or NaN become? There are two families.

Instruction Out-of-range or NaN input
i32.trunc_f64_s (and _u, i64.…, …_f32_…) traps
i32.trunc_sat_f64_s (the "saturating" family) clamps to the min/max integer, NaN becomes 0

Both truncate toward zero (-2.9 → -2).

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (func (export "main")
    (call $log (i32.trunc_f64_s (f64.const -2.9)))
    (call $log (i32.trunc_sat_f64_s (f64.const 1e20)))
    (call $log (i32.trunc_sat_f64_s (f64.const -1e20)))
    (call $log (i32.trunc_sat_f64_s (f64.const nan)))
    (call $log (i32.trunc_sat_f64_u (f64.const -5)))
    (call $log (i32.trunc_f64_s (f64.const 1e20)))))     ;; traps
-2
2147483647
-2147483648
0
0
RuntimeError: float unrepresentable in integer range

The first five lines print before the last conversion traps. Saturating conversions match what Rust's as does and are what most compilers emit today.

Float ↔ float

f64.promote_f32 widens an f32 exactly; f32.demote_f64 rounds an f64 to the nearest f32.

Reinterpreting bits

Sometimes you want the same bits viewed as another type, without any numeric conversion. The reinterpret instructions do that (like a C union or memcpy):

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (import "console" "log" (func $logf (param f32)))
  (func (export "main")
    ;; the IEEE 754 bit pattern of 1.0f is 0x3F800000
    (call $log (i32.reinterpret_f32 (f32.const 1.0)))
    (call $log (i32.eq (i32.reinterpret_f32 (f32.const 1.0)) (i32.const 0x3F800000)))
    (call $logf (f32.reinterpret_i32 (i32.const 0x40490FDB)))))   ;; close to pi
1065353216
1
3.1415927410125732

1065353216 is 0x3F800000. Reinterpretation is how libraries implement fast tricks such as reading a float's exponent bits.

Common mistakes

  • Using convert when you meant reinterpret (or vice versa): f32.convert_i32_s of 0x3F800000 gives 1065353216.0, not 1.0.
  • Forgetting the _s/_u choice: unsigned conversion of a negative i32 produces a large positive number.
  • Using a trapping trunc on untrusted input: prefer trunc_sat unless a trap is what you want.

Test yourself

Which instruction converts a signed i32 to an f64?

  1. i32.convert_f64_s
  2. f64.convert_i32_s
  3. f64.reinterpret_i32
  4. f64.promote_i32

What does i32.trunc_sat_f64_s return for NaN?

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

What does i32.wrap_i64 do to 0x1_0000_0005?

  1. Traps because it doesn't fit
  2. Returns 5 (the low 32 bits)
  3. Returns 2147483647
  4. Returns -5