| 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 `neon` is almost always |
| 6 |
available for `aarch64` targets.) |
| 7 |
*/ |
| 8 |
|
| 9 |
macro_rules! defraw { |
| 10 |
($ty:ident, $find:ident, $start:ident, $end:ident, $($needles:ident),+) => {{ |
| 11 |
#[cfg(target_feature = "neon")] |
| 12 |
{ |
| 13 |
use crate::arch::aarch64::neon::memchr::$ty; |
| 14 |
|
| 15 |
debug!("chose neon for {}", stringify!($ty)); |
| 16 |
debug_assert!($ty::is_available()); |
| 17 |
// SAFETY: We know that wasm memchr is always available whenever |
| 18 |
// code is compiled for `aarch64` with the `neon` target feature |
| 19 |
// enabled. |
| 20 |
$ty::new_unchecked($($needles),+).$find($start, $end) |
| 21 |
} |
| 22 |
#[cfg(not(target_feature = "neon"))] |
| 23 |
{ |
| 24 |
use crate::arch::all::memchr::$ty; |
| 25 |
|
| 26 |
debug!( |
| 27 |
"no neon feature available, using fallback for {}", |
| 28 |
stringify!($ty), |
| 29 |
); |
| 30 |
$ty::new($($needles),+).$find($start, $end) |
| 31 |
} |
| 32 |
}} |
| 33 |
} |
| 34 |
|
| 35 |
/// memchr, but using raw pointers to represent the haystack. |
| 36 |
/// |
| 37 |
/// # Safety |
| 38 |
/// |
| 39 |
/// Pointers must be valid. See `One::find_raw`. |
| 40 |
#[inline(always)] |
| 41 |
pub(crate) unsafe fn memchr_raw( |
| 42 |
n1: u8, |
| 43 |
start: *const u8, |
| 44 |
end: *const u8, |
| 45 |
) -> Option<*const u8> { |
| 46 |
defraw!(One, find_raw, start, end, n1) |
| 47 |
} |
| 48 |
|
| 49 |
/// memrchr, but using raw pointers to represent the haystack. |
| 50 |
/// |
| 51 |
/// # Safety |
| 52 |
/// |
| 53 |
/// Pointers must be valid. See `One::rfind_raw`. |
| 54 |
#[inline(always)] |
| 55 |
pub(crate) unsafe fn memrchr_raw( |
| 56 |
n1: u8, |
| 57 |
start: *const u8, |
| 58 |
end: *const u8, |
| 59 |
) -> Option<*const u8> { |
| 60 |
defraw!(One, rfind_raw, start, end, n1) |
| 61 |
} |
| 62 |
|
| 63 |
/// memchr2, but using raw pointers to represent the haystack. |
| 64 |
/// |
| 65 |
/// # Safety |
| 66 |
/// |
| 67 |
/// Pointers must be valid. See `Two::find_raw`. |
| 68 |
#[inline(always)] |
| 69 |
pub(crate) unsafe fn memchr2_raw( |
| 70 |
n1: u8, |
| 71 |
n2: u8, |
| 72 |
start: *const u8, |
| 73 |
end: *const u8, |
| 74 |
) -> Option<*const u8> { |
| 75 |
defraw!(Two, find_raw, start, end, n1, n2) |
| 76 |
} |
| 77 |
|
| 78 |
/// memrchr2, but using raw pointers to represent the haystack. |
| 79 |
/// |
| 80 |
/// # Safety |
| 81 |
/// |
| 82 |
/// Pointers must be valid. See `Two::rfind_raw`. |
| 83 |
#[inline(always)] |
| 84 |
pub(crate) unsafe fn memrchr2_raw( |
| 85 |
n1: u8, |
| 86 |
n2: u8, |
| 87 |
start: *const u8, |
| 88 |
end: *const u8, |
| 89 |
) -> Option<*const u8> { |
| 90 |
defraw!(Two, rfind_raw, start, end, n1, n2) |
| 91 |
} |
| 92 |
|
| 93 |
/// memchr3, but using raw pointers to represent the haystack. |
| 94 |
/// |
| 95 |
/// # Safety |
| 96 |
/// |
| 97 |
/// Pointers must be valid. See `Three::find_raw`. |
| 98 |
#[inline(always)] |
| 99 |
pub(crate) unsafe fn memchr3_raw( |
| 100 |
n1: u8, |
| 101 |
n2: u8, |
| 102 |
n3: u8, |
| 103 |
start: *const u8, |
| 104 |
end: *const u8, |
| 105 |
) -> Option<*const u8> { |
| 106 |
defraw!(Three, find_raw, start, end, n1, n2, n3) |
| 107 |
} |
| 108 |
|
| 109 |
/// memrchr3, but using raw pointers to represent the haystack. |
| 110 |
/// |
| 111 |
/// # Safety |
| 112 |
/// |
| 113 |
/// Pointers must be valid. See `Three::rfind_raw`. |
| 114 |
#[inline(always)] |
| 115 |
pub(crate) unsafe fn memrchr3_raw( |
| 116 |
n1: u8, |
| 117 |
n2: u8, |
| 118 |
n3: u8, |
| 119 |
start: *const u8, |
| 120 |
end: *const u8, |
| 121 |
) -> Option<*const u8> { |
| 122 |
defraw!(Three, rfind_raw, start, end, n1, n2, n3) |
| 123 |
} |
| 124 |
|
| 125 |
/// Count all matching bytes, but using raw pointers to represent the haystack. |
| 126 |
/// |
| 127 |
/// # Safety |
| 128 |
/// |
| 129 |
/// Pointers must be valid. See `One::count_raw`. |
| 130 |
#[inline(always)] |
| 131 |
pub(crate) unsafe fn count_raw( |
| 132 |
n1: u8, |
| 133 |
start: *const u8, |
| 134 |
end: *const u8, |
| 135 |
) -> usize { |
| 136 |
defraw!(One, count_raw, start, end, n1) |
| 137 |
} |