<div dir="ltr"><div>Hi,</div><div><br></div><div>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.</div><div><br></div><div>OpenCL has builtin functions for this:<br></div><div><a href="https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/any.html">https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/any.html</a></div><div><br></div><div>We could add the equivalent ones for CUDA and C++ like this:<br></div><div><br></div><div><span style="font-family:monospace,monospace">#ifndef __KERNEL_OPENCL__<br>ccl_device_inline bool any(const int4& a)<br>{</span></div><div><span style="font-family:monospace,monospace">    return a.x && a.y && a.z && a.w;<br>}</span></div><div><span style="font-family:monospace,monospace">#endif<br></span></div><div><br></div><div>Regards,</div><div>Brecht.<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jun 2, 2018 at 3:30 PM, Geraldine Chua <span dir="ltr"><<a href="mailto:chua.gsk@gmail.com" target="_blank">chua.gsk@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi all,</div><div><br></div><div>While working on my GSoC project, I came across an issue with doing comparisons on *4 types (float4, uchar4, half4).<br></div><div><br></div><div>In float4's case, < is overloaded in <i>util/util_math_float4.h</i>, but it returns an int4 instead of a bool, so using this for, say, if statements would not work.</div><div><br></div><div>Here is the relevant code:</div><div>ccl_device_inline int4 operator<(const float4& a, const float4& b)<br>{<br>#ifdef __KERNEL_SSE__<br>    return int4(_mm_castps_si128(_mm_<wbr>cmplt_ps(a.m128, b.m128)));<br>#else<br>    return make_int4(a.x < b.x, a.y < b.y, a.z < b.z, a.w < b.w);<br>#endif<br>}<br></div><div><br></div><div>There are two ways to resolve this:</div><div>1. Change the function to return a bool (which will probably break everything that currently compares float4s).</div><div>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.<br></div><div><br></div><div>Implementation of #2 would be something like this:</div><div>/* <i>util/util_types_int4.h</i> */<br></div><div>struct ccl_try_align(16) int4 {</div><div>    int x, y, z, w;<br></div><div>    /* ... */<br></div><div>    operator bool() const { return x && y && z && w;  }<br>};</div><div><br></div><div>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 <i>util/util_math_float4.h</i> 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?<br></div><div><br></div><div>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?</div><div><br></div><div>Best regards,</div><div>Geraldine<br></div><div><br></div></div>
<br>______________________________<wbr>_________________<br>
Bf-cycles mailing list<br>
<a href="mailto:Bf-cycles@blender.org">Bf-cycles@blender.org</a><br>
<a href="https://lists.blender.org/mailman/listinfo/bf-cycles" rel="noreferrer" target="_blank">https://lists.blender.org/<wbr>mailman/listinfo/bf-cycles</a><br>
<br></blockquote></div><br></div>