Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/utils/clz/FixedPointMathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -804,16 +804,16 @@ library FixedPointMathLib {
function cbrt(uint256 x) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Initial guess z ≈ ∛(3/4) · 2𐞥 where q = ⌊(257 − clz(x)) / 3⌋. The
// multiplier 233/256 ≈ 0.909 ≈ ∛(3/4) balances the worst-case
// over/underestimate across each octave triplet (ε_over ≈ 0.445,
// ε_under ≈ −0.278), giving >85 bits of precision after 6 N-R
// iterations. The `add(..., 1)` term ensures z ≥ 1 when x > 0 (the
// `shr` can produce 0 for small `q`)
z := add(shr(8, shl(div(sub(257, clz(x)), 3), 233)), 1)

// 6 Newton-Raphson iterations.
z := div(add(add(div(x, mul(z, z)), z), z), 3)
// Initial guess z ≈ c · 2𐞥 where b = ⌊log₂(x)⌋, q = ⌊b / 3⌋. The
// 8-bit fixed-point multipliers `c`: 144/128, 181/128, and 229/128
// are selected by `b mod 3` to balance each octave's worst-case
// final error. This gives >98 bits of precision after only 5
// Newton-Raphson iterations. The `or(..., 1)` keeps z ≥ 1 when the
// shifted estimate is 0.
let b := sub(255, clz(x))
z := or(shr(7, shl(div(b, 3), byte(add(mod(b, 3), 29), 0x90b5e5))), 1)

// 5 Newton-Raphson iterations
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
z := div(add(add(div(x, mul(z, z)), z), z), 3)
Expand Down