wasm_wat_num (wasm v0.1.0)

View Source

Numeric literals of the text format. Read it when a literal round-trips to bits you did not expect.

The binary format has no literals: a constant is already bits. The text format has to turn 0x1.921fb6p+1, -nan:0x200000 and 1_000 into the same bits, and the interesting cases are the ones a naive list_to_float gets wrong.

Erlang cannot hold the answers. nan and inf are not floats here, so a literal is converted to a bit pattern and handed to wasm_num, the same hybrid representation the interpreter uses. Parsing to an Erlang float first would lose the payload of nan:0x200000 and raise on inf outright.

Every literal is rounded exactly once. A literal becomes an exact rational and is rounded straight to the target width. Rounding to an Erlang double on the way rounds twice, and the specification's float suites are written to catch precisely that: 0x1.fffffefffffff8p127 is the largest finite f32 and becomes infinity if a double sees it first.

Underscores separate digits and nothing else. 1_000 is a thousand; _1, 1_ and 0x_ff are malformed. Stripping them before parsing would accept all three.

Summary

Functions

A floating point literal, as the bit pattern of an f32 or an f64.

An integer literal, checked against the width and signedness it is used at.

Functions

float_bits(Bin, Width)

-spec float_bits(binary(), 32 | 64) -> {ok, non_neg_integer()} | error.

A floating point literal, as the bit pattern of an f32 or an f64.

Answers bits rather than a number because three of the forms have no Erlang value: inf, nan, and nan:0x... with a chosen payload.

integer(Bin, Kind)

-spec integer(binary(), u32 | u64 | s32 | s64 | i32 | i64) -> {ok, integer()} | error.

An integer literal, checked against the width and signedness it is used at.

u32 admits 0 to 2^32-1; s32 admits -2^31 to 2^31-1; i32 admits either, because a constant may be written as a signed or an unsigned pattern and means the same bits. The answer is always the signed interpretation, which is how the runtime holds integers.