base-d 3.0.34

Universal base encoder: Encode binary data to 33+ dictionaries including RFC standards, hieroglyphs, emoji, and more
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Benchmarking utilities for comparing encoding paths.
//!
//! This module exposes internal encoding paths for performance comparison:
//! - Scalar: Pure Rust, no SIMD
//! - LUT: SIMD with runtime lookup tables
//! - Specialized: Hardcoded SIMD for known dictionaries
//!
//! # Example
//!
//! ```ignore
//! use base_d::bench::{EncodingPath, encode_with_path, detect_available_paths};
//!
//! let dict = get_dictionary("base64");
//! let paths = detect_available_paths(&dict);
//!
//! for path in paths {
//!     let result = encode_with_path(data, &dict, path);
//! }
//! ```

use crate::EncodingMode;
use crate::core::dictionary::Dictionary;
use crate::encoders::algorithms::{DecodeError, byte_range, radix};

#[cfg(all(feature = "simd", any(target_arch = "x86_64", target_arch = "aarch64")))]
use crate::simd;

/// Available encoding paths for benchmarking.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EncodingPath {
    /// Pure scalar implementation (no SIMD)
    Scalar,
    /// SIMD with runtime LUT construction
    Lut,
    /// Hardcoded SIMD for known RFC dictionaries
    Specialized,
}

impl std::fmt::Display for EncodingPath {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EncodingPath::Scalar => write!(f, "Scalar"),
            EncodingPath::Lut => write!(f, "LUT"),
            EncodingPath::Specialized => write!(f, "Specialized"),
        }
    }
}

/// Platform capabilities for SIMD.
#[derive(Debug, Clone)]
pub struct PlatformInfo {
    pub arch: &'static str,
    pub simd_features: Vec<&'static str>,
}

impl PlatformInfo {
    /// Detect current platform capabilities.
    pub fn detect() -> Self {
        let arch = std::env::consts::ARCH;
        let mut simd_features = Vec::new();

        #[cfg(target_arch = "x86_64")]
        {
            if is_x86_feature_detected!("avx512vbmi") {
                simd_features.push("AVX-512 VBMI");
            }
            if is_x86_feature_detected!("avx2") {
                simd_features.push("AVX2");
            }
            if is_x86_feature_detected!("ssse3") {
                simd_features.push("SSSE3");
            }
        }

        #[cfg(target_arch = "aarch64")]
        {
            // NEON is always available on aarch64
            simd_features.push("NEON");
        }

        PlatformInfo {
            arch,
            simd_features,
        }
    }

    /// Format as display string.
    pub fn display(&self) -> String {
        if self.simd_features.is_empty() {
            self.arch.to_string()
        } else {
            format!("{} ({})", self.arch, self.simd_features.join(", "))
        }
    }
}

/// Information about a dictionary's benchmark capabilities.
#[derive(Debug, Clone)]
pub struct DictionaryBenchInfo {
    pub name: String,
    pub base: usize,
    pub mode: EncodingMode,
    pub available_paths: Vec<EncodingPath>,
    pub supports_streaming: bool,
}

/// Detect which encoding paths are available for a dictionary.
pub fn detect_available_paths(dict: &Dictionary) -> Vec<EncodingPath> {
    let mut paths = vec![EncodingPath::Scalar]; // Scalar always available

    #[cfg(feature = "simd")]
    {
        let base = dict.base();
        let mode = dict.mode();

        // Check if LUT path is available (power-of-2 base, ASCII chars)
        if base.is_power_of_two() && base <= 256 {
            // Check if all chars are ASCII
            let all_ascii = (0..base).all(|i| {
                dict.encode_digit(i)
                    .map(|c| (c as u32) < 128)
                    .unwrap_or(false)
            });

            if all_ascii && matches!(mode, EncodingMode::Chunked) {
                paths.push(EncodingPath::Lut);
            }
        }

        // Check if specialized path is available
        if is_specialized_available(dict) {
            paths.push(EncodingPath::Specialized);
        }
    }

    paths
}

