Compile AltiVec code in OS X?

rharder

Do not read this sign.
Does anyone know how to get AltiVec instructions to compile in OS X?

I was going through <a href="http://developer.apple.com/hardware/ve/tutorial.html">Apple's tutorial</a>, and I couldn't get anything to compile with "cc" or "cpp."

Are there special header files? Special compiler?

-Rob
 
Ah. There's a -faltivec flag for the "cc" compiler, and that gets you a little further but not all the way.

-Rob
 
I still can't compile AltiVec code. The "vector" keyword seems to throw it for a loop when I compile with "cc -altivec".

Anyone successfully compiled AltiVec code on OS X?

-Rob
 
Have you tried searching the header files for the word 'vector'? Sounds like you may not be including a needed header file.
 
No luck with the header files, but I might have had a different error after all. Here's a simple file that takes two 4-float arrays and does a a*x + b calculation on them in one fell swoop. I present it as starter code for those who would like to start incorporating AltiVec instructions in their code (like me). With a little tweak you could as easily do a a*x + b also:

Code:
main()
{
    float a[] = { 1, 2, 3, 4 };
    float b[] = { 5, 6, 7, 8 };
    float x = 3.14;
    int memOffset = 0;
    float result[4];
    vector float aV, bV, xV, resultV;

    // Convert scalar x to vector xV
    vector float dummy = (vector float)(0);
    *((float *)&dummy) = x;
    xV = vec_splat( dummy, 0 );

    // Create vector types from 4-float arrays
    aV = vec_ld( memOffset, &a[0] );
    bV = vec_ld( memOffset, &b[0] );

    // Perform the a[i]*x + b[i] calculation
    resultV = vec_madd( xV, aV, bV );

    // Save vector type in a 4-float array
    vec_st( resultV, memOffset, &result[0] );

    return 0;
}

I don't know how the dummy code works. I modeled it after some sample code I found from the VAST guys.

-Rob
 
Thank you rob! the -faltivec switch does the trick.

I think that the thing were doing differently is that you're not adding the vecLib Framework to your project. By the way add that -faltivec to the OTHER_CFLAGS in Expert build settings.

peter
 
Thanks, Peter. You're way ahead of me. I haven't even gotten to the ProjectBuilder stuff yet.

-Rob
 
rob,

Just an update, you don't need the vecLib framework after all. It adds convenience funtions and stuff like that. all you need is the switch. also I'm not ahead of you, I couldn't get anything to work until you told me about the -faltivec switch. And just like you I'm still copying and pasting routines without having a clue how to write my own. Know any good docs?

Funny i did the project builder stuff first. I think its easier that way, just set the switch and you're away.

Peter
 
I haven't found any docs that I've been able to make much sense of. Apple has a whole bunch of libraries for doing "common" things like matrix inversions and manipulating very very large integers, but I haven't seen anything that's really given me ideas about how to restructure code.

The guys at http://www.psrv.com/altivec.html have an interesting example of their code and how it compiled things. They had some clever ideas such as not just looping through 4 numbers at a time in one vector, but 4 sets of 4 numbers (16 total) at a time. I think the idea is that the 4 vector ops in a row might pipeline nicely.

There's a PDF from Apple or Motorola that has all the functions listed with a technical description of them, but no good examples of how to use them. Too bad. There's a lot of potential hiding away inside the AltiVec instructions.

-Rob
 
Back
Top