Skip to content

Commit cd93fab

Browse files
committed
Remove E from generics for simplicity
1 parent ab4f49d commit cd93fab

File tree

3 files changed

+45
-44
lines changed

3 files changed

+45
-44
lines changed

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ const LOG2_SIGNIFICAND: [f64; 16] = [
106106
#[macro_export]
107107
macro_rules! select_sized_trait {
108108
(u8, $name:ident, $e:expr, $m:expr) => {
109-
impl $crate::Most8<$e, $m> for $name {
109+
impl $crate::Most8<$m> for $name {
110+
const E: u32 = Self::E;
110111
const B: i32 = Self::B;
111112
const N: $crate::NanStyle = Self::N;
112113

@@ -132,7 +133,8 @@ macro_rules! select_sized_trait {
132133
}
133134
};
134135
(u16, $name:ident, $e:expr, $m:expr) => {
135-
impl $crate::Most16<$e, $m> for $name {
136+
impl $crate::Most16<$m> for $name {
137+
const E: u32 = Self::E;
136138
const B: i32 = Self::B;
137139
const N: $crate::NanStyle = Self::N;
138140

src/most16.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,40 @@ use core::ops::Neg;
2222
///
2323
/// Types generated by [`minifloat!`][crate::minifloat] implement this
2424
/// trait.
25-
pub trait Most16<const E: u32, const M: u32>:
25+
pub trait Most16<const M: u32>:
2626
Sized + Copy + PartialEq + PartialOrd + Neg<Output = Self>
2727
{
2828
/// Exponent bit-width
29-
const E: u32 = E;
29+
const E: u32;
3030

3131
/// Significand (mantissa) precision
3232
const M: u32 = M;
3333

3434
/// Exponent bias
35-
const B: i32 = (1 << (E - 1)) - 1;
35+
const B: i32 = (1 << (Self::E - 1)) - 1;
3636

3737
/// NaN encoding style
3838
const N: NanStyle;
3939

4040
/// Total bitwidth
41-
const BITWIDTH: u32 = 1 + E + M;
41+
const BITWIDTH: u32 = 1 + Self::E + Self::M;
4242

4343
/// The radix of the internal representation
4444
const RADIX: u32 = 2;
4545

4646
/// The number of digits in the significand, including the implicit leading bit
4747
///
4848
/// Equal to `M` + 1
49-
const MANTISSA_DIGITS: u32 = M + 1;
49+
const MANTISSA_DIGITS: u32 = Self::M + 1;
5050

5151
/// The maximum exponent
5252
///
5353
/// Normal numbers < 1 &times; 2<sup>`MAX_EXP`</sup>.
54-
const MAX_EXP: i32 = (1 << E)
54+
const MAX_EXP: i32 = (1 << Self::E)
5555
- Self::B
5656
- match Self::N {
5757
NanStyle::IEEE => 1,
58-
NanStyle::FN => (M == 0) as i32,
58+
NanStyle::FN => (Self::M == 0) as i32,
5959
NanStyle::FNUZ => 0,
6060
};
6161

@@ -73,15 +73,15 @@ pub trait Most16<const E: u32, const M: u32>:
7373
///
7474
/// Equal to floor([`M`][Self::M] log<sub>10</sub>(2))
7575
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
76-
const DIGITS: u32 = (M as f64 * crate::LOG10_2) as u32;
76+
const DIGITS: u32 = (Self::M as f64 * crate::LOG10_2) as u32;
7777

7878
/// Maximum <var>x</var> such that 10<sup>`x`</sup> is normal
7979
///
8080
/// Equal to floor(log<sub>10</sub>([`MAX`][Self::MAX]))
8181
#[allow(clippy::cast_possible_truncation)]
8282
const MAX_10_EXP: i32 = {
83-
let exponent = (1 << E) - Self::B - matches!(Self::N, NanStyle::IEEE) as i32;
84-
let precision = M + !matches!(Self::N, NanStyle::FN) as u32;
83+
let exponent = (1 << Self::E) - Self::B - matches!(Self::N, NanStyle::IEEE) as i32;
84+
let precision = Self::M + !matches!(Self::N, NanStyle::FN) as u32;
8585
let log2_max = exponent as f64 + crate::LOG2_SIGNIFICAND[precision as usize];
8686
(log2_max * crate::LOG10_2) as i32
8787
};
@@ -126,7 +126,7 @@ pub trait Most16<const E: u32, const M: u32>:
126126
const MIN: Self;
127127

128128
/// Magnitude mask for internal usage
129-
const ABS_MASK: u16 = (1 << (E + M)) - 1;
129+
const ABS_MASK: u16 = (1 << (Self::E + Self::M)) - 1;
130130

131131
/// Raw transmutation from `u16`
132132
#[must_use]
@@ -198,8 +198,8 @@ pub trait Most16<const E: u32, const M: u32>:
198198
} else if self.is_infinite() {
199199
core::num::FpCategory::Infinite
200200
} else {
201-
let exp_mask = ((1 << E) - 1) << M;
202-
let man_mask = (1 << M) - 1;
201+
let exp_mask = ((1 << Self::E) - 1) << Self::M;
202+
let man_mask = (1 << Self::M) - 1;
203203

204204
match (self.to_bits() & exp_mask, self.to_bits() & man_mask) {
205205
(0, 0) => core::num::FpCategory::Zero,
@@ -221,19 +221,19 @@ pub trait Most16<const E: u32, const M: u32>:
221221
/// Check if the sign bit is clear
222222
#[must_use]
223223
fn is_sign_positive(self) -> bool {
224-
self.to_bits() >> (E + M) & 1 == 0
224+
self.to_bits() >> (Self::E + Self::M) & 1 == 0
225225
}
226226

227227
/// Check if the sign bit is set
228228
#[must_use]
229229
fn is_sign_negative(self) -> bool {
230-
self.to_bits() >> (E + M) & 1 == 1
230+
self.to_bits() >> (Self::E + Self::M) & 1 == 1
231231
}
232232
}
233233

234234
/// Lossy conversion to [`f64`]
235-
fn as_f64<const E: u32, const M: u32, T: Most16<E, M>>(x: T) -> f64 {
236-
let bias = (1 << (E - 1)) - 1;
235+
fn as_f64<const M: u32, T: Most16<M>>(x: T) -> f64 {
236+
let bias = (1 << (T::E - 1)) - 1;
237237
let sign = if x.is_sign_negative() { -1.0 } else { 1.0 };
238238
let magnitude = x.abs().to_bits();
239239

src/most8.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use core::ops::Neg;
1212

1313
/// IEEE-like minifloat taking at most 8 bits
1414
///
15-
/// * `E`: exponent bit-width
1615
/// * `M`: explicit significand (mantissa) bit-width
1716
///
1817
/// Constraints:
@@ -22,11 +21,11 @@ use core::ops::Neg;
2221
///
2322
/// Types generated by [`minifloat!`][crate::minifloat] implement this
2423
/// trait.
25-
pub trait Most8<const E: u32, const M: u32>:
24+
pub trait Most8<const M: u32>:
2625
Sized + Copy + PartialEq + PartialOrd + Neg<Output = Self>
2726
{
2827
/// Exponent bit-width
29-
const E: u32 = E;
28+
const E: u32;
3029

3130
/// Significand (mantissa) precision
3231
const M: u32 = M;
@@ -38,24 +37,24 @@ pub trait Most8<const E: u32, const M: u32>:
3837
const N: NanStyle;
3938

4039
/// Total bitwidth
41-
const BITWIDTH: u32 = 1 + E + M;
40+
const BITWIDTH: u32 = 1 + Self::E + Self::M;
4241

4342
/// The radix of the internal representation
4443
const RADIX: u32 = 2;
4544

4645
/// The number of digits in the significand, including the implicit leading bit
4746
///
4847
/// Equal to `M` + 1
49-
const MANTISSA_DIGITS: u32 = M + 1;
48+
const MANTISSA_DIGITS: u32 = Self::M + 1;
5049

5150
/// The maximum exponent
5251
///
5352
/// Normal numbers < 1 &times; 2<sup>`MAX_EXP`</sup>.
54-
const MAX_EXP: i32 = (1 << E)
53+
const MAX_EXP: i32 = (1 << Self::E)
5554
- Self::B
5655
- match Self::N {
5756
NanStyle::IEEE => 1,
58-
NanStyle::FN => (M == 0) as i32,
57+
NanStyle::FN => (Self::M == 0) as i32,
5958
NanStyle::FNUZ => 0,
6059
};
6160

@@ -73,14 +72,14 @@ pub trait Most8<const E: u32, const M: u32>:
7372
///
7473
/// Equal to floor([`M`][Self::M] log<sub>10</sub>(2))
7574
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
76-
const DIGITS: u32 = (M as f64 * crate::LOG10_2) as u32;
75+
const DIGITS: u32 = (Self::M as f64 * crate::LOG10_2) as u32;
7776

7877
/// Maximum <var>x</var> such that 10<sup>`x`</sup> is normal
7978
///
8079
/// Equal to floor(log<sub>10</sub>([`MAX`][Self::MAX]))
8180
#[allow(clippy::cast_possible_truncation)]
8281
const MAX_10_EXP: i32 = {
83-
let exponent = (1 << E) - Self::B - matches!(Self::N, NanStyle::IEEE) as i32;
82+
let exponent = (1 << Self::E) - Self::B - matches!(Self::N, NanStyle::IEEE) as i32;
8483
let precision = M + !matches!(Self::N, NanStyle::FN) as u32;
8584
let log2_max = exponent as f64 + crate::LOG2_SIGNIFICAND[precision as usize];
8685
(log2_max * crate::LOG10_2) as i32
@@ -126,7 +125,7 @@ pub trait Most8<const E: u32, const M: u32>:
126125
const MIN: Self;
127126

128127
/// Magnitude mask for internal usage
129-
const ABS_MASK: u8 = (1 << (E + M)) - 1;
128+
const ABS_MASK: u8 = (1 << (Self::E + Self::M)) - 1;
130129

131130
/// Raw transmutation from `u8`
132131
#[must_use]
@@ -198,8 +197,8 @@ pub trait Most8<const E: u32, const M: u32>:
198197
} else if self.is_infinite() {
199198
core::num::FpCategory::Infinite
200199
} else {
201-
let exp_mask = ((1 << E) - 1) << M;
202-
let man_mask = (1 << M) - 1;
200+
let exp_mask = ((1 << Self::E) - 1) << Self::M;
201+
let man_mask = (1 << Self::M) - 1;
203202

204203
match (self.to_bits() & exp_mask, self.to_bits() & man_mask) {
205204
(0, 0) => core::num::FpCategory::Zero,
@@ -221,13 +220,13 @@ pub trait Most8<const E: u32, const M: u32>:
221220
/// Check if the sign bit is clear
222221
#[must_use]
223222
fn is_sign_positive(self) -> bool {
224-
self.to_bits() >> (E + M) & 1 == 0
223+
self.to_bits() >> (Self::E + Self::M) & 1 == 0
225224
}
226225

227226
/// Check if the sign bit is set
228227
#[must_use]
229228
fn is_sign_negative(self) -> bool {
230-
self.to_bits() >> (E + M) & 1 == 1
229+
self.to_bits() >> (Self::E + Self::M) & 1 == 1
231230
}
232231

233232
/// Probably lossy conversion from [`f32`]
@@ -238,17 +237,17 @@ pub trait Most8<const E: u32, const M: u32>:
238237
#[allow(clippy::cast_possible_wrap)]
239238
fn from_f32(x: f32) -> Self {
240239
if x.is_nan() {
241-
let sign_bit = u8::from(x.is_sign_negative()) << (E + M);
240+
let sign_bit = u8::from(x.is_sign_negative()) << (Self::E + Self::M);
242241
return Self::from_bits(Self::NAN.to_bits() | sign_bit);
243242
}
244243

245244
let bits = crate::round_f32_to_precision::<M>(x).to_bits();
246-
let sign_bit = ((bits >> 31) as u8) << (E + M);
247-
let diff = (Self::MIN_EXP - f32::MIN_EXP) << M;
248-
let magnitude = bits << 1 >> (f32::MANTISSA_DIGITS - M);
245+
let sign_bit = ((bits >> 31) as u8) << (Self::E + Self::M);
246+
let diff = (Self::MIN_EXP - f32::MIN_EXP) << Self::M;
247+
let magnitude = bits << 1 >> (f32::MANTISSA_DIGITS - Self::M);
249248
let magnitude = magnitude as i32 - diff;
250249

251-
if magnitude < 1 << M {
250+
if magnitude < 1 << Self::M {
252251
let ticks =
253252
f64::from(x.abs()) * crate::exp2i(Self::MANTISSA_DIGITS as i32 - Self::MIN_EXP);
254253
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
@@ -270,17 +269,17 @@ pub trait Most8<const E: u32, const M: u32>:
270269
#[allow(clippy::cast_possible_wrap)]
271270
fn from_f64(x: f64) -> Self {
272271
if x.is_nan() {
273-
let sign_bit = u8::from(x.is_sign_negative()) << (E + M);
272+
let sign_bit = u8::from(x.is_sign_negative()) << (Self::E + Self::M);
274273
return Self::from_bits(Self::NAN.to_bits() | sign_bit);
275274
}
276275

277276
let bits = crate::round_f64_to_precision::<M>(x).to_bits();
278-
let sign_bit = ((bits >> 63) as u8) << (E + M);
279-
let diff = i64::from(Self::MIN_EXP - f64::MIN_EXP) << M;
280-
let magnitude = bits << 1 >> (f64::MANTISSA_DIGITS - M);
277+
let sign_bit = ((bits >> 63) as u8) << (Self::E + Self::M);
278+
let diff = i64::from(Self::MIN_EXP - f64::MIN_EXP) << Self::M;
279+
let magnitude = bits << 1 >> (f64::MANTISSA_DIGITS - Self::M);
281280
let magnitude = magnitude as i64 - diff;
282281

283-
if magnitude < 1 << M {
282+
if magnitude < 1 << Self::M {
284283
let ticks = x.abs() * crate::exp2i(Self::MANTISSA_DIGITS as i32 - Self::MIN_EXP);
285284
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
286285
let ticks = ticks.round_ties_even() as u8;
@@ -304,7 +303,7 @@ pub trait Most8<const E: u32, const M: u32>:
304303
if self.is_infinite() {
305304
return f32::INFINITY * sign;
306305
}
307-
if magnitude < 1 << M {
306+
if magnitude < 1 << Self::M {
308307
#[allow(clippy::cast_possible_wrap)]
309308
let shift = Self::MIN_EXP - Self::MANTISSA_DIGITS as i32;
310309
#[allow(clippy::cast_possible_truncation)]

0 commit comments

Comments
 (0)