C++ Logo

std-proposals

Advanced search

Re: [std-proposals] D3666R0 Bit-precise integers

From: David Brown <david.brown_at_[hidden]>
Date: Thu, 4 Sep 2025 16:39:11 +0200
On 04/09/2025 15:02, Bjorn Reese via Std-Proposals wrote:
> On 9/3/25 09:03, David Brown via Std-Proposals wrote:
>
>> While code like that /uses/ implicit bool-to-int conversions, having
>> those conversions be implicit is not at all necessary. If the
>> conversions were not implicit, you'd simply write :
>>
>> return int(x >= T(0) - int(x <= T(0));
>
> Presumably you meant to explictly cast to T rather than int, otherwise
> it would fail for SIMD types.

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));

>
> In the generic case there may be no implicit cast at all. The comparison
> may return a mask type, and the operators have overloads to handle the
> combination of T and its associated mask type.
>

OK. I was assuming a standard comparison operator that returns a bool,
since we were talking about implicit bool to int conversions. If you
are talking about types that hold SIMD vectors and masks, then your
comparisons, subtractions, conversions and other operators need to be
defined appropriately anyway - you don't have a simple bool with an
implicit cast. So that is a significantly different situation.

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.

Received on 2025-09-04 14:39:15