How to implement float min(float x, ...) ?


I suggest you not use var_arg functions if you can avoid them.

So I propose a different path.

As a jumping-off point, here is a category method on NSData that assumes that "self" (the NSData instance) is an array of floats:

(The -floats category method merely casts NSData's -bytes to return a pointer-to-float, and the -floatlength category method divides -length by sizeof(float).)

Note that this makes the NSData the memory equivalent of a C array/ vector, so you can now pass it to the Accelerate framework.

Any setup overhead is likely compensated for by the hardware accel of vDSP (when available).

- (float) floatMinValueAndIndex:(int *) outIndex
{
        float minValue = 0;
vDSP_minvi((void *)[self floats], 1, &minValue, outIndex, [self floatLength]);
        return minValue;        
}

The following NSData class category method will convert an arbitrary float array to the NSData required for the above:

+ (NSData *) floatsWithBlock: (float *) block count: (unsigned) count // count of floats
{
NSData * result = [[self class] dataWithBytes: block length: (count * sizeof(float))];
        return result;  
}

- (const float *) floats
{
        return (const float *) [self bytes];
}

- (unsigned) count
{
        return [self floatLength];
}

- (unsigned) floatLength
{
        return [self length]/sizeof(float);
}


If performance is less of an issue than clarity, I would convert the floats to an NSArray of NSNumbers, and then just sort the array using the -sortedArrayUsing... methods. You get more information back -- not just min, but max and next-to-min, and next-to-max, etc.


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to