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

source code of compiler-rt/test/builtins/Unit/divti3_test.c