diff --git a/compiler-rt/lib/builtins/absvsi2.c b/compiler-rt/lib/builtins/absvsi2.c --- a/compiler-rt/lib/builtins/absvsi2.c +++ b/compiler-rt/lib/builtins/absvsi2.c @@ -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; diff --git a/compiler-rt/lib/builtins/ashldi3.c b/compiler-rt/lib/builtins/ashldi3.c --- a/compiler-rt/lib/builtins/ashldi3.c +++ b/compiler-rt/lib/builtins/ashldi3.c @@ -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; diff --git a/compiler-rt/lib/builtins/ashrdi3.c b/compiler-rt/lib/builtins/ashrdi3.c --- a/compiler-rt/lib/builtins/ashrdi3.c +++ b/compiler-rt/lib/builtins/ashrdi3.c @@ -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; diff --git a/compiler-rt/lib/builtins/clzdi2.c b/compiler-rt/lib/builtins/clzdi2.c --- a/compiler-rt/lib/builtins/clzdi2.c +++ b/compiler-rt/lib/builtins/clzdi2.c @@ -24,12 +24,14 @@ extern si_int __clzsi2(si_int); #endif +#define clz(a) (sizeof(a) == sizeof(unsigned long) ? __builtin_clzl(a) : __builtin_clz(a)) + // Precondition: a != 0 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 clz((x.s.high & ~f) | (x.s.low & f)) + (f & ((si_int)(sizeof(si_int) * CHAR_BIT))); } diff --git a/compiler-rt/lib/builtins/ctzdi2.c b/compiler-rt/lib/builtins/ctzdi2.c --- a/compiler-rt/lib/builtins/ctzdi2.c +++ b/compiler-rt/lib/builtins/ctzdi2.c @@ -26,10 +26,10 @@ // 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 __builtin_ctzl((x.s.high & f) | (x.s.low & ~f)) + (f & ((si_int)(sizeof(si_int) * CHAR_BIT))); } diff --git a/compiler-rt/lib/builtins/ffsdi2.c b/compiler-rt/lib/builtins/ffsdi2.c --- a/compiler-rt/lib/builtins/ffsdi2.c +++ b/compiler-rt/lib/builtins/ffsdi2.c @@ -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 __builtin_ctzl(x.s.high) + (1 + sizeof(si_int) * CHAR_BIT); } - return __builtin_ctz(x.s.low) + 1; + return __builtin_ctzl(x.s.low) + 1; } diff --git a/compiler-rt/lib/builtins/ffssi2.c b/compiler-rt/lib/builtins/ffssi2.c --- a/compiler-rt/lib/builtins/ffssi2.c +++ b/compiler-rt/lib/builtins/ffssi2.c @@ -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 __builtin_ctzl(a) + 1; } diff --git a/compiler-rt/lib/builtins/floatdisf.c b/compiler-rt/lib/builtins/floatdisf.c --- a/compiler-rt/lib/builtins/floatdisf.c +++ b/compiler-rt/lib/builtins/floatdisf.c @@ -26,7 +26,7 @@ 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 + int32_t e = sd - 1; // exponent if (sd > FLT_MANT_DIG) { // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR diff --git a/compiler-rt/lib/builtins/floatsidf.c b/compiler-rt/lib/builtins/floatsidf.c --- a/compiler-rt/lib/builtins/floatsidf.c +++ b/compiler-rt/lib/builtins/floatsidf.c @@ -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 @@ } // Exponent of (fp_t)a is the width of abs(a). - const int exponent = (aWidth - 1) - __builtin_clz(a); + const int exponent = (aWidth - 1) - __builtin_clzl(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 @@ #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 diff --git a/compiler-rt/lib/builtins/floatundisf.c b/compiler-rt/lib/builtins/floatundisf.c --- a/compiler-rt/lib/builtins/floatundisf.c +++ b/compiler-rt/lib/builtins/floatundisf.c @@ -24,7 +24,7 @@ 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 + int32_t e = sd - 1; // 8 exponent if (sd > FLT_MANT_DIG) { // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR diff --git a/compiler-rt/lib/builtins/floatunsidf.c b/compiler-rt/lib/builtins/floatunsidf.c --- a/compiler-rt/lib/builtins/floatunsidf.c +++ b/compiler-rt/lib/builtins/floatunsidf.c @@ -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 @@ 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) - __builtin_clzl(a); rep_t result; // Shift a into the significand field and clear the implicit bit. @@ -40,7 +40,7 @@ #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 diff --git a/compiler-rt/lib/builtins/fp_extend.h b/compiler-rt/lib/builtins/fp_extend.h --- a/compiler-rt/lib/builtins/fp_extend.h +++ b/compiler-rt/lib/builtins/fp_extend.h @@ -21,7 +21,13 @@ typedef uint32_t src_rep_t; #define SRC_REP_C UINT32_C static const int srcSigBits = 23; +#if UINT_MAX == 65535 +// 16-bit int +#define src_rep_t_clz __builtin_clzl +#else +// other int (probably 32-bit or 64-bit) #define src_rep_t_clz __builtin_clz +#endif #elif defined SRC_DOUBLE typedef double src_t; diff --git a/compiler-rt/lib/builtins/fp_lib.h b/compiler-rt/lib/builtins/fp_lib.h --- a/compiler-rt/lib/builtins/fp_lib.h +++ b/compiler-rt/lib/builtins/fp_lib.h @@ -69,9 +69,9 @@ return __builtin_clzl(a); #else if (a & REP_C(0xffffffff00000000)) - return __builtin_clz(a >> 32); + return __builtin_clzl(a >> 32); else - return 32 + __builtin_clz(a & REP_C(0xffffffff)); + return 32 + __builtin_clzl(a & REP_C(0xffffffff)); #endif } diff --git a/compiler-rt/lib/builtins/int_div_impl.inc b/compiler-rt/lib/builtins/int_div_impl.inc --- a/compiler-rt/lib/builtins/int_div_impl.inc +++ b/compiler-rt/lib/builtins/int_div_impl.inc @@ -10,7 +10,8 @@ // //===----------------------------------------------------------------------===// -#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) : \ + (sizeof(a) == sizeof(unsigned long) ? __builtin_clzl(a) : __builtin_clz(a))) // Adapted from Figure 3-40 of The PowerPC Compiler Writer's Guide static __inline fixuint_t __udivXi3(fixuint_t n, fixuint_t d) { diff --git a/compiler-rt/lib/builtins/int_types.h b/compiler-rt/lib/builtins/int_types.h --- a/compiler-rt/lib/builtins/int_types.h +++ b/compiler-rt/lib/builtins/int_types.h @@ -22,11 +22,11 @@ #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; -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; diff --git a/compiler-rt/lib/builtins/lshrdi3.c b/compiler-rt/lib/builtins/lshrdi3.c --- a/compiler-rt/lib/builtins/lshrdi3.c +++ b/compiler-rt/lib/builtins/lshrdi3.c @@ -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; diff --git a/compiler-rt/lib/builtins/popcountdi2.c b/compiler-rt/lib/builtins/popcountdi2.c --- a/compiler-rt/lib/builtins/popcountdi2.c +++ b/compiler-rt/lib/builtins/popcountdi2.c @@ -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) diff --git a/compiler-rt/lib/builtins/udivmoddi4.c b/compiler-rt/lib/builtins/udivmoddi4.c --- a/compiler-rt/lib/builtins/udivmoddi4.c +++ b/compiler-rt/lib/builtins/udivmoddi4.c @@ -87,7 +87,7 @@ // K K // --- // K 0 - sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high); + sr = __builtin_clzl(d.s.high) - __builtin_clzl(n.s.high); // 0 <= sr <= n_uword_bits - 2 or sr large if (sr > n_uword_bits - 2) { if (rem) @@ -120,7 +120,7 @@ // K X // --- // 0 K - sr = 1 + n_uword_bits + __builtin_clz(d.s.low) - __builtin_clz(n.s.high); + sr = 1 + n_uword_bits + __builtin_clzl(d.s.low) - __builtin_clzl(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 @@ // K X // --- // K K - sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high); + sr = __builtin_clzl(d.s.high) - __builtin_clzl(n.s.high); // 0 <= sr <= n_uword_bits - 1 or sr large if (sr > n_uword_bits - 1) { if (rem) diff --git a/compiler-rt/test/builtins/Unit/ashldi3_test.c b/compiler-rt/test/builtins/Unit/ashldi3_test.c --- a/compiler-rt/test/builtins/Unit/ashldi3_test.c +++ b/compiler-rt/test/builtins/Unit/ashldi3_test.c @@ -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) diff --git a/compiler-rt/test/builtins/Unit/ashrdi3_test.c b/compiler-rt/test/builtins/Unit/ashrdi3_test.c --- a/compiler-rt/test/builtins/Unit/ashrdi3_test.c +++ b/compiler-rt/test/builtins/Unit/ashrdi3_test.c @@ -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) diff --git a/compiler-rt/test/builtins/Unit/ctzdi2_test.c b/compiler-rt/test/builtins/Unit/ctzdi2_test.c --- a/compiler-rt/test/builtins/Unit/ctzdi2_test.c +++ b/compiler-rt/test/builtins/Unit/ctzdi2_test.c @@ -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; diff --git a/compiler-rt/test/builtins/Unit/ffsdi2_test.c b/compiler-rt/test/builtins/Unit/ffsdi2_test.c --- a/compiler-rt/test/builtins/Unit/ffsdi2_test.c +++ b/compiler-rt/test/builtins/Unit/ffsdi2_test.c @@ -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; diff --git a/compiler-rt/test/builtins/Unit/ffssi2_test.c b/compiler-rt/test/builtins/Unit/ffssi2_test.c --- a/compiler-rt/test/builtins/Unit/ffssi2_test.c +++ b/compiler-rt/test/builtins/Unit/ffssi2_test.c @@ -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; diff --git a/compiler-rt/test/builtins/Unit/lshrdi3_test.c b/compiler-rt/test/builtins/Unit/lshrdi3_test.c --- a/compiler-rt/test/builtins/Unit/lshrdi3_test.c +++ b/compiler-rt/test/builtins/Unit/lshrdi3_test.c @@ -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) diff --git a/compiler-rt/test/builtins/Unit/popcountdi2_test.c b/compiler-rt/test/builtins/Unit/popcountdi2_test.c --- a/compiler-rt/test/builtins/Unit/popcountdi2_test.c +++ b/compiler-rt/test/builtins/Unit/popcountdi2_test.c @@ -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 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);