|
| 1 | +/* AArch64 NEON helpers for the base64 fast paths in binascii.c. |
| 2 | + * |
| 3 | + * Both functions process a prefix of the input and return how much they |
| 4 | + * consumed. base64_encode_fast() and base64_decode_fast() continue from |
| 5 | + * there with the scalar loops, which remain the reference for behaviour: |
| 6 | + * the tail, padding, invalid characters and error reporting are never |
| 7 | + * handled here. |
| 8 | + * |
| 9 | + * Neither function reads outside [in, in + in_len) or writes more than the |
| 10 | + * scalar code would for the consumed prefix. |
| 11 | + */ |
| 12 | +#ifndef BINASCII_NEON_H |
| 13 | +#define BINASCII_NEON_H |
| 14 | + |
| 15 | +#if defined(__aarch64__) || defined(_M_ARM64) |
| 16 | +#define BINASCII_HAVE_NEON 1 |
| 17 | + |
| 18 | +#include <arm_neon.h> |
| 19 | +#include <stdint.h> |
| 20 | + |
| 21 | +/* Encoding works for any 64-byte alphabet: the 6-bit indices are looked up |
| 22 | + * in the table with TBL, which takes four 16-byte registers. |
| 23 | + * |
| 24 | + * 48 input bytes -> 64 output characters. */ |
| 25 | +static inline void |
| 26 | +b64neon_encode48(const unsigned char *in, unsigned char *out, |
| 27 | + uint8x16x4_t tbl) |
| 28 | +{ |
| 29 | + uint8x16x3_t s = vld3q_u8(in); |
| 30 | + uint8x16x4_t d; |
| 31 | + uint8x16_t m = vdupq_n_u8(0x3f); |
| 32 | + |
| 33 | + d.val[0] = vshrq_n_u8(s.val[0], 2); |
| 34 | + d.val[1] = vandq_u8(vsliq_n_u8(vshrq_n_u8(s.val[1], 4), s.val[0], 4), m); |
| 35 | + d.val[2] = vandq_u8(vsliq_n_u8(vshrq_n_u8(s.val[2], 6), s.val[1], 2), m); |
| 36 | + d.val[3] = vandq_u8(s.val[2], m); |
| 37 | + |
| 38 | + d.val[0] = vqtbl4q_u8(tbl, d.val[0]); |
| 39 | + d.val[1] = vqtbl4q_u8(tbl, d.val[1]); |
| 40 | + d.val[2] = vqtbl4q_u8(tbl, d.val[2]); |
| 41 | + d.val[3] = vqtbl4q_u8(tbl, d.val[3]); |
| 42 | + vst4q_u8(out, d); |
| 43 | +} |
| 44 | + |
| 45 | +/* 24 input bytes -> 32 output characters. */ |
| 46 | +static inline void |
| 47 | +b64neon_encode24(const unsigned char *in, unsigned char *out, |
| 48 | + uint8x16x4_t tbl) |
| 49 | +{ |
| 50 | + uint8x8x3_t s = vld3_u8(in); |
| 51 | + uint8x8x4_t d; |
| 52 | + uint8x8_t m = vdup_n_u8(0x3f); |
| 53 | + |
| 54 | + d.val[0] = vshr_n_u8(s.val[0], 2); |
| 55 | + d.val[1] = vand_u8(vsli_n_u8(vshr_n_u8(s.val[1], 4), s.val[0], 4), m); |
| 56 | + d.val[2] = vand_u8(vsli_n_u8(vshr_n_u8(s.val[2], 6), s.val[1], 2), m); |
| 57 | + d.val[3] = vand_u8(s.val[2], m); |
| 58 | + |
| 59 | + d.val[0] = vqtbl4_u8(tbl, d.val[0]); |
| 60 | + d.val[1] = vqtbl4_u8(tbl, d.val[1]); |
| 61 | + d.val[2] = vqtbl4_u8(tbl, d.val[2]); |
| 62 | + d.val[3] = vqtbl4_u8(tbl, d.val[3]); |
| 63 | + vst4_u8(out, d); |
| 64 | +} |
| 65 | + |
| 66 | +/* Encode all complete 3-byte groups. Requires in_len >= 24. |
| 67 | + * Returns in_len rounded down to a multiple of 3. |
| 68 | + * |
| 69 | + * A length that is not a multiple of the block size is finished by encoding |
| 70 | + * one more block that overlaps the previous one. The overlapping part is |
| 71 | + * rewritten with the same characters, since block boundaries stay on 3-byte |
| 72 | + * group boundaries. */ |
| 73 | +static inline Py_ssize_t |
| 74 | +base64_encode_neon(const unsigned char *in, Py_ssize_t in_len, |
| 75 | + unsigned char *out, const unsigned char *table) |
| 76 | +{ |
| 77 | + Py_ssize_t n = in_len / 3 * 3; |
| 78 | + Py_ssize_t i = 0; |
| 79 | + uint8x16x4_t tbl = vld1q_u8_x4(table); |
| 80 | + |
| 81 | + assert(n >= 24); |
| 82 | + if (n < 48) { |
| 83 | + b64neon_encode24(in, out, tbl); |
| 84 | + if (n > 24) { |
| 85 | + b64neon_encode24(in + n - 24, out + (n - 24) / 3 * 4, tbl); |
| 86 | + } |
| 87 | + return n; |
| 88 | + } |
| 89 | + |
| 90 | + for (; i + 96 <= n; i += 96) { |
| 91 | + b64neon_encode48(in + i, out + i / 3 * 4, tbl); |
| 92 | + b64neon_encode48(in + i + 48, out + i / 3 * 4 + 64, tbl); |
| 93 | + } |
| 94 | + if (i + 48 <= n) { |
| 95 | + b64neon_encode48(in + i, out + i / 3 * 4, tbl); |
| 96 | + i += 48; |
| 97 | + } |
| 98 | + if (i < n) { |
| 99 | + b64neon_encode48(in + n - 48, out + (n - 48) / 3 * 4, tbl); |
| 100 | + } |
| 101 | + return n; |
| 102 | +} |
| 103 | + |
| 104 | +/* Decoding with the standard alphabet validates and translates with nibble |
| 105 | + * lookups instead of the 256-byte table. The technique is described in |
| 106 | + * W. Mula and D. Lemire, "Faster Base64 Encoding and Decoding using AVX2 |
| 107 | + * Instructions", ACM TOW 12(3), 2018. |
| 108 | + * |
| 109 | + * For each input byte, err is nonzero iff the byte is not in the alphabet. */ |
| 110 | +static inline uint8x16_t |
| 111 | +b64neon_translate_std(uint8x16_t x, uint8x16_t *err) |
| 112 | +{ |
| 113 | + static const uint8_t lut_lo[16] = { |
| 114 | + 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, |
| 115 | + 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A, |
| 116 | + }; |
| 117 | + static const uint8_t lut_hi[16] = { |
| 118 | + 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, |
| 119 | + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, |
| 120 | + }; |
| 121 | + static const uint8_t lut_roll[16] = { |
| 122 | + 0, 16, 19, 4, (uint8_t)-65, (uint8_t)-65, (uint8_t)-71, (uint8_t)-71, |
| 123 | + 0, 0, 0, 0, 0, 0, 0, 0, |
| 124 | + }; |
| 125 | + uint8x16_t hi = vshrq_n_u8(x, 4); |
| 126 | + uint8x16_t lo = vandq_u8(x, vdupq_n_u8(0x0f)); |
| 127 | + uint8x16_t e = vandq_u8(vqtbl1q_u8(vld1q_u8(lut_lo), lo), |
| 128 | + vqtbl1q_u8(vld1q_u8(lut_hi), hi)); |
| 129 | + /* '/' shares its high nibble with '+' but needs a different offset; |
| 130 | + * vceqq_u8() gives -1 for it, which selects lut_roll[1]. */ |
| 131 | + uint8x16_t idx = vaddq_u8(hi, vceqq_u8(x, vdupq_n_u8('/'))); |
| 132 | + *err = vorrq_u8(*err, e); |
| 133 | + return vaddq_u8(x, vqtbl1q_u8(vld1q_u8(lut_roll), idx)); |
| 134 | +} |
| 135 | + |
| 136 | +typedef struct { |
| 137 | + uint8x16x4_t t[4]; |
| 138 | + int high; /* nonzero if some byte >= 0x80 maps to a valid value */ |
| 139 | +} b64neon_table; |
| 140 | + |
| 141 | +/* Other alphabets use the 256-byte reverse table directly, 64 bytes per |
| 142 | + * TBL/TBX. The upper half is skipped when the table rejects every byte |
| 143 | + * >= 0x80, which is the case for every ASCII alphabet. |
| 144 | + * |
| 145 | + * The scalar code rejects a value with either of the top two bits set; |
| 146 | + * a valid value is <= 63, so any rejection makes the OR of err exceed 63. */ |
| 147 | +static inline uint8x16_t |
| 148 | +b64neon_translate_table(uint8x16_t x, uint8x16_t *err, |
| 149 | + const b64neon_table *T) |
| 150 | +{ |
| 151 | + uint8x16_t v = vqtbl4q_u8(T->t[0], x); |
| 152 | + v = vqtbx4q_u8(v, T->t[1], veorq_u8(x, vdupq_n_u8(0x40))); |
| 153 | + if (T->high) { |
| 154 | + v = vqtbx4q_u8(v, T->t[2], veorq_u8(x, vdupq_n_u8(0x80))); |
| 155 | + v = vqtbx4q_u8(v, T->t[3], veorq_u8(x, vdupq_n_u8(0xc0))); |
| 156 | + *err = vorrq_u8(*err, v); |
| 157 | + } |
| 158 | + else { |
| 159 | + *err = vorrq_u8(*err, vorrq_u8(v, vandq_u8(x, vdupq_n_u8(0x80)))); |
| 160 | + } |
| 161 | + return v; |
| 162 | +} |
| 163 | + |
| 164 | +/* Pack 16 6-bit values (4 quads) into 12 bytes and store exactly 12. */ |
| 165 | +static inline void |
| 166 | +b64neon_pack16(uint8x16_t v, unsigned char *out) |
| 167 | +{ |
| 168 | + static const uint8_t shuf[16] = { |
| 169 | + 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, 0xff, 0xff, 0xff, 0xff, |
| 170 | + }; |
| 171 | + uint16x8_t w16 = vreinterpretq_u16_u8(v); |
| 172 | + w16 = vandq_u16(vsliq_n_u16(vshrq_n_u16(w16, 8), w16, 6), |
| 173 | + vdupq_n_u16(0x0fff)); |
| 174 | + uint32x4_t w32 = vreinterpretq_u32_u16(w16); |
| 175 | + w32 = vsliq_n_u32(vshrq_n_u32(w32, 16), w32, 12); |
| 176 | + uint8x16_t r = vqtbl1q_u8(vreinterpretq_u8_u32(w32), vld1q_u8(shuf)); |
| 177 | + vst1_u8(out, vget_low_u8(r)); |
| 178 | + vst1q_lane_u32((uint32_t *)(void *)(out + 8), vreinterpretq_u32_u8(r), 2); |
| 179 | +} |
| 180 | + |
| 181 | +#define B64NEON_TRANSLATE(x, errp) \ |
| 182 | + (is_std ? b64neon_translate_std((x), (errp)) \ |
| 183 | + : b64neon_translate_table((x), (errp), &T)) |
| 184 | + |
| 185 | +/* Decode blocks of 64, then 16, characters. Stops before the first block |
| 186 | + * containing a character the table rejects; the scalar loop then finds the |
| 187 | + * exact position. Returns the number of characters consumed, a multiple |
| 188 | + * of 4. is_std must be a constant so that each caller is specialized. */ |
| 189 | +static inline Py_ssize_t |
| 190 | +b64neon_decode_blocks(const unsigned char *in, Py_ssize_t in_len, |
| 191 | + unsigned char *out, const unsigned char *table, |
| 192 | + const int is_std) |
| 193 | +{ |
| 194 | + const unsigned char *const start = in; |
| 195 | + b64neon_table T = {0}; |
| 196 | + |
| 197 | + if (!is_std) { |
| 198 | + for (int i = 0; i < 4; i++) { |
| 199 | + T.t[i] = vld1q_u8_x4(table + 64 * i); |
| 200 | + } |
| 201 | + for (int i = 0x80; i < 0x100; i++) { |
| 202 | + if (!(table[i] & 0xc0)) { |
| 203 | + T.high = 1; |
| 204 | + break; |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + for (; in_len >= 64; in += 64, out += 48, in_len -= 64) { |
| 210 | + uint8x16x4_t s = vld4q_u8(in); |
| 211 | + uint8x16_t e0 = vdupq_n_u8(0), e1 = vdupq_n_u8(0); |
| 212 | + uint8x16_t a = B64NEON_TRANSLATE(s.val[0], &e0); |
| 213 | + uint8x16_t b = B64NEON_TRANSLATE(s.val[1], &e1); |
| 214 | + uint8x16_t c = B64NEON_TRANSLATE(s.val[2], &e0); |
| 215 | + uint8x16_t q = B64NEON_TRANSLATE(s.val[3], &e1); |
| 216 | + uint8_t e = vmaxvq_u8(vorrq_u8(e0, e1)); |
| 217 | + if (is_std ? e != 0 : e > 63) { |
| 218 | + break; |
| 219 | + } |
| 220 | + uint8x16x3_t d; |
| 221 | + d.val[0] = vsliq_n_u8(vshrq_n_u8(b, 4), a, 2); |
| 222 | + d.val[1] = vsliq_n_u8(vshrq_n_u8(c, 2), b, 4); |
| 223 | + d.val[2] = vsliq_n_u8(q, c, 6); |
| 224 | + vst3q_u8(out, d); |
| 225 | + } |
| 226 | + for (; in_len >= 16; in += 16, out += 12, in_len -= 16) { |
| 227 | + uint8x16_t e0 = vdupq_n_u8(0); |
| 228 | + uint8x16_t v = B64NEON_TRANSLATE(vld1q_u8(in), &e0); |
| 229 | + uint8_t e = vmaxvq_u8(e0); |
| 230 | + if (is_std ? e != 0 : e > 63) { |
| 231 | + break; |
| 232 | + } |
| 233 | + b64neon_pack16(v, out); |
| 234 | + } |
| 235 | + return in - start; |
| 236 | +} |
| 237 | + |
| 238 | +#undef B64NEON_TRANSLATE |
| 239 | + |
| 240 | +static inline Py_ssize_t |
| 241 | +base64_decode_neon(const unsigned char *in, Py_ssize_t in_len, |
| 242 | + unsigned char *out, const unsigned char *table, |
| 243 | + int is_std) |
| 244 | +{ |
| 245 | + if (is_std) { |
| 246 | + return b64neon_decode_blocks(in, in_len, out, table, 1); |
| 247 | + } |
| 248 | + return b64neon_decode_blocks(in, in_len, out, table, 0); |
| 249 | +} |
| 250 | + |
| 251 | +#endif /* __aarch64__ || _M_ARM64 */ |
| 252 | +#endif /* BINASCII_NEON_H */ |
0 commit comments