Engineering Category: Exact Integer Number Theory & Transcendentals
Best Use in Production: Hardware-verified modular arithmetic, integer roots, and combinatorial invariants for exact rational math engines and cryptographic primitives.
When to Select This Capsule: Sole canonical implementation for its 64-vector functional equivalence class. Low-latency L1-resident kernel (6471/500 cyc/op); well-suited for high-frequency pipeline stages.
Snap-Circuit Wiring Role: 1-to-1 Unary Transform Stage `(i64)->i64` — snaps directly into linear pipelines or fan-out DAG branches.
.text: 478 B (154 x86_64 insns)468 B)pub fn fx_sqrt_q16(value: i64) -> i64 {
// sqrt of a Q16.16 raw value, staying in Q16.16: sqrt(v * 2^16) * 2^16.
// Guarded so any i64 lands in range: negatives and the shift-overflow
// headroom both clamp, and the result is exact integer isqrt -- no float.
const FRAC: u32 = 16;
if value <= 0 {
return 0;
}
let headroom = i64::MAX >> FRAC;
let raw = if value > headroom { headroom } else { value };
(raw << FRAC).isqrt()
}
rustc -C opt-level=3 x86_64 Disassembly (478 B .text / 154 insns)fx_sqrt_q16:
test rdi, rdi
jle .LBB7_1
movabs r8, 140737488355327
cmp rdi, r8
cmovb r8, rdi
shl r8, 16
cmp rdi, 65536
jae .LBB7_3
bsr esi, r8d
xor esi, 31
mov ecx, esi
and cl, 30
shl r8d, cl
mov eax, r8d
shr eax, 24
lea rdx, [rip + .Lanon.a270e05f789020653a98a620bea8a67e.3]
movzx ecx, byte ptr [rdx + 2*rax]
movzx edx, byte ptr [rdx + 2*rax + 1]
mov edi, ecx
add edi, ecx
shl edx, 4
mov eax, r8d
shr eax, 20
and eax, 15
or eax, edx
xor edx, edx
div di
mov edi, r8d
shr edi, 16
xor r9d, r9d
shl cl, 4
movzx ecx, cl
add ecx, eax
shl edx, 4
and edi, 15
or edi, edx
imul eax, eax
sub di, ax
lea eax, [rcx + rcx - 1]
cmovae eax, r9d
sbb cx, 0
add eax, edi
movzx edi, cx
add edi, edi
movzx eax, ax
shl eax, 8
xor edx, edx
div edi
shl ecx, 8
movzx edi, cx
add edi, eax
mov eax, edi
mul edi
seto cl
cmp eax, r8d
seta al
or al, cl
movzx eax, al
sub edi, eax
shr esi
mov ecx, esi
shr edi, cl
mov rax, rdi
ret
.LBB7_1:
xor edi, edi
mov rax, rdi
ret
.LBB7_3:
bsr rsi, r8
xor rsi, 63
mov ecx, esi
and cl, 62
shl r8, cl
mov rax, r8
shr rax, 56
lea rcx, [rip + .Lanon.a270e05f789020653a98a620bea8a67e.3]
movzx edi, byte ptr [rcx + 2*rax]
movzx ecx, byte ptr [rcx + 2*rax + 1]
mov r9d, edi
add r9d, edi
shl ecx, 4
mov rax, r8
shr rax, 52
and eax, 15
or eax, ecx
xor edx, edx
div r9w
mov r9, r8
shr r9, 48
xor ecx, ecx
shl dil, 4
movzx edi, dil
add edi, eax
shl edx, 4
and r9d, 15
or r9d, edx
imul eax, eax
sub r9w, ax
lea eax, [rdi + rdi - 1]
cmovae eax, ecx
sbb di, 0
add eax, r9d
movzx r9d, di
add r9d, r9d
movzx edx, ax
shl edx, 8
mov rax, r8
shr rax, 40
movzx eax, al
or eax, edx
xor edx, edx
div r9d
mov r9, r8
shr r9, 32
shl edi, 8
movzx edi, di
add edi, eax
shl edx, 8
movzx r9d, r9b
or r9d, edx
imul eax, eax
sub r9d, eax
lea eax, [rdi + rdi - 1]
cmovae eax, ecx
lea edx, [r9 + rax]
sbb edi, 0
lea rcx, [rdi + rdi]
shl rdx, 16
mov eax, r8d
shr eax, 16
or rax, rdx
or rdx, rcx
shr rdx, 32
test dx, dx
je .LBB7_4
xor edx, edx
div rcx
jmp .LBB7_6
.LBB7_4:
xor edx, edx
div ecx
.LBB7_6:
shl edi, 16
add rdi, rax
mov rax, rdi
mul rdi
seto cl
cmp rax, r8
seta al
or al, cl
movzx eax, al
sub rdi, rax
shr esi
mov ecx, esi
shr rdi, cl
mov rax, rdi
ret
| Global Rank | Capsule & Domain | Hardware _rdtsc | Latency | Throughput | ELF .text | Ternary Vote | Author Aye |
|---|---|---|---|---|---|---|---|
| #630 | fx_sqrt_q16 (THIS CAPSULE) domain: fixed-point | id: 46fe68ba0b3218784ddec6a6647bf96d | 6471/500 cyc/op | 404479/100000 ns/op | 100000000/404479 ops/us | 478 B .text (154 insns) | + (1) | aye::Peter+Omni(0+1)::aarch64 @ 432Hz |