bytecount/simd/
x86_avx2.rs

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
use std::arch::x86_64::{
    __m256i, _mm256_and_si256, _mm256_cmpeq_epi8, _mm256_extract_epi64, _mm256_loadu_si256,
    _mm256_sad_epu8, _mm256_set1_epi8, _mm256_setzero_si256, _mm256_sub_epi8, _mm256_xor_si256,
};

#[target_feature(enable = "avx2")]
pub unsafe fn _mm256_set1_epu8(a: u8) -> __m256i {
    _mm256_set1_epi8(a as i8)
}

#[target_feature(enable = "avx2")]
pub unsafe fn mm256_cmpneq_epi8(a: __m256i, b: __m256i) -> __m256i {
    _mm256_xor_si256(_mm256_cmpeq_epi8(a, b), _mm256_set1_epi8(-1))
}

const MASK: [u8; 64] = [
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
];

#[target_feature(enable = "avx2")]
unsafe fn mm256_from_offset(slice: &[u8], offset: usize) -> __m256i {
    _mm256_loadu_si256(slice.as_ptr().add(offset) as *const _)
}

#[target_feature(enable = "avx2")]
unsafe fn sum(u8s: &__m256i) -> usize {
    let sums = _mm256_sad_epu8(*u8s, _mm256_setzero_si256());
    (_mm256_extract_epi64(sums, 0)
        + _mm256_extract_epi64(sums, 1)
        + _mm256_extract_epi64(sums, 2)
        + _mm256_extract_epi64(sums, 3)) as usize
}

#[target_feature(enable = "avx2")]
pub unsafe fn chunk_count(haystack: &[u8], needle: u8) -> usize {
    assert!(haystack.len() >= 32);

    let mut offset = 0;
    let mut count = 0;

    let needles = _mm256_set1_epu8(needle);

    // 8160
    while haystack.len() >= offset + 32 * 255 {
        let mut counts = _mm256_setzero_si256();
        for _ in 0..255 {
            counts = _mm256_sub_epi8(
                counts,
                _mm256_cmpeq_epi8(mm256_from_offset(haystack, offset), needles),
            );
            offset += 32;
        }
        count += sum(&counts);
    }

    // 4096
    if haystack.len() >= offset + 32 * 128 {
        let mut counts = _mm256_setzero_si256();
        for _ in 0..128 {
            counts = _mm256_sub_epi8(
                counts,
                _mm256_cmpeq_epi8(mm256_from_offset(haystack, offset), needles),
            );
            offset += 32;
        }
        count += sum(&counts);
    }

    // 32
    let mut counts = _mm256_setzero_si256();
    for i in 0..(haystack.len() - offset) / 32 {
        counts = _mm256_sub_epi8(
            counts,
            _mm256_cmpeq_epi8(mm256_from_offset(haystack, offset + i * 32), needles),
        );
    }
    if haystack.len() % 32 != 0 {
        counts = _mm256_sub_epi8(
            counts,
            _mm256_and_si256(
                _mm256_cmpeq_epi8(mm256_from_offset(haystack, haystack.len() - 32), needles),
                mm256_from_offset(&MASK, haystack.len() % 32),
            ),
        );
    }
    count += sum(&counts);

    count
}

#[target_feature(enable = "avx2")]
unsafe fn is_leading_utf8_byte(u8s: __m256i) -> __m256i {
    mm256_cmpneq_epi8(
        _mm256_and_si256(u8s, _mm256_set1_epu8(0b1100_0000)),
        _mm256_set1_epu8(0b1000_0000),
    )
}

#[target_feature(enable = "avx2")]
pub unsafe fn chunk_num_chars(utf8_chars: &[u8]) -> usize {
    assert!(utf8_chars.len() >= 32);

    let mut offset = 0;
    let mut count = 0;

    // 8160
    while utf8_chars.len() >= offset + 32 * 255 {
        let mut counts = _mm256_setzero_si256();

        for _ in 0..255 {
            counts = _mm256_sub_epi8(
                counts,
                is_leading_utf8_byte(mm256_from_offset(utf8_chars, offset)),
            );
            offset += 32;
        }
        count += sum(&counts);
    }

    // 4096
    if utf8_chars.len() >= offset + 32 * 128 {
        let mut counts = _mm256_setzero_si256();
        for _ in 0..128 {
            counts = _mm256_sub_epi8(
                counts,
                is_leading_utf8_byte(mm256_from_offset(utf8_chars, offset)),
            );
            offset += 32;
        }
        count += sum(&counts);
    }

    // 32
    let mut counts = _mm256_setzero_si256();
    for i in 0..(utf8_chars.len() - offset) / 32 {
        counts = _mm256_sub_epi8(
            counts,
            is_leading_utf8_byte(mm256_from_offset(utf8_chars, offset + i * 32)),
        );
    }
    if utf8_chars.len() % 32 != 0 {
        counts = _mm256_sub_epi8(
            counts,
            _mm256_and_si256(
                is_leading_utf8_byte(mm256_from_offset(utf8_chars, utf8_chars.len() - 32)),
                mm256_from_offset(&MASK, utf8_chars.len() % 32),
            ),
        );
    }
    count += sum(&counts);

    count
}