Skip to content

Commit 0bbb091

Browse files
authored
[SYCL][ESIMD] Add support for esimd_ballot function (#4658)
* [SYCL][ESIMD] Add support for esimd_ballot function Signed-off-by: Sergey Dmitriev <[email protected]>
1 parent 8c1dddd commit 0bbb091

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

sycl/include/sycl/ext/intel/experimental/esimd/math.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,27 @@ ESIMD_NODEBUG ESIMD_INLINE
16671667
return esimd_pack_mask(src_0);
16681668
}
16691669

1670+
/// Compare source vector elements against zero and return a bitfield combining
1671+
/// the comparison result. The representative bit in the result is set if
1672+
/// corresponding source vector element is non-zero, and is unset otherwise.
1673+
/// @param mask the source operand to be compared with zero.
1674+
/// @return an \c uint, where each bit is set if the corresponding element of
1675+
/// the source operand is non-zero and unset otherwise.
1676+
template <typename T, int N>
1677+
ESIMD_NODEBUG ESIMD_INLINE typename sycl::detail::enable_if_t<
1678+
detail::is_type<T, ushort, uint> && (N > 0 && N <= 32), uint>
1679+
esimd_ballot(simd<T, N> mask) {
1680+
simd_mask<N> cmp = (mask != 0);
1681+
if constexpr (N == 8 || N == 16 || N == 32) {
1682+
return __esimd_pack_mask<N>(cmp.data());
1683+
} else {
1684+
constexpr int N1 = (N <= 8 ? 8 : N <= 16 ? 16 : 32);
1685+
simd<uint16_t, N1> res = 0;
1686+
res.template select<N, 1>() = cmp.data();
1687+
return __esimd_pack_mask<N1>(res.data());
1688+
}
1689+
}
1690+
16701691
/// Count number of bits set in the source operand per element.
16711692
/// @param src0 the source operand to count bits in.
16721693
/// @return a vector of \c uint32_t, where each element is set to bit count of

sycl/test/esimd/esimd_math.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,26 @@ bool test_esimd_trunc() __attribute__((sycl_device)) {
7272

7373
return (vfr[0] == 1.f) && (vsr[0] == 1) && (sfr == 2.f) && (ssr == 2);
7474
}
75+
76+
bool test_esimd_ballot() __attribute__((sycl_device)) {
77+
simd<ushort, 4> vus4 = {1, 0, 3, 0};
78+
simd<ushort, 8> vus8 = {1, 0, 3, 0, 5, 0, 7, 0};
79+
simd<ushort, 20> vus20 = {1, 0, 3, 0, 5, 0, 7, 0, 9, 0,
80+
1, 0, 3, 0, 5, 0, 7, 0, 9, 0};
81+
82+
uint mus4 = esimd_ballot(vus4);
83+
uint mus8 = esimd_ballot(vus8);
84+
uint mus20 = esimd_ballot(vus20);
85+
86+
simd<uint, 4> vui4 = {1, 0, 3, 0};
87+
simd<uint, 16> vui16 = {1, 0, 3, 0, 5, 0, 7, 0, 1, 0, 3, 0, 5, 0, 7, 0};
88+
simd<uint, 20> vui20 = {1, 0, 3, 0, 5, 0, 7, 0, 9, 0,
89+
1, 0, 3, 0, 5, 0, 7, 0, 9, 0};
90+
91+
uint mui4 = esimd_ballot(vui4);
92+
uint mui16 = esimd_ballot(vui16);
93+
uint mui20 = esimd_ballot(vui20);
94+
95+
return (mus4 == 0x5) && (mus8 == 0x55) && (mus20 = 0x55555) &&
96+
(mui4 == 0x5) && (mui16 == 0x5555) && (mui20 = 0x55555);
97+
}

0 commit comments

Comments
 (0)