Skip to content

Commit c9af340

Browse files
committed
Add __divmodti4 to match libgcc.
gcc has used this on x86-64 since at least version 7. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D80506
1 parent 57dd927 commit c9af340

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ set(GENERIC_SOURCES
7171
divdi3.c
7272
divmoddi4.c
7373
divmodsi4.c
74+
divmodti4.c
7475
divsc3.c
7576
divsf3.c
7677
divsi3.c

compiler-rt/lib/builtins/README.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ du_int __udivmoddi4(du_int a, du_int b, du_int* rem); // a / b, *rem = a % b u
8787
tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem); // a / b, *rem = a % b unsigned
8888
su_int __udivmodsi4(su_int a, su_int b, su_int* rem); // a / b, *rem = a % b unsigned
8989
si_int __divmodsi4(si_int a, si_int b, si_int* rem); // a / b, *rem = a % b signed
90+
di_int __divmoddi4(di_int a, di_int b, di_int* rem); // a / b, *rem = a % b signed
91+
ti_int __divmodti4(ti_int a, ti_int b, ti_int* rem); // a / b, *rem = a % b signed
9092

9193

9294

compiler-rt/lib/builtins/divmodti4.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- divmodti4.c - Implement __divmodti4 -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements __divmodti4 for the compiler_rt library.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "int_lib.h"
14+
15+
#ifdef CRT_HAS_128BIT
16+
17+
// Returns: a / b, *rem = a % b
18+
19+
COMPILER_RT_ABI ti_int __divmodti4(ti_int a, ti_int b, ti_int *rem) {
20+
const int bits_in_tword_m1 = (int)(sizeof(ti_int) * CHAR_BIT) - 1;
21+
ti_int s_a = a >> bits_in_tword_m1; // s_a = a < 0 ? -1 : 0
22+
ti_int s_b = b >> bits_in_tword_m1; // s_b = b < 0 ? -1 : 0
23+
a = (a ^ s_a) - s_a; // negate if s_a == -1
24+
b = (b ^ s_b) - s_b; // negate if s_b == -1
25+
s_b ^= s_a; // sign of quotient
26+
tu_int r;
27+
ti_int q = (__udivmodti4(a, b, &r) ^ s_b) - s_b; // negate if s_b == -1
28+
*rem = (r ^ s_a) - s_a; // negate if s_a == -1
29+
return q;
30+
}
31+
32+
#endif // CRT_HAS_128BIT
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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, &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("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)0x8000000000000000LL << 64 | 0, (ti_int) 1, (ti_int)0x8000000000000000LL << 64 | 0, (ti_int)0x0LL },
68+
{ (ti_int)0x8000000000000000LL << 64 | 0, (ti_int)-1, (ti_int)0x8000000000000000LL << 64 | 0, (ti_int)0x0LL },
69+
{ (ti_int)0x8000000000000000LL << 64 | 0, (ti_int)-2, (ti_int)0x4000000000000000LL << 64 | 0, (ti_int)0x0LL },
70+
{ (ti_int)0x8000000000000000LL << 64 | 0, (ti_int) 2, (ti_int)0xC000000000000000LL << 64 | 0, (ti_int)0x0LL },
71+
{ (ti_int)0x8000000000000000LL << 64 | 0, (ti_int)-3, (ti_int)0x2AAAAAAAAAAAAAAALL << 64 | 0xAAAAAAAAAAAAAAAALL, (ti_int)-2 },
72+
{ (ti_int)0x8000000000000000LL << 64 | 0, (ti_int) 3, (ti_int)0xD555555555555555LL << 64 | 0x5555555555555556LL, (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(tests[i][0], tests[i][1], tests[i][2], tests[i][3]))
84+
return 1;
85+
86+
87+
#else
88+
printf("skipped\n");
89+
#endif
90+
return 0;
91+
}

0 commit comments

Comments
 (0)