float3 operator/(float s, float3 a) Strange implementation

Hi,

I’ve got a question regarding the built-in division operator /. Here is the code from the cutil_math.h:

[codebox]

inline host device float3 operator/(float3 a, float s)

{

float inv = 1.0f / s;

return a * inv;

}

inline host device float3 operator/(float s, float3 a)

{

float inv = 1.0f / s;

return a * inv;

}

[/codebox]

If you look at the order of arguments for the two functions, it is different in each case, but the function body is identical. This means, that writing 1.f / xyz (uses the second function) gives the same result as xyz / 1.f (the first function). xyz is a float3 variable. I was kind of upset when I found out this feature/bug that was causing my kernels to crash. I was hoping that 1.f / float3 will return a float3 vector of reciprocals, but instead it returns the original float3 vector divided by 1.f. Can someone clarify the reasons why it is implemented this way? I suppose it is not a bug as it appears in all division operators that are there for some time already, but I does not appear to be very intuitive.

It would also be nice if I could ‘override’ the built-in operator with my own, which would return make_float3( s / a.x, s / a.y, s / a.z ). Is this somehow possible?

Thanks

–jan

cutil_math.h is not a “builtin” operator as part of CUDA. cutil is part of the SDK and is not as well tested and QAed as much as CUDA itself. So yes, you can absolutely write your own operators. Just copy and paste what you need from cutil_math.h as a starting point and make the modifications you need for your application.

Ok, now I see the problem. There is nothing such thing as built-in operators. I was including the cutil_math.h in my kernels, so that’s why I got them, but also with this weird implementation of one of the division operators.

Thanks!