SBKarr
SBKarr

Reputation: 548

WebAssembly: (f32.const nan:0x200000) means 0x7fa00000 or 0x7fe00000

In C, strtof("nan(0x200000)", nullptr) represented as 0x7fe00000. But in WebAssembly spec interpreter tests:

(assert_return (invoke "f32.reinterpret_i32" (i32.const 0x7fa00000)) (f32.const nan:0x200000))
(assert_return (invoke "f32.reinterpret_i32" (i32.const 0xffa00000)) (f32.const -nan:0x200000))

So, in WebAssembly nan(0x200000) (or nan:0x200000) should be represented as 0x7fa00000, or it's an error?

Why in C it is implemented differently?

Same question for f64.const.

Upvotes: 0

Views: 345

Answers (1)

interjay
interjay

Reputation: 110108

In C, strtof("nan(0x200000)", nullptr) represented as 0x7fe00000

This is not necessarily so. The behavior of the extra information for NaNs in C is implementation-defined.

The difference between the values 0x7fa00000 and 0x7fe00000 is that the former is a signaling NaN, while the latter is a quiet NaN. It looks like your C implementation chose to represent NaNs as quiet NaNs, so the most-significant bit of the significand is set, while the given payload is used for the remaining bits.

WebAssembly lets you specify all significand bits including the most-significant one. You can create an "arithmetic NaN" (which has the most-significant bit set like a quiet NaN) by using a constant value of at least canonN, which is 0x400000 for 32-bit floating point. So you can create a representation of 0x7fe00000 by specifying a payload of 0x600000.

Upvotes: 4

Related Questions