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 (3351049/200000 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: 3351049/200000 cycles/op
Latency: 3191897/400000 ns/op
Throughput: 400000000/3191897 ops/us
Compiled ELF64 .text: 322 B (79 x86_64 insns)
C64 64KB RAM Budget: 501/65536 (501 B source)
Ternary Vote: + (score 1) | Salience: 805075/9108
Pure-Rust Capsule Source Code (501 B)
pub fn capsule_gaia_pack_trits_unrolled(word: u64) -> u64 {
    const POW3: [u64; 5] = [1, 3, 9, 27, 81];
    let mut byte_val: u64 = 0;
    let mut slot: usize = 0;
    while slot < 5 {
        let raw = ((word >> (slot as u32 * 12)) & 0xFFF) as i64 - 2048;
        let trit: i8 = (raw > 256) as i8 - (raw < -256) as i8;
        let tu = trit as u8;
        let digit = tu.wrapping_add((tu >> 7).wrapping_mul(3)) as u64;
        byte_val += digit * POW3[slot];
        slot += 1;
    }
    byte_val
}
rustc -C opt-level=3 x86_64 Disassembly (322 B .text / 79 insns)
capsule_gaia_pack_trits_unrolled:
    mov    eax, edi
    and    eax, 4095
    cmp    rax, 2305
    setae    cl
    cmp    rax, 1792
    sbb    cl, 0
    mov    eax, ecx
    sar    al, 7
    and    al, 3
    add    al, cl
    movzx    ecx, al
    mov    eax, edi
    shr    eax, 12
    mov    rdx, rdi
    shr    rdx, 24
    mov    rsi, rdi
    shr    rsi, 36
    shr    rdi, 48
    and    edi, 4095
    and    esi, 4095
    and    edx, 4095
    and    eax, 4095
    xor    r8d, r8d
    cmp    eax, 1792
    mov    r9d, 0
    sbb    r9d, r9d
    cmp    rdx, 1792
    movzx    r9d, r9b
    mov    r10d, 0
    sbb    r10d, r10d
    movzx    r10d, r10b
    shl    r10d, 8
    or    r10d, r9d
    cmp    rsi, 1792
    mov    r9d, 0
    sbb    r9d, r9d
    movzx    r9d, r9b
    shl    r9d, 16
    or    r9d, r10d
    cmp    rdi, 1792
    sbb    r8d, r8d
    shl    r8d, 24
    or    r8d, r9d
    movd    xmm0, r8d
    xor    r8d, r8d
    cmp    rdi, 2305
    setae    r8b
    xor    edi, edi
    cmp    rsi, 2305
    setae    dil
    xor    esi, esi
    cmp    rdx, 2305
    setae    sil
    xor    edx, edx
    cmp    eax, 2305
    setae    dl
    shl    esi, 8
    or    esi, edx
    shl    edi, 16
    or    edi, esi
    shl    r8d, 24
    or    r8d, edi
    movd    xmm1, r8d
    paddb    xmm1, xmm0
    pxor    xmm0, xmm0
    pxor    xmm2, xmm2
    pcmpgtb    xmm2, xmm1
    pand    xmm2, xmmword ptr [rip + .LCPI7_0]
    paddb    xmm2, xmm1
    punpcklbw    xmm2, xmm0
    pmullw    xmm2, xmmword ptr [rip + .LCPI7_1]
    punpcklwd    xmm2, xmm0
    pshufd    xmm0, xmm2, 238
    paddd    xmm0, xmm2
    pshufd    xmm1, xmm0, 85
    paddd    xmm1, xmm0
    movd    eax, xmm1
    add    rax, rcx
    ret

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

Global RankCapsule & DomainHardware _rdtscLatencyThroughputELF .textTernary VoteAuthor Aye
#81st_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
#1660capsule_gaia_pack_trits_unrolled (THIS CAPSULE)
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
7636497/200000 cyc/op2545499/200000 ns/op200000000/2545499 ops/us322 B .text (79 insns)+ (1)aye-gemini @ 432Hz
#1705gaia_pack_trits_scalar
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