use crate::int::types::traits::BigInt;
use crate::int::types::Int;
use crate::support::rounding::RoundingMode;
#[derive(Clone, Copy, PartialEq, Eq)]
enum Algorithm {
Series,
#[cfg(feature = "_wide-support")]
Tang,
#[allow(dead_code)]
Schoolbook,
}
#[derive(Clone, Copy)]
enum Select<const N: usize> {
ByAlgorithm(Algorithm),
#[allow(dead_code)]
ByValue(fn(&Int<N>) -> Algorithm),
}
const fn select<const N: usize, const SCALE: u32>() -> Select<N> {
match (N, SCALE) {
#[cfg(any(feature = "d57", feature = "wide"))]
(3, 0..=56) => Select::ByValue(wide_tang_gate::<N, SCALE>),
#[cfg(any(feature = "d76", feature = "wide"))]
(4, 0..=75) => Select::ByValue(wide_tang_gate::<N, SCALE>),
_ => Select::ByAlgorithm(Algorithm::Series),
}
}
#[cfg(feature = "_wide-support")]
fn wide_tang_gate<const N: usize, const SCALE: u32>(raw: &Int<N>) -> Algorithm {
let max_bits = (SCALE + 2) * 332_192 / 100_000;
if BigInt::bit_length(*raw) <= max_bits {
Algorithm::Tang
} else {
Algorithm::Series
}
}
#[inline]
fn resolve<const N: usize, const SCALE: u32>(raw: &Int<N>) -> Algorithm {
match const { select::<N, SCALE>() } {
Select::ByAlgorithm(a) => a,
Select::ByValue(f) => f(raw),
}
}
#[cfg(feature = "_wide-support")]
#[inline]
#[must_use]
pub(crate) const fn is_tang<const N: usize, const SCALE: u32>() -> bool {
let _ = SCALE;
matches!(N, 3 | 4 | 6 | 8 | 12 | 16)
}
#[inline]
#[must_use]
pub(crate) fn dispatch<const N: usize, const SCALE: u32>(raw: Int<N>, mode: RoundingMode) -> Int<N> {
checked_dispatch::<N, SCALE>(raw, mode).unwrap_or_else(|| {
crate::support::diagnostics::overflow_panic_with_scale("exp_strict", SCALE)
})
}
#[inline]
#[must_use]
pub(crate) fn checked_dispatch<const N: usize, const SCALE: u32>(
raw: Int<N>,
mode: RoundingMode,
) -> Option<Int<N>> {
match resolve::<N, SCALE>(&raw) {
Algorithm::Series => series_routed::<N, SCALE>(raw, mode),
#[cfg(feature = "_wide-support")]
Algorithm::Tang => tang_routed::<N, SCALE>(raw, mode),
Algorithm::Schoolbook => schoolbook_routed::<N, SCALE>(raw, mode),
}
}
#[inline]
#[must_use]
pub(crate) fn dispatch_with<const N: usize, const SCALE: u32>(
raw: Int<N>,
working_digits: u32,
mode: RoundingMode,
) -> Int<N> {
match N {
1 | 2 => crate::algos::exp::exp_series_2limb::exp_with(
raw.resize_to::<Int<2>>(),
SCALE,
working_digits,
mode,
)
.and_then(super::narrow_fit::<N>)
.unwrap_or_else(|| {
crate::support::diagnostics::overflow_panic_with_scale("exp_with", SCALE)
}),
_ => {
let _ = working_digits;
dispatch::<N, SCALE>(raw, mode)
}
}
}
#[cfg(feature = "_wide-support")]
#[inline]
pub(crate) fn series_at_rung<C: crate::algos::support::wide_trig_core::WideTrigCore, const SCALE: u32>(
raw: C::Storage,
mode: RoundingMode,
) -> C::Storage
where
<C::W as BigInt>::Scratch: crate::int::types::compute_limbs::ComputeLimbs,
<C::Wexp as BigInt>::Scratch: crate::int::types::compute_limbs::ComputeLimbs,
{
use super::work_rung::{in_budget, rung_match, trig_rung, EXP_ARG_BUDGET};
use crate::algos::support::wide_trig_core::exp_series_g;
if in_budget::<C::Storage, SCALE, EXP_ARG_BUDGET>(&raw) {
rung_match!(trig_rung, C, SCALE, exp_series_g, [SCALE], raw, mode)
} else {
crate::algos::support::wide_trig_core::exp_series::<C, SCALE>(raw, mode)
}
}
#[inline]
fn series_routed<const N: usize, const SCALE: u32>(raw: Int<N>, mode: RoundingMode) -> Option<Int<N>> {
match N {
1 | 2 => crate::algos::exp::exp_series_2limb::exp_strict::<SCALE>(raw.resize_to::<Int<2>>(), mode).and_then(super::narrow_fit::<N>),
#[cfg(any(feature = "d57", feature = "wide"))]
3 => Some(series_at_rung::<crate::types::widths::wide_trig_d57::Core, SCALE>(raw.resize_to::<Int<3>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d76", feature = "wide"))]
4 => Some(series_at_rung::<crate::types::widths::wide_trig_d76::Core, SCALE>(raw.resize_to::<Int<4>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d115", feature = "wide"))]
6 => Some(series_at_rung::<crate::types::widths::wide_trig_d115::Core, SCALE>(raw.resize_to::<Int<6>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d153", feature = "wide"))]
8 => Some(series_at_rung::<crate::types::widths::wide_trig_d153::Core, SCALE>(raw.resize_to::<Int<8>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d230", feature = "wide"))]
12 => Some(series_at_rung::<crate::types::widths::wide_trig_d230::Core, SCALE>(raw.resize_to::<Int<12>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d307", feature = "wide", feature = "x-wide"))]
16 => Some(series_at_rung::<crate::types::widths::wide_trig_d307::Core, SCALE>(raw.resize_to::<Int<16>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d462", feature = "x-wide"))]
24 => Some(series_at_rung::<crate::types::widths::wide_trig_d462::Core, SCALE>(raw.resize_to::<Int<24>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d616", feature = "x-wide"))]
32 => Some(series_at_rung::<crate::types::widths::wide_trig_d616::Core, SCALE>(raw.resize_to::<Int<32>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d924", feature = "xx-wide"))]
48 => Some(series_at_rung::<crate::types::widths::wide_trig_d924::Core, SCALE>(raw.resize_to::<Int<48>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d1232", feature = "xx-wide"))]
64 => Some(series_at_rung::<crate::types::widths::wide_trig_d1232::Core, SCALE>(raw.resize_to::<Int<64>>(), mode).resize_to::<Int<N>>()),
_ => crate::algos::exp::exp_series_2limb::exp_strict::<SCALE>(raw.resize_to::<Int<2>>(), mode).and_then(super::narrow_fit::<N>),
}
}
#[inline]
fn schoolbook_routed<const N: usize, const SCALE: u32>(raw: Int<N>, mode: RoundingMode) -> Option<Int<N>> {
match N {
1 | 2 => super::narrow_fit::<N>(crate::algos::exp::exp_schoolbook::exp_schoolbook_strict::<SCALE>(raw.resize_to::<Int<2>>(), mode)),
#[cfg(any(feature = "d57", feature = "wide"))]
3 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d57::Core, SCALE>(raw.resize_to::<Int<3>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d76", feature = "wide"))]
4 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d76::Core, SCALE>(raw.resize_to::<Int<4>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d115", feature = "wide"))]
6 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d115::Core, SCALE>(raw.resize_to::<Int<6>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d153", feature = "wide"))]
8 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d153::Core, SCALE>(raw.resize_to::<Int<8>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d230", feature = "wide"))]
12 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d230::Core, SCALE>(raw.resize_to::<Int<12>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d307", feature = "wide", feature = "x-wide"))]
16 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d307::Core, SCALE>(raw.resize_to::<Int<16>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d462", feature = "x-wide"))]
24 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d462::Core, SCALE>(raw.resize_to::<Int<24>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d616", feature = "x-wide"))]
32 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d616::Core, SCALE>(raw.resize_to::<Int<32>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d924", feature = "xx-wide"))]
48 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d924::Core, SCALE>(raw.resize_to::<Int<48>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d1232", feature = "xx-wide"))]
64 => Some(crate::algos::exp::exp_schoolbook::exp_schoolbook::<crate::types::widths::wide_trig_d1232::Core, SCALE>(raw.resize_to::<Int<64>>(), mode).resize_to::<Int<N>>()),
_ => super::narrow_fit::<N>(crate::algos::exp::exp_schoolbook::exp_schoolbook_strict::<SCALE>(raw.resize_to::<Int<2>>(), mode)),
}
}
#[cfg(feature = "_wide-support")]
#[inline]
fn tang_routed<const N: usize, const SCALE: u32>(raw: Int<N>, mode: RoundingMode) -> Option<Int<N>> {
match N {
#[cfg(any(feature = "d57", feature = "wide"))]
3 => {
let r = raw.resize_to::<Int<3>>();
let out = match SCALE {
0..=44 => crate::algos::exp::exp_tang::exp_tang::<crate::types::widths::wide_trig_d57::Core, SCALE, 128, 8, true, true, false>(r, mode),
45..=56 => crate::algos::exp::exp_tang::exp_tang::<crate::types::widths::wide_trig_d57::Core, SCALE, 512, 30, true, true, false>(r, mode),
_ => crate::algos::support::wide_trig_core::exp_series::<crate::types::widths::wide_trig_d57::Core, SCALE>(r, mode),
};
Some(out.resize_to::<Int<N>>())
}
#[cfg(any(feature = "d76", feature = "wide"))]
4 => Some(crate::algos::exp::exp_tang::exp_tang::<crate::types::widths::wide_trig_d76::Core, SCALE, 512, 30, true, true, false>(raw.resize_to::<Int<4>>(), mode).resize_to::<Int<N>>()),
_ => series_routed::<N, SCALE>(raw, mode),
}
}
#[inline]
#[must_use]
pub(crate) fn exp2_dispatch<const N: usize, const SCALE: u32>(raw: Int<N>, mode: RoundingMode) -> Int<N> {
checked_exp2_dispatch::<N, SCALE>(raw, mode).unwrap_or_else(|| {
crate::support::diagnostics::overflow_panic_with_scale("exp2_strict", SCALE)
})
}
#[inline]
#[must_use]
pub(crate) fn checked_exp2_dispatch<const N: usize, const SCALE: u32>(
raw: Int<N>,
mode: RoundingMode,
) -> Option<Int<N>> {
match N {
1 | 2 => crate::algos::exp::exp_series_2limb::exp2_strict::<SCALE>(raw.resize_to::<Int<2>>(), mode).and_then(super::narrow_fit::<N>),
#[cfg(any(feature = "d57", feature = "wide"))]
3 => Some(crate::types::widths::wide_trig_d57::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<3>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d76", feature = "wide"))]
4 => Some(crate::types::widths::wide_trig_d76::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<4>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d115", feature = "wide"))]
6 => Some(crate::types::widths::wide_trig_d115::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<6>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d153", feature = "wide"))]
8 => Some(crate::types::widths::wide_trig_d153::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<8>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d230", feature = "wide"))]
12 => Some(crate::types::widths::wide_trig_d230::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<12>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d307", feature = "wide", feature = "x-wide"))]
16 => Some(crate::types::widths::wide_trig_d307::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<16>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d462", feature = "x-wide"))]
24 => Some(crate::types::widths::wide_trig_d462::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<24>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d616", feature = "x-wide"))]
32 => Some(crate::types::widths::wide_trig_d616::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<32>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d924", feature = "xx-wide"))]
48 => Some(crate::types::widths::wide_trig_d924::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<48>>(), mode).resize_to::<Int<N>>()),
#[cfg(any(feature = "d1232", feature = "xx-wide"))]
64 => Some(crate::types::widths::wide_trig_d1232::exp2_strict_with_kernel::<SCALE>(raw.resize_to::<Int<64>>(), mode).resize_to::<Int<N>>()),
_ => crate::algos::exp::exp_series_2limb::exp2_strict::<SCALE>(raw.resize_to::<Int<2>>(), mode).and_then(super::narrow_fit::<N>),
}
}
#[inline]
#[must_use]
pub(crate) fn exp2_dispatch_with<const N: usize, const SCALE: u32>(raw: Int<N>, working_digits: u32, mode: RoundingMode) -> Int<N> {
match N {
1 | 2 => crate::algos::exp::exp_series_2limb::exp2_with(raw.resize_to::<Int<2>>(), SCALE, working_digits, mode).and_then(super::narrow_fit::<N>).unwrap_or_else(|| crate::support::diagnostics::overflow_panic_with_scale("exp2_with", SCALE)),
#[cfg(any(feature = "d57", feature = "wide"))]
3 => crate::types::widths::wide_trig_d57::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<3>>(), working_digits, mode).resize_to::<Int<N>>(),
#[cfg(any(feature = "d76", feature = "wide"))]
4 => crate::types::widths::wide_trig_d76::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<4>>(), working_digits, mode).resize_to::<Int<N>>(),
#[cfg(any(feature = "d115", feature = "wide"))]
6 => crate::types::widths::wide_trig_d115::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<6>>(), working_digits, mode).resize_to::<Int<N>>(),
#[cfg(any(feature = "d153", feature = "wide"))]
8 => crate::types::widths::wide_trig_d153::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<8>>(), working_digits, mode).resize_to::<Int<N>>(),
#[cfg(any(feature = "d230", feature = "wide"))]
12 => crate::types::widths::wide_trig_d230::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<12>>(), working_digits, mode).resize_to::<Int<N>>(),
#[cfg(any(feature = "d307", feature = "wide", feature = "x-wide"))]
16 => crate::types::widths::wide_trig_d307::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<16>>(), working_digits, mode).resize_to::<Int<N>>(),
#[cfg(any(feature = "d462", feature = "x-wide"))]
24 => crate::types::widths::wide_trig_d462::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<24>>(), working_digits, mode).resize_to::<Int<N>>(),
#[cfg(any(feature = "d616", feature = "x-wide"))]
32 => crate::types::widths::wide_trig_d616::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<32>>(), working_digits, mode).resize_to::<Int<N>>(),
#[cfg(any(feature = "d924", feature = "xx-wide"))]
48 => crate::types::widths::wide_trig_d924::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<48>>(), working_digits, mode).resize_to::<Int<N>>(),
#[cfg(any(feature = "d1232", feature = "xx-wide"))]
64 => crate::types::widths::wide_trig_d1232::exp2_approx_with_kernel::<SCALE>(raw.resize_to::<Int<64>>(), working_digits, mode).resize_to::<Int<N>>(),
_ => crate::algos::exp::exp_series_2limb::exp2_with(raw.resize_to::<Int<2>>(), SCALE, working_digits, mode).and_then(super::narrow_fit::<N>).unwrap_or_else(|| crate::support::diagnostics::overflow_panic_with_scale("exp2_with", SCALE)),
}
}
#[cfg(test)]
mod series_rung_tests {
#[cfg(feature = "d307")]
const ALL_MODES: [crate::support::rounding::RoundingMode; 6] = [
crate::support::rounding::RoundingMode::HalfToEven,
crate::support::rounding::RoundingMode::HalfAwayFromZero,
crate::support::rounding::RoundingMode::HalfTowardZero,
crate::support::rounding::RoundingMode::Trunc,
crate::support::rounding::RoundingMode::Floor,
crate::support::rounding::RoundingMode::Ceiling,
];
#[test]
#[cfg(feature = "d307")]
fn d307_exp_rung_matches_tier_kernel() {
type Core = crate::types::widths::wide_trig_d307::Core;
for v in ["0", "0.5", "1", "1.5", "9.9", "-1", "-9.9", "0.0000000001", "50", "-50"] {
let x: crate::D307<19> = v.parse().unwrap();
for mode in ALL_MODES {
assert_eq!(
super::series_at_rung::<Core, 19>(x.to_bits(), mode),
crate::algos::support::wide_trig_core::exp_series::<Core, 19>(x.to_bits(), mode),
"exp({v}) mode {mode:?}"
);
}
}
}
}