-
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 #214
Comments
Ignore my comment about zeros being ordered, I missed the statement that says for min and max they are. So WebAssembly is following the semantics of Java and Go. |
We do have the IEEE 754-2008 maxNum and minNum operations as possible future features. The current However, note that WebAssembly doesn't presently specify which particular NaN bit pattern you get back, and it doesn't presently distinguish between qNaN and sNaN. This is also something possible to be addressed in the future. |
I missed the references to maxNum and minNum operations in the future features document. I know that the current min and max functions are supported on at least ARM and Altivec (for float32), as far as I know Intel does not support them as a single instruction. |
We also haven't specified exactly what the min and max operations should min(a, b) === a < b ? a : b Which seems sane but gives weird results, such as -0.0 == min(0, -0), but 0 Unfortunately the latter seems to be what the Intel "minsd" instruction On Tue, Jun 23, 2015 at 5:41 PM, joncmu [email protected] wrote:
|
FWIW, current design says:
|
@titzer The design already does give the NaN behavior and handling of zeros, so it already does rule out a<b?a:b. |
Doing something simple like min(a,b) === a < b ? a:b would make mapping to hardware probably the simplest on the most architectures. All ISAs support floating point comparison and if condition flags are set a conditional move could be used to avoid branching, or a vector version and a select or blend instruction could then be use to select the two values. (Or if you are on Intel use minsd). Most of the floating point C code I have seen uses a macro with this behavior instead of using fmax/fmin. I would imagine that for 99% of the code that is written the coder doesn't care about the difference in NaN handling or ordered zeros. |
A noted above, ARM and Altivec can do WebAssembly's min and max in a single instruction. Forcing them to do a<b?a:b would be a pessimization on those platforms. And, it would give us the weird semantics noted above. And weirdness of semantics is something we do take into consideration when making choices. |
On Wed, Jun 24, 2015 at 9:25 AM, Dan Gohman [email protected]
|
A quick update to this (old) issue: The latest IEEE 754-2018 draft introduces new minimum and maximum functions, including functions which essentially match the semantics of WebAssembly's (and various other languages') |
WebAssembly doesn't implement the correct semantics for this operation. See WebAssembly/design#214
The min and max builtins in Zig have some intricate behavior related to floats, that is not replicated with the min and max wasm instructions or using simple select operations. By lowering these instructions to compiler_rt, handling around NaNs is done correctly. See also WebAssembly/design#214
The min and max builtins in Zig have some intricate behavior related to floats, that is not replicated with the min and max wasm instructions or using simple select operations. By lowering these instructions to compiler_rt, handling around NaNs is done correctly. See also WebAssembly/design#214
The min and max builtins in Zig have some intricate behavior related to floats, that is not replicated with the min and max wasm instructions or using simple select operations. By lowering these instructions to compiler_rt, handling around NaNs is done correctly. See also WebAssembly/design#214
The min and max builtins in Zig have some intricate behavior related to floats, that is not replicated with the min and max wasm instructions or using simple select operations. By lowering these instructions to compiler_rt, handling around NaNs is done correctly. See also WebAssembly/design#214
While reading through the design documents and some of the comments on issue #148 it seems like the definition for min and max does not follow the IEEE 754-2008 standard. The defined maxNum() and minNum() operations in the standard state that numeric values always win out against a qNaN value. Also the handling of sNaNs for maxNum and minNum is different than qNaN handling because any operation involving an sNaN shall deliver a qNaN as the result.
Therefore:
maxNum(numeric,sNan) = qNaN
maxNum(numeric,qNaN) = numeric
With the current definition where NaNs win out over numerics there is no difference between sNaN and qNaN handling.
One other question is are zeros ordered? The IEEE specification does not provide for any ordering but several other language specifications (Java, Go) have a similar definition to what is proposed except that zeros are ordered.
These are minute details that most people probably will never care about, but because of all of the differences it makes it difficult for CPU ISA designers to create efficient instructions for accelerating these operations.
The text was updated successfully, but these errors were encountered: