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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
use core::arch::x86_64::*;
use core::cmp;
use core::mem::size_of;

const VECTOR_SIZE: usize = size_of::<__m128i>();
const VECTOR_ALIGN: usize = VECTOR_SIZE - 1;

// The number of bytes to loop at in one iteration of memchr/memrchr.
const LOOP_SIZE: usize = 4 * VECTOR_SIZE;

// The number of bytes to loop at in one iteration of memchr2/memrchr2 and
// memchr3/memrchr3. There was no observable difference between 64 and 32 bytes
// in benchmarks. memchr3 in particular only gets a very slight speed up from
// the loop unrolling.
const LOOP_SIZE2: usize = 2 * VECTOR_SIZE;

#[target_feature(enable = "sse2")]
pub unsafe fn memchr(n1: u8, haystack: &[u8]) -> Option<usize> {
    // What follows is a fast SSE2-only algorithm to detect the position of
    // `n1` in `haystack` if it exists. From what I know, this is the "classic"
    // algorithm. I believe it can be found in places like glibc and Go's
    // standard library. It appears to be well known and is elaborated on in
    // more detail here: https://gms.tf/stdfind-and-memchr-optimizations.html
    //
    // While this routine is very long, the basic idea is actually very simple
    // and can be expressed straight-forwardly in pseudo code:
    //
    //     needle = (n1 << 15) | (n1 << 14) | ... | (n1 << 1) | n1
    //     // Note: shift amount in bytes
    //
    //     while i <= haystack.len() - 16:
    //       // A 16 byte vector. Each byte in chunk corresponds to a byte in
    //       // the haystack.
    //       chunk = haystack[i:i+16]
    //       // Compare bytes in needle with bytes in chunk. The result is a 16
    //       // byte chunk where each byte is 0xFF if the corresponding bytes
    //       // in needle and chunk were equal, or 0x00 otherwise.
    //       eqs = cmpeq(needle, chunk)
    //       // Return a 32 bit integer where the most significant 16 bits
    //       // are always 0 and the lower 16 bits correspond to whether the
    //       // most significant bit in the correspond byte in `eqs` is set.
    //       // In other words, `mask as u16` has bit i set if and only if
    //       // needle[i] == chunk[i].
    //       mask = movemask(eqs)
    //
    //       // Mask is 0 if there is no match, and non-zero otherwise.
    //       if mask != 0:
    //         // trailing_zeros tells us the position of the least significant
    //         // bit that is set.
    //         return i + trailing_zeros(mask)
    //
    //     // haystack length may not be a multiple of 16, so search the rest.
    //     while i < haystack.len():
    //       if haystack[i] == n1:
    //         return i
    //
    //     // No match found.
    //     return NULL
    //
    // In fact, we could loosely translate the above code to Rust line-for-line
    // and it would be a pretty fast algorithm. But, we pull out all the stops
    // to go as fast as possible:
    //
    // 1. We use aligned loads. That is, we do some finagling to make sure our
    //    primary loop not only proceeds in increments of 16 bytes, but that
    //    the address of haystack's pointer that we dereference is aligned to
    //    16 bytes. 16 is a magic number here because it is the size of SSE2
    //    128-bit vector. (For the AVX2 algorithm, 32 is the magic number.)
    //    Therefore, to get aligned loads, our pointer's address must be evenly
    //    divisible by 16.
    // 2. Our primary loop proceeds 64 bytes at a time instead of 16. It's
    //    kind of like loop unrolling, but we combine the equality comparisons
    //    using a vector OR such that we only need to extract a single mask to
    //    determine whether a match exists or not. If so, then we do some
    //    book-keeping to determine the precise location but otherwise mush on.
    // 3. We use our "chunk" comparison routine in as many places as possible,
    //    even if it means using unaligned loads. In particular, if haystack
    //    starts with an unaligned address, then we do an unaligned load to
    //    search the first 16 bytes. We then start our primary loop at the
    //    smallest subsequent aligned address, which will actually overlap with
    //    previously searched bytes. But we're OK with that. We do a similar
    //    dance at the end of our primary loop. Finally, to avoid a
    //    byte-at-a-time loop at the end, we do a final 16 byte unaligned load
    //    that may overlap with a previous load. This is OK because it converts
    //    a loop into a small number of very fast vector instructions.
    //
    // The primary downside of this algorithm is that it's effectively
    // completely unsafe. Therefore, we have to be super careful to avoid
    // undefined behavior:
    //
    // 1. We use raw pointers everywhere. Not only does dereferencing a pointer
    //    require the pointer to be valid, but we actually can't even store the
    //    address of an invalid pointer (unless it's 1 past the end of
    //    haystack) without sacrificing performance.
    // 2. _mm_loadu_si128 is used when you don't care about alignment, and
    //    _mm_load_si128 is used when you do care. You cannot use the latter
    //    on unaligned pointers.
    // 3. We make liberal use of debug_assert! to check assumptions.
    // 4. We make a concerted effort to stick with pointers instead of indices.
    //    Indices are nicer because there's less to worry about with them (see
    //    above about pointer offsets), but I could not get the compiler to
    //    produce as good of code as what the below produces. In any case,
    //    pointers are what we really care about here, and alignment is
    //    expressed a bit more naturally with them.
    //
    // In general, most of the algorithms in this crate have a similar
    // structure to what you see below, so this comment applies fairly well to
    // all of them.

    let vn1 = _mm_set1_epi8(n1 as i8);
    let len = haystack.len();
    let loop_size = cmp::min(LOOP_SIZE, len);
    let start_ptr = haystack.as_ptr();
    let end_ptr = haystack[haystack.len()..].as_ptr();
    let mut ptr = start_ptr;

    if haystack.len() < VECTOR_SIZE {
        while ptr < end_ptr {
            if *ptr == n1 {
                return Some(sub(ptr, start_ptr));
            }
            ptr = ptr.offset(1);
        }
        return None;
    }

    if let Some(i) = forward_search1(start_ptr, end_ptr, ptr, vn1) {
        return Some(i);
    }

    ptr = ptr.add(VECTOR_SIZE - (start_ptr as usize & VECTOR_ALIGN));
    debug_assert!(ptr > start_ptr && end_ptr.sub(VECTOR_SIZE) >= start_ptr);
    while loop_size == LOOP_SIZE && ptr <= end_ptr.sub(loop_size) {
        debug_assert_eq!(0, (ptr as usize) % VECTOR_SIZE);

        let a = _mm_load_si128(ptr as *const __m128i);
        let b = _mm_load_si128(ptr.add(VECTOR_SIZE) as *const __m128i);
        let c = _mm_load_si128(ptr.add(2 * VECTOR_SIZE) as *const __m128i);
        let d = _mm_load_si128(ptr.add(3 * VECTOR_SIZE) as *const __m128i);
        let eqa = _mm_cmpeq_epi8(vn1, a);
        let eqb = _mm_cmpeq_epi8(vn1, b);
        let eqc = _mm_cmpeq_epi8(vn1, c);
        let eqd = _mm_cmpeq_epi8(vn1, d);
        let or1 = _mm_or_si128(eqa, eqb);
        let or2 = _mm_or_si128(eqc, eqd);
        let or3 = _mm_or_si128(or1, or2);
        if _mm_movemask_epi8(or3) != 0 {
            let mut at = sub(ptr, start_ptr);
            let mask = _mm_movemask_epi8(eqa);
            if mask != 0 {
                return Some(at + forward_pos(mask));
            }

            at += VECTOR_SIZE;
            let mask = _mm_movemask_epi8(eqb);
            if mask != 0 {
                return Some(at + forward_pos(mask));
            }

            at += VECTOR_SIZE;
            let mask = _mm_movemask_epi8(eqc);
            if mask != 0 {
                return Some(at + forward_pos(mask));
            }

            at += VECTOR_SIZE;
            let mask = _mm_movemask_epi8(eqd);
            debug_assert!(mask != 0);
            return Some(at + forward_pos(mask));
        }
        ptr = ptr.add(loop_size);
    }
    while ptr <= end_ptr.sub(VECTOR_SIZE) {
        debug_assert!(sub(end_ptr, ptr) >= VECTOR_SIZE);

        if let Some(i) = forward_search1(start_ptr, end_ptr, ptr, vn1) {
            return Some(i);
        }
        ptr = ptr.add(VECTOR_SIZE);
    }
    if ptr < end_ptr {
        debug_assert!(sub(end_ptr, ptr) < VECTOR_SIZE);
        ptr = ptr.sub(VECTOR_SIZE - sub(end_ptr, ptr));
        debug_assert_eq!(sub(end_ptr, ptr), VECTOR_SIZE);

        return forward_search1(start_ptr, end_ptr, ptr, vn1);
    }
    None
}

#[target_feature(enable = "sse2")]
pub unsafe fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
    let vn1 = _mm_set1_epi8(n1 as i8);
    let vn2 = _mm_set1_epi8(n2 as i8);
    let len = haystack.len();
    let loop_size = cmp::min(LOOP_SIZE2, len);
    let start_ptr = haystack.as_ptr();
    let end_ptr = haystack[haystack.len()..].as_ptr();
    let mut ptr = start_ptr;

    if haystack.len() < VECTOR_SIZE {
        while ptr < end_ptr {
            if *ptr == n1 || *ptr == n2 {
                return Some(sub(ptr, start_ptr));
            }
            ptr = ptr.offset(1);
        }
        return None;
    }

    if let Some(i) = forward_search2(start_ptr, end_ptr, ptr, vn1, vn2) {
        return Some(i);
    }

    ptr = ptr.add(VECTOR_SIZE - (start_ptr as usize & VECTOR_ALIGN));
    debug_assert!(ptr > start_ptr && end_ptr.sub(VECTOR_SIZE) >= start_ptr);
    while loop_size == LOOP_SIZE2 && ptr <= end_ptr.sub(loop_size) {
        debug_assert_eq!(0, (ptr as usize) % VECTOR_SIZE);

        let a = _mm_load_si128(ptr as *const __m128i);
        let b = _mm_load_si128(ptr.add(VECTOR_SIZE) as *const __m128i);
        let eqa1 = _mm_cmpeq_epi8(vn1, a);
        let eqb1 = _mm_cmpeq_epi8(vn1, b);
        let eqa2 = _mm_cmpeq_epi8(vn2, a);
        let eqb2 = _mm_cmpeq_epi8(vn2, b);
        let or1 = _mm_or_si128(eqa1, eqb1);
        let or2 = _mm_or_si128(eqa2, eqb2);
        let or3 = _mm_or_si128(or1, or2);
        if _mm_movemask_epi8(or3) != 0 {
            let mut at = sub(ptr, start_ptr);
            let mask1 = _mm_movemask_epi8(eqa1);
            let mask2 = _mm_movemask_epi8(eqa2);
            if mask1 != 0 || mask2 != 0 {
                return Some(at + forward_pos2(mask1, mask2));
            }

            at += VECTOR_SIZE;
            let mask1 = _mm_movemask_epi8(eqb1);
            let mask2 = _mm_movemask_epi8(eqb2);
            return Some(at + forward_pos2(mask1, mask2));
        }
        ptr = ptr.add(loop_size);
    }
    while ptr <= end_ptr.sub(VECTOR_SIZE) {
        if let Some(i) = forward_search2(start_ptr, end_ptr, ptr, vn1, vn2) {
            return Some(i);
        }
        ptr = ptr.add(VECTOR_SIZE);
    }
    if ptr < end_ptr {
        debug_assert!(sub(end_ptr, ptr) < VECTOR_SIZE);
        ptr = ptr.sub(VECTOR_SIZE - sub(end_ptr, ptr));
        debug_assert_eq!(sub(end_ptr, ptr), VECTOR_SIZE);

        return forward_search2(start_ptr, end_ptr, ptr, vn1, vn2);
    }
    None
}

#[target_feature(enable = "sse2")]
pub unsafe fn memchr3(
    n1: u8,
    n2: u8,
    n3: u8,
    haystack: &[u8],
) -> Option<usize> {
    let vn1 = _mm_set1_epi8(n1 as i8);
    let vn2 = _mm_set1_epi8(n2 as i8);
    let vn3 = _mm_set1_epi8(n3 as i8);
    let len = haystack.len();
    let loop_size = cmp::min(LOOP_SIZE2, len);
    let start_ptr = haystack.as_ptr();
    let end_ptr = haystack[haystack.len()..].as_ptr();
    let mut ptr = start_ptr;

    if haystack.len() < VECTOR_SIZE {
        while ptr < end_ptr {
            if *ptr == n1 || *ptr == n2 || *ptr == n3 {
                return Some(sub(ptr, start_ptr));
            }
            ptr = ptr.offset(1);
        }
        return None;
    }

    if let Some(i) = forward_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3) {
        return Some(i);
    }

    ptr = ptr.add(VECTOR_SIZE - (start_ptr as usize & VECTOR_ALIGN));
    debug_assert!(ptr > start_ptr && end_ptr.sub(VECTOR_SIZE) >= start_ptr);
    while loop_size == LOOP_SIZE2 && ptr <= end_ptr.sub(loop_size) {
        debug_assert_eq!(0, (ptr as usize) % VECTOR_SIZE);

        let a = _mm_load_si128(ptr as *const __m128i);
        let b = _mm_load_si128(ptr.add(VECTOR_SIZE) as *const __m128i);
        let eqa1 = _mm_cmpeq_epi8(vn1, a);
        let eqb1 = _mm_cmpeq_epi8(vn1, b);
        let eqa2 = _mm_cmpeq_epi8(vn2, a);
        let eqb2 = _mm_cmpeq_epi8(vn2, b);
        let eqa3 = _mm_cmpeq_epi8(vn3, a);
        let eqb3 = _mm_cmpeq_epi8(vn3, b);
        let or1 = _mm_or_si128(eqa1, eqb1);
        let or2 = _mm_or_si128(eqa2, eqb2);
        let or3 = _mm_or_si128(eqa3, eqb3);
        let or4 = _mm_or_si128(or1, or2);
        let or5 = _mm_or_si128(or3, or4);
        if _mm_movemask_epi8(or5) != 0 {
            let mut at = sub(ptr, start_ptr);
            let mask1 = _mm_movemask_epi8(eqa1);
            let mask2 = _mm_movemask_epi8(eqa2);
            let mask3 = _mm_movemask_epi8(eqa3);
            if mask1 != 0 || mask2 != 0 || mask3 != 0 {
                return Some(at + forward_pos3(mask1, mask2, mask3));
            }

            at += VECTOR_SIZE;
            let mask1 = _mm_movemask_epi8(eqb1);
            let mask2 = _mm_movemask_epi8(eqb2);
            let mask3 = _mm_movemask_epi8(eqb3);
            return Some(at + forward_pos3(mask1, mask2, mask3));
        }
        ptr = ptr.add(loop_size);
    }
    while ptr <= end_ptr.sub(VECTOR_SIZE) {
        if let Some(i) =
            forward_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3)
        {
            return Some(i);
        }
        ptr = ptr.add(VECTOR_SIZE);
    }
    if ptr < end_ptr {
        debug_assert!(sub(end_ptr, ptr) < VECTOR_SIZE);
        ptr = ptr.sub(VECTOR_SIZE - sub(end_ptr, ptr));
        debug_assert_eq!(sub(end_ptr, ptr), VECTOR_SIZE);

        return forward_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3);
    }
    None
}

#[target_feature(enable = "sse2")]
pub unsafe fn memrchr(n1: u8, haystack: &[u8]) -> Option<usize> {
    let vn1 = _mm_set1_epi8(n1 as i8);
    let len = haystack.len();
    let loop_size = cmp::min(LOOP_SIZE, len);
    let start_ptr = haystack.as_ptr();
    let end_ptr = haystack[haystack.len()..].as_ptr();
    let mut ptr = end_ptr;

    if haystack.len() < VECTOR_SIZE {
        while ptr > start_ptr {
            ptr = ptr.offset(-1);
            if *ptr == n1 {
                return Some(sub(ptr, start_ptr));
            }
        }
        return None;
    }

    ptr = ptr.sub(VECTOR_SIZE);
    if let Some(i) = reverse_search1(start_ptr, end_ptr, ptr, vn1) {
        return Some(i);
    }

    ptr = (end_ptr as usize & !VECTOR_ALIGN) as *const u8;
    debug_assert!(start_ptr <= ptr && ptr <= end_ptr);
    while loop_size == LOOP_SIZE && ptr >= start_ptr.add(loop_size) {
        debug_assert_eq!(0, (ptr as usize) % VECTOR_SIZE);

        ptr = ptr.sub(loop_size);
        let a = _mm_load_si128(ptr as *const __m128i);
        let b = _mm_load_si128(ptr.add(VECTOR_SIZE) as *const __m128i);
        let c = _mm_load_si128(ptr.add(2 * VECTOR_SIZE) as *const __m128i);
        let d = _mm_load_si128(ptr.add(3 * VECTOR_SIZE) as *const __m128i);
        let eqa = _mm_cmpeq_epi8(vn1, a);
        let eqb = _mm_cmpeq_epi8(vn1, b);
        let eqc = _mm_cmpeq_epi8(vn1, c);
        let eqd = _mm_cmpeq_epi8(vn1, d);
        let or1 = _mm_or_si128(eqa, eqb);
        let or2 = _mm_or_si128(eqc, eqd);
        let or3 = _mm_or_si128(or1, or2);
        if _mm_movemask_epi8(or3) != 0 {
            let mut at = sub(ptr.add(3 * VECTOR_SIZE), start_ptr);
            let mask = _mm_movemask_epi8(eqd);
            if mask != 0 {
                return Some(at + reverse_pos(mask));
            }

            at -= VECTOR_SIZE;
            let mask = _mm_movemask_epi8(eqc);
            if mask != 0 {
                return Some(at + reverse_pos(mask));
            }

            at -= VECTOR_SIZE;
            let mask = _mm_movemask_epi8(eqb);
            if mask != 0 {
                return Some(at + reverse_pos(mask));
            }

            at -= VECTOR_SIZE;
            let mask = _mm_movemask_epi8(eqa);
            debug_assert!(mask != 0);
            return Some(at + reverse_pos(mask));
        }
    }
    while ptr >= start_ptr.add(VECTOR_SIZE) {
        ptr = ptr.sub(VECTOR_SIZE);
        if let Some(i) = reverse_search1(start_ptr, end_ptr, ptr, vn1) {
            return Some(i);
        }
    }
    if ptr > start_ptr {
        debug_assert!(sub(ptr, start_ptr) < VECTOR_SIZE);
        return reverse_search1(start_ptr, end_ptr, start_ptr, vn1);
    }
    None
}

#[target_feature(enable = "sse2")]
pub unsafe fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
    let vn1 = _mm_set1_epi8(n1 as i8);
    let vn2 = _mm_set1_epi8(n2 as i8);
    let len = haystack.len();
    let loop_size = cmp::min(LOOP_SIZE2, len);
    let start_ptr = haystack.as_ptr();
    let end_ptr = haystack[haystack.len()..].as_ptr();
    let mut ptr = end_ptr;

    if haystack.len() < VECTOR_SIZE {
        while ptr > start_ptr {
            ptr = ptr.offset(-1);
            if *ptr == n1 || *ptr == n2 {
                return Some(sub(ptr, start_ptr));
            }
        }
        return None;
    }

    ptr = ptr.sub(VECTOR_SIZE);
    if let Some(i) = reverse_search2(start_ptr, end_ptr, ptr, vn1, vn2) {
        return Some(i);
    }

    ptr = (end_ptr as usize & !VECTOR_ALIGN) as *const u8;
    debug_assert!(start_ptr <= ptr && ptr <= end_ptr);
    while loop_size == LOOP_SIZE2 && ptr >= start_ptr.add(loop_size) {
        debug_assert_eq!(0, (ptr as usize) % VECTOR_SIZE);

        ptr = ptr.sub(loop_size);
        let a = _mm_load_si128(ptr as *const __m128i);
        let b = _mm_load_si128(ptr.add(VECTOR_SIZE) as *const __m128i);
        let eqa1 = _mm_cmpeq_epi8(vn1, a);
        let eqb1 = _mm_cmpeq_epi8(vn1, b);
        let eqa2 = _mm_cmpeq_epi8(vn2, a);
        let eqb2 = _mm_cmpeq_epi8(vn2, b);
        let or1 = _mm_or_si128(eqa1, eqb1);
        let or2 = _mm_or_si128(eqa2, eqb2);
        let or3 = _mm_or_si128(or1, or2);
        if _mm_movemask_epi8(or3) != 0 {
            let mut at = sub(ptr.add(VECTOR_SIZE), start_ptr);
            let mask1 = _mm_movemask_epi8(eqb1);
            let mask2 = _mm_movemask_epi8(eqb2);
            if mask1 != 0 || mask2 != 0 {
                return Some(at + reverse_pos2(mask1, mask2));
            }

            at -= VECTOR_SIZE;
            let mask1 = _mm_movemask_epi8(eqa1);
            let mask2 = _mm_movemask_epi8(eqa2);
            return Some(at + reverse_pos2(mask1, mask2));
        }
    }
    while ptr >= start_ptr.add(VECTOR_SIZE) {
        ptr = ptr.sub(VECTOR_SIZE);
        if let Some(i) = reverse_search2(start_ptr, end_ptr, ptr, vn1, vn2) {
            return Some(i);
        }
    }
    if ptr > start_ptr {
        debug_assert!(sub(ptr, start_ptr) < VECTOR_SIZE);
        return reverse_search2(start_ptr, end_ptr, start_ptr, vn1, vn2);
    }
    None
}

#[target_feature(enable = "sse2")]
pub unsafe fn memrchr3(
    n1: u8,
    n2: u8,
    n3: u8,
    haystack: &[u8],
) -> Option<usize> {
    let vn1 = _mm_set1_epi8(n1 as i8);
    let vn2 = _mm_set1_epi8(n2 as i8);
    let vn3 = _mm_set1_epi8(n3 as i8);
    let len = haystack.len();
    let loop_size = cmp::min(LOOP_SIZE2, len);
    let start_ptr = haystack.as_ptr();
    let end_ptr = haystack[haystack.len()..].as_ptr();
    let mut ptr = end_ptr;

    if haystack.len() < VECTOR_SIZE {
        while ptr > start_ptr {
            ptr = ptr.offset(-1);
            if *ptr == n1 || *ptr == n2 || *ptr == n3 {
                return Some(sub(ptr, start_ptr));
            }
        }
        return None;
    }

    ptr = ptr.sub(VECTOR_SIZE);
    if let Some(i) = reverse_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3) {
        return Some(i);
    }

    ptr = (end_ptr as usize & !VECTOR_ALIGN) as *const u8;
    debug_assert!(start_ptr <= ptr && ptr <= end_ptr);
    while loop_size == LOOP_SIZE2 && ptr >= start_ptr.add(loop_size) {
        debug_assert_eq!(0, (ptr as usize) % VECTOR_SIZE);

        ptr = ptr.sub(loop_size);
        let a = _mm_load_si128(ptr as *const __m128i);
        let b = _mm_load_si128(ptr.add(VECTOR_SIZE) as *const __m128i);
        let eqa1 = _mm_cmpeq_epi8(vn1, a);
        let eqb1 = _mm_cmpeq_epi8(vn1, b);
        let eqa2 = _mm_cmpeq_epi8(vn2, a);
        let eqb2 = _mm_cmpeq_epi8(vn2, b);
        let eqa3 = _mm_cmpeq_epi8(vn3, a);
        let eqb3 = _mm_cmpeq_epi8(vn3, b);
        let or1 = _mm_or_si128(eqa1, eqb1);
        let or2 = _mm_or_si128(eqa2, eqb2);
        let or3 = _mm_or_si128(eqa3, eqb3);
        let or4 = _mm_or_si128(or1, or2);
        let or5 = _mm_or_si128(or3, or4);
        if _mm_movemask_epi8(or5) != 0 {
            let mut at = sub(ptr.add(VECTOR_SIZE), start_ptr);
            let mask1 = _mm_movemask_epi8(eqb1);
            let mask2 = _mm_movemask_epi8(eqb2);
            let mask3 = _mm_movemask_epi8(eqb3);
            if mask1 != 0 || mask2 != 0 || mask3 != 0 {
                return Some(at + reverse_pos3(mask1, mask2, mask3));
            }

            at -= VECTOR_SIZE;
            let mask1 = _mm_movemask_epi8(eqa1);
            let mask2 = _mm_movemask_epi8(eqa2);
            let mask3 = _mm_movemask_epi8(eqa3);
            return Some(at + reverse_pos3(mask1, mask2, mask3));
        }
    }
    while ptr >= start_ptr.add(VECTOR_SIZE) {
        ptr = ptr.sub(VECTOR_SIZE);
        if let Some(i) =
            reverse_search3(start_ptr, end_ptr, ptr, vn1, vn2, vn3)
        {
            return Some(i);
        }
    }
    if ptr > start_ptr {
        debug_assert!(sub(ptr, start_ptr) < VECTOR_SIZE);
        return reverse_search3(start_ptr, end_ptr, start_ptr, vn1, vn2, vn3);
    }
    None
}

#[target_feature(enable = "sse2")]
pub unsafe fn forward_search1(
    start_ptr: *const u8,
    end_ptr: *const u8,
    ptr: *const u8,
    vn1: __m128i,
) -> Option<usize> {
    debug_assert!(sub(end_ptr, start_ptr) >= VECTOR_SIZE);
    debug_assert!(start_ptr <= ptr);
    debug_assert!(ptr <= end_ptr.sub(VECTOR_SIZE));

    let chunk = _mm_loadu_si128(ptr as *const __m128i);
    let mask = _mm_movemask_epi8(_mm_cmpeq_epi8(chunk, vn1));
    if mask != 0 {
        Some(sub(ptr, start_ptr) + forward_pos(mask))
    } else {
        None
    }
}

