public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Testcase for strict-aliasing wanted
@ 2002-01-18 12:42 Osku Salerma
  2002-01-18 15:15 ` Geoff Keating
  0 siblings, 1 reply; 4+ messages in thread
From: Osku Salerma @ 2002-01-18 12:42 UTC (permalink / raw)
  To: gcc

Does anybody have a simple testcase available that breaks with strict
aliasing enabled? I tried to construct my own, but I can't get either
2.95.2 or the latest CVS version to break intentionally. Either I'm
misunderstanding some of the aliasing rules or gcc isn't optimizing as
aggressively in this case as would be needed for the aliasing issues
to surface.

For reference, here's my (not-working) effort:

#include <stdio.h>

void f(char* p1, int* p2)
{
    char c1,c2;

    c1 = *p1;
    *p2 = 0;
    c2 = *p1;

    printf("(%d,%d)\n", c1,c2);
}

int main(void)
{
    char a[4];

    a[0] = 1;

    f(a, (int*)a);

    return 0;
}

--
Osku Salerma - osku@iki.fi - http://www.iki.fi/osku/
Information wants to be free.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Testcase for strict-aliasing wanted
  2002-01-18 12:42 Testcase for strict-aliasing wanted Osku Salerma
@ 2002-01-18 15:15 ` Geoff Keating
  2002-01-19 13:17   ` Osku Salerma
  0 siblings, 1 reply; 4+ messages in thread
From: Geoff Keating @ 2002-01-18 15:15 UTC (permalink / raw)
  To: Osku Salerma; +Cc: gcc


Osku Salerma <osku@iki.fi> writes:

> Does anybody have a simple testcase available that breaks with strict
> aliasing enabled? I tried to construct my own, but I can't get either
> 2.95.2 or the latest CVS version to break intentionally. Either I'm
> misunderstanding some of the aliasing rules or gcc isn't optimizing as
> aggressively in this case as would be needed for the aliasing issues
> to surface.
> 
> For reference, here's my (not-working) effort:
> 
> #include <stdio.h>
> 
> void f(char* p1, int* p2)
> {
>     char c1,c2;
> 
>     c1 = *p1;
>     *p2 = 0;
>     c2 = *p1;
> 
>     printf("(%d,%d)\n", c1,c2);
> }
> 
> int main(void)
> {
>     char a[4];
> 
>     a[0] = 1;
> 
>     f(a, (int*)a);
> 
>     return 0;
> }

`char' is a special case; ISO C requires that it be allowed to alias
with anything.  Try the same testcase with `short' instead of `char'.

-- 
- Geoffrey Keating <geoffk@geoffk.org> <geoffk@redhat.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Testcase for strict-aliasing wanted
  2002-01-18 15:15 ` Geoff Keating
@ 2002-01-19 13:17   ` Osku Salerma
  0 siblings, 0 replies; 4+ messages in thread
From: Osku Salerma @ 2002-01-19 13:17 UTC (permalink / raw)
  To: Geoff Keating; +Cc: gcc

On 18 Jan 2002, Geoff Keating wrote:
> Osku Salerma <osku@iki.fi> writes:
> > [invalid test case]
> 
> `char' is a special case; ISO C requires that it be allowed to alias
> with anything.  Try the same testcase with `short' instead of `char'.

Yep, it works as expected with 'char' changed to 'short'. I did know
about the fact that char can alias anything, but for some reason I
kept thinking that since I was accessing "char data" through an int
pointer, the compiler would be free to assume such things wouldn't
happen. Totally wrong, of course, for several reasons.

Thanks for the help.

--
Osku Salerma - osku@iki.fi - http://www.iki.fi/osku/
Information wants to be free.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Testcase for strict-aliasing wanted
@ 2002-01-18 13:23 mike stump
  0 siblings, 0 replies; 4+ messages in thread
From: mike stump @ 2002-01-18 13:23 UTC (permalink / raw)
  To: gcc, osku

> Date: Fri, 18 Jan 2002 21:39:38 +0200 (EET)
> From: Osku Salerma <osku@iki.fi>
> To: gcc@gcc.gnu.org

> Does anybody have a simple testcase available that breaks with strict
> aliasing enabled?

Ok, here is a quick quiz to determine if you know C.  Does the
following program always print:

a)	  12345678	 or	12345678	(depending on endian)
	  12340000		5678

or

b)	12345678
	12345678

or c) neither.

?  Answer at the end.

#include <stdio.h>
 
int
main ()
{
    int a = 0x12345678;
    unsigned short *b = (unsigned short *)&a;
 
    printf ("%x\n", a);
    b[1] = 0;
    printf ("%x\n", a);
 
    return 0;
}

If you said a, you'd be wrong, if you said b, you'd be wrong.  This is
non-portable in ANSI/ISO C.  With the older compilers, those without
type based aliasing, one would always get a, with newer compilers with
more powerful optimizers, it is more common to get b if one optimizes.

[ ... ]


Isn't this in the C programming FAQ yet?  If not, please go over there
and get it added.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-01-19 12:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-18 12:42 Testcase for strict-aliasing wanted Osku Salerma
2002-01-18 15:15 ` Geoff Keating
2002-01-19 13:17   ` Osku Salerma
2002-01-18 13:23 mike stump

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).