sc_neurocore_engine 3.15.7

High-performance SIMD backend for SC-NeuroCore stochastic neuromorphic computing
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// SPDX-License-Identifier: AGPL-3.0-or-later
// Commercial license available
// © Concepts 1996–2026 Miroslav Šotek. All rights reserved.
// © Code 2020–2026 Miroslav Šotek. All rights reserved.
// ORCID: 0009-0009-3560-0851
// Contact: www.anulum.li | protoscience@anulum.li
// SC-NeuroCore — Bitstream Operations

//! # Bitstream Operations
//!
//! Core bitstream packing and logic primitives for stochastic computing.
//! Probabilities are represented as packed Bernoulli bitstreams stored in `u64` words.

use rand::{Rng, RngExt};

use crate::simd;

/// Packed bitstream tensor with original bit length metadata.
#[derive(Clone, Debug)]
pub struct BitStreamTensor {
    /// Packed words containing bitstream data.
    pub data: Vec<u64>,
    /// Original unpacked bit length.
    pub length: usize,
}

impl BitStreamTensor {
    /// Create a tensor from pre-packed words.
    pub fn from_words(data: Vec<u64>, length: usize) -> Self {
        assert!(length > 0, "bitstream length must be > 0");
        Self { data, length }
    }

    // ── HDC / VSA primitives ─────────────────────────────────────────

    /// HDC BIND: In-place XOR with another tensor.
    pub fn xor_inplace(&mut self, other: &BitStreamTensor) {
        assert_eq!(
            self.length, other.length,
            "Bitstream lengths must match for XOR."
        );
        for (a, b) in self.data.iter_mut().zip(other.data.iter()) {
            *a ^= *b;
        }
    }

    /// HDC BIND: XOR returning a new tensor.
    pub fn xor(&self, other: &BitStreamTensor) -> BitStreamTensor {
        assert_eq!(
            self.length, other.length,
            "Bitstream lengths must match for XOR."
        );
        let data = self
            .data
            .iter()
            .zip(other.data.iter())
            .map(|(&a, &b)| a ^ b)
            .collect();
        BitStreamTensor {
            data,
            length: self.length,
        }
    }

    /// HDC PERMUTE: Cyclic right rotation by `shift` bits.
    ///
    /// Rotates the entire logical bitstream, handling cross-word boundaries.
    pub fn rotate_right(&mut self, shift: usize) {
        if self.length == 0 || shift.is_multiple_of(self.length) {
            return;
        }
        let mut bits = unpack(self);
        bits.rotate_right(shift % self.length);
        *self = pack(&bits);
    }

    /// HDC SIMILARITY: Normalized Hamming distance (0.0 = identical, 1.0 = opposite).
    pub fn hamming_distance(&self, other: &BitStreamTensor) -> f32 {
        assert_eq!(
            self.length, other.length,
            "Bitstream lengths must match for Hamming distance."
        );
        let xor_count: u64 = crate::simd::fused_xor_popcount_dispatch(&self.data, &other.data);
        xor_count as f32 / self.length as f32
    }

    /// HDC BUNDLE: Majority vote across N tensors.
    ///
    /// Bit is 1 if a strict majority (> N/2) of inputs have it set.
    /// Optimized bitwise implementation using full adders.
    pub fn bundle(vectors: &[&BitStreamTensor]) -> BitStreamTensor {
        assert!(!vectors.is_empty(), "Cannot bundle zero vectors.");
        let length = vectors[0].length;
        let words = vectors[0].data.len();

        if vectors.len() == 1 {
            return vectors[0].clone();
        }

        // Bitwise majority vote for N inputs.
        // For N=3, maj(a,b,c) = (a&b) | (b&c) | (a&c)
        // For general N, we use a bit-counting approach (bitwise full adders).
        let mut data = vec![0u64; words];

        if vectors.len() == 3 {
            for (i, item) in data.iter_mut().enumerate().take(words) {
                let a = vectors[0].data[i];
                let b = vectors[1].data[i];
                let c = vectors[2].data[i];
                *item = (a & b) | (b & c) | (a & c);
            }
        } else {
            // General N: Slow fallback for now, but still per-word.
            let threshold = vectors.len() / 2;
            for (i, item) in data.iter_mut().enumerate().take(words) {
                for bit in 0..64 {
                    let mut count = 0;
                    for v in vectors {
                        if (v.data[i] >> bit) & 1 == 1 {
                            count += 1;
                        }
                    }
                    if count > threshold {
                        *item |= 1u64 << bit;
                    }
                }
            }
        }

        BitStreamTensor { data, length }
    }
}

