Skip to content

Commit

Permalink
Merge pull request rust-lang#152 from oli-obk/intrinsics
Browse files Browse the repository at this point in the history
Intrinsics and overflow
  • Loading branch information
solson committed Mar 14, 2017
2 parents 0ca0676 + 257ac58 commit a82924b
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 55 deletions.
20 changes: 2 additions & 18 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,9 @@ pub fn binary_op<'tcx>(

// These ops can have an RHS with a different numeric type.
if bin_op == Shl || bin_op == Shr {
// These are the maximum values a bitshift RHS could possibly have. For example, u16
// can be bitshifted by 0..16, so masking with 0b1111 (16 - 1) will ensure we are in
// that range.
let type_bits: u32 = match left_kind {
I8 | U8 => 8,
I16 | U16 => 16,
I32 | U32 => 32,
I64 | U64 => 64,
I128 | U128 => 128,
_ => bug!("bad MIR: bitshift lhs is not integral"),
};

// Cast to `u32` because `overflowing_sh{l,r}` only take `u32`, then apply the bitmask
// to ensure it's within the valid shift value range.
let masked_shift_width = (r as u32) & (type_bits - 1);

return match bin_op {
Shl => int_shift!(left_kind, overflowing_shl, l, masked_shift_width),
Shr => int_shift!(left_kind, overflowing_shr, l, masked_shift_width),
Shl => int_shift!(left_kind, overflowing_shl, l, r as u32),
Shr => int_shift!(left_kind, overflowing_shr, l, r as u32),
_ => bug!("it has already been checked that this is a shift op"),
};
}
Expand Down
119 changes: 82 additions & 37 deletions src/terminator/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
// we are inherently singlethreaded and singlecored, this is a nop
}

