Skip to content

Commit 81609be

Browse files
committed
Fix new dead_code warnings from recent nightlies
1 parent b6e15ef commit 81609be

File tree

5 files changed

+116
-103
lines changed

5 files changed

+116
-103
lines changed

libm/src/math/support/float_traits.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::int_traits::{CastFrom, Int, MinInt};
66

77
/// Trait for some basic operations on floats
88
// #[allow(dead_code)]
9+
#[allow(dead_code)] // Some constants are only used with tests
910
pub trait Float:
1011
Copy
1112
+ fmt::Debug

libm/src/math/support/hex_float.rs

Lines changed: 108 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Utilities for working with hex float formats.
22
3-
use core::fmt;
4-
5-
use super::{Float, Round, Status, f32_from_bits, f64_from_bits};
3+
use super::{Round, Status, f32_from_bits, f64_from_bits};
64

75
/// Construct a 16-bit float from hex float representation (C-style)
86
#[cfg(f16_enabled)]
@@ -352,133 +350,143 @@ const fn u128_ilog2(v: u128) -> u32 {
352350
u128::BITS - 1 - v.leading_zeros()
353351
}
354352

355-
/// Format a floating point number as its IEEE hex (`%a`) representation.
356-
pub struct Hexf<F>(pub F);
353+
#[cfg(any(test, feature = "unstable-public-internals"))]
354+
mod hex_fmt {
355+
use core::fmt;
357356

358-
// Adapted from https://github.com/ericseppanen/hexfloat2/blob/a5c27932f0ff/src/format.rs
359-
#[cfg(not(feature = "compiler-builtins"))]
360-
fn fmt_any_hex<F: Float>(x: &F, f: &mut fmt::Formatter<'_>) -> fmt::Result {
361-
if x.is_sign_negative() {
362-
write!(f, "-")?;
363-
}
357+
use crate::support::Float;
364358

365-
if x.is_nan() {
366-
return write!(f, "NaN");
367-
} else if x.is_infinite() {
368-
return write!(f, "inf");
369-
} else if *x == F::ZERO {
370-
return write!(f, "0x0p+0");
371-
}
359+
/// Format a floating point number as its IEEE hex (`%a`) representation.
360+
pub struct Hexf<F>(pub F);
372361

373-
let mut exponent = x.exp_unbiased();
374-
let sig = x.to_bits() & F::SIG_MASK;
375-
376-
let bias = F::EXP_BIAS as i32;
377-
// The mantissa MSB needs to be shifted up to the nearest nibble.
378-
let mshift = (4 - (F::SIG_BITS % 4)) % 4;
379-
let sig = sig << mshift;
380-
// The width is rounded up to the nearest char (4 bits)
381-
let mwidth = (F::SIG_BITS as usize + 3) / 4;
382-
let leading = if exponent == -bias {
383-
// subnormal number means we shift our output by 1 bit.
384-
exponent += 1;
385-
"0."
386-
} else {
387-
"1."
388-
};
362+
// Adapted from https://github.com/ericseppanen/hexfloat2/blob/a5c27932f0ff/src/format.rs
363+
#[cfg(not(feature = "compiler-builtins"))]
364+
pub(super) fn fmt_any_hex<F: Float>(x: &F, f: &mut fmt::Formatter<'_>) -> fmt::Result {
365+
if x.is_sign_negative() {
366+
write!(f, "-")?;
367+
}
389368

390-
write!(f, "0x{leading}{sig:0mwidth$x}p{exponent:+}")
391-
}
369+
if x.is_nan() {
370+
return write!(f, "NaN");
371+
} else if x.is_infinite() {
372+
return write!(f, "inf");
373+
} else if *x == F::ZERO {
374+
return write!(f, "0x0p+0");
375+
}
392376

393-
#[cfg(feature = "compiler-builtins")]
394-
fn fmt_any_hex<F: Float>(_x: &F, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
395-
unimplemented!()
396-
}
377+
let mut exponent = x.exp_unbiased();
378+
let sig = x.to_bits() & F::SIG_MASK;
379+
380+
let bias = F::EXP_BIAS as i32;
381+
// The mantissa MSB needs to be shifted up to the nearest nibble.
382+
let mshift = (4 - (F::SIG_BITS % 4)) % 4;
383+
let sig = sig << mshift;
384+
// The width is rounded up to the nearest char (4 bits)
385+
let mwidth = (F::SIG_BITS as usize + 3) / 4;
386+
let leading = if exponent == -bias {
387+
// subnormal number means we shift our output by 1 bit.
388+
exponent += 1;
389+
"0."
390+
} else {
391+
"1."
392+
};
397393

398-
impl<F: Float> fmt::LowerHex for Hexf<F> {
399-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
400-
cfg_if! {
401-
if #[cfg(feature = "compiler-builtins")] {
402-
let _ = f;
403-
unimplemented!()
404-
} else {
405-
fmt_any_hex(&self.0, f)
394+
write!(f, "0x{leading}{sig:0mwidth$x}p{exponent:+}")
395+
}
396+
397+
#[cfg(feature = "compiler-builtins")]
398+
pub(super) fn fmt_any_hex<F: Float>(_x: &F, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
399+
unimplemented!()
400+
}
401+
402+
impl<F: Float> fmt::LowerHex for Hexf<F> {
403+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
404+
cfg_if! {
405+
if #[cfg(feature = "compiler-builtins")] {
406+
let _ = f;
407+
unimplemented!()
408+
} else {
409+
fmt_any_hex(&self.0, f)
410+
}
406411
}
407412
}
408413
}
409-
}
410414

411-
impl<F: Float> fmt::LowerHex for Hexf<(F, F)> {
412-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
413-
cfg_if! {
414-
if #[cfg(feature = "compiler-builtins")] {
415-
let _ = f;
416-
unimplemented!()
417-
} else {
418-
write!(f, "({:x}, {:x})", Hexf(self.0.0), Hexf(self.0.1))
415+
impl<F: Float> fmt::LowerHex for Hexf<(F, F)> {
416+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
417+
cfg_if! {
418+
if #[cfg(feature = "compiler-builtins")] {
419+
let _ = f;
420+
unimplemented!()
421+
} else {
422+
write!(f, "({:x}, {:x})", Hexf(self.0.0), Hexf(self.0.1))
423+
}
419424
}
420425
}
421426
}
422-
}
423427

424-
impl<F: Float> fmt::LowerHex for Hexf<(F, i32)> {
425-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
426-
cfg_if! {
427-
if #[cfg(feature = "compiler-builtins")] {
428-
let _ = f;
429-
unimplemented!()
430-
} else {
431-
write!(f, "({:x}, {:x})", Hexf(self.0.0), Hexf(self.0.1))
428+
impl<F: Float> fmt::LowerHex for Hexf<(F, i32)> {
429+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
430+
cfg_if! {
431+
if #[cfg(feature = "compiler-builtins")] {
432+
let _ = f;
433+
unimplemented!()
434+
} else {
435+
write!(f, "({:x}, {:x})", Hexf(self.0.0), Hexf(self.0.1))
436+
}
432437
}
433438
}
434439
}
435-
}
436440

437-
impl fmt::LowerHex for Hexf<i32> {
438-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
439-
cfg_if! {
440-
if #[cfg(feature = "compiler-builtins")] {
441-
let _ = f;
442-
unimplemented!()
443-
} else {
444-
fmt::LowerHex::fmt(&self.0, f)
441+
impl fmt::LowerHex for Hexf<i32> {
442+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
443+
cfg_if! {
444+
if #[cfg(feature = "compiler-builtins")] {
445+
let _ = f;
446+
unimplemented!()
447+
} else {
448+
fmt::LowerHex::fmt(&self.0, f)
449+
}
445450
}
446451
}
447452
}
448-
}
449453

450-
impl<T> fmt::Debug for Hexf<T>
451-
where
452-
Hexf<T>: fmt::LowerHex,
453-
{
454-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
455-
cfg_if! {
456-
if #[cfg(feature = "compiler-builtins")] {
457-
let _ = f;
458-
unimplemented!()
459-
} else {
460-
fmt::LowerHex::fmt(self, f)
454+
impl<T> fmt::Debug for Hexf<T>
455+
where
456+
Hexf<T>: fmt::LowerHex,
457+
{
458+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
459+
cfg_if! {
460+
if #[cfg(feature = "compiler-builtins")] {
461+
let _ = f;
462+
unimplemented!()
463+
} else {
464+
fmt::LowerHex::fmt(self, f)
465+
}
461466
}
462467
}
463468
}
464-
}
465469

466-
impl<T> fmt::Display for Hexf<T>
467-
where
468-
Hexf<T>: fmt::LowerHex,
469-
{
470-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
471-
cfg_if! {
472-
if #[cfg(feature = "compiler-builtins")] {
473-
let _ = f;
474-
unimplemented!()
475-
} else {
476-
fmt::LowerHex::fmt(self, f)
470+
impl<T> fmt::Display for Hexf<T>
471+
where
472+
Hexf<T>: fmt::LowerHex,
473+
{
474+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
475+
cfg_if! {
476+
if #[cfg(feature = "compiler-builtins")] {
477+
let _ = f;
478+
unimplemented!()
479+
} else {
480+
fmt::LowerHex::fmt(self, f)
481+
}
477482
}
478483
}
479484
}
480485
}
481486

487+
#[cfg(any(test, feature = "unstable-public-internals"))]
488+
pub use hex_fmt::*;
489+
482490
#[cfg(test)]
483491
mod parse_tests {
484492
extern crate std;
@@ -1064,6 +1072,7 @@ mod print_tests {
10641072
use std::string::ToString;
10651073

10661074
use super::*;
1075+
use crate::support::Float;
10671076

10681077
#[test]
10691078
#[cfg(f16_enabled)]

libm/src/math/support/int_traits.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::{cmp, fmt, ops};
22

33
/// Minimal integer implementations needed on all integer types, including wide integers.
4+
#[allow(dead_code)] // Some constants are only used with tests
45
pub trait MinInt:
56
Copy
67
+ fmt::Debug

libm/src/math/support/macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ macro_rules! hf128 {
137137
#[cfg(test)]
138138
macro_rules! assert_biteq {
139139
($left:expr, $right:expr, $($tt:tt)*) => {{
140-
use $crate::support::Int;
141140
let l = $left;
142141
let r = $right;
143-
let bits = Int::leading_zeros(l.to_bits() - l.to_bits()); // hack to get the width from the value
142+
// hack to get width from a value
143+
let bits = $crate::support::Int::leading_zeros(l.to_bits() - l.to_bits());
144144
assert!(
145-
l.biteq(r),
145+
$crate::support::Float::biteq(l, r),
146146
"{}\nl: {l:?} ({lb:#0width$x})\nr: {r:?} ({rb:#0width$x})",
147147
format_args!($($tt)*),
148148
lb = l.to_bits(),

libm/src/math/support/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ pub use env::{FpResult, Round, Status};
1717
#[allow(unused_imports)]
1818
pub use float_traits::{DFloat, Float, HFloat, IntTy};
1919
pub(crate) use float_traits::{f32_from_bits, f64_from_bits};
20+
#[cfg(any(test, feature = "unstable-public-internals"))]
21+
pub use hex_float::Hexf;
2022
#[cfg(f16_enabled)]
2123
#[allow(unused_imports)]
2224
pub use hex_float::hf16;
2325
#[cfg(f128_enabled)]
2426
#[allow(unused_imports)]
2527
pub use hex_float::hf128;
2628
#[allow(unused_imports)]
27-
pub use hex_float::{Hexf, hf32, hf64};
29+
pub use hex_float::{hf32, hf64};
2830
pub use int_traits::{CastFrom, CastInto, DInt, HInt, Int, MinInt};
2931

3032
/// Hint to the compiler that the current path is cold.

0 commit comments

Comments
 (0)