Fix NaN payload unittest in errorfunction#3976
Conversation
I guess discussion should happen here for a wider audience. Please note that dmd has the same issue when working with doubles instead of reals (on targets that use simd instructions for floating point): This fails with optimizations. The problem is that the optimizer uses |
|
This seems reasonable. |
|
@rainers: How do you define |
As much as I understand it, an |
|
Only the second assert fails for me |
std/math.d
Outdated
| * Returns: true if x isNaN with payload | ||
| */ | ||
| bool isNaNWithPayload(real x, ulong payload) @safe pure nothrow @nogc | ||
| { |
There was a problem hiding this comment.
I think a comment would be nice why this is necessary at all, as it is not obvious. For example "some compilers implement '-x' by a bit operation on the sign bit, but this is inaccurate for NaNs. So better skip the sign bit when comparing the result of passing a NaN through some calculus."
|
@smolt could you please update this according to the comment https://github.com/D-Programming-Language/phobos/pull/3976/files#r52418960? This is also a chance for others to object before I merge it... |
|
@rainers ok, I'll add a comment. It could be a more general statement because C99 says this about C math functions:
and from wiki article on IEEE Floating Point:
I think An alternative is to not add the new math function and just change the unittest to check for nan and then extract and compare payload. |
|
@smolt Interesting. I wasn't aware that this isuue is even covered by the C standard.
Yeah, I guess it should just do what the name says: Making it a local function in the test should be fine, too. |
|
I think I will do that, make it local to the test with your suggestion. I am away, you could just create new PR and close this one. I may not get to it until Saturday. |
Doesn't work writing |
|
I'll fix on this PR unless some thinks it should be a separate PR. |
Shift was off-by-one.
For some platforms, math on a NaN may change the sign. This is ok according to C99. Replaced with test that ignores signbit.
333af84 to
b938a74
Compare
|
Ok, reworked along with fix for As a separate issue, compile std.math unittest with -O -debug=UnitTest and there appears to be some sort of dmd optimization problem. Is if familiar? It could explain why the unittest is disabled in a debug block. I'll create a bug report if needed. |
dmd takes the liberty to omit the reduction to double before calling getNaNPayload. I think that's ok for the optimizer to do. Making nan5 and nan6 static variables avoids this optimization and makes the (no-longer-pure) unittest pass. There's also strange indentation at the end of getNaNPayload, it would be nice if you could fix that along the way.
I think a bugzilla issue should be created for the fix to getNaNPayload. |
Just realized that the fixed branch never affects dmd because it always has 80-bit reals. So I'll go ahead and merge this as is, other stuff can go into a separate PR. |
|
Auto-merge toggled on |
It's on my TODO list. |
|
did this stall? |
|
It seems the build server forgot the merge state. |
|
Auto-merge toggled on |
This is a mod that allows std.internal.math.errorfunction unittest to pass on several other platforms.
For some platforms, math on a NaN may change the sign. Replaced with test that ignores signbit for NaN.
Is this the appropriate? We are discussing it here: ldc-developers#25.