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
//! This module is only included on x86 and x86_64.
use crate::Config;
use crate::decode::block::{BlockDecoder, IntoBlockDecoder, ScalarBlockDecoder};
use crate::decode::DecodeError;
use crate::{Std, StdNoPad, UrlSafe, UrlSafeNoPad, Crypt, Fast};
#[derive(Debug, Clone, Copy)]
pub struct Decoder<C>(C);

impl<C> BlockDecoder for Decoder<C> where C: Config + avx2::Translate256i {
    #[inline]
    fn decode_blocks(
        self,
        input: &[u8],
        output: &mut [u8],
    ) -> Result<(usize, usize), DecodeError> {
        if let Ok(decoder) = avx2::Decoder::new(self.0) {
            Ok(decoder.decode_blocks(input, output))
        } else {
            ScalarBlockDecoder::new(self.0).decode_blocks(input, output)
        }
    }
}

macro_rules! define_into_block_decoder {
    ($( $cfg:ident ),+) => {$(
        impl IntoBlockDecoder for $cfg {
            type BlockDecoder = Decoder<Self>;

            #[inline]
            fn into_block_decoder(self) -> Self::BlockDecoder {
                Decoder(self)
            }
        }
    )+}
}
define_into_block_decoder!(Std,StdNoPad,UrlSafe,UrlSafeNoPad,Crypt,Fast);

mod avx2 {
     #[cfg(target_arch = "x86")]
    use std::arch::x86::*;
    #[cfg(target_arch = "x86_64")]
    use std::arch::x86_64::*;
    use crate::{Std, StdNoPad, UrlSafe, UrlSafeNoPad, Crypt, Fast};

    pub trait Translate256i: Copy {
        unsafe fn translate_m256i(input: __m256i) -> Result<__m256i, ()>;
    }

    #[derive(Debug,Clone,Copy)]
    pub(crate) struct Decoder<C>(C);

    impl<C> Decoder<C> where C: Translate256i {
        #[inline]
        pub(crate) fn new(config: C) -> Result<Self, ()> {
            if is_x86_feature_detected!("avx2") {
                Ok(Decoder(config))
            } else {
                Err(())
            }
        }

        pub(crate) fn decode_blocks(
            self,
            input: &[u8],
            output: &mut [u8],
        ) -> (usize, usize) {
            // The unsafe block is required because _encode_blocks relies on AVX2
            // intrinsics. This is safe because Encoder::new() ensures that an
            // encoder is only successfully created when the CPU supports AVX2.
            unsafe { self._decode_blocks(input, output) }
        }

        #[target_feature(enable = "avx2")]
        unsafe fn _decode_blocks(
            self,
            input: &[u8],
            output: &mut [u8],
        ) -> (usize, usize) {
            let mut iter = BlockIter::new(input, output);
            while let Some((input_block, output_block)) = iter.next_chunk() {
                #[allow(clippy::cast_ptr_alignment)]
                let mut data = _mm256_loadu_si256(input_block.as_ptr() as *const __m256i);
                data = match self.decode_block(data) {
                    Ok(data) => data,
                    Err(_) => {
                        // Move back to the beginning of the chunk that failed
                        // and return the remaining slice to the non-optimized
                        // decoder for better error reporting.
                        iter.step_back();
                        return iter.remaining();
                    }
                };
                #[allow(clippy::cast_ptr_alignment)]
                _mm256_storeu_si256(output_block.as_mut_ptr() as *mut __m256i, data);
            }
            iter.remaining()
        }

        #[target_feature(enable = "avx2")]
        unsafe fn decode_block(self, input: __m256i) -> Result<__m256i, ()> {
            let input = C::translate_m256i(input)?;
            let input = _mm256_maddubs_epi16(input, _mm256_set1_epi32(0x0140_0140));
            let input = _mm256_madd_epi16(input, _mm256_set1_epi32(0x0001_1000));
            #[rustfmt::skip]
            let input = _mm256_shuffle_epi8(
                input,
                _mm256_setr_epi8(
                    2, 1, 0,        // The trailing comments fix a bug in tarpaulin
                    6,  5,  4,      // causing the args to be lines not covered.
                    10, 9,  8,      //
                    14, 13, 12,     //
                    -1, -1, -1, -1, //

                    2,  1,  0,      //
                    6,  5,  4,      //
                    10, 9,  8,      //
                    14, 13, 12,     //
                    -1, -1, -1, -1, //
                ),
            );
            Ok(_mm256_permutevar8x32_epi32(input, _mm256_setr_epi32(0, 1, 2, 4, 5, 6, -1, -1)))
        }
    }

    define_block_iter!(name=BlockIter, input_chunk_size=32, input_stride=32, output_chunk_size=32, output_stride=24);

