bitfield.hh revision 3422
12SN/A/* 21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 292665Ssaidi@eecs.umich.edu * Nathan Binkert 302SN/A */ 312SN/A 321112SN/A#ifndef __BASE_BITFIELD_HH__ 331112SN/A#define __BASE_BITFIELD_HH__ 342SN/A 353386Sgblack@eecs.umich.edu#include <inttypes.h> 362SN/A 372SN/A/** 382SN/A * Generate a 64-bit mask of 'nbits' 1s, right justified. 392SN/A */ 402SN/Ainline uint64_t 412SN/Amask(int nbits) 422SN/A{ 432SN/A return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1; 442SN/A} 452SN/A 462SN/A 472SN/A/** 482SN/A * Extract the bitfield from position 'first' to 'last' (inclusive) 492SN/A * from 'val' and right justify it. MSB is numbered 63, LSB is 0. 502SN/A */ 512SN/Atemplate <class T> 522SN/Ainline 532SN/AT 542SN/Abits(T val, int first, int last) 552SN/A{ 562SN/A int nbits = first - last + 1; 572SN/A return (val >> last) & mask(nbits); 582SN/A} 592SN/A 602SN/A/** 612SN/A * Sign-extend an N-bit value to 64 bits. 622SN/A */ 632SN/Atemplate <int N> 642SN/Ainline 652SN/Aint64_t 662SN/Asext(uint64_t val) 672SN/A{ 682SN/A int sign_bit = bits(val, N-1, N-1); 692SN/A return sign_bit ? (val | ~mask(N)) : val; 702SN/A} 712SN/A 723422Sgblack@eecs.umich.edu/** 733422Sgblack@eecs.umich.edu * Return val with bits first to last set to bit_val 743422Sgblack@eecs.umich.edu */ 753422Sgblack@eecs.umich.edutemplate <class T, class B> 763422Sgblack@eecs.umich.eduinline 773422Sgblack@eecs.umich.eduT 783422Sgblack@eecs.umich.eduinsertBits(T val, int first, int last, B bit_val) 793422Sgblack@eecs.umich.edu{ 803422Sgblack@eecs.umich.edu T bmask = mask(first - last + 1) << last; 813422Sgblack@eecs.umich.edu return ((bit_val << last) & bmask) | (val & ~bmask); 823422Sgblack@eecs.umich.edu} 833422Sgblack@eecs.umich.edu 843422Sgblack@eecs.umich.edu/** 853422Sgblack@eecs.umich.edu * A convenience function to replace bits first to last of val with bit_val 863422Sgblack@eecs.umich.edu * in place. 873422Sgblack@eecs.umich.edu */ 883422Sgblack@eecs.umich.edutemplate <class T, class B> 893422Sgblack@eecs.umich.eduinline 903422Sgblack@eecs.umich.eduvoid 913422Sgblack@eecs.umich.edureplaceBits(T& val, int first, int last, B bit_val) 923422Sgblack@eecs.umich.edu{ 933422Sgblack@eecs.umich.edu val = insertBits(val, first, last, bit_val); 943422Sgblack@eecs.umich.edu} 953422Sgblack@eecs.umich.edu 961112SN/A#endif // __BASE_BITFIELD_HH__ 97