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

Commit b1b30ca

Browse files
committed
Update and slightly refactor some of the Float trait
Add a constant for negative pi and provide a standalone const `from_bits`, which can be combined with what we already had in `hex_float`. Also provide another default method to reduce what needs to be provided by the macro.
1 parent e80dad0 commit b1b30ca

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/math/support/float_traits.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub trait Float:
3838
const MAX: Self;
3939
const MIN: Self;
4040
const PI: Self;
41+
const NEG_PI: Self;
4142
const FRAC_PI_2: Self;
4243

4344
/// The bitwidth of the float type
@@ -71,7 +72,9 @@ pub trait Float:
7172
fn to_bits(self) -> Self::Int;
7273

7374
/// Returns `self` transmuted to `Self::SignedInt`
74-
fn to_bits_signed(self) -> Self::SignedInt;
75+
fn to_bits_signed(self) -> Self::SignedInt {
76+
self.to_bits().signed()
77+
}
7578

7679
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
7780
/// represented in multiple different ways. This method returns `true` if two NaNs are
@@ -158,7 +161,15 @@ pub trait Float:
158161
pub type IntTy<F> = <F as Float>::Int;
159162

160163
macro_rules! float_impl {
161-
($ty:ident, $ity:ident, $sity:ident, $expty:ident, $bits:expr, $significand_bits:expr) => {
164+
(
165+
$ty:ident,
166+
$ity:ident,
167+
$sity:ident,
168+
$expty:ident,
169+
$bits:expr,
170+
$significand_bits:expr,
171+
$from_bits:path
172+
) => {
162173
impl Float for $ty {
163174
type Int = $ity;
164175
type SignedInt = $sity;
@@ -173,13 +184,10 @@ macro_rules! float_impl {
173184
const NAN: Self = Self::NAN;
174185
const MAX: Self = -Self::MIN;
175186
// Sign bit set, saturated mantissa, saturated exponent with last bit zeroed
176-
// FIXME(msrv): just use `from_bits` when available
177-
// SAFETY: POD cast with no preconditions
178-
const MIN: Self = unsafe {
179-
mem::transmute::<Self::Int, Self>(Self::Int::MAX & !(1 << Self::SIG_BITS))
180-
};
187+
const MIN: Self = $from_bits(Self::Int::MAX & !(1 << Self::SIG_BITS));
181188

182189
const PI: Self = core::$ty::consts::PI;
190+
const NEG_PI: Self = -Self::PI;
183191
const FRAC_PI_2: Self = core::$ty::consts::FRAC_PI_2;
184192

185193
const BITS: u32 = $bits;
@@ -193,9 +201,6 @@ macro_rules! float_impl {
193201
fn to_bits(self) -> Self::Int {
194202
self.to_bits()
195203
}
196-
fn to_bits_signed(self) -> Self::SignedInt {
197-
self.to_bits() as Self::SignedInt
198-
}
199204
fn is_nan(self) -> bool {
200205
self.is_nan()
201206
}
@@ -220,8 +225,22 @@ macro_rules! float_impl {
220225
}
221226

222227
#[cfg(f16_enabled)]
223-
float_impl!(f16, u16, i16, i8, 16, 10);
224-
float_impl!(f32, u32, i32, i16, 32, 23);
225-
float_impl!(f64, u64, i64, i16, 64, 52);
228+
float_impl!(f16, u16, i16, i8, 16, 10, f16::from_bits);
229+
float_impl!(f32, u32, i32, i16, 32, 23, f32_from_bits);
230+
float_impl!(f64, u64, i64, i16, 64, 52, f64_from_bits);
226231
#[cfg(f128_enabled)]
227-
float_impl!(f128, u128, i128, i16, 128, 112);
232+
float_impl!(f128, u128, i128, i16, 128, 112, f128::from_bits);
233+
234+
/* FIXME(msrv): vendor some things that are not const stable at our MSRV */
235+
236+
/// `f32::from_bits`
237+
pub const fn f32_from_bits(bits: u32) -> f32 {
238+
// SAFETY: POD cast with no preconditions
239+
unsafe { mem::transmute::<u32, f32>(bits) }
240+
}
241+
242+
/// `f64::from_bits`
243+
pub const fn f64_from_bits(bits: u64) -> f64 {
244+
// SAFETY: POD cast with no preconditions
245+
unsafe { mem::transmute::<u64, f64>(bits) }
246+
}

src/math/support/hex_float.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
#![allow(dead_code)] // FIXME: remove once this gets used
44

5+
use super::{f32_from_bits, f64_from_bits};
6+
57
/// Construct a 32-bit float from hex float representation (C-style)
68
pub const fn hf32(s: &str) -> f32 {
79
f32_from_bits(parse_any(s, 32, 23) as u32)
@@ -159,16 +161,6 @@ const fn hex_digit(c: u8) -> u8 {
159161

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

162-
/// `f32::from_bits`
163-
const fn f32_from_bits(v: u32) -> f32 {
164-
unsafe { core::mem::transmute(v) }
165-
}
166-
167-
/// `f64::from_bits`
168-
const fn f64_from_bits(v: u64) -> f64 {
169-
unsafe { core::mem::transmute(v) }
170-
}
171-
172164
/// `u128::ilog2`
173165
const fn u128_ilog2(v: u128) -> u32 {
174166
assert!(v != 0);

src/math/support/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod int_traits;
66

77
#[allow(unused_imports)]
88
pub use float_traits::{Float, IntTy};
9+
pub(crate) use float_traits::{f32_from_bits, f64_from_bits};
910
#[allow(unused_imports)]
1011
pub use hex_float::{hf32, hf64};
1112
pub use int_traits::{CastFrom, CastInto, DInt, HInt, Int, MinInt};

0 commit comments

Comments
 (0)