How is data generated for a twin in a web assembly?
In Webassembly, the following:
double x = 1;
An analogue of this WAST:
(module
(table 0 anyfunc)
(memory $0 1)
(data (i32.const 16) "\00\00\00\00\00\00\f0?")
(export "memory" (memory $0))
)
I've already figured out how it works with integers (using hex numbers), but what I see for doubles is not the usual sign that I'm familiar with.
For example, an integer of 15 is equivalent to "\ 0f \ 00 \ 00 \ 00" which I understand, but how it works for doubles. What is this hex and what determines the offset within the data section?
I am using https://mbebenita.github.io/WasmExplorer/ for testing purposes.
source to share
WebAssembly uses IEEE-754 for encoding f32
and f64
.
The data section is initialized with "\00\00\00\00\00\00\f0?"
which is ASCII encoding. This is what the author of the s-expressions chose, in the actual binary it is encoded as 4 or 8 bytes (for f32
versus f64
). We could also use hex-float as C / C ++ support, we just need lossless encoding (and hex-float is lossy with NaN). Another option is base64, or raw hex, or whatever. The data section doesn't care what type of data it contains: in binary, it's all just bytes. This way we don't encode floating point values or integers, just bytes.
This stands for hex 3FF000000000
. What for? There's a ?
at the end, its ASCII value is 3F
. Then you have all the escaped ASCII values that don't have nicer representations, so the escape encoding is used. This is really not a great show!
What does it mean? Try this tool to understand the IEEE-754 encoding!
Binary64: 3FF0000000000000
Status Sign [1] Exponent [11] Significand [52]
Normal 0 (+) 01111111111 (0) 1.0000000000000000000000000000000000000000000000000000 (1.0)
The sign is positive, the exponent is 0, and the value is 1.0. This is the value 1.0
in IEEE-754.
source to share