Skip to content

Commit

Permalink
fixes mask building in bit_sub
Browse files Browse the repository at this point in the history
  • Loading branch information
eyck committed Feb 18, 2023
1 parent 9da215c commit cd08370
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/common/util/ities.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@
template <unsigned int bit, unsigned int width, typename T>
inline constexpr typename std::enable_if<std::is_unsigned<T>::value, T>::type bit_sub(T v) {
static_assert((bit+width)<=8*sizeof(T));
return (v >> bit) & ((T(1) << width) - 1);
T res = (v >> bit) & ((T(1) << width) - 1);
return res;
}

template <unsigned int bit, unsigned int width, typename T>
inline constexpr typename std::enable_if<std::is_signed<T>::value, T>::type bit_sub(T v) {
static_assert((bit+width)<=8*sizeof(T));
auto sign = (1 << (bit + width - 1)) & v;
auto mask = ((T(1) << width) - 1);
return sign? (v >> bit) | mask : (v >> bit) & mask;
static_assert(width>0);
auto field = v>>bit;
auto amount = (field & ~(~T(1) << (width - 1) << 1)) -
(field & (T(1) << (width - 1)) << 1);
return amount;
}
/**
* @brief sign-extend a given value
Expand Down

0 comments on commit cd08370

Please sign in to comment.