File: comparison-operators-2-struct.rs

package info (click to toggle)
rustc 1.87.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 925,564 kB
  • sloc: xml: 158,127; python: 36,039; javascript: 19,761; sh: 19,737; cpp: 18,981; ansic: 13,133; asm: 4,376; makefile: 710; perl: 29; lisp: 28; ruby: 19; sql: 11
file content (61 lines) | stat: -rw-r--r-- 2,294 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//@ compile-flags: -C opt-level=1
//@ min-llvm-version: 20

// The `derive(PartialOrd)` for a 2-field type doesn't override `lt`/`le`/`gt`/`ge`.
// This double-checks that the `Option<Ordering>` intermediate values used
// in the operators for such a type all optimize away.

#![crate_type = "lib"]

use std::cmp::Ordering;

#[derive(PartialOrd, PartialEq)]
pub struct Foo(i32, u32);

// CHECK-LABEL: @check_lt(
// CHECK-SAME: i32{{.+}}%[[A0:.+]], i32{{.+}}%[[A1:.+]], i32{{.+}}%[[B0:.+]], i32{{.+}}%[[B1:.+]])
#[no_mangle]
pub fn check_lt(a: Foo, b: Foo) -> bool {
    // CHECK-DAG: %[[EQ:.+]] = icmp eq i32 %[[A0]], %[[B0]]
    // CHECK-DAG: %[[R0:.+]] = icmp slt i32 %[[A0]], %[[B0]]
    // CHECK-DAG: %[[R1:.+]] = icmp ult i32 %[[A1]], %[[B1]]
    // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[R1]], i1 %[[R0]]
    // CHECK-NEXT: ret i1 %[[R]]
    a < b
}

// CHECK-LABEL: @check_le(
// CHECK-SAME: i32{{.+}}%[[A0:.+]], i32{{.+}}%[[A1:.+]], i32{{.+}}%[[B0:.+]], i32{{.+}}%[[B1:.+]])
#[no_mangle]
pub fn check_le(a: Foo, b: Foo) -> bool {
    // CHECK-DAG: %[[EQ:.+]] = icmp eq i32 %[[A0]], %[[B0]]
    // CHECK-DAG: %[[R0:.+]] = icmp sle i32 %[[A0]], %[[B0]]
    // CHECK-DAG: %[[R1:.+]] = icmp ule i32 %[[A1]], %[[B1]]
    // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[R1]], i1 %[[R0]]
    // CHECK-NEXT: ret i1 %[[R]]
    a <= b
}

// CHECK-LABEL: @check_gt(
// CHECK-SAME: i32{{.+}}%[[A0:.+]], i32{{.+}}%[[A1:.+]], i32{{.+}}%[[B0:.+]], i32{{.+}}%[[B1:.+]])
#[no_mangle]
pub fn check_gt(a: Foo, b: Foo) -> bool {
    // CHECK-DAG: %[[EQ:.+]] = icmp eq i32 %[[A0]], %[[B0]]
    // CHECK-DAG: %[[R0:.+]] = icmp sgt i32 %[[A0]], %[[B0]]
    // CHECK-DAG: %[[R1:.+]] = icmp ugt i32 %[[A1]], %[[B1]]
    // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[R1]], i1 %[[R0]]
    // CHECK-NEXT: ret i1 %[[R]]
    a > b
}

// CHECK-LABEL: @check_ge(
// CHECK-SAME: i32{{.+}}%[[A0:.+]], i32{{.+}}%[[A1:.+]], i32{{.+}}%[[B0:.+]], i32{{.+}}%[[B1:.+]])
#[no_mangle]
pub fn check_ge(a: Foo, b: Foo) -> bool {
    // CHECK-DAG: %[[EQ:.+]] = icmp eq i32 %[[A0]], %[[B0]]
    // CHECK-DAG: %[[R0:.+]] = icmp sge i32 %[[A0]], %[[B0]]
    // CHECK-DAG: %[[R1:.+]] = icmp uge i32 %[[A1]], %[[B1]]
    // CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[R1]], i1 %[[R0]]
    // CHECK-NEXT: ret i1 %[[R]]
    a >= b
}