访问向量类型的第i个组件

huangapple go评论56阅读模式
英文:

Access i'th component of vector type

问题

有没有一种方法可以访问类似int4的向量类型的第j个分量?以下是我目前的方法,但是否有更简单的方法?

int4 temp = (int4)(10, 20, 30, 40)
for (int j = 0; j < 4; ++j){
   int component_j = shuffle(pix, (uint4)(j, (j+1)&3, (j+2)&3, (j+3)&3)).s0;
}
英文:

Is there a way of accessing the j'th component of a vector type such as int4 ?
Here is how I currently do it, but is there an easier way ?

int4 temp = (int4)(10,20,30,40)
for (int j = 0; j < 4; ++j){
   int component_j = shuffle(pix, (uint4)(j,(j+1)&3,(j+2)&3,(j+3)&3)).s0;
}

答案1

得分: 3

There is no shuffle op in CUDA so your proposed method won't work there.

As indicated in the comments you could do:

int temp[4] = {10, 20, 30, 40};
for (int j = 0; j < 4; ++j){
int component_j = temp[j];
}

Another possibility if you want to preserve the vector type:

int4 temp = ...;
for (int j = 0; j < 4; ++j){
int component_j;
memcpy(&int_component_j, ((int *)&temp)+j, sizeof(int));
}

英文:

There is no shuffle op in CUDA so your proposed method won't work there.

As indicated in the comments you could do:

int temp[4] = {10, 20, 30, 40};
for (int j = 0; j &lt; 4; ++j){
   int component_j = temp[j];
}

Another possibility if you want to preserve the vector type:

int4 temp = ...;
for (int j = 0; j &lt; 4; ++j){
   int component_j;
   memcpy(&amp;int_component_j, ((int *)&amp;temp)+j, sizeof(int));
}

huangapple
  • 本文由 发表于 2023年3月4日 09:19:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定