Fork me on GitHub

  最近遇到了一个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

posted on   虚生  阅读(2500)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示