public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* asm() / goto() concern
       [not found] <1538337561.2146473.1653382920615.ref@mail.yahoo.com>
@ 2022-05-24  9:02 ` Ian Worthington
  2022-05-24 20:56   ` Segher Boessenkool
  0 siblings, 1 reply; 2+ messages in thread
From: Ian Worthington @ 2022-05-24  9:02 UTC (permalink / raw)
  To: gcc-help

I'm trying to write some inline assembler but I see that asm() does not support branching to labels, and that asm goto() does not support output (at least prior to gcc 11).  

If I tie them together by declaring an output of the asm() as an input to asm goto() I assume that this will prevent the optimiser from reordering them, but how can I ensure that they are run without any intervening instructions if the goto() has a dependency on the cc generated by the asm(), eg:

    asm ( "algr %[rs],%[rb] \n\t"
          : [rs] "+r" (sum)     // output
          : [rb] "r"  (u64b)    // input
        );

    asm goto ( "bc 3,%l[done]"        // branch if carry bit set (cc 2 or 3)
               :                    // output
               : [rs] "r"  (sum)    // input:  ensure this comes after the algr
               :                     // clobbers
               : done
             ); 

Best wishes / Mejores deseos /  Meilleurs vœux

Ian ...

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: asm() / goto() concern
  2022-05-24  9:02 ` asm() / goto() concern Ian Worthington
@ 2022-05-24 20:56   ` Segher Boessenkool
  0 siblings, 0 replies; 2+ messages in thread
From: Segher Boessenkool @ 2022-05-24 20:56 UTC (permalink / raw)
  To: Ian Worthington; +Cc: gcc-help

Hi!

On Tue, May 24, 2022 at 09:02:00AM +0000, Ian Worthington via Gcc-help wrote:
> I'm trying to write some inline assembler but I see that asm() does not support branching to labels, and that asm goto() does not support output (at least prior to gcc 11).  
> 
> If I tie them together by declaring an output of the asm() as an input to asm goto() I assume that this will prevent the optimiser from reordering them, but how can I ensure that they are run without any intervening instructions if the goto() has a dependency on the cc generated by the asm(), eg:

You cannot.  Sorry.

>     asm ( "algr %[rs],%[rb] \n\t"
>           : [rs] "+r" (sum)     // output
>           : [rb] "r"  (u64b)    // input
>         );
> 
>     asm goto ( "bc 3,%l[done]"        // branch if carry bit set (cc 2 or 3)
>                :                    // output
>                : [rs] "r"  (sum)    // input:  ensure this comes after the algr
>                :                     // clobbers
>                : done
>              ); 

You have to write to something that is a C variable in that first asm,
and read from that in the asm goto.  You don't get any advantage over
plain C that way, of course, for your example at least.

Alternatively you can use a newer compiler.  Or you can write the code
you want as a real assembler function (in a .s file, not inline asm).

gl;hf,


Segher

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-24 20:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1538337561.2146473.1653382920615.ref@mail.yahoo.com>
2022-05-24  9:02 ` asm() / goto() concern Ian Worthington
2022-05-24 20:56   ` Segher Boessenkool

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).