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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
pub use crate::{float_helper::FloatHelper, int_helper::IntHelper};
use crate::{
types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8},
FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64,
FixedU8,
};
use core::cmp::Ordering;
pub enum Widest {
Unsigned(u128),
Negative(i128),
}
pub struct ToFixedHelper {
pub(crate) bits: Widest,
pub(crate) dir: Ordering,
pub(crate) overflow: bool,
}
pub struct ToFloatHelper {
pub(crate) neg: bool,
pub(crate) abs: u128,
}
pub struct FromFloatHelper {
pub(crate) kind: FloatKind,
}
pub enum FloatKind {
NaN,
Infinite { neg: bool },
Finite { neg: bool, conv: ToFixedHelper },
}
pub trait Sealed: Copy {
fn private_to_fixed_helper(self, dst_frac_nbits: u32, dst_int_nbits: u32) -> ToFixedHelper;
fn private_to_float_helper(self) -> ToFloatHelper;
fn private_saturating_from_float_helper(src: FromFloatHelper) -> Self;
fn private_overflowing_from_float_helper(src: FromFloatHelper) -> (Self, bool);
}
macro_rules! impl_sealed {
($Fixed:ident($LeEqU:ident, $Signedness:tt)) => {
impl<Frac: $LeEqU> Sealed for $Fixed<Frac> {
#[inline]
fn private_to_fixed_helper(
self,
dst_frac_nbits: u32,
dst_int_nbits: u32,
) -> ToFixedHelper {
self.to_bits().to_fixed_helper(
Self::FRAC_NBITS as i32,
dst_frac_nbits,
dst_int_nbits,
)
}
#[inline]
fn private_to_float_helper(self) -> ToFloatHelper {
let (neg, abs) = self.to_bits().neg_abs();
let abs = abs.into();
ToFloatHelper { neg, abs }
}
#[inline]
fn private_saturating_from_float_helper(src: FromFloatHelper) -> Self {
let neg = match src.kind {
FloatKind::NaN => panic!("NaN"),
FloatKind::Infinite { neg } => neg,
FloatKind::Finite { neg, .. } => neg,
};
let saturated = if neg { Self::MIN } else { Self::MAX };
let conv = match src.kind {
FloatKind::Finite { conv, .. } => conv,
_ => return saturated,
};
if conv.overflow {
return saturated;
}
let bits = if_signed_unsigned! {
$Signedness,
match conv.bits {
Widest::Unsigned(bits) => {
let bits = bits as _;
if bits < 0 {
return Self::MAX;
}
bits
}
Widest::Negative(bits) => bits as _,
},
match conv.bits {
Widest::Unsigned(bits) => bits as _,
Widest::Negative(_) => return Self::MIN,
},
};
Self::from_bits(bits)
}
#[inline]
fn private_overflowing_from_float_helper(src: FromFloatHelper) -> (Self, bool) {
let conv = match src.kind {
FloatKind::NaN => panic!("NaN"),
FloatKind::Infinite { .. } => panic!("infinite"),
FloatKind::Finite { conv, .. } => conv,
};
let mut new_overflow = false;
let bits = if_signed_unsigned! {
$Signedness,
match conv.bits {
Widest::Unsigned(bits) => {
let bits = bits as _;
if bits < 0 {
new_overflow = true;
}
bits
}
Widest::Negative(bits) => bits as _,
},
match conv.bits {
Widest::Unsigned(bits) => bits as _,
Widest::Negative(bits) => {
new_overflow = true;
bits as _
}
},
};
(Self::from_bits(bits), conv.overflow || new_overflow)
}
}
};
}
impl_sealed! { FixedI8(LeEqU8, Signed) }
impl_sealed! { FixedI16(LeEqU16, Signed) }
impl_sealed! { FixedI32(LeEqU32, Signed) }
impl_sealed! { FixedI64(LeEqU64, Signed) }
impl_sealed! { FixedI128(LeEqU128, Signed) }
impl_sealed! { FixedU8(LeEqU8, Unsigned) }
impl_sealed! { FixedU16(LeEqU16, Unsigned) }
impl_sealed! { FixedU32(LeEqU32, Unsigned) }
impl_sealed! { FixedU64(LeEqU64, Unsigned) }
impl_sealed! { FixedU128(LeEqU128, Unsigned) }