Best Use & Engineering Application Guide

Engineering Category: Balanced Ternary & b158 Quantization

Best Use in Production: Packing and unpacking balanced ternary (-1, 0, +1) activation lanes into dense 64-bit registers for C64-constrained inference pipelines.

When to Select This Capsule: Sole canonical implementation for its 64-vector functional equivalence class. Sub-10-cycle hardware kernel (1/1 cyc/op); safe for ultra-hot per-sample or per-packet inner loops.

Snap-Circuit Wiring Role: 1-to-1 Unary Transform Stage `(u64)->u64` — snaps directly into linear pipelines or fan-out DAG branches.

Hardware _rdtsc: 1/1 cycles/op
Latency: 752833/200000 ns/op
Throughput: 200000000/752833 ops/us
Compiled ELF64 .text: 165 B (45 x86_64 insns)
C64 64KB RAM Budget: 487/65536 (487 B source)
Ternary Vote: + (score 1) | Salience: 1704985/19656
Pure-Rust Capsule Source Code (487 B)
pub fn ospop_pack5(word: u64) -> u64 {
    const POW3: [u64; 5] = [1, 3, 9, 27, 81];
    let mut byte: u64 = 0;
    let mut lane = 0usize;
    while lane < 5 {
        // Two's-complement ternary: 0 -> 0, 1 -> 1, -1 -> 255 (0xFF)
        let tu = ((word >> (lane * 8)) as u8) as i64;
        let trit = if tu > 1 { 1 } else if tu == 255 || tu == 0xFF { -1 } else { tu as i8 };
        let digit = (trit + 1) as u64;
        byte += digit * POW3[lane];
        lane += 1;
    }
    byte
}
rustc -C opt-level=3 x86_64 Disassembly (165 B .text / 45 insns)
ospop_pack5:
    lea    eax, [rdi + 1]
    and    eax, 3
    test    dil, -2
    mov    ecx, 2
    cmove    rcx, rax
    mov    eax, edi
    shr    eax, 8
    inc    eax
    and    eax, 3
    test    edi, 65024
    lea    rax, [rax + 2*rax]
    mov    edx, 6
    cmove    rdx, rax
    add    rdx, rcx
    mov    eax, edi
    shr    eax, 16
    inc    eax
    and    eax, 3
    test    edi, 16646144
    lea    rax, [rax + 8*rax]
    mov    ecx, 18
    cmove    rcx, rax
    add    rcx, rdx
    mov    eax, edi
    shr    eax, 24
    inc    eax
    and    eax, 3
    test    edi, -33554432
    lea    rax, [rax + 8*rax]
    lea    rax, [rax + 2*rax]
    mov    edx, 54
    cmove    rdx, rax
    mov    rax, rdi
    shr    rax, 32
    inc    eax
    and    eax, 3
    movabs    rsi, 1090921693184
    test    rdi, rsi
    lea    rax, [rax + 8*rax]
    lea    rsi, [rax + 8*rax]
    mov    eax, 162
    cmove    rax, rsi
    add    rax, rdx
    add    rax, rcx
    ret

Head-to-Head Functional Equivalence Class Peers (1 Implementation(s) Matching 64 Test Vectors)

Global RankCapsule & DomainHardware _rdtscLatencyThroughputELF .textTernary VoteAuthor Aye
#583ospop_pack5 (THIS CAPSULE)
domain: ternary | id: 1aa4c4c12cfebbe7c06ea1a6db532a64
1/1 cyc/op752833/200000 ns/op200000000/752833 ops/us165 B .text (45 insns)+ (1)aye::Hue+Gemini(0+1)::aarch64 @ 432Hz