Use bitxor to implement Neg for floats
This commit is contained in:
@@ -212,7 +212,18 @@ macro_rules! impl_op {
|
|||||||
impl core::ops::Neg for $type {
|
impl core::ops::Neg for $type {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn neg(self) -> Self::Output {
|
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 {
|
||||||
|
Self::from_bits(<$type>::splat(-0.0).to_bits() ^ self.to_bits())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -310,7 +321,7 @@ macro_rules! impl_float_ops {
|
|||||||
impl_op! { impl Mul for $vector, $scalar }
|
impl_op! { impl Mul for $vector, $scalar }
|
||||||
impl_op! { impl Div for $vector, $scalar }
|
impl_op! { impl Div for $vector, $scalar }
|
||||||
impl_op! { impl Rem 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 }
|
impl_op! { impl Index for $vector, $scalar }
|
||||||
)*
|
)*
|
||||||
)*
|
)*
|
||||||
|
|||||||
@@ -289,6 +289,15 @@ macro_rules! float_tests {
|
|||||||
assert_biteq!(-v, expected);
|
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]
|
#[test]
|
||||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn abs_negative() {
|
fn abs_negative() {
|
||||||
|
|||||||
Reference in New Issue
Block a user