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: Verified baseline member of a 4-capsule Equivalence Class; useful as a reference or cross-validation peer. Low-latency L1-resident kernel (688417/20000 cyc/op); well-suited for high-frequency pipeline stages.

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

Hardware _rdtsc: 688417/20000 cycles/op
Latency: 4450269/400000 ns/op
Throughput: 400000000/4450269 ops/us
Compiled ELF64 .text: 593 B (138 x86_64 insns)
C64 64KB RAM Budget: 635/65536 (635 B source)
Ternary Vote: 0 (score 0) | Salience: 359095/4212
Pure-Rust Capsule Source Code (635 B)
pub fn gaia_pack_trits_scalar(word: u64) -> u64 {
    let mut byte_val: u64 = 0;
    let mut pow3: u64 = 1;
    let mut slot: u32 = 0;
    while slot < 5 {
        let raw = ((word >> (slot * 12)) & 0xFFF) as i64 - 2048;
        let mut cursor: i64 = 0;
        let mut step: i64 = raw;
        while step > 256 {
            cursor = 1;
            step -= 113;
        }
        while step < -256 {
            cursor = -1;
            step += 113;
        }
        let digit: u64 = if cursor < 0 { 2 } else if cursor > 0 { 1 } else { 0 };
        byte_val += digit * pow3;
        pow3 *= 3;
        slot += 1;
    }
    byte_val
}
rustc -C opt-level=3 x86_64 Disassembly (593 B .text / 138 insns)
gaia_pack_trits_scalar:
    push    rbp
    push    r15
    push    r14
    push    r13
    push    r12
    push    rbx
    mov    r10d, edi
    and    r10d, 4095
    lea    rax, [r10 - 2048]
    xor    esi, esi
    cmp    r10, 2305
    setae    sil
    cmp    rax, 256
    mov    r11d, 256
    cmovge    rax, r11
    neg    rax
    lea    rcx, [r10 + rax]
    add    rcx, -1936
    movabs    rbx, -7999030616033345391
    mov    rax, rcx
    mul    rbx
    shr    rdx, 6
    imul    rax, rdx, 113
    mov    rdx, rcx
    sub    rdx, rax
    sub    rdx, rcx
    mov    r8d, edi
    shr    r8d, 12
    and    r8d, 4095
    lea    rax, [r8 - 2048]
    xor    r9d, r9d
    cmp    r8d, 2305
    setae    r9b
    cmp    rax, 256
    cmovge    rax, r11
    add    r10, rdx
    add    r10, -2048
    neg    rax
    lea    rcx, [r8 + rax]
    add    rcx, -1936
    mov    rax, rcx
    mul    rbx
    shr    rdx, 6
    imul    rax, rdx, 113
    mov    r14, rcx
    sub    r14, rax
    sub    r14, rcx
    mov    r13, rdi
    shr    r13, 24
    and    r13d, 4095
    lea    rax, [r13 - 2048]
    xor    r15d, r15d
    cmp    r13, 2305
    setae    r15b
    cmp    rax, 256
    cmovge    rax, r11
    neg    rax
    lea    rcx, [rax + r13]
    add    rcx, -1936
    mov    rax, rcx
    mul    rbx
    shr    rdx, 6
    imul    rax, rdx, 113
    mov    rdx, rcx
    sub    rdx, rax
    sub    rdx, rcx
    mov    r12, rdi
    shr    r12, 36
    and    r12d, 4095
    lea    rax, [r12 - 2048]
    cmp    rax, 256
    cmovge    rax, r11
    add    r13, rdx
    add    r13, -2048
    neg    rax
    lea    rcx, [r12 + rax]
    add    rcx, -1936
    mov    rax, rcx
    mul    rbx
    shr    rdx, 6
    imul    rax, rdx, 113
    mov    rbp, rcx
    sub    rbp, rax
    sub    rbp, rcx
    shr    rdi, 48
    and    edi, 4095
    lea    rax, [rdi - 2048]
    cmp    rax, 256
    cmovl    r11, rax
    neg    r11
    lea    rcx, [rdi + r11]
    add    rcx, -1936
    mov    rax, rcx
    mul    rbx
    shr    rdx, 6
    imul    rdx, rdx, 113
    mov    rax, rcx
    sub    rax, rdx
    sub    rax, rcx
    xor    ecx, ecx
    cmp    r12, 2305
    lea    rdx, [rbp + r12 - 2048]
    mov    r11d, 27
    cmovb    r11, rcx
    cmp    rdx, -256
    mov    ebx, 54
    cmovge    rbx, r11
    cmp    r13, -256
    lea    r11, [r15 + 8*r15]
    mov    edx, 18
    cmovge    rdx, r11
    add    r8, r14
    add    r8, -2048
    cmp    r8, -256
    lea    r8, [r9 + 2*r9]
    mov    r9d, 6
    cmovge    r9, r8
    cmp    r10, -256
    mov    r8d, 2
    cmovge    r8, rsi
    add    r8, r9
    add    rdx, rbx
    add    rdx, r8
    cmp    rdi, 2305
    lea    rax, [rax + rdi - 2048]
    mov    esi, 81
    cmovb    rsi, rcx
    cmp    rax, -256
    mov    eax, 162
    cmovge    rax, rsi
    add    rax, rdx
    pop    rbx
    pop    r12
    pop    r13
    pop    r14
    pop    r15
    pop    rbp
    ret

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

Global RankCapsule & DomainHardware _rdtscLatencyThroughputELF .textTernary VoteAuthor Aye
#80st_golf_gaia_pack_trits_horner_u64
domain: gaia-mlx-quant | id: 5ae0cdaba109858c999a7446a0af55cf
1930529/200000 cyc/op523749/200000 ns/op200000000/523749 ops/us228 B .text (57 insns)+ (1)aye::Hue+Gemini(0+1)::x86_64 @ 432Hz
#1659capsule_gaia_pack_trits_unrolled
domain: smart-tree | id: 31f371c736b48b658fb6d367287d7d49
3351049/200000 cyc/op3191897/400000 ns/op400000000/3191897 ops/us322 B .text (79 insns)+ (1)aye::Hue+Gemini(0+1)::x86_64 @ 432Hz
#1692gaia_pack_trits_unrolled
domain: gaia-mlx-quant | id: 919c37509f21ba416f9d7f7cc6bc63e6
14842677/400000 cyc/op4947559/400000 ns/op400000000/4947559 ops/us322 B .text (79 insns)+ (1)aye-gemini @ 432Hz
#1705gaia_pack_trits_scalar (THIS CAPSULE)
domain: gaia-mlx-quant | id: bb00daa6126618631b483a310bc3a4ea
688417/20000 cyc/op4450269/400000 ns/op400000000/4450269 ops/us593 B .text (138 insns)0 (0)aye-claude @ 432Hz