bitfield.hh revision 3814:33bd4ec9d66a
111832SChristian.Menard@tu-dresden.de/* 211832SChristian.Menard@tu-dresden.de * Copyright (c) 2003-2005 The Regents of The University of Michigan 311832SChristian.Menard@tu-dresden.de * All rights reserved. 411832SChristian.Menard@tu-dresden.de * 511832SChristian.Menard@tu-dresden.de * Redistribution and use in source and binary forms, with or without 610993Sjungma@eit.uni-kl.de * modification, are permitted provided that the following conditions are 710993Sjungma@eit.uni-kl.de * met: redistributions of source code must retain the above copyright 811832SChristian.Menard@tu-dresden.de * notice, this list of conditions and the following disclaimer; 911832SChristian.Menard@tu-dresden.de * redistributions in binary form must reproduce the above copyright 1011832SChristian.Menard@tu-dresden.de * notice, this list of conditions and the following disclaimer in the 1111832SChristian.Menard@tu-dresden.de * documentation and/or other materials provided with the distribution; 1211832SChristian.Menard@tu-dresden.de * neither the name of the copyright holders nor the names of its 1311832SChristian.Menard@tu-dresden.de * contributors may be used to endorse or promote products derived from 1411832SChristian.Menard@tu-dresden.de * this software without specific prior written permission. 1511832SChristian.Menard@tu-dresden.de * 1611832SChristian.Menard@tu-dresden.de * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1711832SChristian.Menard@tu-dresden.de * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1811832SChristian.Menard@tu-dresden.de * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1911832SChristian.Menard@tu-dresden.de * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2011832SChristian.Menard@tu-dresden.de * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2111832SChristian.Menard@tu-dresden.de * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2211832SChristian.Menard@tu-dresden.de * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2311832SChristian.Menard@tu-dresden.de * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2411832SChristian.Menard@tu-dresden.de * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2511832SChristian.Menard@tu-dresden.de * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2611832SChristian.Menard@tu-dresden.de * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2711832SChristian.Menard@tu-dresden.de * 2811832SChristian.Menard@tu-dresden.de * Authors: Steve Reinhardt 2911832SChristian.Menard@tu-dresden.de * Nathan Binkert 3011832SChristian.Menard@tu-dresden.de */ 3111832SChristian.Menard@tu-dresden.de 3211971Szulian@eit.uni-kl.de#ifndef __BASE_BITFIELD_HH__ 3311832SChristian.Menard@tu-dresden.de#define __BASE_BITFIELD_HH__ 3411832SChristian.Menard@tu-dresden.de 3511971Szulian@eit.uni-kl.de#include <inttypes.h> 3611971Szulian@eit.uni-kl.de 3711832SChristian.Menard@tu-dresden.de/** 3811832SChristian.Menard@tu-dresden.de * Generate a 64-bit mask of 'nbits' 1s, right justified. 3911832SChristian.Menard@tu-dresden.de */ 4011832SChristian.Menard@tu-dresden.deinline uint64_t 4111832SChristian.Menard@tu-dresden.demask(int nbits) 4211832SChristian.Menard@tu-dresden.de{ 4311832SChristian.Menard@tu-dresden.de return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1; 4411832SChristian.Menard@tu-dresden.de} 4511832SChristian.Menard@tu-dresden.de 4611832SChristian.Menard@tu-dresden.de 4711832SChristian.Menard@tu-dresden.de/** 4811832SChristian.Menard@tu-dresden.de * Extract the bitfield from position 'first' to 'last' (inclusive) 4911832SChristian.Menard@tu-dresden.de * from 'val' and right justify it. MSB is numbered 63, LSB is 0. 5011832SChristian.Menard@tu-dresden.de */ 5111832SChristian.Menard@tu-dresden.detemplate <class T> 5211832SChristian.Menard@tu-dresden.deinline 5311832SChristian.Menard@tu-dresden.deT 5411832SChristian.Menard@tu-dresden.debits(T val, int first, int last) 5511832SChristian.Menard@tu-dresden.de{ 5611832SChristian.Menard@tu-dresden.de int nbits = first - last + 1; 5711832SChristian.Menard@tu-dresden.de return (val >> last) & mask(nbits); 5811832SChristian.Menard@tu-dresden.de} 5911832SChristian.Menard@tu-dresden.de 6011832SChristian.Menard@tu-dresden.de/** 6111832SChristian.Menard@tu-dresden.de * Mask off the given bits in place like bits() but without shifting. 6211832SChristian.Menard@tu-dresden.de * msb = 63, lsb = 0 6311832SChristian.Menard@tu-dresden.de */ 6411832SChristian.Menard@tu-dresden.detemplate <class T> 6511832SChristian.Menard@tu-dresden.deinline 6611832SChristian.Menard@tu-dresden.deT 6711832SChristian.Menard@tu-dresden.dembits(T val, int first, int last) 6811832SChristian.Menard@tu-dresden.de{ 6911832SChristian.Menard@tu-dresden.de return val & (mask(first+1) & ~mask(last)); 7011832SChristian.Menard@tu-dresden.de} 7111832SChristian.Menard@tu-dresden.de 7211832SChristian.Menard@tu-dresden.de/** 7311832SChristian.Menard@tu-dresden.de * Sign-extend an N-bit value to 64 bits. 7411832SChristian.Menard@tu-dresden.de */ 7510993Sjungma@eit.uni-kl.detemplate <int N> 7610993Sjungma@eit.uni-kl.deinline 7710993Sjungma@eit.uni-kl.deint64_t 7810993Sjungma@eit.uni-kl.desext(uint64_t val) 7910993Sjungma@eit.uni-kl.de{ 8010993Sjungma@eit.uni-kl.de int sign_bit = bits(val, N-1, N-1); 8110993Sjungma@eit.uni-kl.de return sign_bit ? (val | ~mask(N)) : val; 8210993Sjungma@eit.uni-kl.de} 8310993Sjungma@eit.uni-kl.de 8411832SChristian.Menard@tu-dresden.de/** 8511832SChristian.Menard@tu-dresden.de * Return val with bits first to last set to bit_val 8610993Sjungma@eit.uni-kl.de */ 8710993Sjungma@eit.uni-kl.detemplate <class T, class B> 8810993Sjungma@eit.uni-kl.deinline 8910993Sjungma@eit.uni-kl.deT 9010993Sjungma@eit.uni-kl.deinsertBits(T val, int first, int last, B bit_val) 9110993Sjungma@eit.uni-kl.de{ 9210993Sjungma@eit.uni-kl.de T bmask = mask(first - last + 1) << last; 9311832SChristian.Menard@tu-dresden.de return ((bit_val << last) & bmask) | (val & ~bmask); 9411832SChristian.Menard@tu-dresden.de} 9511099Sabdul.mutaal@gmail.com 9610993Sjungma@eit.uni-kl.de/** 9711791Sjungma@eit.uni-kl.de * A convenience function to replace bits first to last of val with bit_val 9811832SChristian.Menard@tu-dresden.de * in place. 9911832SChristian.Menard@tu-dresden.de */ 10011791Sjungma@eit.uni-kl.detemplate <class T, class B> 10110993Sjungma@eit.uni-kl.deinline 10210993Sjungma@eit.uni-kl.devoid 10310993Sjungma@eit.uni-kl.dereplaceBits(T& val, int first, int last, B bit_val) 10411791Sjungma@eit.uni-kl.de{ 10511791Sjungma@eit.uni-kl.de val = insertBits(val, first, last, bit_val); 10611791Sjungma@eit.uni-kl.de} 10711832SChristian.Menard@tu-dresden.de 10811832SChristian.Menard@tu-dresden.de#endif // __BASE_BITFIELD_HH__ 10911832SChristian.Menard@tu-dresden.de