bitfield.hh revision 2665
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 351112SN/A#include "sim/host.hh" 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 721112SN/A#endif // __BASE_BITFIELD_HH__ 73