-
Notifications
You must be signed in to change notification settings - Fork 693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Floating-point min and max that prefer numbers #1548
Comments
How are you proposing to handle canonical vs non-canonical NaNs? Currently we are supposed to preserve exact NaN value if it is canonical (in this case if both arguments are NaNs). Implementation examples don't seem to have canonical NaN handling. |
Looking at the code more closely, I believe I found a bug in LLVM here. LLVM's documentation for Fixing that bug may require LLVM to use a significantly different code sequence, so we'll see where canonical NaN handling ends up. However, in general, it tends to be the case on architectures that implement the recommendations in IEEE 754-2019 6.2.3 NaN propagation, eg.:
as most architectures do, that Wasm-compatible canonical NaN handling happens without any additional instructions. |
With LLVM That aside, I've tried evaluating this a few years ago, and the x86 sequences looked really long due to canonical NaN handling, which is a bit different from IEEE 754-2019, that's why I am curious what you are thinking. And what canonical NaN semantics would be here (canonical and non-canonical - return canonical? what about canonical and canonical?)? |
The docs for that only talk about exception flags, traps, and rounding modes, and don't mention sNaN, so it's unclear. There's ongoing discussion in llvm/llvm-project#123263. If LLVM decides its behavior does not include sNaN -> qNaN conversion, then we'll need to add
I'd expect these operators to follow the existing NaN rules, which means that canonical and non-canonical => nondeterministic arithmetic NaN, and canonical and canonical => canonical. |
Wasm's current
f32.min
andf32.max
, correspond to IEEE 754-2019'sminimum
andmaximum
operations. When exactly one of their two operands is a NaN, they pick the NaN. Wasm may wish to consider adding instructions corresponding to IEEE 754-2019'sminimumNumber
andmaximumNumber
operations, which would pick the non-NaN.In LLVM, the corresponding operations are
minimumnum
andmaximumnum
. In C23, the corresponding functions arefminimum_num
andfmaximum_num
.These are similar to the now-removed
minNum
andmaxNum
operators from IEEE 754-2008, but specify that negative zero is to be treated as less than zero rather than being nondeterministic, and that signaling NaN is to be handled more like quiet NaN so that the operators are associative.Implementations
Looking at current major CPU ISAs, it appears only PowerPC has direct support for these instructions. However, as can be seen on aarch64, CPUs with instructions implementing 754-2008's
minNum
andmaxNum
can implement it with three instructions, and as can be seen on x86_64, AVX enables significant optimizations, so adding these new instructions would at least be an improvement over the status quo on most architectures for code that would use them.Naming
Given that IEEE 754-2019's
minimum
corresponds to Wasm'smin
, it's tempting to mapminimumNumber
tominnum
in Wasm, however this risks confusion with the old and removedminNum
andmaxNum
operators.Consequently, I suggest the names
minnumber
andmaxnumber
for Wasm. The full set would be:f32.minnumber
f32.maxnumber
f64.minnumber
f64.maxnumber
Next steps
If anyone has information about source languages, compilers, or libraries using IEEE 754-2019's
minumNumber
andmaximumNumber
, or CPUs implementing them, or use cases that would benefit from them, please post about it here!The text was updated successfully, but these errors were encountered: