#### /Data/BitUtil.hs

http://github.com/exclipy/pdata
Haskell | 291 lines | 281 code | 8 blank | 2 comment | 1 complexity | 1af91454ea4e17623ae26669db70734d MD5 | raw file
```  1module Data.BitUtil where
2
3import Data.Bits
4import Data.Word
5import Data.Int
6import Data.List (foldl')
7
8-- Bit operations
9
10-- Given a bitmap and a subhash, this function returns the index into the list
11fromBitmap :: (Integral b, Integral c) => Int32 -> b -> c
12fromBitmap bitmap subHash =
13    let mask = fromIntegral (pred (toBitmap subHash :: Word32)) :: Int32
14        in bitCount32 \$ bitmap .&. mask
15
16toBitmap :: (Bits t, Num t, Integral a) => a -> t
17toBitmap subHash = 1 `shiftL` fromIntegral subHash
18
19bitmapToIndices :: (Bits a, Num a, Num b, Eq b) => a -> [b]
20bitmapToIndices bitmap = loop 0 bitmap
21    where loop _ 0  = []
22          loop 32 _ = []
23          loop ix bitmap | bitmap .&. 1 == 0 = loop (ix+1) (bitmap `shiftR` 1)
24                         | otherwise         = ix:(loop (ix+1) (bitmap `shiftR` 1))
25
26indicesToBitmap :: (Bits a, Num a) => [Int] -> a
27indicesToBitmap = foldl' (\bm ix -> bm .|. (1 `shiftL` ix)) 0
28
29bitCount32 :: (Integral a) => Int32 -> a
30bitCount32 x = bitCount8 ((x `shiftR` 24) .&. 0xff) +
31               bitCount8 ((x `shiftR` 16) .&. 0xff) +
32               bitCount8 ((x `shiftR` 8) .&. 0xff) +
33               bitCount8 (x .&. 0xff)
34
35bitCount8 :: (Bits a, Num a, Integral b) => a -> b
36bitCount8 0 = 0
37bitCount8 1 = 1
38bitCount8 2 = 1
39bitCount8 3 = 2
40bitCount8 4 = 1
41bitCount8 5 = 2
42bitCount8 6 = 2
43bitCount8 7 = 3
44bitCount8 8 = 1
45bitCount8 9 = 2
46bitCount8 10 = 2
47bitCount8 11 = 3
48bitCount8 12 = 2
49bitCount8 13 = 3
50bitCount8 14 = 3
51bitCount8 15 = 4
52bitCount8 16 = 1
53bitCount8 17 = 2
54bitCount8 18 = 2
55bitCount8 19 = 3
56bitCount8 20 = 2
57bitCount8 21 = 3
58bitCount8 22 = 3
59bitCount8 23 = 4
60bitCount8 24 = 2
61bitCount8 25 = 3
62bitCount8 26 = 3
63bitCount8 27 = 4
64bitCount8 28 = 3
65bitCount8 29 = 4
66bitCount8 30 = 4
67bitCount8 31 = 5
68bitCount8 32 = 1
69bitCount8 33 = 2
70bitCount8 34 = 2
71bitCount8 35 = 3
72bitCount8 36 = 2
73bitCount8 37 = 3
74bitCount8 38 = 3
75bitCount8 39 = 4
76bitCount8 40 = 2
77bitCount8 41 = 3
78bitCount8 42 = 3
79bitCount8 43 = 4
80bitCount8 44 = 3
81bitCount8 45 = 4
82bitCount8 46 = 4
83bitCount8 47 = 5
84bitCount8 48 = 2
85bitCount8 49 = 3
86bitCount8 50 = 3
87bitCount8 51 = 4
88bitCount8 52 = 3
89bitCount8 53 = 4
90bitCount8 54 = 4
91bitCount8 55 = 5
92bitCount8 56 = 3
93bitCount8 57 = 4
94bitCount8 58 = 4
95bitCount8 59 = 5
96bitCount8 60 = 4
97bitCount8 61 = 5
98bitCount8 62 = 5
99bitCount8 63 = 6
100bitCount8 64 = 1
101bitCount8 65 = 2
102bitCount8 66 = 2
103bitCount8 67 = 3
104bitCount8 68 = 2
105bitCount8 69 = 3
106bitCount8 70 = 3
107bitCount8 71 = 4
108bitCount8 72 = 2
109bitCount8 73 = 3
110bitCount8 74 = 3
111bitCount8 75 = 4
112bitCount8 76 = 3
113bitCount8 77 = 4
114bitCount8 78 = 4
115bitCount8 79 = 5
116bitCount8 80 = 2
117bitCount8 81 = 3
118bitCount8 82 = 3
119bitCount8 83 = 4
120bitCount8 84 = 3
121bitCount8 85 = 4
122bitCount8 86 = 4
123bitCount8 87 = 5
124bitCount8 88 = 3
125bitCount8 89 = 4
126bitCount8 90 = 4
127bitCount8 91 = 5
128bitCount8 92 = 4
129bitCount8 93 = 5
130bitCount8 94 = 5
131bitCount8 95 = 6
132bitCount8 96 = 2
133bitCount8 97 = 3
134bitCount8 98 = 3
135bitCount8 99 = 4
136bitCount8 100 = 3
137bitCount8 101 = 4
138bitCount8 102 = 4
139bitCount8 103 = 5
140bitCount8 104 = 3
141bitCount8 105 = 4
142bitCount8 106 = 4
143bitCount8 107 = 5
144bitCount8 108 = 4
145bitCount8 109 = 5
146bitCount8 110 = 5
147bitCount8 111 = 6
148bitCount8 112 = 3
149bitCount8 113 = 4
150bitCount8 114 = 4
151bitCount8 115 = 5
152bitCount8 116 = 4
153bitCount8 117 = 5
154bitCount8 118 = 5
155bitCount8 119 = 6
156bitCount8 120 = 4
157bitCount8 121 = 5
158bitCount8 122 = 5
159bitCount8 123 = 6
160bitCount8 124 = 5
161bitCount8 125 = 6
162bitCount8 126 = 6
163bitCount8 127 = 7
164bitCount8 128 = 1
165bitCount8 129 = 2
166bitCount8 130 = 2
167bitCount8 131 = 3
168bitCount8 132 = 2
169bitCount8 133 = 3
170bitCount8 134 = 3
171bitCount8 135 = 4
172bitCount8 136 = 2
173bitCount8 137 = 3
174bitCount8 138 = 3
175bitCount8 139 = 4
176bitCount8 140 = 3
177bitCount8 141 = 4
178bitCount8 142 = 4
179bitCount8 143 = 5
180bitCount8 144 = 2
181bitCount8 145 = 3
182bitCount8 146 = 3
183bitCount8 147 = 4
184bitCount8 148 = 3
185bitCount8 149 = 4
186bitCount8 150 = 4
187bitCount8 151 = 5
188bitCount8 152 = 3
189bitCount8 153 = 4
190bitCount8 154 = 4
191bitCount8 155 = 5
192bitCount8 156 = 4
193bitCount8 157 = 5
194bitCount8 158 = 5
195bitCount8 159 = 6
196bitCount8 160 = 2
197bitCount8 161 = 3
198bitCount8 162 = 3
199bitCount8 163 = 4
200bitCount8 164 = 3
201bitCount8 165 = 4
202bitCount8 166 = 4
203bitCount8 167 = 5
204bitCount8 168 = 3
205bitCount8 169 = 4
206bitCount8 170 = 4
207bitCount8 171 = 5
208bitCount8 172 = 4
209bitCount8 173 = 5
210bitCount8 174 = 5
211bitCount8 175 = 6
212bitCount8 176 = 3
213bitCount8 177 = 4
214bitCount8 178 = 4
215bitCount8 179 = 5
216bitCount8 180 = 4
217bitCount8 181 = 5
218bitCount8 182 = 5
219bitCount8 183 = 6
220bitCount8 184 = 4
221bitCount8 185 = 5
222bitCount8 186 = 5
223bitCount8 187 = 6
224bitCount8 188 = 5
225bitCount8 189 = 6
226bitCount8 190 = 6
227bitCount8 191 = 7
228bitCount8 192 = 2
229bitCount8 193 = 3
230bitCount8 194 = 3
231bitCount8 195 = 4
232bitCount8 196 = 3
233bitCount8 197 = 4
234bitCount8 198 = 4
235bitCount8 199 = 5
236bitCount8 200 = 3
237bitCount8 201 = 4
238bitCount8 202 = 4
239bitCount8 203 = 5
240bitCount8 204 = 4
241bitCount8 205 = 5
242bitCount8 206 = 5
243bitCount8 207 = 6
244bitCount8 208 = 3
245bitCount8 209 = 4
246bitCount8 210 = 4
247bitCount8 211 = 5
248bitCount8 212 = 4
249bitCount8 213 = 5
250bitCount8 214 = 5
251bitCount8 215 = 6
252bitCount8 216 = 4
253bitCount8 217 = 5
254bitCount8 218 = 5
255bitCount8 219 = 6
256bitCount8 220 = 5
257bitCount8 221 = 6
258bitCount8 222 = 6
259bitCount8 223 = 7
260bitCount8 224 = 3
261bitCount8 225 = 4
262bitCount8 226 = 4
263bitCount8 227 = 5
264bitCount8 228 = 4
265bitCount8 229 = 5
266bitCount8 230 = 5
267bitCount8 231 = 6
268bitCount8 232 = 4
269bitCount8 233 = 5
270bitCount8 234 = 5
271bitCount8 235 = 6
272bitCount8 236 = 5
273bitCount8 237 = 6
274bitCount8 238 = 6
275bitCount8 239 = 7
276bitCount8 240 = 4
277bitCount8 241 = 5
278bitCount8 242 = 5
279bitCount8 243 = 6
280bitCount8 244 = 5
281bitCount8 245 = 6
282bitCount8 246 = 6
283bitCount8 247 = 7
284bitCount8 248 = 5
285bitCount8 249 = 6
286bitCount8 250 = 6
287bitCount8 251 = 7
288bitCount8 252 = 6
289bitCount8 253 = 7
290bitCount8 254 = 7
291bitCount8 255 = 8
```