Skip to content

Commit

Permalink
Merge pull request rust-lang#31 from thomcc/negxor
Browse files Browse the repository at this point in the history
Use xor to implement Neg::neg for floats
  • Loading branch information
Lokathor authored Oct 8, 2020
2 parents a1c327a + ffd562f commit 0a46ca4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
17 changes: 15 additions & 2 deletions crates/core_simd/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,20 @@ macro_rules! impl_op {
impl core::ops::Neg for $type {
type Output = Self;
fn neg(self) -> Self::Output {
<$type>::splat(-<$scalar>::default()) - self
<$type>::splat(0) - self
}
}
}
};

{ impl Neg for $type:ty, $scalar:ty, @float } => {
impl_ref_ops! {
impl core::ops::Neg for $type {
type Output = Self;
fn neg(self) -> Self::Output {
// FIXME: Replace this with fneg intrinsic once available.
// https://github.com/rust-lang/stdsimd/issues/32
Self::from_bits(<$type>::splat(-0.0).to_bits() ^ self.to_bits())
}
}
}
Expand Down Expand Up @@ -310,7 +323,7 @@ macro_rules! impl_float_ops {
impl_op! { impl Mul for $vector, $scalar }
impl_op! { impl Div for $vector, $scalar }
impl_op! { impl Rem for $vector, $scalar }
impl_op! { impl Neg for $vector, $scalar }
impl_op! { impl Neg for $vector, $scalar, @float }
impl_op! { impl Index for $vector, $scalar }
)*
)*
Expand Down
9 changes: 9 additions & 0 deletions crates/core_simd/tests/ops_impl/float_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ macro_rules! float_tests {
assert_biteq!(-v, expected);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn neg_odd_floats() {
for v in slice_chunks(&C) {
let expected = apply_unary_lanewise(v, core::ops::Neg::neg);
assert_biteq!(-v, expected);
}
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn abs_negative() {
Expand Down

0 comments on commit 0a46ca4

Please sign in to comment.