]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/asm-in-out-operand.rs
auto merge of #18056 : TeXitoi/rust/shootout-reverse-complement-improvement, r=alexcr...
[rust.git] / src / test / run-pass / asm-in-out-operand.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(asm)]
12
13 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
14 unsafe fn next_power_of_2(n: u32) -> u32 {
15     let mut tmp = n;
16     asm!("dec $0" : "+rm"(tmp) :: "cc");
17     let mut shift = 1u;
18     while shift <= 16 {
19         asm!(
20             "shr %cl, $2
21             or $2, $0
22             shl $$1, $1"
23             : "+&rm"(tmp), "+{ecx}"(shift) : "r"(tmp) : "cc"
24         );
25     }
26     asm!("inc $0" : "+rm"(tmp) :: "cc");
27     return tmp;
28 }
29
30 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
31 pub fn main() {
32     unsafe {
33         assert_eq!(64, next_power_of_2(37));
34         assert_eq!(2147483648, next_power_of_2(2147483647));
35     }
36
37     let mut y: int = 5;
38     let x: int;
39     unsafe {
40         // Treat the output as initialization.
41         asm!(
42             "shl $2, $1
43             add $3, $1
44             mov $1, $0"
45             : "=r"(x), "+r"(y) : "i"(3u), "ir"(7u) : "cc"
46         );
47     }
48     assert_eq!(x, 47);
49     assert_eq!(y, 47);
50
51     let mut x = x + 1;
52     assert_eq!(x, 48);
53
54     unsafe {
55         // Assignment to mutable.
56         // Early clobber "&":
57         // Forbids the use of a single register by both operands.
58         asm!("shr $$2, $1; add $1, $0" : "+&r"(x) : "r"(x) : "cc");
59     }
60     assert_eq!(x, 60);
61 }
62
63 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
64 pub fn main() {}