Skip to content

Commit 35460d4

Browse files
committed
Use a fallback for f16 float format/parse if the type is unreliable
In order to avoid crashes when compiling with Cranelift or on targets where f16 is not well supported, choose to use a fallback based on `target_has_reliable_f16`.
1 parent 5b2b504 commit 35460d4

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

library/core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ check-cfg = [
3636
# and to stdarch `core_arch` crate which messes-up with Cargo list
3737
# of declared features, we therefor expect any feature cfg
3838
'cfg(feature, values(any()))',
39+
# Internal features aren't marked known config by default
40+
'cfg(target_has_reliable_f16)',
3941
]

library/core/src/fmt/float.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,59 @@ macro_rules! floating {
230230
};
231231
}
232232

233-
floating! { f16 f32 f64 }
233+
floating! { f32 f64 }
234+
235+
#[cfg(bootstrap)]
236+
#[stable(feature = "rust1", since = "1.0.0")]
237+
impl Debug for f16 {
238+
#[inline]
239+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
240+
write!(f, "{:#06x}", self.to_bits())
241+
}
242+
}
243+
244+
// On target
245+
246+
#[cfg(not(bootstrap))]
247+
#[cfg(target_has_reliable_f16)]
248+
floating! { f16 }
249+
250+
#[cfg(not(bootstrap))]
251+
#[cfg(not(target_has_reliable_f16))]
252+
#[stable(feature = "rust1", since = "1.0.0")]
253+
impl Debug for f16 {
254+
#[inline]
255+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
256+
write!(f, "{:#06x}", self.to_bits())
257+
}
258+
}
259+
260+
#[cfg(not(bootstrap))]
261+
#[cfg(not(target_has_reliable_f16))]
262+
#[stable(feature = "rust1", since = "1.0.0")]
263+
impl Display for f16 {
264+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
265+
Debug::fmt(self, fmt)
266+
}
267+
}
268+
269+
#[cfg(not(bootstrap))]
270+
#[cfg(not(target_has_reliable_f16))]
271+
#[stable(feature = "rust1", since = "1.0.0")]
272+
impl LowerExp for f16 {
273+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
274+
Debug::fmt(self, fmt)
275+
}
276+
}
277+
278+
#[cfg(not(bootstrap))]
279+
#[cfg(not(target_has_reliable_f16))]
280+
#[stable(feature = "rust1", since = "1.0.0")]
281+
impl UpperExp for f16 {
282+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
283+
Debug::fmt(self, fmt)
284+
}
285+
}
234286

235287
#[stable(feature = "rust1", since = "1.0.0")]
236288
impl Debug for f128 {

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
//
9696
// Library features:
9797
// tidy-alphabetical-start
98+
#![cfg_attr(not(bootstrap), feature(cfg_target_has_reliable_f16_f128))]
9899
#![feature(array_ptr_get)]
99100
#![feature(asm_experimental_arch)]
100101
#![feature(bigint_helper_methods)]

library/core/src/num/dec2flt/float.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ const fn pow2_to_pow10(a: i64) -> i64 {
218218
res as i64
219219
}
220220

221+
#[cfg(not(bootstrap))]
222+
#[cfg(not(target_has_reliable_f16))]
221223
impl RawFloat for f16 {
222224
type Int = u16;
223225

library/core/src/num/dec2flt/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,22 @@ macro_rules! from_str_float_impl {
172172
};
173173
}
174174

175+
#[cfg(not(bootstrap))]
176+
#[cfg(target_has_reliable_f16)]
175177
from_str_float_impl!(f16);
176178
from_str_float_impl!(f32);
177179
from_str_float_impl!(f64);
178180

181+
#[cfg(not(bootstrap))]
182+
#[cfg(not(target_has_reliable_f16))]
183+
impl FromStr for f16 {
184+
type Err = ParseFloatError;
185+
186+
fn from_str(_src: &str) -> Result<Self, ParseFloatError> {
187+
unimplemented!("requires target_has_reliable_f16")
188+
}
189+
}
190+
179191
/// An error which can be returned when parsing a float.
180192
///
181193
/// This error is used as the error type for the [`FromStr`] implementation

library/core/src/num/flt2dec/decoder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub trait DecodableFloat: RawFloat + Copy {
4545
fn min_pos_norm_value() -> Self;
4646
}
4747

48+
#[cfg(not(bootstrap))]
49+
#[cfg(not(target_has_reliable_f16))]
4850
impl DecodableFloat for f16 {
4951
fn min_pos_norm_value() -> Self {
5052
f16::MIN_POSITIVE

0 commit comments

Comments
 (0)