#[target_feature(enable = "sse2")]
unsafe fn forward_search2(
    start_ptr: *const u8,
    end_ptr: *const u8,
    ptr: *const u8,
    vn1: __m128i,
    vn2: __m128i,
) -> Option<usize> {
    debug_assert!(sub(end_ptr, start_ptr) >= VECTOR_SIZE);
    debug_assert!(start_ptr <= ptr);
    debug_assert!(ptr <= end_ptr.sub(VECTOR_SIZE));

    let chunk = _mm_loadu_si128(ptr as *const __m128i);
    let eq1 = _mm_cmpeq_epi8(chunk, vn1);
    let eq2 = _mm_cmpeq_epi8(chunk, vn2);
    if _mm_movemask_epi8(_mm_or_si128(eq1, eq2)) != 0 {
        let mask1 = _mm_movemask_epi8(eq1);
        let mask2 = _mm_movemask_epi8(eq2);
        Some(sub(ptr, start_ptr) + forward_pos2(mask1, mask2))
    } else {
        None
    }
}

#[target_feature(enable = "sse2")]
pub unsafe fn forward_search3(
    start_ptr: *const u8,
    end_ptr: *const u8,
    ptr: *const u8,
    vn1: __m128i,
    vn2: __m128i,
    vn3: __m128i,
) -> Option<usize> {
    debug_assert!(sub(end_ptr, start_ptr) >= VECTOR_SIZE);
    debug_assert!(start_ptr <= ptr);
    debug_assert!(ptr <= end_ptr.sub(VECTOR_SIZE));

    let chunk = _mm_loadu_si128(ptr as *const __m128i);
    let eq1 = _mm_cmpeq_epi8(chunk, vn1);
    let eq2 = _mm_cmpeq_epi8(chunk, vn2);
    let eq3 = _mm_cmpeq_epi8(chunk, vn3);
    let or = _mm_or_si128(eq1, eq2);
    if _mm_movemask_epi8(_mm_or_si128(or, eq3)) != 0 {
        let mask1 = _mm_movemask_epi8(eq1);
        let mask2 = _mm_movemask_epi8(eq2);
        let mask3 = _mm_movemask_epi8(eq3);
        Some(sub(ptr, start_ptr) + forward_pos3(mask1, mask2, mask3))
    } else {
        None
    }
}

#[target_feature(enable = "sse2")]
unsafe fn reverse_search1(
    start_ptr: *const u8,
    end_ptr: *const u8,
    ptr: *const u8,
    vn1: __m128i,
) -> Option<usize> {
    debug_assert!(sub(end_ptr, start_ptr) >= VECTOR_SIZE);
    debug_assert!(start_ptr <= ptr);
    debug_assert!(ptr <= end_ptr.sub(VECTOR_SIZE));

    let chunk = _mm_loadu_si128(ptr as *const __m128i);
    let mask = _mm_movemask_epi8(_mm_cmpeq_epi8(vn1, chunk));
    if mask != 0 {
        Some(sub(ptr, start_ptr) + reverse_pos(mask))
    } else {
        None
    }
}

#[target_feature(enable = "sse2")]
unsafe fn reverse_search2(
    start_ptr: *const u8,
    end_ptr: *const u8,
    ptr: *const u8,
    vn1: __m128i,
    vn2: __m128i,
) -> Option<usize> {
    debug_assert!(sub(end_ptr, start_ptr) >= VECTOR_SIZE);
    debug_assert!(start_ptr <= ptr);
    debug_assert!(ptr <= end_ptr.sub(VECTOR_SIZE));

    let chunk = _mm_loadu_si128(ptr as *const __m128i);
    let eq1 = _mm_cmpeq_epi8(chunk, vn1);
    let eq2 = _mm_cmpeq_epi8(chunk, vn2);
    if _mm_movemask_epi8(_mm_or_si128(eq1, eq2)) != 0 {
        let mask1 = _mm_movemask_epi8(eq1);
        let mask2 = _mm_movemask_epi8(eq2);
        Some(sub(ptr, start_ptr) + reverse_pos2(mask1, mask2))
    } else {
        None
    }
}

