2 * Copyright (c) 2003-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 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. 15 * 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. 27 * 28 * Authors: Steve Reinhardt 29 * Nathan Binkert 30 */ 31 32#ifndef __BASE_BITFIELD_HH__ 33#define __BASE_BITFIELD_HH__ 34 35#include <inttypes.h> 36 37/** 38 * Generate a 64-bit mask of 'nbits' 1s, right justified. 39 */ 40inline uint64_t 41mask(int nbits) 42{ 43 return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1; 44} 45 46 47 48/** 49 * Extract the bitfield from position 'first' to 'last' (inclusive) 50 * from 'val' and right justify it. MSB is numbered 63, LSB is 0. 51 */ 52template <class T> 53inline 54T 55bits(T val, int first, int last) 56{ 57 int nbits = first - last + 1; 58 return (val >> last) & mask(nbits); 59} 60 61/** 62 * Extract the bit from this position from 'val' and right justify it. 63 */ 64template <class T> 65inline 66T 67bits(T val, int bit) 68{ 69 return bits(val, bit, bit); 70} 71 72/** 73 * Mask off the given bits in place like bits() but without shifting. 74 * msb = 63, lsb = 0 75 */ 76template <class T> 77inline 78T 79mbits(T val, int first, int last) 80{ 81 return val & (mask(first+1) & ~mask(last)); 82} 83 84inline uint64_t 85mask(int first, int last) 86{ 87 return mbits((uint64_t)-1LL, first, last); 88} 89 90/** 91 * Sign-extend an N-bit value to 64 bits. 92 */ 93template <int N> 94inline 95uint64_t 96sext(uint64_t val) 97{ 98 int sign_bit = bits(val, N-1, N-1); 99 return sign_bit ? (val | ~mask(N)) : val; 100} 101 102/** 103 * Return val with bits first to last set to bit_val 104 */ 105template <class T, class B> 106inline 107T 108insertBits(T val, int first, int last, B bit_val) 109{ 110 T t_bit_val = bit_val; 111 T bmask = mask(first - last + 1) << last; 112 return ((t_bit_val << last) & bmask) | (val & ~bmask); 113} 114 115/** 116 * Overloaded for access to only one bit in value 117 */ 118template <class T, class B> 119inline 120T 121insertBits(T val, int bit, B bit_val) 122{ 123 return insertBits(val, bit, bit, bit_val); 124} 125 126/** 127 * A convenience function to replace bits first to last of val with bit_val 128 * in place. 129 */ 130template <class T, class B> 131inline 132void 133replaceBits(T& val, int first, int last, B bit_val) 134{ 135 val = insertBits(val, first, last, bit_val); 136} 137 138/** Overloaded function to allow to access only 1 bit*/ 139template <class T, class B> 140inline 141void 142replaceBits(T& val, int bit, B bit_val) 143{ 144 val = insertBits(val, bit, bit, bit_val); 145} 146/** 147 * Returns the bit position of the MSB that is set in the input 148 */ 149inline 150int 151findMsbSet(uint64_t val) { 152 int msb = 0; 153 if (!val) 154 return 0; 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; } 161 return msb; 162} 163 164/** 165 * Returns the bit position of the LSB that is set in the input 166 */ 167inline int 168findLsbSet(uint64_t val) { 169 int lsb = 0; 170 if (!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; } 178 return lsb; 179} 180 181/** 182 * Checks if a number is a power of two, or zero. 183 */ 184template <class T> 185inline bool 186isPow2(T v) { 187 return (v & (v - 1)) == (T)0; 188} 189 190/** 191 * Returns the number of set ones in the provided value. 192 * PD algorithm from 193 * http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel 194 */ 195inline int 196popCount(uint64_t val) { 197#ifndef __has_builtin 198 #define __has_builtin(foo) 0 199#endif 200#if defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl)) 201 return __builtin_popcountl(val); 202#else 203 const uint64_t m1 = 0x5555555555555555; // ..010101b 204 const uint64_t m2 = 0x3333333333333333; // ..110011b 205 const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; // ..001111b 206 const uint64_t sum = 0x0101010101010101; 207 208 val -= (val >> 1) & m1; // 2 bits count -> 2 bits 209 val = (val & m2) + ((val >> 2) & m2); // 4 bits count -> 4 bits 210 val = (val + (val >> 4)) & m4; // 8 bits count -> 8 bits 211 return (val * sum) >> 56; // horizontal sum 212#endif // defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl)) 213}
| 14 * Copyright (c) 2003-2005 The Regents of The University of Michigan 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions are 19 * met: redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer; 21 * redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution; 24 * neither the name of the copyright holders nor the names of its 25 * contributors may be used to endorse or promote products derived from 26 * this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 * 40 * Authors: Steve Reinhardt 41 * Nathan Binkert 42 */ 43 44#ifndef __BASE_BITFIELD_HH__ 45#define __BASE_BITFIELD_HH__ 46 47#include <inttypes.h> 48 49/** 50 * Generate a 64-bit mask of 'nbits' 1s, right justified. 51 */ 52inline uint64_t 53mask(int nbits) 54{ 55 return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1; 56} 57 58 59 60/** 61 * Extract the bitfield from position 'first' to 'last' (inclusive) 62 * from 'val' and right justify it. MSB is numbered 63, LSB is 0. 63 */ 64template <class T> 65inline 66T 67bits(T val, int first, int last) 68{ 69 int nbits = first - last + 1; 70 return (val >> last) & mask(nbits); 71} 72 73/** 74 * Extract the bit from this position from 'val' and right justify it. 75 */ 76template <class T> 77inline 78T 79bits(T val, int bit) 80{ 81 return bits(val, bit, bit); 82} 83 84/** 85 * Mask off the given bits in place like bits() but without shifting. 86 * msb = 63, lsb = 0 87 */ 88template <class T> 89inline 90T 91mbits(T val, int first, int last) 92{ 93 return val & (mask(first+1) & ~mask(last)); 94} 95 96inline uint64_t 97mask(int first, int last) 98{ 99 return mbits((uint64_t)-1LL, first, last); 100} 101 102/** 103 * Sign-extend an N-bit value to 64 bits. 104 */ 105template <int N> 106inline 107uint64_t 108sext(uint64_t val) 109{ 110 int sign_bit = bits(val, N-1, N-1); 111 return sign_bit ? (val | ~mask(N)) : val; 112} 113 114/** 115 * Return val with bits first to last set to bit_val 116 */ 117template <class T, class B> 118inline 119T 120insertBits(T val, int first, int last, B bit_val) 121{ 122 T t_bit_val = bit_val; 123 T bmask = mask(first - last + 1) << last; 124 return ((t_bit_val << last) & bmask) | (val & ~bmask); 125} 126 127/** 128 * Overloaded for access to only one bit in value 129 */ 130template <class T, class B> 131inline 132T 133insertBits(T val, int bit, B bit_val) 134{ 135 return insertBits(val, bit, bit, bit_val); 136} 137 138/** 139 * A convenience function to replace bits first to last of val with bit_val 140 * in place. 141 */ 142template <class T, class B> 143inline 144void 145replaceBits(T& val, int first, int last, B bit_val) 146{ 147 val = insertBits(val, first, last, bit_val); 148} 149 150/** Overloaded function to allow to access only 1 bit*/ 151template <class T, class B> 152inline 153void 154replaceBits(T& val, int bit, B bit_val) 155{ 156 val = insertBits(val, bit, bit, bit_val); 157} 158/** 159 * Returns the bit position of the MSB that is set in the input 160 */ 161inline 162int 163findMsbSet(uint64_t val) { 164 int msb = 0; 165 if (!val) 166 return 0; 167 if (bits(val, 63,32)) { msb += 32; val >>= 32; } 168 if (bits(val, 31,16)) { msb += 16; val >>= 16; } 169 if (bits(val, 15,8)) { msb += 8; val >>= 8; } 170 if (bits(val, 7,4)) { msb += 4; val >>= 4; } 171 if (bits(val, 3,2)) { msb += 2; val >>= 2; } 172 if (bits(val, 1,1)) { msb += 1; } 173 return msb; 174} 175 176/** 177 * Returns the bit position of the LSB that is set in the input 178 */ 179inline int 180findLsbSet(uint64_t val) { 181 int lsb = 0; 182 if (!val) 183 return sizeof(val) * 8; 184 if (!bits(val, 31,0)) { lsb += 32; val >>= 32; } 185 if (!bits(val, 15,0)) { lsb += 16; val >>= 16; } 186 if (!bits(val, 7,0)) { lsb += 8; val >>= 8; } 187 if (!bits(val, 3,0)) { lsb += 4; val >>= 4; } 188 if (!bits(val, 1,0)) { lsb += 2; val >>= 2; } 189 if (!bits(val, 0,0)) { lsb += 1; } 190 return lsb; 191} 192 193/** 194 * Checks if a number is a power of two, or zero. 195 */ 196template <class T> 197inline bool 198isPow2(T v) { 199 return (v & (v - 1)) == (T)0; 200} 201 202/** 203 * Returns the number of set ones in the provided value. 204 * PD algorithm from 205 * http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel 206 */ 207inline int 208popCount(uint64_t val) { 209#ifndef __has_builtin 210 #define __has_builtin(foo) 0 211#endif 212#if defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl)) 213 return __builtin_popcountl(val); 214#else 215 const uint64_t m1 = 0x5555555555555555; // ..010101b 216 const uint64_t m2 = 0x3333333333333333; // ..110011b 217 const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; // ..001111b 218 const uint64_t sum = 0x0101010101010101; 219 220 val -= (val >> 1) & m1; // 2 bits count -> 2 bits 221 val = (val & m2) + ((val >> 2) & m2); // 4 bits count -> 4 bits 222 val = (val + (val >> 4)) & m4; // 8 bits count -> 8 bits 223 return (val * sum) >> 56; // horizontal sum 224#endif // defined(__GNUC__) || (defined(__clang__) && __has_builtin(__builtin_popcountl)) 225}
|