* Inline assembly and extended instruction sets
@ 2005-07-06 23:53 Michele Dall'Arno
2005-07-07 9:17 ` Nathan Sidwell
0 siblings, 1 reply; 3+ messages in thread
From: Michele Dall'Arno @ 2005-07-06 23:53 UTC (permalink / raw)
To: gcc-help
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Inline assembly and extended instruction sets
2005-07-06 23:53 Inline assembly and extended instruction sets Michele Dall'Arno
@ 2005-07-07 9:17 ` Nathan Sidwell
0 siblings, 0 replies; 3+ messages in thread
From: Nathan Sidwell @ 2005-07-07 9:17 UTC (permalink / raw)
To: Michele Dall'Arno; +Cc: gcc-help
Michele Dall'Arno wrote:
> 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!
use the mmx builtins, then you won't have to write asms at all.
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
nathan@codesourcery.com :: http://www.planetfall.pwp.blueyonder.co.uk
^ permalink raw reply [flat|nested] 3+ messages in thread
* Inline assembly and extended instruction sets
@ 2005-07-06 23:51 Michele Dall'Arno
0 siblings, 0 replies; 3+ messages in thread
From: Michele Dall'Arno @ 2005-07-06 23:51 UTC (permalink / raw)
To: gcc-help
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-07-07 9:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-06 23:53 Inline assembly and extended instruction sets Michele Dall'Arno
2005-07-07 9:17 ` Nathan Sidwell
-- strict thread matches above, loose matches on Subject: below --
2005-07-06 23:51 Michele Dall'Arno
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).