public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Michele Dall'Arno <michele.dallarno@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Inline assembly and extended instruction sets
Date: Wed, 06 Jul 2005 23:53:00 -0000	[thread overview]
Message-ID: <42CC6F51.4070007@gmail.com> (raw)

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.

             reply	other threads:[~2005-07-06 23:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-06 23:53 Michele Dall'Arno [this message]
2005-07-07  9:17 ` Nathan Sidwell
  -- strict thread matches above, loose matches on Subject: below --
2005-07-06 23:51 Michele Dall'Arno

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42CC6F51.4070007@gmail.com \
    --to=michele.dallarno@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).