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
use crate::simd::intrinsics;
use crate::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount};
use core::ops::{Neg, Not}; // 一元运算

macro_rules! neg {
    ($(impl<const LANES: usize> Neg for Simd<$scalar:ty, LANES>)*) => {
        $(impl<const LANES: usize> Neg for Simd<$scalar, LANES>
        where
            $scalar: SimdElement,
            LaneCount<LANES>: SupportedLaneCount,
        {
            type Output = Self;

            #[inline]
            #[must_use = "operator returns a new vector without mutating the input"]
            fn neg(self) -> Self::Output {
                // 安全性: `self` 是有符号的 vector
                unsafe { intrinsics::simd_neg(self) }
            }
        })*
    }
}

neg! {
    impl<const LANES: usize> Neg for Simd<f32, LANES>

    impl<const LANES: usize> Neg for Simd<f64, LANES>

    impl<const LANES: usize> Neg for Simd<i8, LANES>

    impl<const LANES: usize> Neg for Simd<i16, LANES>

    impl<const LANES: usize> Neg for Simd<i32, LANES>

    impl<const LANES: usize> Neg for Simd<i64, LANES>

    impl<const LANES: usize> Neg for Simd<isize, LANES>
}

macro_rules! not {
    ($(impl<const LANES: usize> Not for Simd<$scalar:ty, LANES>)*) => {
        $(impl<const LANES: usize> Not for Simd<$scalar, LANES>
        where
            $scalar: SimdElement,
            LaneCount<LANES>: SupportedLaneCount,
        {
            type Output = Self;

            #[inline]
            #[must_use = "operator returns a new vector without mutating the input"]
            fn not(self) -> Self::Output {
                self ^ (Simd::splat(!(0 as $scalar)))
            }
        })*
    }
}

not! {
    impl<const LANES: usize> Not for Simd<i8, LANES>

    impl<const LANES: usize> Not for Simd<i16, LANES>

    impl<const LANES: usize> Not for Simd<i32, LANES>

    impl<const LANES: usize> Not for Simd<i64, LANES>

    impl<const LANES: usize> Not for Simd<isize, LANES>

    impl<const LANES: usize> Not for Simd<u8, LANES>

    impl<const LANES: usize> Not for Simd<u16, LANES>

    impl<const LANES: usize> Not for Simd<u32, LANES>

    impl<const LANES: usize> Not for Simd<u64, LANES>

    impl<const LANES: usize> Not for Simd<usize, LANES>
}