Hi everybody!
I'm writing a code to benchmark some processor-specific instruction
sets, as MMX, SSE, SSE2 and 3dNow!. To do so, I use an inline assembly
program that performs some simple arithmetic instructions.
My problem is that those instruction sets works with vectors of objects
(for example, SSE uses 128bit register, each of witch made of 4 32bit
floating point numbers). So I'm not sure I'm interfacing C and asm in
the correct way, i.e. I'm not sure I'm passing and receiving data to and
from the asm program the best way.
This is what I've done, after having read a lot of gcc documentation and
googled as much.
I'll have just to change this typedef if the float type on the
implementation has more or less than 32bit.
typedef float float32;
I'm not sure about what the next typedef exactly does, expecially about
the parameter aligned(16). I just found it in the documentation. Any
explanation would be appreciated!
typedef float32 v4sf __attribute__ ((mode(V4SF),aligned(16)));
This is the best way I've found to convert 128bit values from
asm-readable to C-readable and vice-versa. Is that the best way? I'm not
sure...
union xmm
{
v4sf vector;
float32 array[4];
};
And this is the main program.
int main(int argc, char **argv)
{
union xmm *xmm0, *xmm1;
The xmm_init() function declares, defines and initializes a xmm union,
assigning to the 4 array elements 4 random floating point values. It
returns a pointer to the new union. In fact, it seems to work ok!
xmm0 = xmm_init();
xmm1 = xmm_init();
The function xmm_to_string returns the four elements of the array it
takes as parameter separated by commas as a C-string, nothing more.
Seems to work too.
printf("Initial vaues:\n");
printf("\txmm0 = %s\n", xmm_to_string(xmm0->array));
printf("\txmm1 = %s\n", xmm_to_string(xmm1->array));
This is the asm program. It performs some unuseful calculus. As it is,
it doesn't works. I get "error: inconsistent operand constraints in an
`asm'" from gcc. If I do some modificatons to the program, for example
if I don't use pointers to the unions but unions (i.e. "union xmm xmm0,
xmm1;" instead of "union xmm *xmm0, *xmm1;" and "xmm0.vector" instead of
"xmm0->vector"), everything SEEMS to work correctly. But the fact is
that I'ld like to use pointers to the union (I'd like to be able to pass
pointers to the unions in function calls) and I'd love to understand
everything deeply.
asm(
"movups %0, %%xmm0\n\t"
"movups %1, %%xmm1\n\t"
"movl $10, %%ecx\n\t"
"begin:"
"addps %%xmm1, %%xmm0\n\t"
"mulps %%xmm1, %%xmm0\n\t"
"subps %%xmm1, %%xmm0\n\t"
"divps %%xmm1, %%xmm0\n\t"
"loop begin\n\t"
"movups %%xmm0, %0\n\t":
"=m" (xmm0->vector):
"0" (xmm0->vector), "m" (xmm1->vector)
);
printf("Final vaues:\n");
printf("\txmm0 = %s\n", xmm_to_string(xmm0->array));
printf("\txmm1 = %s\n", xmm_to_string(xmm1->array));
}
Is the union-way the best way to make C and asm able to exchange such
vectors? Why the program doesn't compile with union pointers? Any kind
of corrections and explanations would be greatly appreciated!
Michele Dall'Arno.