tinycc/tests/tests2/93_integer_promotion.c
grischka 1ed20a01c9 bitfields: promote to signed int
For integer promotion with for example arithmetics or
expr_cond (x ? y : z), integral types need to be promoted
to signed if they fit.

According to latest standards, this also applies to bit-field
types taking into account their specific width.

In tcc, VT_BITFIELD set means width < original type width
Field-widths between 33 and 63 are promoted to signed long long
accordingly.

    struct { unsigned long long ullb:35; } s = { 1 };
    #define X (s.ullb - 2)

    int main (void)
    {
        long long Y = X;
        printf("%d %016llx %016llx\n", X < 0, -X, -Y);
        return 0;
    }

Results:
    GCC 4.7 : 0 0000000000000001 FFFFFFF800000001
    MSVC    : 1 0000000000000001 0000000000000001
    TCC     : 1 0000000000000001 0000000000000001

Also, gcc would promote long long bitfields of size < 32
to int as well.  Example:

    struct { unsigned long long x:20; } t = { 123 };
    /* with gcc: */ printf("%d %d\n", t.x, 456);
    /* with tcc: */ printf("%lld %d\n", t.x, 456);
2017-05-09 18:36:24 +02:00

72 lines
1.3 KiB
C

/* integer promotion */
int printf(const char*, ...);
#define promote(s) printf(" %ssigned : %s\n", (s) - 100 < 0 ? " " : "un", #s);
int main (void)
{
struct {
unsigned ub:3;
unsigned u:32;
unsigned long long ullb:35;
unsigned long long ull:64;
unsigned char c;
} s = { 1, 1, 1 };
promote(s.ub);
promote(s.u);
promote(s.ullb);
promote(s.ull);
promote(s.c);
printf("\n");
promote((1 ? s.ub : 1));
promote((1 ? s.u : 1));
promote((1 ? s.ullb : 1));
promote((1 ? s.ull : 1));
promote((1 ? s.c : 1));
printf("\n");
promote(s.ub << 1);
promote(s.u << 1);
promote(s.ullb << 1);
promote(s.ull << 1);
promote(s.c << 1);
printf("\n");
promote(+s.ub);
promote(+s.u);
promote(+s.ullb);
promote(+s.ull);
promote(+s.c);
printf("\n");
promote(-s.ub);
promote(-s.u);
promote(-s.ullb);
promote(-s.ull);
promote(-s.c);
printf("\n");
promote(~s.ub);
promote(~s.u);
promote(~s.ullb);
promote(~s.ull);
promote(~s.c);
printf("\n");
promote(!s.ub);
promote(!s.u);
promote(!s.ullb);
promote(!s.ull);
promote(!s.c);
printf("\n");
promote(+(unsigned)s.ub);
promote(-(unsigned)s.ub);
promote(~(unsigned)s.ub);
promote(!(unsigned)s.ub);
return 0;
}