1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_subvti3
3 // REQUIRES: int128
4 
5 #include "int_lib.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 #ifdef CRT_HAS_128BIT
10 
11 // Returns: a - b
12 
13 // Effects: aborts if a - b overflows
14 
15 COMPILER_RT_ABI ti_int __subvti3(ti_int a, ti_int b);
16 
test__subvti3(ti_int a,ti_int b)17 int test__subvti3(ti_int a, ti_int b)
18 {
19     ti_int x = __subvti3(a, b);
20     ti_int expected = a - b;
21     if (x != expected)
22     {
23         twords at;
24         at.all = a;
25         twords bt;
26         bt.all = b;
27         twords xt;
28         xt.all = x;
29         twords expectedt;
30         expectedt.all = expected;
31         printf("error in test__subvsi3(0x%.16llX%.16llX, 0x%.16llX%.16llX) = "
32                "0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
33                at.s.high, at.s.low, bt.s.high, bt.s.low, xt.s.high, xt.s.low,
34                expectedt.s.high, expectedt.s.low);
35     }
36     return x != expected;
37 }
38 
39 #endif
40 
main()41 int main()
42 {
43 #ifdef CRT_HAS_128BIT
44 //     test__subvti3(make_ti(0x8000000000000000LL, 0), 1);  // should abort
45 //     test__subvti3(0, make_ti(0x8000000000000000LL, 0));  // should abort
46 //     test__subvti3(1, make_ti(0x8000000000000000LL, 0));  // should abort
47 //     test__subvti3(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), -1);  // should abort
48 //     test__subvti3(-2, make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL));  // should abort
49 
50     if (test__subvti3(make_ti(0x8000000000000000LL, 0), -1))
51         return 1;
52     if (test__subvti3(make_ti(0x8000000000000000LL, 0), 0))
53         return 1;
54     if (test__subvti3(-1, make_ti(0x8000000000000000LL, 0)))
55         return 1;
56     if (test__subvti3(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 1))
57         return 1;
58     if (test__subvti3(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL), 0))
59         return 1;
60     if (test__subvti3(1, make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL)))
61         return 1;
62     if (test__subvti3(0, make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL)))
63         return 1;
64     if (test__subvti3(-1, make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL)))
65         return 1;
66 
67 #else
68     printf("skipped\n");
69 #endif
70     return 0;
71 }
72