Skip to content

Commit

Permalink
rustc_const_{math,eval}: use APFloat in ConstFloat.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jul 30, 2017
1 parent 5b9b0d2 commit 8b2db58
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 123 deletions.
55 changes: 27 additions & 28 deletions src/librustc_const_eval/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use rustc::util::nodemap::DefIdMap;

use syntax::abi::Abi;
use syntax::ast;
use syntax::attr;
use rustc::hir::{self, Expr};
use syntax_pos::Span;

Expand Down Expand Up @@ -560,8 +561,15 @@ fn cast_const_int<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
ty::TyUint(ast::UintTy::Us) => {
Ok(Integral(Usize(ConstUsize::new_truncating(v, tcx.sess.target.uint_type))))
},
ty::TyFloat(ast::FloatTy::F64) => Ok(Float(F64(val.to_f64()))),
ty::TyFloat(ast::FloatTy::F32) => Ok(Float(F32(val.to_f32()))),
ty::TyFloat(fty) => {
if let Some(i) = val.to_u128() {
Ok(Float(ConstFloat::from_u128(i, fty)))
} else {
// The value must be negative, go through signed integers.
let i = val.to_u128_unchecked() as i128;
Ok(Float(ConstFloat::from_i128(i, fty)))
}
}
ty::TyRawPtr(_) => Err(ErrKind::UnimplementedConstVal("casting an address to a raw ptr")),
ty::TyChar => match val {
U8(u) => Ok(Char(u as char)),
Expand All @@ -574,30 +582,25 @@ fn cast_const_int<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
fn cast_const_float<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
val: ConstFloat,
ty: Ty<'tcx>) -> CastResult<'tcx> {
let int_width = |ty| {
ty::layout::Integer::from_attr(tcx, ty).size().bits() as usize
};
match ty.sty {
ty::TyInt(_) | ty::TyUint(_) => {
let i = match val {
F32(f) if f >= 0.0 => U128(f as u128),
F64(f) if f >= 0.0 => U128(f as u128),

F32(f) => I128(f as i128),
F64(f) => I128(f as i128)
};

if let (I128(_), &ty::TyUint(_)) = (i, &ty.sty) {
return Err(CannotCast);
ty::TyInt(ity) => {
if let Some(i) = val.to_i128(int_width(attr::SignedInt(ity))) {
cast_const_int(tcx, I128(i), ty)
} else {
Err(CannotCast)
}
}
ty::TyUint(uty) => {
if let Some(i) = val.to_u128(int_width(attr::UnsignedInt(uty))) {
cast_const_int(tcx, U128(i), ty)
} else {
Err(CannotCast)
}

cast_const_int(tcx, i, ty)
}
ty::TyFloat(ast::FloatTy::F64) => Ok(Float(F64(match val {
F32(f) => f as f64,
F64(f) => f
}))),
ty::TyFloat(ast::FloatTy::F32) => Ok(Float(F32(match val {
F64(f) => f as f32,
F32(f) => f
}))),
ty::TyFloat(fty) => Ok(Float(val.convert(fty))),
_ => Err(CannotCast),
}
}
Expand Down Expand Up @@ -691,11 +694,7 @@ fn lit_to_const<'a, 'tcx>(lit: &ast::LitKind,

fn parse_float<'tcx>(num: &str, fty: ast::FloatTy)
-> Result<ConstFloat, ErrKind<'tcx>> {
let val = match fty {
ast::FloatTy::F32 => num.parse::<f32>().map(F32),
ast::FloatTy::F64 => num.parse::<f64>().map(F64)
};
val.map_err(|_| {
ConstFloat::from_str(num, fty).map_err(|_| {
// FIXME(#31407) this is only necessary because float parsing is buggy
UnimplementedConstVal("could not evaluate float literal (see issue #31407)")
})
Expand Down
179 changes: 129 additions & 50 deletions src/librustc_const_math/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
// except according to those terms.

use std::cmp::Ordering;
use std::hash;
use std::mem::transmute;
use std::num::ParseFloatError;

use syntax::ast;

use super::apfloat::{Float, IeeeSingle, IeeeDouble, OpStatus, Round};
use super::err::*;

#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
// Note that equality for `ConstFloat` means that the it is the same
// constant, not that the rust values are equal. In particular, `NaN
// == NaN` (at least if it's the same NaN; distinct encodings for NaN
// are considering unequal).
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, RustcEncodable, RustcDecodable)]
pub enum ConstFloat {
F32(f32),
F64(f64)
F32(u32),
F64(u64)
}
pub use self::ConstFloat::*;
use self::ConstFloat::*;

impl ConstFloat {
/// Description of the type, not the value
Expand All @@ -32,68 +38,133 @@ impl ConstFloat {

pub fn is_nan(&self) -> bool {
match *self {
F32(f) => f.is_nan(),
F64(f) => f.is_nan(),
F32(f) => IeeeSingle::from_bits(f as u128).is_nan(),
F64(f) => IeeeDouble::from_bits(f as u128).is_nan(),
}
}

/// Compares the values if they are of the same type
pub fn try_cmp(self, rhs: Self) -> Result<Ordering, ConstMathErr> {
match (self, rhs) {
(F64(a), F64(b)) => {
let a = IeeeDouble::from_bits(a as u128);
let b = IeeeDouble::from_bits(b as u128);
// This is pretty bad but it is the existing behavior.
Ok(if a == b {
Ordering::Equal
} else if a < b {
Ordering::Less
} else {
Ordering::Greater
})
Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
}

(F32(a), F32(b)) => {
Ok(if a == b {
Ordering::Equal
} else if a < b {
Ordering::Less
} else {
Ordering::Greater
})
let a = IeeeSingle::from_bits(a as u128);
let b = IeeeSingle::from_bits(b as u128);
Ok(a.partial_cmp(&b).unwrap_or(Ordering::Greater))
}

_ => Err(CmpBetweenUnequalTypes),
}
}
}

/// Note that equality for `ConstFloat` means that the it is the same
/// constant, not that the rust values are equal. In particular, `NaN
/// == NaN` (at least if it's the same NaN; distinct encodings for NaN
/// are considering unequal).
impl PartialEq for ConstFloat {
fn eq(&self, other: &Self) -> bool {
match (*self, *other) {
(F64(a), F64(b)) => {
unsafe{transmute::<_,u64>(a) == transmute::<_,u64>(b)}
pub fn from_i128(input: i128, fty: ast::FloatTy) -> Self {
match fty {
ast::FloatTy::F32 => {
let (r, _) = IeeeSingle::from_i128(input, Round::NearestTiesToEven);
F32(r.to_bits() as u32)
}
(F32(a), F32(b)) => {
unsafe{transmute::<_,u32>(a) == transmute::<_,u32>(b)}
ast::FloatTy::F64 => {
let (r, _) = IeeeDouble::from_i128(input, Round::NearestTiesToEven);
F64(r.to_bits() as u64)
}
_ => false
}
}
}

impl Eq for ConstFloat {}
pub fn from_u128(input: u128, fty: ast::FloatTy) -> Self {
match fty {
ast::FloatTy::F32 => {
let (r, _) = IeeeSingle::from_u128(input, Round::NearestTiesToEven);
F32(r.to_bits() as u32)
}
ast::FloatTy::F64 => {
let (r, _) = IeeeDouble::from_u128(input, Round::NearestTiesToEven);
F64(r.to_bits() as u64)
}
}
}

impl hash::Hash for ConstFloat {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
match *self {
F64(a) => {
unsafe { transmute::<_,u64>(a) }.hash(state)
pub fn from_str(num: &str, fty: ast::FloatTy) -> Result<Self, ParseFloatError> {
match fty {
ast::FloatTy::F32 => {
let rust_bits = num.parse::<f32>()?.to_bits();
let apfloat = num.parse::<IeeeSingle>().unwrap_or_else(|e| {
panic!("apfloat::IeeeSingle failed to parse `{}`: {:?}", num, e);
});
let apfloat_bits = apfloat.to_bits() as u32;
assert!(rust_bits == apfloat_bits,
"apfloat::IeeeSingle gave different result for `{}`: \
{}({:#x}) vs Rust's {}({:#x})", num,
F32(apfloat_bits), apfloat_bits, F32(rust_bits), rust_bits);
Ok(F32(apfloat_bits))
}
ast::FloatTy::F64 => {
let rust_bits = num.parse::<f64>()?.to_bits();
let apfloat = num.parse::<IeeeDouble>().unwrap_or_else(|e| {
panic!("apfloat::IeeeDouble failed to parse `{}`: {:?}", num, e);
});
let apfloat_bits = apfloat.to_bits() as u64;
assert!(rust_bits == apfloat_bits,
"apfloat::IeeeDouble gave different result for `{}`: \
{}({:#x}) vs Rust's {}({:#x})", num,
F64(apfloat_bits), apfloat_bits, F64(rust_bits), rust_bits);
Ok(F64(apfloat_bits))
}
}
}

pub fn to_i128(self, width: usize) -> Option<i128> {
assert!(width <= 128);
let (r, fs) = match self {
F32(f) => {
IeeeSingle::from_bits(f as u128).to_i128(width, Round::TowardZero, &mut true)
}
F64(f) => {
IeeeDouble::from_bits(f as u128).to_i128(width, Round::TowardZero, &mut true)
}
};
if fs.intersects(OpStatus::INVALID_OP) {
None
} else {
Some(r)
}
}

pub fn to_u128(self, width: usize) -> Option<u128> {
assert!(width <= 128);
let (r, fs) = match self {
F32(f) => {
IeeeSingle::from_bits(f as u128).to_u128(width, Round::TowardZero, &mut true)
}
F64(f) => {
IeeeDouble::from_bits(f as u128).to_u128(width, Round::TowardZero, &mut true)
}
};
if fs.intersects(OpStatus::INVALID_OP) {
None
} else {
Some(r)
}
}

pub fn convert(self, fty: ast::FloatTy) -> Self {
match (self, fty) {
(F32(f), ast::FloatTy::F32) => F32(f),
(F64(f), ast::FloatTy::F64) => F64(f),
(F32(f), ast::FloatTy::F64) => {
let from = IeeeSingle::from_bits(f as u128);
let (to, _) = from.convert(Round::NearestTiesToEven, &mut false);
F64(IeeeDouble::to_bits(to) as u64)
}
F32(a) => {
unsafe { transmute::<_,u32>(a) }.hash(state)
(F64(f), ast::FloatTy::F32) => {
let from = IeeeDouble::from_bits(f as u128);
let (to, _) = from.convert(Round::NearestTiesToEven, &mut false);
F32(IeeeSingle::to_bits(to) as u32)
}
}
}
Expand All @@ -102,8 +173,8 @@ impl hash::Hash for ConstFloat {
impl ::std::fmt::Display for ConstFloat {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
match *self {
F32(f) => write!(fmt, "{}f32", f),
F64(f) => write!(fmt, "{}f64", f),
F32(f) => write!(fmt, "{}f32", IeeeSingle::from_bits(f as u128)),
F64(f) => write!(fmt, "{}f64", IeeeDouble::from_bits(f as u128)),
}
}
}
Expand All @@ -114,8 +185,16 @@ macro_rules! derive_binop {
type Output = Result<Self, ConstMathErr>;
fn $func(self, rhs: Self) -> Result<Self, ConstMathErr> {
match (self, rhs) {
(F32(a), F32(b)) => Ok(F32(a.$func(b))),
(F64(a), F64(b)) => Ok(F64(a.$func(b))),
(F32(a), F32(b)) =>{
let a = IeeeSingle::from_bits(a as u128);
let b = IeeeSingle::from_bits(b as u128);
Ok(F32(a.$func(b).to_bits() as u32))
}
(F64(a), F64(b)) => {
let a = IeeeDouble::from_bits(a as u128);
let b = IeeeDouble::from_bits(b as u128);
Ok(F64(a.$func(b).to_bits() as u64))
}
_ => Err(UnequalTypes(Op::$op)),
}
}
Expand All @@ -133,8 +212,8 @@ impl ::std::ops::Neg for ConstFloat {
type Output = Self;
fn neg(self) -> Self {
match self {
F32(f) => F32(-f),
F64(f) => F64(-f),
F32(f) => F32((-IeeeSingle::from_bits(f as u128)).to_bits() as u32),
F64(f) => F64((-IeeeDouble::from_bits(f as u128)).to_bits() as u64),
}
}
}
42 changes: 0 additions & 42 deletions src/librustc_const_math/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,48 +211,6 @@ impl ConstInt {
}
}

pub fn to_f32(self) -> f32 {
match self {
I8(i) => i as f32,
I16(i) => i as f32,
I32(i) => i as f32,
I64(i) => i as f32,
I128(i) => i as f32,
Isize(Is16(i)) => i as f32,
Isize(Is32(i)) => i as f32,
Isize(Is64(i)) => i as f32,
U8(i) => i as f32,
U16(i) => i as f32,
U32(i) => i as f32,
U64(i) => i as f32,
U128(i) => i as f32,
Usize(Us16(i)) => i as f32,
Usize(Us32(i)) => i as f32,
Usize(Us64(i)) => i as f32,
}
}

pub fn to_f64(self) -> f64 {
match self {
I8(i) => i as f64,
I16(i) => i as f64,
I32(i) => i as f64,
I64(i) => i as f64,
I128(i) => i as f64,
Isize(Is16(i)) => i as f64,
Isize(Is32(i)) => i as f64,
Isize(Is64(i)) => i as f64,
U8(i) => i as f64,
U16(i) => i as f64,
U32(i) => i as f64,
U64(i) => i as f64,
U128(i) => i as f64,
Usize(Us16(i)) => i as f64,
Usize(Us32(i)) => i as f64,
Usize(Us64(i)) => i as f64,
}
}

pub fn is_negative(&self) -> bool {
match *self {
I8(v) => v < 0,
Expand Down
Loading

0 comments on commit 8b2db58

Please sign in to comment.