Fix extract_bits and insert_bits.

(1 << len) is actually 1 when len is (sizeof(T)*8) so we need to special case that. len is usually constant so the compare won't be compiled.
This commit is contained in:
joevt 2023-09-18 16:17:36 -07:00 committed by dingusdev
parent 3062a29b78
commit 7fc92e236b
1 changed files with 2 additions and 2 deletions

View File

@ -60,12 +60,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
template <class T>
inline T extract_bits(T val, int pos, int len) {
return (val >> pos) & (((T)1 << len) - 1);
return (val >> pos) & ((len == sizeof(T) * 8) ? (T)-1 : ((T)1 << len) - 1);
}
template <class T>
inline void insert_bits(T &old_val, T new_val, int pos, int len) {
T mask = (((T)1 << len) - 1) << pos;
T mask = ((len == sizeof(T) * 8) ? (T)-1 : ((T)1 << len) - 1) << pos;
old_val = (old_val & ~mask) | ((new_val << pos) & mask);
}