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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Implements `PartialOrd` for vector types.
//!
//! This implements a lexicographical order.

macro_rules! impl_cmp_partial_ord {
    ([$elem_ty:ident; $elem_count:expr]: $id:ident | $test_tt:tt) => {
        impl $id {
            /// Returns a wrapper that implements `PartialOrd`.
            #[inline]
            pub fn partial_lex_ord(&self) -> LexicographicallyOrdered<$id> {
                LexicographicallyOrdered(*self)
            }
        }

        impl crate::cmp::PartialOrd<LexicographicallyOrdered<$id>>
            for LexicographicallyOrdered<$id>
        {
            #[inline]
            fn partial_cmp(
                &self, other: &Self,
            ) -> Option<crate::cmp::Ordering> {
                if PartialEq::eq(self, other) {
                    Some(crate::cmp::Ordering::Equal)
                } else if PartialOrd::lt(self, other) {
                    Some(crate::cmp::Ordering::Less)
                } else if PartialOrd::gt(self, other) {
                    Some(crate::cmp::Ordering::Greater)
                } else {
                    None
                }
            }
            #[inline]
            fn lt(&self, other: &Self) -> bool {
                let m_lt = self.0.lt(other.0);
                let m_eq = self.0.eq(other.0);
                for i in 0..$id::lanes() {
                    if m_eq.extract(i) {
                        continue;
                    }
                    return m_lt.extract(i);
                }
                false
            }
            #[inline]
            fn le(&self, other: &Self) -> bool {
                self.lt(other) | PartialEq::eq(self, other)
            }
            #[inline]
            fn ge(&self, other: &Self) -> bool {
                self.gt(other) | PartialEq::eq(self, other)
            }
            #[inline]
            fn gt(&self, other: &Self) -> bool {
                let m_gt = self.0.gt(other.0);
                let m_eq = self.0.eq(other.0);
                for i in 0..$id::lanes() {
                    if m_eq.extract(i) {
                        continue;
                    }
                    return m_gt.extract(i);
                }
                false
            }
        }
    };
}

macro_rules! test_cmp_partial_ord_int {
    ([$elem_ty:ident; $elem_count:expr]: $id:ident | $test_tt:tt) => {
        test_if!{
            $test_tt:
            paste::item! {
                pub mod [<$id _cmp_PartialOrd>] {
                    use super::*;
                    #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn partial_lex_ord() {
                        use crate::testing::utils::{test_cmp};
                        // constant values
                        let a = $id::splat(0);
                        let b = $id::splat(1);

                        test_cmp(a.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Less));
                        test_cmp(b.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Greater));
                        test_cmp(a.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Equal));
                        test_cmp(b.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Equal));

                        // variable values: a = [0, 1, 2, 3]; b = [3, 2, 1, 0]
                        let mut a = $id::splat(0);
                        let mut b = $id::splat(0);
                        for i in 0..$id::lanes() {
                            a = a.replace(i, i as $elem_ty);
                            b = b.replace(i, ($id::lanes() - i) as $elem_ty);
                        }
                        test_cmp(a.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Less));
                        test_cmp(b.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Greater));
                        test_cmp(a.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Equal));
                        test_cmp(b.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Equal));

                        // variable values: a = [0, 1, 2, 3]; b = [0, 1, 2, 4]
                        let mut b = a;
                        b = b.replace(
                            $id::lanes() - 1,
                            a.extract($id::lanes() - 1) + 1 as $elem_ty
                        );
                        test_cmp(a.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Less));
                        test_cmp(b.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Greater));
                        test_cmp(a.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Equal));
                        test_cmp(b.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(crate::cmp::Ordering::Equal));

                        if $id::lanes() > 2 {
                            // variable values a = [0, 1, 0, 0]; b = [0, 1, 2, 3]
                            let b = a;
                            let mut a = $id::splat(0);
                            a = a.replace(1, 1 as $elem_ty);
                            test_cmp(a.partial_lex_ord(), b.partial_lex_ord(),
                                     Some(crate::cmp::Ordering::Less));
                            test_cmp(b.partial_lex_ord(), a.partial_lex_ord(),
                                     Some(crate::cmp::Ordering::Greater));
                            test_cmp(a.partial_lex_ord(), a.partial_lex_ord(),
                                     Some(crate::cmp::Ordering::Equal));
                            test_cmp(b.partial_lex_ord(), b.partial_lex_ord(),
                                     Some(crate::cmp::Ordering::Equal));

                            // variable values: a = [0, 1, 2, 3]; b = [0, 1, 3, 2]
                            let mut b = a;
                            b = b.replace(
                                2, a.extract($id::lanes() - 1) + 1 as $elem_ty
                            );
                            test_cmp(a.partial_lex_ord(), b.partial_lex_ord(),
                                     Some(crate::cmp::Ordering::Less));
                            test_cmp(b.partial_lex_ord(), a.partial_lex_ord(),
                                     Some(crate::cmp::Ordering::Greater));
                            test_cmp(a.partial_lex_ord(), a.partial_lex_ord(),
                                     Some(crate::cmp::Ordering::Equal));
                            test_cmp(b.partial_lex_ord(), b.partial_lex_ord(),
                                     Some(crate::cmp::Ordering::Equal));
                        }
                    }
                }
            }
        }
    };
}

