首页 > 编程知识 正文

矢量编程语言,矢量的运算一定要遵循特殊的方法

时间:2023-05-06 15:10:11 阅读:225646 作者:3016

  最近遇到了一个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 <stdio.h>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 n",res[cnt]); printf("%d n",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

转载于:https://www.cnblogs.com/dylancao/p/9954214.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。