/// Pack a `0/1` byte slice into `u64` words.
pub fn pack(bits: &[u8]) -> BitStreamTensor {
    let length = bits.len();
    let words = length.div_ceil(64);
    let mut data = vec![0_u64; words];

    for (idx, bit) in bits.iter().copied().enumerate() {
        if bit != 0 {
            data[idx / 64] |= 1_u64 << (idx % 64);
        }
    }

    BitStreamTensor { data, length }
}

/// Portable fast pack: processes 8 bytes into one output byte at a time.
pub fn pack_fast(bits: &[u8]) -> BitStreamTensor {
    let length = bits.len();
    let words = length.div_ceil(64);
    let mut data = vec![0_u64; words];

    for (word_idx, word) in data.iter_mut().enumerate() {
        let base = word_idx * 64;
        let chunk = &bits[base..std::cmp::min(base + 64, length)];

        for (byte_idx, byte_chunk) in chunk.chunks(8).enumerate() {
            let mut packed_byte: u8 = 0;
            for (bit_idx, &bit) in byte_chunk.iter().enumerate() {
                packed_byte |= u8::from(bit != 0) << bit_idx;
            }
            *word |= (packed_byte as u64) << (byte_idx * 8);
        }
    }

    BitStreamTensor { data, length }
}

/// Unpack a packed tensor back into a `0/1` byte vector.
pub fn unpack(tensor: &BitStreamTensor) -> Vec<u8> {
    let mut bits = vec![0_u8; tensor.length];

    for (idx, bit) in bits.iter_mut().enumerate().take(tensor.length) {
        let word = tensor.data[idx / 64];
        *bit = ((word >> (idx % 64)) & 1) as u8;
    }

    bits
}

/// Compute bitwise-AND between two packed tensors.
pub fn bitwise_and(a: &BitStreamTensor, b: &BitStreamTensor) -> BitStreamTensor {
    assert_eq!(
        a.length, b.length,
        "Bitstream lengths must match for bitwise AND."
    );
    assert_eq!(
        a.data.len(),
        b.data.len(),
        "Packed bitstream shapes must match for bitwise AND."
    );

    let data = a
        .data
        .iter()
        .zip(b.data.iter())
        .map(|(lhs, rhs)| lhs & rhs)
        .collect();

    BitStreamTensor {
        data,
        length: a.length,
    }
}

/// Portable SWAR popcount for a single `u64` word.
pub fn swar_popcount_word(mut x: u64) -> u64 {
    x = x.wrapping_sub((x >> 1) & 0x5555_5555_5555_5555);
    x = (x & 0x3333_3333_3333_3333) + ((x >> 2) & 0x3333_3333_3333_3333);
    x = (x + (x >> 4)) & 0x0f0f_0f0f_0f0f_0f0f;
    x.wrapping_mul(0x0101_0101_0101_0101) >> 56
}

/// Portable popcount over a packed word slice.
pub fn popcount_words_portable(data: &[u64]) -> u64 {
    data.iter().copied().map(swar_popcount_word).sum()
}

/// Popcount of all bits set in a packed tensor.
pub fn popcount(tensor: &BitStreamTensor) -> u64 {
    popcount_words_portable(&tensor.data)
}

/// Generate an unpacked Bernoulli bitstream from a probability.
///
/// Values are clamped into `[0, 1]` before sampling.
pub fn bernoulli_stream<R: Rng + ?Sized>(prob: f64, length: usize, rng: &mut R) -> Vec<u8> {
    let p = prob.clamp(0.0, 1.0);
    let mut out = vec![0_u8; length];
    for bit in &mut out {
        *bit = if rng.random::<f64>() < p { 1 } else { 0 };
    }
    out
}

/// Generate a packed Bernoulli bitstream directly into `u64` words.
///
/// This is bit-identical to `pack(&bernoulli_stream(...)).data` for the same
/// RNG state while avoiding the intermediate `Vec<u8>` allocation.
pub fn bernoulli_packed<R: Rng + ?Sized>(prob: f64, length: usize, rng: &mut R) -> Vec<u64> {
    let p = prob.clamp(0.0, 1.0);
    let words = length.div_ceil(64);
    let mut data = vec![0_u64; words];
    for (word_idx, word) in data.iter_mut().enumerate() {
        let bits_in_word = std::cmp::min(64, length.saturating_sub(word_idx * 64));
        for bit in 0..bits_in_word {
            if rng.random::<f64>() < p {
                *word |= 1_u64 << bit;
            }
        }
    }
    data
}

