| 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 |
#ifndef LZFSE_FSE_H |
| 30 |
#define LZFSE_FSE_H |
| 31 |
|
| 32 |
#include <linux/types.h> |
| 33 |
#include <linux/string.h> |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#if defined(_M_AMD64) || defined(__x86_64__) || defined(__arm64__) |
| 41 |
#define FSE_IOSTREAM_64 1 |
| 42 |
#else |
| 43 |
#define FSE_IOSTREAM_64 0 |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
typedef int32_t fse_bit_count; |
| 50 |
|
| 51 |
|
| 52 |
typedef uint16_t fse_state; |
| 53 |
|
| 54 |
|
| 55 |
static inline uint64_t fse_mask_lsb64(uint64_t x, fse_bit_count nbits) |
| 56 |
{ |
| 57 |
static const uint64_t mtable[65] = { |
| 58 |
0x0000000000000000LLU, 0x0000000000000001LLU, 0x0000000000000003LLU, |
| 59 |
0x0000000000000007LLU, 0x000000000000000fLLU, 0x000000000000001fLLU, |
| 60 |
0x000000000000003fLLU, 0x000000000000007fLLU, 0x00000000000000ffLLU, |
| 61 |
0x00000000000001ffLLU, 0x00000000000003ffLLU, 0x00000000000007ffLLU, |
| 62 |
0x0000000000000fffLLU, 0x0000000000001fffLLU, 0x0000000000003fffLLU, |
| 63 |
0x0000000000007fffLLU, 0x000000000000ffffLLU, 0x000000000001ffffLLU, |
| 64 |
0x000000000003ffffLLU, 0x000000000007ffffLLU, 0x00000000000fffffLLU, |
| 65 |
0x00000000001fffffLLU, 0x00000000003fffffLLU, 0x00000000007fffffLLU, |
| 66 |
0x0000000000ffffffLLU, 0x0000000001ffffffLLU, 0x0000000003ffffffLLU, |
| 67 |
0x0000000007ffffffLLU, 0x000000000fffffffLLU, 0x000000001fffffffLLU, |
| 68 |
0x000000003fffffffLLU, 0x000000007fffffffLLU, 0x00000000ffffffffLLU, |
| 69 |
0x00000001ffffffffLLU, 0x00000003ffffffffLLU, 0x00000007ffffffffLLU, |
| 70 |
0x0000000fffffffffLLU, 0x0000001fffffffffLLU, 0x0000003fffffffffLLU, |
| 71 |
0x0000007fffffffffLLU, 0x000000ffffffffffLLU, 0x000001ffffffffffLLU, |
| 72 |
0x000003ffffffffffLLU, 0x000007ffffffffffLLU, 0x00000fffffffffffLLU, |
| 73 |
0x00001fffffffffffLLU, 0x00003fffffffffffLLU, 0x00007fffffffffffLLU, |
| 74 |
0x0000ffffffffffffLLU, 0x0001ffffffffffffLLU, 0x0003ffffffffffffLLU, |
| 75 |
0x0007ffffffffffffLLU, 0x000fffffffffffffLLU, 0x001fffffffffffffLLU, |
| 76 |
0x003fffffffffffffLLU, 0x007fffffffffffffLLU, 0x00ffffffffffffffLLU, |
| 77 |
0x01ffffffffffffffLLU, 0x03ffffffffffffffLLU, 0x07ffffffffffffffLLU, |
| 78 |
0x0fffffffffffffffLLU, 0x1fffffffffffffffLLU, 0x3fffffffffffffffLLU, |
| 79 |
0x7fffffffffffffffLLU, 0xffffffffffffffffLLU, |
| 80 |
}; |
| 81 |
return x & mtable[nbits]; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
static inline uint32_t fse_mask_lsb32(uint32_t x, fse_bit_count nbits) |
| 86 |
{ |
| 87 |
static const uint32_t mtable[33] = { |
| 88 |
0x0000000000000000U, 0x0000000000000001U, 0x0000000000000003U, 0x0000000000000007U, |
| 89 |
0x000000000000000fU, 0x000000000000001fU, 0x000000000000003fU, 0x000000000000007fU, |
| 90 |
0x00000000000000ffU, 0x00000000000001ffU, 0x00000000000003ffU, 0x00000000000007ffU, |
| 91 |
0x0000000000000fffU, 0x0000000000001fffU, 0x0000000000003fffU, 0x0000000000007fffU, |
| 92 |
0x000000000000ffffU, 0x000000000001ffffU, 0x000000000003ffffU, 0x000000000007ffffU, |
| 93 |
0x00000000000fffffU, 0x00000000001fffffU, 0x00000000003fffffU, 0x00000000007fffffU, |
| 94 |
0x0000000000ffffffU, 0x0000000001ffffffU, 0x0000000003ffffffU, 0x0000000007ffffffU, |
| 95 |
0x000000000fffffffU, 0x000000001fffffffU, 0x000000003fffffffU, 0x000000007fffffffU, |
| 96 |
0x00000000ffffffffU, |
| 97 |
}; |
| 98 |
return x & mtable[nbits]; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
static __always_inline uint64_t fse_extract_bits64(uint64_t x, fse_bit_count start, |
| 105 |
fse_bit_count nbits) |
| 106 |
{ |
| 107 |
|
| 108 |
if (__builtin_constant_p(start) && __builtin_constant_p(nbits)) |
| 109 |
return (x >> start) & ((1LLU << nbits) - 1LLU); |
| 110 |
|
| 111 |
|
| 112 |
return fse_mask_lsb64(x >> start, nbits); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static __always_inline uint32_t fse_extract_bits32(uint32_t x, fse_bit_count start, |
| 119 |
fse_bit_count nbits) |
| 120 |
{ |
| 121 |
|
| 122 |
if (__builtin_constant_p(start) && __builtin_constant_p(nbits)) |
| 123 |
return (x >> start) & ((1U << nbits) - 1U); |
| 124 |
|
| 125 |
|
| 126 |
return fse_mask_lsb32(x >> start, nbits); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
typedef struct { |
| 139 |
uint64_t accum; |
| 140 |
fse_bit_count accum_nbits; |
| 141 |
} fse_out_stream64; |
| 142 |
|
| 143 |
|
| 144 |
typedef struct { |
| 145 |
uint32_t accum; |
| 146 |
fse_bit_count accum_nbits; |
| 147 |
} fse_out_stream32; |
| 148 |
|
| 149 |
|
| 150 |
typedef struct { |
| 151 |
uint64_t accum; |
| 152 |
fse_bit_count accum_nbits; |
| 153 |
} fse_in_stream64; |
| 154 |
|
| 155 |
|
| 156 |
typedef struct { |
| 157 |
uint32_t accum; |
| 158 |
fse_bit_count accum_nbits; |
| 159 |
} fse_in_stream32; |
| 160 |
|
| 161 |
|
| 162 |
static __always_inline void fse_out_init64(fse_out_stream64 *s) |
| 163 |
{ |
| 164 |
s->accum = 0; |
| 165 |
s->accum_nbits = 0; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
static __always_inline void fse_out_init32(fse_out_stream32 *s) |
| 170 |
{ |
| 171 |
s->accum = 0; |
| 172 |
s->accum_nbits = 0; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static __always_inline void fse_out_flush64(fse_out_stream64 *s, uint8_t **pbuf) |
| 182 |
{ |
| 183 |
fse_bit_count nbits = s->accum_nbits & -8; |
| 184 |
|
| 185 |
|
| 186 |
memcpy(*pbuf, &(s->accum), 8); |
| 187 |
*pbuf += (nbits >> 3); |
| 188 |
|
| 189 |
|
| 190 |
s->accum >>= nbits; |
| 191 |
s->accum_nbits -= nbits; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
static __always_inline void fse_out_flush32(fse_out_stream32 *s, uint8_t **pbuf) |
| 201 |
{ |
| 202 |
fse_bit_count nbits = s->accum_nbits & -8; |
| 203 |
|
| 204 |
|
| 205 |
memcpy(*pbuf, &(s->accum), 4); |
| 206 |
*pbuf += (nbits >> 3); |
| 207 |
|
| 208 |
|
| 209 |
s->accum >>= nbits; |
| 210 |
s->accum_nbits -= nbits; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
static __always_inline void fse_out_finish64(fse_out_stream64 *s, uint8_t **pbuf) |
| 220 |
{ |
| 221 |
fse_bit_count nbits = (s->accum_nbits + 7) & -8; |
| 222 |
|
| 223 |
|
| 224 |
memcpy(*pbuf, &(s->accum), 8); |
| 225 |
*pbuf += (nbits >> 3); |
| 226 |
|
| 227 |
|
| 228 |
s->accum = 0; |
| 229 |
s->accum_nbits -= nbits; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
static __always_inline void fse_out_finish32(fse_out_stream32 *s, uint8_t **pbuf) |
| 239 |
{ |
| 240 |
fse_bit_count nbits = (s->accum_nbits + 7) & -8; |
| 241 |
|
| 242 |
|
| 243 |
memcpy(*pbuf, &(s->accum), 4); |
| 244 |
*pbuf += (nbits >> 3); |
| 245 |
|
| 246 |
|
| 247 |
s->accum = 0; |
| 248 |
s->accum_nbits -= nbits; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
static __always_inline void fse_out_push64(fse_out_stream64 *s, fse_bit_count n, uint64_t b) |
| 257 |
{ |
| 258 |
s->accum |= b << s->accum_nbits; |
| 259 |
s->accum_nbits += n; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
static __always_inline void fse_out_push32(fse_out_stream32 *s, fse_bit_count n, uint32_t b) |
| 268 |
{ |
| 269 |
s->accum |= b << s->accum_nbits; |
| 270 |
s->accum_nbits += n; |
| 271 |
} |
| 272 |
|
| 273 |
#define DEBUG_CHECK_INPUT_STREAM_PARAMETERS |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
static __always_inline int fse_in_checked_init64(fse_in_stream64 *s, fse_bit_count n, |
| 283 |
const uint8_t **pbuf, const uint8_t *buf_start) |
| 284 |
{ |
| 285 |
if (n) { |
| 286 |
if (*pbuf < buf_start + 8) |
| 287 |
return -1; |
| 288 |
*pbuf -= 8; |
| 289 |
memcpy(&(s->accum), *pbuf, 8); |
| 290 |
s->accum_nbits = n + 64; |
| 291 |
} else { |
| 292 |
if (*pbuf < buf_start + 7) |
| 293 |
return -1; |
| 294 |
*pbuf -= 7; |
| 295 |
memcpy(&(s->accum), *pbuf, 7); |
| 296 |
s->accum &= 0xffffffffffffff; |
| 297 |
s->accum_nbits = n + 56; |
| 298 |
} |
| 299 |
|
| 300 |
if ((s->accum_nbits < 56 || s->accum_nbits >= 64) || ((s->accum >> s->accum_nbits) != 0)) { |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
return -1; |
| 306 |
} |
| 307 |
|
| 308 |
return 0; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
static __always_inline int fse_in_checked_init32(fse_in_stream32 *s, fse_bit_count n, |
| 315 |
const uint8_t **pbuf, const uint8_t *buf_start) |
| 316 |
{ |
| 317 |
if (n) { |
| 318 |
if (*pbuf < buf_start + 4) |
| 319 |
return -1; |
| 320 |
*pbuf -= 4; |
| 321 |
memcpy(&(s->accum), *pbuf, 4); |
| 322 |
s->accum_nbits = n + 32; |
| 323 |
} else { |
| 324 |
if (*pbuf < buf_start + 3) |
| 325 |
return -1; |
| 326 |
*pbuf -= 3; |
| 327 |
memcpy(&(s->accum), *pbuf, 3); |
| 328 |
s->accum &= 0xffffff; |
| 329 |
s->accum_nbits = n + 24; |
| 330 |
} |
| 331 |
|
| 332 |
if ((s->accum_nbits < 24 || s->accum_nbits >= 32) || ((s->accum >> s->accum_nbits) != 0)) { |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
return -1; |
| 338 |
} |
| 339 |
|
| 340 |
return 0; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
static __always_inline int fse_in_checked_flush64(fse_in_stream64 *s, const uint8_t **pbuf, |
| 350 |
const uint8_t *buf_start) |
| 351 |
{ |
| 352 |
|
| 353 |
fse_bit_count nbits = (63 - s->accum_nbits) & -8; |
| 354 |
|
| 355 |
const uint8_t *buf = (*pbuf) - (nbits >> 3); |
| 356 |
uint64_t incoming; |
| 357 |
|
| 358 |
if (buf < buf_start) |
| 359 |
return -1; |
| 360 |
*pbuf = buf; |
| 361 |
memcpy(&incoming, buf, 8); |
| 362 |
|
| 363 |
s->accum = (s->accum << nbits) | fse_mask_lsb64(incoming, nbits); |
| 364 |
s->accum_nbits += nbits; |
| 365 |
DEBUG_CHECK_INPUT_STREAM_PARAMETERS |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
static __always_inline int fse_in_checked_flush32(fse_in_stream32 *s, const uint8_t **pbuf, |
| 373 |
const uint8_t *buf_start) |
| 374 |
{ |
| 375 |
|
| 376 |
fse_bit_count nbits = (31 - s->accum_nbits) & -8; |
| 377 |
|
| 378 |
if (nbits > 0) { |
| 379 |
|
| 380 |
const uint8_t *buf = (*pbuf) - (nbits >> 3); |
| 381 |
uint32_t incoming; |
| 382 |
|
| 383 |
if (buf < buf_start) |
| 384 |
return -1; |
| 385 |
|
| 386 |
*pbuf = buf; |
| 387 |
|
| 388 |
incoming = *((uint32_t *)buf); |
| 389 |
|
| 390 |
|
| 391 |
s->accum = (s->accum << nbits) | fse_mask_lsb32(incoming, nbits); |
| 392 |
s->accum_nbits += nbits; |
| 393 |
} |
| 394 |
DEBUG_CHECK_INPUT_STREAM_PARAMETERS |
| 395 |
return 0; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
static __always_inline uint64_t fse_in_pull64(fse_in_stream64 *s, fse_bit_count n) |
| 400 |
{ |
| 401 |
uint64_t result; |
| 402 |
|
| 403 |
s->accum_nbits -= n; |
| 404 |
result = s->accum >> s->accum_nbits; |
| 405 |
s->accum = fse_mask_lsb64(s->accum, s->accum_nbits); |
| 406 |
return result; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
static __always_inline uint32_t fse_in_pull32(fse_in_stream32 *s, fse_bit_count n) |
| 411 |
{ |
| 412 |
uint32_t result; |
| 413 |
|
| 414 |
s->accum_nbits -= n; |
| 415 |
result = s->accum >> s->accum_nbits; |
| 416 |
s->accum = fse_mask_lsb32(s->accum, s->accum_nbits); |
| 417 |
return result; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
#if FSE_IOSTREAM_64 |
| 424 |
|
| 425 |
typedef uint64_t fse_bits; |
| 426 |
typedef fse_out_stream64 fse_out_stream; |
| 427 |
typedef fse_in_stream64 fse_in_stream; |
| 428 |
#define fse_mask_lsb fse_mask_lsb64 |
| 429 |
#define fse_extract_bits fse_extract_bits64 |
| 430 |
#define fse_out_init fse_out_init64 |
| 431 |
#define fse_out_flush fse_out_flush64 |
| 432 |
#define fse_out_finish fse_out_finish64 |
| 433 |
#define fse_out_push fse_out_push64 |
| 434 |
#define fse_in_init fse_in_checked_init64 |
| 435 |
#define fse_in_checked_init fse_in_checked_init64 |
| 436 |
#define fse_in_flush fse_in_checked_flush64 |
| 437 |
#define fse_in_checked_flush fse_in_checked_flush64 |
| 438 |
#define fse_in_flush2(_unused, _parameters, _unused2) 0 |
| 439 |
#define fse_in_checked_flush2(_unused, _parameters) |
| 440 |
#define fse_in_pull fse_in_pull64 |
| 441 |
|
| 442 |
#else |
| 443 |
|
| 444 |
typedef uint32_t fse_bits; |
| 445 |
typedef fse_out_stream32 fse_out_stream; |
| 446 |
typedef fse_in_stream32 fse_in_stream; |
| 447 |
#define fse_mask_lsb fse_mask_lsb32 |
| 448 |
#define fse_extract_bits fse_extract_bits32 |
| 449 |
#define fse_out_init fse_out_init32 |
| 450 |
#define fse_out_flush fse_out_flush32 |
| 451 |
#define fse_out_finish fse_out_finish32 |
| 452 |
#define fse_out_push fse_out_push32 |
| 453 |
#define fse_in_init fse_in_checked_init32 |
| 454 |
#define fse_in_checked_init fse_in_checked_init32 |
| 455 |
#define fse_in_flush fse_in_checked_flush32 |
| 456 |
#define fse_in_checked_flush fse_in_checked_flush32 |
| 457 |
#define fse_in_flush2 fse_in_checked_flush32 |
| 458 |
#define fse_in_checked_flush2 fse_in_checked_flush32 |
| 459 |
#define fse_in_pull fse_in_pull32 |
| 460 |
|
| 461 |
#endif |
| 462 |
|
| 463 |
|
| 464 |
typedef struct { |
| 465 |
int8_t k; |
| 466 |
uint8_t symbol; |
| 467 |
int16_t delta; |
| 468 |
} fse_decoder_entry; |
| 469 |
|
| 470 |
|
| 471 |
struct fse_value_decoder_entry { |
| 472 |
uint8_t total_bits; |
| 473 |
uint8_t value_bits; |
| 474 |
int16_t delta; |
| 475 |
int32_t vbase; |
| 476 |
}; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
static __always_inline uint8_t fse_decode(fse_state *__restrict pstate, |
| 484 |
const int32_t *__restrict decoder_table, |
| 485 |
fse_in_stream *__restrict in) |
| 486 |
{ |
| 487 |
int32_t e = decoder_table[*pstate]; |
| 488 |
|
| 489 |
|
| 490 |
*pstate = (fse_state)(e >> 16) + (fse_state)fse_in_pull(in, e & 0xff); |
| 491 |
|
| 492 |
|
| 493 |
return fse_extract_bits(e, 8, 8); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
static __always_inline int32_t fse_value_decode(fse_state *__restrict pstate, |
| 503 |
const struct fse_value_decoder_entry *value_decoder_table, |
| 504 |
fse_in_stream *__restrict in) |
| 505 |
{ |
| 506 |
struct fse_value_decoder_entry entry = value_decoder_table[*pstate]; |
| 507 |
uint32_t state_and_value_bits = (uint32_t)fse_in_pull(in, entry.total_bits); |
| 508 |
*pstate = (fse_state)(entry.delta + (state_and_value_bits >> entry.value_bits)); |
| 509 |
return (int32_t)(entry.vbase + fse_mask_lsb(state_and_value_bits, entry.value_bits)); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
static __always_inline int fse_check_freq(const uint16_t *freq_table, const size_t table_size, |
| 524 |
const size_t number_of_states) |
| 525 |
{ |
| 526 |
size_t sum_of_freq = 0; |
| 527 |
int i; |
| 528 |
|
| 529 |
for (i = 0; i < table_size; i++) |
| 530 |
sum_of_freq += freq_table[i]; |
| 531 |
return (sum_of_freq > number_of_states) ? -1 : 0; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
int fse_init_decoder_table(int nstates, int nsymbols, const uint16_t *__restrict freq, |
| 551 |
int32_t *__restrict t); |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
void fse_init_value_decoder_table(int nstates, int nsymbols, const uint16_t *__restrict freq, |
| 569 |
const uint8_t *__restrict symbol_vbits, |
| 570 |
const int32_t *__restrict symbol_vbase, |
| 571 |
struct fse_value_decoder_entry *__restrict t); |
| 572 |
|
| 573 |
#endif |