Skip to content

Commit

Permalink
Merge pull request #1840 from DARMA-tasking/1839-ub-left-shifting-neg…
Browse files Browse the repository at this point in the history
…ative-signed-values

#1839: use unsigned types for left shifts in BitPacker
  • Loading branch information
nmm0 authored Jun 6, 2022
2 parents f792858 + 121aa67 commit 31a7e33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/vt/utils/bits/bits_packer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,27 @@

#include <cassert>
#include <cstdlib>
#include <cstring>

namespace vt { namespace utils {

// Non-constexpr C++14 version of bit_cast
// See: https://en.cppreference.com/w/cpp/numeric/bit_cast
template<typename Dst, typename Src>
std::enable_if_t<
sizeof(Dst) == sizeof(Src)
&& std::is_trivially_copyable<Dst>::value
&& std::is_trivially_copyable<Src>::value
&& std::is_trivially_constructible<Dst>::value,
Dst
>
bit_cast(const Src &src) {
Dst dst;
std::memcpy(&dst, &src, sizeof(Dst));
return dst;
}


struct BitPacker {
using FieldType = int64_t;
using FieldUnsignedType = uint64_t;
Expand Down
9 changes: 8 additions & 1 deletion src/vt/utils/bits/bits_packer.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,16 @@ template <typename BitType, typename BitField>
BitPacker::setFieldDynamic(
FieldType start, FieldType len, BitField& field, BitType const& segment
) {
// make_unsigned UB before C++20 for non-integral or bool types (ill-formed after C++20)
static_assert(std::is_integral<BitField>::value && !std::is_same<BitField, bool>::value,
"BitField must be non-bool integral type");
// sizeof(UnsignedBitField) is guaranteed to be the same as sizeof(BitField)
using UnsignedBitField = std::make_unsigned_t<BitField>;
auto const nbits = (sizeof(BitField) * 8) - len;
field = field & ~(gen_bit_mask(len) << start);
field = field | (((static_cast<BitField>(segment) << nbits) >> nbits) << start);
auto seg = bit_cast<UnsignedBitField>(static_cast<BitField>(segment));
UnsignedBitField bits = ((seg << nbits) >> nbits) << start;
field = field | bit_cast<BitField>( bits );
}

template <FieldType start, FieldType len, typename BitField>
Expand Down

0 comments on commit 31a7e33

Please sign in to comment.