linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* PowerPC function returning long long
@ 1999-10-13  4:54 Bob Doyle
  1999-10-13  8:06 ` Gabriel Paubert
  0 siblings, 1 reply; 7+ messages in thread
From: Bob Doyle @ 1999-10-13  4:54 UTC (permalink / raw)
  To: linuxppc-dev


I was playing with the inline assembler and the ppc
timebase facility and created the following function -

unsigned long long get_timebase(void) {
        unsigned long tbu;
        unsigned long tbl;
        unsigned long junk;
        __asm__ __volatile__ ("
1:      mftbu   %2
        mftb    %1
        mftbu   %0
        cmpw    %0,%2
        bne     1b"
        : "=r" (tbu), "=r" (tbl), "=r" (junk));
        return ((unsigned long long)tbu << 32) | tbl;
}

This function compiles to (gcc 2.95.1) :

get_timebase:
1:      mftbu   5 
        mftb    6
        mftbu   0   
        cmpw    0,5 
        bne     1b
        mr 10,0
        li 9,0   
        mr 7,10
        mr 12,6
        li 8,0
        li 11,0
        or 3,7,11 
        or 4,8,12
        blr

As one can see, most of this is a bunch of register thrashing.
I expected it to generate something along this:

1:      mftbu   5 
        mftb    4
        mftbu   3   
        cmpw    3,4
        bne     1b
	blr 

I assume it is because gcc is struggling with the code
in the return statement.

Is there a better way to write the return statement?

Is there a register constraint for a long long register
(like the "A" constraint for the x86 which returns the
64 bit data in edx:eax)?

Any ideas?

Yes I know that there is a similar function in
arch/ppc/kernel/apus_setup.c which isn't quite
what I want.

Bob

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

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

* Re: PowerPC function returning long long
@ 1999-10-13  6:14 Christophe Lizzi
  0 siblings, 0 replies; 7+ messages in thread
From: Christophe Lizzi @ 1999-10-13  6:14 UTC (permalink / raw)
  To: linuxppc-dev, doyle



> I was playing with the inline assembler and the ppc
> timebase facility and created the following function -

> Is there a better way to write the return statement?
> 
> Is there a register constraint for a long long register
> (like the "A" constraint for the x86 which returns the
> 64 bit data in edx:eax)?

64 bit values are returned in r3:r4.

I'm definitively not an asm guru, but the following code works:

/*
  from PowerPC Microprocessor Family: The Programming Environments,
       section 2.2: PowerPC VEA Register Set - Time Base,
       IBM Microelectronics - Motorola.
*/

unsigned long long timebase(void)
{
    asm( "isync" );   /* discard prefetched instructions */
    /* the loop ensures that a consistent pair of values is obtained */
    asm("loop:"     ); 
    asm("mftbu 3"   ); /* load r3 from TBU       */
    asm("mftb  4"   ); /* load r4 from TBL       */
    asm("mftbu 5"   ); /* load r5 from TBU       */
    asm("cmpw  5, 3"); /* compare r5 and r3      */
    asm("bne   loop"); /* loop if carry occured  */
}

--Christophe

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

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

* Re: PowerPC function returning long long
  1999-10-13  4:54 Bob Doyle
@ 1999-10-13  8:06 ` Gabriel Paubert
  1999-10-13 12:27   ` Michael Meissner
  0 siblings, 1 reply; 7+ messages in thread
From: Gabriel Paubert @ 1999-10-13  8:06 UTC (permalink / raw)
  To: Bob Doyle; +Cc: linuxppc-dev




On Wed, 13 Oct 1999, Bob Doyle wrote:

> 
> I was playing with the inline assembler and the ppc
> timebase facility and created the following function -
> 
> unsigned long long get_timebase(void) {
>         unsigned long tbu;
>         unsigned long tbl;
>         unsigned long junk;
>         __asm__ __volatile__ ("
> 1:      mftbu   %2
>         mftb    %1
>         mftbu   %0
>         cmpw    %0,%2
>         bne     1b"
>         : "=r" (tbu), "=r" (tbl), "=r" (junk));
>         return ((unsigned long long)tbu << 32) | tbl;
> }

Rather write: 
unsigned long long get_timebase(void) {
        unsigned long long retval;
        unsigned long junk;
        __asm__ __volatile__ ("
1:      mftbu   %1
        mftb    %0+1
        mftbu   %0
        cmpw    %0,%1
        bne     1b"
        : "=r" (retval), "=r" (junk));
        return retval;
}

which generates on my machine (with optimization of course): 
Disassembly of section .text:

00000000 <get_timebase>:
   0:   7c 0d 42 e6     mftbu   r0
   4:   7c 8c 42 e6     mftb    r4
   8:   7c 6d 42 e6     mftbu   r3
   c:   7c 03 00 00     cmpw    r3,r0
  10:   40 82 ff f0     bne     0 <get_timebase>
  14:   4e 80 00 20     blr


Note: as you see you can access the second register of a 64 bit value with
%n+1. At this point I'd rather declarre the function inline...

> I assume it is because gcc is struggling with the code
> in the return statement.

Indeed. 64 bit integer handling is currently poor on PPC (except PPC64 of
course).

> Is there a better way to write the return statement?
> 
> Is there a register constraint for a long long register
> (like the "A" constraint for the x86 which returns the
> 64 bit data in edx:eax)?

No, because for 64 bit values, gcc always allocates a register pair
Rn/Rn+1. That why you can use the %n+1 trick. Register numbering on Intel
is a mess, the HW order atually is eax,ecx,edx,ebx,esp,ebp,esi,edi from
the register encoding and the pusha/popa instruction (but then it is
completely different for 16 bit adressing modes). And for 8 bit registers
it is al,cl,dl,bl,ah,ch,dh,bh.

	Regards,
	Gabriel.


** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

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

* Re: PowerPC function returning long long
  1999-10-13  8:06 ` Gabriel Paubert
