2

I've tried playing with clang's extended vectors. The ternary operator is supposed to work, but it is not working for me. Example:

int main()
{
  using int4 = int __attribute__((ext_vector_type(4)));

  int4 a{0, 1, 3, 4};
  int4 b{2, 1, 4, 5};

  auto const r(a - b ? a : b);

  return 0;
}

Please provide examples on how I might make it work, like it works under OpenCL. I am using clang-3.4.2.

Error:

t.cpp:8:16: error: value of type 'int __attribute__((ext_vector_type(4)))' is not contextually convertible to 'bool'
  auto const r(a - b ? a : b);
               ^~~~~
1 error generated.
user1095108
  • 14,119
  • 9
  • 58
  • 116

3 Answers3

3

You can loop over the elements directly in Clang. Here is a solution for GCC and Clang.

#include <inttypes.h>
#include <x86intrin.h>

#if defined(__clang__)
typedef float float4 __attribute__ ((ext_vector_type(4)));
typedef   int   int4 __attribute__ ((ext_vector_type(4)));
#else
typedef float float4 __attribute__ ((vector_size (sizeof(float)*4)));
typedef   int   int4 __attribute__ ((vector_size (sizeof(int)*4)));
#endif

float4 select(int4 s, float4 a, float4 b) {
  float4 c;
  #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
  c = s ? a : b;
  #else
  for(int i=0; i<4; i++) c[i] = s[i] ? a[i] : b[i];
  #endif
  return c;
}

The both generate

select(int __vector(4), float __vector(4), float __vector(4)):
  pxor xmm3, xmm3
  pcmpeqd xmm0, xmm3
  blendvps xmm1, xmm2, xmm0
  movaps xmm0, xmm1
  ret

But with AVX512 it's better to use masks (e.g. __mmask16).

Z boson
  • 32,619
  • 11
  • 123
  • 226
1

This works in a pinch:

auto const diff = a-b;
auto const ra( - (diff!=zero) * a - (diff==zero) *b);

I guess this is a bug in the compiler, or in the documentation you linked.

pqnet
  • 6,070
  • 1
  • 30
  • 51
  • A little bit of both. I think it's [this bug](http://llvm.org/bugs/show_bug.cgi?id=15781), which shows that clang does implement `?:` for vector conditions, but only in OpenCL context. –  Aug 17 '14 at 12:43
  • You gave a workaround :) Multiplication is not the same as the ternary operator. – user1095108 Aug 17 '14 at 13:41
  • @user1095108 in SIMD hardware this is usually faster. If the ternary operator doesn't work it is not my fault – pqnet Aug 17 '14 at 14:17
  • @pqnet I didn't know that. Are you sure? From what I've seen in the code compiled with gcc using ?: created no branches in the code. – user1095108 Aug 17 '14 at 14:30
  • @user1095108 if it doesn't its because it does some operation similar to this. Are you referring to the vectorial version of the operator? is it supported by gcc? Of course, if the compiler optimizes the final code for the ternary operator and this version should be the same in the end. Note however that this implementation assumes true = -1, which I observed true for this case (but there is no guarantee of it) – pqnet Aug 17 '14 at 15:44
  • @pqnet Yes, gcc is much better when it comes to the ternary operator support, as well as vector support in general. I've ported some code from gcc and then expected it (in vain) to work out of the box. – user1095108 Aug 17 '14 at 16:23
0

In the end I went with this:

#if defined(__clang__)
template <typename U, typename V>
constexpr inline std::enable_if_t<
  !std::is_arithmetic<V>{},
  V
>
select(V const a, V const b, U const c) noexcept
{
  return V((c & U(a)) | (~c & U(b)));
}
#else
template <typename U, typename V>
constexpr inline std::enable_if_t<
  !std::is_arithmetic<V>{},
  V
>
select(V const a, V const b, U const c) noexcept
{
  return c ? a : b;
}
#endif

The same could have been accomplished in other ways, using the indices trick, for example, but it might not optimize very well (I didn't want any conditionals in there).

user1095108
  • 14,119
  • 9
  • 58
  • 116