| 1 |
#[cfg(test)] |
| 2 |
const MAX_COMPARE_SIZE: usize = 256; |
| 3 |
|
| 4 |
pub fn compare256_slice(src0: &[u8], src1: &[u8]) -> usize { |
| 5 |
let src0 = first_chunk::<_, 256>(src0).unwrap(); |
| 6 |
let src1 = first_chunk::<_, 256>(src1).unwrap(); |
| 7 |
|
| 8 |
compare256(src0, src1) |
| 9 |
} |
| 10 |
|
| 11 |
fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 12 |
#[cfg(target_arch = "x86_64")] |
| 13 |
if crate::cpu_features::is_enabled_avx2() { |
| 14 |
return unsafe { avx2::compare256(src0, src1) }; |
| 15 |
} |
| 16 |
|
| 17 |
#[cfg(target_arch = "aarch64")] |
| 18 |
if crate::cpu_features::is_enabled_neon() { |
| 19 |
return unsafe { neon::compare256(src0, src1) }; |
| 20 |
} |
| 21 |
|
| 22 |
#[cfg(target_arch = "wasm32")] |
| 23 |
if crate::cpu_features::is_enabled_simd128() { |
| 24 |
return wasm32::compare256(src0, src1); |
| 25 |
} |
| 26 |
|
| 27 |
rust::compare256(src0, src1) |
| 28 |
} |
| 29 |
|
| 30 |
pub fn compare256_rle_slice(byte: u8, src: &[u8]) -> usize { |
| 31 |
rust::compare256_rle(byte, src) |
| 32 |
} |
| 33 |
|
| 34 |
#[inline] |
| 35 |
pub const fn first_chunk<T, const N: usize>(slice: &[T]) -> Option<&[T; N]> { |
| 36 |
if slice.len() < N { |
| 37 |
None |
| 38 |
} else { |
| 39 |
// SAFETY: We explicitly check for the correct number of elements, |
| 40 |
// and do not let the reference outlive the slice. |
| 41 |
Some(unsafe { &*(slice.as_ptr() as *const [T; N]) }) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
mod rust { |
| 46 |
|
| 47 |
pub fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 48 |
// only unrolls 4 iterations; zlib-ng unrolls 8 |
| 49 |
src0.iter().zip(src1).take_while(|(x, y)| x == y).count() |
| 50 |
} |
| 51 |
|
| 52 |
// run-length encoding |
| 53 |
pub fn compare256_rle(byte: u8, src: &[u8]) -> usize { |
| 54 |
assert!(src.len() >= 256, "too short {}", src.len()); |
| 55 |
|
| 56 |
let mut sv = byte as u64; |
| 57 |
sv |= sv << 8; |
| 58 |
sv |= sv << 16; |
| 59 |
sv |= sv << 32; |
| 60 |
|
| 61 |
let mut len = 0; |
| 62 |
|
| 63 |
// this optimizes well because we statically limit the slice to 256 bytes. |
| 64 |
// the loop gets unrolled 4 times automatically. |
| 65 |
for chunk in src[..256].chunks_exact(8) { |
| 66 |
let mv = u64::from_le_bytes(chunk.try_into().unwrap()); |
| 67 |
|
| 68 |
let diff = sv ^ mv; |
| 69 |
|
| 70 |
if diff > 0 { |
| 71 |
let match_byte = diff.trailing_zeros() / 8; |
| 72 |
return len + match_byte as usize; |
| 73 |
} |
| 74 |
|
| 75 |
len += 8 |
| 76 |
} |
| 77 |
|
| 78 |
256 |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn test_compare256() { |
| 83 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 84 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 85 |
|
| 86 |
for i in 0..str1.len() { |
| 87 |
str2[i] = 0; |
| 88 |
|
| 89 |
let match_len = compare256(&str1, &str2); |
| 90 |
assert_eq!(match_len, i); |
| 91 |
|
| 92 |
str2[i] = b'a'; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn test_compare256_rle() { |
| 98 |
let mut string = [b'a'; super::MAX_COMPARE_SIZE]; |
| 99 |
|
| 100 |
for i in 0..string.len() { |
| 101 |
string[i] = 0; |
| 102 |
|
| 103 |
let match_len = compare256_rle(b'a', &string); |
| 104 |
assert_eq!(match_len, i); |
| 105 |
|
| 106 |
string[i] = b'a'; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
#[cfg(target_arch = "aarch64")] |
| 112 |
mod neon { |
| 113 |
use core::arch::aarch64::{ |
| 114 |
uint8x16_t, veorq_u8, vgetq_lane_u64, vld1q_u8, vreinterpretq_u64_u8, |
| 115 |
}; |
| 116 |
|
| 117 |
/// # Safety |
| 118 |
/// |
| 119 |
/// Behavior is undefined if the `neon` target feature is not enabled |
| 120 |
#[target_feature(enable = "neon")] |
| 121 |
pub unsafe fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 122 |
let src0: &[[u8; 16]; 16] = unsafe { core::mem::transmute(src0) }; |
| 123 |
let src1: &[[u8; 16]; 16] = unsafe { core::mem::transmute(src1) }; |
| 124 |
|
| 125 |
let mut len = 0; |
| 126 |
|
| 127 |
for (a, b) in src0.iter().zip(src1) { |
| 128 |
unsafe { |
| 129 |
let a: uint8x16_t = vld1q_u8(a.as_ptr()); |
| 130 |
let b: uint8x16_t = vld1q_u8(b.as_ptr()); |
| 131 |
|
| 132 |
let cmp = veorq_u8(a, b); |
| 133 |
|
| 134 |
let lane = vgetq_lane_u64(vreinterpretq_u64_u8(cmp), 0); |
| 135 |
if lane != 0 { |
| 136 |
let match_byte = lane.trailing_zeros() / 8; |
| 137 |
return len + match_byte as usize; |
| 138 |
} |
| 139 |
|
| 140 |
len += 8; |
| 141 |
|
| 142 |
let lane = vgetq_lane_u64(vreinterpretq_u64_u8(cmp), 1); |
| 143 |
if lane != 0 { |
| 144 |
let match_byte = lane.trailing_zeros() / 8; |
| 145 |
return len + match_byte as usize; |
| 146 |
} |
| 147 |
|
| 148 |
len += 8; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
256 |
| 153 |
} |
| 154 |
|
| 155 |
#[test] |
| 156 |
fn test_compare256() { |
| 157 |
if crate::cpu_features::is_enabled_neon() { |
| 158 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 159 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 160 |
|
| 161 |
for i in 0..str1.len() { |
| 162 |
str2[i] = 0; |
| 163 |
|
| 164 |
let match_len = unsafe { compare256(&str1, &str2) }; |
| 165 |
assert_eq!(match_len, i); |
| 166 |
|
| 167 |
str2[i] = b'a'; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
#[cfg(target_arch = "x86_64")] |
| 174 |
mod avx2 { |
| 175 |
use core::arch::x86_64::{ |
| 176 |
__m256i, _mm256_cmpeq_epi8, _mm256_loadu_si256, _mm256_movemask_epi8, |
| 177 |
}; |
| 178 |
|
| 179 |
/// # Safety |
| 180 |
/// |
| 181 |
/// Behavior is undefined if the `avx` target feature is not enabled |
| 182 |
#[target_feature(enable = "avx2")] |
| 183 |
pub unsafe fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 184 |
let src0: &[[u8; 32]; 8] = unsafe { core::mem::transmute(src0) }; |
| 185 |
let src1: &[[u8; 32]; 8] = unsafe { core::mem::transmute(src1) }; |
| 186 |
|
| 187 |
let mut len = 0; |
| 188 |
|
| 189 |
unsafe { |
| 190 |
for (chunk0, chunk1) in src0.iter().zip(src1) { |
| 191 |
let ymm_src0 = _mm256_loadu_si256(chunk0.as_ptr() as *const __m256i); |
| 192 |
let ymm_src1 = _mm256_loadu_si256(chunk1.as_ptr() as *const __m256i); |
| 193 |
|
| 194 |
let ymm_cmp = _mm256_cmpeq_epi8(ymm_src0, ymm_src1); /* non-identical bytes = 00, identical bytes = FF */ |
| 195 |
let mask = _mm256_movemask_epi8(ymm_cmp) as u32; |
| 196 |
|
| 197 |
if mask != 0xFFFFFFFF { |
| 198 |
let match_byte = (!mask).trailing_zeros(); /* Invert bits so identical = 0 */ |
| 199 |
return len + match_byte as usize; |
| 200 |
} |
| 201 |
|
| 202 |
len += 32; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
256 |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn test_compare256() { |
| 211 |
if crate::cpu_features::is_enabled_avx2() { |
| 212 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 213 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 214 |
|
| 215 |
for i in 0..str1.len() { |
| 216 |
str2[i] = 0; |
| 217 |
|
| 218 |
let match_len = unsafe { compare256(&str1, &str2) }; |
| 219 |
assert_eq!(match_len, i); |
| 220 |
|
| 221 |
str2[i] = b'a'; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
#[cfg(target_arch = "wasm32")] |
| 228 |
mod wasm32 { |
| 229 |
use core::arch::wasm32::{u8x16_bitmask, u8x16_eq, v128, v128_load}; |
| 230 |
|
| 231 |
#[target_feature(enable = "simd128")] |
| 232 |
pub fn compare256(src0: &[u8; 256], src1: &[u8; 256]) -> usize { |
| 233 |
let src0: &[[u8; 16]; 16] = unsafe { core::mem::transmute(src0) }; |
| 234 |
let src1: &[[u8; 16]; 16] = unsafe { core::mem::transmute(src1) }; |
| 235 |
|
| 236 |
let mut len = 0; |
| 237 |
|
| 238 |
for (chunk0, chunk1) in src0.iter().zip(src1) { |
| 239 |
// SAFETY: these are valid pointers to slice data. |
| 240 |
let v128_src0 = unsafe { v128_load(chunk0.as_ptr() as *const v128) }; |
| 241 |
let v128_src1 = unsafe { v128_load(chunk1.as_ptr() as *const v128) }; |
| 242 |
|
| 243 |
let v128_cmp = u8x16_eq(v128_src0, v128_src1); |
| 244 |
let mask = u8x16_bitmask(v128_cmp); |
| 245 |
|
| 246 |
if mask != 0xFFFF { |
| 247 |
let match_byte = (!mask).trailing_zeros(); /* Invert bits so identical = 0 */ |
| 248 |
return len + match_byte as usize; |
| 249 |
} |
| 250 |
|
| 251 |
len += 16; |
| 252 |
} |
| 253 |
|
| 254 |
256 |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn test_compare256() { |
| 259 |
if crate::cpu_features::is_enabled_simd128() { |
| 260 |
let str1 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 261 |
let mut str2 = [b'a'; super::MAX_COMPARE_SIZE]; |
| 262 |
|
| 263 |
for i in 0..str1.len() { |
| 264 |
str2[i] = 0; |
| 265 |
|
| 266 |
let match_len = unsafe { compare256(&str1, &str2) }; |
| 267 |
assert_eq!(match_len, i); |
| 268 |
|
| 269 |
str2[i] = b'a'; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |