Cコードにおけるベクトル演算のいくつかの例

6899 ワード

最近1つのC言語の中でベクトル演算の問題に出会って、多くの失敗をして、ここで総括をして、後で類似の間違いを犯さないようにします.
データ構造の定義は次のとおりです.
typedef int v4si __attribute__ ((vector_size (16)))

基本的な操作:
typedef int v4si __attribute__ ((vector_size (16)));

v4si a, b, c;
long l;

a = b + 1;    /* a = b + {1,1,1,1}; */
a = 2 * b;    /* a = {2,2,2,2} * b; */

a = l + a;    /* Error, cannot convert long to int. */

特殊な演算:
typedef int v4si __attribute__ ((vector_size (16)));

v4si a = {1,2,3,4};
v4si b = {3,2,1,4};
v4si c;

c = a >  b;     /* The result would be {0, 0,-1, 0}  */
c = a == b;     /* The result would be {0,-1, 0,-1}  */

収束関数を使用した演算結果:
typedef int v4si __attribute__ ((vector_size (16)));

v4si a = {1,2,3,4};
v4si b = {5,6,7,8};
v4si mask1 = {0,1,1,3};
v4si mask2 = {0,4,2,5};
v4si res;

res = __builtin_shuffle (a, mask1);       /* res is {1,2,2,4}  */
res = __builtin_shuffle (a, b, mask2);    /* res is {1,5,3,6}  */

例:
#include 

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

int main()
{
        v4si a = {1,2,3,4};
        v4si b = {5,6,7,8};
        v4si mask1 = {0,1,1,3};
        v4si mask2 = {0,4,2,5};
        v4si res;
        v4si res1;

        res = __builtin_shuffle (a, mask1);       /* res is {1,2,2,4}  */
        res1 = __builtin_shuffle (a, b, mask2);    /* res is {1,5,3,6}  */


        for(int cnt =0; cnt < 4; cnt++)
        {

                printf("%d 
",res[cnt]); printf("%d
",res1[cnt]); } return 0; } ~

演算結果:
1 
1 
2 
5 
2 
3 
4 
6

実は、この概念についての詳細な説明は後でドキュメントを参考にしてください.私はただこれがどのように使われているかを知っています.
参照ドキュメント:
1 https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
2 https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Vector-Extensions.html