/usr/include/kmer/util/bitPacking.h is in libkmer-dev 0~20150903+r2013-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | #ifndef BRI_BITPACKING_H
#define BRI_BITPACKING_H
#include <stdio.h>
#include <assert.h>
// Routines used for stuffing bits into a word array.
// Define this to enable testing that the width of the data element
// is greater than zero. The uint64MASK() macro (bri.h) does not
// generate a mask for 0. Compiler warnings are issued, because you
// shouldn't use this in production code.
//
//#define CHECK_WIDTH
// As CHECK_WIDTH is kind of expensive, we'll warn.
#ifdef CHECK_WIDTH
#warning libutil/bitPacking.h defined CHECK_WIDTH
#endif
// Returns 'siz' bits from the stream based at 'ptr' and currently at
// location 'pos'. The position of the stream is not changed.
//
// Retrieves a collection of values; the number of bits advanced in
// the stream is returned.
//
// Copies the lowest 'siz' bits in 'val' to the stream based at 'ptr'
// and currently at 'pos'. The position of the stream is not
// changed.
//
// Sets a collection of values; the number of bits advanced in the
// stream is returned.
//
uint64 getDecodedValue (uint64 *ptr, uint64 pos, uint64 siz);
uint64 getDecodedValues(uint64 *ptr, uint64 pos, uint64 num, uint64 *sizs, uint64 *vals);
void setDecodedValue (uint64 *ptr, uint64 pos, uint64 siz, uint64 val);
uint64 setDecodedValues(uint64 *ptr, uint64 pos, uint64 num, uint64 *sizs, uint64 *vals);
// Like getDecodedValue() but will pre/post increment/decrement the
// value stored in the stream before in addition to returning the
// value.
//
// preIncrementDecodedValue(ptr, pos, siz) === x = getDecodedValue(ptr, pos, siz) + 1;
// setDecodedValue(ptr, pos, siz, x);
//
// preDecrementDecodedValue(ptr, pos, siz) === x = getDecodedValue(ptr, pos, siz) - 1;
// setDecodedValue(ptr, pos, siz, x);
//
// postIncrementDecodedValue(ptr, pos, siz) === x = getDecodedValue(ptr, pos, siz);
// setDecodedValue(ptr, pos, siz, x + 1);
//
// postDecrementDecodedValue(ptr, pos, siz) === x = getDecodedValue(ptr, pos, siz);
// setDecodedValue(ptr, pos, siz, x - 1);
//
uint64 preIncrementDecodedValue(uint64 *ptr, uint64 pos, uint64 siz);
uint64 preDecrementDecodedValue(uint64 *ptr, uint64 pos, uint64 siz);
uint64 postIncrementDecodedValue(uint64 *ptr, uint64 pos, uint64 siz);
uint64 postDecrementDecodedValue(uint64 *ptr, uint64 pos, uint64 siz);
// N.B. - I assume the bits in words are big-endian, which is
// backwards from the way we shift things around.
//
// I define the "addresses" of bits in two consectuve words as
// [0123][0123]. When adding words to the bit array, they're added
// from left to right:
//
// setDecodedValue(bitstream, %0abc, 3)
// setDecodedValue(bitstream, %0def, 3)
//
// results in [abcd][ef00]
//
// But when shifting things around, we typically do it from the right
// side, since that is where the machine places numbers.
//
// A picture or two might help.
//
//
// |----b1-----|
// |-bit-||-sz-|
// XXXXXX
// [0---------------63]
// ^
// pos
//
//
// If the bits span two words, it'll look like this; b1 is smaller
// than siz, and we update bit to be the "uncovered" piece of XXX
// (all the stuff in word2). The first word is masked, then those
// bits are shifted onto the result in the correct place. The second
// word has the correct bits shifted to the right, then those are
// appended to the result.
//
// |b1-|
// |-----bit-----||---sz---|
// XXXXXXXXXX
// [0------------word1][0-------------word2]
// ^
// pos
//
inline
uint64
getDecodedValue(uint64 *ptr,
uint64 pos,
uint64 siz) {
uint64 wrd = (pos >> 6) & 0x0000cfffffffffffllu;
//PREFETCH(ptr + wrd); makes it worse
uint64 bit = (pos ) & 0x000000000000003fllu;
uint64 b1 = 64 - bit;
uint64 ret = 0;
#ifdef CHECK_WIDTH
if (siz == 0) {
fprintf(stderr, "ERROR: getDecodedValue() called with zero size!\n");
abort();
}
if (siz > 64) {
fprintf(stderr, "ERROR: getDecodedValue() called with huge size ("uint64FMT")!\n", siz);
abort();
}
#endif
if (b1 >= siz) {
ret = ptr[wrd] >> (b1 - siz);
} else {
bit = siz - b1;
ret = (ptr[wrd] & uint64MASK(b1)) << bit;
wrd++;
ret |= (ptr[wrd] >> (64 - bit)) & uint64MASK(bit);
}
ret &= uint64MASK(siz);
return(ret);
}
inline
void
setDecodedValue(uint64 *ptr,
uint64 pos,
uint64 siz,
uint64 val) {
uint64 wrd = (pos >> 6) & 0x0000cfffffffffffllu;
uint64 bit = (pos ) & 0x000000000000003fllu;
uint64 b1 = 64 - bit;
#ifdef CHECK_WIDTH
if (siz == 0) {
fprintf(stderr, "ERROR: setDecodedValue() called with zero size!\n");
abort();
}
if (siz > 64) {
fprintf(stderr, "ERROR: getDecodedValue() called with huge size ("uint64FMT")!\n", siz);
abort();
}
#endif
val &= uint64MASK(siz);
if (b1 >= siz) {
ptr[wrd] &= ~( uint64MASK(siz) << (b1 - siz) );
ptr[wrd] |= val << (b1 - siz);
} else {
bit = siz - b1;
ptr[wrd] &= ~uint64MASK(b1);
ptr[wrd] |= (val & (uint64MASK(b1) << (bit))) >> (bit);
wrd++;
ptr[wrd] &= ~(uint64MASK(bit) << (64 - bit));
ptr[wrd] |= (val & (uint64MASK(bit))) << (64 - bit);
}
}
inline
uint64
getDecodedValues(uint64 *ptr,
uint64 pos,
uint64 num,
uint64 *sizs,
uint64 *vals) {
// compute the location of the start of the encoded words, then
// just walk through to get the remaining words.
uint64 wrd = (pos >> 6) & 0x0000cfffffffffffllu;
//PREFETCH(ptr + wrd); makes it worse
uint64 bit = (pos ) & 0x000000000000003fllu;
uint64 b1 = 0;
for (uint64 i=0; i<num; i++) {
b1 = 64 - bit;
#ifdef CHECK_WIDTH
if (siz[i] == 0) {
fprintf(stderr, "ERROR: postDecrementDecodedValue() called with zero size!\n");
abort();
}
if (siz[i] > 64) {
fprintf(stderr, "ERROR: getDecodedValue() called with huge size ("uint64FMT")!\n", siz);
abort();
}
#endif
if (b1 >= sizs[i]) {
//fprintf(stderr, "get-single pos=%d b1=%d bit=%d wrd=%d\n", pos, b1, bit, wrd);
vals[i] = ptr[wrd] >> (b1 - sizs[i]);
bit += sizs[i];
} else {
//fprintf(stderr, "get-double pos=%d b1=%d bit=%d wrd=%d bitafter=%d\n", pos, b1, bit, wrd, sizs[i]-b1);
bit = sizs[i] - b1;
vals[i] = (ptr[wrd] & uint64MASK(b1)) << bit;
wrd++;
vals[i] |= (ptr[wrd] >> (64 - bit)) & uint64MASK(bit);
}
if (bit == 64) {
wrd++;
bit = 0;
}
assert(bit < 64);
vals[i] &= uint64MASK(sizs[i]);
pos += sizs[i];
}
return(pos);
}
inline
uint64
setDecodedValues(uint64 *ptr,
uint64 pos,
uint64 num,
uint64 *sizs,
uint64 *vals) {
uint64 wrd = (pos >> 6) & 0x0000cfffffffffffllu;
uint64 bit = (pos ) & 0x000000000000003fllu;
uint64 b1 = 0;
for (uint64 i=0; i<num; i++) {
vals[i] &= uint64MASK(sizs[i]);
b1 = 64 - bit;
#ifdef CHECK_WIDTH
if (siz[i] == 0) {
fprintf(stderr, "ERROR: postDecrementDecodedValue() called with zero size!\n");
abort();
}
if (siz[i] > 64) {
fprintf(stderr, "ERROR: getDecodedValue() called with huge size ("uint64FMT")!\n", siz);
abort();
}
#endif
if (b1 >= sizs[i]) {
//fprintf(stderr, "set-single pos=%d b1=%d bit=%d wrd=%d\n", pos, b1, bit, wrd);
ptr[wrd] &= ~( uint64MASK(sizs[i]) << (b1 - sizs[i]) );
ptr[wrd] |= vals[i] << (b1 - sizs[i]);
bit += sizs[i];
} else {
//fprintf(stderr, "set-double pos=%d b1=%d bit=%d wrd=%d bitafter=%d\n", pos, b1, bit, wrd, sizs[i]-b1);
bit = sizs[i] - b1;
ptr[wrd] &= ~uint64MASK(b1);
ptr[wrd] |= (vals[i] & (uint64MASK(b1) << (bit))) >> (bit);
wrd++;
ptr[wrd] &= ~(uint64MASK(bit) << (64 - bit));
ptr[wrd] |= (vals[i] & (uint64MASK(bit))) << (64 - bit);
}
if (bit == 64) {
wrd++;
bit = 0;
}
assert(bit < 64);
pos += sizs[i];
}
return(pos);
}
inline
uint64
preIncrementDecodedValue(uint64 *ptr,
uint64 pos,
uint64 siz) {
uint64 wrd = (pos >> 6) & 0x0000cfffffffffffllu;
uint64 bit = (pos ) & 0x000000000000003fllu;
uint64 b1 = 64 - bit;
uint64 ret = 0;
#ifdef CHECK_WIDTH
if (siz == 0) {
fprintf(stderr, "ERROR: preIncrementDecodedValue() called with zero size!\n");
abort();
}
if (siz > 64) {
fprintf(stderr, "ERROR: getDecodedValue() called with huge size ("uint64FMT")!\n", siz);
abort();
}
#endif
if (b1 >= siz) {
ret = ptr[wrd] >> (b1 - siz);
ret++;
ret &= uint64MASK(siz);
ptr[wrd] &= ~( uint64MASK(siz) << (b1 - siz) );
ptr[wrd] |= ret << (b1 - siz);
} else {
bit = siz - b1;
ret = (ptr[wrd] & uint64MASK(b1)) << bit;
ret |= (ptr[wrd+1] >> (64 - bit)) & uint64MASK(bit);
ret++;
ret &= uint64MASK(siz);
ptr[wrd] &= ~uint64MASK(b1);
ptr[wrd] |= (ret & (uint64MASK(b1) << (bit))) >> (bit);
wrd++;
ptr[wrd] &= ~(uint64MASK(bit) << (64 - bit));
ptr[wrd] |= (ret & (uint64MASK(bit))) << (64 - bit);
}
return(ret);
}
inline
uint64
preDecrementDecodedValue(uint64 *ptr,
uint64 pos,
uint64 siz) {
uint64 wrd = (pos >> 6) & 0x0000cfffffffffffllu;
uint64 bit = (pos ) & 0x000000000000003fllu;
uint64 b1 = 64 - bit;
uint64 ret = 0;
#ifdef CHECK_WIDTH
if (siz == 0) {
fprintf(stderr, "ERROR: preDecrementDecodedValue() called with zero size!\n");
abort();
}
if (siz > 64) {
fprintf(stderr, "ERROR: getDecodedValue() called with huge size ("uint64FMT")!\n", siz);
abort();
}
#endif
if (b1 >= siz) {
ret = ptr[wrd] >> (b1 - siz);
ret--;
ret &= uint64MASK(siz);
ptr[wrd] &= ~( uint64MASK(siz) << (b1 - siz) );
ptr[wrd] |= ret << (b1 - siz);
} else {
bit = siz - b1;
ret = (ptr[wrd] & uint64MASK(b1)) << bit;
ret |= (ptr[wrd+1] >> (64 - bit)) & uint64MASK(bit);
ret--;
ret &= uint64MASK(siz);
ptr[wrd] &= ~uint64MASK(b1);
ptr[wrd] |= (ret & (uint64MASK(b1) << (bit))) >> (bit);
wrd++;
ptr[wrd] &= ~(uint64MASK(bit) << (64 - bit));
ptr[wrd] |= (ret & (uint64MASK(bit))) << (64 - bit);
}
return(ret);
}
inline
uint64
postIncrementDecodedValue(uint64 *ptr,
uint64 pos,
uint64 siz) {
uint64 wrd = (pos >> 6) & 0x0000cfffffffffffllu;
uint64 bit = (pos ) & 0x000000000000003fllu;
uint64 b1 = 64 - bit;
uint64 ret = 0;
#ifdef CHECK_WIDTH
if (siz == 0) {
fprintf(stderr, "ERROR: postIncrementDecodedValue() called with zero size!\n");
abort();
}
if (siz > 64) {
fprintf(stderr, "ERROR: getDecodedValue() called with huge size ("uint64FMT")!\n", siz);
abort();
}
#endif
if (b1 >= siz) {
ret = ptr[wrd] >> (b1 - siz);
ret++;
ret &= uint64MASK(siz);
ptr[wrd] &= ~( uint64MASK(siz) << (b1 - siz) );
ptr[wrd] |= ret << (b1 - siz);
} else {
bit = siz - b1;
ret = (ptr[wrd] & uint64MASK(b1)) << bit;
ret |= (ptr[wrd+1] >> (64 - bit)) & uint64MASK(bit);
ret++;
ret &= uint64MASK(siz);
ptr[wrd] &= ~uint64MASK(b1);
ptr[wrd] |= (ret & (uint64MASK(b1) << (bit))) >> (bit);
wrd++;
ptr[wrd] &= ~(uint64MASK(bit) << (64 - bit));
ptr[wrd] |= (ret & (uint64MASK(bit))) << (64 - bit);
}
ret--;
ret &= uint64MASK(siz);
return(ret);
}
inline
uint64
postDecrementDecodedValue(uint64 *ptr,
uint64 pos,
uint64 siz) {
uint64 wrd = (pos >> 6) & 0x0000cfffffffffffllu;
uint64 bit = (pos ) & 0x000000000000003fllu;
uint64 b1 = 64 - bit;
uint64 ret = 0;
#ifdef CHECK_WIDTH
if (siz == 0) {
fprintf(stderr, "ERROR: postDecrementDecodedValue() called with zero size!\n");
abort();
}
if (siz > 64) {
fprintf(stderr, "ERROR: getDecodedValue() called with huge size ("uint64FMT")!\n", siz);
abort();
}
#endif
if (b1 >= siz) {
ret = ptr[wrd] >> (b1 - siz);
ret--;
ret &= uint64MASK(siz);
ptr[wrd] &= ~( uint64MASK(siz) << (b1 - siz) );
ptr[wrd] |= ret << (b1 - siz);
} else {
bit = siz - b1;
ret = (ptr[wrd] & uint64MASK(b1)) << bit;
ret |= (ptr[wrd+1] >> (64 - bit)) & uint64MASK(bit);
ret--;
ret &= uint64MASK(siz);
ptr[wrd] &= ~uint64MASK(b1);
ptr[wrd] |= (ret & (uint64MASK(b1) << (bit))) >> (bit);
wrd++;
ptr[wrd] &= ~(uint64MASK(bit) << (64 - bit));
ptr[wrd] |= (ret & (uint64MASK(bit))) << (64 - bit);
}
ret++;
ret &= uint64MASK(siz);
return(ret);
}
#endif // BRI_BITPACKING_H
|