]> git.proxmox.com Git - rustc.git/blame - src/test/ui/llvm-asm/llvm-asm-in-out-operand.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / src / test / ui / llvm-asm / llvm-asm-in-out-operand.rs
CommitLineData
416331ca
XL
1// run-pass
2
ba9703b0 3#![feature(llvm_asm)]
94222f64 4#![allow(deprecated)] // llvm_asm!
1a4d82fc
JJ
5
6#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
7unsafe fn next_power_of_2(n: u32) -> u32 {
8 let mut tmp = n;
ba9703b0 9 llvm_asm!("dec $0" : "+rm"(tmp) :: "cc");
62682a34 10 let mut shift = 1_u32;
1a4d82fc 11 while shift <= 16 {
ba9703b0 12 llvm_asm!(
1a4d82fc
JJ
13 "shr %cl, $2
14 or $2, $0
15 shl $$1, $1"
16 : "+&rm"(tmp), "+{ecx}"(shift) : "r"(tmp) : "cc"
17 );
18 }
ba9703b0 19 llvm_asm!("inc $0" : "+rm"(tmp) :: "cc");
1a4d82fc
JJ
20 return tmp;
21}
22
23#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
24pub fn main() {
25 unsafe {
26 assert_eq!(64, next_power_of_2(37));
27 assert_eq!(2147483648, next_power_of_2(2147483647));
28 }
29
c34b1796
AL
30 let mut y: isize = 5;
31 let x: isize;
1a4d82fc
JJ
32 unsafe {
33 // Treat the output as initialization.
ba9703b0 34 llvm_asm!(
1a4d82fc
JJ
35 "shl $2, $1
36 add $3, $1
37 mov $1, $0"
85aaf69f 38 : "=r"(x), "+r"(y) : "i"(3_usize), "ir"(7_usize) : "cc"
1a4d82fc
JJ
39 );
40 }
41 assert_eq!(x, 47);
42 assert_eq!(y, 47);
43
44 let mut x = x + 1;
45 assert_eq!(x, 48);
46
47 unsafe {
48 // Assignment to mutable.
49 // Early clobber "&":
50 // Forbids the use of a single register by both operands.
ba9703b0 51 llvm_asm!("shr $$2, $1; add $1, $0" : "+&r"(x) : "r"(x) : "cc");
1a4d82fc
JJ
52 }
53 assert_eq!(x, 60);
54}
55
56#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
57pub fn main() {}