[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Problem with inline assembly and -O2 or -O3
From: |
Jeremy Friesner |
Subject: |
Problem with inline assembly and -O2 or -O3 |
Date: |
Sat, 07 Aug 2004 12:02:01 PDT (-0700) |
Hi all,
I have a problem with some simple inline assembly -- I wrote a function to byte
swap a 64-bit integer under x86, but it only works when compiled with -O0 or
-O1. When compiled with -O2 or -O3, it gives the wrong result. Is this a
compiler bug or have I done something wrong?
I'm using gcc/g++ 3.3.3 on a dual P3 under SUSE 9.1, FWIW.
Here is the correct output I get when compiling with -O0 or -O1:
$ ./swap
Test completed successfully.
and here is the (unexpected) output I get when compiling with -O2 or -O3:
$ ./swap
Error! backagain=0 orig=1
And finally, here is the test program that demonstrates the problem... can
anyone give me a suggestion?
--------------------------------------------------------------
#include <stdio.h>
typedef unsigned long long uint64;
typedef unsigned long uint32;
inline uint64 Swap64(uint64 val)
{
uint64 a;
volatile uint32 * inLo = (uint32 *) &val;
volatile uint32 * outLo = (uint32 *) &a;
volatile uint32 * inHi = (inLo+1);
volatile uint32 * outHi = (outLo+1);
__asm__ __volatile__ ("bswap %0" : "=r" (*inLo) : "0" (*inLo));
__asm__ __volatile__ ("bswap %0" : "=r" (*inHi) : "0" (*inHi));
*outHi = *inLo;
*outLo = *inHi;
return a;
}
int main(int, char **)
{
for (uint64 x=0; x<1000; x++)
{
uint64 swapped = Swap64(x);
uint64 backagain = Swap64(swapped);
if (backagain != x)
{
printf("Error! backagain=%llu orig=%llu\n", backagain, x);
return 10;
}
}
printf("Test completed successfully.\n");
return 0;
}
- Problem with inline assembly and -O2 or -O3,
Jeremy Friesner <=