base64-ng 2.0.0

no_std-first Base64 encoding and decoding with strict APIs and a security-heavy release process
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#![allow(unsafe_code)]

//! SIMD admission boundary.
//!
//! This module is the only source module allowed to lower the crate-level
//! `unsafe_code` lint. Keep all future architecture-specific intrinsics behind
//! this boundary, with a local safety explanation for every unsafe block.
//!
//! The module admits `x86`/`x86_64` AVX-512 VBMI, AVX2, SSSE3/SSE4.1,
//! and little-endian `aarch64` NEON encode backends for Standard and
//! URL-safe alphabet families. It also admits `x86`/`x86_64` AVX-512 VBMI,
//! AVX2, and SSSE3/SSE4.1 strict decode plus little-endian `aarch64` NEON
//! strict decode for Standard and URL-safe alphabet families through the
//! separate decode backend boundary. Narrow wasm `simd128` encode and strict
//! decode are admitted for the documented wasm runtime profile. Wrapped decode
//! may enter admitted strict decode only after scalar line-profile validation
//! and line-ending compaction. Legacy whitespace decode may enter admitted
//! strict decode only after scalar whitespace compaction. Strict in-place
//! encode and decode may enter admitted backends only after stack staging.
//! Exact-profile Linux RISC-V dispatch may use RVV 1.0 on the measured
//! `SpacemiT` X60 profile. Non-Standard-family custom alphabets, other RISC-V
//! profiles, big-endian `AArch64`, CT secret decode, and every other unsupported
//! SIMD surface still execute through the scalar implementation.
//! `Alphabet::ENCODE` is authoritative for ordinary scalar and
//! SIMD decode; overridable `Alphabet::decode` code is never an admission input.
//! A `no_std` build may execute an admitted backend only when complete static
//! target-feature evidence and the atomic backend-health latch are available.
//!
//! The x86 AVX-512 VBMI, AVX2, SSSE3/SSE4.1, and `AArch64` NEON fixed-block
//! encoders are reachable after runtime CPU probing on `std`, or complete
//! compile-time target-feature checks on `no_std`. The wasm `simd128`
//! fixed-block implementation is reachable only for the admitted wasm runtime
//! profile and remains scoped out of broader browser/JIT claims.
//! Commit 33 also carries an internal, non-dispatchable SVE encode/decode
//! candidate for QEMU and generated-code evidence. Public `AArch64` dispatch
//! remains admitted NEON or scalar until the native SVE admission contract is
//! satisfied.

mod static_token;
pub use static_token::StaticBackendToken;

#[cfg(all(
    feature = "simd",
    target_arch = "aarch64",
    target_endian = "little",
    base64_ng_sve_candidate
))]
mod sve;
#[cfg(all(
    feature = "std",
    feature = "simd",
    test,
    target_arch = "aarch64",
    target_endian = "little",
    target_os = "linux",
    base64_ng_sve_candidate
))]
mod sve_tests;

#[cfg(all(feature = "simd", target_arch = "riscv64"))]
mod rvv;
#[cfg(all(feature = "simd", target_arch = "riscv64", base64_ng_perf_evidence))]
pub(crate) use rvv::candidate_available as rvv_candidate_available;
#[cfg(all(feature = "simd", target_arch = "riscv64"))]
pub(crate) use rvv::{
    available as rvv_available, decode_slice as decode_slice_rvv, encode_slice as encode_slice_rvv,
    supports_alphabet as rvv_supports_alphabet,
};
#[cfg(all(
    feature = "std",
    feature = "simd",
    test,
    target_arch = "riscv64",
    base64_ng_rvv_candidate
))]
mod rvv_tests;