macro_rules! test_cmp_partial_ord_mask {
    ([$elem_ty:ident; $elem_count:expr]: $id:ident | $test_tt:tt) => {
        test_if!{
            $test_tt:
            paste::item! {
                pub mod [<$id _cmp_PartialOrd>] {
                    use super::*;
                    #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn partial_lex_ord() {
                        use crate::testing::utils::{test_cmp};
                        use crate::cmp::Ordering;

                        // constant values
                        let a = $id::splat(false);
                        let b = $id::splat(true);

                        test_cmp(a.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(Ordering::Less));
                        test_cmp(b.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(Ordering::Greater));
                        test_cmp(a.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(Ordering::Equal));
                        test_cmp(b.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(Ordering::Equal));

                        // variable values:
                        // a = [false, false, false, false];
                        // b = [false, false, false, true]
                        let a = $id::splat(false);
                        let mut b = $id::splat(false);
                        b = b.replace($id::lanes() - 1, true);
                        test_cmp(a.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(Ordering::Less));
                        test_cmp(b.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(Ordering::Greater));
                        test_cmp(a.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(Ordering::Equal));
                        test_cmp(b.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(Ordering::Equal));

                        // variable values:
                        // a = [true, true, true, false];
                        // b = [true, true, true, true]
                        let mut a = $id::splat(true);
                        let b = $id::splat(true);
                        a = a.replace($id::lanes() - 1, false);
                        test_cmp(a.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(Ordering::Less));
                        test_cmp(b.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(Ordering::Greater));
                        test_cmp(a.partial_lex_ord(), a.partial_lex_ord(),
                                 Some(Ordering::Equal));
                        test_cmp(b.partial_lex_ord(), b.partial_lex_ord(),
                                 Some(Ordering::Equal));

                        if $id::lanes() > 2 {
                            // variable values
                            // a = [false, true, false, false];
                            // b = [false, true, true, true]
                            let mut a = $id::splat(false);
                            let mut b = $id::splat(true);
                            a = a.replace(1, true);
                            b = b.replace(0, false);
                            test_cmp(a.partial_lex_ord(), b.partial_lex_ord(),
                                     Some(Ordering::Less));
                            test_cmp(b.partial_lex_ord(), a.partial_lex_ord(),
                                     Some(Ordering::Greater));
                            test_cmp(a.partial_lex_ord(), a.partial_lex_ord(),
                                     Some(Ordering::Equal));
                            test_cmp(b.partial_lex_ord(), b.partial_lex_ord(),
                                     Some(Ordering::Equal));
                        }
                    }
                }
            }
        }
    };
}