/// Check if a specialized SIMD path exists for this dictionary.
#[cfg(feature = "simd")]
fn is_specialized_available(dict: &Dictionary) -> bool {
    use crate::simd::variants::{identify_base32_variant, identify_base64_variant};

    let base = dict.base();

    match base {
        16 => {
            // Check if it's standard hex (uppercase or lowercase)
            let first_char = dict.encode_digit(10); // 'A' or 'a' position
            matches!(first_char, Some('A') | Some('a'))
        }
        32 => identify_base32_variant(dict).is_some(),
        64 => identify_base64_variant(dict).is_some(),
        256 => matches!(dict.mode(), EncodingMode::Chunked | EncodingMode::ByteRange),
        _ => false,
    }
}

#[cfg(not(feature = "simd"))]
fn is_specialized_available(_dict: &Dictionary) -> bool {
    false
}

/// Encode using a specific path (for benchmarking).
///
/// Returns `None` if the path is not available for this dictionary.
pub fn encode_with_path(data: &[u8], dict: &Dictionary, path: EncodingPath) -> Option<String> {
    match path {
        EncodingPath::Scalar => Some(encode_scalar(data, dict)),
        EncodingPath::Lut => encode_lut(data, dict),
        EncodingPath::Specialized => encode_specialized(data, dict),
    }
}

/// Decode using a specific path (for benchmarking).
///
/// Returns `None` if the path is not available for this dictionary.
pub fn decode_with_path(encoded: &str, dict: &Dictionary, path: EncodingPath) -> Option<Vec<u8>> {
    match path {
        EncodingPath::Scalar => decode_scalar(encoded, dict).ok(),
        EncodingPath::Lut => decode_lut(encoded, dict),
        EncodingPath::Specialized => decode_specialized(encoded, dict),
    }
}

/// Pure scalar encoding (no SIMD).
fn encode_scalar(data: &[u8], dict: &Dictionary) -> String {
    match dict.mode() {
        EncodingMode::Radix => radix::encode(data, dict),
        EncodingMode::Chunked => encode_chunked_scalar(data, dict),
        EncodingMode::ByteRange => byte_range::encode_byte_range(data, dict).expect(
            "ByteRange encode failed: dictionary should have been validated at construction time",
        ),
    }
}

/// Pure scalar decoding (no SIMD).
fn decode_scalar(encoded: &str, dict: &Dictionary) -> Result<Vec<u8>, crate::DecodeError> {
    match dict.mode() {
        EncodingMode::Radix => radix::decode(encoded, dict),
        EncodingMode::Chunked => decode_chunked_scalar(encoded, dict),
        EncodingMode::ByteRange => byte_range::decode_byte_range(encoded, dict),
    }
}

/// Scalar chunked encoding (bypasses SIMD).
fn encode_chunked_scalar(data: &[u8], dict: &Dictionary) -> String {
    let base = dict.base();
    let bits_per_char = (base as f64).log2() as usize;

    if bits_per_char == 0 || base & (base - 1) != 0 {
        // Non-power-of-2, fall back to radix
        return radix::encode(data, dict);
    }

    let mut result = String::new();
    let mut bit_buffer: u64 = 0;
    let mut bits_in_buffer = 0;

    for &byte in data {
        bit_buffer = (bit_buffer << 8) | byte as u64;
        bits_in_buffer += 8;

        while bits_in_buffer >= bits_per_char {
            bits_in_buffer -= bits_per_char;
            let index = ((bit_buffer >> bits_in_buffer) & ((1 << bits_per_char) - 1)) as usize;
            if let Some(ch) = dict.encode_digit(index) {
                result.push(ch);
            }
        }
    }

    // Handle remaining bits
    if bits_in_buffer > 0 {
        let index = ((bit_buffer << (bits_per_char - bits_in_buffer)) & ((1 << bits_per_char) - 1))
            as usize;
        if let Some(ch) = dict.encode_digit(index) {
            result.push(ch);
        }
    }

    // Add padding if needed
    if let Some(pad) = dict.padding() {
        let output_block_size = match bits_per_char {
            6 => 4, // base64
            5 => 8, // base32
            4 => 2, // base16
            _ => 1,
        };
        while !result.len().is_multiple_of(output_block_size) {
            result.push(pad);
        }
    }

    result
}

/// Scalar chunked decoding (bypasses SIMD).
fn decode_chunked_scalar(encoded: &str, dict: &Dictionary) -> Result<Vec<u8>, crate::DecodeError> {
    let base = dict.base();
    let bits_per_char = (base as f64).log2() as usize;

    if bits_per_char == 0 || base & (base - 1) != 0 {
        return radix::decode(encoded, dict);
    }

    // Strip padding
    let padding = dict.padding();
    let encoded = if let Some(pad) = padding {
        encoded.trim_end_matches(pad)
    } else {
        encoded
    };

    let mut result = Vec::new();
    let mut bit_buffer: u64 = 0;
    let mut bits_in_buffer = 0;

    for ch in encoded.chars() {
        let value = dict.decode_char(ch).ok_or(DecodeError::InvalidCharacter {
            char: ch,
            position: 0,
            input: String::new(),
            valid_chars: String::new(),
        })?;
        bit_buffer = (bit_buffer << bits_per_char) | value as u64;
        bits_in_buffer += bits_per_char;

        while bits_in_buffer >= 8 {
            bits_in_buffer -= 8;
            result.push((bit_buffer >> bits_in_buffer) as u8);
        }
    }

    Ok(result)
}

/// LUT-based SIMD encoding (uses runtime LUT construction, not hardcoded tables).
#[cfg(all(feature = "simd", any(target_arch = "x86_64", target_arch = "aarch64")))]
fn encode_lut(data: &[u8], dict: &Dictionary) -> Option<String> {
    let base = dict.base();

    // Skip specialized paths - force LUT-based codecs only
    // 1. Try GenericSimdCodec for sequential power-of-2 dictionaries
    if let Some(codec) = simd::GenericSimdCodec::from_dictionary(dict) {
        return codec.encode(data, dict);
    }

    // 2. Try GappedSequentialCodec for near-sequential dictionaries
    if let Some(codec) = simd::GappedSequentialCodec::from_dictionary(dict) {
        return codec.encode(data, dict);
    }

    // 3. Try SmallLutCodec for small arbitrary dictionaries (≤16 chars)
    if base <= 16
        && base.is_power_of_two()
        && let Some(codec) = simd::SmallLutCodec::from_dictionary(dict)
    {
        return codec.encode(data, dict);
    }

    // 4. Try Base64LutCodec for larger arbitrary dictionaries (17-64 chars)
    if (17..=64).contains(&base)
        && base.is_power_of_two()
        && let Some(codec) = simd::Base64LutCodec::from_dictionary(dict)
    {
        return codec.encode(data, dict);
    }

    None
}

#[cfg(not(all(feature = "simd", any(target_arch = "x86_64", target_arch = "aarch64"))))]
fn encode_lut(_data: &[u8], _dict: &Dictionary) -> Option<String> {
    None
}

/// LUT-based SIMD decoding (uses runtime LUT construction, not hardcoded tables).
#[cfg(all(feature = "simd", any(target_arch = "x86_64", target_arch = "aarch64")))]
fn decode_lut(encoded: &str, dict: &Dictionary) -> Option<Vec<u8>> {
    let base = dict.base();

    // Skip specialized paths - force LUT-based codecs only
    // 1. Try GenericSimdCodec for sequential power-of-2 dictionaries
    if let Some(codec) = simd::GenericSimdCodec::from_dictionary(dict) {
        return codec.decode(encoded, dict);
    }

    // 2. Try GappedSequentialCodec for near-sequential dictionaries
    if let Some(codec) = simd::GappedSequentialCodec::from_dictionary(dict) {
        return codec.decode(encoded, dict);
    }

    // 3. Try SmallLutCodec for small arbitrary dictionaries (≤16 chars)
    if base <= 16
        && base.is_power_of_two()
        && let Some(codec) = simd::SmallLutCodec::from_dictionary(dict)
    {
        return codec.decode(encoded, dict);
    }

    // 4. Try Base64LutCodec for larger arbitrary dictionaries (17-64 chars)
    if (17..=64).contains(&base)
        && base.is_power_of_two()
        && let Some(codec) = simd::Base64LutCodec::from_dictionary(dict)
    {
        return codec.decode(encoded, dict);
    }

    None
}

#[cfg(not(all(feature = "simd", any(target_arch = "x86_64", target_arch = "aarch64"))))]
fn decode_lut(_encoded: &str, _dict: &Dictionary) -> Option<Vec<u8>> {
    None
}

