Skip to content

Commit 0012af6

Browse files
committed
trait-ize binary_float_op
1 parent 0803d75 commit 0012af6

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed

src/librustc/mir/interpret/value.rs

+14
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ impl<Tag> fmt::Display for Scalar<Tag> {
132132
}
133133
}
134134

135+
impl<Tag> From<Single> for Scalar<Tag> {
136+
#[inline(always)]
137+
fn from(f: Single) -> Self {
138+
Scalar::from_f32(f)
139+
}
140+
}
141+
142+
impl<Tag> From<Double> for Scalar<Tag> {
143+
#[inline(always)]
144+
fn from(f: Double) -> Self {
145+
Scalar::from_f64(f)
146+
}
147+
}
148+
135149
impl<'tcx> Scalar<()> {
136150
#[inline(always)]
137151
fn check_data(data: u128, size: u8) {

src/librustc_mir/interpret/operator.rs

+35-44
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
4343
bin_op: mir::BinOp,
4444
l: char,
4545
r: char,
46-
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool)> {
46+
) -> (Scalar<M::PointerTag>, bool) {
4747
use rustc::mir::BinOp::*;
4848

4949
let res = match bin_op {
@@ -55,15 +55,15 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
5555
Ge => l >= r,
5656
_ => bug!("Invalid operation on char: {:?}", bin_op),
5757
};
58-
return Ok((Scalar::from_bool(res), false));
58+
return (Scalar::from_bool(res), false);
5959
}
6060

6161
fn binary_bool_op(
6262
&self,
6363
bin_op: mir::BinOp,
6464
l: bool,
6565
r: bool,
66-
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool)> {
66+
) -> (Scalar<M::PointerTag>, bool) {
6767
use rustc::mir::BinOp::*;
6868

6969
let res = match bin_op {
@@ -78,44 +78,32 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
7878
BitXor => l ^ r,
7979
_ => bug!("Invalid operation on bool: {:?}", bin_op),
8080
};
81-
return Ok((Scalar::from_bool(res), false));
81+
return (Scalar::from_bool(res), false);
8282
}
8383

84-
fn binary_float_op(
84+
fn binary_float_op<F: Float + Into<Scalar<M::PointerTag>>>(
8585
&self,
8686
bin_op: mir::BinOp,
87-
fty: FloatTy,
88-
// passing in raw bits
89-
l: u128,
90-
r: u128,
91-
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool)> {
87+
l: F,
88+
r: F,
89+
) -> (Scalar<M::PointerTag>, bool) {
9290
use rustc::mir::BinOp::*;
9391

94-
macro_rules! float_math {
95-
($ty:path, $from_float:ident) => {{
96-
let l = <$ty>::from_bits(l);
97-
let r = <$ty>::from_bits(r);
98-
let val = match bin_op {
99-
Eq => Scalar::from_bool(l == r),
100-
Ne => Scalar::from_bool(l != r),
101-
Lt => Scalar::from_bool(l < r),
102-
Le => Scalar::from_bool(l <= r),
103-
Gt => Scalar::from_bool(l > r),
104-
Ge => Scalar::from_bool(l >= r),
105-
Add => Scalar::$from_float((l + r).value),
106-
Sub => Scalar::$from_float((l - r).value),
107-
Mul => Scalar::$from_float((l * r).value),
108-
Div => Scalar::$from_float((l / r).value),
109-
Rem => Scalar::$from_float((l % r).value),
110-
_ => bug!("invalid float op: `{:?}`", bin_op),
111-
};
112-
return Ok((val, false));
113-
}};
114-
}
115-
match fty {
116-
FloatTy::F32 => float_math!(Single, from_f32),
117-
FloatTy::F64 => float_math!(Double, from_f64),
118-
}
92+
let val = match bin_op {
93+
Eq => Scalar::from_bool(l == r),
94+
Ne => Scalar::from_bool(l != r),
95+
Lt => Scalar::from_bool(l < r),
96+
Le => Scalar::from_bool(l <= r),
97+
Gt => Scalar::from_bool(l > r),
98+
Ge => Scalar::from_bool(l >= r),
99+
Add => (l + r).value.into(),
100+
Sub => (l - r).value.into(),
101+
Mul => (l * r).value.into(),
102+
Div => (l / r).value.into(),
103+
Rem => (l % r).value.into(),
104+
_ => bug!("invalid float op: `{:?}`", bin_op),
105+
};
106+
return (val, false);
119107
}
120108

121109
fn binary_int_op(
@@ -284,21 +272,24 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
284272
match left.layout.ty.sty {
285273
ty::Char => {
286274
assert_eq!(left.layout.ty, right.layout.ty);
287-
let left = left.to_scalar()?.to_char()?;
288-
let right = right.to_scalar()?.to_char()?;
289-
self.binary_char_op(bin_op, left, right)
275+
let left = left.to_scalar()?;
276+
let right = right.to_scalar()?;
277+
Ok(self.binary_char_op(bin_op, left.to_char()?, right.to_char()?))
290278
}
291279
ty::Bool => {
292280
assert_eq!(left.layout.ty, right.layout.ty);
293-
let left = left.to_scalar()?.to_bool()?;
294-
let right = right.to_scalar()?.to_bool()?;
295-
self.binary_bool_op(bin_op, left, right)
281+
let left = left.to_scalar()?;
282+
let right = right.to_scalar()?;
283+
Ok(self.binary_bool_op(bin_op, left.to_bool()?, right.to_bool()?))
296284
}
297285
ty::Float(fty) => {
298286
assert_eq!(left.layout.ty, right.layout.ty);
299-
let left = left.to_bits()?;
300-
let right = right.to_bits()?;
301-
self.binary_float_op(bin_op, fty, left, right)
287+
let left = left.to_scalar()?;
288+
let right = right.to_scalar()?;
289+
Ok(match fty {
290+
FloatTy::F32 => self.binary_float_op(bin_op, left.to_f32()?, right.to_f32()?),
291+
FloatTy::F64 => self.binary_float_op(bin_op, left.to_f64()?, right.to_f64()?),
292+
})
302293
}
303294
_ => {
304295
// Must be integer(-like) types. Don't forget about == on fn pointers.

0 commit comments

Comments
 (0)