#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
mod neon;
#[cfg(all(test, target_arch = "aarch64", target_endian = "little"))]
pub(in crate::simd) use neon::decode_16_bytes_neon;
#[cfg(all(feature = "simd", target_arch = "aarch64", target_endian = "little"))]
pub(crate) use neon::decode_slice_neon;
#[cfg(all(
    test,
    any(
        target_arch = "aarch64",
        all(target_arch = "arm", target_feature = "neon")
    )
))]
pub(in crate::simd) use neon::encode_12_bytes_neon;
#[cfg(all(feature = "simd", target_arch = "aarch64", target_endian = "little"))]
pub(crate) use neon::encode_slice_neon;
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
pub(crate) use neon::neon_available;
#[cfg(all(feature = "simd", target_arch = "aarch64", target_endian = "little"))]
pub(crate) use neon::neon_supports_alphabet;
#[cfg(all(feature = "simd", target_arch = "aarch64", target_endian = "little"))]
pub(crate) use neon::neon_supports_decode_alphabet;
#[cfg(any(
    all(test, any(target_arch = "x86", target_arch = "x86_64")),
    all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64"))
))]
mod x86;
#[cfg(all(
    feature = "std",
    test,
    any(target_arch = "x86", target_arch = "x86_64")
))]
pub(super) use x86::{
    decode_16_bytes_ssse3_sse41, decode_32_bytes_avx2, decode_64_bytes_avx512,
    encode_12_bytes_ssse3_sse41, encode_24_bytes_avx2, encode_48_bytes_avx512,
};
#[cfg(all(
    feature = "std",
    test,
    target_arch = "aarch64",
    target_endian = "little"
))]
mod neon_decode_tests;
#[cfg(all(
    feature = "std",
    test,
    target_arch = "aarch64",
    target_endian = "little"
))]
mod neon_direct_tests;
#[cfg(all(target_arch = "wasm32", any(test, feature = "simd")))]
mod wasm;
#[cfg(all(
    feature = "std",
    test,
    any(target_arch = "x86", target_arch = "x86_64")
))]
mod x86_decode_direct_tests;
#[cfg(all(
    feature = "std",
    test,
    any(target_arch = "x86", target_arch = "x86_64")
))]
mod x86_decode_tests;
#[cfg(all(
    feature = "std",
    test,
    any(target_arch = "x86", target_arch = "x86_64")
))]
mod x86_encode_tests;

/// Backend currently allowed to execute.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ActiveBackend {
    /// The audited scalar implementation.
    Scalar,
    /// `x86`/`x86_64` AVX-512 VBMI encode backend.
    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
    Avx512Vbmi,
    /// `x86`/`x86_64` AVX2 encode backend.
    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
    Avx2,
    /// `x86`/`x86_64` SSSE3/SSE4.1 encode backend.
    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
    Ssse3Sse41,
    /// Little-endian `aarch64` NEON encode backend.
    #[cfg(all(feature = "simd", target_arch = "aarch64", target_endian = "little"))]
    Neon,
    /// wasm32 `simd128` fixed-block encode backend.
    #[cfg(all(feature = "simd", target_arch = "wasm32"))]
    WasmSimd128,
    /// Linux `SpacemiT` X60 RVV 1.0 encode backend.
    #[cfg(all(
        feature = "std",
        feature = "simd",
        target_arch = "riscv64",
        target_os = "linux"
    ))]
    Rvv,
}

/// SIMD candidate detected for the current target.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum Candidate {
    /// No supported SIMD candidate was detected.
    Scalar,
    /// `x86`/`x86_64` AVX-512 VBMI is available.
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    Avx512Vbmi,
    /// `x86`/`x86_64` AVX2 is available.
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    Avx2,
    /// `x86`/`x86_64` SSSE3/SSE4.1 is available.
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    Ssse3Sse41,
    /// ARM NEON is available.
    #[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
    Neon,
    /// SVE is visible to the non-admitted project-owned candidate build.
    #[cfg(all(target_arch = "aarch64", base64_ng_sve_candidate))]
    Sve,
    /// wasm32 `simd128` is available.
    #[cfg(target_arch = "wasm32")]
    WasmSimd128,
    /// RVV is visible to the exact production profile or candidate build.
    #[cfg(target_arch = "riscv64")]
    Rvv,
}

/// Returns the backend that is allowed to execute for this build.
#[must_use]
pub(crate) fn active_backend() -> ActiveBackend {
    #[cfg(all(feature = "std", not(target_arch = "riscv64")))]
    {
        static ACTIVE_BACKEND: std::sync::OnceLock<ActiveBackend> = std::sync::OnceLock::new();
        *ACTIVE_BACKEND.get_or_init(detect_active_backend)
    }

    #[cfg(any(not(feature = "std"), target_arch = "riscv64"))]
    {
        detect_active_backend()
    }
}

fn detect_active_backend() -> ActiveBackend {
    #[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
    {
        if avx512_vbmi_base64_available() {
            return ActiveBackend::Avx512Vbmi;
        }

        if avx2_available() {
            return ActiveBackend::Avx2;
        }

        if ssse3_sse41_available() {
            return ActiveBackend::Ssse3Sse41;
        }
    }

    #[cfg(all(feature = "simd", target_arch = "aarch64", target_endian = "little"))]
    {
        if neon_available() {
            return ActiveBackend::Neon;
        }
    }

    #[cfg(all(feature = "simd", target_arch = "wasm32"))]
    {
        if wasm_simd128_available() {
            return ActiveBackend::WasmSimd128;
        }
    }

    #[cfg(all(
        feature = "std",
        feature = "simd",
        target_arch = "riscv64",
        target_os = "linux"
    ))]
    {
        if rvv::available() {
            return ActiveBackend::Rvv;
        }
    }

    let _ = detected_candidate();
    ActiveBackend::Scalar
}

