Skip to content

Commit

Permalink
Fix a potential (but never materialized) UB
Browse files Browse the repository at this point in the history
  • Loading branch information
jk-jeon committed Apr 4, 2024
1 parent 7c56c6c commit d20ee3d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions include/dragonbox/dragonbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,14 @@ namespace jkj {

namespace bits {
// Most compilers should be able to optimize this into the ROR instruction.
// n is assumed to be at most of bit_width bits.
template <stdr::size_t bit_width, class UInt>
JKJ_CONSTEXPR14 UInt rotr(UInt n, unsigned int r) noexcept {
static_assert(bit_width > 0, "jkj::dragonbox: rotation bit-width must be positive");
static_assert(bit_width <= value_bits<UInt>::value,
"jkj::dragonbox: rotation bit width too large");
"jkj::dragonbox: rotation bit-width is too large");
r &= (bit_width - 1);
return (n >> r) | (n << (bit_width - r));
return (n >> r) | (n << ((bit_width - r) & (bit_width - 1)));
}
}

Expand Down

0 comments on commit d20ee3d

Please sign in to comment.