use crate::algos::sqrt;
use crate::types::widths::{D9, D18, D38};
use crate::support::rounding::RoundingMode;
pub(crate) trait SqrtPolicy: Sized {
fn sqrt_impl(self, mode: RoundingMode) -> Self;
}
impl<const SCALE: u32> SqrtPolicy for D9<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
if self.0 <= 0 {
return Self(0);
}
sqrt::widen_to_d38::sqrt_d9(self, mode)
}
}
impl<const SCALE: u32> SqrtPolicy for D18<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
if self.0 <= 0 {
return Self(0);
}
sqrt::widen_to_d38::sqrt_d18(self, mode)
}
}
impl<const SCALE: u32> SqrtPolicy for D38<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
if self.0 <= 0 {
return Self(0);
}
Self(sqrt::mg_divide_d38::sqrt(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d57", feature = "wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D57<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
if matches!(SCALE, 20..=20) {
return Self(sqrt::lookup_d57_s20::sqrt(self.0, mode));
}
Self(sqrt::generic_wide::sqrt_d57(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d76", feature = "wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D76<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
Self(sqrt::generic_wide::sqrt_d76(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d115", feature = "wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D115<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
Self(sqrt::generic_wide::sqrt_d115(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d153", feature = "wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D153<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
Self(sqrt::generic_wide::sqrt_d153(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d230", feature = "wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D230<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
Self(sqrt::generic_wide::sqrt_d230(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d307", feature = "wide", feature = "x-wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D307<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
Self(sqrt::generic_wide::sqrt_d307(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d462", feature = "x-wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D462<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
Self(sqrt::generic_wide::sqrt_d462(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d616", feature = "x-wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D616<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
Self(sqrt::generic_wide::sqrt_d616(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d924", feature = "xx-wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D924<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
Self(sqrt::generic_wide::sqrt_d924(self.0, SCALE, mode))
}
}
#[cfg(any(feature = "d1232", feature = "xx-wide"))]
impl<const SCALE: u32> SqrtPolicy for crate::types::widths::D1232<SCALE> {
#[inline]
fn sqrt_impl(self, mode: RoundingMode) -> Self {
Self(sqrt::generic_wide::sqrt_d1232(self.0, SCALE, mode))
}
}