]> git.lizzy.rs Git - rust.git/blob - src/test/codegen/swap-small-types.rs
Rollup merge of #100026 - WaffleLapkin:array-chunks, r=scottmcm
[rust.git] / src / test / codegen / swap-small-types.rs
1 // compile-flags: -O
2 // only-x86_64
3 // ignore-debug: the debug assertions get in the way
4
5 #![crate_type = "lib"]
6
7 use std::mem::swap;
8
9 type RGB48 = [u16; 3];
10
11 // CHECK-LABEL: @swap_rgb48
12 #[no_mangle]
13 pub fn swap_rgb48(x: &mut RGB48, y: &mut RGB48) {
14     // FIXME MIR inlining messes up LLVM optimizations.
15 // WOULD-CHECK-NOT: alloca
16 // WOULD-CHECK: load i48
17 // WOULD-CHECK: store i48
18     swap(x, y)
19 }
20
21 // LLVM doesn't vectorize a loop over 3-byte elements,
22 // so we chunk it down to bytes and loop over those instead.
23 type RGB24 = [u8; 3];
24
25 // CHECK-LABEL: @swap_rgb24_slices
26 #[no_mangle]
27 pub fn swap_rgb24_slices(x: &mut [RGB24], y: &mut [RGB24]) {
28 // CHECK-NOT: alloca
29 // CHECK: load <{{[0-9]+}} x i8>
30 // CHECK: store <{{[0-9]+}} x i8>
31     if x.len() == y.len() {
32         x.swap_with_slice(y);
33     }
34 }
35
36 // This one has a power-of-two size, so we iterate over it directly
37 type RGBA32 = [u8; 4];
38
39 // CHECK-LABEL: @swap_rgba32_slices
40 #[no_mangle]
41 pub fn swap_rgba32_slices(x: &mut [RGBA32], y: &mut [RGBA32]) {
42 // CHECK-NOT: alloca
43 // CHECK: load <{{[0-9]+}} x i32>
44 // CHECK: store <{{[0-9]+}} x i32>
45     if x.len() == y.len() {
46         x.swap_with_slice(y);
47     }
48 }
49
50 // Strings have a non-power-of-two size, but have pointer alignment,
51 // so we swap usizes instead of dropping all the way down to bytes.
52 const _: () = assert!(!std::mem::size_of::<String>().is_power_of_two());
53
54 // CHECK-LABEL: @swap_string_slices
55 #[no_mangle]
56 pub fn swap_string_slices(x: &mut [String], y: &mut [String]) {
57 // CHECK-NOT: alloca
58 // CHECK: load <{{[0-9]+}} x i64>
59 // CHECK: store <{{[0-9]+}} x i64>
60     if x.len() == y.len() {
61         x.swap_with_slice(y);
62     }
63 }