use crate::common::impl_fixed19;
use crate::consts::ONE_RAW;
use crate::{Fixed19, OutOfRange, Round, UDec19};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[must_use]
#[repr(transparent)]
pub struct Dec19(i128);
impl_fixed19! {
type = Dec19,
raw = i128,
int = i64,
mac = "dec",
max_ascii_len = 41,
sub_overflow = "Dec19 overflow in subtraction",
}
impl Dec19 {
const KEY_FLIP: u128 = 1 << 127;
const LITERAL_RANGE_ERROR: &'static str =
"dec! literal out of range (about ±1.7e19)";
#[inline]
pub(crate) const fn to_parts(self) -> (bool, u128) {
(self.0 < 0, self.0.unsigned_abs())
}
#[inline]
pub(crate) const fn raw_from_parts(
negative: bool,
magnitude: u128,
) -> Option<i128> {
if negative {
if magnitude > 1u128 << 127 {
None
} else {
Some((magnitude as i128).wrapping_neg())
}
} else if magnitude > i128::MAX as u128 {
None
} else {
Some(magnitude as i128)
}
}
#[inline]
pub(crate) const fn from_parts(
negative: bool,
magnitude: u128,
) -> Option<Self> {
match Self::raw_from_parts(negative, magnitude) {
Some(raw) => Some(Self(raw)),
None => None,
}
}
#[must_use]
#[inline]
pub const fn is_negative(self) -> bool {
self.0 < 0
}
#[must_use]
#[inline]
pub const fn is_positive(self) -> bool {
self.0 > 0
}
#[inline]
#[track_caller]
pub const fn abs(self) -> Self {
match self.checked_abs() {
Some(v) => v,
None => panic!("Dec19 overflow in abs"),
}
}
#[must_use]
#[inline]
pub const fn checked_abs(self) -> Option<Self> {
match self.0.checked_abs() {
Some(v) => Some(Self(v)),
None => None,
}
}
#[inline]
pub const fn saturating_abs(self) -> Self {
Self(self.0.saturating_abs())
}
#[inline]
pub const fn unsigned_abs(self) -> UDec19 {
UDec19::from_raw(self.0.unsigned_abs())
}
#[inline]
pub const fn abs_diff(self, other: Self) -> UDec19 {
UDec19::from_raw(self.0.abs_diff(other.0))
}
#[must_use]
#[inline]
pub const fn checked_neg(self) -> Option<Self> {
match self.0.checked_neg() {
Some(v) => Some(Self(v)),
None => None,
}
}
#[inline]
pub const fn saturating_neg(self) -> Self {
Self(self.0.saturating_neg())
}
#[track_caller]
#[inline]
pub fn mul<R: Fixed19>(self, rhs: R, mode: Round) -> Self {
self
.checked_mul(rhs, mode)
.expect("Dec19 overflow in multiplication")
}
#[must_use]
#[inline]
pub fn checked_mul<R: Fixed19>(self, rhs: R, mode: Round) -> Option<Self> {
let (neg_a, a) = self.to_parts();
let (neg_b, b) = rhs.__to_parts();
let negative = neg_a != neg_b;
let m = crate::kernel::mul::mul_parts(a, b, negative, mode)?;
Self::from_parts(negative, m)
}
#[inline]
pub fn saturating_mul<R: Fixed19>(self, rhs: R, mode: Round) -> Self {
match self.checked_mul(rhs, mode) {
Some(v) => v,
None if self.is_negative() != rhs.__to_parts().0 => Self::MIN,
None => Self::MAX,
}
}
#[track_caller]
#[inline]
pub fn div<R: Fixed19>(self, rhs: R, mode: Round) -> Self {
self
.checked_div(rhs, mode)
.expect("Dec19 division by zero or overflow")
}
#[must_use]
#[inline]
pub fn checked_div<R: Fixed19>(self, rhs: R, mode: Round) -> Option<Self> {
let (neg_a, a) = self.to_parts();
let (neg_b, b) = rhs.__to_parts();
let negative = neg_a != neg_b;
let m = crate::kernel::div::div_parts(a, b, negative, mode)?;
Self::from_parts(negative, m)
}
}
impl core::ops::Neg for Dec19 {
type Output = Self;
#[inline]
#[track_caller]
fn neg(self) -> Self {
self.checked_neg().expect("Dec19 overflow in negation")
}
}
macro_rules! from_int {
($($t:ty),*) => {$(
impl From<$t> for Dec19 {
#[inline]
fn from(n: $t) -> Self {
Self(n as i128 * ONE_RAW as i128)
}
}
)*};
}
from_int!(i8, i16, i32, i64, u8, u16, u32);
macro_rules! try_from_int {
($($t:ty),*) => {$(
impl TryFrom<$t> for Dec19 {
type Error = OutOfRange;
#[inline]
fn try_from(n: $t) -> Result<Self, OutOfRange> {
i128::try_from(n)
.ok()
.and_then(|n| n.checked_mul(ONE_RAW as i128))
.map(Self)
.ok_or(OutOfRange)
}
}
)*};
}
try_from_int!(u64, i128, u128);
impl TryFrom<UDec19> for Dec19 {
type Error = OutOfRange;
#[inline]
fn try_from(v: UDec19) -> Result<Self, OutOfRange> {
i128::try_from(v.to_raw()).map(Self).map_err(|_| OutOfRange)
}
}