1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-FileCopyrightText: 2026 John Moxley
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Integer cube-root policy — the native-vs-Newton algorithm matcher.
//!
//! `Uint<N>::icbrt` and `Int<N>::icbrt` delegate to [`dispatch`], which
//! follows the canonical policy shape (see `docs/ARCHITECTURE.md` →
//! "Policy file structure"):
//!
//! 1. an [`Algorithm`] enum — the real icbrt algorithms, no `Default`
//! variant;
//! 2. a [`Select`] verdict — a settled algorithm or "the value decides";
//! 3. a `const fn` [`select`] keyed on `N`, total over the key;
//! 4. dispatch via an inline `const { select::<N>() }` block, then an
//! **exhaustive** `match algo` — no `_`, no panic.
//!
//! Because `select` is `const` and keyed only on the const generic `N`,
//! the `const { … }` block folds per monomorphisation and the unchosen arm
//! is dead-arm-eliminated in release: each concrete `Uint<N>` compiles to a
//! direct call to the chosen kernel, no runtime branch.
//!
//! # Algorithm selection
//!
//! Unlike isqrt (which has a genuine hardware `u64::isqrt` / `u128::isqrt`
//! narrow path), there is **no native hardware cube root**: every width is
//! served by the one width-agnostic Newton kernel
//! ([`crate::int::algos::icbrt::icbrt_newton::icbrt_newton`]) — Newton
//! iteration with a shared-library `f64::cbrt` seed over u64 limbs. The
//! `icbrt_ab` N-way A/B confirms this Newton kernel beats the bitwise
//! [`Algorithm::Schoolbook`] reference at EVERY width (12.9x at `N == 1`
//! growing to ~50x at `N == 64`), so `select` returns
//! [`Algorithm::Newton`] for all `N`.
//!
//! [`Algorithm::Schoolbook`] is the registered-but-unselected reference
//! baseline (kept per `docs/ARCHITECTURE.md` → "Keeping the alternatives").
//! The `ByValue` arm of [`Select`] is present for canonical-shape
//! uniformity; `select` never returns it.
//!
//! # Const-ness
//!
//! `dispatch` is **not** `const fn`. The `Newton` arm calls
//! [`crate::int::algos::icbrt::icbrt_newton::icbrt_newton`] which performs Newton
//! iteration and is not const-evaluable.
//!
//! Names follow RULES §4: `icbrt_newton` → `Newton`, `icbrt_schoolbook` →
//! `Schoolbook`.
use crateicbrt_newton as icbrt_newton_kernel;
use crateicbrt_schoolbook as icbrt_schoolbook_kernel;
use crateUint;
// ── 1. the real icbrt algorithms — NAMED, no `Default` ───────────────
/// The integer cube-root algorithms this policy chooses between. Variants
/// are the CamelCase of each kernel fn's name minus the `icbrt_` function
/// prefix — strict 1:1 with the kernel fns.
// ── 2. the verdict ────────────────────────────────────────────────────
/// A settled algorithm, or "the value decides". The icbrt picker always
/// returns `ByAlgorithm(Newton)`: one algorithm serves every `N`. `ByValue`
/// is part of the canonical shape for uniformity across functions; `select`
/// never returns it.
// ── 3. the matcher: const, keyed on `N`, total over the key ──────────
/// Pick the icbrt algorithm for storage limb count `N`. Total over the key;
/// [`Algorithm::Newton`] wins at every `N` (the `icbrt_ab` A/B beats the
/// `Schoolbook` reference 12.9x–50x across the full width sweep).
const
// ── algorithm fns ─────────────────────────────────────────────────────
/// Newton integer cube root for `Uint<N>` — serves every `N`.
///
/// Delegates to [`icbrt_newton_kernel`]: Newton iteration with a
/// shared-library `f64::cbrt` seed over u64 limbs; converges quadratically to
/// `floor(x^(1/3))`. For the narrow widths (`N <= 2`) the limb loop
/// terminates in a handful of iterations; there is no distinct hardware
/// cube-root path, so this one kernel is the icbrt for all widths.
pub
/// Schoolbook bit-by-bit integer cube root for `Uint<N>`.
///
/// Delegates to
/// [`icbrt_schoolbook_kernel`][`crate::int::algos::icbrt::icbrt_schoolbook::icbrt_schoolbook`]:
/// digit-by-digit restoring algorithm; no division, no float seed.
/// Serves any `N` as a generic reference baseline.
pub
// ── 4. the dispatcher: fold the verdict, then dispatch ────────────────
/// Integer cube-root dispatcher for `Uint<N>`.
///
/// Resolves the compile-time algorithm verdict via
/// `const { select::<N>() }` (folds per monomorphisation; dead arms are
/// eliminated in release) then dispatches exhaustively over [`Algorithm`].
///
/// Not `const fn`: the `Newton` arm delegates to
/// [`crate::int::algos::icbrt::icbrt_newton::icbrt_newton`] (Newton iteration, not
/// const-evaluable).
pub