Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 8940fbf

Browse files
committed
Remove ExpInt from Float, always use i32 instead
`ExpInt` is likely to only have performance benefits on 16-bit platforms, but makes working with the exponent more difficult. It seems like a worthwhile tradeoff to instead just use `i32`, so do that here.
1 parent 5fb644f commit 8940fbf

File tree

3 files changed

+10
-19
lines changed

3 files changed

+10
-19
lines changed

crates/libm-test/src/f8_impl.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub struct f8(u8);
2020
impl Float for f8 {
2121
type Int = u8;
2222
type SignedInt = i8;
23-
type ExpInt = i8;
2423

2524
const ZERO: Self = Self(0b0_0000_000);
2625
const NEG_ZERO: Self = Self(0b1_0000_000);
@@ -62,10 +61,6 @@ impl Float for f8 {
6261
self.0 & Self::SIGN_MASK != 0
6362
}
6463

65-
fn exp(self) -> Self::ExpInt {
66-
unimplemented!()
67-
}
68-
6964
fn from_bits(a: Self::Int) -> Self {
7065
Self(a)
7166
}

src/math/support/float_traits.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::{fmt, mem, ops};
22

3-
use super::int_traits::{Int, MinInt};
3+
use super::int_traits::{CastInto, Int, MinInt};
44

55
/// Trait for some basic operations on floats
66
#[allow(dead_code)]
@@ -25,9 +25,6 @@ pub trait Float:
2525
/// A int of the same width as the float
2626
type SignedInt: Int + MinInt<OtherSign = Self::Int, Unsigned = Self::Int>;
2727

28-
/// An int capable of containing the exponent bits plus a sign bit. This is signed.
29-
type ExpInt: Int;
30-
3128
const ZERO: Self;
3229
const NEG_ZERO: Self;
3330
const ONE: Self;
@@ -98,7 +95,9 @@ pub trait Float:
9895
}
9996

10097
/// Returns the exponent, not adjusting for bias.
101-
fn exp(self) -> Self::ExpInt;
98+
fn exp(self) -> i32 {
99+
((self.to_bits() & Self::EXP_MASK) >> Self::SIG_BITS).cast()
100+
}
102101

103102
/// Returns the significand with no implicit bit (or the "fractional" part)
104103
fn frac(self) -> Self::Int {
@@ -146,15 +145,13 @@ macro_rules! float_impl {
146145
$ty:ident,
147146
$ity:ident,
148147
$sity:ident,
149-
$expty:ident,
150148
$bits:expr,
151149
$significand_bits:expr,
152150
$from_bits:path
153151
) => {
154152
impl Float for $ty {
155153
type Int = $ity;
156154
type SignedInt = $sity;
157-
type ExpInt = $expty;
158155

159156
const ZERO: Self = 0.0;
160157
const NEG_ZERO: Self = -0.0;
@@ -191,9 +188,6 @@ macro_rules! float_impl {
191188
fn is_sign_negative(self) -> bool {
192189
self.is_sign_negative()
193190
}
194-
fn exp(self) -> Self::ExpInt {
195-
((self.to_bits() & Self::EXP_MASK) >> Self::SIG_BITS) as Self::ExpInt
196-
}
197191
fn from_bits(a: Self::Int) -> Self {
198192
Self::from_bits(a)
199193
}
@@ -226,11 +220,11 @@ macro_rules! float_impl {
226220
}
227221

228222
#[cfg(f16_enabled)]
229-
float_impl!(f16, u16, i16, i8, 16, 10, f16::from_bits);
230-
float_impl!(f32, u32, i32, i16, 32, 23, f32_from_bits);
231-
float_impl!(f64, u64, i64, i16, 64, 52, f64_from_bits);
223+
float_impl!(f16, u16, i16, 16, 10, f16::from_bits);
224+
float_impl!(f32, u32, i32, 32, 23, f32_from_bits);
225+
float_impl!(f64, u64, i64, 64, 52, f64_from_bits);
232226
#[cfg(f128_enabled)]
233-
float_impl!(f128, u128, i128, i16, 128, 112, f128::from_bits);
227+
float_impl!(f128, u128, i128, 128, 112, f128::from_bits);
234228

235229
/* FIXME(msrv): vendor some things that are not const stable at our MSRV */
236230

src/math/support/int_traits.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub trait Int:
5555
+ ops::BitAnd<Output = Self>
5656
+ cmp::Ord
5757
+ CastInto<usize>
58+
+ CastInto<i32>
59+
+ CastFrom<i32>
5860
+ CastFrom<u8>
5961
{
6062
fn signed(self) -> OtherSign<Self::Unsigned>;

0 commit comments

Comments
 (0)