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
79
use dsp_fixedpoint::Q;
use num_traits::{ConstOne, ConstZero, clamp};
/// Constants
pub trait Clamp: ConstOne + ConstZero + PartialOrd {
/// Lowest value
///
/// Negative infinity for floating point values.
///
/// ```
/// # use idsp::Clamp;
/// assert_eq!(<i8 as Clamp>::MIN, -128);
/// assert_eq!(<f32 as Clamp>::MIN, f32::NEG_INFINITY);
/// ```
const MIN: Self;
/// Highest value
///
/// Positive infinity for floating point values.
///
/// ```
/// # use idsp::Clamp;
/// assert_eq!(<i8 as Clamp>::MAX, 127);
/// ```
const MAX: Self;
/// Clamp
fn clamp(self, min: Self, max: Self) -> Self {
clamp(self, min, max)
}
}
macro_rules! impl_const_float {
($ty:ident) => {
impl Clamp for $ty {
const MIN: Self = <$ty>::NEG_INFINITY;
const MAX: Self = <$ty>::INFINITY;
}
};
}
impl_const_float!(f32);
impl_const_float!(f64);
macro_rules! impl_const_int {
($($ty:ident),*) => {$(
impl Clamp for $ty {
const MIN: Self = <$ty>::MIN;
const MAX: Self = <$ty>::MAX;
}
)*};
}
impl_const_int!(
i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, isize, usize
);
impl<T: Clamp, A, const F: i8> Clamp for Q<T, A, F>
where
Self: ConstOne,
{
/// Lowest value
///
/// ```
/// # use dsp_fixedpoint::Q8;
/// # use idsp::Clamp;
/// # use num_traits::AsPrimitive;
/// assert_eq!(Q8::<4>::MIN, (-16f32).as_());
/// ```
const MIN: Self = Self::from_bits(T::MIN);
/// Highest value
///
/// ```
/// # use dsp_fixedpoint::Q8;
/// # use idsp::Clamp;
/// # use num_traits::AsPrimitive;
/// assert_eq!(Q8::<4>::MAX, (16.0f32 - 1.0 / 16.0).as_());
/// ```
const MAX: Self = Self::from_bits(T::MAX);
}