bitfield.hh revision 4070:74449a198a44
13388Sgblack@eecs.umich.edu/*
23388Sgblack@eecs.umich.edu * Copyright (c) 2003-2005 The Regents of The University of Michigan
33388Sgblack@eecs.umich.edu * All rights reserved.
43388Sgblack@eecs.umich.edu *
53388Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
63388Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
73388Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
83388Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
93388Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
103388Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
113388Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
123388Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
133388Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
143388Sgblack@eecs.umich.edu * this software without specific prior written permission.
153388Sgblack@eecs.umich.edu *
163388Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
173388Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183388Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193388Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
203388Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
213388Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223388Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233388Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243388Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253388Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
263388Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273388Sgblack@eecs.umich.edu *
283388Sgblack@eecs.umich.edu * Authors: Steve Reinhardt
293388Sgblack@eecs.umich.edu *          Nathan Binkert
303270SN/A */
313270SN/A
323270SN/A#ifndef __BASE_BITFIELD_HH__
333270SN/A#define __BASE_BITFIELD_HH__
343270SN/A
353270SN/A#include <inttypes.h>
363270SN/A
373270SN/A/**
383270SN/A * Generate a 64-bit mask of 'nbits' 1s, right justified.
393270SN/A */
403270SN/Ainline uint64_t
413270SN/Amask(int nbits)
423270SN/A{
433270SN/A    return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
443388Sgblack@eecs.umich.edu}
453388Sgblack@eecs.umich.edu
463270SN/A
473270SN/A
483270SN/A/**
493270SN/A * Extract the bitfield from position 'first' to 'last' (inclusive)
503270SN/A * from 'val' and right justify it.  MSB is numbered 63, LSB is 0.
513270SN/A */
523270SN/Atemplate <class T>
533270SN/Ainline
543388Sgblack@eecs.umich.eduT
553388Sgblack@eecs.umich.edubits(T val, int first, int last)
563270SN/A{
573270SN/A    int nbits = first - last + 1;
583270SN/A    return (val >> last) & mask(nbits);
593270SN/A}
603270SN/A
613270SN/A/**
623270SN/A * Mask off the given bits in place like bits() but without shifting.
633270SN/A * msb = 63, lsb = 0
643270SN/A */
653270SN/Atemplate <class T>
663270SN/Ainline
673270SN/AT
683270SN/Ambits(T val, int first, int last)
693270SN/A{
703270SN/A    return val & (mask(first+1) & ~mask(last));
713270SN/A}
723270SN/A
733270SN/Ainline uint64_t
743270SN/Amask(int first, int last)
753270SN/A{
763270SN/A    return mbits((uint64_t)-1LL, first, last);
773270SN/A}
783270SN/A
793270SN/A/**
803270SN/A * Sign-extend an N-bit value to 64 bits.
813270SN/A */
823270SN/Atemplate <int N>
833270SN/Ainline
843270SN/Aint64_t
853270SN/Asext(uint64_t val)
863270SN/A{
873270SN/A    int sign_bit = bits(val, N-1, N-1);
883270SN/A    return sign_bit ? (val | ~mask(N)) : val;
893270SN/A}
903270SN/A
913270SN/A/**
923270SN/A * Return val with bits first to last set to bit_val
933270SN/A */
943270SN/Atemplate <class T, class B>
953280SN/Ainline
963270SN/AT
973270SN/AinsertBits(T val, int first, int last, B bit_val)
983270SN/A{
993270SN/A    T bmask = mask(first - last + 1) << last;
1003270SN/A    return ((bit_val << last) & bmask) | (val & ~bmask);
1013270SN/A}
1023270SN/A
1033270SN/A/**
1043270SN/A * A convenience function to replace bits first to last of val with bit_val
1053270SN/A * in place.
1063270SN/A */
1073270SN/Atemplate <class T, class B>
1083270SN/Ainline
1093270SN/Avoid
1103270SN/AreplaceBits(T& val, int first, int last, B bit_val)
1113270SN/A{
1123270SN/A    val = insertBits(val, first, last, bit_val);
1133270SN/A}
1143270SN/A
1153270SN/A#endif // __BASE_BITFIELD_HH__
1163270SN/A