Make NaNs quiet by default, and make signalling NaNs distinct#10432
Make NaNs quiet by default, and make signalling NaNs distinct#10432LewisGaul wants to merge 3 commits intoziglang:masterfrom
Conversation
|
The failing |
|
I generally agree with cleaning up nan handling and defaulting to quiet nans but a few comments about this at a high-level. This has some good reading on an overview of how signalling/quiet nans are differentiated in C. The key takeaways are:
All binary NaN bit strings have all the bits of the biased exponent field E set to 1 (see 3.4). A quiet NaN bit I would probably go the route of removing explicit signalling nan support (isSignallingNan and snan constants) and only explicitly supporting quiet nans. Quiet nan constants we emit should then be based on the above as mentioned in the standard (e.g. 32-bit repr should be Perhaps we can add a function similar to |
Thanks, that helped my understanding of NaNs!
I understand that after reading the above. My understanding is that FP exceptions aren't properly supported in Zig yet, and I'd consider worrying about this to be out of the scope of this PR, would you agree?
Ah ok... Is this something we need to worry about, and if so what would you suggest doing about it?
I'm happy to go this route. I guess the point you're making is that my implementation of
This is what musl/glibc do, so yes this PR changes quiet NaNs to be IEEE-754 conformant.
I'd be happy to add that, if it's something we want. |
This is correct. This would require something like https://en.cppreference.com/w/c/numeric/fenv in general and ties in a bit to non-local program state to control fpu registers since by default I know some platforms don't have things like nan exceptions enabled.
We could probably push nan generation for any bit size behind a function for now and use the existing bit representation. I am not sure what this would look like for other platforms right now.
Correct. Along with removing the snan constants since as we don't actually provide a useful mechanism right now for managing this to a user, there would be a manual process anyway; so let's not give them something which they may assume work (raises fpu exceptions). We would just provide a "nan" which is always quiet. I would be interested to see some use cases of signalling nan in practice as I personally don't used. Right now if a user really needs signalling nan support they would need to use fenv from c anyway, in which case they can use some of the relevant c functions for managing imo, until we decide what this looks like in zig. |
|
@tiehuis thanks for working with @LewisGaul on this! Looking forward to seeing what you two come up with. I'm closing this PR because it's old, it has conflicts, and the CI was never passing, but of course please open a fresh one if you want to push forward. |
This allows the
std.mathfunctions to return the same NaN that matches GCC/Musl for non-NaN inputs (related: #10415).Note the implementation of the
nan()andsnan()functions could be made generic if/when #10133 is merged, which would be something that could be addressed in this PR...