🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.asm.x86
Article #166

Re: inline ASM: SSE

#166
From: Ben Peddell
Date: Mon, 26 May 2003 10:35
72 lines
4043 bytes
Christoph Brunner wrote:
> Hi,
> 
> I'm currently trying to get into extended inline ASM in gcc (gcc version 
> 3.2.2, gas 2.13.90.0.18).
> However, I'm having some trouble: The following code, using SSE 
> instructions, segfaults, and I don't really know why.
> 
> Perhaps someone could give me a hint?
> 
> void add_asm(float *a, float *b, float *c, int n)
> {
>    // a,b,c properly aligned; (n % 4) = 0
>    int iter = 0;
>     
>    __asm__ __volatile__
>    (
>     "calc:\n"
>     "subl $4, %%ecx\n"
>        
>     // perform package load/addition/store
>     "movaps (%3), %%xmm0\n"
>     "addps (%4), %%xmm0\n"
>     "movaps %%xmm0, (%0)\n"
OK. %0 should be an input operand.
>     // increment number of iterations
>     "addl $1, %1\n"
>     // advance pointers
>     // removing the following lines prevents the segfault
>     "addl $16, %3\n"
>     "addl $16, %4\n"
>     "addl $16, %0\n"
> 
>     "jnz calc\n"
> 
>     : "+r"(c), "+r"(iter), "+c" (n)
>     : "r"(a), "r"(b)
>    );
>    printf("iter:%d\n", iter); // STRANGE: always prints "0"
> }
> 

Perhaps:
     void add_asm (float *a, float *b, float *c, int n){
         int iter = 0;
         __asm__ __volatile__ (
             "1:\n\t"
             "cmpl %0, %1\n\t"
             "jae 2f\n\t"
             "movaps (%3,%0,4), %%xmm0\n\t"
             "addps (%4,%0,4), %%xmm0\n\t"
             "movaps %%xmm0, (%2,%0,4)\n\t"
             "addl $4, %0\n\t"
             "jmp 1b\n\t"
             "2:"
             : "+r" (iter)
             : "rm" (n), "r" (c), "r" (a), "r" (b)
         );
         /* equivalent to:
            for (iter = 0; (unsigned)iter < (unsigned)n; iter += 4){
                __asm__ __volatile__ (
                    "movaps %1, %%xmm0\n\t"
                    "addps %2, %%xmm0\n\t"
                    "movaps %%xmm0, %0"
                    : "o" (c[iter])
                    : "o" (a[iter]), "o" (b[iter])
                );
            }
          */
         iter >>= 2;
         printf ("iter:%d\n", iter);
     }
Technical Details
Message-ID: <AsmAa.43159$1s1.590548@newsfeeds.bigpond.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-post-02!sn-post-01!supernews.com!corp.supernews.com!news.crayne.org!clax86!robomod!not-for-mail
References: <Ko8Aa.20426$Kn1.264902@news.chello.at>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,11e8a07eb4e4bb6f
X-Google-Attributes: gid11318c,public
X-Google-Thread: 114917,11e8a07eb4e4bb6f
X-Google-Attributes: gid114917,public
X-Google-ArrivalTime: 2003-05-26 04:56:48 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-post-02!sn-post-01!supernews.com!corp.supernews.com!news.crayne.org!clax86!robomod!not-for-mail
From: Ben Peddell <killer.lightspeed@bigpond.com>
Newsgroups: comp.lang.asm.x86,gnu.gcc.help
Subject: Re: inline ASM: SSE
Approved: CLAX86 Moderators <ccrayne@crayne.org>
Date: Mon, 26 May 2003 20:35:57 +1000
Organization: Telstra BigPond Internet Services (http://www.bigpond.com)
Message-ID: <AsmAa.43159$1s1.590548@newsfeeds.bigpond.com>
X-CLAX86-Policy: http://www.pacificsites.com/~ccrayne/clax86.html
X-CLAX86-Faq: http://www2.dgsys.com/~raymoon/x86faqs.html
X-CLAX86-Info-1: Send submissions to             comp-lang-asm-x86@moderators.isc.org
X-CLAX86-Info-2: Send technical complaints to    ccrayne@crayne.org
X-CLAX86-Info-3: Send complaints about policy to ccrayne@crayne.org
X-Comment: moderators do not necessarily agree or disagree with this article.
User-Agent: Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.1) Gecko/20020826
X-Accept-Language: en-us, en
MIME-Version: 1.0
References: <Ko8Aa.20426$Kn1.264902@news.chello.at>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
X-Spamscanner: mailbox3.ucsd.edu  (v1.2 Mar 17 2003 15:04:36, -1.7/5.0 2.43)
X-Spam-Level: Level 
X-MailScanner: Found to be clean
X-MailScanner-SpamCheck: not spam (whitelisted), SpamAssassin (score=-2,
 required 5, NOSPAM_INC, QUOTED_EMAIL_TEXT, REFERENCES,
 SPAM_PHRASE_00_01, USER_AGENT, USER_AGENT_MOZILLA_UA, X_ACCEPT_LANG)
X-UIDL: ?l7!!I,P!!5]'"!>RC"!
X-Complaints-To: abuse@supernews.com
Lines: 74