Debian Codegraph @

lzfse/lzfse_fse.h

raw symbols SCIP
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3 * Copyright (c) 2015-2016, Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 *
9 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
10 * in the documentation and/or other materials provided with the distribution.
11 *
12 * 3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
17 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 */
22
23 /*
24 * Finite state entropy coding (FSE)
25 * This is an implementation of the tANS algorithm described by Jarek Duda,
26 * we use the more descriptive name "Finite State Entropy".
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 * Select between 32/64-bit I/O streams for FSE. Note that the FSE stream
37 * size need not match the word size of the machine, but in practice you
38 * want to use 64b streams on 64b systems for better performance.
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 /* MARK: - Bit utils */
47
48 /*! @abstract Signed type used to represent bit count. */
49 typedef int32_t fse_bit_count;
50
51 /*! @abstract Unsigned type used to represent FSE state. */
52 typedef uint16_t fse_state;
53
54 /* Mask the NBITS lsb of X. 0 <= NBITS < 64 */
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 /* Mask the NBITS lsb of X. 0 <= NBITS < 32 */
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 /*! @abstract Select \c nbits at index \c start from \c x.
102 * 0 <= start <= start+nbits <= 64
103 */
104 static __always_inline uint64_t fse_extract_bits64(uint64_t x, fse_bit_count start,
105 fse_bit_count nbits)
106 {
107 /* If START and NBITS are constants, map to bit-field extraction instructions */
108 if (__builtin_constant_p(start) && __builtin_constant_p(nbits))
109 return (x >> start) & ((1LLU << nbits) - 1LLU);
110
111 /* Otherwise, shift and mask */
112 return fse_mask_lsb64(x >> start, nbits);
113 }
114
115 /*! @abstract Select \c nbits at index \c start from \c x.
116 * 0 <= start <= start+nbits <= 32
117 */
118 static __always_inline uint32_t fse_extract_bits32(uint32_t x, fse_bit_count start,
119 fse_bit_count nbits)
120 {
121 /* If START and NBITS are constants, map to bit-field extraction instructions */
122 if (__builtin_constant_p(start) && __builtin_constant_p(nbits))
123 return (x >> start) & ((1U << nbits) - 1U);
124
125 /* Otherwise, shift and mask */
126 return fse_mask_lsb32(x >> start, nbits);
127 }
128
129 /* MARK: - Bit stream */
130
131 /*
132 * I/O streams
133 * The streams can be shared between several FSE encoders/decoders, which is why
134 * they are not in the state struct
135 */
136
137 /*! @abstract Output stream, 64-bit accum. */
138 typedef struct {
139 uint64_t accum; /* Output bits */
140 fse_bit_count accum_nbits; /* Number of valid bits in ACCUM, other bits are 0 */
141 } fse_out_stream64;
142
143 /*! @abstract Output stream, 32-bit accum. */
144 typedef struct {
145 uint32_t accum; /* Output bits */
146 fse_bit_count accum_nbits; /* Number of valid bits in ACCUM, other bits are 0 */
147 } fse_out_stream32;
148
149 /*! @abstract Object representing an input stream. */
150 typedef struct {
151 uint64_t accum; /* Input bits */
152 fse_bit_count accum_nbits; /* Number of valid bits in ACCUM, other bits are 0 */
153 } fse_in_stream64;
154
155 /*! @abstract Object representing an input stream. */
156 typedef struct {
157 uint32_t accum; /* Input bits */
158 fse_bit_count accum_nbits; /* Number of valid bits in ACCUM, other bits are 0 */
159 } fse_in_stream32;
160
161 /*! @abstract Initialize an output stream object. */
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 /*! @abstract Initialize an output stream object. */
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 /*! @abstract Write full bytes from the accumulator to output buffer, ensuring
176 * accum_nbits is in [0, 7].
177 * We assume we can write 8 bytes to the output buffer \c (*pbuf[0..7]) in all
178 * cases.
179 * @note *pbuf is incremented by the number of written bytes.
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; /* number of bits written, multiple of 8 */
184
185 /* Write 8 bytes of current accumulator */
186 memcpy(*pbuf, &(s->accum), 8);
187 *pbuf += (nbits >> 3); /* bytes */
188
189 /* Update state */
190 s->accum >>= nbits; /* remove nbits */
191 s->accum_nbits -= nbits;
192 }
193
194 /*! @abstract Write full bytes from the accumulator to output buffer, ensuring
195 * accum_nbits is in [0, 7].
196 * We assume we can write 4 bytes to the output buffer \c (*pbuf[0..3]) in all
197 * cases.
198 * @note *pbuf is incremented by the number of written bytes.
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; /* number of bits written, multiple of 8 */
203
204 /* Write 4 bytes of current accumulator */
205 memcpy(*pbuf, &(s->accum), 4);
206 *pbuf += (nbits >> 3); /* bytes */
207
208 /* Update state */
209 s->accum >>= nbits; /* remove nbits */
210 s->accum_nbits -= nbits;
211 }
212
213 /*! @abstract Write the last bytes from the accumulator to output buffer,
214 * ensuring accum_nbits is in [-7, 0]. Bits are padded with 0 if needed.
215 * We assume we can write 8 bytes to the output buffer \c (*pbuf[0..7]) in all
216 * cases.
217 * @note *pbuf is incremented by the number of written bytes.
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; /* number of bits written, multiple of 8 */
222
223 /* Write 8 bytes of current accumulator */
224 memcpy(*pbuf, &(s->accum), 8);
225 *pbuf += (nbits >> 3); /* bytes */
226
227 /* Update state */
228 s->accum = 0; /* remove nbits */
229 s->accum_nbits -= nbits;
230 }
231
232 /*! @abstract Write the last bytes from the accumulator to output buffer,
233 * ensuring accum_nbits is in [-7, 0]. Bits are padded with 0 if needed.
234 * We assume we can write 4 bytes to the output buffer \c (*pbuf[0..3]) in all
235 * cases.
236 * @note *pbuf is incremented by the number of written bytes.
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; /* number of bits written, multiple of 8 */
241
242 /* Write 8 bytes of current accumulator */
243 memcpy(*pbuf, &(s->accum), 4);
244 *pbuf += (nbits >> 3); /* bytes */
245
246 /* Update state */
247 s->accum = 0; /* remove nbits */
248 s->accum_nbits -= nbits;
249 }
250
251 /*! @abstract Accumulate \c n bits \c b to output stream \c s. We \b must have:
252 * 0 <= b < 2^n, and N + s->accum_nbits <= 64.
253 * @note The caller must ensure out_flush is called \b before the accumulator
254 * overflows to more than 64 bits.
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 /*! @abstract Accumulate \c n bits \c b to output stream \c s. We \b must have:
263 * 0 <= n < 2^n, and n + s->accum_nbits <= 32.
264 * @note The caller must ensure out_flush is called \b before the accumulator
265 * overflows to more than 32 bits.
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 /*! @abstract Initialize the fse input stream so that accum holds between 56
276 * and 63 bits. We never want to have 64 bits in the stream, because that allows
277 * us to avoid a special case in the fse_in_pull function (eliminating an
278 * unpredictable branch), while not requiring any additional fse_flush
279 * operations. This is why we have the special case for n == 0 (in which case
280 * we want to load only 7 bytes instead of 8).
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; /* out of range */
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; /* out of range */
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 * the incoming input is wrong (encoder should have zeroed the
303 * upper bits)
304 */
305 return -1;
306 }
307
308 return 0; /* OK */
309 }
310
311 /*! @abstract Identical to previous function, but for 32-bit operation
312 * (resulting bit count is between 24 and 31 bits).
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; /* out of range */
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; /* out of range */
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 * the incoming input is wrong (encoder should have zeroed the
335 * upper bits)
336 */
337 return -1;
338 }
339
340 return 0; /* OK */
341 }
342
343 /*! @abstract Read in new bytes from buffer to ensure that we have a full
344 * complement of bits in the stream object (again, between 56 and 63 bits).
345 * checking the new value of \c *pbuf remains >= \c buf_start.
346 * @return 0 if OK.
347 * @return -1 on failure.
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 /* Get number of bits to add to bring us into the desired range. */
353 fse_bit_count nbits = (63 - s->accum_nbits) & -8;
354 /* Convert bits to bytes and decrement buffer address, then load new data. */
355 const uint8_t *buf = (*pbuf) - (nbits >> 3);
356 uint64_t incoming;
357
358 if (buf < buf_start)
359 return -1; /* out of range */
360 *pbuf = buf;
361 memcpy(&incoming, buf, 8);
362 /* Update the state object and verify its validity (in DEBUG). */
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; // OK
367 }
368
369 /*! @abstract Identical to previous function (but again, we're only filling
370 * a 32-bit field with between 24 and 31 bits).
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 /* Get number of bits to add to bring us into the desired range. */
376 fse_bit_count nbits = (31 - s->accum_nbits) & -8;
377
378 if (nbits > 0) {
379 /* Convert bits to bytes and decrement buffer address, then load new data. */
380 const uint8_t *buf = (*pbuf) - (nbits >> 3);
381 uint32_t incoming;
382
383 if (buf < buf_start)
384 return -1; /* out of range */
385
386 *pbuf = buf;
387
388 incoming = *((uint32_t *)buf);
389
390 /* Update the state object and verify its validity (in DEBUG). */
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; /* OK */
396 }
397
398 /*! @abstract Pull n bits out of the fse stream object. */
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 /*! @abstract Pull n bits out of the fse stream object. */
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 /* MARK: - Encode/Decode */
421
422 /* Map to 32/64-bit implementations and types for I/O */
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 /* nothing */
439 #define fse_in_checked_flush2(_unused, _parameters) /* nothing */
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 /*! @abstract Entry for one state in the decoder table (32b). */
464 typedef struct { /* DO NOT REORDER THE FIELDS */
465 int8_t k; /* Number of bits to read */
466 uint8_t symbol; /* Emitted symbol */
467 int16_t delta; /* Signed increment used to compute next state (+bias) */
468 } fse_decoder_entry;
469
470 /*! @abstract Entry for one state in the value decoder table (64b). */
471 struct fse_value_decoder_entry { /* DO NOT REORDER THE FIELDS */
472 uint8_t total_bits; /* state bits + extra value bits = shift for next decode */
473 uint8_t value_bits; /* extra value bits */
474 int16_t delta; /* state base (delta) */
475 int32_t vbase; /* value base */
476 };
477
478 /*! @abstract Decode and return symbol using the decoder table, and update
479 * \c *pstate, \c in.
480 * @note The caller must ensure we have enough bits available in the input
481 * stream accumulator.
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 /* Update state from K bits of input + DELTA */
490 *pstate = (fse_state)(e >> 16) + (fse_state)fse_in_pull(in, e & 0xff);
491
492 /* Return the symbol for this state */
493 return fse_extract_bits(e, 8, 8); /* symbol */
494 }
495
496 /*! @abstract Decode and return value using the decoder table, and update \c
497 * *pstate, \c in.
498 * \c value_decoder_table[nstates]
499 * @note The caller must ensure we have enough bits available in the input
500 * stream accumulator.
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 /* MARK: - Tables */
513
514 /*
515 * IMPORTANT: To properly decode an FSE encoded stream, both encoder/decoder
516 * tables shall be initialized with the same parameters, including the
517 * FREQ[NSYMBOL] array.
518 */
519
520 /*! @abstract Sanity check on frequency table, verify sum of \c freq
521 * is <= \c number_of_states.
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 /*! @abstract Initialize decoder table \c t[nstates].
535 *
536 * @param nstates
537 * sum \c freq[i]; the number of states (a power of 2).
538 *
539 * @param nsymbols
540 * the number of symbols.
541 *
542 * @param feq[nsymbols]
543 * a normalized histogram of symbol frequencies, with \c freq[i] >= 0.
544 * Some symbols may have a 0 frequency. In that case they should not be
545 * present in the data.
546 *
547 * @return 0 if OK.
548 * @return -1 on failure.
549 */
550 int fse_init_decoder_table(int nstates, int nsymbols, const uint16_t *__restrict freq,
551 int32_t *__restrict t);
552
553 /*! @abstract Initialize value decoder table \c t[nstates].
554 *
555 * @param nstates
556 * sum \cfreq[i]; the number of states (a power of 2).
557 *
558 * @param nsymbols
559 * the number of symbols.
560 *
561 * @param freq[nsymbols]
562 * a normalized histogram of symbol frequencies, with \c freq[i] >= 0.
563 * \c symbol_vbits[nsymbols] and \c symbol_vbase[nsymbols] are the number of
564 * value bits to read and the base value for each symbol.
565 * Some symbols may have a 0 frequency. In that case they should not be
566 * present in the data.
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 /* LZFSE_FSE_H */