Skip to content

Commit d2e6a26

Browse files
jcranmer-intelbader
authored andcommitted
[SYCL] Support rotating by over-sized shift widths.
Signed-off-by: Joshua Cranmer <joshua.cranmer@intel.com>
1 parent 77e8693 commit d2e6a26

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

sycl/source/detail/builtins_integer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@ template <typename T> inline T __u_long_mad_sat(T a, T b, T c) {
154154

155155
template <typename T> inline T __rotate(T x, T n) {
156156
using UT = typename std::make_unsigned<T>::type;
157-
return (x << n) | (UT(x) >> ((sizeof(x) * 8) - n));
157+
// Shrink the shift width so that it's in the range [0, num_bits(T)). Cast
158+
// everything to unsigned to avoid type conversion issues.
159+
constexpr UT size = sizeof(x) * 8;
160+
UT xu = UT(x);
161+
UT nu = UT(n) & (size - 1);
162+
return (xu << nu) | (xu >> (size - nu));
158163
}
159164

160165
template <typename T> inline T __u_sub_sat(T x, T y) {

sycl/test/built-ins/scalar_integer.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,22 @@ int main() {
319319
assert(r == 0x00000111);
320320
}
321321

322+
// rotate (with large rotate size)
323+
{
324+
s::cl_char r{ 0 };
325+
{
326+
s::buffer<s::cl_char, 1> BufR(&r, s::range<1>(1));
327+
s::queue myQueue;
328+
myQueue.submit([&](s::handler &cgh) {
329+
auto AccR = BufR.get_access<s::access::mode::write>(cgh);
330+
cgh.single_task<class rotateSI1SI2>([=]() {
331+
AccR[0] = s::rotate(static_cast<s::cl_char>((unsigned char)0xe0),
332+
s::cl_char{ 50 });
333+
});
334+
});
335+
}
336+
assert((unsigned char)r == 0x83);
337+
}
322338
// sub_sat
323339
{
324340
s::cl_int r{ 0 };

0 commit comments

Comments
 (0)