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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
pub(crate) const WIDTH: usize = 16;
#[macro_use]
mod cfg_macros;

#[cfg(all(mirai, not(feature = "verify")))]
compile_error!(
    "To run mirai on `swift-check` you must enable the `verify` feature: \n\
     `cargo mirai --tests --features verify`"
);

cfg_verify!(
    pub(crate) fn is_aligned(ptr: *const Ptr) -> bool {
        byte_ptr(ptr).align_offset(WIDTH) == 0
    }
    macro_rules! contract {
        ($kind:ident!($args:expr)) => {
            mirai_annotations::$kind!($args)
        };
        ($expr:expr) => {
            $expr
        };
    }
);

cfg_runtime!(
    #[allow(unused_macros)]
    macro_rules! contract {
        ($kind:ident!($args:expr)) => {};
        ($expr:expr) => {};
    }
);

cfg_neon!(
    pub mod aarch64;
    pub use aarch64 as arch;
);

cfg_sse!(
    pub mod x86_64;
    pub use x86_64 as arch;
);

cfg_simd128!(
    pub mod wasm;
    pub use wasm as arch;
);

cfg_fallback!(
    pub mod fallback;
    pub use fallback as arch;
);

#[doc(hidden)]
pub use arch::{
    eq, not, xor, or, and, splat, byte_ptr, simd_ptr, load_partial, load_aligned, maybe_aligned_load
};

#[doc(hidden)]
pub use arch::{MoveMask, Ptr, STEP, STEP_SIZE};

pub use arch::Vector;
pub use arch::{load, load_unchecked};

cfg_simd!(
    #[doc(hidden)]
    pub mod simd_scan;
    #[doc(hidden)]
    pub use simd_scan as scan;
);

cfg_fallback!(
    #[doc(hidden)]
    pub mod fallback_scan;
    #[doc(hidden)]
    pub use fallback_scan as scan;
);

cfg_i8!(
    macro_rules! impl_cmp {
        (
            $CONST:ident,
            overflow => $handle_overflow:expr,
            default => $handle_non_overflow:expr
            $(, max => $handle_max:expr)?
            $(, min => $handle_min:expr)?
            $(,)?
        ) => {
            match $CONST {
                $(255 => $handle_max,)?
                128..=255 => $handle_overflow,
                $(0 => $handle_min,)?
                _ => $handle_non_overflow
            }
        };
    }

    macro_rules! impl_gt {
        ($MIN:ident, $gt:ident, $handle_max:expr $(, $handle_min:expr)? $(,)?) => {
            impl_cmp!(
                $MIN,
                overflow => {
                    // ensure that the value is less than 0 and greater than MAX
                    move |data| {
                        and(
                            arch::less_than(data, splat(0)),
                            arch::$gt(data, splat($MIN))
                        )
                    }
                },
                default => {
                    // everything else we need to check that the value is either below 0 or greater
                    // than max
                    move |data| {
                        or(
                            arch::less_than(data, splat(0)),
                            arch::$gt(data, splat($MIN))
                        )
                    }
                },
                max => $handle_max
                $(, min => $handle_min)?
            )
        };
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn greater_than<const MIN: u8>() -> impl Fn(Vector) -> Vector {
        unsafe { impl_gt!(
            MIN,
            greater_than,
            move |_| { splat(0) }
        ) }
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn greater_than_or_eq<const MIN: u8>() -> impl Fn(Vector) -> Vector {
        unsafe { impl_gt!(
            MIN,
            greater_than_or_eq,
            move |data| { eq(data, splat(255)) },
            move |_| {
                // gt or eq 0 would always be true, splat all ones
                splat(0xFF)
            }
        ) }
    }

    macro_rules! impl_lt {
        ($MAX:ident, $lt:ident, $handle_min:expr $(, $handle_max:expr)? $(,)?) => {
            impl_cmp!(
                $MAX,
                overflow => move |data| unsafe {
                    // overflow, so less than 127 OR MIN
                    or(
                        arch::greater_than_or_eq(data, splat(0)),
                        arch::$lt(data, splat($MAX))
                    )
                },
                default => move |data| unsafe {
                    // no overflow, but data coming in could have, so greater than 0 and less than
                    // MIN
                    and(
                        arch::greater_than_or_eq(data, splat(0)),
                        arch::$lt(data, splat(MAX))
                    )
                },
                $(max => $handle_max,)?
                min => $handle_min
            )
        };
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn less_than<const MAX: u8>() -> impl Fn(Vector) -> Vector {
        impl_lt!(
            MAX,
            less_than,
            move |_| unsafe {
                // always fails
                splat(0)
            }
        )
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn less_than_or_eq<const MAX: u8>() -> impl Fn(Vector) -> Vector {
        impl_lt!(
            MAX,
            less_than_or_eq,
            move |data| unsafe {
                // lt or eq 0 is practically just eq 0
                eq(data, splat(0))
            },
            move |_| unsafe {
                // lt or eq maximum value is always true
                splat(0xFF)
            }
        )
    }

    macro_rules! impl_range_cast {
        ($MIN:ident, $MAX:ident, $gt:ident, $lt:ident, $eq:expr, $max_128:expr) => {
            match ($MIN, $MAX) {
                _ if $MIN == $MAX => $eq,
                (0..=127, 129..=255) => {
                    // gt min OR lt MAX
                    move |data| unsafe {
                        or(
                            arch::$gt(data, splat($MIN)),
                            arch::$lt(data, splat($MAX))
                        )
                    }
                },
                (0..=127, 128) => $max_128,
                _ => {
                    // gte min AND lte MAX
                    move |data| unsafe {
                        and(
                            arch::$gt(data, splat($MIN)),
                            arch::$lt(data, splat($MAX))
                        )
                    }
                }
            }
        };
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn range<const MIN: u8, const MAX: u8>() -> impl Fn(Vector) -> Vector {
        impl_range_cast!(MIN, MAX, greater_than_or_eq, less_than_or_eq,
            move |data| unsafe { eq(data, splat(MIN)) },
            move |data| unsafe {
                or(
                    arch::greater_than_or_eq(data, splat(MIN)),
                    eq(data, splat(MAX))
                )
            }
        )
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn exclusive_range<const MIN: u8, const MAX: u8>() -> impl Fn(Vector) -> Vector {
        if MIN.abs_diff(MAX) == 1 {
            move |_data: Vector| -> Vector { unsafe { splat(0) } }
        } else {
            impl_range_cast!(
                MIN, MAX, greater_than, less_than, move |_| unsafe { splat(0) },
                move |data| unsafe { arch::greater_than(data, splat(MIN)) }
            )
        }
    }
);

cfg_u8!(
    #[doc(hidden)] #[inline(always)]
    pub const fn less_than<const MAX: u8>() -> impl Fn(Vector) -> Vector {
        match MAX {
            0 => move |_| unsafe {
                // always false
                splat(0)
            },
            _ => move |data| unsafe {
                arch::less_than(data, splat(MAX))
            }
        }
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn less_than_or_eq<const MAX: u8>() -> impl Fn(Vector) -> Vector {
        match MAX {
            255 => move |_| unsafe {
                // always true
                splat(0xFF)
            },
            _ => move |data| unsafe {
                arch::less_than_or_eq(data, splat(MAX))
            }
        }
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn greater_than<const MIN: u8>() -> impl Fn(Vector) -> Vector {
        match MIN {
            255 => move |_| unsafe {
                // always false
                splat(0)
            },
            _ => move |data| unsafe {
                arch::greater_than(data, splat(MIN))
            }
        }
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn greater_than_or_eq<const MIN: u8>() -> impl Fn(Vector) -> Vector {
        match MIN {
            0 => move |_| unsafe {
                // always true
                splat(0xFF)
            },
            _ => move |data| unsafe {
                arch::greater_than_or_eq(data, splat(MIN))
            }
        }
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn range<const MIN: u8, const MAX: u8>() -> impl Fn(Vector) -> Vector {
        match (MIN, MAX) {
            _ if MIN == MAX => move |data| unsafe { eq(data, splat(MIN)) },
            _ => move |data| unsafe {
                and(
                    arch::greater_than_or_eq(data, splat(MIN)),
                    arch::less_than_or_eq(data, splat(MAX))
                )
            }
        }
    }

    #[doc(hidden)] #[inline(always)]
    pub const fn exclusive_range<const MIN: u8, const MAX: u8>() -> impl Fn(Vector) -> Vector {
        match (MIN, MAX, MIN.abs_diff(MAX)) {
            (_, _, 1) |
            (_, _, _) if MIN == MAX => move |_| unsafe { splat(0) },
            _ => move |data| unsafe {
                and(
                    arch::greater_than(data, splat(MIN)),
                    arch::less_than(data, splat(MAX))
                )
            }
        }
    }
);