| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <esl_config.h> |
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
#include <limits.h> |
| 19 |
|
| 20 |
#include "easel.h" |
| 21 |
#include "esl_varint.h" |
| 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 |
int |
| 49 |
esl_varint_expgol(int v, int k, uint64_t *opt_code, int *opt_n) |
| 50 |
{ |
| 51 |
ESL_DASSERT1(( v >= 0 && k >= 0 )); |
| 52 |
|
| 53 |
uint64_t m = (1 << k); |
| 54 |
uint64_t q = v / m; |
| 55 |
uint64_t r = v % m; |
| 56 |
int n = 0; |
| 57 |
uint64_t code = q+1; |
| 58 |
int status; |
| 59 |
|
| 60 |
|
| 61 |
q = q+1; |
| 62 |
while (q) { q >>= 1; n++; } |
| 63 |
n = 2 * n - 1; |
| 64 |
|
| 65 |
if (opt_code && n > 64) ESL_XEXCEPTION(eslERANGE, "exponential Golomb codeword length > 64"); |
| 66 |
|
| 67 |
|
| 68 |
code = (code << k) | r; |
| 69 |
n += k; |
| 70 |
|
| 71 |
if (opt_code) *opt_code = code; |
| 72 |
if (opt_n) *opt_n = n; |
| 73 |
return eslOK; |
| 74 |
|
| 75 |
ERROR: |
| 76 |
if (opt_code) *opt_code = 0; |
| 77 |
if (opt_n) *opt_n = 0; |
| 78 |
return status; |
| 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 |
int |
| 116 |
esl_varint_expgol_decode(uint64_t code, int k, int *opt_v, int *opt_n) |
| 117 |
{ |
| 118 |
int b,n,r,v; |
| 119 |
int status; |
| 120 |
|
| 121 |
if (! code) { status = eslEOD; goto ERROR; } |
| 122 |
|
| 123 |
for (b = 63; b >= 0; b--) |
| 124 |
if (code & (1ull << b)) break; |
| 125 |
n = 2*(64-b)+k-1; |
| 126 |
if (n > 64) { status = eslECORRUPT; goto ERROR; } |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
code >>= 64-n; |
| 131 |
r = code & ((1ull << k) - 1); |
| 132 |
code >>= k; |
| 133 |
v = (code-1) * (1ull << k) + r; |
| 134 |
|
| 135 |
if (opt_v) *opt_v = v; |
| 136 |
if (opt_n) *opt_n = n; |
| 137 |
return eslOK; |
| 138 |
|
| 139 |
ERROR: |
| 140 |
if (opt_v) *opt_v = 0; |
| 141 |
if (opt_n) *opt_n = 0; |
| 142 |
return status; |
| 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 |
int |
| 170 |
esl_varint_rice(int v, int k, uint64_t *opt_code, int *opt_n) |
| 171 |
{ |
| 172 |
ESL_DASSERT1(( v >= 0 && k >= 0)); |
| 173 |
int m = (1 << k); |
| 174 |
int q = v / m; |
| 175 |
int r = v % m; |
| 176 |
uint64_t code = 0; |
| 177 |
int status; |
| 178 |
|
| 179 |
if (opt_code && q > 64-(k+1)) ESL_XEXCEPTION(eslERANGE, "Golomb-Rice codeword length > 64"); |
| 180 |
|
| 181 |
code = (1ull << q) - 1; |
| 182 |
code = code << (1 + k); |
| 183 |
code |= r; |
| 184 |
|
| 185 |
if (opt_code) *opt_code = code; |
| 186 |
if (opt_n) *opt_n = q + k + 1; |
| 187 |
return eslOK; |
| 188 |
|
| 189 |
ERROR: |
| 190 |
if (opt_code) *opt_code = 0; |
| 191 |
if (opt_n) *opt_n = 0; |
| 192 |
return status; |
| 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 |
int |
| 227 |
esl_varint_rice_decode(uint64_t code, int k, int *opt_v, int *opt_n) |
| 228 |
{ |
| 229 |
int q; |
| 230 |
int status; |
| 231 |
|
| 232 |
if (~code == 0) { status = eslEOD; goto ERROR; } |
| 233 |
|
| 234 |
for (q = 0; q <= 64-k; q++) |
| 235 |
if ( (~code) & (1ull << (63-q))) break; |
| 236 |
|
| 237 |
code >>= 64-(q+k+1); |
| 238 |
|
| 239 |
if (opt_v) *opt_v = (int) (q * (1ull << k) + (code & ( (1ull << (k+1)) - 1))); |
| 240 |
if (opt_n) *opt_n = q + k + 1; |
| 241 |
return eslOK; |
| 242 |
|
| 243 |
ERROR: |
| 244 |
if (opt_v) *opt_v = 0; |
| 245 |
if (opt_n) *opt_n = 0; |
| 246 |
return status; |
| 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 |
int |
| 275 |
esl_varint_delta(int v, uint64_t *opt_code, int *opt_n) |
| 276 |
{ |
| 277 |
ESL_DASSERT1(( v >= 1 )); |
| 278 |
int a = 0; |
| 279 |
int b = 0; |
| 280 |
int n = 0; |
| 281 |
uint64_t code = 0; |
| 282 |
uint64_t tmp; |
| 283 |
int status; |
| 284 |
|
| 285 |
tmp = ((uint64_t) v >> 1); while (tmp) { tmp = tmp >> 1; a++; } |
| 286 |
tmp = ((uint64_t) (a+1) >> 1); while (tmp) { tmp = tmp >> 1; b++; } |
| 287 |
|
| 288 |
n = b; |
| 289 |
n += b+1; code = a+1; |
| 290 |
n += a; code = (code << a) | (v & ((1 << a) - 1)); |
| 291 |
|
| 292 |
if (opt_code && n > 64) ESL_XEXCEPTION(eslERANGE, "Elias delta codeword length > 64"); |
| 293 |
|
| 294 |
if (opt_code) *opt_code = code; |
| 295 |
if (opt_n) *opt_n = n; |
| 296 |
return eslOK; |
| 297 |
|
| 298 |
ERROR: |
| 299 |
if (opt_code) *opt_code = 0; |
| 300 |
if (opt_n) *opt_n = 0; |
| 301 |
return status; |
| 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 |
int |
| 337 |
esl_varint_delta_decode(uint64_t code, int *opt_v, int *opt_n) |
| 338 |
{ |
| 339 |
int a,b,n,status; |
| 340 |
|
| 341 |
if (code == 0) { status = eslEOD; goto ERROR; } |
| 342 |
|
| 343 |
for (b = 0; b < 64; b++) |
| 344 |
if (code & (1ull << (63-b))) break; |
| 345 |
a = (code >> (63 - 2*b)) - 1; |
| 346 |
n = 2*b + a + 1; |
| 347 |
|
| 348 |
if (n > 64) { status = eslECORRUPT; goto ERROR; } |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
if (opt_v) *opt_v = (1<<a) + ((code >> (64 - n)) & ( (1<<a)-1)); |
| 353 |
if (opt_n) *opt_n = n; |
| 354 |
return eslOK; |
| 355 |
|
| 356 |
ERROR: |
| 357 |
if (opt_v) *opt_v = 0; |
| 358 |
if (opt_n) *opt_n = 0; |
| 359 |
return status; |
| 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 |
int |
| 391 |
esl_varint_google(int v, int k, uint64_t *opt_code, int *opt_n) |
| 392 |
{ |
| 393 |
ESL_DASSERT1(( k >= 2 )); |
| 394 |
ESL_DASSERT1(( v >= 0 )); |
| 395 |
|
| 396 |
int base = 1 << (k-1); |
| 397 |
uint64_t code = 0; |
| 398 |
int n = 0; |
| 399 |
int status; |
| 400 |
|
| 401 |
code |= v % base; |
| 402 |
v /= base; |
| 403 |
n += k; |
| 404 |
while (v > 0) |
| 405 |
{ |
| 406 |
code |= (1 << (k-1)); |
| 407 |
code = code << k; |
| 408 |
|
| 409 |
code |= v % base; |
| 410 |
v /= base; |
| 411 |
n += k; |
| 412 |
} |
| 413 |
if (n > 64 && opt_code) ESL_XEXCEPTION(eslERANGE, "google varint code length > 64"); |
| 414 |
|
| 415 |
if (opt_code) *opt_code = code; |
| 416 |
if (opt_n) *opt_n = n; |
| 417 |
return eslOK; |
| 418 |
|
| 419 |
ERROR: |
| 420 |
if (opt_code) *opt_code = 0; |
| 421 |
if (opt_n) *opt_n = 0; |
| 422 |
return status; |
| 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 |
int |
| 456 |
esl_varint_google_decode(uint64_t code, int k, int *opt_v, int *opt_n) |
| 457 |
{ |
| 458 |
uint64_t cmask = 1ull << (k-1); |
| 459 |
uint64_t vmask = cmask-1; |
| 460 |
int v = 0; |
| 461 |
uint64_t grp; |
| 462 |
int g; |
| 463 |
int status; |
| 464 |
|
| 465 |
for (g = 0; g < 64/k; code <<= k, g++) |
| 466 |
{ |
| 467 |
grp = code >> (64-k); |
| 468 |
v += (grp & vmask) << (g*(k-1)); |
| 469 |
if (!(grp & cmask)) break; |
| 470 |
} |
| 471 |
if (grp & cmask) { status = eslEOD; goto ERROR; } |
| 472 |
|
| 473 |
if (opt_v) *opt_v = v; |
| 474 |
if (opt_n) *opt_n = (g+1)*k; |
| 475 |
return eslOK; |
| 476 |
|
| 477 |
ERROR: |
| 478 |
if (opt_v) *opt_v = 0; |
| 479 |
if (opt_n) *opt_n = 0; |
| 480 |
return status; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
#ifdef eslVARINT_TESTDRIVE |
| 490 |
|
| 491 |
#include "esl_random.h" |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
static int |
| 501 |
floor_log2(int v) |
| 502 |
{ |
| 503 |
return (v == 0 ? 0 : (int) floor(log2(v))); |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
static int |
| 510 |
floor_log2k(int v, int k) |
| 511 |
{ |
| 512 |
return ( v == 0 ? 0 : (int) floor( log2(v)) / k); |
| 513 |
} |
| 514 |
|
| 515 |
int len_expgol(int v, int k) { return (2 * floor_log2((v / (1<<k)) + 1) + 1 + k); } |
| 516 |
int64_t max_expgol(int b, int k) { return (((1ull << (((b-k-1)/2) + 1)) - 1) << k) - 1; } |
| 517 |
int len_rice (int v, int k) { return (1 + k + (v / (1ull << k))); } |
| 518 |
int max_rice (int b, int k) { return ( (1ull << k) * (b-k) - 1); } |
| 519 |
int len_delta (int v) { return (floor_log2(v) + 2 * floor_log2( (floor_log2(v) + 1)) + 1); } |
| 520 |
int len_google(int v, int k) { return ( k * (floor_log2k(v,k-1) + 1)); } |
| 521 |
int64_t max_google(int b, int k) { return ((1ull << ((b/k) * (k-1))) - 1); } |
| 522 |
|
| 523 |
|
| 524 |
static void |
| 525 |
utest_expgol(ESL_RANDOMNESS *rng) |
| 526 |
{ |
| 527 |
char msg[] = "utest_expgol exponential Golomb tests failed"; |
| 528 |
uint64_t code; |
| 529 |
int64_t maxv; |
| 530 |
int v, v2; |
| 531 |
int n, n2; |
| 532 |
int k; |
| 533 |
int b; |
| 534 |
int i; |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
if ( esl_varint_expgol( 9, 0, &code, &n) != eslOK) esl_fatal(msg); |
| 541 |
if ( code != 0xall || n != 7) esl_fatal(msg); |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
if ( esl_varint_expgol( 9, 2, &code, &n) != eslOK) esl_fatal(msg); |
| 547 |
if ( code != 0xdll || n != 5) esl_fatal(msg); |
| 548 |
|
| 549 |
|
| 550 |
for (k = 0; k <= 10; k++) |
| 551 |
{ |
| 552 |
|
| 553 |
for (v = 0; v < 1000; v++) |
| 554 |
{ |
| 555 |
if ( esl_varint_expgol(v, k, &code, &n) != eslOK) esl_fatal(msg); |
| 556 |
if ( esl_varint_expgol_decode(code << (64-n), k, &v2, &n2) != eslOK) esl_fatal(msg); |
| 557 |
if (n != len_expgol(v, k)) esl_fatal(msg); |
| 558 |
if (v != v2 || n != n2) esl_fatal(msg); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
maxv = ESL_MIN(INT_MAX, max_expgol(64, k)); |
| 563 |
for (i = 0; i < 1000; i++) |
| 564 |
{ |
| 565 |
v = esl_rnd_Roll(rng, (int) maxv); |
| 566 |
if ( esl_varint_expgol(v, k, &code, &n) != eslOK) esl_fatal(msg); |
| 567 |
if ( esl_varint_expgol_decode(code << (64-n), k, &v2, &n2) != eslOK) esl_fatal(msg); |
| 568 |
if (n != len_expgol(v, k)) esl_fatal(msg); |
| 569 |
if (v != v2 || n != n2) esl_fatal(msg); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
for (b = k+1; b <= 62; b++) |
| 574 |
{ |
| 575 |
maxv = max_expgol(b, k); |
| 576 |
if (maxv <= INT_MAX) |
| 577 |
{ |
| 578 |
if ( esl_varint_expgol((int) maxv, k, &code, &n) != eslOK) esl_fatal(msg); |
| 579 |
if ( esl_varint_expgol_decode(code << (64-n), k, &v2, &n2) != eslOK) esl_fatal(msg); |
| 580 |
if (n != len_expgol(maxv, k)) esl_fatal(msg); |
| 581 |
if (maxv != v2 || n != n2) esl_fatal(msg); |
| 582 |
} |
| 583 |
if (maxv < INT_MAX) |
| 584 |
{ |
| 585 |
if ( esl_varint_expgol((int) (maxv+1), k, NULL, &n2) != eslOK) esl_fatal(msg); |
| 586 |
if (n2 <= n) esl_fatal(msg); |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
static void |
| 594 |
utest_rice(ESL_RANDOMNESS *rng) |
| 595 |
{ |
| 596 |
char msg[] = "utest_rice Golomb-Rice tests failed"; |
| 597 |
uint64_t code; |
| 598 |
int64_t maxv; |
| 599 |
int v, v2; |
| 600 |
int n, n2; |
| 601 |
int k; |
| 602 |
int b; |
| 603 |
int i; |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
if ( esl_varint_rice( 9, 2, &code, &n) != eslOK) esl_fatal(msg); |
| 609 |
if ( code != 0x19ll || n != 5) esl_fatal(msg); |
| 610 |
|
| 611 |
|
| 612 |
for (k = 0; k <= 10; k++) |
| 613 |
{ |
| 614 |
|
| 615 |
maxv = ESL_MIN(1000, max_rice(64, k)); |
| 616 |
for (v = 0; v <= maxv; v++) |
| 617 |
{ |
| 618 |
if ( esl_varint_rice(v, k, &code, &n) != eslOK) esl_fatal(msg); |
| 619 |
if ( esl_varint_rice_decode(code << (64-n), k, &v2, &n2) != eslOK) esl_fatal(msg); |
| 620 |
if (n != len_rice(v, k)) esl_fatal(msg); |
| 621 |
if (v != v2 || n != n2) esl_fatal(msg); |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
maxv = ESL_MIN(INT_MAX, max_rice(64, k)); |
| 626 |
for (i = 0; i < 1000; i++) |
| 627 |
{ |
| 628 |
v = esl_rnd_Roll(rng, (int) maxv); |
| 629 |
if ( esl_varint_rice(v, k, &code, &n) != eslOK) esl_fatal(msg); |
| 630 |
if ( esl_varint_rice_decode(code << (64-n), k, &v2, &n2) != eslOK) esl_fatal(msg); |
| 631 |
if (n != len_rice(v, k)) esl_fatal(msg); |
| 632 |
if (v != v2 || n != n2) esl_fatal(msg); |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
for (b = k+1; b <= 63; b++) |
| 637 |
{ |
| 638 |
maxv = max_rice(b, k); |
| 639 |
if (maxv <= INT_MAX) |
| 640 |
{ |
| 641 |
if ( esl_varint_rice((int) maxv, k, &code, &n) != eslOK) esl_fatal(msg); |
| 642 |
if ( esl_varint_rice_decode(code << (64-n), k, &v2, &n2) != eslOK) esl_fatal(msg); |
| 643 |
if (n != len_rice(maxv, k)) esl_fatal(msg); |
| 644 |
if (maxv != v2 || n != n2) esl_fatal(msg); |
| 645 |
} |
| 646 |
if (maxv < INT_MAX) |
| 647 |
{ |
| 648 |
if ( esl_varint_rice((int) (maxv+1), k, NULL, &n2) != eslOK) esl_fatal(msg); |
| 649 |
if (n2 <= n) esl_fatal(msg); |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
static void |
| 656 |
utest_delta(ESL_RANDOMNESS *rng) |
| 657 |
{ |
| 658 |
char msg[] = "utest_delta Elias delta tests failed"; |
| 659 |
uint64_t code; |
| 660 |
int64_t maxv; |
| 661 |
int v, v2; |
| 662 |
int n, n2; |
| 663 |
int i; |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
if ( esl_varint_delta( 9, &code, &n) != eslOK) esl_fatal(msg); |
| 669 |
if ( code != 0x21ll || n != 8) esl_fatal(msg); |
| 670 |
|
| 671 |
|
| 672 |
for (v = 1; v < 1000; v++) |
| 673 |
{ |
| 674 |
if ( esl_varint_delta(v, &code, &n) != eslOK) esl_fatal(msg); |
| 675 |
if ( esl_varint_delta_decode(code << (64-n), &v2, &n2) != eslOK) esl_fatal(msg); |
| 676 |
if (n != len_delta(v)) esl_fatal(msg); |
| 677 |
if (v != v2 || n != n2) esl_fatal(msg); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
maxv = INT_MAX; |
| 682 |
for (i = 0; i < 1000; i++) |
| 683 |
{ |
| 684 |
v = 1 + esl_rnd_Roll(rng, (int) maxv); |
| 685 |
if ( esl_varint_delta(v, &code, &n) != eslOK) esl_fatal(msg); |
| 686 |
if ( esl_varint_delta_decode(code << (64-n), &v2, &n2) != eslOK) esl_fatal(msg); |
| 687 |
if (n != len_delta(v)) esl_fatal(msg); |
| 688 |
if (v != v2 || n != n2) esl_fatal(msg); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
static void |
| 697 |
utest_google(ESL_RANDOMNESS *rng) |
| 698 |
{ |
| 699 |
char msg[] = "utest_google Google varint tests failed"; |
| 700 |
uint64_t code; |
| 701 |
int v, v2; |
| 702 |
int n, n2; |
| 703 |
int k; |
| 704 |
int b; |
| 705 |
int i; |
| 706 |
int64_t maxv; |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
if ( esl_varint_google( 9, 2, &code, &n) != eslOK) esl_fatal(msg); |
| 712 |
if ( code != 0xe9ll) esl_fatal(msg); |
| 713 |
if ( n != 8) esl_fatal(msg); |
| 714 |
|
| 715 |
|
| 716 |
for (k = 2; k <= 10; k++) |
| 717 |
{ |
| 718 |
|
| 719 |
for (v = 0; v < 1000; v++) |
| 720 |
{ |
| 721 |
if ( esl_varint_google(v, k, &code, &n) != eslOK) esl_fatal(msg); |
| 722 |
if ( esl_varint_google_decode(code << (64-n), k, &v2, &n2) != eslOK) esl_fatal(msg); |
| 723 |
if (n != len_google(v, k)) esl_fatal(msg); |
| 724 |
if (v != v2) esl_fatal(msg); |
| 725 |
if (n != n2) esl_fatal(msg); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
maxv = ESL_MIN(INT_MAX, max_google(64, k)); |
| 731 |
for (i = 0; i < 1000; i++) |
| 732 |
{ |
| 733 |
v = esl_rnd_Roll(rng, (int) maxv); |
| 734 |
if ( esl_varint_google(v, k, &code, &n) != eslOK) esl_fatal(msg); |
| 735 |
if ( esl_varint_google_decode(code << (64-n), k, &v2, &n2) != eslOK) esl_fatal(msg); |
| 736 |
if (n != len_google(v, k)) esl_fatal(msg); |
| 737 |
if (v != v2) esl_fatal(msg); |
| 738 |
if (n != n2) esl_fatal(msg); |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
for (b = k; b <= 64-k; b++) |
| 744 |
{ |
| 745 |
maxv = max_google(b, k); |
| 746 |
if (maxv <= INT_MAX) |
| 747 |
{ |
| 748 |
if ( esl_varint_google((int) maxv, k, &code, &n) != eslOK) esl_fatal(msg); |
| 749 |
if ( esl_varint_google_decode(code << (64-n), k, &v2, &n2) != eslOK) esl_fatal(msg); |
| 750 |
if (n != len_google(maxv, k)) esl_fatal(msg); |
| 751 |
if (maxv != v2) esl_fatal(msg); |
| 752 |
if (n != n2) esl_fatal(msg); |
| 753 |
} |
| 754 |
if (maxv < INT_MAX) |
| 755 |
{ |
| 756 |
if ( esl_varint_google((int) (maxv+1), k, NULL, &n2) != eslOK) esl_fatal(msg); |
| 757 |
if (n2 <= n) esl_fatal(msg); |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
} |
| 762 |
#endif |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
#ifdef eslVARINT_TESTDRIVE |
| 769 |
|
| 770 |
#include "easel.h" |
| 771 |
#include "esl_getopts.h" |
| 772 |
#include "esl_random.h" |
| 773 |
|
| 774 |
static ESL_OPTIONS options[] = { |
| 775 |
|
| 776 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 777 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 778 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 779 |
}; |
| 780 |
static char usage[] = "[-options]"; |
| 781 |
static char banner[] = "test driver for varint module"; |
| 782 |
|
| 783 |
|
| 784 |
int |
| 785 |
main(int argc, char **argv) |
| 786 |
{ |
| 787 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 788 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 789 |
|
| 790 |
fprintf(stderr, "## %s\n", argv[0]); |
| 791 |
fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 792 |
|
| 793 |
utest_expgol(rng); |
| 794 |
utest_rice (rng); |
| 795 |
utest_delta (rng); |
| 796 |
utest_google(rng); |
| 797 |
|
| 798 |
fprintf(stderr, "# status = ok\n"); |
| 799 |
|
| 800 |
esl_randomness_Destroy(rng); |
| 801 |
esl_getopts_Destroy(go); |
| 802 |
return eslOK; |
| 803 |
} |
| 804 |
#endif |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
#ifdef eslVARINT_EXAMPLE |
| 810 |
|
| 811 |
#include "esl_getopts.h" |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
#define CODEOPTS "--expgol,--rice,--google,--delta" |
| 827 |
#define OUTOPTS "--tbl,--fig1,--fig2" |
| 828 |
|
| 829 |
static ESL_OPTIONS options[] = { |
| 830 |
|
| 831 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 832 |
{ "-k", eslARG_INT, "2", NULL, NULL, NULL, NULL,"--delta", "k parameter for an encoding", 0}, |
| 833 |
{ "-L", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t", "only show lengths, not code (for large -n)",0}, |
| 834 |
{ "-n", eslARG_INT, "200", NULL, NULL, NULL, NULL, NULL, "show table for encoding of up to v=<n>", 0}, |
| 835 |
{ "-t", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "format table for LaTeX tabular", 0}, |
| 836 |
{ "-1", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,"--delta", "encode v-1 (i.e. 1..n not 0..n)", 0}, |
| 837 |
{ "--expgol", eslARG_NONE,"default",NULL, NULL, CODEOPTS, NULL, NULL, "Use exp-Golomb-k encoding", 0}, |
| 838 |
{ "--rice", eslARG_NONE, FALSE, NULL, NULL, CODEOPTS, NULL, NULL, "Use Golomb-Rice-k encoding", 0}, |
| 839 |
{ "--delta", eslARG_NONE, FALSE, NULL, NULL, CODEOPTS, NULL, NULL, "Use Elias delta encoding", 0}, |
| 840 |
{ "--google", eslARG_NONE, FALSE, NULL, NULL, CODEOPTS, NULL, NULL, "Use Varint-k encoding", 0}, |
| 841 |
{ "--tbl", eslARG_NONE,"default",NULL, NULL, OUTOPTS, NULL, NULL, "output table of ints and their codes", 0}, |
| 842 |
{ "--fig1", eslARG_NONE, FALSE, NULL, NULL, OUTOPTS, NULL, NULL, "output XY fig data for v vs codelen", 0}, |
| 843 |
{ "--fig2", eslARG_NONE, FALSE, NULL, NULL, OUTOPTS, NULL, NULL, "output XY fig data for v vs implicit prob", 0}, |
| 844 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 845 |
}; |
| 846 |
|
| 847 |
|
| 848 |
static char banner[] = "table and figure data for varint prefix codes"; |
| 849 |
static char usage[] = "[-options]"; |
| 850 |
|
| 851 |
static void |
| 852 |
dump_uint64(FILE *fp, uint64_t code, int n, int fieldwidth) |
| 853 |
{ |
| 854 |
ESL_DASSERT1(( n > 0 )); |
| 855 |
ESL_DASSERT1(( fieldwidth > 0 )); |
| 856 |
int i; |
| 857 |
|
| 858 |
fprintf(fp, "%*s", fieldwidth-n, ""); |
| 859 |
for (i = n-1; i >= 0; i--) |
| 860 |
putc( (code & (1ull<<i)) ? '1' : '0', fp); |
| 861 |
} |
| 862 |
|
| 863 |
int |
| 864 |
main(int argc, char **argv) |
| 865 |
{ |
| 866 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 867 |
int k = esl_opt_GetInteger(go, "-k"); |
| 868 |
int vmin = esl_opt_GetBoolean(go, "--delta") ? 1 : 0; |
| 869 |
int vmax = esl_opt_GetInteger(go, "-n"); |
| 870 |
int do_offset = esl_opt_GetBoolean(go, "-1"); |
| 871 |
int v; |
| 872 |
uint64_t code; |
| 873 |
int n; |
| 874 |
int do_encoding = esl_opt_GetBoolean(go, "--tbl") ? TRUE : FALSE; |
| 875 |
|
| 876 |
for (v = vmin; v <= vmax; v++) |
| 877 |
{ |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
if (esl_opt_GetBoolean(go, "--expgol")) esl_varint_expgol(v, k, do_encoding? &code : NULL, &n); |
| 882 |
else if (esl_opt_GetBoolean(go, "--rice")) esl_varint_rice (v, k, do_encoding? &code : NULL, &n); |
| 883 |
else if (esl_opt_GetBoolean(go, "--google")) esl_varint_google(v, k, do_encoding? &code : NULL, &n); |
| 884 |
else if (esl_opt_GetBoolean(go, "--delta")) esl_varint_delta (v, do_encoding? &code : NULL, &n); |
| 885 |
|
| 886 |
if (esl_opt_GetBoolean(go, "--tbl")) |
| 887 |
{ |
| 888 |
printf("| %10d | %2d | ", do_offset ? v+1 : v, n); |
| 889 |
dump_uint64(stdout, code, n, 30); |
| 890 |
printf("\n"); |
| 891 |
} |
| 892 |
else if (esl_opt_GetBoolean(go, "--fig1")) printf("%d %d\n", do_offset? v+1 : v, n); |
| 893 |
else if (esl_opt_GetBoolean(go, "--fig2")) printf("%d %g\n", do_offset? v+1 : v, pow(2., -1. * n)); |
| 894 |
} |
| 895 |
|
| 896 |
if (esl_opt_GetBoolean(go, "--fig1") || esl_opt_GetBoolean(go, "--fig2")) |
| 897 |
printf("&\n"); |
| 898 |
|
| 899 |
esl_getopts_Destroy(go); |
| 900 |
return eslOK; |
| 901 |
} |
| 902 |
#endif |
| 903 |
|
| 904 |
|