/// Returns the fastest SIMD candidate visible to this build.
///
/// Candidate detection is intentionally separate from activation. Only the
/// admitted target and operation scopes may become active; every other SIMD
/// candidate still executes through scalar code.
#[must_use]
pub(crate) fn detected_candidate() -> Candidate {
    #[cfg(all(
        feature = "simd",
        target_arch = "aarch64",
        target_endian = "little",
        base64_ng_sve_candidate
    ))]
    {
        if sve::available() {
            return Candidate::Sve;
        }
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if avx512_vbmi_base64_available() {
            return Candidate::Avx512Vbmi;
        }

        if avx2_available() {
            return Candidate::Avx2;
        }

        if ssse3_sse41_available() {
            return Candidate::Ssse3Sse41;
        }
    }

    #[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
    {
        if neon_available() {
            return Candidate::Neon;
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        if wasm_simd128_available() {
            return Candidate::WasmSimd128;
        }
    }

    #[cfg(all(feature = "simd", target_arch = "riscv64", base64_ng_rvv_candidate))]
    {
        if rvv::candidate_available() {
            return Candidate::Rvv;
        }
    }

    #[cfg(all(
        feature = "std",
        feature = "simd",
        target_arch = "riscv64",
        target_os = "linux",
        not(base64_ng_rvv_candidate)
    ))]
    {
        if rvv::available() {
            return Candidate::Rvv;
        }
    }

    Candidate::Scalar
}

#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
pub(crate) use x86::{
    avx2_decode_available, avx2_encode_available, avx2_supports_alphabet,
    avx2_supports_decode_alphabet, avx512_decode_available, avx512_supports_alphabet,
    avx512_supports_decode_alphabet, encode_slice_avx2, encode_slice_avx512,
    encode_slice_ssse3_sse41, ssse3_sse41_decode_available, ssse3_sse41_encode_available,
    ssse3_sse41_supports_alphabet, ssse3_sse41_supports_decode_alphabet,
};

#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
pub(crate) use x86::{decode_slice_avx2, decode_slice_avx512, decode_slice_ssse3_sse41};

#[cfg(all(feature = "simd", target_arch = "wasm32"))]
pub(crate) use wasm::{decode_slice_wasm_simd128, encode_slice_wasm_simd128};

#[cfg(all(feature = "simd", target_arch = "wasm32"))]
pub(crate) use wasm::{
    wasm_simd128_decode_available, wasm_simd128_supports_alphabet,
    wasm_simd128_supports_decode_alphabet,
};

#[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
fn avx512_vbmi_base64_available() -> bool {
    std::is_x86_feature_detected!("avx512f")
        && std::is_x86_feature_detected!("avx512bw")
        && std::is_x86_feature_detected!("avx512vl")
        && std::is_x86_feature_detected!("avx512vbmi")
}

#[cfg(all(not(feature = "std"), any(target_arch = "x86", target_arch = "x86_64")))]
fn avx512_vbmi_base64_available() -> bool {
    cfg!(target_feature = "avx512f")
        && cfg!(target_feature = "avx512bw")
        && cfg!(target_feature = "avx512vl")
        && cfg!(target_feature = "avx512vbmi")
}

#[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
fn avx2_available() -> bool {
    std::is_x86_feature_detected!("avx2")
}

#[cfg(all(not(feature = "std"), any(target_arch = "x86", target_arch = "x86_64")))]
fn avx2_available() -> bool {
    cfg!(target_feature = "avx2")
}

#[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
fn ssse3_sse41_available() -> bool {
    std::is_x86_feature_detected!("ssse3") && std::is_x86_feature_detected!("sse4.1")
}

#[cfg(all(not(feature = "std"), any(target_arch = "x86", target_arch = "x86_64")))]
fn ssse3_sse41_available() -> bool {
    cfg!(target_feature = "ssse3") && cfg!(target_feature = "sse4.1")
}

#[cfg(target_arch = "wasm32")]
fn wasm_simd128_available() -> bool {
    cfg!(target_feature = "simd128")
}

#[cfg(target_arch = "wasm32")]
pub(crate) const fn wasm_simd128_artifact_selected() -> bool {
    cfg!(target_feature = "simd128")
}

#[cfg(all(
    test,
    any(
        target_arch = "aarch64",
        all(feature = "std", any(target_arch = "x86", target_arch = "x86_64"))
    )
))]
mod tests;