/// Fast packed Bernoulli generation using byte-threshold comparison.
///
/// Instead of generating one f64 (8 bytes) per bit and comparing,
/// this generates one u8 (1 byte) per bit via `rng.fill()` and
/// compares against a u8 threshold = `(prob * 256.0) as u8`.
///
/// This uses 8x less RNG bandwidth than `bernoulli_packed` at the
/// cost of 8-bit probability resolution (1/256 granularity).
/// For bitstream lengths >= 256, the statistical difference is
/// negligible compared to inherent sampling noise.
///
/// The output is NOT bit-identical to `bernoulli_packed` for the
/// same RNG state.
pub fn bernoulli_packed_fast<R: Rng + ?Sized>(prob: f64, length: usize, rng: &mut R) -> Vec<u64> {
    let words = length.div_ceil(64);
    if prob <= 0.0 {
        return vec![0_u64; words];
    }
    if prob >= 1.0 {
        let mut data = vec![u64::MAX; words];
        let trailing = length % 64;
        if trailing > 0 {
            data[words - 1] = (1_u64 << trailing) - 1;
        }
        return data;
    }
    let threshold = (prob.clamp(0.0, 1.0) * 256.0) as u8;
    let mut data = vec![0_u64; words];
    let mut buf = [0_u8; 64];

    for (word_idx, word) in data.iter_mut().enumerate() {
        let bits_in_word = std::cmp::min(64, length.saturating_sub(word_idx * 64));
        rng.fill(&mut buf[..bits_in_word]);
        for (bit, &rb) in buf[..bits_in_word].iter().enumerate() {
            if rb < threshold {
                *word |= 1_u64 << bit;
            }
        }
    }
    data
}

/// SIMD-accelerated packed Bernoulli generation.
///
/// Semantics match `bernoulli_packed_fast` (byte-threshold sampling) while
/// vectorizing the threshold comparison for full 64-bit words.
pub fn bernoulli_packed_simd<R: Rng + ?Sized>(prob: f64, length: usize, rng: &mut R) -> Vec<u64> {
    let words = length.div_ceil(64);
    if prob <= 0.0 {
        return vec![0_u64; words];
    }
    if prob >= 1.0 {
        let mut data = vec![u64::MAX; words];
        let trailing = length % 64;
        if trailing > 0 {
            data[words - 1] = (1_u64 << trailing) - 1;
        }
        return data;
    }
    let threshold = (prob.clamp(0.0, 1.0) * 256.0) as u8;
    let mut data = vec![0_u64; words];
    let full_words = length / 64;
    let mut buf = [0_u8; 1024];
    let mut chunks = data[..full_words].chunks_exact_mut(16);

    for w_chunk in chunks.by_ref() {
        rng.fill(&mut buf);
        crate::simd::bernoulli_compare_batch_1024(&buf, threshold, w_chunk);
    }

    for word in chunks.into_remainder() {
        let mut small_buf = [0_u8; 64];
        rng.fill(&mut small_buf);
        *word = simd_bernoulli_compare_exposed(&small_buf, threshold);
    }

    if full_words < words {
        let remaining = length - full_words * 64;
        rng.fill(&mut buf[..remaining]);
        let mut tail = 0_u64;
        for (bit, &rb) in buf[..remaining].iter().enumerate() {
            if rb < threshold {
                tail |= 1_u64 << bit;
            }
        }
        data[full_words] = tail;
    }

    data
}

/// Fused encode+AND+popcount without materializing encoded input words.
///
/// Semantically equivalent to:
/// 1. `encoded = bernoulli_packed_simd(prob, length, rng)`
/// 2. `sum(popcount(encoded[word] & weight_words[word]))`
pub fn encode_and_popcount<R: Rng + ?Sized>(
    weight_words: &[u64],
    prob: f64,
    length: usize,
    rng: &mut R,
) -> u64 {
    if prob <= 0.0 {
        return 0;
    }
    if prob >= 1.0 {
        // All bits set → popcount of weight words directly
        let full_words = length / 64;
        let mut total = 0_u64;
        for w in weight_words.iter().take(full_words) {
            total += w.count_ones() as u64;
        }
        let trailing = length % 64;
        if trailing > 0 && full_words < weight_words.len() {
            let mask = (1_u64 << trailing) - 1;
            total += (weight_words[full_words] & mask).count_ones() as u64;
        }
        return total;
    }
    let threshold = (prob.clamp(0.0, 1.0) * 256.0) as u8;
    let full_words = length / 64;
    let mut total = 0_u64;
    let mut buf = [0_u8; 1024]; // 16 words worth
    let mut chunks = weight_words[..full_words].chunks_exact(16);

    let mut encoded_batch = [0_u64; 16];
    for w_chunk in chunks.by_ref() {
        rng.fill(&mut buf);
        crate::simd::bernoulli_compare_batch_1024(&buf, threshold, &mut encoded_batch);
        for (i, &w_word) in w_chunk.iter().enumerate() {
            total += (encoded_batch[i] & w_word).count_ones() as u64;
        }
    }

    for &w_word in chunks.remainder() {
        let mut small_buf = [0_u8; 64];
        rng.fill(&mut small_buf);
        let encoded = simd_bernoulli_compare_exposed(&small_buf, threshold);
        total += (encoded & w_word).count_ones() as u64;
    }

    let remaining = length.saturating_sub(full_words * 64);
    if remaining > 0 && full_words < weight_words.len() {
        rng.fill(&mut buf[..remaining]);
        let mut encoded = 0_u64;
        for (bit, &rb) in buf[..remaining].iter().enumerate() {
            if rb < threshold {
                encoded |= 1_u64 << bit;
            }
        }
        total += (encoded & weight_words[full_words]).count_ones() as u64;
    }

    total
}

/// Compare 64 bytes against a threshold and return a packed bit mask.
#[inline]
pub fn simd_bernoulli_compare_exposed(buf: &[u8], threshold: u8) -> u64 {
    debug_assert!(buf.len() >= 64, "buffer must contain at least 64 bytes");

    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("avx512bw") {
            // SAFETY: Runtime feature-gated.
            return unsafe { simd::avx512::bernoulli_compare_avx512(buf, threshold) };
        }
        if is_x86_feature_detected!("avx2") {
            // SAFETY: Runtime feature-gated.
            let lo = unsafe { simd::avx2::bernoulli_compare_avx2(&buf[0..32], threshold) };
            // SAFETY: Runtime feature-gated.
            let hi = unsafe { simd::avx2::bernoulli_compare_avx2(&buf[32..64], threshold) };
            return (lo as u64) | ((hi as u64) << 32);
        }
    }

    let mut mask = 0_u64;
    for (bit, &rb) in buf.iter().take(64).enumerate() {
        if rb < threshold {
            mask |= 1_u64 << bit;
        }
    }
    mask
}

/// Encode a flat matrix of probabilities into packed Bernoulli bitstreams.
///
/// Each value is clamped into `[0, 1]` before sampling.
pub fn encode_matrix_prob_to_packed<R: Rng + ?Sized>(
    values: &[f64],
    rows: usize,
    cols: usize,
    length: usize,
    words: usize,
    rng: &mut R,
) -> Vec<Vec<u64>> {
    let mut packed = Vec::with_capacity(rows * cols);
    for value in values.iter().take(rows * cols) {
        let mut row = bernoulli_packed_simd(*value, length, rng);
        row.resize(words, 0);
        packed.push(row);
    }
    packed
}

#[cfg(test)]
mod tests {
    use super::{
        bernoulli_packed, bernoulli_packed_fast, bernoulli_packed_simd, bernoulli_stream,
        bitwise_and, encode_and_popcount, pack, pack_fast, popcount, unpack,
    };

    #[test]
    fn pack_unpack_roundtrip() {
        let bits = vec![1, 0, 1, 1, 0, 1, 0, 0, 1];
        let packed = pack(&bits);
        let unpacked = unpack(&packed);
        assert_eq!(bits, unpacked);
    }

    #[test]
    fn pack_fast_matches_pack() {
        let cases = [0_usize, 1, 7, 8, 9, 63, 64, 65, 127, 128, 256, 1025];
        for length in cases {
            let bits: Vec<u8> = (0..length).map(|i| ((i * 7 + 3) % 2) as u8).collect();
            let slow = pack(&bits);
            let fast = pack_fast(&bits);
            assert_eq!(fast.length, slow.length);
            assert_eq!(fast.data, slow.data, "Mismatch at length={length}");
        }
    }

    #[test]
    fn pack_fast_roundtrip() {
        let bits: Vec<u8> = (0..2048).map(|i| ((i * 5 + 1) % 2) as u8).collect();
        let packed = pack_fast(&bits);
        let unpacked = unpack(&packed);
        assert_eq!(bits, unpacked);
    }