    #[target_feature(enable = "avx2")]
    #[inline]
    unsafe fn translate_std(input: __m256i) -> Result<__m256i, ()> {
        let hi_nibbles = _mm256_and_si256(_mm256_srli_epi32(input, 4), _mm256_set1_epi8(0x0f));
        let low_nibbles = _mm256_and_si256(input, _mm256_set1_epi8(0x0f));
        if !is_valid_std(hi_nibbles, low_nibbles) {
            return Err(());
        }

        #[rustfmt::skip]
        let shift_lut = _mm256_setr_epi8(
            0,   0,  19,   4, -65, -65, -71, -71,   // The trailing comments fix a bug in tarpaulin
            0,   0,   0,   0,   0,   0,   0,   0,   // causing the args to be lines not covered.
            0,   0,  19,   4, -65, -65, -71, -71,   //
            0,   0,   0,   0,   0,   0,   0,   0,   //
        );

        let sh = _mm256_shuffle_epi8(shift_lut, hi_nibbles);
        let eq_underscore = _mm256_cmpeq_epi8(input, _mm256_set1_epi8(b'/' as i8));
        let shift = _mm256_blendv_epi8(sh, _mm256_set1_epi8(16), eq_underscore);
        Ok(_mm256_add_epi8(input, shift))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    #[allow(overflowing_literals)]
    unsafe fn is_valid_std(hi_nibbles: __m256i, low_nibbles: __m256i) -> bool {
        #[rustfmt::skip]
        let mask_lut = _mm256_setr_epi8(
            0b1010_1000,                            // 0
            0b1111_1000, 0b1111_1000, 0b1111_1000,  // 1 .. 9
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_0000,                            // 10
            0b0101_0100,                            // 11
            0b0101_0000, 0b0101_0000, 0b0101_0000,  // 12 .. 14
            0b0101_0100,                            // 15

            0b1010_1000,                            // 0
            0b1111_1000, 0b1111_1000, 0b1111_1000,  // 1 .. 9
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_0000,                            // 10
            0b0101_0100,                            // 11
            0b0101_0000, 0b0101_0000, 0b0101_0000,  // 12 .. 14
            0b0101_0100,                            // 15
        );

        #[rustfmt::skip]
        let bit_pos_lut = _mm256_setr_epi8(
            0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, // The trailing comments fix a bug in tarpaulin
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // causing the args to be lines not covered.
            0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, //
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
        );

        let m = _mm256_shuffle_epi8(mask_lut, low_nibbles);
        let bit = _mm256_shuffle_epi8(bit_pos_lut, hi_nibbles);
        let non_match = _mm256_cmpeq_epi8(_mm256_and_si256(m, bit), _mm256_setzero_si256());
        _mm256_movemask_epi8(non_match) == 0
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    unsafe fn translate_url_safe(input: __m256i) -> Result<__m256i, ()> {
        let hi_nibbles = _mm256_and_si256(_mm256_srli_epi32(input, 4), _mm256_set1_epi8(0x0f));
        let low_nibbles = _mm256_and_si256(input, _mm256_set1_epi8(0x0f));
        if !is_valid_url_safe(hi_nibbles, low_nibbles) {
            return Err(());
        }

        #[rustfmt::skip]
        let shift_lut = _mm256_setr_epi8(
            0,   0,  17,   4, -65, -65, -71, -71,   // The trailing comments fix a bug in tarpaulin
            0,   0,   0,   0,   0,   0,   0,   0,   // causing the args to be lines not covered.
            0,   0,  17,   4, -65, -65, -71, -71,   //
            0,   0,   0,   0,   0,   0,   0,   0,   //
        );

        let sh = _mm256_shuffle_epi8(shift_lut, hi_nibbles);
        let eq_underscore = _mm256_cmpeq_epi8(input, _mm256_set1_epi8(b'_' as i8));
        let shift = _mm256_blendv_epi8(sh, _mm256_set1_epi8(-32), eq_underscore);
        Ok(_mm256_add_epi8(input, shift))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    #[allow(overflowing_literals)]
    unsafe fn is_valid_url_safe(hi_nibbles: __m256i, low_nibbles: __m256i) -> bool {
        #[rustfmt::skip]
        let mask_lut = _mm256_setr_epi8(
            0b1010_1000,                            // 0
            0b1111_1000, 0b1111_1000, 0b1111_1000,  // 1 .. 9
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_0000,                            // 10
            0b0101_0000, 0b0101_0000,               // 11 .. 12
            0b0101_0100,                            // 13
            0b0101_0000,                            // 14
            0b0111_0000,                            // 15

            0b1010_1000,                            // 0
            0b1111_1000, 0b1111_1000, 0b1111_1000,  // 1 .. 9
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_0000,                            // 10
            0b0101_0000, 0b0101_0000,               // 11 .. 12
            0b0101_0100,                            // 13
            0b0101_0000,                            // 14
            0b0111_0000,                            // 15
        );

        #[rustfmt::skip]
        let bit_pos_lut = _mm256_setr_epi8(
            0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, // The trailing comments fix a bug in tarpaulin
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // causing the args to be lines not covered.
            0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, //
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
        );

        let m = _mm256_shuffle_epi8(mask_lut, low_nibbles);
        let bit = _mm256_shuffle_epi8(bit_pos_lut, hi_nibbles);
        let non_match = _mm256_cmpeq_epi8(_mm256_and_si256(m, bit), _mm256_setzero_si256());
        _mm256_movemask_epi8(non_match) == 0
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    unsafe fn translate_crypt(input: __m256i) -> Result<__m256i, ()> {
        let hi_nibbles = _mm256_and_si256(_mm256_srli_epi32(input, 4), _mm256_set1_epi8(0x0f));
        let low_nibbles = _mm256_and_si256(input, _mm256_set1_epi8(0x0f));
        if !is_valid_crypt(hi_nibbles, low_nibbles) {
            return Err(());
        }

        #[rustfmt::skip]
        let shift_lut = _mm256_setr_epi8(
            0,   0, -46, -46, -53, -53, -59, -59,   // The trailing comments fix a bug in tarpaulin
            0,   0,   0,   0,   0,   0,   0,   0,   // causing the args to be lines not covered.
            0,   0, -46, -46, -53, -53, -59, -59,   //
            0,   0,   0,   0,   0,   0,   0,   0,   //
        );
        let sh = _mm256_shuffle_epi8(shift_lut, hi_nibbles);
        Ok(_mm256_add_epi8(input, sh))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    #[allow(overflowing_literals)]
    unsafe fn is_valid_crypt(hi_nibbles: __m256i, low_nibbles: __m256i) -> bool {
        #[rustfmt::skip]
        let mask_lut = _mm256_setr_epi8(
            0b1010_1000,                            // 0
            0b1111_1000, 0b1111_1000, 0b1111_1000,  // 1 .. 9 
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_0000,                            // 10
            0b0101_0000, 0b0101_0000, 0b0101_0000,  // 11 .. 13
            0b0101_0100, 0b0101_0100,               // 14 .. 15

            0b1010_1000,                            // 0
            0b1111_1000, 0b1111_1000, 0b1111_1000,  // 1 .. 9 
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_1000, 0b1111_1000, 0b1111_1000,  //
            0b1111_0000,                            // 10
            0b0101_0000, 0b0101_0000, 0b0101_0000,  // 11 .. 13
            0b0101_0100, 0b0101_0100,               // 14 .. 15
        );

        #[rustfmt::skip]
        let bit_pos_lut = _mm256_setr_epi8(
            0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, // The trailing comments fix a bug in tarpaulin
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // causing the args to be lines not covered.
            0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, //
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
        );

        let m = _mm256_shuffle_epi8(mask_lut, low_nibbles);
        let bit = _mm256_shuffle_epi8(bit_pos_lut, hi_nibbles);
        let non_match = _mm256_cmpeq_epi8(_mm256_and_si256(m, bit), _mm256_setzero_si256());
        _mm256_movemask_epi8(non_match) == 0
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    unsafe fn translate_fast(input: __m256i) -> Result<__m256i, ()> {
        if !is_valid_fast(input) {
            return Err(());
        }
        Ok(_mm256_sub_epi8(input, _mm256_set1_epi8(62)))
    }

    #[target_feature(enable = "avx2")]
    #[inline]
    #[allow(overflowing_literals)]
    unsafe fn is_valid_fast(input: __m256i) -> bool {
        let gt_125 = _mm256_cmpgt_epi8(input, _mm256_set1_epi8(125));
        let lt_61 = _mm256_cmpgt_epi8(_mm256_set1_epi8(62), input);
        _mm256_movemask_epi8(_mm256_or_si256(gt_125, lt_61)) == 0
    }

    impl Translate256i for Std {
        #[inline]
        unsafe fn translate_m256i(input: __m256i) -> Result<__m256i, ()> {
            translate_std(input)
        }
    }

    impl Translate256i for StdNoPad {
        #[inline]
        unsafe fn translate_m256i(input: __m256i) -> Result<__m256i, ()> {
            translate_std(input)
        }
    }

    impl Translate256i for UrlSafe {
        #[inline]
        unsafe fn translate_m256i(input: __m256i) -> Result<__m256i, ()> {
            translate_url_safe(input)
        }
    }

    impl Translate256i for UrlSafeNoPad {
        #[inline]
        unsafe fn translate_m256i(input: __m256i) -> Result<__m256i, ()> {
            translate_url_safe(input)
        }
    }

    impl Translate256i for Crypt {
        #[inline]
        unsafe fn translate_m256i(input: __m256i) -> Result<__m256i, ()> {
            translate_crypt(input)
        }
    }

    impl Translate256i for Fast {
        #[inline]
        unsafe fn translate_m256i(input: __m256i) -> Result<__m256i, ()> {
            translate_fast(input)
        }
    }
}