bitfield.hh revision 3814:33bd4ec9d66a
1/*
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 * Extract the bitfield from position 'first' to 'last' (inclusive)
49 * from 'val' and right justify it.  MSB is numbered 63, LSB is 0.
50 */
51template <class T>
52inline
53T
54bits(T val, int first, int last)
55{
56    int nbits = first - last + 1;
57    return (val >> last) & mask(nbits);
58}
59
60/**
61 * Mask off the given bits in place like bits() but without shifting.
62 * msb = 63, lsb = 0
63 */
64template <class T>
65inline
66T
67mbits(T val, int first, int last)
68{
69    return val & (mask(first+1) & ~mask(last));
70}
71
72/**
73 * Sign-extend an N-bit value to 64 bits.
74 */
75template <int N>
76inline
77int64_t
78sext(uint64_t val)
79{
80    int sign_bit = bits(val, N-1, N-1);
81    return sign_bit ? (val | ~mask(N)) : val;
82}
83
84/**
85 * Return val with bits first to last set to bit_val
86 */
87template <class T, class B>
88inline
89T
90insertBits(T val, int first, int last, B bit_val)
91{
92    T bmask = mask(first - last + 1) << last;
93    return ((bit_val << last) & bmask) | (val & ~bmask);
94}
95
96/**
97 * A convenience function to replace bits first to last of val with bit_val
98 * in place.
99 */
100template <class T, class B>
101inline
102void
103replaceBits(T& val, int first, int last, B bit_val)
104{
105    val = insertBits(val, first, last, bit_val);
106}
107
108#endif // __BASE_BITFIELD_HH__
109