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
use crate::*;
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

/*
 * legal utf-8 byte sequence
 * http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf - page 94
 *
 *  Code Points        1st       2s       3s       4s
 * U+0000..U+007F     00..7F
 * U+0080..U+07FF     C2..DF   80..BF
 * U+0800..U+0FFF     E0       A0..BF   80..BF
 * U+1000..U+CFFF     E1..EC   80..BF   80..BF
 * U+D000..U+D7FF     ED       80..9F   80..BF
 * U+E000..U+FFFF     EE..EF   80..BF   80..BF
 * U+10000..U+3FFFF   F0       90..BF   80..BF   80..BF
 * U+40000..U+FFFFF   F1..F3   80..BF   80..BF   80..BF
 * U+100000..U+10FFFF F4       80..8F   80..BF   80..BF
 *
 */

// all byte values must be no larger than 0xF4

/*****************************/
#[cfg_attr(not(feature = "no-inline"), inline)]
fn push_last_byte_of_a_to_b(a: __m128i, b: __m128i) -> __m128i {
    unsafe { _mm_alignr_epi8(b, a, 15) }
}

#[cfg_attr(not(feature = "no-inline"), inline)]
fn push_last_2bytes_of_a_to_b(a: __m128i, b: __m128i) -> __m128i {
    unsafe { _mm_alignr_epi8(b, a, 14) }
}

// all byte values must be no larger than 0xF4
#[cfg_attr(not(feature = "no-inline"), inline)]
fn avxcheck_smaller_than_0xf4(current_bytes: __m128i, has_error: &mut __m128i) {
    // unsigned, saturates to 0 below max
    *has_error = unsafe {
        _mm_or_si128(
            *has_error,
            _mm_subs_epu8(current_bytes, _mm_set1_epi8(static_cast_i8!(0xF4u8))),
        )
    };
}

#[cfg_attr(not(feature = "no-inline"), inline)]
fn avxcontinuation_lengths(high_nibbles: __m128i) -> __m128i {
    unsafe {
        _mm_shuffle_epi8(
            _mm_setr_epi8(
                1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII)
                0, 0, 0, 0, // 10xx (continuation)
                2, 2, // 110x
                3, // 1110
                4, // 1111, next should be 0 (not checked here)
            ),
            high_nibbles,
        )
    }
}

#[cfg_attr(not(feature = "no-inline"), inline)]
fn avxcarry_continuations(initial_lengths: __m128i, previous_carries: __m128i) -> __m128i {
    unsafe {
        let right1: __m128i = _mm_subs_epu8(
            push_last_byte_of_a_to_b(previous_carries, initial_lengths),
            _mm_set1_epi8(1),
        );
        let sum: __m128i = _mm_add_epi8(initial_lengths, right1);
        let right2: __m128i = _mm_subs_epu8(
            push_last_2bytes_of_a_to_b(previous_carries, sum),
            _mm_set1_epi8(2),
        );
        _mm_add_epi8(sum, right2)
    }
}

#[cfg_attr(not(feature = "no-inline"), inline)]
fn avxcheck_continuations(initial_lengths: __m128i, carries: __m128i, has_error: &mut __m128i) {
    // overlap || underlap
    // carry > length && length > 0 || !(carry > length) && !(length > 0)
    // (carries > length) == (lengths > 0)
    unsafe {
        let overunder: __m128i = _mm_cmpeq_epi8(
            _mm_cmpgt_epi8(carries, initial_lengths),
            _mm_cmpgt_epi8(initial_lengths, _mm_setzero_si128()),
        );

        *has_error = _mm_or_si128(*has_error, overunder);
    }
}

// when 0xED is found, next byte must be no larger than 0x9F
// when 0xF4 is found, next byte must be no larger than 0x8F
// next byte must be continuation, ie sign bit is set, so signed < is ok
#[cfg_attr(not(feature = "no-inline"), inline)]
fn avxcheck_first_continuation_max(
    current_bytes: __m128i,
    off1_current_bytes: __m128i,
    has_error: &mut __m128i,
) {
    unsafe {
        let mask_ed: __m128i = _mm_cmpeq_epi8(
            off1_current_bytes,
            _mm_set1_epi8(static_cast_i8!(0xEDu8)),
        );
        let mask_f4: __m128i = _mm_cmpeq_epi8(
            off1_current_bytes,
            _mm_set1_epi8(static_cast_i8!(0xF4u8)),
        );

        let badfollow_ed: __m128i = _mm_and_si128(
            _mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(static_cast_i8!(0x9Fu8))),
            mask_ed,
        );
        let badfollow_f4: __m128i = _mm_and_si128(
            _mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(static_cast_i8!(0x8Fu8))),
            mask_f4,
        );

        *has_error = _mm_or_si128(*has_error, _mm_or_si128(badfollow_ed, badfollow_f4));
    }
}

