Use xor to implement Neg::neg for floats #31
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This returns the correct on all architectures, and avoids raising float exceptions, canonicalizing values, and setting status registers no matter the argument in both debug/release builds. This is required by IEEE-754, see1
Note that without the impl change, I believe we'd need to bring in the CI changes from #30 to actually trigger a failure, but https://github.com/thomcc/stdsimd/runs/1220021695 has examples of the failure cases (don't look at the ones that failed to build, I'm referring to the ones that failed the test this adds).
Ultimately though, this is a placeholder until some sort of neg intrinsic is provided by rustc, as while this is fairly optimal on Intel, that is not true for ARM. That said, this fixes the issue with the old implementation, even if it's not optimal everywhere.
1: From IEEE754-2019 section 5.5.1 "Sign bit operations"
It also says (when introducing negate/abs/copysign) a few lines earlier:
Note that subtraction/multiplication would violate these requirements.