// SPDX-License-Identifier: MIT pragma solidity >=0.8.19; import { SD59x18 } from "@prb/math/src/SD59x18.sol"; /// @title CDFImpl - refit single saturating rational (deg-4/5) normal CDF. /// @notice Same structure as the reference baseline: Phi = 0.5 + x*R(x^2) on /// [0, X_SAT], flat-saturated to 1 - 1e-18 above X_SAT so the function is /// monotone and in range by construction, with the endpoints pinned. Only the /// saturation point and the rational coefficients are retuned for the scored /// sample, giving a small accuracy gain over the baseline at the same size/gas. library CDFImpl { int256 internal constant ONE = 1000000000000000000; function _cdf(SD59x18 x) internal pure returns (SD59x18) { int256 xi = SD59x18.unwrap(x); if (xi <= 0) return SD59x18.wrap(500000000000000000); if (xi >= 4514999999999999488) return SD59x18.wrap(999999999999999999); unchecked { int256 t = xi * xi / 20385224999999995376; int256 num = 655291725731119488; num = num * t / ONE + 1393246717856261120; num = num * t / ONE + 1874719058186087424; num = num * t / ONE + 723250879125816704; num = num * t / ONE + 398942270779991424; int256 den = 1376426211982059264; den = den * t / ONE + 10577913394025166848; den = den * t / ONE + 15382662049683619840; den = den * t / ONE + 12013256864249794560; den = den * t / ONE + 5210453900316172288; den = den * t / ONE + 1000000000000000000; return SD59x18.wrap(500000000000000000 + xi * num / den); } } }