Thursday, December 29, 2011

GCC vector intrinsics example

typedef int v4si __attribute__ ((vector_size(16)));

typedef int RGBA[4];

void diff(const RGBA & a, const RGBA & b, RGBA & out) {
#if 0
    for(int i = 0; i < 4; ++i)
        out[i] = a[i] - b[i];
   
    for(int i = 0; i < 4; ++i) {
        int v = out[i];
        if (v < 0) v =0;
        if (v > 255) v = 255;
        out[i] = v;
    }
#else
v4si va = *(v4si*)a;//{a[0], a[1], a[2], a[3]};
v4si vb = *(v4si*)b;//{b[0], b[1], b[2], b[3]};
v4si vr;
    vr = __builtin_ia32_psubd128(va, vb);
    *(v4si*)(&out[0]) = vr;
   
#endif
   
}

No comments: