vsimd/
mask.rs

1use crate::isa::{AVX2, NEON, SSE2, WASM128};
2use crate::vector::{V128, V256};
3use crate::{SIMD128, SIMD256};
4
5use core::ops::Not;
6
7#[inline(always)]
8pub fn mask8x16_all<S: SIMD128>(s: S, x: V128) -> bool {
9    if matches_isa!(S, SSE2 | WASM128) {
10        return s.u8x16_bitmask(x) == u16::MAX;
11    }
12    if matches_isa!(S, NEON) {
13        if cfg!(target_arch = "arm") {
14            return s.u8x16_any_zero(x).not();
15        }
16        if cfg!(target_arch = "aarch64") {
17            return s.u8x16_reduce_min(x) != 0;
18        }
19    }
20    unreachable!()
21}
22
23#[inline(always)]
24pub fn mask8x32_all<S: SIMD256>(s: S, x: V256) -> bool {
25    if matches_isa!(S, AVX2) {
26        return s.u8x32_bitmask(x) == u32::MAX;
27    }
28    if matches_isa!(S, SSE2 | WASM128 | NEON) {
29        let x = x.to_v128x2();
30        let x = s.v128_and(x.0, x.1);
31        return mask8x16_all(s, x);
32    }
33    unreachable!()
34}
35
36#[inline(always)]
37pub fn mask8x16_any<S: SIMD128>(s: S, x: V128) -> bool {
38    if matches_isa!(S, SSE2 | WASM128) {
39        return s.u8x16_bitmask(x) != 0;
40    }
41    if matches_isa!(S, NEON) {
42        return s.v128_all_zero(x).not();
43    }
44    unreachable!()
45}
46
47#[inline(always)]
48pub fn mask8x32_any<S: SIMD256>(s: S, x: V256) -> bool {
49    if matches_isa!(S, AVX2) {
50        return s.u8x32_bitmask(x) != 0;
51    }
52    if matches_isa!(S, SSE2 | WASM128 | NEON) {
53        let x = x.to_v128x2();
54        let x = s.v128_or(x.0, x.1);
55        return mask8x16_any(s, x);
56    }
57    unreachable!()
58}
59
60#[inline(always)]
61pub fn u8x16_highbit_all<S: SIMD128>(s: S, x: V128) -> bool {
62    if matches_isa!(S, SSE2 | WASM128) {
63        return s.u8x16_bitmask(x) == u16::MAX;
64    }
65    if matches_isa!(S, NEON) {
66        if cfg!(target_arch = "arm") {
67            return mask8x16_all(s, s.i8x16_lt(x, s.v128_create_zero()));
68        }
69        if cfg!(target_arch = "aarch64") {
70            return s.u8x16_reduce_min(x) >= 0x80;
71        }
72    }
73    unreachable!()
74}
75
76#[inline(always)]
77pub fn u8x32_highbit_all<S: SIMD256>(s: S, x: V256) -> bool {
78    if matches_isa!(S, AVX2) {
79        return s.u8x32_bitmask(x) == u32::MAX;
80    }
81    if matches_isa!(S, SSE2 | WASM128 | NEON) {
82        let x = x.to_v128x2();
83        let x = s.v128_and(x.0, x.1);
84        return u8x16_highbit_all(s, x);
85    }
86    unreachable!()
87}
88
89#[inline(always)]
90pub fn u8x16_highbit_any<S: SIMD128>(s: S, x: V128) -> bool {
91    if matches_isa!(S, SSE2 | WASM128) {
92        return s.u8x16_bitmask(x) != 0;
93    }
94    if matches_isa!(S, NEON) {
95        if cfg!(target_arch = "arm") {
96            return mask8x16_any(s, s.i8x16_lt(x, s.v128_create_zero()));
97        }
98        if cfg!(target_arch = "aarch64") {
99            return s.u8x16_reduce_max(x) >= 0x80;
100        }
101    }
102    unreachable!()
103}
104
105#[inline(always)]
106pub fn u8x32_highbit_any<S: SIMD256>(s: S, x: V256) -> bool {
107    if matches_isa!(S, AVX2) {
108        return s.u8x32_bitmask(x) != 0;
109    }
110    if matches_isa!(S, SSE2 | WASM128 | NEON) {
111        let x = x.to_v128x2();
112        let x = s.v128_or(x.0, x.1);
113        return u8x16_highbit_any(s, x);
114    }
115    unreachable!()
116}