Comparison & Logic
WebAssembly has no boolean type. Comparisons produce an i32: 1 for true, 0 for false. Conditional
instructions (if, br_if, select) accept any i32 and treat every non-zero value as true.
Comparison instructions
Integers (i32/i64) |
Floats (f32/f64) |
Meaning |
|---|---|---|
eq ne |
eq ne |
equal, not equal |
lt_s lt_u |
lt |
less than |
gt_s gt_u |
gt |
greater than |
le_s le_u |
le |
less or equal |
ge_s ge_u |
ge |
greater or equal |
eqz |
is zero? (unary) |
Integers need the signed/unsigned choice; floats don't. Every comparison returns an i32, even i64.lt_s and
f64.eq.
(module
(import "console" "log" (func $log (param i32)))
(func (export "main")
(call $log (i32.eq (i32.const 5) (i32.const 5)))
(call $log (i32.lt_s (i32.const -1) (i32.const 1))) ;; -1 < 1 signed: true
(call $log (i32.lt_u (i32.const -1) (i32.const 1))) ;; 4294967295 < 1: false
(call $log (i64.ge_s (i64.const 10) (i64.const 10)))
(call $log (f64.gt (f64.const 0.5) (f64.const 0.25)))
(call $log (i32.eqz (i32.const 0)))
(call $log (i32.eqz (i32.const 42)))))1
1
0
1
1
1
0
The lt_u line is a classic bug source: comparing a negative number as unsigned treats it as huge. Compilers use
lt_u for C's unsigned and for array bounds checks ("is i within 0..len?" becomes one unsigned compare).
NaN is never equal
Float comparisons follow IEEE 754: any comparison involving NaN is false, except ne, which is true. So x ne x
is a NaN test:
(module
(import "console" "log" (func $log (param i32)))
(func $is_nan (param $x f64) (result i32)
(f64.ne (local.get $x) (local.get $x)))
(func (export "main")
(call $log (f64.eq (f64.const nan) (f64.const nan)))
(call $log (call $is_nan (f64.const nan)))
(call $log (call $is_nan (f64.const 1.5)))
(call $log (f64.eq (f64.const 0) (f64.const -0))))) ;; +0 and -0 compare equal0
1
0
1
Logical AND, OR, NOT
There are no separate logical operators. Because comparisons produce exactly 0 or 1, the bitwise i32.and,
i32.or and i32.xor work as logical operators on them, and i32.eqz works as NOT:
(module
(import "console" "log" (func $log (param i32)))
;; is $c a lowercase ASCII letter? ('a' = 97, 'z' = 122)
(func $is_lower (param $c i32) (result i32)
(i32.and
(i32.ge_u (local.get $c) (i32.const 97))
(i32.le_u (local.get $c) (i32.const 122))))
(func (export "main")
(call $log (call $is_lower (i32.const 104))) ;; 'h'
(call $log (call $is_lower (i32.const 72))) ;; 'H'
(call $log (i32.eqz (call $is_lower (i32.const 72)))) ;; NOT
(call $log (i32.or (i32.const 0) (i32.const 1)))))1
0
1
1
Note: Unlike
&&and||in C or JavaScript,i32.and/i32.oralways evaluate both operands. If the second operand has side effects or could trap (like a division), useifto short-circuit instead.
A useful trick: a range check lo <= x <= hi can be written as one unsigned comparison,
(x - lo) <=u (hi - lo), because values below lo wrap around to huge numbers:
(module
(import "console" "log" (func $log (param i32)))
(func $is_digit (param $c i32) (result i32) ;; '0'..'9' = 48..57
(i32.le_u (i32.sub (local.get $c) (i32.const 48)) (i32.const 9)))
(func (export "main")
(call $log (call $is_digit (i32.const 55))) ;; '7'
(call $log (call $is_digit (i32.const 47))) ;; '/'
(call $log (call $is_digit (i32.const 58))))) ;; ':'1
0
0
Choosing a value: select
select picks one of two values based on a condition, without branching. It takes three operands: the value if
true, the value if false, and the condition (last).
(module
(import "console" "log" (func $log (param i32)))
(func $max (param $a i32) (param $b i32) (result i32)
(select
(local.get $a)
(local.get $b)
(i32.gt_s (local.get $a) (local.get $b))))
(func (export "main")
(call $log (call $max (i32.const 3) (i32.const 8)))
(call $log (call $max (i32.const -2) (i32.const -9)))))8
-2
You'll see select again (and the if alternative) in If/Else & select.
Test yourself
What type does f64.lt produce?
- f64
- i64
- i32
- A special bool type
What is (i32.lt_u (i32.const -1) (i32.const 1))?
- 1
- 0
- -1
- It fails validation
Why can i32.and be used as logical AND on comparison results?
- Because comparisons return exactly 0 or 1
- Because it short-circuits
- Because Wasm converts to bool automatically
- It can't be