// map off1_hibits => error condition
// hibits     off1    cur
// C       => < C2 && true
// E       => < E1 && < A0
// F       => < F1 && < 90
// else      false && false
#[cfg_attr(not(feature = "no-inline"), inline)]
fn avxcheck_overlong(
    current_bytes: __m128i,
    off1_current_bytes: __m128i,
    hibits: __m128i,
    previous_hibits: __m128i,
    has_error: &mut __m128i,
) {
    unsafe {
        let off1_hibits: __m128i = push_last_byte_of_a_to_b(previous_hibits, hibits);
        let initial_mins: __m128i = _mm_shuffle_epi8(
            _mm_setr_epi8(
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128, // 10xx => false
                static_cast_i8!(0xC2u8),
                -128,                    // 110x
                static_cast_i8!(0xE1u8), // 1110
                static_cast_i8!(0xF1u8), // 1111
            ),
            off1_hibits,
        );

        let initial_under: __m128i = _mm_cmpgt_epi8(initial_mins, off1_current_bytes);

        let second_mins: __m128i = _mm_shuffle_epi8(
            _mm_setr_epi8(
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128,
                -128, // 10xx => false
                127,
                127,                     // 110x => true
                static_cast_i8!(0xA0u8), // 1110
                static_cast_i8!(0x90u8), // 1111
            ),
            off1_hibits,
        );
        let second_under: __m128i = _mm_cmpgt_epi8(second_mins, current_bytes);
        *has_error = _mm_or_si128(*has_error, _mm_and_si128(initial_under, second_under));
    }
}

pub struct AvxProcessedUtfBytes {
    rawbytes: __m128i,
    high_nibbles: __m128i,
    pub carried_continuations: __m128i,
}

impl Default for AvxProcessedUtfBytes {
    #[cfg_attr(not(feature = "no-inline"), inline)]
    fn default() -> Self {
        unsafe {
            AvxProcessedUtfBytes {
                rawbytes: _mm_setzero_si128(),
                high_nibbles: _mm_setzero_si128(),
                carried_continuations: _mm_setzero_si128(),
            }
        }
    }
}

#[cfg_attr(not(feature = "no-inline"), inline)]
fn avx_count_nibbles(bytes: __m128i, answer: &mut AvxProcessedUtfBytes) {
    answer.rawbytes = bytes;
    answer.high_nibbles =
        unsafe { _mm_and_si128(_mm_srli_epi16(bytes, 4), _mm_set1_epi8(0x0F)) };
}

// check whether the current bytes are valid UTF-8
// at the end of the function, previous gets updated
#[cfg_attr(not(feature = "no-inline"), inline)]
pub fn avxcheck_utf8_bytes(
    current_bytes: __m128i,
    previous: &AvxProcessedUtfBytes,
    has_error: &mut __m128i,
) -> AvxProcessedUtfBytes {
    let mut pb = AvxProcessedUtfBytes::default();
    avx_count_nibbles(current_bytes, &mut pb);

    avxcheck_smaller_than_0xf4(current_bytes, has_error);

    let initial_lengths: __m128i = avxcontinuation_lengths(pb.high_nibbles);

    pb.carried_continuations =
        avxcarry_continuations(initial_lengths, previous.carried_continuations);

    avxcheck_continuations(initial_lengths, pb.carried_continuations, has_error);

    let off1_current_bytes: __m128i = push_last_byte_of_a_to_b(previous.rawbytes, pb.rawbytes);
    avxcheck_first_continuation_max(current_bytes, off1_current_bytes, has_error);

    avxcheck_overlong(
        current_bytes,
        off1_current_bytes,
        pb.high_nibbles,
        previous.high_nibbles,
        has_error,
    );
    pb
}