Technical: Hardware: G4
Advanced Search
Apple Developer Connection
Member Login Log In | Not a Member? Support

The C Programming Model

The C programming model for AltiVec is designed to make programming for AltiVec in C as much like programming for any other data type as possible. It is described in detail in the AltiVecâ„¢ Technology Programming Interface Manual from Motorola.

 

Advantages

The C Programming Model is an extension to C (and C++ and Obj C) that enables facile use of AltiVec within any C, C++ or Obj C program. You may of course program the vector unit using assembly language, if you prefer. However, the advantages of the C Programming Model over assembly are many:

  • Improved code readability
  • More stringent type checking
  • Automatic stack frame allocation and deallocation, including:
    • proper data alignment
    • observes stack frame conventions
    • automatic setup of the vrsave special purpose register
    • proper save and restore of volatile / non-volatile registers
  • The compiler will schedule your code for you for better speed
  • Dynamic register allocation (greatly improved in GCC 3.3!)
  • Other automatic optimizations that increase overall code performance

Developers who use the C Programming Model should find the AltiVec syntax and operation entirely familiar. Standard C programs should compile without modification on C compilers with the AltiVec C Programming Model enabled. New AltiVec-using functions and data structures may be then added and used just like other functions and data structures.

 

Data Types

The C Programming Model adds new intrinsic vector data types that correspond with the classical scalar intrinsics such as float, char, int, etc. The new intrinsic vector data types represent 128 bit vectors holding different kinds of data (chars, shorts, floats, etc.) as a packed 128 bit array. Their names are based on the packed scalar type contained within the vector. As an example, a vector unsigned short is a 128 bit long packed array of 16-bit unsigned integers (8 unsigned shorts stuffed end to end in a 16 byte array). The new vector keyword must be used to denote that it is a vector type and not a simple scalar.

8-bits
per element
16-bits
per element
32-bits
per element
vector bool char
vector bool short
vector bool int
vector signed char
vector signed short
vector signed int
vector unsigned char
vector unsigned short
vector unsigned int

vector pixel
vector float

Vector types work just like other types. You can load and store them, apply functions to them, declare them on the stack, write new functions that use them, etc.

 

Functions

In addition, a series of C-style functions that use these vector types have been provided. With a few exceptions, these map 1:1 with the 160 AltiVec instructions, giving you detailed control over the code generation from your vector code. For example, to add two AltiVec vectors together, you would use the vec_add "function":

result = vec_add( aVector, someOtherVector );

When you compile the application, the compiler will generate the following instruction:

vadduwm result, aVector, someOtherVector

(vadduwm is the AltiVec instruction for adding two vectors full of 32 bit ints to each other.) A complete list of AltiVec intrinsic functions appears in the Instruction Reference.

Table of ContentsNextPrevious