1 | // RUN: %clang_builtins %s %librt -o %t && %run %t |
2 | // REQUIRES: librt_has_divmodti4 |
3 | // REQUIRES: int128 |
4 | //===-- divmodti4_test.c - Test __divmodti4 -------------------------------===// |
5 | // |
6 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
7 | // See https://llvm.org/LICENSE.txt for license information. |
8 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
9 | // |
10 | //===----------------------------------------------------------------------===// |
11 | // |
12 | // This file tests __divmodti4 for the compiler_rt library. |
13 | // |
14 | //===----------------------------------------------------------------------===// |
15 | |
16 | #include "int_lib.h" |
17 | #include <stdio.h> |
18 | |
19 | #ifdef CRT_HAS_128BIT |
20 | |
21 | // Effects: if rem != 0, *rem = a % b |
22 | // Returns: a / b |
23 | |
24 | COMPILER_RT_ABI ti_int __divmodti4(ti_int a, ti_int b, ti_int* rem); |
25 | |
26 | int test__divmodti4(ti_int a, ti_int b, ti_int expected_q, ti_int expected_r) { |
27 | ti_int r; |
28 | ti_int q = __divmodti4(a, b, rem: &r); |
29 | if (q != expected_q || r != expected_r) |
30 | { |
31 | utwords at; |
32 | at.all = a; |
33 | utwords bt; |
34 | bt.all = b; |
35 | utwords expected_qt; |
36 | expected_qt.all = expected_q; |
37 | utwords expected_rt; |
38 | expected_rt.all = expected_r; |
39 | utwords qt; |
40 | qt.all = q; |
41 | utwords rt; |
42 | rt.all = r; |
43 | printf(format: "error in __divmodti4: 0x%.16llX%.16llX / 0x%.16llX%.16llX = " |
44 | "0x%.16llX%.16llX, R = 0x%.16llX%.16llX, expected 0x%.16llX%.16llX, " |
45 | "0x%.16llX%.16llX\n" , |
46 | at.s.high, at.s.low, bt.s.high, bt.s.low, qt.s.high, qt.s.low, |
47 | rt.s.high, rt.s.low, expected_qt.s.high, expected_qt.s.low, |
48 | expected_rt.s.high, expected_rt.s.low); |
49 | } |
50 | return !(q == expected_q && r == expected_r); |
51 | } |
52 | |
53 | char assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0}; |
54 | |
55 | tu_int tests[][4] = |
56 | { |
57 | { (ti_int) 0, (ti_int) 1, (ti_int) 0, (ti_int) 0 }, |
58 | { (ti_int) 0, (ti_int)-1, (ti_int) 0, (ti_int) 0 }, |
59 | { (ti_int) 2, (ti_int) 1, (ti_int) 2, (ti_int) 0 }, |
60 | { (ti_int) 2, (ti_int)-1, (ti_int)-2, (ti_int) 0 }, |
61 | { (ti_int)-2, (ti_int) 1, (ti_int)-2, (ti_int) 0 }, |
62 | { (ti_int)-2, (ti_int)-1, (ti_int) 2, (ti_int) 0 }, |
63 | { (ti_int) 5, (ti_int) 3, (ti_int) 1, (ti_int) 2 }, |
64 | { (ti_int) 5, (ti_int)-3, (ti_int)-1, (ti_int) 2 }, |
65 | { (ti_int)-5, (ti_int) 3, (ti_int)-1, (ti_int)-2 }, |
66 | { (ti_int)-5, (ti_int)-3, (ti_int) 1, (ti_int)-2 }, |
67 | { (ti_int)0x8000000000000000ULL << 64 | 0, (ti_int) 1, (ti_int)0x8000000000000000ULL << 64 | 0, (ti_int)0x0LL }, |
68 | { (ti_int)0x8000000000000000ULL << 64 | 0, (ti_int)-1, (ti_int)0x8000000000000000ULL << 64 | 0, (ti_int)0x0LL }, |
69 | { (ti_int)0x8000000000000000ULL << 64 | 0, (ti_int)-2, (ti_int)0x4000000000000000ULL << 64 | 0, (ti_int)0x0LL }, |
70 | { (ti_int)0x8000000000000000ULL << 64 | 0, (ti_int) 2, (ti_int)0xC000000000000000ULL << 64 | 0, (ti_int)0x0LL }, |
71 | { (ti_int)0x8000000000000000ULL << 64 | 0, (ti_int)-3, (ti_int)0x2AAAAAAAAAAAAAAAULL << 64 | 0xAAAAAAAAAAAAAAAAULL, (ti_int)-2 }, |
72 | { (ti_int)0x8000000000000000ULL << 64 | 0, (ti_int) 3, (ti_int)0xD555555555555555ULL << 64 | 0x5555555555555556ULL, (ti_int)-2 }, |
73 | }; |
74 | |
75 | #endif |
76 | |
77 | int main() |
78 | { |
79 | #ifdef CRT_HAS_128BIT |
80 | const unsigned N = sizeof(tests) / sizeof(tests[0]); |
81 | unsigned i; |
82 | for (i = 0; i < N; ++i) |
83 | if (test__divmodti4(a: tests[i][0], b: tests[i][1], expected_q: tests[i][2], expected_r: tests[i][3])) |
84 | return 1; |
85 | |
86 | |
87 | #else |
88 | printf("skipped\n" ); |
89 | #endif |
90 | return 0; |
91 | } |
92 | |