[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
This commit is contained in:
Ayke van Laethem
2020-04-22 20:25:22 +02:00
parent c1cb733db6
commit 4d41df6482
25 changed files with 60 additions and 51 deletions

View File

@@ -18,7 +18,7 @@
COMPILER_RT_ABI si_int __absvsi2(si_int a) {
const int N = (int)(sizeof(si_int) * CHAR_BIT);
if (a == (1 << (N - 1)))
if (a == ((si_int)1 << (N - 1)))
compilerrt_abort();
const si_int t = a >> (N - 1);
return (a ^ t) - t;

View File

@@ -16,7 +16,7 @@
// Precondition: 0 <= b < bits_in_dword
COMPILER_RT_ABI di_int __ashldi3(di_int a, si_int b) {
COMPILER_RT_ABI di_int __ashldi3(di_int a, int b) {
const int bits_in_word = (int)(sizeof(si_int) * CHAR_BIT);
dwords input;
dwords result;

View File

@@ -16,7 +16,7 @@
// Precondition: 0 <= b < bits_in_dword
COMPILER_RT_ABI di_int __ashrdi3(di_int a, si_int b) {
COMPILER_RT_ABI di_int __ashrdi3(di_int a, int b) {
const int bits_in_word = (int)(sizeof(si_int) * CHAR_BIT);
dwords input;
dwords result;

View File

@@ -30,6 +30,6 @@ COMPILER_RT_ABI si_int __clzdi2(di_int a) {
dwords x;
x.all = a;
const si_int f = -(x.s.high == 0);
return __builtin_clz((x.s.high & ~f) | (x.s.low & f)) +
return clzsi((x.s.high & ~f) | (x.s.low & f)) +
(f & ((si_int)(sizeof(si_int) * CHAR_BIT)));
}

View File

@@ -26,10 +26,10 @@ extern si_int __ctzsi2(si_int);
// Precondition: a != 0
COMPILER_RT_ABI si_int __ctzdi2(di_int a) {
COMPILER_RT_ABI int __ctzdi2(di_int a) {
dwords x;
x.all = a;
const si_int f = -(x.s.low == 0);
return __builtin_ctz((x.s.high & f) | (x.s.low & ~f)) +
return ctzsi((x.s.high & f) | (x.s.low & ~f)) +
(f & ((si_int)(sizeof(si_int) * CHAR_BIT)));
}

View File

@@ -15,13 +15,13 @@
// Returns: the index of the least significant 1-bit in a, or
// the value zero if a is zero. The least significant bit is index one.
COMPILER_RT_ABI si_int __ffsdi2(di_int a) {
COMPILER_RT_ABI int __ffsdi2(di_int a) {
dwords x;
x.all = a;
if (x.s.low == 0) {
if (x.s.high == 0)
return 0;
return __builtin_ctz(x.s.high) + (1 + sizeof(si_int) * CHAR_BIT);
return ctzsi(x.s.high) + (1 + sizeof(si_int) * CHAR_BIT);
}
return __builtin_ctz(x.s.low) + 1;
return ctzsi(x.s.low) + 1;
}

View File

@@ -15,9 +15,9 @@
// Returns: the index of the least significant 1-bit in a, or
// the value zero if a is zero. The least significant bit is index one.
COMPILER_RT_ABI si_int __ffssi2(si_int a) {
COMPILER_RT_ABI int __ffssi2(si_int a) {
if (a == 0) {
return 0;
}
return __builtin_ctz(a) + 1;
return ctzsi(a) + 1;
}

View File

@@ -26,7 +26,7 @@ COMPILER_RT_ABI float __floatdisf(di_int a) {
const di_int s = a >> (N - 1);
a = (a ^ s) - s;
int sd = N - __builtin_clzll(a); // number of significant digits
int e = sd - 1; // exponent
si_int e = sd - 1; // exponent
if (sd > FLT_MANT_DIG) {
// start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
// finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR

View File

@@ -17,7 +17,7 @@
#include "int_lib.h"
COMPILER_RT_ABI fp_t __floatsidf(int a) {
COMPILER_RT_ABI fp_t __floatsidf(si_int a) {
const int aWidth = sizeof a * CHAR_BIT;
@@ -33,14 +33,14 @@ COMPILER_RT_ABI fp_t __floatsidf(int a) {
}
// Exponent of (fp_t)a is the width of abs(a).
const int exponent = (aWidth - 1) - __builtin_clz(a);
const int exponent = (aWidth - 1) - clzsi(a);
rep_t result;
// Shift a into the significand field and clear the implicit bit. Extra
// cast to unsigned int is necessary to get the correct behavior for
// the input INT_MIN.
const int shift = significandBits - exponent;
result = (rep_t)(unsigned int)a << shift ^ implicitBit;
result = (rep_t)(su_int)a << shift ^ implicitBit;
// Insert the exponent
result += (rep_t)(exponent + exponentBias) << significandBits;
@@ -50,7 +50,7 @@ COMPILER_RT_ABI fp_t __floatsidf(int a) {
#if defined(__ARM_EABI__)
#if defined(COMPILER_RT_ARMHF_TARGET)
AEABI_RTABI fp_t __aeabi_i2d(int a) { return __floatsidf(a); }
AEABI_RTABI fp_t __aeabi_i2d(si_int a) { return __floatsidf(a); }
#else
COMPILER_RT_ALIAS(__floatsidf, __aeabi_i2d)
#endif

View File

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

View File

@@ -17,7 +17,7 @@
#include "int_lib.h"
COMPILER_RT_ABI fp_t __floatunsidf(unsigned int a) {
COMPILER_RT_ABI fp_t __floatunsidf(su_int a) {
const int aWidth = sizeof a * CHAR_BIT;
@@ -26,7 +26,7 @@ COMPILER_RT_ABI fp_t __floatunsidf(unsigned int a) {
return fromRep(0);
// Exponent of (fp_t)a is the width of abs(a).
const int exponent = (aWidth - 1) - __builtin_clz(a);
const int exponent = (aWidth - 1) - clzsi(a);
rep_t result;
// Shift a into the significand field and clear the implicit bit.
@@ -40,7 +40,7 @@ COMPILER_RT_ABI fp_t __floatunsidf(unsigned int a) {
#if defined(__ARM_EABI__)
#if defined(COMPILER_RT_ARMHF_TARGET)
AEABI_RTABI fp_t __aeabi_ui2d(unsigned int a) { return __floatunsidf(a); }
AEABI_RTABI fp_t __aeabi_ui2d(su_int a) { return __floatunsidf(a); }
#else
COMPILER_RT_ALIAS(__floatunsidf, __aeabi_ui2d)
#endif

View File

@@ -21,7 +21,7 @@ typedef float src_t;
typedef uint32_t src_rep_t;
#define SRC_REP_C UINT32_C
static const int srcSigBits = 23;
#define src_rep_t_clz __builtin_clz
#define src_rep_t_clz clzsi
#elif defined SRC_DOUBLE
typedef double src_t;

View File

@@ -69,9 +69,9 @@ static __inline int rep_clz(rep_t a) {
return __builtin_clzl(a);
#else
if (a & REP_C(0xffffffff00000000))
return __builtin_clz(a >> 32);
return clzsi(a >> 32);
else
return 32 + __builtin_clz(a & REP_C(0xffffffff));
return 32 + clzsi(a & REP_C(0xffffffff));
#endif
}

View File

@@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//
#define clz(a) (sizeof(a) == sizeof(unsigned long long) ? __builtin_clzll(a) : __builtin_clz(a))
#define clz(a) (sizeof(a) == sizeof(unsigned long long) ? __builtin_clzll(a) : clzsi(a))
// Adapted from Figure 3-40 of The PowerPC Compiler Writer's Guide
static __inline fixuint_t __udivXi3(fixuint_t n, fixuint_t d) {

View File

@@ -22,11 +22,20 @@
#ifdef si_int
#undef si_int
#endif
typedef int si_int;
typedef unsigned su_int;
typedef int32_t si_int;
typedef uint32_t su_int;
#if UINT_MAX == 0xFFFFFFFF
#define clzsi __builtin_clz
#define ctzsi __builtin_ctz
#elif ULONG_MAX == 0xFFFFFFFF
#define clzsi __builtin_clzl
#define ctzsi __builtin_ctzl
#else
#error could not determine appropriate clzsi macro for this system
#endif
typedef long long di_int;
typedef unsigned long long du_int;
typedef int64_t di_int;
typedef uint64_t du_int;
typedef union {
di_int all;

View File

@@ -16,7 +16,7 @@
// Precondition: 0 <= b < bits_in_dword
COMPILER_RT_ABI di_int __lshrdi3(di_int a, si_int b) {
COMPILER_RT_ABI di_int __lshrdi3(di_int a, int b) {
const int bits_in_word = (int)(sizeof(si_int) * CHAR_BIT);
udwords input;
udwords result;

View File

@@ -14,7 +14,7 @@
// Returns: count of 1 bits
COMPILER_RT_ABI si_int __popcountdi2(di_int a) {
COMPILER_RT_ABI int __popcountdi2(di_int a) {
du_int x2 = (du_int)a;
x2 = x2 - ((x2 >> 1) & 0x5555555555555555uLL);
// Every 2 bits holds the sum of every pair of bits (32)

View File

@@ -87,7 +87,7 @@ COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem) {
// K K
// ---
// K 0
sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
sr = clzsi(d.s.high) - clzsi(n.s.high);
// 0 <= sr <= n_uword_bits - 2 or sr large
if (sr > n_uword_bits - 2) {
if (rem)
@@ -120,7 +120,7 @@ COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem) {
// K X
// ---
// 0 K
sr = 1 + n_uword_bits + __builtin_clz(d.s.low) - __builtin_clz(n.s.high);
sr = 1 + n_uword_bits + clzsi(d.s.low) - clzsi(n.s.high);
// 2 <= sr <= n_udword_bits - 1
// q.all = n.all << (n_udword_bits - sr);
// r.all = n.all >> sr;
@@ -145,7 +145,7 @@ COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem) {
// K X
// ---
// K K
sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
sr = clzsi(d.s.high) - clzsi(n.s.high);
// 0 <= sr <= n_uword_bits - 1 or sr large
if (sr > n_uword_bits - 1) {
if (rem)

View File

@@ -19,9 +19,9 @@
// Precondition: 0 <= b < bits_in_dword
COMPILER_RT_ABI di_int __ashldi3(di_int a, si_int b);
COMPILER_RT_ABI di_int __ashldi3(di_int a, int b);
int test__ashldi3(di_int a, si_int b, di_int expected)
int test__ashldi3(di_int a, int b, di_int expected)
{
di_int x = __ashldi3(a, b);
if (x != expected)

View File

@@ -19,9 +19,9 @@
// Precondition: 0 <= b < bits_in_dword
COMPILER_RT_ABI di_int __ashrdi3(di_int a, si_int b);
COMPILER_RT_ABI di_int __ashrdi3(di_int a, int b);
int test__ashrdi3(di_int a, si_int b, di_int expected)
int test__ashrdi3(di_int a, int b, di_int expected)
{
di_int x = __ashrdi3(a, b);
if (x != expected)

View File

@@ -19,11 +19,11 @@
// Precondition: a != 0
COMPILER_RT_ABI si_int __ctzdi2(di_int a);
COMPILER_RT_ABI int __ctzdi2(di_int a);
int test__ctzdi2(di_int a, si_int expected)
int test__ctzdi2(di_int a, int expected)
{
si_int x = __ctzdi2(a);
int x = __ctzdi2(a);
if (x != expected)
printf("error in __ctzdi2(0x%llX) = %d, expected %d\n", a, x, expected);
return x != expected;

View File

@@ -18,11 +18,11 @@
// Returns: the index of the least significant 1-bit in a, or
// the value zero if a is zero. The least significant bit is index one.
COMPILER_RT_ABI si_int __ffsdi2(di_int a);
COMPILER_RT_ABI int __ffsdi2(di_int a);
int test__ffsdi2(di_int a, si_int expected)
int test__ffsdi2(di_int a, int expected)
{
si_int x = __ffsdi2(a);
int x = __ffsdi2(a);
if (x != expected)
printf("error in __ffsdi2(0x%llX) = %d, expected %d\n", a, x, expected);
return x != expected;

View File

@@ -18,11 +18,11 @@
// Returns: the index of the least significant 1-bit in a, or
// the value zero if a is zero. The least significant bit is index one.
COMPILER_RT_ABI si_int __ffssi2(si_int a);
COMPILER_RT_ABI int __ffssi2(si_int a);
int test__ffssi2(si_int a, si_int expected)
int test__ffssi2(si_int a, int expected)
{
si_int x = __ffssi2(a);
int x = __ffssi2(a);
if (x != expected)
printf("error in __ffssi2(0x%X) = %d, expected %d\n", a, x, expected);
return x != expected;

View File

@@ -19,9 +19,9 @@
// Precondition: 0 <= b < bits_in_dword
COMPILER_RT_ABI di_int __lshrdi3(di_int a, si_int b);
COMPILER_RT_ABI di_int __lshrdi3(di_int a, int b);
int test__lshrdi3(di_int a, si_int b, di_int expected)
int test__lshrdi3(di_int a, int b, di_int expected)
{
di_int x = __lshrdi3(a, b);
if (x != expected)

View File

@@ -18,7 +18,7 @@
// Returns: count of 1 bits
COMPILER_RT_ABI si_int __popcountdi2(di_int a);
COMPILER_RT_ABI int __popcountdi2(di_int a);
int naive_popcount(di_int a)
{
@@ -30,8 +30,8 @@ int naive_popcount(di_int a)
int test__popcountdi2(di_int a)
{
si_int x = __popcountdi2(a);
si_int expected = naive_popcount(a);
int x = __popcountdi2(a);
int expected = naive_popcount(a);
if (x != expected)
printf("error in __popcountdi2(0x%llX) = %d, expected %d\n",
a, x, expected);