@ 1999-10-13 12:27   ` Michael Meissner
  1999-10-14  4:12     ` Bob Doyle
  0 siblings, 1 reply; 7+ messages in thread
From: Michael Meissner @ 1999-10-13 12:27 UTC (permalink / raw)
  To: Gabriel Paubert, Bob Doyle; +Cc: linuxppc-dev


On Wed, Oct 13, 1999 at 10:06:02AM +0200, Gabriel Paubert wrote:
> 
> 
> 
> On Wed, 13 Oct 1999, Bob Doyle wrote:
> 
> > 
> > I was playing with the inline assembler and the ppc
> > timebase facility and created the following function -
> > 
> > unsigned long long get_timebase(void) {
> >         unsigned long tbu;
> >         unsigned long tbl;
> >         unsigned long junk;
> >         __asm__ __volatile__ ("
> > 1:      mftbu   %2
> >         mftb    %1
> >         mftbu   %0
> >         cmpw    %0,%2
> >         bne     1b"
> >         : "=r" (tbu), "=r" (tbl), "=r" (junk));
> >         return ((unsigned long long)tbu << 32) | tbl;
> > }
> 
> Rather write: 
> unsigned long long get_timebase(void) {
>         unsigned long long retval;
>         unsigned long junk;
>         __asm__ __volatile__ ("
> 1:      mftbu   %1
>         mftb    %0+1
>         mftbu   %0
>         cmpw    %0,%1
>         bne     1b"
>         : "=r" (retval), "=r" (junk));
>         return retval;
> }

The above doesn't work with -mreg-names and/or -pedantic.  Instead you should
write:

unsigned long long get_timebase(void) {
        unsigned long long retval;
        unsigned long junk;
        __asm__ __volatile__ ("\n\
1:      mftbu   %1\n\
        mftb    %L0\n\
        mftbu   %0\n\
        cmpw    %0,%1\n\
        bne     1b"
        : "=r" (retval), "=r" (junk));
        return retval;
}

-- 
Michael Meissner, Cygnus Solutions
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886
email: meissner@cygnus.com	phone: 978-486-9304	fax: 978-692-4482

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

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

* Re: PowerPC function returning long long
  1999-10-13 12:27   ` Michael Meissner
@ 1999-10-14  4:12     ` Bob Doyle
  1999-10-14  4:35       ` Michael Meissner
  1999-10-14 12:44       ` Daniel Jacobowitz
  0 siblings, 2 replies; 7+ messages in thread
From: Bob Doyle @ 1999-10-14  4:12 UTC (permalink / raw)
  To: Michael Meissner; +Cc: linuxppc-dev


Michael Meissner wrote:
> 
> On Wed, Oct 13, 1999 at 10:06:02AM +0200, Gabriel Paubert wrote:
> >
> > On Wed, 13 Oct 1999, Bob Doyle wrote:
> >

[snip]

> The above doesn't work with -mreg-names and/or -pedantic.  Instead you should
> write:
> 
> unsigned long long get_timebase(void) {
>         unsigned long long retval;
>         unsigned long junk;
>         __asm__ __volatile__ ("\n\
> 1:      mftbu   %1\n\
>         mftb    %L0\n\
>         mftbu   %0\n\
>         cmpw    %0,%1\n\
>         bne     1b"
>         : "=r" (retval), "=r" (junk));
>         return retval;
> }

Thanks, thats just what I was looking for... 

I've found several tutorials on x86 GNU inline asm but none on the
PowerPC.
If I were to write a HOWTO would anyone volunteer to proof read
and/or contribute code examples?  I suspect I'm not the only one 
struggling through this.

Bob Doyle
doyle@primenet.com

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

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

* Re: PowerPC function returning long long
  1999-10-14  4:12     ` Bob Doyle
@ 1999-10-14  4:35       ` Michael Meissner
  1999-10-14 12:44       ` Daniel Jacobowitz
  1 sibling, 0 replies; 7+ messages in thread
From: Michael Meissner @ 1999-10-14  4:35 UTC (permalink / raw)
  To: Bob Doyle, Michael Meissner; +Cc: linuxppc-dev


On Thu, Oct 14, 1999 at 04:12:30AM +0000, Bob Doyle wrote:
> 
> Michael Meissner wrote:
> > 
> > On Wed, Oct 13, 1999 at 10:06:02AM +0200, Gabriel Paubert wrote:
> > >
> > > On Wed, 13 Oct 1999, Bob Doyle wrote:
> > >
> 
> [snip]
> 
> > The above doesn't work with -mreg-names and/or -pedantic.  Instead you should
> > write:
> > 
> > unsigned long long get_timebase(void) {
> >         unsigned long long retval;
> >         unsigned long junk;
> >         __asm__ __volatile__ ("\n\
> > 1:      mftbu   %1\n\
> >         mftb    %L0\n\
> >         mftbu   %0\n\
> >         cmpw    %0,%1\n\
> >         bne     1b"
> >         : "=r" (retval), "=r" (junk));
> >         return retval;
> > }
> 
> Thanks, thats just what I was looking for... 
> 
> I've found several tutorials on x86 GNU inline asm but none on the
> PowerPC.
> If I were to write a HOWTO would anyone volunteer to proof read
> and/or contribute code examples?  I suspect I'm not the only one 
> struggling through this.

I don't know whether he reads this list, but the two PowerPC GCC maintainers
are David Edelsohn <edelsohn@gnu.org> and myself <meissner@cygnus.com>.  We can
probably offer suggestions, though I suspect we probably don't have as much
time to actually write anything.

-- 
Michael Meissner, Cygnus Solutions
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886
email: meissner@cygnus.com	phone: 978-486-9304	fax: 978-692-4482

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

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

* Re: PowerPC function returning long long
  1999-10-14  4:12     ` Bob Doyle
  1999-10-14  4:35       ` Michael Meissner
@ 1999-10-14 12:44       ` Daniel Jacobowitz
  1 sibling, 0 replies; 7+ messages in thread
From: Daniel Jacobowitz @ 1999-10-14 12:44 UTC (permalink / raw)
  To: linuxppc-dev


On Thu, Oct 14, 1999 at 04:12:30AM +0000, Bob Doyle wrote:
> Thanks, thats just what I was looking for... 
> 
> I've found several tutorials on x86 GNU inline asm but none on the
> PowerPC.
> If I were to write a HOWTO would anyone volunteer to proof read
> and/or contribute code examples?  I suspect I'm not the only one 
> struggling through this.

Yes!  I'd certainly help as I could, and I would love to see someone
put such a document together.

Dan

/--------------------------------\  /--------------------------------\
|       Daniel Jacobowitz        |__|        SCS Class of 2002       |
|   Debian GNU/Linux Developer    __    Carnegie Mellon University   |
|         dan@debian.org         |  |       dmj+@andrew.cmu.edu      |
\--------------------------------/  \--------------------------------/

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

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

end of thread, other threads:[~1999-10-14 12:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-10-13  6:14 PowerPC function returning long long Christophe Lizzi
  -- strict thread matches above, loose matches on Subject: below --
1999-10-13  4:54 Bob Doyle
1999-10-13  8:06 ` Gabriel Paubert
1999-10-13 12:27   ` Michael Meissner
1999-10-14  4:12     ` Bob Doyle
1999-10-14  4:35       ` Michael Meissner
1999-10-14 12:44       ` Daniel Jacobowitz

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 NNTP newsgroup(s).