[Bf-cycles] Comparison overloading for *4 types

Geraldine Chua chua.gsk at gmail.com
Sat Jun 2 15:30:21 CEST 2018


Hi all,

While working on my GSoC project, I came across an issue with doing
comparisons on *4 types (float4, uchar4, half4).

In float4's case, < is overloaded in *util/util_math_float4.h*, but it
returns an int4 instead of a bool, so using this for, say, if statements
would not work.

Here is the relevant code:
ccl_device_inline int4 operator<(const float4& a, const float4& b)
{
#ifdef __KERNEL_SSE__
    return int4(_mm_castps_si128(_mm_cmplt_ps(a.m128, b.m128)));
#else
    return make_int4(a.x < b.x, a.y < b.y, a.z < b.z, a.w < b.w);
#endif
}

There are two ways to resolve this:
1. Change the function to return a bool (which will probably break
everything that currently compares float4s).
2. Write a conversion operator for int4 so that we may evaluate the int4
comparison result as a bool. However, I have heard that operator bools are
discouraged since it can result in weird casting issues.

Implementation of #2 would be something like this:
/* *util/util_types_int4.h* */
struct ccl_try_align(16) int4 {
    int x, y, z, w;
    /* ... */
    operator bool() const { return x && y && z && w;  }
};

For uchar4 and half4, neither of them even have util_math files. A trivial
way to solve this would be to just copy most of *util/util_math_float4.h*
for these types, although I would like to ask around first before just
adding new util_math files. For these types, if I were to base their
util_math files on float4's, should we overload other operators such as +,
==, etc. since we are already creating the files, even if there is no code
currently using them?

I am interested in having proper overloaded operators particularly because
it would make template code much cleaner. What are your thoughts on what
should be done with these types?

Best regards,
Geraldine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.blender.org/pipermail/bf-cycles/attachments/20180602/0fb7d86c/attachment.html>


More information about the Bf-cycles mailing list