Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: pseudo-builtin ppc instructions




On Sep 30, 2004, at 8:58 AM, Marc Colosimo wrote:

On Sep 29, 2004, at 5:42 PM, Ian Ollmann wrote:

For future reference, you can find many ppc instructions, including isync, available for use as pseudo-builtins in the style of MWCW __intrinsics() if you  #include<ppc_intrinsics.h>.


Wow, didn't know about those. This is for postgreSQL and used in ppc blocks (#ifdef __ppc__), which include linux and ibm machines. Is ppc_intrinsics.h included in linux and xlc? Now it lives at  /usr/include/gcc/darwin/3.3/ppc_intrinsics.h.


Also, is there a good tutorial on using asm in gcc? What I've found seems to be high level.
For example, what are "memory" or "cc"  for?


#define __eieio() __asm__ ("eieio" : : : "memory")

I'm not aware of a good one. There is some description in the GCC mainline documentation available online. 


There are several ways to use assembly in C code with gcc. 

1) You can use the cryptic GCC native syntax, e.g.:

        __asm__ ( "mfspr %0, 1023" : "=r" (result ) );

This is often extremely difficult to use correctly because the format specifiers are picky and there are a lot of possibilities. There are other subtle necessities that need to be added for consistently correct operation. There is no feedback when you get it wrong other than incorrect operation, and in many cases you can get it wrong, have it work anyway, only to fail when you change compiler revisions or tweak other flags. It is not recommended that you go crazy with these unless you have a lot of experience with that syntax.

2) In order to boost MWCW compatibility and ease of use, Apple added the ppc_intrinsics.h header to map the difficult to use syntax to Metrowerks' easy one, e.g.:

        result = __mfspr( 1023 );

The macros/functions in the header should use the right format specifiers. I don't know if this header has been pushed into mainline gcc or not. Andrew might. However, subject to various legal restrictions I won't claim to understand you can at least in practice copy the header around from platform to platform and expect it to still work as long as its PowerPC. 

3) You may of course write complete files entirely in assembly using normal methods. 

4) Finally you can mix asm blocks into C as follows:

int ReadSPR1023( void )
{
    register int result;

    asm
    {
        mfspr  result, 1023
    }

    return result;
}

Enable with -fasm-blocks.

If you run into problems with any of these methods (except (1) where it is likely your fault ;-) please file a bug against the gcc component. Inline assembly is not frequently used and doesn't get as thorough battle testing as do most other compiler features. 

Please note that most or all inline assembly methods typically confuse GCC's scheduler and may cause some spectacularly bad (but correctly operating) code generation in unfortunate cases. Thus, asm optimizations that should speed up code may end up slowing it down. Where possible, I'd recommend using the GCC real __builtin*s instead. These gcc knows how to schedule. A common example would be data prefetch hints. You could use dcbt:

        __dcbt( byteOffset, address );

but better to use the builtin since gcc knows how to schedule that:

        __builtin_prefetch( (char*) address + byteOffset );

It should also be more portable.

Ian

P.S. Please note that the particular mfspr asm example I've chosen above will terminate user code with an illegal instruction exception. This is correct. 1023 is a supervisor level SPR. I use it frequently as a trigger to get amber -I to start sampling.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Scitech mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/scitech/email@hidden


This email sent to email@hidden
References: 
 >Re: GCC 3.3 can't inline?!? (From: Ian Ollmann <email@hidden>)
 >Re: pseudo-builtin ppc instructions (From: Marc Colosimo <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.