    #[test]
    fn and_and_popcount() {
        let a = pack(&[1, 0, 1, 1, 0, 0, 1, 1]);
        let b = pack(&[1, 1, 1, 0, 0, 1, 1, 0]);
        let c = bitwise_and(&a, &b);
        assert_eq!(unpack(&c), vec![1, 0, 1, 0, 0, 0, 1, 0]);
        assert_eq!(popcount(&c), 3);
    }

    #[test]
    fn bernoulli_packed_matches_stream_then_pack() {
        use rand::SeedableRng;
        use rand_chacha::ChaCha8Rng;

        let prob = 0.35;
        let length = 200;

        let mut rng1 = ChaCha8Rng::seed_from_u64(999);
        let stream = bernoulli_stream(prob, length, &mut rng1);
        let packed_via_stream = pack(&stream).data;

        let mut rng2 = ChaCha8Rng::seed_from_u64(999);
        let packed_direct = bernoulli_packed(prob, length, &mut rng2);

        assert_eq!(
            packed_via_stream, packed_direct,
            "bernoulli_packed must produce bit-identical output"
        );
    }

    #[test]
    fn bernoulli_packed_fast_statistics() {
        use rand::SeedableRng;
        use rand_chacha::ChaCha8Rng;

        let prob = 0.35;
        let length = 10_000;
        let mut rng = ChaCha8Rng::seed_from_u64(42);
        let packed = bernoulli_packed_fast(prob, length, &mut rng);
        let count: u64 = packed.iter().map(|w| w.count_ones() as u64).sum();
        let measured = count as f64 / length as f64;
        assert!(
            (measured - prob).abs() < 0.03,
            "Expected ~{prob}, got {measured}"
        );
    }

    #[test]
    fn bernoulli_packed_fast_deterministic() {
        use rand::SeedableRng;
        use rand_chacha::ChaCha8Rng;

        let mut rng1 = ChaCha8Rng::seed_from_u64(99);
        let a = bernoulli_packed_fast(0.5, 512, &mut rng1);

        let mut rng2 = ChaCha8Rng::seed_from_u64(99);
        let b = bernoulli_packed_fast(0.5, 512, &mut rng2);

        assert_eq!(a, b, "Same seed must produce identical output");
    }

    #[test]
    fn bernoulli_packed_simd_statistics() {
        use rand::SeedableRng;
        use rand_chacha::ChaCha8Rng;

        let prob = 0.35;
        let length = 10_000;
        let mut rng = ChaCha8Rng::seed_from_u64(1337);
        let packed = bernoulli_packed_simd(prob, length, &mut rng);
        let count: u64 = packed.iter().map(|w| w.count_ones() as u64).sum();
        let measured = count as f64 / length as f64;
        assert!(
            (measured - prob).abs() < 0.03,
            "Expected ~{prob}, got {measured}"
        );
    }

    #[test]
    fn bernoulli_packed_simd_deterministic() {
        use rand::SeedableRng;
        use rand_chacha::ChaCha8Rng;

        let mut rng1 = ChaCha8Rng::seed_from_u64(2026);
        let a = bernoulli_packed_simd(0.5, 1024, &mut rng1);

        let mut rng2 = ChaCha8Rng::seed_from_u64(2026);
        let b = bernoulli_packed_simd(0.5, 1024, &mut rng2);

        assert_eq!(a, b, "Same seed must produce identical output");
    }

    #[test]
    fn encode_and_popcount_matches_materialized() {
        use rand::SeedableRng;
        use rand_xoshiro::Xoshiro256PlusPlus;

        let prob = 0.41;
        let lengths = [63_usize, 64, 65, 1003, 1024];
        for length in lengths {
            let words = length.div_ceil(64);
            let weights: Vec<u64> = (0..words)
                .map(|i| (i as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15) ^ 0xA5A5_A5A5_5A5A_5A5A)
                .collect();

            let mut rng1 = Xoshiro256PlusPlus::seed_from_u64(2026);
            let fused = encode_and_popcount(&weights, prob, length, &mut rng1);

            let mut rng2 = Xoshiro256PlusPlus::seed_from_u64(2026);
            let encoded = bernoulli_packed_simd(prob, length, &mut rng2);
            let expected: u64 = encoded
                .iter()
                .zip(weights.iter())
                .map(|(&e, &w)| (e & w).count_ones() as u64)
                .sum();

            assert_eq!(fused, expected, "Mismatch at length={length}");
        }
    }
}