forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rust-lang#152 from oli-obk/intrinsics
Intrinsics and overflow
- Loading branch information
Showing
6 changed files
with
232 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |