public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: optimization/10258: byteswapping code fails in -O2
@ 2003-03-28 22:16 bangerth
  0 siblings, 0 replies; 5+ messages in thread
From: bangerth @ 2003-03-28 22:16 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, nobody, warren_baird

Synopsis: byteswapping code fails in -O2

State-Changed-From-To: open->feedback
State-Changed-By: bangerth
State-Changed-When: Fri Mar 28 22:14:41 2003
State-Changed-Why:
    Does the result change if you add to -O2 the flag
    -fno-strict-aliasing? You might want to review the section
    on this flag in the manual? -fstrict-aliasing is switched on
    by -O2.
    
    W.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=10258


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

* Re: optimization/10258: byteswapping code fails in -O2
@ 2003-03-31 17:20 bangerth
  0 siblings, 0 replies; 5+ messages in thread
From: bangerth @ 2003-03-31 17:20 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, nobody, warren_baird

Synopsis: byteswapping code fails in -O2

State-Changed-From-To: feedback->closed
State-Changed-By: bangerth
State-Changed-When: Mon Mar 31 16:26:35 2003
State-Changed-Why:
    On request of submitter

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=10258


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

* Re: optimization/10258: byteswapping code fails in -O2
@ 2003-03-31 16:26 Warren_Baird
  0 siblings, 0 replies; 5+ messages in thread
From: Warren_Baird @ 2003-03-31 16:26 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR optimization/10258; it has been noted by GNATS.

From: Warren_Baird@cimmetry.com
To: bangerth@dealii.org, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org,
	nobody@gcc.gnu.org, gcc-gnats@gcc.gnu.org
Cc:  
Subject: Re: optimization/10258: byteswapping code fails in -O2
Date: Mon, 31 Mar 2003 11:19:48 -0500

 Yeah, it looks like the -fno-strict-aliasing solves the problem.  Sorry about
 that...
 
 Warren
 
 


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

* Re: optimization/10258: byteswapping code fails in -O2
@ 2003-03-28 22:26 Falk Hueffner
  0 siblings, 0 replies; 5+ messages in thread
From: Falk Hueffner @ 2003-03-28 22:26 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR optimization/10258; it has been noted by GNATS.

From: Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
To: warren_baird@cimmetry.com
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: optimization/10258: byteswapping code fails in -O2
Date: 28 Mar 2003 23:15:44 +0100

 warren_baird@cimmetry.com writes:
 
 > FLOAT swapFLOAT(FLOAT f)
 > {
 > 	FLOAT ret;
 > 	DWORD* pDW = (DWORD*)&ret;
 > 	*pDW = BYTESWAP_DWORD(*(DWORD*)&f);
 
 You're violating aliasing constraints here
 
 > 	printf("0x%08X 0x%08X\n", *(long *)&f1, *(long *)&sf1);
 
 and here. So this is not a gcc bug. Please read the documentation of
 -fstrict-aliasing.
 
 -- 
 	Falk


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

* optimization/10258: byteswapping code fails in -O2
@ 2003-03-28 22:14 warren_baird
  0 siblings, 0 replies; 5+ messages in thread
From: warren_baird @ 2003-03-28 22:14 UTC (permalink / raw)
  To: gcc-gnats


>Number:         10258
>Category:       optimization
>Synopsis:       byteswapping code fails in -O2
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Fri Mar 28 22:06:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Warren Baird
>Release:        gcc-3.2.2
>Organization:
>Environment:
Solaris 8 - Sparc
>Description:
We have macros to do byteswapping of integers, and we wrote a short function to use integer macros to swap a float.  It works fine in -O0 or -O, but in -O2 it returns incorrect values.  The byte swapping macros are a bit complex, so I've attached the preprocessor output as required, but I'll include the non-preprocessed output here - it may be easier to follow...

#include <stdio.h>
#include <string.h>

typedef unsigned short  WORD,       *PWORD,    *LPWORD;
typedef unsigned long   DWORD,      *PDWORD,   *LPDWORD;
typedef float           FLOAT,      *PFLOAT;

#define BYTESWAP_LO8(s)   ((WORD)(s) & 0xff)
#define BYTESWAP_HI8(s)   (((WORD)(s) >> 8) & 0xff)
#define BYTESWAP_WORD(s)  ((WORD)((BYTESWAP_LO8(s) << 8) + (BYTESWAP_HI8(s))))

#define BYTESWAP_LO16(l)  ((DWORD)(l) & 0xffff)
#define BYTESWAP_HI16(l)  (((DWORD)(l) >> 16) & 0xffff)
#define BYTESWAP_DWORD(l) ((DWORD)(((BYTESWAP_WORD(BYTESWAP_LO16(l)) << 16) & 0xffff0000) + \
                                  (((BYTESWAP_WORD(BYTESWAP_HI16(l))) & 0x0000ffff))))

FLOAT swapFLOAT(FLOAT f)
{
	FLOAT ret;
	DWORD* pDW = (DWORD*)&ret;
	*pDW = BYTESWAP_DWORD(*(DWORD*)&f);
	return ret;
}

int main() {

	long foo = 0xfed80000;
	FLOAT f1 = 1.0;
	FLOAT sf1 = swapFLOAT(f1);

	printf("0x%08X 0x%08X\n", *(long *)&f1, *(long *)&sf1);

	return 0;

}
>How-To-Repeat:
"g++ -O testfloat.cpp" gives the expected output:
0x3F800000 0x0000803F

however, "g++ -O2 testfloat.cpp" gives the output:
0x3F800000 0x00000000

In our code, it seems to actually produce either 0, or occasionally a very large negative number.

I wasn't able to repro this on an x86-linux box...
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/gzip; name="testfloat.ii.gz"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="testfloat.ii.gz"

H4sICHbBhD4CA3Rlc3RmbG9hdC5paQC9WmmP2zYQ/Rz/CsJBC9n12pJ8x2nQotsAAVKkaAukQBoI
WpnyCitLAint0SD/vUNSFylKloN1jSQ6+OZxODMcDqm8RBYappimfhi76dRLkuHgJXv3+iYLwvQq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2OSoo8f/rieoPHv+fU9v9k10fy05bpAX5fwaxnP+0Rv33/4+S+G4zdc7XXjv/3wNkQf3ITf
GeLZHw2+lF+1xCuCa9/neH9jlFx/RD8iQzyNvpcg47xRtI4M9ivvipu8bVxS+CP0PTIfffiNitsR
ev0abUboB3SG6Js3TCQngB/jsFYVxIRfQdnUS8sOlHWGE/r1pFD1FPdMOwEYjSqTgn0zCE1u5q+D
AS9u3CAyRiyHFCBx3BrHYHvgxPsN49opzvQtaLamjfeUN1TR4Fuj2nfX/HRiaD5+Z27+RuLyTzSE
GDN4t2yQVv2JygT5AKBb0P8/XFHThr8mAAA=


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

end of thread, other threads:[~2003-03-31 16:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-28 22:16 optimization/10258: byteswapping code fails in -O2 bangerth
  -- strict thread matches above, loose matches on Subject: below --
2003-03-31 17:20 bangerth
2003-03-31 16:26 Warren_Baird
2003-03-28 22:26 Falk Hueffner
2003-03-28 22:14 warren_baird

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).