bits.hpp

Go to the documentation of this file.
00001 /* Copyright 1994, 1995 LongView Technologies L.L.C. $Revision: 1.7 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 # define AllBits        ~0
00025 # define NoBits         0
00026 # define OneBit         1
00027 
00028 # define addBits(x, m)  ((x) | (m))
00029 # define setBits(x, m)  ((x) |= (m))
00030 # define subBits(x, m)  ((x) & ~(m))
00031 # define clearBits(x, m) ((x) &= ~(m))
00032 # define maskBits(x, m) ((x) & (m))
00033 # define anySet(x, m)   (maskBits((x), (m)) != NoBits)
00034 # define nthBit(n)      (OneBit << (n))
00035 # define addNth(x, n)   addBits((x), nthBit(n))
00036 # define setNth(x, n)   setBits((x), nthBit(n))
00037 # define clearNth(x, n) clearBits((x), nthBit(n))
00038 # define subNth(x, n)   subBits((x), nthBit(n))
00039 # define isSet(x, n)    anySet((x), nthBit(n))
00040 # define nthMask(n)     (n == 32 ? AllBits : (nthBit(n) - OneBit))
00041 # define lowerBits(x, n) maskBits((x), nthMask(n))
00042 
00043 # define roundMask(x, m) (((x) + (m)) & ~(m))
00044 # define roundBits(x, n) roundMask((x), nthMask(n))
00045 # define roundTo(x, v)  roundMask((x), (v) - OneBit)
00046 
00047 inline int arithmetic_shift_right(int value, int shift) { return value >> shift; }
00048 inline int logic_shift_right(int value, int shift)      { return value << shift; }
00049 
00050 
00051 inline int get_unsigned_bitfield(int value, int start_bit_no, int field_length) {
00052   return (int) lowerBits((unsigned int) value >> start_bit_no, field_length);
00053 }
00054 
00055 inline int get_signed_bitfield(int value, int start_bit_no, int field_length) {
00056   int result = get_unsigned_bitfield(value, start_bit_no, field_length);
00057   return isSet(result, start_bit_no + field_length -1) 
00058          ? addBits(result, ~nthMask(field_length))
00059          : result;
00060 }
00061 
00062 inline int set_unsigned_bitfield(int value, int start_bit_no, int field_length, unsigned int new_field_value) {
00063   assert(addBits(new_field_value, ~nthMask(field_length)) == 0, "range check");
00064   int mask = nthMask(field_length) << start_bit_no;
00065   return addBits(subBits(value, mask), maskBits(new_field_value << start_bit_no, mask));
00066 }

Generated on Mon Oct 9 13:37:03 2006 for Strongtalk VM by  doxygen 1.4.7