1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_ucmpti2
3 // REQUIRES: int128
4 
5 #include "int_lib.h"
6 #include <stdio.h>
7 
8 #ifdef CRT_HAS_128BIT
9 
10 // Returns:  if (a <  b) returns 0
11 //           if (a == b) returns 1
12 //           if (a >  b) returns 2
13 
14 COMPILER_RT_ABI si_int __ucmpti2(tu_int a, tu_int b);
15 
test__ucmpti2(tu_int a,tu_int b,si_int expected)16 int test__ucmpti2(tu_int a, tu_int b, si_int expected)
17 {
18     si_int x = __ucmpti2(a, b);
19     if (x != expected)
20     {
21         utwords at;
22         at.all = a;
23         utwords bt;
24         bt.all = b;
25         printf("error in __ucmpti2(0x%.16llX%.16llX, 0x%.16llX%.16llX) = %d, "
26                "expected %d\n",
27                at.s.high, at.s.low, bt.s.high, bt.s.low, x, expected);
28     }
29     return x != expected;
30 }
31 
32 #endif
33 
main()34 int main()
35 {
36 #ifdef CRT_HAS_128BIT
37     if (test__ucmpti2(0, 0, 1))
38         return 1;
39     if (test__ucmpti2(1, 1, 1))
40         return 1;
41     if (test__ucmpti2(2, 2, 1))
42         return 1;
43     if (test__ucmpti2(0x7FFFFFFF, 0x7FFFFFFF, 1))
44         return 1;
45     if (test__ucmpti2(0x80000000, 0x80000000, 1))
46         return 1;
47     if (test__ucmpti2(0x80000001, 0x80000001, 1))
48         return 1;
49     if (test__ucmpti2(0xFFFFFFFF, 0xFFFFFFFF, 1))
50         return 1;
51     if (test__ucmpti2(0x000000010000000LL, 0x000000010000000LL, 1))
52         return 1;
53     if (test__ucmpti2(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL, 1))
54         return 1;
55 
56     if (test__ucmpti2(0x0000000200000002LL, 0x0000000300000001LL, 0))
57         return 1;
58     if (test__ucmpti2(0x0000000200000002LL, 0x0000000300000002LL, 0))
59         return 1;
60     if (test__ucmpti2(0x0000000200000002LL, 0x0000000300000003LL, 0))
61         return 1;
62 
63     if (test__ucmpti2(0x0000000200000002LL, 0x0000000100000001LL, 2))
64         return 1;
65     if (test__ucmpti2(0x0000000200000002LL, 0x0000000100000002LL, 2))
66         return 1;
67     if (test__ucmpti2(0x0000000200000002LL, 0x0000000100000003LL, 2))
68         return 1;
69 
70     if (test__ucmpti2(0x0000000200000002LL, 0x0000000200000001LL, 2))
71         return 1;
72     if (test__ucmpti2(0x0000000200000002LL, 0x0000000200000002LL, 1))
73         return 1;
74     if (test__ucmpti2(0x0000000200000002LL, 0x0000000200000003LL, 0))
75         return 1;
76 
77     if (test__ucmpti2(make_tu(0x0000000000000001uLL, 0x0000000000000000uLL),
78                       make_tu(0x0000000000000000uLL, 0xFFFFFFFFFFFFFFFFuLL), 2))
79         return 1;
80     if (test__ucmpti2(make_tu(0x0000000000000001uLL, 0x0000000000000000uLL),
81                       make_tu(0x0000000000000001uLL, 0x0000000000000000uLL), 1))
82         return 1;
83     if (test__ucmpti2(make_tu(0x0000000000000001uLL, 0x0000000000000000uLL),
84                       make_tu(0x0000000000000001uLL, 0x0000000000000001uLL), 0))
85         return 1;
86 
87     if (test__ucmpti2(make_tu(0x8000000000000000uLL, 0x0000000000000000uLL),
88                       make_tu(0x7FFFFFFFFFFFFFFFuLL, 0xFFFFFFFFFFFFFFFFuLL), 2))
89         return 1;
90     if (test__ucmpti2(make_tu(0x8000000000000000uLL, 0x0000000000000000uLL),
91                       make_tu(0x8000000000000000uLL, 0x0000000000000000uLL), 1))
92         return 1;
93     if (test__ucmpti2(make_tu(0x8000000000000000uLL, 0x0000000000000000uLL),
94                       make_tu(0x8000000000000000uLL, 0x0000000000000001uLL), 0))
95         return 1;
96 
97     if (test__ucmpti2(make_tu(0xFFFFFFFFFFFFFFFFuLL, 0xFFFFFFFFFFFFFFFFuLL),
98                       make_tu(0xFFFFFFFFFFFFFFFFuLL, 0xFFFFFFFFFFFFFFFEuLL), 2))
99         return 1;
100     if (test__ucmpti2(make_tu(0xFFFFFFFFFFFFFFFFuLL, 0xFFFFFFFFFFFFFFFFuLL),
101                       make_tu(0xFFFFFFFFFFFFFFFFuLL, 0xFFFFFFFFFFFFFFFFuLL), 1))
102         return 1;
103 #else
104     printf("skipped\n");
105 #endif
106    return 0;
107 }
108