This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: A bug in IA32 assembler
On Fri, Jun 29, 2001 at 12:15:56AM -0700, H . J . Lu wrote:
> This one:
>
> asm("movaps (%0),%%xmm0" //SSE
> :
> :"g"(A));
This one is wrong because A is an array, which decomposes to a pointer,
which has the constant value A.2. And you've told the compiler that
immediates are acceptable. So it prints A.2 as an immediate.
It's also wrong in that you're _not_ telling the compiler that you are
reading from memory. So it'll be happy to move stores across this asm,
or even remove them as dead.
> asm("movaps %%xmm0,(%0)":"=g"(C)); //SSE
Likewise, you're not telling the compiler that you are storing to memory.
The easiest way to write things correctly is like so.
r~
#include <stdio.h>
typedef struct
{
float x[4] __attribute__((aligned(16)));
} V4SF;
static void dump (const char *name, const V4SF *v)
{
int i;
printf("float %s=(", name);
for(i = 0; i < 4; i++)
printf("%f,", v->x[i]);
printf("\b)\n");
}
int main()
{
static V4SF A = {1.,1.,1.,1.};
static V4SF B = {0.,1.,2.,3.};
static V4SF C;
dump("A", &A);
dump("B", &B);
asm("movaps %0,%%xmm0" : :"m"(A));
asm("movaps %0,%%xmm1" : :"m"(B));
asm("addps %xmm1,%xmm0");
// asm("mulps %xmm1,%xmm0");
asm("movaps %%xmm0,%0" : "=m"(C));
dump("C", &C);
return 0;
}