xref: /llvm-project/compiler-rt/test/builtins/Unit/multi3_test.c (revision 70f9cfc857f14ce7ae4057aea7b3568e6aa41d38)
1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_multi3
3 // REQUIRES: int128
4 
5 #include "int_lib.h"
6 #include <stdio.h>
7 
8 #ifdef CRT_HAS_128BIT
9 
10 COMPILER_RT_ABI ti_int __multi3(ti_int a, ti_int b);
11 
test__multi3(ti_int a,ti_int b,ti_int expected)12 int test__multi3(ti_int a, ti_int b, ti_int expected)
13 {
14     ti_int x = __multi3(a, b);
15     if (x != expected)
16     {
17         twords at;
18         at.all = a;
19         twords bt;
20         bt.all = b;
21         twords xt;
22         xt.all = x;
23         twords expectedt;
24         expectedt.all = expected;
25         printf("error in __multi3: 0x%.16llX%.16llX * 0x%.16llX%.16llX = "
26                "0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
27                at.s.high, at.s.low, bt.s.high, bt.s.low, xt.s.high, xt.s.low,
28                expectedt.s.high, expectedt.s.low);
29     }
30     return x != expected;
31 }
32 
33 char assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0};
34 
35 #endif
36 
main()37 int main()
38 {
39 #ifdef CRT_HAS_128BIT
40     if (test__multi3(0, 0, 0))
41         return 1;
42     if (test__multi3(0, 1, 0))
43         return 1;
44     if (test__multi3(1, 0, 0))
45         return 1;
46     if (test__multi3(0, 10, 0))
47         return 1;
48     if (test__multi3(10, 0, 0))
49         return 1;
50     if (test__multi3(0, 81985529216486895LL, 0))
51         return 1;
52     if (test__multi3(81985529216486895LL, 0, 0))
53         return 1;
54 
55     if (test__multi3(0, -1, 0))
56         return 1;
57     if (test__multi3(-1, 0, 0))
58         return 1;
59     if (test__multi3(0, -10, 0))
60         return 1;
61     if (test__multi3(-10, 0, 0))
62         return 1;
63     if (test__multi3(0, -81985529216486895LL, 0))
64         return 1;
65     if (test__multi3(-81985529216486895LL, 0, 0))
66         return 1;
67 
68     if (test__multi3(1, 1, 1))
69         return 1;
70     if (test__multi3(1, 10, 10))
71         return 1;
72     if (test__multi3(10, 1, 10))
73         return 1;
74     if (test__multi3(1, 81985529216486895LL, 81985529216486895LL))
75         return 1;
76     if (test__multi3(81985529216486895LL, 1, 81985529216486895LL))
77         return 1;
78 
79     if (test__multi3(1, -1, -1))
80         return 1;
81     if (test__multi3(1, -10, -10))
82         return 1;
83     if (test__multi3(-10, 1, -10))
84         return 1;
85     if (test__multi3(1, -81985529216486895LL, -81985529216486895LL))
86         return 1;
87     if (test__multi3(-81985529216486895LL, 1, -81985529216486895LL))
88         return 1;
89 
90     if (test__multi3(3037000499LL, 3037000499LL, 9223372030926249001LL))
91         return 1;
92     if (test__multi3(-3037000499LL, 3037000499LL, -9223372030926249001LL))
93         return 1;
94     if (test__multi3(3037000499LL, -3037000499LL, -9223372030926249001LL))
95         return 1;
96     if (test__multi3(-3037000499LL, -3037000499LL, 9223372030926249001LL))
97         return 1;
98 
99     if (test__multi3(4398046511103LL, 2097152LL, 9223372036852678656LL))
100         return 1;
101     if (test__multi3(-4398046511103LL, 2097152LL, -9223372036852678656LL))
102         return 1;
103     if (test__multi3(4398046511103LL, -2097152LL, -9223372036852678656LL))
104         return 1;
105     if (test__multi3(-4398046511103LL, -2097152LL, 9223372036852678656LL))
106         return 1;
107 
108     if (test__multi3(2097152LL, 4398046511103LL, 9223372036852678656LL))
109         return 1;
110     if (test__multi3(-2097152LL, 4398046511103LL, -9223372036852678656LL))
111         return 1;
112     if (test__multi3(2097152LL, -4398046511103LL, -9223372036852678656LL))
113         return 1;
114     if (test__multi3(-2097152LL, -4398046511103LL, 9223372036852678656LL))
115         return 1;
116 
117     if (test__multi3(make_ti(0x00000000000000B5LL, 0x04F333F9DE5BE000LL),
118                      make_ti(0x0000000000000000LL, 0x00B504F333F9DE5BLL),
119                      make_ti(0x7FFFFFFFFFFFF328LL, 0xDF915DA296E8A000LL)))
120         return 1;
121 #else
122     printf("skipped\n");
123 #endif
124     return 0;
125 }
126