1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Macro-generated helper methods: min/max/clamp/recip/copysign.
//!
//! `min` / `max` / `clamp` / `recip` are identical across native and
//! wide storage (both surfaces support `Ord::min` etc. and the `/`
//! operator). Only `copysign` differs: a hand-rolled wide integer cannot be
//! compared against the `0` literal, so the `wide` arm uses
//! `is_negative()` instead.
/// Emits `min`, `max`, `clamp`, `recip`, `copysign` for `$Type<SCALE>`.
macro_rules! decl_decimal_helpers {
// Wide storage.
(wide $Type:ident) => {
$crate::macros::helpers::decl_decimal_helpers!(@common $Type);
impl<const SCALE: u32> $Type<SCALE> {
/// Magnitude of `self` with the sign of `sign`. Zero sign is
/// treated as positive (the storage type has no negative zero).
#[inline]
#[must_use]
pub fn copysign(self, sign: Self) -> Self {
let mag = self.0.abs();
if sign.0.is_negative() { Self(-mag) } else { Self(mag) }
}
}
};
// Native (primitive integer) storage.
($Type:ident) => {
$crate::macros::helpers::decl_decimal_helpers!(@common $Type);
impl<const SCALE: u32> $Type<SCALE> {
/// Magnitude of `self` with the sign of `sign`. Zero sign is
/// treated as positive (the storage type has no negative zero).
#[inline]
#[must_use]
pub fn copysign(self, sign: Self) -> Self {
let mag = self.0.abs();
if sign.0 < 0 { Self(-mag) } else { Self(mag) }
}
}
};
// Shared: min / max / clamp / recip.
(@common $Type:ident) => {
impl<const SCALE: u32> $Type<SCALE> {
/// The lesser of `self` and `other`.
#[inline]
#[must_use]
pub fn min(self, other: Self) -> Self {
Self(self.0.min(other.0))
}
/// The greater of `self` and `other`.
#[inline]
#[must_use]
pub fn max(self, other: Self) -> Self {
Self(self.0.max(other.0))
}
/// Restrict `self` to the closed interval `[lo, hi]`.
/// Panics if `lo > hi`.
#[inline]
#[must_use]
pub fn clamp(self, lo: Self, hi: Self) -> Self {
Self(self.0.clamp(lo.0, hi.0))
}
/// Multiplicative inverse: `ONE / self`. Panics on `self == ZERO`.
#[inline]
#[must_use]
pub fn recip(self) -> Self {
Self::ONE / self
}
}
};
}
pub(crate) use decl_decimal_helpers;