Rename bits to bytes for consistency.
This commit is contained in:
parent
4fe41ad8d5
commit
6b7c68bec2
@ -9,11 +9,11 @@ use value::{
|
||||
PrimVal,
|
||||
PrimValKind,
|
||||
Value,
|
||||
bits_to_f32,
|
||||
bits_to_f64,
|
||||
f32_to_bits,
|
||||
f64_to_bits,
|
||||
bits_to_bool,
|
||||
bytes_to_f32,
|
||||
bytes_to_f64,
|
||||
f32_to_bytes,
|
||||
f64_to_bytes,
|
||||
bytes_to_bool,
|
||||
};
|
||||
|
||||
impl<'a, 'tcx> EvalContext<'a, 'tcx> {
|
||||
@ -108,23 +108,23 @@ macro_rules! int_shift {
|
||||
}
|
||||
|
||||
macro_rules! float_arithmetic {
|
||||
($from_bits:ident, $to_bits:ident, $float_op:tt, $l:expr, $r:expr) => ({
|
||||
let l = $from_bits($l);
|
||||
let r = $from_bits($r);
|
||||
let bits = $to_bits(l $float_op r);
|
||||
PrimVal::Bytes(bits)
|
||||
($from_bytes:ident, $to_bytes:ident, $float_op:tt, $l:expr, $r:expr) => ({
|
||||
let l = $from_bytes($l);
|
||||
let r = $from_bytes($r);
|
||||
let bytes = $to_bytes(l $float_op r);
|
||||
PrimVal::Bytes(bytes)
|
||||
})
|
||||
}
|
||||
|
||||
macro_rules! f32_arithmetic {
|
||||
($float_op:tt, $l:expr, $r:expr) => (
|
||||
float_arithmetic!(bits_to_f32, f32_to_bits, $float_op, $l, $r)
|
||||
float_arithmetic!(bytes_to_f32, f32_to_bytes, $float_op, $l, $r)
|
||||
)
|
||||
}
|
||||
|
||||
macro_rules! f64_arithmetic {
|
||||
($float_op:tt, $l:expr, $r:expr) => (
|
||||
float_arithmetic!(bits_to_f64, f64_to_bits, $float_op, $l, $r)
|
||||
float_arithmetic!(bytes_to_f64, f64_to_bytes, $float_op, $l, $r)
|
||||
)
|
||||
}
|
||||
|
||||
@ -202,19 +202,19 @@ pub fn binary_op<'tcx>(
|
||||
}
|
||||
|
||||
let val = match (bin_op, left_kind) {
|
||||
(Eq, F32) => PrimVal::from_bool(bits_to_f32(l) == bits_to_f32(r)),
|
||||
(Ne, F32) => PrimVal::from_bool(bits_to_f32(l) != bits_to_f32(r)),
|
||||
(Lt, F32) => PrimVal::from_bool(bits_to_f32(l) < bits_to_f32(r)),
|
||||
(Le, F32) => PrimVal::from_bool(bits_to_f32(l) <= bits_to_f32(r)),
|
||||
(Gt, F32) => PrimVal::from_bool(bits_to_f32(l) > bits_to_f32(r)),
|
||||
(Ge, F32) => PrimVal::from_bool(bits_to_f32(l) >= bits_to_f32(r)),
|
||||
(Eq, F32) => PrimVal::from_bool(bytes_to_f32(l) == bytes_to_f32(r)),
|
||||
(Ne, F32) => PrimVal::from_bool(bytes_to_f32(l) != bytes_to_f32(r)),
|
||||
(Lt, F32) => PrimVal::from_bool(bytes_to_f32(l) < bytes_to_f32(r)),
|
||||
(Le, F32) => PrimVal::from_bool(bytes_to_f32(l) <= bytes_to_f32(r)),
|
||||
(Gt, F32) => PrimVal::from_bool(bytes_to_f32(l) > bytes_to_f32(r)),
|
||||
(Ge, F32) => PrimVal::from_bool(bytes_to_f32(l) >= bytes_to_f32(r)),
|
||||
|
||||
(Eq, F64) => PrimVal::from_bool(bits_to_f64(l) == bits_to_f64(r)),
|
||||
(Ne, F64) => PrimVal::from_bool(bits_to_f64(l) != bits_to_f64(r)),
|
||||
(Lt, F64) => PrimVal::from_bool(bits_to_f64(l) < bits_to_f64(r)),
|
||||
(Le, F64) => PrimVal::from_bool(bits_to_f64(l) <= bits_to_f64(r)),
|
||||
(Gt, F64) => PrimVal::from_bool(bits_to_f64(l) > bits_to_f64(r)),
|
||||
(Ge, F64) => PrimVal::from_bool(bits_to_f64(l) >= bits_to_f64(r)),
|
||||
(Eq, F64) => PrimVal::from_bool(bytes_to_f64(l) == bytes_to_f64(r)),
|
||||
(Ne, F64) => PrimVal::from_bool(bytes_to_f64(l) != bytes_to_f64(r)),
|
||||
(Lt, F64) => PrimVal::from_bool(bytes_to_f64(l) < bytes_to_f64(r)),
|
||||
(Le, F64) => PrimVal::from_bool(bytes_to_f64(l) <= bytes_to_f64(r)),
|
||||
(Gt, F64) => PrimVal::from_bool(bytes_to_f64(l) > bytes_to_f64(r)),
|
||||
(Ge, F64) => PrimVal::from_bool(bytes_to_f64(l) >= bytes_to_f64(r)),
|
||||
|
||||
(Add, F32) => f32_arithmetic!(+, l, r),
|
||||
(Sub, F32) => f32_arithmetic!(-, l, r),
|
||||
@ -278,7 +278,7 @@ pub fn unary_op<'tcx>(
|
||||
let bytes = val.to_bytes()?;
|
||||
|
||||
let result_bytes = match (un_op, val_kind) {
|
||||
(Not, Bool) => !bits_to_bool(bytes) as u64,
|
||||
(Not, Bool) => !bytes_to_bool(bytes) as u64,
|
||||
|
||||
(Not, U8) => !(bytes as u8) as u64,
|
||||
(Not, U16) => !(bytes as u16) as u64,
|
||||
@ -295,8 +295,8 @@ pub fn unary_op<'tcx>(
|
||||
(Neg, I32) => -(bytes as i32) as u64,
|
||||
(Neg, I64) => -(bytes as i64) as u64,
|
||||
|
||||
(Neg, F32) => f32_to_bits(-bits_to_f32(bytes)),
|
||||
(Neg, F64) => f64_to_bits(-bits_to_f64(bytes)),
|
||||
(Neg, F32) => f32_to_bytes(-bytes_to_f32(bytes)),
|
||||
(Neg, F64) => f64_to_bytes(-bytes_to_f64(bytes)),
|
||||
|
||||
_ => {
|
||||
let msg = format!("unimplemented unary op: {:?}, {:?}", un_op, val);
|
||||
|
24
src/value.rs
24
src/value.rs
@ -6,25 +6,25 @@ use std::mem::transmute;
|
||||
use error::{EvalError, EvalResult};
|
||||
use memory::{Memory, Pointer};
|
||||
|
||||
pub(super) fn bits_to_f32(bits: u64) -> f32 {
|
||||
unsafe { transmute::<u32, f32>(bits as u32) }
|
||||
pub(super) fn bytes_to_f32(bytes: u64) -> f32 {
|
||||
unsafe { transmute::<u32, f32>(bytes as u32) }
|
||||
}
|
||||
|
||||
pub(super) fn bits_to_f64(bits: u64) -> f64 {
|
||||
unsafe { transmute::<u64, f64>(bits) }
|
||||
pub(super) fn bytes_to_f64(bytes: u64) -> f64 {
|
||||
unsafe { transmute::<u64, f64>(bytes) }
|
||||
}
|
||||
|
||||
pub(super) fn f32_to_bits(f: f32) -> u64 {
|
||||
pub(super) fn f32_to_bytes(f: f32) -> u64 {
|
||||
unsafe { transmute::<f32, u32>(f) as u64 }
|
||||
}
|
||||
|
||||
pub(super) fn f64_to_bits(f: f64) -> u64 {
|
||||
pub(super) fn f64_to_bytes(f: f64) -> u64 {
|
||||
unsafe { transmute::<f64, u64>(f) }
|
||||
}
|
||||
|
||||
pub(super) fn bits_to_bool(n: u64) -> bool {
|
||||
pub(super) fn bytes_to_bool(n: u64) -> bool {
|
||||
// FIXME(solson): Can we reach here due to user error?
|
||||
debug_assert!(n == 0 || n == 1, "bits interpreted as bool were {}", n);
|
||||
debug_assert!(n == 0 || n == 1, "bytes interpreted as bool were {}", n);
|
||||
n & 1 == 1
|
||||
}
|
||||
|
||||
@ -126,11 +126,11 @@ impl<'tcx> PrimVal {
|
||||
}
|
||||
|
||||
pub fn from_f32(f: f32) -> Self {
|
||||
PrimVal::Bytes(f32_to_bits(f))
|
||||
PrimVal::Bytes(f32_to_bytes(f))
|
||||
}
|
||||
|
||||
pub fn from_f64(f: f64) -> Self {
|
||||
PrimVal::Bytes(f64_to_bits(f))
|
||||
PrimVal::Bytes(f64_to_bytes(f))
|
||||
}
|
||||
|
||||
pub fn from_bool(b: bool) -> Self {
|
||||
@ -166,11 +166,11 @@ impl<'tcx> PrimVal {
|
||||
}
|
||||
|
||||
pub fn to_f32(self) -> EvalResult<'tcx, f32> {
|
||||
self.to_bytes().map(bits_to_f32)
|
||||
self.to_bytes().map(bytes_to_f32)
|
||||
}
|
||||
|
||||
pub fn to_f64(self) -> EvalResult<'tcx, f64> {
|
||||
self.to_bytes().map(bits_to_f64)
|
||||
self.to_bytes().map(bytes_to_f64)
|
||||
}
|
||||
|
||||
pub fn to_bool(self) -> EvalResult<'tcx, bool> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user