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
// SPDX-FileCopyrightText: 2026 John Moxley
// SPDX-License-Identifier: MIT OR Apache-2.0
//! `sqrt_mg_divide` — D38 square-root kernel
//! (`mg_divide::sqrt_raw_with`).
//!
//! Captures the **width-bespoke specialisation** that has lived on D38
//! since before the algorithm library existed: a hand-tuned isqrt on a
//! 256-bit intermediate (`mul_u128_to_u256` + `isqrt_256`) tailored to
//! the `Int<2>` (`i128`/`u128`) storage. Strictly faster than the
//! generic Newton kernel widening `Int<2> → Int<4>` and running the
//! generic `isqrt`.
//!
//! Genuinely width-bespoke (Q4.1b): the body is `i128`/`u128`-specific
//! 256-bit arithmetic with a hardware-`u128::isqrt` fast arm, so it
//! cannot be made generic over the storage width without losing the
//! intrinsic-backed fast path. It serves `N == 2` only (the D18 `N == 1`
//! tier widens to `Int<2>` in the policy layer and reuses it).
//!
//! Signature mirrors [`crate::algos::sqrt::sqrt_newton`]: takes the raw
//! storage integer, the scale, and the rounding mode; returns the raw
//! storage integer of the square root.
//!
//! Negative-input saturation (`raw < 0 → 0`) is handled by the policy
//! caller, not here, mirroring the contract of
//! [`crate::algos::support::mg_divide::sqrt_raw_with`] which only accepts
//! non-negative input.
use crateInt;
use crateRoundingMode;
/// D38 square-root kernel. Input `raw` must be non-negative; the policy
/// caller saturates negatives to zero before invoking this.
///
/// `Int<2>` entry point: bridges the decimal storage type to the `i128`
/// core ([`sqrt_mg_divide_raw`]) at the algorithm boundary. `i128` does
/// not escape this module.
pub
/// `i128` core of the D38 square-root kernel.