Skip to content

Commit 0a46ca4

Browse files
authored
Merge pull request rust-lang#31 from thomcc/negxor
Use xor to implement Neg::neg for floats
2 parents a1c327a + ffd562f commit 0a46ca4

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

crates/core_simd/src/ops.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,20 @@ macro_rules! impl_op {
212212
impl core::ops::Neg for $type {
213213
type Output = Self;
214214
fn neg(self) -> Self::Output {
215-
<$type>::splat(-<$scalar>::default()) - self
215+
<$type>::splat(0) - self
216+
}
217+
}
218+
}
219+
};
220+
221+
{ impl Neg for $type:ty, $scalar:ty, @float } => {
222+
impl_ref_ops! {
223+
impl core::ops::Neg for $type {
224+
type Output = Self;
225+
fn neg(self) -> Self::Output {
226+
// FIXME: Replace this with fneg intrinsic once available.
227+
// https://github.com/rust-lang/stdsimd/issues/32
228+
Self::from_bits(<$type>::splat(-0.0).to_bits() ^ self.to_bits())
216229
}
217230
}
218231
}
@@ -310,7 +323,7 @@ macro_rules! impl_float_ops {
310323
impl_op! { impl Mul for $vector, $scalar }
311324
impl_op! { impl Div for $vector, $scalar }
312325
impl_op! { impl Rem for $vector, $scalar }
313-
impl_op! { impl Neg for $vector, $scalar }
326+
impl_op! { impl Neg for $vector, $scalar, @float }
314327
impl_op! { impl Index for $vector, $scalar }
315328
)*
316329
)*

crates/core_simd/tests/ops_impl/float_macros.rs

+9
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@ macro_rules! float_tests {
289289
assert_biteq!(-v, expected);
290290
}
291291

292+
#[test]
293+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
294+
fn neg_odd_floats() {
295+
for v in slice_chunks(&C) {
296+
let expected = apply_unary_lanewise(v, core::ops::Neg::neg);
297+
assert_biteq!(-v, expected);
298+
}
299+
}
300+
292301
#[test]
293302
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
294303
fn abs_negative() {

0 commit comments

Comments
 (0)