Skip to content

Commit

Permalink
Merge pull request #1061 from hacspec/add-floats-consts
Browse files Browse the repository at this point in the history
feat(frontend/consts): float lits: use strings not bits, more support
  • Loading branch information
W95Psp authored Oct 29, 2024
2 parents 381bd0f + 42cc4cc commit 5b781f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
30 changes: 21 additions & 9 deletions frontend/exporter/src/constant_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ pub enum ConstantInt {
pub enum ConstantLiteral {
Bool(bool),
Char(char),
// Rust floats do not have the Eq or Ord traits due to the presence of NaN
// We instead store their bit representation, which always fits in a u128
Float(u128, FloatTy),
Float(String, FloatTy),
Int(ConstantInt),
Str(String, StrStyle),
ByteStr(Vec<u8>, StrStyle),
Expand Down Expand Up @@ -173,7 +171,7 @@ mod rustc {
}
}
}
Float(_bits, _ty) => todo!("Converting float literals back to AST"),
Float(f, ty) => LitKind::Float(f, LitFloatType::Suffixed(ty)),
ByteStr(raw, str_style) => LitKind::ByteStr(raw, str_style),
Str(raw, str_style) => LitKind::Str(raw, str_style),
};
Expand Down Expand Up @@ -250,7 +248,10 @@ mod rustc {
let v = x.to_uint(x.size());
ConstantLiteral::Int(ConstantInt::Uint(v, kind.sinto(s)))
}
// https://github.com/rust-lang/rust/pull/116930
ty::Float(kind) => {
let v = x.to_bits_unchecked();
bits_and_type_to_float_constant_literal(v, kind.sinto(s))
}
_ => {
let ty_sinto: Ty = ty.sinto(s);
supposely_unreachable_fatal!(
Expand All @@ -262,6 +263,18 @@ mod rustc {
}
}

/// Converts a bit-representation of a float of type `ty` to a constant literal
fn bits_and_type_to_float_constant_literal(bits: u128, ty: FloatTy) -> ConstantLiteral {
use rustc_apfloat::{ieee, Float};
let string = match &ty {
FloatTy::F16 => ieee::Half::from_bits(bits).to_string(),
FloatTy::F32 => ieee::Single::from_bits(bits).to_string(),
FloatTy::F64 => ieee::Double::from_bits(bits).to_string(),
FloatTy::F128 => ieee::Quad::from_bits(bits).to_string(),
};
ConstantLiteral::Float(string, ty)
}

#[tracing::instrument(level = "trace", skip(s))]
pub(crate) fn scalar_to_constant_expr<'tcx, S: UnderOwnerState<'tcx>>(
s: &S,
Expand Down Expand Up @@ -292,10 +305,9 @@ mod rustc {
scalar
)
});
ConstantExprKind::Literal(ConstantLiteral::Float(
scalar_int.to_bits_unchecked(),
float_type.sinto(s),
))
let data = scalar_int.to_bits_unchecked();
let lit = bits_and_type_to_float_constant_literal(data, float_type.sinto(s));
ConstantExprKind::Literal(lit)
}
ty::Ref(_, inner_ty, Mutability::Not) | ty::RawPtr(inner_ty, Mutability::Mut) => {
let tcx = s.base().tcx;
Expand Down
1 change: 1 addition & 0 deletions frontend/exporter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cfg_feature_rustc! {
extern crate rustc_ast;
extern crate rustc_ast_pretty;
extern crate rustc_attr;
extern crate rustc_apfloat;
extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_errors;
Expand Down

0 comments on commit 5b781f6

Please sign in to comment.