Skip to content

Commit 48cc21c

Browse files
committed
Fix left shift operator undefined behavior in integer_ops
1 parent 5673883 commit 48cc21c

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

test_conformance/integer_ops/test_extended_bit_ops_insert.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ cpu_bit_insert(T tbase, T tinsert, cl_uint offset, cl_uint count)
3535
cl_ulong base = static_cast<cl_ulong>(tbase);
3636
cl_ulong insert = static_cast<cl_ulong>(tinsert);
3737

38-
cl_ulong mask = (count < 64) ? ((1ULL << count) - 1) << offset : ~0ULL;
39-
cl_ulong result = ((insert << offset) & mask) | (base & ~mask);
38+
cl_ulong mask = 0;
39+
if(offset < 64 ) mask = (count < 64) ? ((1ULL << count) - 1) << offset : ~0ULL;
40+
cl_ulong shifted = 0;
41+
if(offset < 64 ) shifted = (insert << offset);
42+
cl_ulong result = (shifted & mask) | (base & ~mask);
4043

4144
return static_cast<typename std::make_unsigned<T>::type>(result);
4245
}

0 commit comments

Comments
 (0)