Bits.h

Go to the documentation of this file.
00001 //===-- Bits.h --------------------------------------------------*- C++ -*-===//
00002 //
00003 //                     The KLEE Symbolic Virtual Machine
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef KLEE_UTIL_BITS_H
00011 #define KLEE_UTIL_BITS_H
00012 
00013 #include "llvm/Support/DataTypes.h"
00014 
00015 namespace klee {
00016   namespace bits32 {
00017     // @pre(0 <= N <= 32)
00018     // @post(retval = max([truncateToNBits(i,N) for i in naturals()]))
00019     inline unsigned maxValueOfNBits(unsigned N) {
00020       if (N==0)
00021         return 0;
00022       return ((unsigned) -1) >> (32 - N);
00023     }
00024 
00025     // @pre(0 < N <= 32)
00026     inline unsigned truncateToNBits(unsigned x, unsigned N) {
00027       return x&(((unsigned) -1) >> (32 - N));
00028     }
00029 
00030     inline unsigned withoutRightmostBit(unsigned x) {
00031       return x&(x-1);
00032     }
00033 
00034     inline unsigned isolateRightmostBit(unsigned x) {
00035       return x&-x;
00036     }
00037 
00038     inline unsigned isPowerOfTwo(unsigned x) {
00039       if (x==0) return 0;
00040       return !(x&(x-1));
00041     }
00042 
00043     // @pre(withoutRightmostBit(x) == 0)
00044     // @post((1 << retval) == x)
00045     inline unsigned indexOfSingleBit(unsigned x) {
00046       unsigned res = 0;
00047       if (x&0xFFFF0000) res += 16;
00048       if (x&0xFF00FF00) res += 8;
00049       if (x&0xF0F0F0F0) res += 4;
00050       if (x&0xCCCCCCCC) res += 2;
00051       if (x&0xAAAAAAAA) res += 1;
00052       return res;
00053     } 
00054 
00055     inline unsigned indexOfRightmostBit(unsigned x) {
00056       return indexOfSingleBit(isolateRightmostBit(x));
00057     }
00058   }
00059 
00060   namespace bits64 {
00061     // @pre(0 <= N <= 32)
00062     // @post(retval = max([truncateToNBits(i,N) for i in naturals()]))
00063     inline uint64_t maxValueOfNBits(unsigned N) {
00064       if (N==0)
00065         return 0;
00066       return ((uint64_t) (int64_t) -1) >> (64 - N);
00067     }
00068     
00069     // @pre(0 < N <= 64)
00070     inline uint64_t truncateToNBits(uint64_t x, unsigned N) {
00071       return x&(((uint64_t) (int64_t) -1) >> (64 - N));
00072     }
00073 
00074     inline uint64_t withoutRightmostBit(uint64_t x) {
00075       return x&(x-1);
00076     }
00077 
00078     inline uint64_t isolateRightmostBit(uint64_t x) {
00079       return x&-x;
00080     }
00081 
00082     inline uint64_t isPowerOfTwo(uint64_t x) {
00083       if (x==0) return 0;
00084       return !(x&(x-1));
00085     }
00086 
00087     // @pre((x&(x-1)) == 0)
00088     // @post((1 << retval) == x)
00089     inline unsigned indexOfSingleBit(uint64_t x) {
00090       unsigned res = bits32::indexOfSingleBit((unsigned) (x | (x>>32)));
00091       if (x&((uint64_t) 0xFFFFFFFF << 32))
00092           res += 32;
00093       return res;
00094     } 
00095 
00096     inline uint64_t indexOfRightmostBit(uint64_t x) {
00097       return indexOfSingleBit(isolateRightmostBit(x));
00098     }
00099   }
00100 } // End klee namespace
00101 
00102 #endif

Generated on Fri Jun 5 03:31:31 2009 for klee by  doxygen 1.5.8