/// Specialized SIMD encoding (for known RFC dictionaries).
#[cfg(all(feature = "simd", target_arch = "x86_64"))]
fn encode_specialized(data: &[u8], dict: &Dictionary) -> Option<String> {
    use crate::simd::{
        encode_base16_simd, encode_base32_simd, encode_base64_simd, encode_base256_simd,
    };

    match dict.base() {
        16 => encode_base16_simd(data, dict),
        32 => encode_base32_simd(data, dict),
        64 => encode_base64_simd(data, dict),
        256 => encode_base256_simd(data, dict),
        _ => None,
    }
}

#[cfg(all(feature = "simd", not(target_arch = "x86_64")))]
fn encode_specialized(_data: &[u8], _dict: &Dictionary) -> Option<String> {
    // ARM doesn't have specialized paths yet (uses LUT)
    None
}

#[cfg(not(feature = "simd"))]
fn encode_specialized(_data: &[u8], _dict: &Dictionary) -> Option<String> {
    None
}

/// Specialized SIMD decoding (for known RFC dictionaries).
#[cfg(all(feature = "simd", target_arch = "x86_64"))]
fn decode_specialized(encoded: &str, dict: &Dictionary) -> Option<Vec<u8>> {
    use crate::simd::{
        decode_base16_simd, decode_base32_simd, decode_base64_simd, decode_base256_simd,
    };

    match dict.base() {
        16 => decode_base16_simd(encoded, dict),
        32 => decode_base32_simd(encoded, dict),
        64 => decode_base64_simd(encoded, dict),
        256 => decode_base256_simd(encoded, dict),
        _ => None,
    }
}

#[cfg(all(feature = "simd", not(target_arch = "x86_64")))]
fn decode_specialized(_encoded: &str, _dict: &Dictionary) -> Option<Vec<u8>> {
    None
}

#[cfg(not(feature = "simd"))]
fn decode_specialized(_encoded: &str, _dict: &Dictionary) -> Option<Vec<u8>> {
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DictionaryRegistry;

    fn get_test_dict(name: &str) -> Dictionary {
        let config = DictionaryRegistry::load_default().unwrap();
        let dict_config = config.get_dictionary(name).unwrap();
        let chars: Vec<char> = dict_config.effective_chars().unwrap().chars().collect();
        let padding = dict_config.padding.as_ref().and_then(|s| s.chars().next());
        let mut builder = Dictionary::builder()
            .chars(chars)
            .mode(dict_config.effective_mode());
        if let Some(p) = padding {
            builder = builder.padding(p);
        }
        builder.build().unwrap()
    }

    #[test]
    fn test_platform_detection() {
        let info = PlatformInfo::detect();
        assert!(!info.arch.is_empty());
        println!("Platform: {}", info.display());
    }

    #[test]
    fn test_path_detection_base64() {
        let dict = get_test_dict("base64");
        let paths = detect_available_paths(&dict);

        assert!(paths.contains(&EncodingPath::Scalar));
        #[cfg(feature = "simd")]
        {
            assert!(
                paths.contains(&EncodingPath::Lut) || paths.contains(&EncodingPath::Specialized)
            );
        }
    }

    #[test]
    fn test_scalar_round_trip() {
        let dict = get_test_dict("base64");
        let data = b"Hello, World!";

        let encoded = encode_with_path(data, &dict, EncodingPath::Scalar).unwrap();
        let decoded = decode_with_path(&encoded, &dict, EncodingPath::Scalar).unwrap();

        assert_eq!(&decoded[..], &data[..]);
    }

    #[test]
    fn test_paths_produce_same_output() {
        let dict = get_test_dict("base64");
        let data = b"The quick brown fox jumps over the lazy dog";
        let paths = detect_available_paths(&dict);

        let mut results: Vec<(EncodingPath, String)> = Vec::new();
        for path in &paths {
            if let Some(encoded) = encode_with_path(data, &dict, *path) {
                results.push((*path, encoded));
            }
        }

        // Compare Scalar with others, stripping padding for fair comparison
        // (LUT codecs don't add padding, specialized RFC implementations do)
        let scalar_result = results.iter().find(|(p, _)| *p == EncodingPath::Scalar);
        if let Some((_, scalar_encoded)) = scalar_result {
            let scalar_stripped = scalar_encoded.trim_end_matches('=');
            for (path, encoded) in &results {
                if *path != EncodingPath::Scalar {
                    let stripped = encoded.trim_end_matches('=');
                    assert_eq!(
                        scalar_stripped, stripped,
                        "{:?} output differs from Scalar (ignoring padding)",
                        path
                    );
                }
            }
        }
    }
}