Skip to content

Commit db9163e

Browse files
committed
Add compiler support for parsing f16 and f128
1 parent 89b7830 commit db9163e

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

compiler/rustc_ast/src/util/literal.rs

+2
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,10 @@ fn filtered_float_lit(
276276
Some(suffix) => LitKind::Float(
277277
symbol,
278278
ast::LitFloatType::Suffixed(match suffix {
279+
sym::f16 => ast::FloatTy::F16,
279280
sym::f32 => ast::FloatTy::F32,
280281
sym::f64 => ast::FloatTy::F64,
282+
sym::f128 => ast::FloatTy::F128,
281283
_ => return Err(LitError::InvalidFloatSuffix(suffix)),
282284
}),
283285
),

compiler/rustc_middle/src/mir/interpret/value.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use either::{Either, Left, Right};
44

55
use rustc_apfloat::{
6-
ieee::{Double, Single},
6+
ieee::{Double, Half, Quad, Single},
77
Float,
88
};
99
use rustc_macros::HashStable;
@@ -201,6 +201,11 @@ impl<Prov> Scalar<Prov> {
201201
Self::from_int(i, cx.data_layout().pointer_size)
202202
}
203203

204+
#[inline]
205+
pub fn from_f16(f: Half) -> Self {
206+
Scalar::Int(f.into())
207+
}
208+
204209
#[inline]
205210
pub fn from_f32(f: Single) -> Self {
206211
Scalar::Int(f.into())
@@ -211,6 +216,11 @@ impl<Prov> Scalar<Prov> {
211216
Scalar::Int(f.into())
212217
}
213218

219+
#[inline]
220+
pub fn from_f128(f: Quad) -> Self {
221+
Scalar::Int(f.into())
222+
}
223+
214224
/// This is almost certainly not the method you want! You should dispatch on the type
215225
/// and use `to_{u8,u16,...}`/`scalar_to_ptr` to perform ptr-to-int / int-to-ptr casts as needed.
216226
///

compiler/rustc_middle/src/ty/consts/int.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_apfloat::ieee::{Double, Single};
1+
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
22
use rustc_apfloat::Float;
33
use rustc_errors::{DiagArgValue, IntoDiagnosticArg};
44
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
@@ -450,6 +450,22 @@ impl TryFrom<ScalarInt> for char {
450450
}
451451
}
452452

453+
impl From<Half> for ScalarInt {
454+
#[inline]
455+
fn from(f: Half) -> Self {
456+
// We trust apfloat to give us properly truncated data.
457+
Self { data: f.to_bits(), size: NonZero::new((Half::BITS / 8) as u8).unwrap() }
458+
}
459+
}
460+
461+
impl TryFrom<ScalarInt> for Half {
462+
type Error = Size;
463+
#[inline]
464+
fn try_from(int: ScalarInt) -> Result<Self, Size> {
465+
int.to_bits(Size::from_bytes(2)).map(Self::from_bits)
466+
}
467+
}
468+
453469
impl From<Single> for ScalarInt {
454470
#[inline]
455471
fn from(f: Single) -> Self {
@@ -482,6 +498,22 @@ impl TryFrom<ScalarInt> for Double {
482498
}
483499
}
484500

501+
impl From<Quad> for ScalarInt {
502+
#[inline]
503+
fn from(f: Quad) -> Self {
504+
// We trust apfloat to give us properly truncated data.
505+
Self { data: f.to_bits(), size: NonZero::new((Quad::BITS / 8) as u8).unwrap() }
506+
}
507+
}
508+
509+
impl TryFrom<ScalarInt> for Quad {
510+
type Error = Size;
511+
#[inline]
512+
fn try_from(int: ScalarInt) -> Result<Self, Size> {
513+
int.to_bits(Size::from_bytes(16)).map(Self::from_bits)
514+
}
515+
}
516+
485517
impl fmt::Debug for ScalarInt {
486518
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
487519
// Dispatch to LowerHex below.

compiler/rustc_mir_build/src/build/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::build::expr::as_place::PlaceBuilder;
22
use crate::build::scope::DropKind;
33
use itertools::Itertools;
4-
use rustc_apfloat::ieee::{Double, Single};
4+
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
55
use rustc_apfloat::Float;
66
use rustc_ast::attr;
77
use rustc_data_structures::fx::FxHashMap;
@@ -1053,7 +1053,8 @@ pub(crate) fn parse_float_into_scalar(
10531053
) -> Option<Scalar> {
10541054
let num = num.as_str();
10551055
match float_ty {
1056-
ty::FloatTy::F16 => unimplemented!("f16_f128"),
1056+
// FIXME(f16_f128): When available, compare to the library parser as with `f32` and `f64`
1057+
ty::FloatTy::F16 => num.parse::<Half>().ok().map(Scalar::from_f16),
10571058
ty::FloatTy::F32 => {
10581059
let Ok(rust_f) = num.parse::<f32>() else { return None };
10591060
let mut f = num
@@ -1100,7 +1101,8 @@ pub(crate) fn parse_float_into_scalar(
11001101

11011102
Some(Scalar::from_f64(f))
11021103
}
1103-
ty::FloatTy::F128 => unimplemented!("f16_f128"),
1104+
// FIXME(f16_f128): When available, compare to the library parser as with `f32` and `f64`
1105+
ty::FloatTy::F128 => num.parse::<Quad>().ok().map(Scalar::from_f128),
11041106
}
11051107
}
11061108

0 commit comments

Comments
 (0)