英文:
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 < 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));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论