From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2417 invoked by alias); 6 Jul 2005 23:53:30 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 2394 invoked by uid 22791); 6 Jul 2005 23:53:26 -0000 Received: from wproxy.gmail.com (HELO wproxy.gmail.com) (64.233.184.207) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Wed, 06 Jul 2005 23:53:26 +0000 Received: by wproxy.gmail.com with SMTP id 68so82142wra for ; Wed, 06 Jul 2005 16:53:25 -0700 (PDT) Received: by 10.54.57.78 with SMTP id f78mr246953wra; Wed, 06 Jul 2005 16:53:24 -0700 (PDT) Received: from ?192.168.1.2? ([80.116.157.254]) by mx.gmail.com with ESMTP id g9sm522816wra.2005.07.06.16.53.23; Wed, 06 Jul 2005 16:53:24 -0700 (PDT) Message-ID: <42CC6F51.4070007@gmail.com> Date: Wed, 06 Jul 2005 23:53:00 -0000 From: Michele Dall'Arno User-Agent: Mozilla Thunderbird 1.0 (X11/20041207) MIME-Version: 1.0 To: gcc-help@gcc.gnu.org Subject: Inline assembly and extended instruction sets Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-SW-Source: 2005-07/txt/msg00057.txt.bz2 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.