2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Steve Reinhardt
32 #ifndef __BASE_BITFIELD_HH__
33 #define __BASE_BITFIELD_HH__
35 #include "base/types.hh"
38 * Generate a 64-bit mask of 'nbits' 1s, right justified.
43 return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
49 * Extract the bitfield from position 'first' to 'last' (inclusive)
50 * from 'val' and right justify it. MSB is numbered 63, LSB is 0.
55 bits(T val, int first, int last)
57 int nbits = first - last + 1;
58 return (val >> last) & mask(nbits);
62 * Extract the bit from this position from 'val' and right justify it.
69 return bits(val, bit, bit);
73 * Mask off the given bits in place like bits() but without shifting.
79 mbits(T val, int first, int last)
81 return val & (mask(first+1) & ~mask(last));
85 mask(int first, int last)
87 return mbits((uint64_t)-1LL, first, last);
91 * Sign-extend an N-bit value to 64 bits.
98 int sign_bit = bits(val, N-1, N-1);
99 return sign_bit ? (val | ~mask(N)) : val;
103 * Return val with bits first to last set to bit_val
105 template <class T, class B>
108 insertBits(T val, int first, int last, B bit_val)
110 T t_bit_val = bit_val;
111 T bmask = mask(first - last + 1) << last;
112 return ((t_bit_val << last) & bmask) | (val & ~bmask);
116 * Overloaded for access to only one bit in value
118 template <class T, class B>
121 insertBits(T val, int bit, B bit_val)
123 return insertBits(val, bit, bit, bit_val);
127 * A convenience function to replace bits first to last of val with bit_val
130 template <class T, class B>
133 replaceBits(T& val, int first, int last, B bit_val)
135 val = insertBits(val, first, last, bit_val);
138 /** Overloaded function to allow to access only 1 bit*/
139 template <class T, class B>
142 replaceBits(T& val, int bit, B bit_val)
144 val = insertBits(val, bit, bit, bit_val);
147 * Returns the bit position of the MSB that is set in the input
151 findMsbSet(uint64_t val) {
155 if (bits(val, 63,32)) { msb += 32; val >>= 32; }
156 if (bits(val, 31,16)) { msb += 16; val >>= 16; }
157 if (bits(val, 15,8)) { msb += 8; val >>= 8; }
158 if (bits(val, 7,4)) { msb += 4; val >>= 4; }
159 if (bits(val, 3,2)) { msb += 2; val >>= 2; }
160 if (bits(val, 1,1)) { msb += 1; }
165 * Returns the bit position of the LSB that is set in the input
168 findLsbSet(uint64_t val) {
171 return sizeof(val) * 8;
172 if (!bits(val, 31,0)) { lsb += 32; val >>= 32; }
173 if (!bits(val, 15,0)) { lsb += 16; val >>= 16; }
174 if (!bits(val, 7,0)) { lsb += 8; val >>= 8; }
175 if (!bits(val, 3,0)) { lsb += 4; val >>= 4; }
176 if (!bits(val, 1,0)) { lsb += 2; val >>= 2; }
177 if (!bits(val, 0,0)) { lsb += 1; }
181 #endif // __BASE_BITFIELD_HH__