#[target_feature(enable = "sse2")]
unsafe fn reverse_search3(
    start_ptr: *const u8,
    end_ptr: *const u8,
    ptr: *const u8,
    vn1: __m128i,
    vn2: __m128i,
    vn3: __m128i,
) -> Option<usize> {
    debug_assert!(sub(end_ptr, start_ptr) >= VECTOR_SIZE);
    debug_assert!(start_ptr <= ptr);
    debug_assert!(ptr <= end_ptr.sub(VECTOR_SIZE));

    let chunk = _mm_loadu_si128(ptr as *const __m128i);
    let eq1 = _mm_cmpeq_epi8(chunk, vn1);
    let eq2 = _mm_cmpeq_epi8(chunk, vn2);
    let eq3 = _mm_cmpeq_epi8(chunk, vn3);
    let or = _mm_or_si128(eq1, eq2);
    if _mm_movemask_epi8(_mm_or_si128(or, eq3)) != 0 {
        let mask1 = _mm_movemask_epi8(eq1);
        let mask2 = _mm_movemask_epi8(eq2);
        let mask3 = _mm_movemask_epi8(eq3);
        Some(sub(ptr, start_ptr) + reverse_pos3(mask1, mask2, mask3))
    } else {
        None
    }
}

/// Compute the position of the first matching byte from the given mask. The
/// position returned is always in the range [0, 15].
///
/// The mask given is expected to be the result of _mm_movemask_epi8.
fn forward_pos(mask: i32) -> usize {
    // We are dealing with little endian here, where the most significant byte
    // is at a higher address. That means the least significant bit that is set
    // corresponds to the position of our first matching byte. That position
    // corresponds to the number of zeros after the least significant bit.
    mask.trailing_zeros() as usize
}

/// Compute the position of the first matching byte from the given masks. The
/// position returned is always in the range [0, 15]. Each mask corresponds to
/// the equality comparison of a single byte.
///
/// The masks given are expected to be the result of _mm_movemask_epi8, where
/// at least one of the masks is non-zero (i.e., indicates a match).
fn forward_pos2(mask1: i32, mask2: i32) -> usize {
    debug_assert!(mask1 != 0 || mask2 != 0);

    forward_pos(mask1 | mask2)
}

/// Compute the position of the first matching byte from the given masks. The
/// position returned is always in the range [0, 15]. Each mask corresponds to
/// the equality comparison of a single byte.
///
/// The masks given are expected to be the result of _mm_movemask_epi8, where
/// at least one of the masks is non-zero (i.e., indicates a match).
fn forward_pos3(mask1: i32, mask2: i32, mask3: i32) -> usize {
    debug_assert!(mask1 != 0 || mask2 != 0 || mask3 != 0);

    forward_pos(mask1 | mask2 | mask3)
}

/// Compute the position of the last matching byte from the given mask. The
/// position returned is always in the range [0, 15].
///
/// The mask given is expected to be the result of _mm_movemask_epi8.
fn reverse_pos(mask: i32) -> usize {
    // We are dealing with little endian here, where the most significant byte
    // is at a higher address. That means the most significant bit that is set
    // corresponds to the position of our last matching byte. The position from
    // the end of the mask is therefore the number of leading zeros in a 16
    // bit integer, and the position from the start of the mask is therefore
    // 16 - (leading zeros) - 1.
    VECTOR_SIZE - (mask as u16).leading_zeros() as usize - 1
}

/// Compute the position of the last matching byte from the given masks. The
/// position returned is always in the range [0, 15]. Each mask corresponds to
/// the equality comparison of a single byte.
///
/// The masks given are expected to be the result of _mm_movemask_epi8, where
/// at least one of the masks is non-zero (i.e., indicates a match).
fn reverse_pos2(mask1: i32, mask2: i32) -> usize {
    debug_assert!(mask1 != 0 || mask2 != 0);

    reverse_pos(mask1 | mask2)
}

/// Compute the position of the last matching byte from the given masks. The
/// position returned is always in the range [0, 15]. Each mask corresponds to
/// the equality comparison of a single byte.
///
/// The masks given are expected to be the result of _mm_movemask_epi8, where
/// at least one of the masks is non-zero (i.e., indicates a match).
fn reverse_pos3(mask1: i32, mask2: i32, mask3: i32) -> usize {
    debug_assert!(mask1 != 0 || mask2 != 0 || mask3 != 0);

    reverse_pos(mask1 | mask2 | mask3)
}

/// Subtract `b` from `a` and return the difference. `a` should be greater than
/// or equal to `b`.
fn sub(a: *const u8, b: *const u8) -> usize {
    debug_assert!(a >= b);
    (a as usize) - (b as usize)
}