Date: Thu, 4 Sep 2025 18:41:28 +0200
On 9/4/25 16:39, David Brown via Std-Proposals wrote:
> I had meant to explicitly cast to int, but was missing some parentheses
> (because your code has questionable use of parentheses, and I was not
> paying enough attention) :
>
> return int(x >= T(0)) - int(x <= T(0));
Apologies for the missing paranthesis in my code. However, that is not
the issue. The issue is that if T is a SIMD type, then the comparison
returns a mask containing multiple answers. Casting to int (if T has
an approriate conversion operator) and subtracting the scalar values
gives the wrong result. Casting to T gives the right result.
> It is, however, not unknown for people to use "(x >= 0) - (x <= 0)" as a
> way to calculate the sign of a scalar integer. And there, explicit
> casts to int as I wrote (or tried to write!) would work just as well as
> implicit casts from bool to int.
You are arguing for the removal of implicit bool casts, but this has
implications for generic code as I have shown.
> I had meant to explicitly cast to int, but was missing some parentheses
> (because your code has questionable use of parentheses, and I was not
> paying enough attention) :
>
> return int(x >= T(0)) - int(x <= T(0));
Apologies for the missing paranthesis in my code. However, that is not
the issue. The issue is that if T is a SIMD type, then the comparison
returns a mask containing multiple answers. Casting to int (if T has
an approriate conversion operator) and subtracting the scalar values
gives the wrong result. Casting to T gives the right result.
> It is, however, not unknown for people to use "(x >= 0) - (x <= 0)" as a
> way to calculate the sign of a scalar integer. And there, explicit
> casts to int as I wrote (or tried to write!) would work just as well as
> implicit casts from bool to int.
You are arguing for the removal of implicit bool casts, but this has
implications for generic code as I have shown.
Received on 2025-09-04 16:41:34