-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add is_signaling_nan()
to std::f32
and std::f64
#48825
Comments
IIRC we support some targets that aren't IEEE 754-2008 (mips maybe?), so the implementation may need have a bit of cfg(target_arch) applied. |
This issue is related: #10186 |
I wonder whether the platform-dependentness is relevant for this use case. Since wasm is supposed to be platform independent and explicitly follows IEEE 754-2008, wouldn't wasm implementations want to consistently apply the IEEE 754-2008 rules? I tried to look up what wasm says on the subject but everything I found seems to indicate signaling NaNs are perfectly valid. |
Hmmm, it appears that I was misremembering the spec; the part I was thinking of is here: Note: Currently, on x86_64 Debian Linux (Testing), signaling NaN's appear to be ignored. I really hate relying on something like that though when I can't even find where that option in the kernel/whatever is set. |
IEEE specifies a default handling of "exceptions" (which are really nothing at all like exceptions in programming languages, traps in hardware, or anything like that). In the absence of the program explicitly choosing different handling (edit: which Rust doesn't offer, and which LLVM doesn't support yet anyway) in a certain scope, a conforming implementation should always handle the exceptions this way. For invalid operation exceptions (this is the exception signaled by operations on sNaN, but note that many other operations not involving sNaN are also covered by the same exception), this default handling consists of returning a qNaN. Those who have access to the standard can read this up in sections 7 and 8 (edit: 2008 edition). |
Oh, lovely. That makes life easy again. I'm still willing to add methods to the floating point types that recognize a signaling NaN on platforms that support them, if anyone wants them. If not, then this can be closed. |
If there's not a compelling use case, I'd err towards leaving them out until we see one. |
Older MIPS platforms treat signaling NaNs exactly the opposite to intel and IEEE754-2008. Newer MIPS standards switched however: first they made IEEE754-2008 NaNs optional and controllable via a flag, then they made that flag read-only and required it to be set, then they mandated IEEE754-2008 for everyone. Issue is though that MIPS is virtually dead, everyone uses ARM now so I couldn't find any CPU that used one of the newer MIPS iterations that have IEEE754-2008 behaviour. |
Triage: Sounds very reasonable. Let's close as won't fix. |
Use case: I'm writing a webassembly interpreter. Webassembly specifies that signaling NaN's are invalid. So, I must be able to detect signaling NaN's from my input and return an error when they occur. But, Rust doesn't offer a method for it, so I had to write my own bit-twiddling implementations. This isn't hard, but it seems like it should exist already.
I first brought this up in the
num
crate, here: rust-num/num#364 , but they can't add it to theFloat
trait without breaking backwards compatibility, and the exact definition of a signaling NaN is quite architecture-dependent (or at least more so than usual) so it seems like maybe it belongs in std anyway.For any platform that implements IEEE 754-2008 the following implementations should suffice:
However, I think LLVM has intrinsics for this too, if I'm looking in the right place: https://llvm.org/doxygen/classllvm_1_1detail_1_1IEEEFloat.html#accc978f15db9b1b2b8e3ac171cbac4e3
I'll make an RFC if people want, but this didn't seem like a "significant change", so here it is. I'll be happy to make a PR for these too if desired.
The text was updated successfully, but these errors were encountered: