> Am 18.07.2024 um 16:22 schrieb Alexander Monakov <amona...@ispras.ru>:
> 
> 
>> On Thu, 18 Jul 2024, Richard Biener wrote:
>> 
>> The following adds support for vector conditionals in C.  The support
>> was nearly there already but c_objc_common_truthvalue_conversion
>> rejecting vector types.  Instead of letting them pass there unchanged
>> I chose to instead skip it when parsing conditionals instead as a
>> variant with less possible fallout.  The part missing was promotion
>> of a scalar second or third operand to vector, I copied the logic
>> from binary operator handling for this.
>> 
>> I've moved the testcases I could easily spot over to c-c++-common.
>> 
>> The C++ frontend implements vector ? '1' : '0' with promotion of
>> both scalar to a vector but without applying integer promotions
>> first (r0-124280-g07298ffd6f7c2d).  I don't think this is
>> what we document or backed by OpenCL or C++ valarray.
> 
> I think we do document that:
> 
>    If both b and c are scalars and the type of true?b:c has the same size
>    as the element type of a, then b and c are converted to a vector type
>    whose elements have this type and with the same number of elements as a.
> 
> (in https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html )

But sizeof(typeof(true?‘a‘:‘b‘)) is four nevertheless the C++ frontend produces 
vectors of char.

> and I added
> 
>    In contrast to scalar operations in C and C++, operands of integer vector
>    operations do not undergo integer promotions.
> 
> when spelling out the behavior of vector shifts.
> 
> Clang also supports 'vector ? scalar : scalar'.
> 
>> I've left
>> this out for now.  I think we need better coverage in the testsuite
>> for this and others, for example the patch below now accepts
>> vector(int) ? vector(short) : 0 but the C++ frontend rejects this
>> (I think OpenCL is fine with this).  OpenCL says the conditional
>> op is equivalent to select(exp3,exp2,exp1) which is defined as
>> result[i] = if MSB of c[i] is set ? b[i] : a[i], with no restriction
>> on the conditional.
> 
> I think we require that sizes match because that's natural considering
> how it is lowered (bitwise blending of comparison mask with op2/op3).

It works naturally for mixed sizes with for example AVX512 and better lowering 
could be implemented for SSE.

> Note that in Clang ext_vector_type has OpenCL semantics for c?a:b
> (taking the MSB of 'c') while vector_size has GNU semantics ('c' is
> compared with zero).

Yes, that’s documented.

Richard 

> Thanks.
> Alexander

Reply via email to