1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
//! Vertical (lane-wise) vector-vector arithmetic operations.
macro_rules! impl_ops_vector_arithmetic {
([$elem_ty:ident; $elem_count:expr]: $id:ident | $test_tt:tt) => {
impl crate::ops::Add for $id {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
use crate::llvm::simd_add;
unsafe { Simd(simd_add(self.0, other.0)) }
}
}
impl crate::ops::Sub for $id {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
use crate::llvm::simd_sub;
unsafe { Simd(simd_sub(self.0, other.0)) }
}
}
impl crate::ops::Mul for $id {
type Output = Self;
#[inline]
fn mul(self, other: Self) -> Self {
use crate::llvm::simd_mul;
unsafe { Simd(simd_mul(self.0, other.0)) }
}
}
impl crate::ops::Div for $id {
type Output = Self;
#[inline]
fn div(self, other: Self) -> Self {
use crate::llvm::simd_div;
unsafe { Simd(simd_div(self.0, other.0)) }
}
}
impl crate::ops::Rem for $id {
type Output = Self;
#[inline]
fn rem(self, other: Self) -> Self {
use crate::llvm::simd_rem;
unsafe { Simd(simd_rem(self.0, other.0)) }
}
}
impl crate::ops::AddAssign for $id {
#[inline]
fn add_assign(&mut self, other: Self) {
*self = *self + other;
}
}
impl crate::ops::SubAssign for $id {
#[inline]
fn sub_assign(&mut self, other: Self) {
*self = *self - other;
}
}
impl crate::ops::MulAssign for $id {
#[inline]
fn mul_assign(&mut self, other: Self) {
*self = *self * other;
}
}
impl crate::ops::DivAssign for $id {
#[inline]
fn div_assign(&mut self, other: Self) {
*self = *self / other;
}
}
impl crate::ops::RemAssign for $id {
#[inline]
fn rem_assign(&mut self, other: Self) {
*self = *self % other;
}
}
test_if!{
$test_tt:
paste::item! {
pub mod [<$id _ops_vector_arith>] {
use super::*;
#[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn ops_vector_arithmetic() {
let z = $id::splat(0 as $elem_ty);
let o = $id::splat(1 as $elem_ty);
let t = $id::splat(2 as $elem_ty);
let f = $id::splat(4 as $elem_ty);
// add
assert_eq!(z + z, z);
assert_eq!(o + z, o);
assert_eq!(t + z, t);
assert_eq!(t + t, f);
// sub
assert_eq!(z - z, z);
assert_eq!(o - z, o);
assert_eq!(t - z, t);
assert_eq!(f - t, t);
assert_eq!(f - o - o, t);
// mul
assert_eq!(z * z, z);
assert_eq!(z * o, z);
assert_eq!(z * t, z);
assert_eq!(o * t, t);
assert_eq!(t * t, f);
// div
assert_eq!(z / o, z);
assert_eq!(t / o, t);
assert_eq!(f / o, f);
assert_eq!(t / t, o);
assert_eq!(f / t, t);
// rem
assert_eq!(o % o, z);
assert_eq!(f % t, z);
{
let mut v = z;
assert_eq!(v, z);
v += o; // add_assign
assert_eq!(v, o);
v -= o; // sub_assign
assert_eq!(v, z);
v = t;
v *= o; // mul_assign
assert_eq!(v, t);
v *= t;
assert_eq!(v, f);
v /= o; // div_assign
assert_eq!(v, f);
v /= t;
assert_eq!(v, t);
v %= t; // rem_assign
assert_eq!(v, z);
}
}
}
}
}
};
}