File: bitsOperationUtil.h

package info (click to toggle)
perm 0.4.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 976 kB
  • sloc: cpp: 13,499; makefile: 98; sh: 12
file content (170 lines) | stat: -rw-r--r-- 7,028 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#pragma once
#ifndef BITS_OPERATION_UTIL_H
#define BITS_OPERATION_UTIL_H
#include "stdafx.h"
/*
 * Most bitwise operation are adopted from Sean Eron Anderson at Stanford
 */

/*
#ifdef BIT32
	#define WORD_SIZE unsigned int
	const unsigned int wordSize = 32;
#else */
#define WORD_SIZE unsigned long long
const unsigned int wordSize = 64;
// #endif

#ifdef __MSVC
#endif
#ifdef __GNUC__
#endif

// unsigned int reverse32bits(unsigned int word);
// unsigned long long reverse64bits(unsigned long long word);
// void printBinaryString(unsigned long long word, unsigned int length);

static const unsigned char BitReverseTable256[] = {
    0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
    0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
    0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94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xBB, 0x7B, 0xFB,
    0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
    0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
};

inline unsigned int reverse32bits(unsigned int word)
{
    unsigned int c;
    unsigned char * p = (unsigned char *) & word;
    unsigned char * q = (unsigned char *) & c;
    q[3] = BitReverseTable256[p[0]];
    q[2] = BitReverseTable256[p[1]];
    q[1] = BitReverseTable256[p[2]];
    q[0] = BitReverseTable256[p[3]];
    return c;
}

inline unsigned long long reverse64bits(unsigned long long word)
{
    //get the lower 32bits first
    unsigned long long returnValue =
        (unsigned long long) reverse32bits(0xffffffff & ((unsigned int) word));
    returnValue <<= 32; //shift to upper bits
    word >>= 32; //Shift upper bits to lower bits.
    returnValue += (unsigned long long) reverse32bits(0xffffffff & ((unsigned int) word));
    return (returnValue);
}

static const unsigned short MortonTable256[] = {
    0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015,
    0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055,
    0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115,
    0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155,
    0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415,
    0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455,
    0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515,
    0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555,
    0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015,
    0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055,
    0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115,
    0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155,
    0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415,
    0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455,
    0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515,
    0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555,
    0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015,
    0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055,
    0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115,
    0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155,
    0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415,
    0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455,
    0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515,
    0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555,
    0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015,
    0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055,
    0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115,
    0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155,
    0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415,
    0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455,
    0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515,
    0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555
};

// 11 bitwise operations
inline unsigned int InterleaveBits(unsigned short x, unsigned short y)
{
    unsigned int z = MortonTable256[y >> 8]   << 17 |
                     MortonTable256[x >> 8]   << 16 |
                     MortonTable256[y & 0xFF] <<  1 |
                     MortonTable256[x & 0xFF];
    return(z);
}

inline unsigned long long longlongShiftLeft(unsigned long long word, unsigned int shiftBits)
{
    const unsigned int BITS_PER_INT = 32;
    // Not all the compiler can support variable >> 32 dirrectly
    unsigned long long returnValue = word << (shiftBits % BITS_PER_INT);
    if (shiftBits >= BITS_PER_INT) {
        returnValue <<= 16;
        returnValue <<= 16;
    }
    return(returnValue);
}

inline unsigned long long longlongShiftRight(unsigned long long word, unsigned int shiftBits)
{
    const unsigned int BITS_PER_INT = 32;
    unsigned long long returnValue = word >> (shiftBits % BITS_PER_INT); // shift (word mod 32);
    if (shiftBits >= BITS_PER_INT) {
        returnValue >>= 16;
        returnValue >>= 16;
    }
    return(returnValue);
}

inline void clearLastBit(unsigned long long& bitsStr)
{
    bitsStr >>= 0x01;
    bitsStr <<= 0x01;
}

inline bool isKthBitSet(unsigned long long data, unsigned int k)
{
    unsigned long long flagMask = 0x01;
    if (k) {
        flagMask = longlongShiftLeft(flagMask, k - 1);
    }
    if (data & flagMask) {
        return(true);
    } else {
        return(false);
    }
}

inline void setKthBit(unsigned long long& bitsStr, unsigned int k, bool bit)
{
    unsigned long long flagMask = 0x01;
    if (k) {
        flagMask = longlongShiftLeft(flagMask, k - 1);
    }
    if (bit) {
        bitsStr |= flagMask;
    } else {
        bitsStr &= (!flagMask);
    }
}
#endif /* BITS_OPERATION_UTIL_H */