Skip to content

Commit

Permalink
[builtins] Change si_int to int in some helper declarations
Browse files Browse the repository at this point in the history
This patch changes types of some integer function arguments or return values from `si_int` to the default `int` type to make it more compatible with `libgcc`.

The compiler-rt/lib/builtins/README.txt has a link to the [libgcc specification](http://gcc.gnu.org/onlinedocs/gccint/Libgcc.html#Libgcc). This specification has an explicit note on `int`, `float` and other such types being just illustrations in some cases while the actual types are expressed with machine modes.

Such usage of always-32-bit-wide integer type may lead to issues on 16-bit platforms such as MSP430. Provided [libgcc2.h](https://gcc.gnu.org/git/?p=gcc.git;a=blob_plain;f=libgcc/libgcc2.h;hb=HEAD) can be used as a reference for all targets supported by the libgcc, this patch fixes some existing differences in helper declarations.

This patch is expected to not change behavior at all for targets with 32-bit `int` type.

Differential Revision: https://reviews.llvm.org/D81285
  • Loading branch information
atrosinenko authored and asl committed Jun 30, 2020
1 parent 19e7571 commit 0ee439b
Show file tree
Hide file tree
Showing 33 changed files with 183 additions and 178 deletions.
51 changes: 28 additions & 23 deletions compiler-rt/lib/builtins/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ Here is the specification for this library:

http://gcc.gnu.org/onlinedocs/gccint/Libgcc.html#Libgcc

Please note that the libgcc specification explicitly mentions actual types of
arguments and returned values being expressed with machine modes.
In some cases particular types such as "int", "unsigned", "long long", etc.
may be specified just as examples there.

Here is a synopsis of the contents of this library:

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;

// Integral bit manipulation

Expand All @@ -38,24 +43,24 @@ ti_int __ashrti3(ti_int a, si_int b); // a >> b arithmetic (sign fill)
di_int __lshrdi3(di_int a, si_int b); // a >> b logical (zero fill)
ti_int __lshrti3(ti_int a, si_int b); // a >> b logical (zero fill)

si_int __clzsi2(si_int a); // count leading zeros
si_int __clzdi2(di_int a); // count leading zeros
si_int __clzti2(ti_int a); // count leading zeros
si_int __ctzsi2(si_int a); // count trailing zeros
si_int __ctzdi2(di_int a); // count trailing zeros
si_int __ctzti2(ti_int a); // count trailing zeros
int __clzsi2(si_int a); // count leading zeros
int __clzdi2(di_int a); // count leading zeros
int __clzti2(ti_int a); // count leading zeros
int __ctzsi2(si_int a); // count trailing zeros
int __ctzdi2(di_int a); // count trailing zeros
int __ctzti2(ti_int a); // count trailing zeros

si_int __ffssi2(si_int a); // find least significant 1 bit
si_int __ffsdi2(di_int a); // find least significant 1 bit
si_int __ffsti2(ti_int a); // find least significant 1 bit
int __ffssi2(si_int a); // find least significant 1 bit
int __ffsdi2(di_int a); // find least significant 1 bit
int __ffsti2(ti_int a); // find least significant 1 bit

si_int __paritysi2(si_int a); // bit parity
si_int __paritydi2(di_int a); // bit parity
si_int __parityti2(ti_int a); // bit parity
int __paritysi2(si_int a); // bit parity
int __paritydi2(di_int a); // bit parity
int __parityti2(ti_int a); // bit parity

si_int __popcountsi2(si_int a); // bit population
si_int __popcountdi2(di_int a); // bit population
si_int __popcountti2(ti_int a); // bit population
int __popcountsi2(si_int a); // bit population
int __popcountdi2(di_int a); // bit population
int __popcountti2(ti_int a); // bit population

uint32_t __bswapsi2(uint32_t a); // a byteswapped
uint64_t __bswapdi2(uint64_t a); // a byteswapped
Expand Down Expand Up @@ -169,10 +174,10 @@ long double __floatuntixf(tu_int a);

// Floating point raised to integer power

float __powisf2( float a, si_int b); // a ^ b
double __powidf2( double a, si_int b); // a ^ b
long double __powixf2(long double a, si_int b); // a ^ b
long double __powitf2(long double a, si_int b); // ppc only, a ^ b
float __powisf2( float a, int b); // a ^ b
double __powidf2( double a, int b); // a ^ b
long double __powixf2(long double a, int b); // a ^ b
long double __powitf2(long double a, int b); // ppc only, a ^ b

// Complex arithmetic

Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/builtins/clzdi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
// ctz instruction, gcc resolves __builtin_clz to __clzdi2 rather than
// __clzsi2, leading to infinite recursion.
#define __builtin_clz(a) __clzsi2(a)
extern si_int __clzsi2(si_int);
extern int __clzsi2(si_int);
#endif

// Precondition: a != 0

COMPILER_RT_ABI si_int __clzdi2(di_int a) {
COMPILER_RT_ABI int __clzdi2(di_int a) {
dwords x;
x.all = a;
const si_int f = -(x.s.high == 0);
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/clzsi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

// Precondition: a != 0

COMPILER_RT_ABI si_int __clzsi2(si_int a) {
COMPILER_RT_ABI int __clzsi2(si_int a) {
su_int x = (su_int)a;
si_int t = ((x & 0xFFFF0000) == 0) << 4; // if (x is small) t = 16 else 0
x >>= 16 - t; // x = [0 - 0xFFFF]
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/clzti2.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

// Precondition: a != 0

COMPILER_RT_ABI si_int __clzti2(ti_int a) {
COMPILER_RT_ABI int __clzti2(ti_int a) {
twords x;
x.all = a;
const di_int f = -(x.s.high == 0);
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/ctzdi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// ctz instruction, gcc resolves __builtin_ctz to __ctzdi2 rather than
// __ctzsi2, leading to infinite recursion.
#define __builtin_ctz(a) __ctzsi2(a)
extern si_int __ctzsi2(si_int);
extern int __ctzsi2(si_int);
#endif

// Precondition: a != 0
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/ctzsi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

// Precondition: a != 0

COMPILER_RT_ABI si_int __ctzsi2(si_int a) {
COMPILER_RT_ABI int __ctzsi2(si_int a) {
su_int x = (su_int)a;
si_int t = ((x & 0x0000FFFF) == 0)
<< 4; // if (x has no small bits) t = 16 else 0
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/ctzti2.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

// Precondition: a != 0

COMPILER_RT_ABI si_int __ctzti2(ti_int a) {
COMPILER_RT_ABI int __ctzti2(ti_int a) {
twords x;
x.all = a;
const di_int f = -(x.s.low == 0);
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/ffsti2.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// 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 __ffsti2(ti_int a) {
COMPILER_RT_ABI int __ffsti2(ti_int a) {
twords x;
x.all = a;
if (x.s.low == 0) {
Expand Down
14 changes: 7 additions & 7 deletions compiler-rt/lib/builtins/int_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
// Include internal utility function declarations.
#include "int_util.h"

COMPILER_RT_ABI si_int __paritysi2(si_int a);
COMPILER_RT_ABI si_int __paritydi2(di_int a);
COMPILER_RT_ABI int __paritysi2(si_int a);
COMPILER_RT_ABI int __paritydi2(di_int a);

COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b);
COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b);
Expand All @@ -102,37 +102,37 @@ COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d);
COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int *rem);
COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem);
#ifdef CRT_HAS_128BIT
COMPILER_RT_ABI si_int __clzti2(ti_int a);
COMPILER_RT_ABI int __clzti2(ti_int a);
COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int *rem);
#endif

// Definitions for builtins unavailable on MSVC
#if defined(_MSC_VER) && !defined(__clang__)
#include <intrin.h>

uint32_t __inline __builtin_ctz(uint32_t value) {
int __inline __builtin_ctz(uint32_t value) {
unsigned long trailing_zero = 0;
if (_BitScanForward(&trailing_zero, value))
return trailing_zero;
return 32;
}

uint32_t __inline __builtin_clz(uint32_t value) {
int __inline __builtin_clz(uint32_t value) {
unsigned long leading_zero = 0;
if (_BitScanReverse(&leading_zero, value))
return 31 - leading_zero;
return 32;
}

#if defined(_M_ARM) || defined(_M_X64)
uint32_t __inline __builtin_clzll(uint64_t value) {
int __inline __builtin_clzll(uint64_t value) {
unsigned long leading_zero = 0;
if (_BitScanReverse64(&leading_zero, value))
return 63 - leading_zero;
return 64;
}
#else
uint32_t __inline __builtin_clzll(uint64_t value) {
int __inline __builtin_clzll(uint64_t value) {
if (value == 0)
return 64;
uint32_t msh = (uint32_t)(value >> 32);
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/paritydi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Returns: 1 if number of bits is odd else returns 0

COMPILER_RT_ABI si_int __paritydi2(di_int a) {
COMPILER_RT_ABI int __paritydi2(di_int a) {
dwords x;
x.all = a;
return __paritysi2(x.s.high ^ x.s.low);
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/paritysi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Returns: 1 if number of bits is odd else returns 0

COMPILER_RT_ABI si_int __paritysi2(si_int a) {
COMPILER_RT_ABI int __paritysi2(si_int a) {
su_int x = (su_int)a;
x ^= x >> 16;
x ^= x >> 8;
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/parityti2.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

// Returns: 1 if number of bits is odd else returns 0

COMPILER_RT_ABI si_int __parityti2(ti_int a) {
COMPILER_RT_ABI int __parityti2(ti_int a) {
twords x;
x.all = a;
return __paritydi2(x.s.high ^ x.s.low);
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/popcountsi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Returns: count of 1 bits

COMPILER_RT_ABI si_int __popcountsi2(si_int a) {
COMPILER_RT_ABI int __popcountsi2(si_int a) {
su_int x = (su_int)a;
x = x - ((x >> 1) & 0x55555555);
// Every 2 bits holds the sum of every pair of bits
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/popcountti2.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// Returns: count of 1 bits

COMPILER_RT_ABI si_int __popcountti2(ti_int a) {
COMPILER_RT_ABI int __popcountti2(ti_int a) {
tu_int x3 = (tu_int)a;
x3 = x3 - ((x3 >> 1) &
(((tu_int)0x5555555555555555uLL << 64) | 0x5555555555555555uLL));
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/powidf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Returns: a ^ b

COMPILER_RT_ABI double __powidf2(double a, si_int b) {
COMPILER_RT_ABI double __powidf2(double a, int b) {
const int recip = b < 0;
double r = 1;
while (1) {
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/powisf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Returns: a ^ b

COMPILER_RT_ABI float __powisf2(float a, si_int b) {
COMPILER_RT_ABI float __powisf2(float a, int b) {
const int recip = b < 0;
float r = 1;
while (1) {
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/powitf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// Returns: a ^ b

COMPILER_RT_ABI long double __powitf2(long double a, si_int b) {
COMPILER_RT_ABI long double __powitf2(long double a, int b) {
const int recip = b < 0;
long double r = 1;
while (1) {
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/builtins/powixf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

// Returns: a ^ b

COMPILER_RT_ABI long double __powixf2(long double a, si_int b) {
COMPILER_RT_ABI long double __powixf2(long double a, int b) {
const int recip = b < 0;
long double r = 1;
while (1) {
Expand Down
6 changes: 3 additions & 3 deletions compiler-rt/test/builtins/Unit/clzdi2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

// Precondition: a != 0

COMPILER_RT_ABI si_int __clzdi2(di_int a);
COMPILER_RT_ABI int __clzdi2(di_int a);

int test__clzdi2(di_int a, si_int expected)
int test__clzdi2(di_int a, int expected)
{
si_int x = __clzdi2(a);
int x = __clzdi2(a);
if (x != expected)
printf("error in __clzdi2(0x%llX) = %d, expected %d\n", a, x, expected);
return x != expected;
Expand Down
6 changes: 3 additions & 3 deletions compiler-rt/test/builtins/Unit/clzsi2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

// Precondition: a != 0

COMPILER_RT_ABI si_int __clzsi2(si_int a);
COMPILER_RT_ABI int __clzsi2(si_int a);

int test__clzsi2(si_int a, si_int expected)
int test__clzsi2(si_int a, int expected)
{
si_int x = __clzsi2(a);
int x = __clzsi2(a);
if (x != expected)
printf("error in __clzsi2(0x%X) = %d, expected %d\n", a, x, expected);
return x != expected;
Expand Down
6 changes: 3 additions & 3 deletions compiler-rt/test/builtins/Unit/clzti2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

// Precondition: a != 0

COMPILER_RT_ABI si_int __clzti2(ti_int a);
COMPILER_RT_ABI int __clzti2(ti_int a);

int test__clzti2(ti_int a, si_int expected)
int test__clzti2(ti_int a, int expected)
{
si_int x = __clzti2(a);
int x = __clzti2(a);
if (x != expected)
{
twords at;
Expand Down
6 changes: 3 additions & 3 deletions compiler-rt/test/builtins/Unit/ctzsi2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

// Precondition: a != 0

COMPILER_RT_ABI si_int __ctzsi2(si_int a);
COMPILER_RT_ABI int __ctzsi2(si_int a);

int test__ctzsi2(si_int a, si_int expected)
int test__ctzsi2(si_int a, int expected)
{
si_int x = __ctzsi2(a);
int x = __ctzsi2(a);
if (x != expected)
printf("error in __ctzsi2(0x%X) = %d, expected %d\n", a, x, expected);
return x != expected;
Expand Down
6 changes: 3 additions & 3 deletions compiler-rt/test/builtins/Unit/ctzti2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

// Precondition: a != 0

COMPILER_RT_ABI si_int __ctzti2(ti_int a);
COMPILER_RT_ABI int __ctzti2(ti_int a);

int test__ctzti2(ti_int a, si_int expected)
int test__ctzti2(ti_int a, int expected)
{
si_int x = __ctzti2(a);
int x = __ctzti2(a);
if (x != expected)
{
twords at;
Expand Down
6 changes: 3 additions & 3 deletions compiler-rt/test/builtins/Unit/ffsti2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,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 __ffsti2(ti_int a);
COMPILER_RT_ABI int __ffsti2(ti_int a);

int test__ffsti2(ti_int a, si_int expected)
int test__ffsti2(ti_int a, int expected)
{
si_int x = __ffsti2(a);
int x = __ffsti2(a);
if (x != expected)
{
twords at;
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/test/builtins/Unit/paritydi2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Returns: 1 if number of bits is odd else returns 0

COMPILER_RT_ABI si_int __paritydi2(di_int a);
COMPILER_RT_ABI int __paritydi2(di_int a);

int naive_parity(di_int a)
{
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/test/builtins/Unit/paritysi2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Returns: 1 if number of bits is odd else returns 0

COMPILER_RT_ABI si_int __paritysi2(si_int a);
COMPILER_RT_ABI int __paritysi2(si_int a);

int naive_parity(si_int a)
{
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/test/builtins/Unit/parityti2_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// Returns: 1 if number of bits is odd else returns 0

COMPILER_RT_ABI si_int __parityti2(ti_int a);
COMPILER_RT_ABI int __parityti2(ti_int a);

int naive_parity(ti_int a)
{
Expand Down
Loading

0 comments on commit 0ee439b

Please sign in to comment.