Bits.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
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
00018
00019 inline unsigned maxValueOfNBits(unsigned N) {
00020 if (N==0)
00021 return 0;
00022 return ((unsigned) -1) >> (32 - N);
00023 }
00024
00025
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
00044
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
00062
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
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
00088
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 }
00101
00102 #endif