Debian Codegraph @

mypyc/lib-rt/base64/arch/neon32/enc_translate.c

raw
1 static BASE64_FORCE_INLINE uint8x16x4_t
2 enc_translate (const uint8x16x4_t in)
3 {
4 // A lookup table containing the absolute offsets for all ranges:
5 const uint8x16_t lut = {
6 65U, 71U, 252U, 252U,
7 252U, 252U, 252U, 252U,
8 252U, 252U, 252U, 252U,
9 237U, 240U, 0U, 0U
10 };
11
12 const uint8x16_t offset = vdupq_n_u8(51);
13
14 uint8x16x4_t indices, mask, delta, out;
15
16 // Translate values 0..63 to the Base64 alphabet. There are five sets:
17 // # From To Abs Index Characters
18 // 0 [0..25] [65..90] +65 0 ABCDEFGHIJKLMNOPQRSTUVWXYZ
19 // 1 [26..51] [97..122] +71 1 abcdefghijklmnopqrstuvwxyz
20 // 2 [52..61] [48..57] -4 [2..11] 0123456789
21 // 3 [62] [43] -19 12 +
22 // 4 [63] [47] -16 13 /
23
24 // Create LUT indices from input:
25 // the index for range #0 is right, others are 1 less than expected:
26 indices.val[0] = vqsubq_u8(in.val[0], offset);
27 indices.val[1] = vqsubq_u8(in.val[1], offset);
28 indices.val[2] = vqsubq_u8(in.val[2], offset);
29 indices.val[3] = vqsubq_u8(in.val[3], offset);
30
31 // mask is 0xFF (-1) for range #[1..4] and 0x00 for range #0:
32 mask.val[0] = vcgtq_u8(in.val[0], vdupq_n_u8(25));
33 mask.val[1] = vcgtq_u8(in.val[1], vdupq_n_u8(25));
34 mask.val[2] = vcgtq_u8(in.val[2], vdupq_n_u8(25));
35 mask.val[3] = vcgtq_u8(in.val[3], vdupq_n_u8(25));
36
37 // Subtract -1, so add 1 to indices for range #[1..4], All indices are
38 // now correct:
39 indices.val[0] = vsubq_u8(indices.val[0], mask.val[0]);
40 indices.val[1] = vsubq_u8(indices.val[1], mask.val[1]);
41 indices.val[2] = vsubq_u8(indices.val[2], mask.val[2]);
42 indices.val[3] = vsubq_u8(indices.val[3], mask.val[3]);
43
44 // Lookup delta values:
45 delta.val[0] = vqtbl1q_u8(lut, indices.val[0]);
46 delta.val[1] = vqtbl1q_u8(lut, indices.val[1]);
47 delta.val[2] = vqtbl1q_u8(lut, indices.val[2]);
48 delta.val[3] = vqtbl1q_u8(lut, indices.val[3]);
49
50 // Add delta values:
51 out.val[0] = vaddq_u8(in.val[0], delta.val[0]);
52 out.val[1] = vaddq_u8(in.val[1], delta.val[1]);
53 out.val[2] = vaddq_u8(in.val[2], delta.val[2]);
54 out.val[3] = vaddq_u8(in.val[3], delta.val[3]);
55
56 return out;
57 }