Skip to content

Conversation

@thomcc
Copy link
Member

@thomcc thomcc commented Oct 7, 2020

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"

negate(x) copies a floating-point operand x to a destination in the same format, reversing the sign bit. negate(x) is not the same as subtraction(0, x)

It also says (when introducing negate/abs/copysign) a few lines earlier:

The operations treat floating-point numbers and NaNs alike, and signal no exception. These operations may propagate non-canonical encodings.

Note that subtraction/multiplication would violate these requirements.

type Output = Self;
fn neg(self) -> Self::Output {
<$type>::splat(-<$scalar>::default()) - self
<$type>::splat(0) - self
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this line to {integer} 0 and the line later to -0.0 so that if you forgot or wrongly added the @float you'd get a compile error.

@thomcc thomcc changed the title Use xor to implement Neg::nex for floats Use xor to implement Neg::neg for floats Oct 7, 2020
@thomcc
Copy link
Member Author

thomcc commented Oct 7, 2020

travis seems to have stalled, weirdly.

@Lokathor Lokathor merged commit 0a46ca4 into rust-lang:master Oct 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants