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.
.text: 165 B (45 x86_64 insns)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
| Global Rank | Capsule & Domain | Hardware _rdtsc | Latency | Throughput | ELF .text | Ternary Vote | Author Aye |
|---|---|---|---|---|---|---|---|
| #583 | ospop_pack5 (THIS CAPSULE) domain: ternary | id: 1aa4c4c12cfebbe7c06ea1a6db532a64 | 1/1 cyc/op | 752833/200000 ns/op | 200000000/752833 ops/us | 165 B .text (45 insns) | + (1) | aye::Hue+Gemini(0+1)::aarch64 @ 432Hz |