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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! This crate provides one function: `fastchr`, which very quickly finds the first occurrence of a given byte in a slice.
//! `fastchr` is implemented using SIMD intrinsics and runtime CPU feature detection so it will always use the fastest method available
//! on a platform. If SIMD features are not available, `fastchr` falls back to using `memchr`.

#![warn(missing_docs)]
#![feature(stdsimd)]

#[macro_use]
#[cfg(test)]
extern crate quickcheck;

extern crate memchr;

use memchr::memchr;

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

#[cfg(target_arch = "x86")]
use std::arch::x86::*;

use std::mem;
use std::iter::FusedIterator;

/// An iterator for byte positions using `fastchr`.
/// 
/// This struct is created by [`Fastchr::new`].
#[derive(Debug, Clone)]
pub struct Fastchr<'a> {
    needle: u8,
    haystack: &'a [u8],
    position: usize,
}

impl<'a> Fastchr<'a> {
    /// Creates a new iterator that yields all positions of needle in haystack.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use fastchr::Fastchr;
    /// 
    /// let slice = [100, 50, 70, 50, 100, 100, 50];
    /// let mut iter = Fastchr::new(50, &slice[..]);
    /// 
    /// assert_eq!(iter.next().unwrap(), 1);
    /// assert_eq!(iter.next().unwrap(), 3);
    /// assert_eq!(iter.next().unwrap(), 6);
    /// assert!(iter.next().is_none());
    /// ```
    pub fn new(needle: u8, haystack: &[u8]) -> Fastchr {
        Fastchr{
            needle,
            haystack,
            position: 0,
        }
    }
}

impl<'a> Iterator for Fastchr<'a> {
    type Item = usize;
    fn next(&mut self) -> Option<Self::Item> {
        fastchr(self.needle, self.haystack).map(
            move |new_index| {
                self.haystack = self.haystack.split_at(new_index + 1).1;
                let found_pos = self.position + new_index;
                self.position = found_pos + 1;
                found_pos
            }
        )
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, Some(self.haystack.len()))
    }
}

impl<'a> FusedIterator for Fastchr<'a> {}

/// An iterator of subslices separated by a specified byte. The separator byte is not included in the subslices.
/// 
/// This struct is created by [`FastchrSplit::new`]
#[derive(Debug, Clone)]
pub struct FastchrSplit<'a> {
    needle: u8,
    haystack: &'a [u8],
    finished: bool,
}

impl<'a> FastchrSplit<'a> {
    /// Returns an iterator over subslices of `haystack` separated by `needle`. `needle` is not contained in the subslices.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use fastchr::FastchrSplit;
    /// 
    /// let slice = [10, 40, 33, 20];
    /// let mut iter = FastchrSplit::new(40, &slice[..]);
    /// 
    /// assert_eq!(iter.next().unwrap(), &[10]);
    /// assert_eq!(iter.next().unwrap(), &[33, 20]);
    /// assert!(iter.next().is_none());
    /// ```
    /// 
    /// ```
    /// use fastchr::FastchrSplit;
    /// 
    /// let slice = [100, 50, 70, 50, 100, 100, 50];
    /// let mut iter = FastchrSplit::new(50, &slice[..]);
    /// 
    /// assert_eq!(iter.next().unwrap(), &[100]);
    /// assert_eq!(iter.next().unwrap(), &[70]);
    /// assert_eq!(iter.next().unwrap(), &[100, 100]);
    /// assert_eq!(iter.next().unwrap(), &[]);
    /// assert!(iter.next().is_none());
    /// ```
    pub fn new(needle: u8, haystack: &[u8]) -> FastchrSplit {
        FastchrSplit{
            needle,
            haystack,
            finished: false,
        }
    }
}

