user3351949 user3351949 - 2 months ago 22
Objective-C Question

How to use SSE Instruction set in C/Objective C?

I'm trying to make a Function that Sums 2

CGVectors
.

This is my try:

Includes:

@import GLKit;
@import Foundations;


// Defenition of CGVector
struct CGVector {
CGFloat dx;
CGFloat dy;
};

typedef struct CGVector CGVector;

inline CGVector CGVECADD(CGVector v1 , CGVector v2){
#ifdef GLK_SSE3_INTRINSICS
__m128d v;
v = _mm_add_sd(*(__m128d *)&v1, *(__m128d *)&v2);

return *(CGVector *)& v;
#else
return CGVectorMake(v1.dx + v2.dx, v1.dy + v2.dy);
#endif
}


Then I am trying to use it like

CGVector v1, v2 , v3;
v1 = CGVectorMake(2, 3);
v2 = CGVectorMake(4, 4);;
v3 = CGVECADD(v1, v2); //(6, 7) expected
NSLog(@"V3 = (%lf, %lf)", v3.dx , v3.dy);


No compiler errors But what I got from logs is


V3 = (6.000000, 3.000000)


I expected to have 6,7 :-)

What Have I done wrong ?

Answer

You should be able to do:

CGVector v1, v2, v3;
v1 = CGVectorMake(2, 3);
v2 = CGVectorMake(4, 4);
v3 = CGVectorMake(v1.dx + v2.dx, v1.dy + v2.dy);
NSLog(@"V3 = (%f, %f)", v3.dx, v3.dy);

Output:

V3 = (6.000000, 7.000000)