Skip to content

Commit

Permalink
Compiler checks correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Aug 8, 2023
1 parent 146c075 commit f33f672
Show file tree
Hide file tree
Showing 5 changed files with 2,473 additions and 1 deletion.
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_ssa/src/traits/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ pub trait BaseTypeMethods<'tcx>: Backend<'tcx> {
fn type_i128(&self) -> Self::Type;
fn type_isize(&self) -> Self::Type;

fn type_f16(&self) -> Self::Type;
fn type_f32(&self) -> Self::Type;
fn type_f64(&self) -> Self::Type;
fn type_f128(&self) -> Self::Type;

fn type_array(&self, ty: Self::Type, len: u64) -> Self::Type;
fn type_func(&self, args: &[Self::Type], ret: Self::Type) -> Self::Type;
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,12 @@ fn lint_literal<'tcx>(
ty::Float(t) => {
let is_infinite = match lit.node {
ast::LitKind::Float(v, _) => match t {
// v.as_str().parse().map(f16::is_infinite)
ty::FloatTy::F16 => todo!("f16::is_infinite"),
ty::FloatTy::F32 => v.as_str().parse().map(f32::is_infinite),
ty::FloatTy::F64 => v.as_str().parse().map(f64::is_infinite),
// v.as_str().parse().map(f128::is_infinite),
ty::FloatTy::F128 => todo!("f128::is_infinite"),
},
_ => bug!(),
};
Expand Down
72 changes: 71 additions & 1 deletion compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::build::expr::as_place::PlaceBuilder;
use crate::build::scope::DropKind;
use rustc_apfloat::ieee::{Double, Single};
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
use rustc_apfloat::Float;
use rustc_ast::attr;
use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -967,13 +967,60 @@ fn parse_float_into_constval<'tcx>(
parse_float_into_scalar(num, float_ty, neg).map(ConstValue::Scalar)
}

// #[cfg(not(bootstrap))]
// fn parse_f16(num: &str) -> Option<f16> {
// num.parse().ok()
// }

// FIXME: bootstrap `f16` parsing via `f32`
// #[cfg(bootstrap)]
fn parse_f16(num: &str) -> Option<f32> {
num.parse().ok()
}

// #[cfg(not(bootstrap))]
// fn parse_f128(num: &str) -> Option<f128> {
// num.parse().ok()
// }

// FIXME: bootstrap `f16` parsing via `f32`
// #[cfg(bootstrap)]
fn parse_f128(num: &str) -> Option<f64> {
num.parse().ok()
}

pub(crate) fn parse_float_into_scalar(
num: Symbol,
float_ty: ty::FloatTy,
neg: bool,
) -> Option<Scalar> {
let num = num.as_str();

match float_ty {
ty::FloatTy::F16 => {
let rust_f = parse_f16(num)?;

let mut f = num
.parse::<Half>()
.unwrap_or_else(|e| panic!("apfloat::ieee::Half failed to parse `{num}`: {e:?}"));

assert!(
u128::from(rust_f.to_bits()) == f.to_bits(),
"apfloat::ieee::Half gave different result for `{}`: \
{}({:#x}) vs Rust's {}({:#x})",
rust_f,
f,
f.to_bits(),
Half::from_bits(rust_f.to_bits().into()),
rust_f.to_bits()
);

if neg {
f = -f;
}

Some(Scalar::from_f16(f))
}
ty::FloatTy::F32 => {
let Ok(rust_f) = num.parse::<f32>() else { return None };
let mut f = num
Expand Down Expand Up @@ -1020,6 +1067,29 @@ pub(crate) fn parse_float_into_scalar(

Some(Scalar::from_f64(f))
}
ty::FloatTy::F128 => {
let rust_f = parse_f128(num)?;
let mut f = num
.parse::<Quad>()
.unwrap_or_else(|e| panic!("apfloat::ieee::Quad failed to parse `{num}`: {e:?}"));

assert!(
u128::from(rust_f.to_bits()) == f.to_bits(),
"apfloat::ieee::Quad gave different result for `{}`: \
{}({:#x}) vs Rust's {}({:#x})",
rust_f,
f,
f.to_bits(),
Quad::from_bits(rust_f.to_bits().into()),
rust_f.to_bits()
);

if neg {
f = -f;
}

Some(Scalar::from_f128(f))
}
}
}

Expand Down
Loading

0 comments on commit f33f672

Please sign in to comment.