wasmi icon indicating copy to clipboard operation
wasmi copied to clipboard

Implement optional NaN canonicalization mode

Open NikVolf opened this issue 7 years ago • 7 comments

NikVolf avatar Jan 26 '18 13:01 NikVolf

I'm assuming this issue tries to solve the non-determinism problem like so: https://github.com/WebAssembly/design/issues/582#issuecomment-191318866

For NaN bits, it'd be straightforward for a preprocessor to inject NaN canonicalization code after every floating-point computation, and that would suffice.

Doing this after every operation might be excessive, NaNs with different bit patterns still mostly behave the same. It should be enough to canonicalize in the few places where they don't: reinterpret, store, etc. There is a list here: https://github.com/WebAssembly/design/blob/master/Rationale.md#nan-bit-pattern-nondeterminism

Although it would be fun to write a contract that pays out when someone mines it with ARM instead of x86. Might be good for an ARM node bounty! 🤡

recmo avatar Sep 27 '18 14:09 recmo

Yep, the motivation is to prevent non-determinism still having some base2 floats.

it's not very important atm, since we can ban floats on the embedder and/or use soft floats in the user space

But still might be useful!

NikVolf avatar Sep 28 '18 10:09 NikVolf

Although it would be fun to write a contract that pays out when someone mines it with ARM instead of x86. Might be good for an ARM node bounty! 🤡

Interesting idea, but how would you actually proof that? : D

pepyakin avatar Oct 29 '18 10:10 pepyakin

I don't think NaN is only non-determinism part of float points in WebAssembly. Operations like sqrt and inverse sqrt (rsqrt) implement differently even on x86 by some vendors. ARM for example flush all subnormal values to zero (FTZ and DAZ modes). Conversion from floats to integers also behave differently on hardware and by compiler's mode.

MaxGraey avatar Feb 07 '19 02:02 MaxGraey

Wasm's sqrt really is deterministic; while other languages can use x87 which rounds things like sqrt differently in some cases, wasm doesn't permit this, and this is specifically tested for in the spec testsuite.

The ARM subnormal issue is only on 32-bit ARM, and only NEON, not VFP, so wasm's current scalar operations aren't affected. This is a potential concern for the SIMD proposal, however.

WebAssembly also fully specifies the behavior of conversion from floats to integers, and the behavior in all the cases where hardware is known to differ is covered in the testsuite too.

So while wasm could add rsqrt approximations or other things in the future, and SIMD will have to cope with 32-bit ARM NEON, in the current spec, NaN bit patterns really are the only floating-point nondeterminism.

sunfishcode avatar Feb 07 '19 04:02 sunfishcode

@sunfishcode Thank you for so detail clarifying! Now you had dispelled any doubts)

MaxGraey avatar Feb 07 '19 08:02 MaxGraey