Skip to content

Commit 4d41df6

Browse files
committed
[builtins] Support architectures with 16-bit int
This is the first patch in a series to add support for the AVR target. This patch includes changes to make compiler-rt more target independent by not relying on the width of an int or long. Differential Revision: https://reviews.llvm.org/D78662
1 parent c1cb733 commit 4d41df6

25 files changed

+60
-51
lines changed

compiler-rt/lib/builtins/absvsi2.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
COMPILER_RT_ABI si_int __absvsi2(si_int a) {
2020
const int N = (int)(sizeof(si_int) * CHAR_BIT);
21-
if (a == (1 << (N - 1)))
21+
if (a == ((si_int)1 << (N - 1)))
2222
compilerrt_abort();
2323
const si_int t = a >> (N - 1);
2424
return (a ^ t) - t;

compiler-rt/lib/builtins/ashldi3.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// Precondition: 0 <= b < bits_in_dword
1818

19-
COMPILER_RT_ABI di_int __ashldi3(di_int a, si_int b) {
19+
COMPILER_RT_ABI di_int __ashldi3(di_int a, int b) {
2020
const int bits_in_word = (int)(sizeof(si_int) * CHAR_BIT);
2121
dwords input;
2222
dwords result;

compiler-rt/lib/builtins/ashrdi3.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// Precondition: 0 <= b < bits_in_dword
1818

19-
COMPILER_RT_ABI di_int __ashrdi3(di_int a, si_int b) {
19+
COMPILER_RT_ABI di_int __ashrdi3(di_int a, int b) {
2020
const int bits_in_word = (int)(sizeof(si_int) * CHAR_BIT);
2121
dwords input;
2222
dwords result;

compiler-rt/lib/builtins/clzdi2.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ COMPILER_RT_ABI si_int __clzdi2(di_int a) {
3030
dwords x;
3131
x.all = a;
3232
const si_int f = -(x.s.high == 0);
33-
return __builtin_clz((x.s.high & ~f) | (x.s.low & f)) +
33+
return clzsi((x.s.high & ~f) | (x.s.low & f)) +
3434
(f & ((si_int)(sizeof(si_int) * CHAR_BIT)));
3535
}

compiler-rt/lib/builtins/ctzdi2.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ extern si_int __ctzsi2(si_int);
2626

2727
// Precondition: a != 0
2828

29-
COMPILER_RT_ABI si_int __ctzdi2(di_int a) {
29+
COMPILER_RT_ABI int __ctzdi2(di_int a) {
3030
dwords x;
3131
x.all = a;
3232
const si_int f = -(x.s.low == 0);
33-
return __builtin_ctz((x.s.high & f) | (x.s.low & ~f)) +
33+
return ctzsi((x.s.high & f) | (x.s.low & ~f)) +
3434
(f & ((si_int)(sizeof(si_int) * CHAR_BIT)));
3535
}

compiler-rt/lib/builtins/ffsdi2.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
// Returns: the index of the least significant 1-bit in a, or
1616
// the value zero if a is zero. The least significant bit is index one.
1717

18-
COMPILER_RT_ABI si_int __ffsdi2(di_int a) {
18+
COMPILER_RT_ABI int __ffsdi2(di_int a) {
1919
dwords x;
2020
x.all = a;
2121
if (x.s.low == 0) {
2222
if (x.s.high == 0)
2323
return 0;
24-
return __builtin_ctz(x.s.high) + (1 + sizeof(si_int) * CHAR_BIT);
24+
return ctzsi(x.s.high) + (1 + sizeof(si_int) * CHAR_BIT);
2525
}
26-
return __builtin_ctz(x.s.low) + 1;
26+
return ctzsi(x.s.low) + 1;
2727
}

compiler-rt/lib/builtins/ffssi2.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
// Returns: the index of the least significant 1-bit in a, or
1616
// the value zero if a is zero. The least significant bit is index one.
1717

18-
COMPILER_RT_ABI si_int __ffssi2(si_int a) {
18+
COMPILER_RT_ABI int __ffssi2(si_int a) {
1919
if (a == 0) {
2020
return 0;
2121
}
22-
return __builtin_ctz(a) + 1;
22+
return ctzsi(a) + 1;
2323
}

compiler-rt/lib/builtins/floatdisf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ COMPILER_RT_ABI float __floatdisf(di_int a) {
2626
const di_int s = a >> (N - 1);
2727
a = (a ^ s) - s;
2828
int sd = N - __builtin_clzll(a); // number of significant digits
29-
int e = sd - 1; // exponent
29+
si_int e = sd - 1; // exponent
3030
if (sd > FLT_MANT_DIG) {
3131
// start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
3232
// finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR

compiler-rt/lib/builtins/floatsidf.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include "int_lib.h"
1919

20-
COMPILER_RT_ABI fp_t __floatsidf(int a) {
20+
COMPILER_RT_ABI fp_t __floatsidf(si_int a) {
2121

2222
const int aWidth = sizeof a * CHAR_BIT;
2323

@@ -33,14 +33,14 @@ COMPILER_RT_ABI fp_t __floatsidf(int a) {
3333
}
3434

3535
// Exponent of (fp_t)a is the width of abs(a).
36-
const int exponent = (aWidth - 1) - __builtin_clz(a);
36+
const int exponent = (aWidth - 1) - clzsi(a);
3737
rep_t result;
3838

3939
// Shift a into the significand field and clear the implicit bit. Extra
4040
// cast to unsigned int is necessary to get the correct behavior for
4141
// the input INT_MIN.
4242
const int shift = significandBits - exponent;
43-
result = (rep_t)(unsigned int)a << shift ^ implicitBit;
43+
result = (rep_t)(su_int)a << shift ^ implicitBit;
4444

4545
// Insert the exponent
4646
result += (rep_t)(exponent + exponentBias) << significandBits;
@@ -50,7 +50,7 @@ COMPILER_RT_ABI fp_t __floatsidf(int a) {
5050

5151
#if defined(__ARM_EABI__)
5252
#if defined(COMPILER_RT_ARMHF_TARGET)
53-
AEABI_RTABI fp_t __aeabi_i2d(int a) { return __floatsidf(a); }
53+
AEABI_RTABI fp_t __aeabi_i2d(si_int a) { return __floatsidf(a); }
5454
#else
5555
COMPILER_RT_ALIAS(__floatsidf, __aeabi_i2d)
5656
#endif

compiler-rt/lib/builtins/floatundisf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ COMPILER_RT_ABI float __floatundisf(du_int a) {
2424
return 0.0F;
2525
const unsigned N = sizeof(du_int) * CHAR_BIT;
2626
int sd = N - __builtin_clzll(a); // number of significant digits
27-
int e = sd - 1; // 8 exponent
27+
si_int e = sd - 1; // 8 exponent
2828
if (sd > FLT_MANT_DIG) {
2929
// start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
3030
// finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR

compiler-rt/lib/builtins/floatunsidf.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include "int_lib.h"
1919

20-
COMPILER_RT_ABI fp_t __floatunsidf(unsigned int a) {
20+
COMPILER_RT_ABI fp_t __floatunsidf(su_int a) {
2121

2222
const int aWidth = sizeof a * CHAR_BIT;
2323

@@ -26,7 +26,7 @@ COMPILER_RT_ABI fp_t __floatunsidf(unsigned int a) {
2626
return fromRep(0);
2727

2828
// Exponent of (fp_t)a is the width of abs(a).
29-
const int exponent = (aWidth - 1) - __builtin_clz(a);
29+
const int exponent = (aWidth - 1) - clzsi(a);
3030
rep_t result;
3131

3232
// Shift a into the significand field and clear the implicit bit.
@@ -40,7 +40,7 @@ COMPILER_RT_ABI fp_t __floatunsidf(unsigned int a) {
4040

4141
#if defined(__ARM_EABI__)
4242
#if defined(COMPILER_RT_ARMHF_TARGET)
43-
AEABI_RTABI fp_t __aeabi_ui2d(unsigned int a) { return __floatunsidf(a); }
43+
AEABI_RTABI fp_t __aeabi_ui2d(su_int a) { return __floatunsidf(a); }
4444
#else
4545
COMPILER_RT_ALIAS(__floatunsidf, __aeabi_ui2d)
4646
#endif

compiler-rt/lib/builtins/fp_extend.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef float src_t;
2121
typedef uint32_t src_rep_t;
2222
#define SRC_REP_C UINT32_C
2323
static const int srcSigBits = 23;
24-
#define src_rep_t_clz __builtin_clz
24+
#define src_rep_t_clz clzsi
2525

2626
#elif defined SRC_DOUBLE
2727
typedef double src_t;

compiler-rt/lib/builtins/fp_lib.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ static __inline int rep_clz(rep_t a) {
6969
return __builtin_clzl(a);
7070
#else
7171
if (a & REP_C(0xffffffff00000000))
72-
return __builtin_clz(a >> 32);
72+
return clzsi(a >> 32);
7373
else
74-
return 32 + __builtin_clz(a & REP_C(0xffffffff));
74+
return 32 + clzsi(a & REP_C(0xffffffff));
7575
#endif
7676
}
7777

compiler-rt/lib/builtins/int_div_impl.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#define clz(a) (sizeof(a) == sizeof(unsigned long long) ? __builtin_clzll(a) : __builtin_clz(a))
13+
#define clz(a) (sizeof(a) == sizeof(unsigned long long) ? __builtin_clzll(a) : clzsi(a))
1414

1515
// Adapted from Figure 3-40 of The PowerPC Compiler Writer's Guide
1616
static __inline fixuint_t __udivXi3(fixuint_t n, fixuint_t d) {

compiler-rt/lib/builtins/int_types.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@
2222
#ifdef si_int
2323
#undef si_int
2424
#endif
25-
typedef int si_int;
26-
typedef unsigned su_int;
25+
typedef int32_t si_int;
26+
typedef uint32_t su_int;
27+
#if UINT_MAX == 0xFFFFFFFF
28+
#define clzsi __builtin_clz
29+
#define ctzsi __builtin_ctz
30+
#elif ULONG_MAX == 0xFFFFFFFF
31+
#define clzsi __builtin_clzl
32+
#define ctzsi __builtin_ctzl
33+
#else
34+
#error could not determine appropriate clzsi macro for this system
35+
#endif
2736

28-
typedef long long di_int;
29-
typedef unsigned long long du_int;
37+
typedef int64_t di_int;
38+
typedef uint64_t du_int;
3039

3140
typedef union {
3241
di_int all;

compiler-rt/lib/builtins/lshrdi3.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// Precondition: 0 <= b < bits_in_dword
1818

19-
COMPILER_RT_ABI di_int __lshrdi3(di_int a, si_int b) {
19+
COMPILER_RT_ABI di_int __lshrdi3(di_int a, int b) {
2020
const int bits_in_word = (int)(sizeof(si_int) * CHAR_BIT);
2121
udwords input;
2222
udwords result;

compiler-rt/lib/builtins/popcountdi2.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// Returns: count of 1 bits
1616

17-
COMPILER_RT_ABI si_int __popcountdi2(di_int a) {
17+
COMPILER_RT_ABI int __popcountdi2(di_int a) {
1818
du_int x2 = (du_int)a;
1919
x2 = x2 - ((x2 >> 1) & 0x5555555555555555uLL);
2020
// Every 2 bits holds the sum of every pair of bits (32)

compiler-rt/lib/builtins/udivmoddi4.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem) {
8787
// K K
8888
// ---
8989
// K 0
90-
sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
90+
sr = clzsi(d.s.high) - clzsi(n.s.high);
9191
// 0 <= sr <= n_uword_bits - 2 or sr large
9292
if (sr > n_uword_bits - 2) {
9393
if (rem)
@@ -120,7 +120,7 @@ COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem) {
120120
// K X
121121
// ---
122122
// 0 K
123-
sr = 1 + n_uword_bits + __builtin_clz(d.s.low) - __builtin_clz(n.s.high);
123+
sr = 1 + n_uword_bits + clzsi(d.s.low) - clzsi(n.s.high);
124124
// 2 <= sr <= n_udword_bits - 1
125125
// q.all = n.all << (n_udword_bits - sr);
126126
// r.all = n.all >> sr;
@@ -145,7 +145,7 @@ COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem) {
145145
// K X
146146
// ---
147147
// K K
148-
sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
148+
sr = clzsi(d.s.high) - clzsi(n.s.high);
149149
// 0 <= sr <= n_uword_bits - 1 or sr large
150150
if (sr > n_uword_bits - 1) {
151151
if (rem)

compiler-rt/test/builtins/Unit/ashldi3_test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
// Precondition: 0 <= b < bits_in_dword
2121

22-
COMPILER_RT_ABI di_int __ashldi3(di_int a, si_int b);
22+
COMPILER_RT_ABI di_int __ashldi3(di_int a, int b);
2323

24-
int test__ashldi3(di_int a, si_int b, di_int expected)
24+
int test__ashldi3(di_int a, int b, di_int expected)
2525
{
2626
di_int x = __ashldi3(a, b);
2727
if (x != expected)

compiler-rt/test/builtins/Unit/ashrdi3_test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
// Precondition: 0 <= b < bits_in_dword
2121

22-
COMPILER_RT_ABI di_int __ashrdi3(di_int a, si_int b);
22+
COMPILER_RT_ABI di_int __ashrdi3(di_int a, int b);
2323

24-
int test__ashrdi3(di_int a, si_int b, di_int expected)
24+
int test__ashrdi3(di_int a, int b, di_int expected)
2525
{
2626
di_int x = __ashrdi3(a, b);
2727
if (x != expected)

compiler-rt/test/builtins/Unit/ctzdi2_test.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
// Precondition: a != 0
2121

22-
COMPILER_RT_ABI si_int __ctzdi2(di_int a);
22+
COMPILER_RT_ABI int __ctzdi2(di_int a);
2323

24-
int test__ctzdi2(di_int a, si_int expected)
24+
int test__ctzdi2(di_int a, int expected)
2525
{
26-
si_int x = __ctzdi2(a);
26+
int x = __ctzdi2(a);
2727
if (x != expected)
2828
printf("error in __ctzdi2(0x%llX) = %d, expected %d\n", a, x, expected);
2929
return x != expected;

compiler-rt/test/builtins/Unit/ffsdi2_test.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
// Returns: the index of the least significant 1-bit in a, or
1919
// the value zero if a is zero. The least significant bit is index one.
2020

21-
COMPILER_RT_ABI si_int __ffsdi2(di_int a);
21+
COMPILER_RT_ABI int __ffsdi2(di_int a);
2222

23-
int test__ffsdi2(di_int a, si_int expected)
23+
int test__ffsdi2(di_int a, int expected)
2424
{
25-
si_int x = __ffsdi2(a);
25+
int x = __ffsdi2(a);
2626
if (x != expected)
2727
printf("error in __ffsdi2(0x%llX) = %d, expected %d\n", a, x, expected);
2828
return x != expected;

compiler-rt/test/builtins/Unit/ffssi2_test.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
// Returns: the index of the least significant 1-bit in a, or
1919
// the value zero if a is zero. The least significant bit is index one.
2020

21-
COMPILER_RT_ABI si_int __ffssi2(si_int a);
21+
COMPILER_RT_ABI int __ffssi2(si_int a);
2222

23-
int test__ffssi2(si_int a, si_int expected)
23+
int test__ffssi2(si_int a, int expected)
2424
{
25-
si_int x = __ffssi2(a);
25+
int x = __ffssi2(a);
2626
if (x != expected)
2727
printf("error in __ffssi2(0x%X) = %d, expected %d\n", a, x, expected);
2828
return x != expected;

compiler-rt/test/builtins/Unit/lshrdi3_test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
// Precondition: 0 <= b < bits_in_dword
2121

22-
COMPILER_RT_ABI di_int __lshrdi3(di_int a, si_int b);
22+
COMPILER_RT_ABI di_int __lshrdi3(di_int a, int b);
2323

24-
int test__lshrdi3(di_int a, si_int b, di_int expected)
24+
int test__lshrdi3(di_int a, int b, di_int expected)
2525
{
2626
di_int x = __lshrdi3(a, b);
2727
if (x != expected)

compiler-rt/test/builtins/Unit/popcountdi2_test.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
// Returns: count of 1 bits
2020

21-
COMPILER_RT_ABI si_int __popcountdi2(di_int a);
21+
COMPILER_RT_ABI int __popcountdi2(di_int a);
2222

2323
int naive_popcount(di_int a)
2424
{
@@ -30,8 +30,8 @@ int naive_popcount(di_int a)
3030

3131
int test__popcountdi2(di_int a)
3232
{
33-
si_int x = __popcountdi2(a);
34-
si_int expected = naive_popcount(a);
33+
int x = __popcountdi2(a);
34+
int expected = naive_popcount(a);
3535
if (x != expected)
3636
printf("error in __popcountdi2(0x%llX) = %d, expected %d\n",
3737
a, x, expected);

0 commit comments

Comments
 (0)