This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Masked bitfield comparisons may yield incorrect results
- To: egcs-bugs at egcs dot cygnus dot com
- Subject: Re: Masked bitfield comparisons may yield incorrect results
- From: "Charles M. Hannum" <root at ihack dot net>
- Date: Thu, 4 Mar 1999 01:09:52 -0500 (EST)
- Cc: felix at dworkin dot nl, lehotsky at mc dot com
BTW, if someone were to add the test program to the test suite, I
would suggest being slightly more thorough, as in:
-----8<-----snip-----8<-----snip-----8<-----snip-----8<-----snip-----8<-----
#include <stdio.h>
int
foo_1()
{
struct foo {
char a, b;
short c;
};
static struct foo x = { 1, 2, ~1 }, y = { 65, 2, ~2 };
return (x.a == (y.a & ~64) && x.b == y.b);
}
int
bar_1()
{
struct baz {
int c;
short b, a;
};
static struct baz x = { ~1, 2, 1 }, y = { ~2, 2, 65 };
return (x.a == (y.a & ~64) && x.b == y.b);
}
int
baz_1()
{
struct baz {
unsigned int c:4, b:14, a:14;
};
static struct baz x = { ~1, 2, 1 }, y = { ~2, 2, 65 };
return (x.a == (y.a & ~64) && x.b == y.b);
}
int
qux_1()
{
struct qux {
unsigned int a:14, b:14, c:4;
};
static struct qux x = { 1, 2, ~1 }, y = { 65, 2, ~2 };
return (x.a == (y.a & ~64) && x.b == y.b);
}
int
foo_2()
{
struct foo {
char a, b;
short c;
};
static struct foo x = { 1, 66, ~1 }, y = { 1, 2, ~2 };
return (x.a == y.a && (x.b & ~64) == y.b);
}
int
bar_2()
{
struct baz {
int c;
short b, a;
};
static struct baz x = { ~1, 66, 1 }, y = { ~2, 2, 1 };
return (x.a == y.a && (x.b & ~64) == y.b);
}
int
baz_2()
{
struct baz {
unsigned int c:4, b:14, a:14;
};
static struct baz x = { ~1, 66, 1 }, y = { ~2, 2, 1 };
return (x.a == y.a && (x.b & ~64) == y.b);
}
int
qux_2()
{
struct qux {
unsigned int a:14, b:14, c:4;
};
static struct qux x = { 1, 66, ~1 }, y = { 1, 2, ~2 };
return (x.a == y.a && (x.b & ~64) == y.b);
}
int
foo_3()
{
struct foo {
char a, b;
short c;
};
static struct foo x = { 9, 66, ~1 }, y = { 33, 18, ~2 };
return ((x.a & ~8) == (y.a & ~32) && (x.b & ~64) == (y.b & ~16));
}
int
bar_3()
{
struct baz {
int c;
short b, a;
};
static struct baz x = { ~1, 66, 9 }, y = { ~2, 18, 33 };
return ((x.a & ~8) == (y.a & ~32) && (x.b & ~64) == (y.b & ~16));
}
int
baz_3()
{
struct baz {
unsigned int c:4, b:14, a:14;
};
static struct baz x = { ~1, 66, 9 }, y = { ~2, 18, 33 };
return ((x.a & ~8) == (y.a & ~32) && (x.b & ~64) == (y.b & ~16));
}
int
qux_3()
{
struct qux {
unsigned int a:14, b:14, c:4;
};
static struct qux x = { 9, 66, ~1 }, y = { 33, 18, ~2 };
return ((x.a & ~8) == (y.a & ~32) && (x.b & ~64) == (y.b & ~16));
}
int
main()
{
printf("foo_1: %d\n", foo_1());
printf("bar_1: %d\n", bar_1());
printf("baz_1: %d\n", baz_1());
printf("qux_1: %d\n", qux_1());
printf("foo_2: %d\n", foo_2());
printf("bar_2: %d\n", bar_2());
printf("baz_2: %d\n", baz_2());
printf("qux_2: %d\n", qux_2());
printf("foo_3: %d\n", foo_3());
printf("bar_3: %d\n", bar_3());
printf("baz_3: %d\n", baz_3());
printf("qux_3: %d\n", qux_3());
}
-----8<-----snip-----8<-----snip-----8<-----snip-----8<-----snip-----8<-----