Re: AltiVec on OSX in Cocoa
Re: AltiVec on OSX in Cocoa
- Subject: Re: AltiVec on OSX in Cocoa
- From: Brendan Younger <email@hidden>
- Date: Sun, 11 Nov 2001 00:39:13 -0500
On Saturday, November 10, 2001, at 09:15 PM, Jake wrote:
How does one go about adding altiVec code in a Cocoa program? Looking at
the Apple documentation seems to suggest that one should use MrC. I
don't
see an altivec project template. Maybe someone here can shed some light
on
this. By inspecting some of the sample project builder projects i see
that
one need to add
-faltivec
as a compile option. Is there any thing else that one need to do other
than the addition of the compile time flag?
MrC has really complete support for altivec, unfortunately, gcc is not
quite as tailored to it.
The C extensions and the "vector" keyword are both fully supported, but
the memory allocation is a little sketchy.
Stack-based vectors will be automatically aligned correctly, but AFAIK,
there is no vec_malloc() which will guarantee 16-bit aligned memory
blocks. So, you are stuck either rolling your own, or using NewPtr()
(from Carbon). However, I have had some difficulties with NewPtr()
(more specifically, DisposePtr()). Hence, I suggest using a custom
struct which will hold both a pointer to the actual memory allocated and
a pointer to the next 16-bit aligned memory address.
(Written in Mail, so please don't blindly cut and paste)
struct a {
char* realPointer;
vector unsigned char* vectors;
};
//And then code your allocation like this:
a.realPointer = malloc((numberOfVectors * 16) + 15);
if(a.realPointer != nil)
a.vectors = a.realPointer + ((int)(a.realPointer) & 0x0000000F);
//Deallocation is a cinch:
free(a.realPointer);
Sure, you waste a few bytes here and there, but it's better than
dynamically adjusting for non-aligned addresses.
As far as a template is concerned, the best you're going to get is:
http://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Velocity_Engine.
htm. There's also some good general info at
http://developer.apple.com/hardware/ve/.
Brendan Younger