I’m just writing my first CUDA program, and it’s actually a rewrite of a C++ code. Now it deals with a lot of vector maths, so I use the float4 datatype which provides exactly what I need. However, the old code contains a lot of
float *vec;
vec = new float[4];
for(int i=0; i<4; i++) vec[i] = ...;
Now with float4 all I can do is write a line for each .x,.y,.z,.w which I find a bit annoying. Is there no way of accessing float4 elements in a similar fashion, i.e.
float4 vec;
for(int i=0; i<4; i++) vec[i] = ...;
Unfortunately I couldn’t find any hints on the internet.
Thanks in advance.
You could use a union, e.g.
For your arrays of
float4you would just change the underlying type toU4.Note: technically it’s UB to write to one variant of a union and then read from another, but it should work OK in this case and you don’t need to worry about portability since this is CUDA-specific.