Debian Codegraph @

lzfse/lzfse_fse.c

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 #include "lzfse_internal.h"
24
25 /*
26 * Initialize decoder table T[NSTATES].
27 * NSTATES = sum FREQ[i] is the number of states (a power of 2)
28 * NSYMBOLS is the number of symbols.
29 * FREQ[NSYMBOLS] is a normalized histogram of symbol frequencies, with FREQ[i]
30 * >= 0.
31 * Some symbols may have a 0 frequency. In that case, they should not be
32 * present in the data.
33 */
34 int fse_init_decoder_table(int nstates, int nsymbols, const uint16_t *__restrict freq,
35 int32_t *__restrict t)
36 {
37 int n_clz = __builtin_clz(nstates);
38 int sum_of_freq = 0;
39 int i, j0, j;
40
41 for (i = 0; i < nsymbols; i++) {
42 int f = (int)freq[i];
43 int k;
44
45 if (f == 0)
46 continue; /* skip this symbol, no occurrences */
47
48 sum_of_freq += f;
49
50 if (sum_of_freq > nstates)
51 return -1;
52
53 k = __builtin_clz(f) - n_clz; /* shift needed to ensure N <= (F<<K) < 2*N */
54 j0 = ((2 * nstates) >> k) - f;
55
56 /* Initialize all states S reached by this symbol: OFFSET <= S < OFFSET + F */
57 for (j = 0; j < f; j++) {
58 fse_decoder_entry e;
59
60 e.symbol = (uint8_t)i;
61 if (j < j0) {
62 e.k = (int8_t)k;
63 e.delta = (int16_t)(((f + j) << k) - nstates);
64 } else {
65 e.k = (int8_t)(k - 1);
66 e.delta = (int16_t)((j - j0) << (k - 1));
67 }
68
69 memcpy(t, &e, sizeof(e));
70 t++;
71 }
72 }
73
74 return 0; /* OK */
75 }
76
77 /*
78 * Initialize value decoder table T[NSTATES].
79 * NSTATES = sum FREQ[i] is the number of states (a power of 2)
80 * NSYMBOLS is the number of symbols.
81 * FREQ[NSYMBOLS] is a normalized histogram of symbol frequencies, with FREQ[i]
82 * >= 0.
83 * SYMBOL_VBITS[NSYMBOLS] and SYMBOLS_VBASE[NSYMBOLS] are the number of value
84 * bits to read and the base value for each symbol.
85 * Some symbols may have a 0 frequency. In that case, they should not be
86 * present in the data.
87 */
88 void fse_init_value_decoder_table(int nstates, int nsymbols, const uint16_t *__restrict freq,
89 const uint8_t *__restrict symbol_vbits,
90 const int32_t *__restrict symbol_vbase,
91 struct fse_value_decoder_entry *__restrict t)
92 {
93 int n_clz = __builtin_clz(nstates);
94 int i;
95
96 for (i = 0; i < nsymbols; i++) {
97 struct fse_value_decoder_entry ei = { 0 };
98 int f = (int)freq[i];
99 int k, j0, j;
100
101 if (f == 0)
102 continue; /* skip this symbol, no occurrences */
103
104 k = __builtin_clz(f) - n_clz; /* shift needed to ensure N <= (F<<K) < 2*N */
105 j0 = ((2 * nstates) >> k) - f;
106
107 ei.value_bits = symbol_vbits[i];
108 ei.vbase = symbol_vbase[i];
109
110 /* Initialize all states S reached by this symbol: OFFSET <= S < OFFSET + F */
111 for (j = 0; j < f; j++) {
112 struct fse_value_decoder_entry e = ei;
113
114 if (j < j0) {
115 e.total_bits = (uint8_t)k + e.value_bits;
116 e.delta = (int16_t)(((f + j) << k) - nstates);
117 } else {
118 e.total_bits = (uint8_t)(k - 1) + e.value_bits;
119 e.delta = (int16_t)((j - j0) << (k - 1));
120 }
121
122 memcpy(t, &e, 8);
123 t++;
124 }
125 }
126 }