Skip to content

Commit 4b83156

Browse files
committed
implement floats by running the ops on the host architecture
1 parent a7cc77a commit 4b83156

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

src/interpreter/mod.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
169169
// TODO(solson): Try making const_to_primval instead.
170170
fn const_to_ptr(&mut self, const_val: &const_val::ConstVal) -> EvalResult<'tcx, Pointer> {
171171
use rustc::middle::const_val::ConstVal::*;
172-
use rustc_const_math::{ConstInt, ConstIsize, ConstUsize};
172+
use rustc_const_math::{ConstInt, ConstIsize, ConstUsize, ConstFloat};
173+
use std::mem::transmute;
173174
macro_rules! i2p {
174175
($i:ident, $n:expr) => {{
175176
let ptr = self.memory.allocate($n);
@@ -178,7 +179,15 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
178179
}}
179180
}
180181
match *const_val {
181-
Float(_f) => unimplemented!(),
182+
Float(ConstFloat::F32(f)) => {
183+
let i = unsafe { transmute::<_, u32>(f) };
184+
i2p!(i, 4)
185+
},
186+
Float(ConstFloat::F64(f)) => {
187+
let i = unsafe { transmute::<_, u64>(f) };
188+
i2p!(i, 8)
189+
},
190+
Float(ConstFloat::FInfer{..}) => unreachable!(),
182191
Integral(ConstInt::Infer(_)) => unreachable!(),
183192
Integral(ConstInt::InferSigned(_)) => unreachable!(),
184193
Integral(ConstInt::I8(i)) => i2p!(i, 1),
@@ -824,7 +833,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
824833
}
825834

826835
pub fn read_primval(&mut self, ptr: Pointer, ty: Ty<'tcx>) -> EvalResult<'tcx, PrimVal> {
827-
use syntax::ast::{IntTy, UintTy};
836+
use syntax::ast::{IntTy, UintTy, FloatTy};
837+
use std::mem::transmute;
828838
let val = match (self.memory.pointer_size(), &ty.sty) {
829839
(_, &ty::TyBool) => PrimVal::Bool(self.memory.read_bool(ptr)?),
830840
(_, &ty::TyChar) => {
@@ -848,6 +858,14 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
848858
(_, &ty::TyUint(UintTy::U32)) => PrimVal::U32(self.memory.read_uint(ptr, 4)? as u32),
849859
(8, &ty::TyUint(UintTy::Us)) |
850860
(_, &ty::TyUint(UintTy::U64)) => PrimVal::U64(self.memory.read_uint(ptr, 8)? as u64),
861+
(_, &ty::TyFloat(FloatTy::F32)) => {
862+
let i = self.memory.read_uint(ptr, 4)? as u32;
863+
PrimVal::F32(unsafe { transmute(i) })
864+
},
865+
(_, &ty::TyFloat(FloatTy::F64)) => {
866+
let i = self.memory.read_uint(ptr, 8)?;
867+
PrimVal::F64(unsafe { transmute(i) })
868+
},
851869

852870
(_, &ty::TyFnDef(def_id, substs, fn_ty)) => {
853871
PrimVal::FnPtr(self.memory.create_fn_ptr(def_id, substs, fn_ty))

src/memory.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
394394
}
395395

396396
pub fn write_primval(&mut self, ptr: Pointer, val: PrimVal) -> EvalResult<'tcx, ()> {
397+
use std::mem::transmute;
397398
let pointer_size = self.pointer_size();
398399
match val {
399400
PrimVal::Bool(b) => self.write_bool(ptr, b),
@@ -407,6 +408,8 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
407408
PrimVal::U64(n) => self.write_uint(ptr, n as u64, 8),
408409
PrimVal::Char(c) => self.write_uint(ptr, c as u64, 4),
409410
PrimVal::IntegerPtr(n) => self.write_uint(ptr, n as u64, pointer_size),
411+
PrimVal::F32(f) => self.write_uint(ptr, unsafe { transmute::<_, u32>(f) } as u64, 4),
412+
PrimVal::F64(f) => self.write_uint(ptr, unsafe { transmute::<_, u64>(f) }, 8),
410413
PrimVal::FnPtr(_p) |
411414
PrimVal::AbstractPtr(_p) => unimplemented!(),
412415
}

src/primval.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub enum PrimVal {
1313
FnPtr(Pointer),
1414
IntegerPtr(u64),
1515
Char(char),
16+
17+
F32(f32), F64(f64),
1618
}
1719

1820
/// returns the result of the operation and whether the operation overflowed
@@ -57,6 +59,34 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
5759
})
5860
}
5961

62+
macro_rules! float_binops {
63+
($v:ident, $l:ident, $r:ident) => ({
64+
match bin_op {
65+
Add => $v($l + $r),
66+
Sub => $v($l - $r),
67+
Mul => $v($l * $r),
68+
Div => $v($l / $r),
69+
Rem => $v($l % $r),
70+
71+
// invalid float ops
72+
BitXor => unreachable!(),
73+
BitAnd => unreachable!(),
74+
BitOr => unreachable!(),
75+
Shl => unreachable!(),
76+
Shr => unreachable!(),
77+
78+
// directly comparing floats is questionable
79+
// miri could forbid it, or at least miri as rust const eval should forbid it
80+
Eq => Bool($l == $r),
81+
Ne => Bool($l != $r),
82+
Lt => Bool($l < $r),
83+
Le => Bool($l <= $r),
84+
Gt => Bool($l > $r),
85+
Ge => Bool($l >= $r),
86+
}
87+
})
88+
}
89+
6090
fn unrelated_ptr_ops<'tcx>(bin_op: mir::BinOp) -> EvalResult<'tcx, PrimVal> {
6191
use rustc::mir::repr::BinOp::*;
6292
match bin_op {
@@ -128,6 +158,8 @@ pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> Eva
128158
(U16(l), U16(r)) => int_binops!(U16, l, r),
129159
(U32(l), U32(r)) => int_binops!(U32, l, r),
130160
(U64(l), U64(r)) => int_binops!(U64, l, r),
161+
(F32(l), F32(r)) => float_binops!(F32, l, r),
162+
(F64(l), F64(r)) => float_binops!(F64, l, r),
131163
(Char(l), Char(r)) => match bin_op {
132164
Eq => Bool(l == r),
133165
Ne => Bool(l != r),
@@ -211,6 +243,9 @@ pub fn unary_op<'tcx>(un_op: mir::UnOp, val: PrimVal) -> EvalResult<'tcx, PrimVa
211243
(Not, U16(n)) => Ok(U16(!n)),
212244
(Not, U32(n)) => Ok(U32(!n)),
213245
(Not, U64(n)) => Ok(U64(!n)),
246+
247+
(Neg, F64(n)) => Ok(F64(-n)),
248+
(Neg, F32(n)) => Ok(F32(-n)),
214249
_ => Err(EvalError::Unimplemented(format!("unimplemented unary op: {:?}, {:?}", un_op, val))),
215250
}
216251
}

tests/run-pass/floats.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
fn main() {
3+
assert_eq!(6.0_f32*6.0_f32, 36.0_f32);
4+
assert_eq!(6.0_f64*6.0_f64, 36.0_f64);
5+
assert_eq!(-{5.0_f32}, -5.0_f32);
6+
assert!((5.0_f32/0.0).is_infinite());
7+
assert!((-5.0_f32).sqrt().is_nan());
8+
}

0 commit comments

Comments
 (0)