不论是CPU还是GPU,一般来说总是将整数乘以2的幂转化为移位操作。
如下version 1和2 所示将tid*4转化为tid<<2,理论上应该加速的,但实际结果是ver 1比ver2快。
两个版本测了很多次,并且其太东西不变。
求合理可能的解释:
uint tid
version 1
des[tid * 4 + 0] = …;
des[tid * 4 + 1] = …;
des[tid * 4 + 2] = …;
des[tid * 4 + 3] = …;
__syncthreads();
…
__syncthreads();
float4 d;
d.x = __fdividef(des[tid * 4 + 0], len[0]);
d.y = __fdividef(des[tid * 4 + 1], len[0]);
d.z = __fdividef(des[tid * 4 + 2], len[0]);
d.w = __fdividef(des[tid * 4 + 3], len[0]);
d_des[idx * 16 + tid] = d;
version 2
des[(tid<<2)] = …;
des[(tid<<2) + 1] = …;
des[(tid<<2) + 2] = …;
des[(tid<<2) + 3] = …;
__syncthreads();
…
__syncthreads();
float4 d;
d.x = __fdividef(des[(tid<<2)], len[0]);
d.y = __fdividef(des[(tid<<2) + 1], len[0]);
d.z = __fdividef(des[(tid<<2) + 2], len[0]);
d.w = __fdividef(des[(tid<<2) + 3], len[0]);
d_des[(idx<<4) + tid] = d;