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

Programming Examples

Several examples are provided in this section, one in Assembly and the others in C. Two downloadable files are provided containing one example per folder, one file for MPW and the other for CodeWarrior Pro 5.

Transpose Matrix

"transposeMatrix" and "transposeMatrixSubroutine" contain the sources to a matrix transpose and illustrate the use of the "pitsTT6" trace facility. (Note: pitsTT6 is the method to collect a execution trace on MacOS 9. On MacOS X, use amber instead. It is shipped as a command line utility as part of CHUD.)

The assembly language option is provided by the modified version of PPCAsm, which is available along with an example using the trace facility, "transposeMatrixInAsm".

Inverse Discrete Cosine Transform

Another example is a comprehensive set of Inverse Discrete Cosine Transform (IDCT) functions in C. IDCT is used in the codec section of MPEG2 video compression, but generally provides a good tutorial on conversion of existing scalar PPC programs to AltiVec.

The folder "inverseDiscreteCosineTransform (IDCT)" contains IDCT implementations in the PPC scalar integer and floating-point domain along with two AltiVec versions using the native AltiVec compiler and the programming model respectively. The folder is augmented with the original codec section of MPEG2 IDCT for the sake of completeness. All versions of IDCT in this folder use the same data and therefore produce the same output. This function does not use the trace feature, which is left as an exercise for the reader.

The Complete List of Examples

Project Builder Programming Examples and CodeWarrior Pro 7.2 Programming Examples contain the examples mentioned above as well as several other functions taken from everyday applications. They are included for illustrative purposes. However these functions have a direct performance impact in their relavent context. The Project Builder examples should work in XCode. The entire set is listed here:

Function
Description

alignment

A method to load and align data in the vector registers that does not reside on a 16-byte boundary.

bitInterleave

A trick for accomplishing bit-level interleaving on 128-bit streams of data.

constants

A short program showing how to create constants needed by algorithms, without having to prepare and load them from memory.

dotProduct

A function that calculates the dot product of 2 arrays of N elements.

DSTExample

This routine shows how to use the Data Stream Touch instructions to preload cache in order to improve program performance.

elementSelectAndInsert

These two functions show how to select a particular scalar element from a vector and how to insert a scalar into a particular element of a vector.

imageFilter

A filter that reduces the red component of a pixel.

inverseDiscreteCosineTransform

Four examples of the cosine transform, including one for MPEG2.

moreAlignment

Three more examples of techniques to load and store data from/to non-aligned memory locations.

multiply

This function multiplies 2 32-bit values and returns a 64-bit result.

nonZeroElement

This function finds the value of the only non-zero byte value in a AltiVec vector and moves it to the LSB position. This example also illustrates that with some care, a AltiVec implementation can be rendered even more efficient.

packAndUnpackPixel

Illustrates a technique to use the unpack instruction in a QuickDraw setting.

permuteMaskCalc

Two examples of generating permute mask constants.

permutePackUnpackPixel

An alternate technique of packing and unpacking pixel data using the permute instruction.

qdConstants

A collection of techniques to generate constants without the load instrucion.

setAndGetNJBit

An example of how to get and set the NJ bit of the VSCR (vector status and control register).

shiftsAndRotates

This function shows how to use all shifts and rotates in AltiVec.

tableLookUp

This function shows how to use the permute instruction to carry out a fast small table look up.

transposeMatrix

The three transpose matrix routines described above.

vectorElementCompare

This small program shows an easy way to get the comparison result of one element in a vec_f32 vector with another element.

Table of ContentsNextPrevious Top of Page