如何在C/Objective C中使用SSE指令集?

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

我正在尝试制作一个对 2 CGVectors 求和的函数。

这是我的尝试:

包括:

@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
    }

然后我尝试像

那样使用它
    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);

没有编译器错误但是我从日志中得到的是

V3 = (6.000000, 3.000000)

我预计会有 6,7 :-)

我做错了什么?

你应该可以做到:

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);

输出:

V3 = (6.000000, 7.000000)