Operator overloading for float4


The following snippet successfully compiles and runs on the GPU.

inline __device__ float4 operator*(float4 a,float4 b) {return make_float4(a.x*b.x,a.y*b.y,a.z*b.z,a.w*b.w);}

However, since float4 is a struct, there is no such thing as the keyword ‘this’, so something like the following:

inline __device__ (float4&) operator*=(float4 b) {return make_float4(this->x*b.x,this->y*b.y,this->z*b.z,this->w*b.w);}

does not work, which was expectable anyway. My question is how can I overload the *= operator for a struct, when there is no ‘this’. I am aware that (a *= b) is simply (a = a * b), but can it be done?

Thank you.

Try this.

__device__ inline float4& operator +=(float4& a, const float4& b)
    a.x += b.x;
    a.y += b.y;
    a.z += b.z;
    a.w += b.w;
    return a;