[Bf-cycles] Comparison overloading for *4 types

Brecht Van Lommel brechtvanlommel at gmail.com
Tue Jun 5 15:54:12 CEST 2018


I think it would be best to be explicit about the conversion to bool, since
it's not immediately obvious if this should test if any or all of the
values are true.

OpenCL has builtin functions for this:

We could add the equivalent ones for CUDA and C++ like this:

#ifndef __KERNEL_OPENCL__
ccl_device_inline bool any(const int4& a)
    return a.x && a.y && a.z && a.w;


On Sat, Jun 2, 2018 at 3:30 PM, Geraldine Chua <chua.gsk at gmail.com> wrote:

> 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
> _______________________________________________
> Bf-cycles mailing list
> Bf-cycles at blender.org
> https://lists.blender.org/mailman/listinfo/bf-cycles
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.blender.org/pipermail/bf-cycles/attachments/20180605/2cdd88ab/attachment.html>

More information about the Bf-cycles mailing list