• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*!
2 Wrapper routines for `memchr` and friends.
3 
4 These routines choose the best implementation at compile time. (This is
5 different from `x86_64` because it is expected that `simd128` is almost always
6 available for `wasm32` targets.)
7 */
8 
9 macro_rules! defraw {
10     ($ty:ident, $find:ident, $start:ident, $end:ident, $($needles:ident),+) => {{
11         use crate::arch::wasm32::simd128::memchr::$ty;
12 
13         debug!("chose simd128 for {}", stringify!($ty));
14         debug_assert!($ty::is_available());
15         // SAFETY: We know that wasm memchr is always available whenever
16         // code is compiled for `wasm32` with the `simd128` target feature
17         // enabled.
18         $ty::new_unchecked($($needles),+).$find($start, $end)
19     }}
20 }
21 
22 /// memchr, but using raw pointers to represent the haystack.
23 ///
24 /// # Safety
25 ///
26 /// Pointers must be valid. See `One::find_raw`.
27 #[inline(always)]
memchr_raw( n1: u8, start: *const u8, end: *const u8, ) -> Option<*const u8>28 pub(crate) unsafe fn memchr_raw(
29     n1: u8,
30     start: *const u8,
31     end: *const u8,
32 ) -> Option<*const u8> {
33     defraw!(One, find_raw, start, end, n1)
34 }
35 
36 /// memrchr, but using raw pointers to represent the haystack.
37 ///
38 /// # Safety
39 ///
40 /// Pointers must be valid. See `One::rfind_raw`.
41 #[inline(always)]
memrchr_raw( n1: u8, start: *const u8, end: *const u8, ) -> Option<*const u8>42 pub(crate) unsafe fn memrchr_raw(
43     n1: u8,
44     start: *const u8,
45     end: *const u8,
46 ) -> Option<*const u8> {
47     defraw!(One, rfind_raw, start, end, n1)
48 }
49 
50 /// memchr2, but using raw pointers to represent the haystack.
51 ///
52 /// # Safety
53 ///
54 /// Pointers must be valid. See `Two::find_raw`.
55 #[inline(always)]
memchr2_raw( n1: u8, n2: u8, start: *const u8, end: *const u8, ) -> Option<*const u8>56 pub(crate) unsafe fn memchr2_raw(
57     n1: u8,
58     n2: u8,
59     start: *const u8,
60     end: *const u8,
61 ) -> Option<*const u8> {
62     defraw!(Two, find_raw, start, end, n1, n2)
63 }
64 
65 /// memrchr2, but using raw pointers to represent the haystack.
66 ///
67 /// # Safety
68 ///
69 /// Pointers must be valid. See `Two::rfind_raw`.
70 #[inline(always)]
memrchr2_raw( n1: u8, n2: u8, start: *const u8, end: *const u8, ) -> Option<*const u8>71 pub(crate) unsafe fn memrchr2_raw(
72     n1: u8,
73     n2: u8,
74     start: *const u8,
75     end: *const u8,
76 ) -> Option<*const u8> {
77     defraw!(Two, rfind_raw, start, end, n1, n2)
78 }
79 
80 /// memchr3, but using raw pointers to represent the haystack.
81 ///
82 /// # Safety
83 ///
84 /// Pointers must be valid. See `Three::find_raw`.
85 #[inline(always)]
memchr3_raw( n1: u8, n2: u8, n3: u8, start: *const u8, end: *const u8, ) -> Option<*const u8>86 pub(crate) unsafe fn memchr3_raw(
87     n1: u8,
88     n2: u8,
89     n3: u8,
90     start: *const u8,
91     end: *const u8,
92 ) -> Option<*const u8> {
93     defraw!(Three, find_raw, start, end, n1, n2, n3)
94 }
95 
96 /// memrchr3, but using raw pointers to represent the haystack.
97 ///
98 /// # Safety
99 ///
100 /// Pointers must be valid. See `Three::rfind_raw`.
101 #[inline(always)]
memrchr3_raw( n1: u8, n2: u8, n3: u8, start: *const u8, end: *const u8, ) -> Option<*const u8>102 pub(crate) unsafe fn memrchr3_raw(
103     n1: u8,
104     n2: u8,
105     n3: u8,
106     start: *const u8,
107     end: *const u8,
108 ) -> Option<*const u8> {
109     defraw!(Three, rfind_raw, start, end, n1, n2, n3)
110 }
111 
112 /// Count all matching bytes, but using raw pointers to represent the haystack.
113 ///
114 /// # Safety
115 ///
116 /// Pointers must be valid. See `One::count_raw`.
117 #[inline(always)]
count_raw( n1: u8, start: *const u8, end: *const u8, ) -> usize118 pub(crate) unsafe fn count_raw(
119     n1: u8,
120     start: *const u8,
121     end: *const u8,
122 ) -> usize {
123     defraw!(One, count_raw, start, end, n1)
124 }
125