From f88b90dd564f59ca0f045df6f12c87185cbef687 Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Thu, 26 Oct 2006 20:25:22 -0400 Subject: [PATCH] Added a few functions to stuff values into bitfields in an instruction. --HG-- extra : convert_revision : 507d7e13fd6276acf36b75eba31dff5e8080113f --- src/base/bitfield.hh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/base/bitfield.hh b/src/base/bitfield.hh index 879780d56..177279678 100644 --- a/src/base/bitfield.hh +++ b/src/base/bitfield.hh @@ -69,4 +69,28 @@ sext(uint64_t val) return sign_bit ? (val | ~mask(N)) : val; } +/** + * Return val with bits first to last set to bit_val + */ +template +inline +T +insertBits(T val, int first, int last, B bit_val) +{ + T bmask = mask(first - last + 1) << last; + return ((bit_val << last) & bmask) | (val & ~bmask); +} + +/** + * A convenience function to replace bits first to last of val with bit_val + * in place. + */ +template +inline +void +replaceBits(T& val, int first, int last, B bit_val) +{ + val = insertBits(val, first, last, bit_val); +} + #endif // __BASE_BITFIELD_HH__