Botan 3.14.1
Crypto and TLS for C&
base64_avx512.cpp
Go to the documentation of this file.
1/*
2* (C) 2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/base64_avx512.h>
8
9#include <botan/internal/isa_extn.h>
10#include <algorithm>
11#include <array>
12#include <immintrin.h>
13
14namespace Botan {
15
16/*
17* The encoding and decoding kernels follow the AVX-512VBMI algorithms of
18* W. Mula and D. Lemire, "Base64 encoding and decoding at almost the
19* speed of a memory copy" (https://arxiv.org/abs/1910.05109)
20*/
21
22BOTAN_FN_ISA_AVX512 size_t base64_encode_avx512(char out[], const uint8_t in[], size_t length) {
23 // Expands each 3 byte input group g to the 4 bytes [b1,b0,b2,b1], placing
24 // the group in a 32-bit word where vpmultishiftqb can reach each 6-bit field
25 alignas(64) constexpr auto B64_ENC_SHUFFLE = []() {
26 std::array<uint8_t, 64> idx = {};
27 for(size_t g = 0; g != 16; ++g) {
28 idx[4 * g + 0] = static_cast<uint8_t>(3 * g + 1);
29 idx[4 * g + 1] = static_cast<uint8_t>(3 * g + 0);
30 idx[4 * g + 2] = static_cast<uint8_t>(3 * g + 2);
31 idx[4 * g + 3] = static_cast<uint8_t>(3 * g + 1);
32 }
33 return idx;
34 }();
35
36 alignas(64) constexpr char B64_ENC_TABLE[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
37 constexpr __mmask64 LOW_48 = 0x0000FFFFFFFFFFFF;
38
39 const __m512i shuffle = _mm512_load_si512(B64_ENC_SHUFFLE.data());
40 const __m512i lookup = _mm512_load_si512(B64_ENC_TABLE);
41 // Byte i of each output qword takes 8 bits starting at bit multishift[i]
42 const __m512i multishift = _mm512_set1_epi64(0x3036242A1016040A);
43
44 size_t consumed = 0;
45
46 while(length - consumed >= 48) {
47 const __m512i x = _mm512_maskz_loadu_epi8(LOW_48, in + consumed);
48
49 const __m512i expanded = _mm512_permutexvar_epi8(shuffle, x);
50 // The two high junk bits of each extracted byte are ignored by vpermb
51 const __m512i indices = _mm512_multishift_epi64_epi8(multishift, expanded);
52 const __m512i chars = _mm512_permutexvar_epi8(indices, lookup);
53
54 _mm512_storeu_si512(out, chars);
55
56 out += 64;
57 consumed += 48;
58 }
59
60 return consumed;
61}
62
63BOTAN_FN_ISA_AVX512_POPCNT size_t base64_strip_ws_avx512(uint8_t out[], const uint8_t in[], size_t length) {
64 size_t written = 0;
65 size_t pos = 0;
66
67 while(pos < length) {
68 const size_t todo = std::min<size_t>(64, length - pos);
69 const __mmask64 valid = (todo == 64) ? ~__mmask64(0) : ((__mmask64(1) << todo) - 1);
70
71 const __m512i x = _mm512_maskz_loadu_epi8(valid, in + pos);
72
73 const __mmask64 ws =
74 _mm512_cmpeq_epi8_mask(x, _mm512_set1_epi8(' ')) | _mm512_cmpeq_epi8_mask(x, _mm512_set1_epi8('\n')) |
75 _mm512_cmpeq_epi8_mask(x, _mm512_set1_epi8('\t')) | _mm512_cmpeq_epi8_mask(x, _mm512_set1_epi8('\r'));
76
77 const __mmask64 keep = valid & ~ws;
78 _mm512_mask_compressstoreu_epi8(out + written, keep, x);
79 written += static_cast<size_t>(_mm_popcnt_u64(keep));
80 pos += todo;
81 }
82
83 return written;
84}
85
86BOTAN_FN_ISA_AVX512_POPCNT size_t base64_decode_avx512(uint8_t out[], const uint8_t in[], size_t length) {
87 // Maps the low 7 bits of an ASCII character to its base64 value; any
88 // character that is not one of the 64 data characters maps to 0x80
89 alignas(64) constexpr auto B64_DEC_TABLE = []() {
90 std::array<uint8_t, 128> tbl = {};
91 for(auto& b : tbl) {
92 b = 0x80;
93 }
94 for(size_t i = 0; i != 26; ++i) {
95 tbl['A' + i] = static_cast<uint8_t>(i);
96 tbl['a' + i] = static_cast<uint8_t>(26 + i);
97 }
98 for(size_t i = 0; i != 10; ++i) {
99 tbl['0' + i] = static_cast<uint8_t>(52 + i);
100 }
101 tbl['+'] = 62;
102 tbl['/'] = 63;
103 return tbl;
104 }();
105
106 // After the multiply-adds, dword g holds the three decoded bytes of group
107 // g as its bytes [2,1,0]; gather them into 48 contiguous output bytes
108 alignas(64) constexpr auto B64_DEC_PACK = []() {
109 std::array<uint8_t, 64> idx = {};
110 for(size_t g = 0; g != 16; ++g) {
111 idx[3 * g + 0] = static_cast<uint8_t>(4 * g + 2);
112 idx[3 * g + 1] = static_cast<uint8_t>(4 * g + 1);
113 idx[3 * g + 2] = static_cast<uint8_t>(4 * g + 0);
114 }
115 return idx;
116 }();
117
118 constexpr __mmask64 LOW_48 = 0x0000FFFFFFFFFFFF;
119
120 const __m512i lut_lo = _mm512_load_si512(B64_DEC_TABLE.data());
121 const __m512i lut_hi = _mm512_load_si512(&B64_DEC_TABLE[64]);
122 const __m512i pack_shuffle = _mm512_load_si512(B64_DEC_PACK.data());
123
124 // Merge the 6 bit values a,b,c,d of each group first into the pair
125 // (a*64 + b, c*64 + d) and then into a*2^18 + b*2^12 + c*2^6 + d
126 const __m512i pack_mul1 = _mm512_set1_epi16(0x0140);
127 const __m512i pack_mul2 = _mm512_set1_epi32(0x00011000);
128
129 size_t consumed = 0;
130
131 while(length - consumed > 64) {
132 const __m512i x = _mm512_loadu_si512(in + consumed);
133
134 const __m512i values = _mm512_permutex2var_epi8(lut_lo, x, lut_hi);
135
136 // Padding, whitespace, invalid, or non-ASCII input sets a high bit
137 if(_mm512_movepi8_mask(_mm512_or_si512(x, values)) != 0) {
138 break;
139 }
140
141 const __m512i merged16 = _mm512_maddubs_epi16(values, pack_mul1);
142 const __m512i merged32 = _mm512_madd_epi16(merged16, pack_mul2);
143 const __m512i packed = _mm512_permutexvar_epi8(pack_shuffle, merged32);
144
145 _mm512_mask_storeu_epi8(out, LOW_48, packed);
146
147 out += 48;
148 consumed += 64;
149 }
150
151 return consumed;
152}
153
154} // namespace Botan
BOTAN_FN_ISA_AVX512 size_t base64_encode_avx512(char out[], const uint8_t in[], size_t length)
BOTAN_FN_ISA_AVX512_POPCNT size_t base64_strip_ws_avx512(uint8_t out[], const uint8_t in[], size_t length)
BOTAN_FN_ISA_AVX512_POPCNT size_t base64_decode_avx512(uint8_t out[], const uint8_t in[], size_t length)