impl<'a> Iterator for FastchrSplit<'a> {
    type Item = &'a [u8];
    fn next(&mut self) -> Option<Self::Item> {   
        if self.finished {
            None
        } else {
            match fastchr(self.needle, self.haystack) {
                None => {
                    self.finished = true;
                    Some(mem::replace(&mut self.haystack, &[]))},
                Some(i) => {
                    let out = Some(&self.haystack[..i]);
                    self.haystack = &self.haystack[i + 1..];
                    out
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.finished {
            (0, Some(0))
        } else {
            (1, Some(self.haystack.len() + 1))
        }
    }
}

impl<'a> FusedIterator for FastchrSplit<'a> {}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
#[inline]
pub fn fastchr(needle: u8, haystack: &[u8]) -> Option<usize> {
    memchr(needle, haystack)
}

/// Returns the index corresponding to the first occurrence of `needle` in `haystack`, or `None` if one is not found.
/// 
/// `fastchr` is implemented using SIMD intrinsics and run-time CPU feature detection, so it is often faster than `memchr`
/// due to being able to take advantage of available SIMD features at run-time.
/// 
/// # Example
/// 
/// ```
/// use fastchr::fastchr;
/// 
/// let haystack = b"the quick brown fox jumps over the lazy dog";
/// assert_eq!(fastchr(b'k', haystack), Some(8));
/// assert_eq!(fastchr(b'o', haystack), Some(12));
/// assert_eq!(fastchr(b'!', haystack), None);
/// ```
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[inline]
pub fn fastchr(needle: u8, haystack: &[u8]) -> Option<usize> {
    if is_x86_feature_detected!("avx2") {
        unsafe { avx_fastchr(needle, haystack) }
    } else if is_x86_feature_detected!("sse2") {
        unsafe { sse_fastchr(needle, haystack) }
    } else {
        memchr(needle, haystack)
    }
}

#[inline]
#[target_feature(enable = "sse2")]
unsafe fn sse_fastchr(needle: u8, haystack: &[u8]) -> Option<usize> {
    const LANE_WIDTH: usize = 16;

    let needle = needle as i8;
    let haystack: &[i8] = mem::transmute(haystack);
    let ptr = haystack.as_ptr() as usize;
    let mut index: usize = 0;

    // Read a 16 byte lane at a time and check if any bytes are equal to the needle.
    let wide_needle = _mm_set1_epi8(needle);

    while index + LANE_WIDTH < haystack.len() {
        let hay = _mm_loadu_si128((ptr + index) as *const __m128i);
        let hay_cmp = _mm_cmpeq_epi8(hay, wide_needle);
        let hay_cmp_mask = _mm_movemask_epi8(hay_cmp) as usize;
        if hay_cmp_mask != 0 {
            return Some(index + hay_cmp_mask.trailing_zeros() as usize)
        }
        index += LANE_WIDTH;
    }
    
    // If there are bytes left over that don't fill a SIMD register, search them individually.
    while index < haystack.len() {
        if needle == *haystack.get_unchecked(index) {
            return Some(index)
        } else {
            index += 1;
        }
    }

    None
}

#[inline]
#[target_feature(enable = "avx2")]
unsafe fn avx_fastchr(needle: u8, haystack: &[u8]) -> Option<usize> {
    const LANE_WIDTH: usize = 32;

    let needle = needle as i8;
    let haystack: &[i8] = mem::transmute(haystack);
    let mut index = 0;
    let ptr = haystack.as_ptr() as usize;

    // align ptr to 32 bytes
    while (ptr + index) % 32 != 0 && index < haystack.len() {
        if needle == *haystack.get_unchecked(index) {
            return Some(index)
        } else {
            index += 1;
        }
    }

    // Read a 32 byte lane at a time and check if any are equal to the needle.
    let wide_needle = _mm256_set1_epi8(needle);

    while index + LANE_WIDTH < haystack.len() {
        let hay = _mm256_load_si256((ptr + index) as *const __m256i);
        let hay_cmp = _mm256_cmpeq_epi8(hay, wide_needle);
        let hay_cmp_mask = _mm256_movemask_epi8(hay_cmp) as usize;
        if hay_cmp_mask != 0 {
            return Some(index + hay_cmp_mask.trailing_zeros() as usize)
        }
        index += LANE_WIDTH;
    }
    
    // If there are bytes left over that don't fill a SIMD register, search them individually.
    while index < haystack.len() {
        if needle == *haystack.get_unchecked(index) {
            return Some(index)
        } else {
            index += 1;
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::iter;

    const LONG_PREFIX_LEN: usize = 500000;
    const SHORT_PREFIX_LEN: usize = 16 * 2 - 1;
    const ODD_PREFIX_LEN: usize = 50003;
    const BURIED_SAMPLE_LEN: usize = 241;
    const NEEDLE: u8 = 70;

    fn generate_long_sample() -> Vec<u8> {
            iter::repeat(14u8).take(LONG_PREFIX_LEN)
            .chain(iter::once(NEEDLE))
            .chain(iter::repeat(200u8).take(ODD_PREFIX_LEN))
            .collect()
    }

    fn generate_short_sample() -> Vec<u8> {
            iter::repeat(14u8).take(SHORT_PREFIX_LEN)
            .chain(iter::once(NEEDLE))
            .collect()
    }

    fn generate_odd_sample() -> Vec<u8> {
            iter::repeat(14u8).take(ODD_PREFIX_LEN)
            .chain(iter::once(NEEDLE))
            .collect()
    }
    fn generate_long_negative_sample() -> Vec<u8> {
        iter::repeat(231).take(ODD_PREFIX_LEN)
        .collect()
    }

    fn generate_buried_sample() -> Vec<u8> {
        iter::repeat(231u8).take(BURIED_SAMPLE_LEN)
        .chain(iter::once(NEEDLE))
        .chain(iter::repeat(231u8).take(3))
        .chain(iter::once(NEEDLE))
        .chain(iter::repeat(231u8).take(ODD_PREFIX_LEN))
        .collect()
    }

    #[test]
    fn buried_sample_test() {
        let data = generate_buried_sample();
        assert_eq!(Some(BURIED_SAMPLE_LEN), fastchr(NEEDLE, &data));
    }

    #[test]
    fn negative_sample_test() {
        let data = generate_long_negative_sample();
        assert_eq!(None, fastchr(NEEDLE, &data));
    }
    
    #[test]
    fn qbf_test() {
        let haystack = b"the quick brown fox";
        assert_eq!(Some(8), fastchr(b'k', haystack));
    }

    #[test]
    fn memchr_odd_compat_test() {
        let data = generate_odd_sample();
        assert_eq!(memchr(NEEDLE, &data), fastchr(NEEDLE, &data));
    }

    #[test]
    fn memchr_short_compat_test() {
        let data = generate_short_sample();
        assert_eq!(memchr(NEEDLE, &data), fastchr(NEEDLE, &data));
    }

    #[test]
    fn long_find_test() {
        let data = generate_long_sample();
        assert_eq!(Some(LONG_PREFIX_LEN), fastchr(NEEDLE, &data));
    }

    #[test]
    fn short_find_test() {
        let data = generate_short_sample();
        assert_eq!(Some(SHORT_PREFIX_LEN), fastchr(NEEDLE, &data));
    }

    #[test]
    fn odd_find_test() {
        let data = generate_odd_sample();
        assert_eq!(Some(ODD_PREFIX_LEN), fastchr(NEEDLE, &data));
    }

    #[test]
    fn empty_find_test() {
        let data = Vec::new();
        assert_eq!(None, fastchr(NEEDLE, &data));
    }

    quickcheck!{
        fn qc_memchr_equivalence(needle: u8, haystack: Vec<u8>) -> bool {
            fastchr(needle, &haystack) == memchr(needle, &haystack)
        }
    }
}