"atomic_xchg" => {
_ if intrinsic_name.starts_with("atomic_xchg") => {
let ty = substs.type_at(0);
let ptr = arg_vals[0].read_ptr(&self.memory)?;
let change = self.value_to_primval(arg_vals[1], ty)?;
Expand All @@ -92,8 +92,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
self.write_primval(Lvalue::from_ptr(ptr), change, ty)?;
}

"atomic_cxchg_relaxed" |
"atomic_cxchg" => {
_ if intrinsic_name.starts_with("atomic_cxchg") => {
let ty = substs.type_at(0);
let ptr = arg_vals[0].read_ptr(&self.memory)?;
let expect_old = self.value_to_primval(arg_vals[1], ty)?;
Expand All @@ -111,8 +110,11 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
self.write_primval(Lvalue::from_ptr(ptr), change, ty)?;
}

"atomic_xadd" |
"atomic_xadd_relaxed" => {
"atomic_or" | "atomic_or_acq" | "atomic_or_rel" | "atomic_or_acqrel" | "atomic_or_relaxed" |
"atomic_xor" | "atomic_xor_acq" | "atomic_xor_rel" | "atomic_xor_acqrel" | "atomic_xor_relaxed" |
"atomic_and" | "atomic_and_acq" | "atomic_and_rel" | "atomic_and_acqrel" | "atomic_and_relaxed" |
"atomic_xadd" | "atomic_xadd_acq" | "atomic_xadd_rel" | "atomic_xadd_acqrel" | "atomic_xadd_relaxed" |
"atomic_xsub" | "atomic_xsub_acq" | "atomic_xsub_rel" | "atomic_xsub_acqrel" | "atomic_xsub_relaxed" => {
let ty = substs.type_at(0);
let ptr = arg_vals[0].read_ptr(&self.memory)?;
let change = self.value_to_primval(arg_vals[1], ty)?;
Expand All @@ -124,27 +126,18 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
};
self.write_primval(dest, old, ty)?;
let kind = self.ty_to_primval_kind(ty)?;
// FIXME: what do atomics do on overflow?
let (val, _) = operator::binary_op(mir::BinOp::Add, old, kind, change, kind)?;
self.write_primval(Lvalue::from_ptr(ptr), val, ty)?;
},

"atomic_xsub_rel" => {
let ty = substs.type_at(0);
let ptr = arg_vals[0].read_ptr(&self.memory)?;
let change = self.value_to_primval(arg_vals[1], ty)?;
let old = self.read_value(ptr, ty)?;
let old = match old {
Value::ByVal(val) => val,
Value::ByRef(_) => bug!("just read the value, can't be byref"),
Value::ByValPair(..) => bug!("atomic_xsub_rel doesn't work with nonprimitives"),
let op = match intrinsic_name.split('_').nth(1).unwrap() {
"or" => mir::BinOp::BitOr,
"xor" => mir::BinOp::BitXor,
"and" => mir::BinOp::BitAnd,
"xadd" => mir::BinOp::Add,
"xsub" => mir::BinOp::Sub,
_ => bug!(),
};
self.write_primval(dest, old, ty)?;
let kind = self.ty_to_primval_kind(ty)?;
// FIXME: what do atomics do on overflow?
let (val, _) = operator::binary_op(mir::BinOp::Sub, old, kind, change, kind)?;
let (val, _) = operator::binary_op(op, old, kind, change, kind)?;
self.write_primval(Lvalue::from_ptr(ptr), val, ty)?;
}
},

"breakpoint" => unimplemented!(), // halt miri

Expand Down Expand Up @@ -207,14 +200,50 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
return self.eval_drop_impls(drops, span);
}

"fabsf32" => {
"sinf32" | "fabsf32" | "cosf32" |
"sqrtf32" | "expf32" | "exp2f32" |
"logf32" | "log10f32" | "log2f32" |
"floorf32" | "ceilf32" | "truncf32" => {
let f = self.value_to_primval(arg_vals[0], f32)?.to_f32()?;
self.write_primval(dest, PrimVal::from_f32(f.abs()), dest_ty)?;
let f = match intrinsic_name {
"sinf32" => f.sin(),
"fabsf32" => f.abs(),
"cosf32" => f.cos(),
"sqrtf32" => f.sqrt(),
"expf32" => f.exp(),
"exp2f32" => f.exp2(),
"logf32" => f.ln(),
"log10f32" => f.log10(),
"log2f32" => f.log2(),
"floorf32" => f.floor(),
"ceilf32" => f.ceil(),
"truncf32" => f.trunc(),
_ => bug!(),
};
self.write_primval(dest, PrimVal::from_f32(f), dest_ty)?;
}

"fabsf64" => {
"sinf64" | "fabsf64" | "cosf64" |
"sqrtf64" | "expf64" | "exp2f64" |
"logf64" | "log10f64" | "log2f64" |
"floorf64" | "ceilf64" | "truncf64" => {
let f = self.value_to_primval(arg_vals[0], f64)?.to_f64()?;
self.write_primval(dest, PrimVal::from_f64(f.abs()), dest_ty)?;
let f = match intrinsic_name {
"sinf64" => f.sin(),
"fabsf64" => f.abs(),
"cosf64" => f.cos(),
"sqrtf64" => f.sqrt(),
"expf64" => f.exp(),
"exp2f64" => f.exp2(),
"logf64" => f.ln(),
"log10f64" => f.log10(),
"log2f64" => f.log2(),
"floorf64" => f.floor(),
"ceilf64" => f.ceil(),
"truncf64" => f.trunc(),
_ => bug!(),
};
self.write_primval(dest, PrimVal::from_f64(f), dest_ty)?;
}

"fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" => {
Expand Down Expand Up @@ -320,26 +349,42 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
self.intrinsic_overflowing(mir::BinOp::Add, &args[0], &args[1], dest, dest_ty)?;
}

"powif32" => {
"powf32" => {
let f = self.value_to_primval(arg_vals[0], f32)?.to_f32()?;
let i = self.value_to_primval(arg_vals[1], i32)?.to_i128()?;
self.write_primval(dest, PrimVal::from_f32(f.powi(i as i32)), dest_ty)?;
let f2 = self.value_to_primval(arg_vals[1], f32)?.to_f32()?;
self.write_primval(dest, PrimVal::from_f32(f.powf(f2)), dest_ty)?;
}

"powif64" => {
"powf64" => {
let f = self.value_to_primval(arg_vals[0], f64)?.to_f64()?;
let i = self.value_to_primval(arg_vals[1], i32)?.to_i128()?;
self.write_primval(dest, PrimVal::from_f64(f.powi(i as i32)), dest_ty)?;
let f2 = self.value_to_primval(arg_vals[1], f64)?.to_f64()?;
self.write_primval(dest, PrimVal::from_f64(f.powf(f2)), dest_ty)?;
}

"fmaf32" => {
let a = self.value_to_primval(arg_vals[0], f32)?.to_f32()?;
let b = self.value_to_primval(arg_vals[1], f32)?.to_f32()?;
let c = self.value_to_primval(arg_vals[2], f32)?.to_f32()?;
self.write_primval(dest, PrimVal::from_f32(a * b + c), dest_ty)?;
}

"fmaf64" => {
let a = self.value_to_primval(arg_vals[0], f64)?.to_f64()?;
let b = self.value_to_primval(arg_vals[1], f64)?.to_f64()?;
let c = self.value_to_primval(arg_vals[2], f64)?.to_f64()?;
self.write_primval(dest, PrimVal::from_f64(a * b + c), dest_ty)?;
}

"sqrtf32" => {
"powif32" => {
let f = self.value_to_primval(arg_vals[0], f32)?.to_f32()?;
self.write_primval(dest, PrimVal::from_f32(f.sqrt()), dest_ty)?;
let i = self.value_to_primval(arg_vals[1], i32)?.to_i128()?;
self.write_primval(dest, PrimVal::from_f32(f.powi(i as i32)), dest_ty)?;
}

"sqrtf64" => {
"powif64" => {
let f = self.value_to_primval(arg_vals[0], f64)?.to_f64()?;
self.write_primval(dest, PrimVal::from_f64(f.sqrt()), dest_ty)?;
let i = self.value_to_primval(arg_vals[1], i32)?.to_i128()?;
self.write_primval(dest, PrimVal::from_f64(f.powi(i as i32)), dest_ty)?;
}

"size_of" => {
Expand Down
15 changes: 15 additions & 0 deletions tests/compile-fail/overflowing-rsh-6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(exceeding_bitshifts)]

fn main() {
let _n = 1i64 >> 64; //~ Overflow(Shr)
}
30 changes: 30 additions & 0 deletions tests/run-pass/atomic-access-bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT};
use std::sync::atomic::Ordering::*;

static mut ATOMIC: AtomicBool = ATOMIC_BOOL_INIT;

fn main() {
unsafe {
assert_eq!(*ATOMIC.get_mut(), false);
ATOMIC.store(true, SeqCst);
assert_eq!(*ATOMIC.get_mut(), true);
ATOMIC.fetch_or(false, SeqCst);
assert_eq!(*ATOMIC.get_mut(), true);
ATOMIC.fetch_and(false, SeqCst);
assert_eq!(*ATOMIC.get_mut(), false);
ATOMIC.fetch_nand(true, SeqCst);
assert_eq!(*ATOMIC.get_mut(), true);
ATOMIC.fetch_xor(true, SeqCst);
assert_eq!(*ATOMIC.get_mut(), false);
}
}
36 changes: 36 additions & 0 deletions tests/run-pass/atomic-compare_exchange.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::sync::atomic::{AtomicIsize, ATOMIC_ISIZE_INIT};
use std::sync::atomic::Ordering::*;

static ATOMIC: AtomicIsize = ATOMIC_ISIZE_INIT;

fn main() {
// Make sure trans can emit all the intrinsics correctly
ATOMIC.compare_exchange(0, 1, Relaxed, Relaxed).ok();
ATOMIC.compare_exchange(0, 1, Acquire, Relaxed).ok();
ATOMIC.compare_exchange(0, 1, Release, Relaxed).ok();
ATOMIC.compare_exchange(0, 1, AcqRel, Relaxed).ok();
ATOMIC.compare_exchange(0, 1, SeqCst, Relaxed).ok();
ATOMIC.compare_exchange(0, 1, Acquire, Acquire).ok();
ATOMIC.compare_exchange(0, 1, AcqRel, Acquire).ok();
ATOMIC.compare_exchange(0, 1, SeqCst, Acquire).ok();
ATOMIC.compare_exchange(0, 1, SeqCst, SeqCst).ok();
ATOMIC.compare_exchange_weak(0, 1, Relaxed, Relaxed).ok();
ATOMIC.compare_exchange_weak(0, 1, Acquire, Relaxed).ok();
ATOMIC.compare_exchange_weak(0, 1, Release, Relaxed).ok();
ATOMIC.compare_exchange_weak(0, 1, AcqRel, Relaxed).ok();
ATOMIC.compare_exchange_weak(0, 1, SeqCst, Relaxed).ok();
ATOMIC.compare_exchange_weak(0, 1, Acquire, Acquire).ok();
ATOMIC.compare_exchange_weak(0, 1, AcqRel, Acquire).ok();
ATOMIC.compare_exchange_weak(0, 1, SeqCst, Acquire).ok();
ATOMIC.compare_exchange_weak(0, 1, SeqCst, SeqCst).ok();
}
67 changes: 67 additions & 0 deletions tests/run-pass/intrinsics-math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! assert_approx_eq {
($a:expr, $b:expr) => ({
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6,
"{} is not approximately equal to {}", *a, *b);
})
}

pub fn main() {
use std::f32;
use std::f64;

assert_approx_eq!(64f32.sqrt(), 8f32);
assert_approx_eq!(64f64.sqrt(), 8f64);

assert_approx_eq!(25f32.powi(-2), 0.0016f32);
assert_approx_eq!(23.2f64.powi(2), 538.24f64);

assert_approx_eq!(0f32.sin(), 0f32);
assert_approx_eq!((f64::consts::PI / 2f64).sin(), 1f64);

assert_approx_eq!(0f32.cos(), 1f32);
assert_approx_eq!((f64::consts::PI * 2f64).cos(), 1f64);

assert_approx_eq!(25f32.powf(-2f32), 0.0016f32);
assert_approx_eq!(400f64.powf(0.5f64), 20f64);

assert_approx_eq!((1f32.exp() - f32::consts::E).abs(), 0f32);
assert_approx_eq!(1f64.exp(), f64::consts::E);

assert_approx_eq!(10f32.exp2(), 1024f32);
assert_approx_eq!(50f64.exp2(), 1125899906842624f64);

assert_approx_eq!((f32::consts::E.ln() - 1f32).abs(), 0f32);
assert_approx_eq!(1f64.ln(), 0f64);

assert_approx_eq!(10f32.log10(), 1f32);
assert_approx_eq!(f64::consts::E.log10(), f64::consts::LOG10_E);

assert_approx_eq!(8f32.log2(), 3f32);
assert_approx_eq!(f64::consts::E.log2(), f64::consts::LOG2_E);

assert_approx_eq!(1.0f32.mul_add(2.0f32, 5.0f32), 7.0f32);
assert_approx_eq!(0.0f64.mul_add(-2.0f64, f64::consts::E), f64::consts::E);

assert_approx_eq!((-1.0f32).abs(), 1.0f32);
assert_approx_eq!(34.2f64.abs(), 34.2f64);

assert_approx_eq!(3.8f32.floor(), 3.0f32);
assert_approx_eq!((-1.1f64).floor(), -2.0f64);

assert_approx_eq!((-2.3f32).ceil(), -2.0f32);
assert_approx_eq!(3.8f64.ceil(), 4.0f64);

assert_approx_eq!(0.1f32.trunc(), 0.0f32);
assert_approx_eq!((-0.1f64).trunc(), 0.0f64);
}

0 comments on commit a82924b

Please sign in to comment.