lzfse 0.0.1.pre.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,218 @@
1
+ /*
2
+ Copyright (c) 2015-2016, Apple Inc. All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
9
+ in the documentation and/or other materials provided with the distribution.
10
+
11
+ 3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived
12
+ from this software without specific prior written permission.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
16
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20
+ */
21
+
22
+ #ifndef LZFSE_ENCODE_TABLES_H
23
+ #define LZFSE_ENCODE_TABLES_H
24
+
25
+ #if defined(_MSC_VER) && !defined(__clang__)
26
+ # define inline __inline
27
+ #endif
28
+
29
+ static inline uint8_t l_base_from_value(int32_t value) {
30
+ static const uint8_t sym[LZFSE_ENCODE_MAX_L_VALUE + 1] = {
31
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16,
32
+ 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18,
33
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
34
+ 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
35
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
36
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
37
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
38
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
39
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
40
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
41
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
42
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
43
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
44
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
45
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
46
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
47
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
48
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19};
49
+ return sym[value];
50
+ }
51
+ static inline uint8_t m_base_from_value(int32_t value) {
52
+ static const uint8_t sym[LZFSE_ENCODE_MAX_M_VALUE + 1] = {
53
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16,
54
+ 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
55
+ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
56
+ 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
57
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
58
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
59
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
60
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
61
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
62
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
63
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
64
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
65
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
66
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
67
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
68
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
69
+ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
70
+ 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
71
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
72
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
73
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
74
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
75
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
76
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
77
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
78
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
79
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
80
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
81
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
82
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
83
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
84
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
85
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
86
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
87
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
88
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
89
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
90
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
91
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
92
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
93
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
94
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
95
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
96
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
97
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
98
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
99
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
100
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
101
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
102
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
103
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
104
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
105
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
106
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
107
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
108
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
109
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
110
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
111
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
112
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
113
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
114
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
115
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
116
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
117
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
118
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
119
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
120
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
121
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
122
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
123
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
124
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
125
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
126
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
127
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
128
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
129
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
130
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
131
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
132
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
133
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
134
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
135
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
136
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
137
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
138
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
139
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
140
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
141
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
142
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
143
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
144
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
145
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
146
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
147
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
148
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
149
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
150
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
151
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
152
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
153
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
154
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
155
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
156
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
157
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
158
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
159
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
160
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
161
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
162
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
163
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
164
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
165
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
166
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
167
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
168
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
169
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
170
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
171
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
172
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
173
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
174
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
175
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
176
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
177
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
178
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
179
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
180
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
181
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
182
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
183
+ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
184
+ 19, 19};
185
+ return sym[value];
186
+ }
187
+
188
+ static inline uint8_t d_base_from_value(int32_t value) {
189
+ static const uint8_t sym[64 * 4] = {
190
+ 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9,
191
+ 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12,
192
+ 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15,
193
+ 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17, 18, 19, 20, 20, 21, 21,
194
+ 22, 22, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27,
195
+ 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29,
196
+ 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32,
197
+ 32, 32, 32, 33, 34, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 40, 40,
198
+ 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44,
199
+ 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46,
200
+ 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 49, 50, 51, 52, 52,
201
+ 53, 53, 54, 54, 55, 55, 56, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58, 58,
202
+ 59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61,
203
+ 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, 63,
204
+ 0, 0, 0, 0};
205
+ int index = 0;
206
+ int in_range_k;
207
+ in_range_k = (value >= 0 && value < 60);
208
+ index |= (((value - 0) >> 0) + 0) & -in_range_k;
209
+ in_range_k = (value >= 60 && value < 1020);
210
+ index |= (((value - 60) >> 4) + 64) & -in_range_k;
211
+ in_range_k = (value >= 1020 && value < 16380);
212
+ index |= (((value - 1020) >> 8) + 128) & -in_range_k;
213
+ in_range_k = (value >= 16380 && value < 262140);
214
+ index |= (((value - 16380) >> 12) + 192) & -in_range_k;
215
+ return sym[index & 255];
216
+ }
217
+
218
+ #endif // LZFSE_ENCODE_TABLES_H
@@ -0,0 +1,216 @@
1
+ /*
2
+ Copyright (c) 2015-2016, Apple Inc. All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
9
+ in the documentation and/or other materials provided with the distribution.
10
+
11
+ 3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived
12
+ from this software without specific prior written permission.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
16
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20
+ */
21
+
22
+ #include "lzfse_internal.h"
23
+
24
+ // Initialize encoder table T[NSYMBOLS].
25
+ // NSTATES = sum FREQ[i] is the number of states (a power of 2)
26
+ // NSYMBOLS is the number of symbols.
27
+ // FREQ[NSYMBOLS] is a normalized histogram of symbol frequencies, with FREQ[i]
28
+ // >= 0.
29
+ // Some symbols may have a 0 frequency. In that case, they should not be
30
+ // present in the data.
31
+ void fse_init_encoder_table(int nstates, int nsymbols,
32
+ const uint16_t *__restrict freq,
33
+ fse_encoder_entry *__restrict t) {
34
+ int offset = 0; // current offset
35
+ int n_clz = __builtin_clz(nstates);
36
+ for (int i = 0; i < nsymbols; i++) {
37
+ int f = (int)freq[i];
38
+ if (f == 0)
39
+ continue; // skip this symbol, no occurrences
40
+ int k =
41
+ __builtin_clz(f) - n_clz; // shift needed to ensure N <= (F<<K) < 2*N
42
+ t[i].s0 = (int16_t)((f << k) - nstates);
43
+ t[i].k = (int16_t)k;
44
+ t[i].delta0 = (int16_t)(offset - f + (nstates >> k));
45
+ t[i].delta1 = (int16_t)(offset - f + (nstates >> (k - 1)));
46
+ offset += f;
47
+ }
48
+ }
49
+
50
+ // Initialize decoder table T[NSTATES].
51
+ // NSTATES = sum FREQ[i] is the number of states (a power of 2)
52
+ // NSYMBOLS is the number of symbols.
53
+ // FREQ[NSYMBOLS] is a normalized histogram of symbol frequencies, with FREQ[i]
54
+ // >= 0.
55
+ // Some symbols may have a 0 frequency. In that case, they should not be
56
+ // present in the data.
57
+ int fse_init_decoder_table(int nstates, int nsymbols,
58
+ const uint16_t *__restrict freq,
59
+ int32_t *__restrict t) {
60
+ assert(nsymbols <= 256);
61
+ assert(fse_check_freq(freq, nsymbols, nstates) == 0);
62
+ int n_clz = __builtin_clz(nstates);
63
+ int sum_of_freq = 0;
64
+ for (int i = 0; i < nsymbols; i++) {
65
+ int f = (int)freq[i];
66
+ if (f == 0)
67
+ continue; // skip this symbol, no occurrences
68
+
69
+ sum_of_freq += f;
70
+
71
+ if (sum_of_freq > nstates) {
72
+ return -1;
73
+ }
74
+
75
+ int k =
76
+ __builtin_clz(f) - n_clz; // shift needed to ensure N <= (F<<K) < 2*N
77
+ int j0 = ((2 * nstates) >> k) - f;
78
+
79
+ // Initialize all states S reached by this symbol: OFFSET <= S < OFFSET + F
80
+ for (int j = 0; j < f; j++) {
81
+ fse_decoder_entry e;
82
+
83
+ e.symbol = (uint8_t)i;
84
+ if (j < j0) {
85
+ e.k = (int8_t)k;
86
+ e.delta = (int16_t)(((f + j) << k) - nstates);
87
+ } else {
88
+ e.k = (int8_t)(k - 1);
89
+ e.delta = (int16_t)((j - j0) << (k - 1));
90
+ }
91
+
92
+ memcpy(t, &e, sizeof(e));
93
+ t++;
94
+ }
95
+ }
96
+
97
+ return 0; // OK
98
+ }
99
+
100
+ // Initialize value decoder table T[NSTATES].
101
+ // NSTATES = sum FREQ[i] is the number of states (a power of 2)
102
+ // NSYMBOLS is the number of symbols.
103
+ // FREQ[NSYMBOLS] is a normalized histogram of symbol frequencies, with FREQ[i]
104
+ // >= 0.
105
+ // SYMBOL_VBITS[NSYMBOLS] and SYMBOLS_VBASE[NSYMBOLS] are the number of value
106
+ // bits to read and the base value for each symbol.
107
+ // Some symbols may have a 0 frequency. In that case, they should not be
108
+ // present in the data.
109
+ void fse_init_value_decoder_table(int nstates, int nsymbols,
110
+ const uint16_t *__restrict freq,
111
+ const uint8_t *__restrict symbol_vbits,
112
+ const int32_t *__restrict symbol_vbase,
113
+ fse_value_decoder_entry *__restrict t) {
114
+ assert(nsymbols <= 256);
115
+ assert(fse_check_freq(freq, nsymbols, nstates) == 0);
116
+
117
+ int n_clz = __builtin_clz(nstates);
118
+ for (int i = 0; i < nsymbols; i++) {
119
+ int f = (int)freq[i];
120
+ if (f == 0)
121
+ continue; // skip this symbol, no occurrences
122
+
123
+ int k =
124
+ __builtin_clz(f) - n_clz; // shift needed to ensure N <= (F<<K) < 2*N
125
+ int j0 = ((2 * nstates) >> k) - f;
126
+
127
+ fse_value_decoder_entry ei = {0};
128
+ ei.value_bits = symbol_vbits[i];
129
+ ei.vbase = symbol_vbase[i];
130
+
131
+ // Initialize all states S reached by this symbol: OFFSET <= S < OFFSET + F
132
+ for (int j = 0; j < f; j++) {
133
+ fse_value_decoder_entry e = ei;
134
+
135
+ if (j < j0) {
136
+ e.total_bits = (uint8_t)k + e.value_bits;
137
+ e.delta = (int16_t)(((f + j) << k) - nstates);
138
+ } else {
139
+ e.total_bits = (uint8_t)(k - 1) + e.value_bits;
140
+ e.delta = (int16_t)((j - j0) << (k - 1));
141
+ }
142
+
143
+ memcpy(t, &e, 8);
144
+ t++;
145
+ }
146
+ }
147
+ }
148
+
149
+ // Remove states from symbols until the correct number of states is used.
150
+ static void fse_adjust_freqs(uint16_t *freq, int overrun, int nsymbols) {
151
+ for (int shift = 3; overrun != 0; shift--) {
152
+ for (int sym = 0; sym < nsymbols; sym++) {
153
+ if (freq[sym] > 1) {
154
+ int n = (freq[sym] - 1) >> shift;
155
+ if (n > overrun)
156
+ n = overrun;
157
+ freq[sym] -= n;
158
+ overrun -= n;
159
+ if (overrun == 0)
160
+ break;
161
+ }
162
+ }
163
+ }
164
+ }
165
+
166
+ // Normalize a table T[NSYMBOLS] of occurrences to FREQ[NSYMBOLS].
167
+ void fse_normalize_freq(int nstates, int nsymbols, const uint32_t *__restrict t,
168
+ uint16_t *__restrict freq) {
169
+ uint32_t s_count = 0;
170
+ int remaining = nstates; // must be signed; this may become < 0
171
+ int max_freq = 0;
172
+ int max_freq_sym = 0;
173
+ int shift = __builtin_clz(nstates) - 1;
174
+ uint32_t highprec_step;
175
+
176
+ // Compute the total number of symbol occurrences
177
+ for (int i = 0; i < nsymbols; i++)
178
+ s_count += t[i];
179
+
180
+ if (s_count == 0)
181
+ highprec_step = 0; // no symbols used
182
+ else
183
+ highprec_step = ((uint32_t)1 << 31) / s_count;
184
+
185
+ for (int i = 0; i < nsymbols; i++) {
186
+
187
+ // Rescale the occurrence count to get the normalized frequency.
188
+ // Round up if the fractional part is >= 0.5; otherwise round down.
189
+ // For efficiency, we do this calculation using integer arithmetic.
190
+ int f = (((t[i] * highprec_step) >> shift) + 1) >> 1;
191
+
192
+ // If a symbol was used, it must be given a nonzero normalized frequency.
193
+ if (f == 0 && t[i] != 0)
194
+ f = 1;
195
+
196
+ freq[i] = f;
197
+ remaining -= f;
198
+
199
+ // Remember the maximum frequency and which symbol had it.
200
+ if (f > max_freq) {
201
+ max_freq = f;
202
+ max_freq_sym = i;
203
+ }
204
+ }
205
+
206
+ // If there remain states to be assigned, then just assign them to the most
207
+ // frequent symbol. Alternatively, if we assigned more states than were
208
+ // actually available, then either remove states from the most frequent symbol
209
+ // (for minor overruns) or use the slower adjustment algorithm (for major
210
+ // overruns).
211
+ if (-remaining < (max_freq >> 2)) {
212
+ freq[max_freq_sym] += remaining;
213
+ } else {
214
+ fse_adjust_freqs(freq, -remaining, nsymbols);
215
+ }
216
+ }
@@ -0,0 +1,631 @@
1
+ /*
2
+ Copyright (c) 2015-2016, Apple Inc. All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
9
+ in the documentation and/or other materials provided with the distribution.
10
+
11
+ 3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived
12
+ from this software without specific prior written permission.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
16
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20
+ */
21
+
22
+ // Finite state entropy coding (FSE)
23
+ // This is an implementation of the tANS algorithm described by Jarek Duda,
24
+ // we use the more descriptive name "Finite State Entropy".
25
+
26
+ #pragma once
27
+
28
+ #include <assert.h>
29
+ #include <stddef.h>
30
+ #include <stdint.h>
31
+ #include <stdlib.h>
32
+ #include <string.h>
33
+
34
+ // Select between 32/64-bit I/O streams for FSE. Note that the FSE stream
35
+ // size need not match the word size of the machine, but in practice you
36
+ // want to use 64b streams on 64b systems for better performance.
37
+ #if defined(_M_AMD64) || defined(__x86_64__) || defined(__arm64__)
38
+ #define FSE_IOSTREAM_64 1
39
+ #else
40
+ #define FSE_IOSTREAM_64 0
41
+ #endif
42
+
43
+ #if defined(_MSC_VER) && !defined(__clang__)
44
+ # define FSE_INLINE __forceinline
45
+ # define inline __inline
46
+ # pragma warning(disable : 4068) // warning C4068: unknown pragma
47
+ #else
48
+ # define FSE_INLINE static inline __attribute__((__always_inline__))
49
+ #endif
50
+
51
+ // MARK: - Bit utils
52
+
53
+ /*! @abstract Signed type used to represent bit count. */
54
+ typedef int32_t fse_bit_count;
55
+
56
+ /*! @abstract Unsigned type used to represent FSE state. */
57
+ typedef uint16_t fse_state;
58
+
59
+ // Mask the NBITS lsb of X. 0 <= NBITS < 64
60
+ static inline uint64_t fse_mask_lsb64(uint64_t x, fse_bit_count nbits) {
61
+ static const uint64_t mtable[65] = {
62
+ 0x0000000000000000LLU, 0x0000000000000001LLU, 0x0000000000000003LLU,
63
+ 0x0000000000000007LLU, 0x000000000000000fLLU, 0x000000000000001fLLU,
64
+ 0x000000000000003fLLU, 0x000000000000007fLLU, 0x00000000000000ffLLU,
65
+ 0x00000000000001ffLLU, 0x00000000000003ffLLU, 0x00000000000007ffLLU,
66
+ 0x0000000000000fffLLU, 0x0000000000001fffLLU, 0x0000000000003fffLLU,
67
+ 0x0000000000007fffLLU, 0x000000000000ffffLLU, 0x000000000001ffffLLU,
68
+ 0x000000000003ffffLLU, 0x000000000007ffffLLU, 0x00000000000fffffLLU,
69
+ 0x00000000001fffffLLU, 0x00000000003fffffLLU, 0x00000000007fffffLLU,
70
+ 0x0000000000ffffffLLU, 0x0000000001ffffffLLU, 0x0000000003ffffffLLU,
71
+ 0x0000000007ffffffLLU, 0x000000000fffffffLLU, 0x000000001fffffffLLU,
72
+ 0x000000003fffffffLLU, 0x000000007fffffffLLU, 0x00000000ffffffffLLU,
73
+ 0x00000001ffffffffLLU, 0x00000003ffffffffLLU, 0x00000007ffffffffLLU,
74
+ 0x0000000fffffffffLLU, 0x0000001fffffffffLLU, 0x0000003fffffffffLLU,
75
+ 0x0000007fffffffffLLU, 0x000000ffffffffffLLU, 0x000001ffffffffffLLU,
76
+ 0x000003ffffffffffLLU, 0x000007ffffffffffLLU, 0x00000fffffffffffLLU,
77
+ 0x00001fffffffffffLLU, 0x00003fffffffffffLLU, 0x00007fffffffffffLLU,
78
+ 0x0000ffffffffffffLLU, 0x0001ffffffffffffLLU, 0x0003ffffffffffffLLU,
79
+ 0x0007ffffffffffffLLU, 0x000fffffffffffffLLU, 0x001fffffffffffffLLU,
80
+ 0x003fffffffffffffLLU, 0x007fffffffffffffLLU, 0x00ffffffffffffffLLU,
81
+ 0x01ffffffffffffffLLU, 0x03ffffffffffffffLLU, 0x07ffffffffffffffLLU,
82
+ 0x0fffffffffffffffLLU, 0x1fffffffffffffffLLU, 0x3fffffffffffffffLLU,
83
+ 0x7fffffffffffffffLLU, 0xffffffffffffffffLLU,
84
+ };
85
+ return x & mtable[nbits];
86
+ }
87
+
88
+ // Mask the NBITS lsb of X. 0 <= NBITS < 32
89
+ static inline uint32_t fse_mask_lsb32(uint32_t x, fse_bit_count nbits) {
90
+ static const uint32_t mtable[33] = {
91
+ 0x0000000000000000U, 0x0000000000000001U, 0x0000000000000003U,
92
+ 0x0000000000000007U, 0x000000000000000fU, 0x000000000000001fU,
93
+ 0x000000000000003fU, 0x000000000000007fU, 0x00000000000000ffU,
94
+ 0x00000000000001ffU, 0x00000000000003ffU, 0x00000000000007ffU,
95
+ 0x0000000000000fffU, 0x0000000000001fffU, 0x0000000000003fffU,
96
+ 0x0000000000007fffU, 0x000000000000ffffU, 0x000000000001ffffU,
97
+ 0x000000000003ffffU, 0x000000000007ffffU, 0x00000000000fffffU,
98
+ 0x00000000001fffffU, 0x00000000003fffffU, 0x00000000007fffffU,
99
+ 0x0000000000ffffffU, 0x0000000001ffffffU, 0x0000000003ffffffU,
100
+ 0x0000000007ffffffU, 0x000000000fffffffU, 0x000000001fffffffU,
101
+ 0x000000003fffffffU, 0x000000007fffffffU, 0x00000000ffffffffU,
102
+ };
103
+ return x & mtable[nbits];
104
+ }
105
+
106
+ /*! @abstract Select \c nbits at index \c start from \c x.
107
+ * 0 <= start <= start+nbits <= 64 */
108
+ FSE_INLINE uint64_t fse_extract_bits64(uint64_t x, fse_bit_count start,
109
+ fse_bit_count nbits) {
110
+ #if defined(__GNUC__)
111
+ // If START and NBITS are constants, map to bit-field extraction instructions
112
+ if (__builtin_constant_p(start) && __builtin_constant_p(nbits))
113
+ return (x >> start) & ((1LLU << nbits) - 1LLU);
114
+ #endif
115
+
116
+ // Otherwise, shift and mask
117
+ return fse_mask_lsb64(x >> start, nbits);
118
+ }
119
+
120
+ /*! @abstract Select \c nbits at index \c start from \c x.
121
+ * 0 <= start <= start+nbits <= 32 */
122
+ FSE_INLINE uint32_t fse_extract_bits32(uint32_t x, fse_bit_count start,
123
+ fse_bit_count nbits) {
124
+ #if defined(__GNUC__)
125
+ // If START and NBITS are constants, map to bit-field extraction instructions
126
+ if (__builtin_constant_p(start) && __builtin_constant_p(nbits))
127
+ return (x >> start) & ((1U << nbits) - 1U);
128
+ #endif
129
+
130
+ // Otherwise, shift and mask
131
+ return fse_mask_lsb32(x >> start, nbits);
132
+ }
133
+
134
+ // MARK: - Bit stream
135
+
136
+ // I/O streams
137
+ // The streams can be shared between several FSE encoders/decoders, which is why
138
+ // they are not in the state struct
139
+
140
+ /*! @abstract Output stream, 64-bit accum. */
141
+ typedef struct {
142
+ uint64_t accum; // Output bits
143
+ fse_bit_count accum_nbits; // Number of valid bits in ACCUM, other bits are 0
144
+ } fse_out_stream64;
145
+
146
+ /*! @abstract Output stream, 32-bit accum. */
147
+ typedef struct {
148
+ uint32_t accum; // Output bits
149
+ fse_bit_count accum_nbits; // Number of valid bits in ACCUM, other bits are 0
150
+ } fse_out_stream32;
151
+
152
+ /*! @abstract Object representing an input stream. */
153
+ typedef struct {
154
+ uint64_t accum; // Input bits
155
+ fse_bit_count accum_nbits; // Number of valid bits in ACCUM, other bits are 0
156
+ } fse_in_stream64;
157
+
158
+ /*! @abstract Object representing an input stream. */
159
+ typedef struct {
160
+ uint32_t accum; // Input bits
161
+ fse_bit_count accum_nbits; // Number of valid bits in ACCUM, other bits are 0
162
+ } fse_in_stream32;
163
+
164
+ /*! @abstract Initialize an output stream object. */
165
+ FSE_INLINE void fse_out_init64(fse_out_stream64 *s) {
166
+ s->accum = 0;
167
+ s->accum_nbits = 0;
168
+ }
169
+
170
+ /*! @abstract Initialize an output stream object. */
171
+ FSE_INLINE void fse_out_init32(fse_out_stream32 *s) {
172
+ s->accum = 0;
173
+ s->accum_nbits = 0;
174
+ }
175
+
176
+ /*! @abstract Write full bytes from the accumulator to output buffer, ensuring
177
+ * accum_nbits is in [0, 7].
178
+ * We assume we can write 8 bytes to the output buffer \c (*pbuf[0..7]) in all
179
+ * cases.
180
+ * @note *pbuf is incremented by the number of written bytes. */
181
+ FSE_INLINE void fse_out_flush64(fse_out_stream64 *s, uint8_t **pbuf) {
182
+ fse_bit_count nbits =
183
+ 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
+ assert(s->accum_nbits >= 0 && s->accum_nbits <= 7);
194
+ assert(s->accum_nbits == 64 || (s->accum >> s->accum_nbits) == 0);
195
+ }
196
+
197
+ /*! @abstract Write full bytes from the accumulator to output buffer, ensuring
198
+ * accum_nbits is in [0, 7].
199
+ * We assume we can write 4 bytes to the output buffer \c (*pbuf[0..3]) in all
200
+ * cases.
201
+ * @note *pbuf is incremented by the number of written bytes. */
202
+ FSE_INLINE void fse_out_flush32(fse_out_stream32 *s, uint8_t **pbuf) {
203
+ fse_bit_count nbits =
204
+ s->accum_nbits & -8; // number of bits written, multiple of 8
205
+
206
+ // Write 4 bytes of current accumulator
207
+ memcpy(*pbuf, &(s->accum), 4);
208
+ *pbuf += (nbits >> 3); // bytes
209
+
210
+ // Update state
211
+ s->accum >>= nbits; // remove nbits
212
+ s->accum_nbits -= nbits;
213
+
214
+ assert(s->accum_nbits >= 0 && s->accum_nbits <= 7);
215
+ assert(s->accum_nbits == 32 || (s->accum >> s->accum_nbits) == 0);
216
+ }
217
+
218
+ /*! @abstract Write the last bytes from the accumulator to output buffer,
219
+ * ensuring accum_nbits is in [-7, 0]. Bits are padded with 0 if needed.
220
+ * We assume we can write 8 bytes to the output buffer \c (*pbuf[0..7]) in all
221
+ * cases.
222
+ * @note *pbuf is incremented by the number of written bytes. */
223
+ FSE_INLINE void fse_out_finish64(fse_out_stream64 *s, uint8_t **pbuf) {
224
+ fse_bit_count nbits =
225
+ (s->accum_nbits + 7) & -8; // number of bits written, multiple of 8
226
+
227
+ // Write 8 bytes of current accumulator
228
+ memcpy(*pbuf, &(s->accum), 8);
229
+ *pbuf += (nbits >> 3); // bytes
230
+
231
+ // Update state
232
+ s->accum = 0; // remove nbits
233
+ s->accum_nbits -= nbits;
234
+
235
+ assert(s->accum_nbits >= -7 && s->accum_nbits <= 0);
236
+ }
237
+
238
+ /*! @abstract Write the last bytes from the accumulator to output buffer,
239
+ * ensuring accum_nbits is in [-7, 0]. Bits are padded with 0 if needed.
240
+ * We assume we can write 4 bytes to the output buffer \c (*pbuf[0..3]) in all
241
+ * cases.
242
+ * @note *pbuf is incremented by the number of written bytes. */
243
+ FSE_INLINE void fse_out_finish32(fse_out_stream32 *s, uint8_t **pbuf) {
244
+ fse_bit_count nbits =
245
+ (s->accum_nbits + 7) & -8; // number of bits written, multiple of 8
246
+
247
+ // Write 8 bytes of current accumulator
248
+ memcpy(*pbuf, &(s->accum), 4);
249
+ *pbuf += (nbits >> 3); // bytes
250
+
251
+ // Update state
252
+ s->accum = 0; // remove nbits
253
+ s->accum_nbits -= nbits;
254
+
255
+ assert(s->accum_nbits >= -7 && s->accum_nbits <= 0);
256
+ }
257
+
258
+ /*! @abstract Accumulate \c n bits \c b to output stream \c s. We \b must have:
259
+ * 0 <= b < 2^n, and N + s->accum_nbits <= 64.
260
+ * @note The caller must ensure out_flush is called \b before the accumulator
261
+ * overflows to more than 64 bits. */
262
+ FSE_INLINE void fse_out_push64(fse_out_stream64 *s, fse_bit_count n,
263
+ uint64_t b) {
264
+ s->accum |= b << s->accum_nbits;
265
+ s->accum_nbits += n;
266
+
267
+ assert(s->accum_nbits >= 0 && s->accum_nbits <= 64);
268
+ assert(s->accum_nbits == 64 || (s->accum >> s->accum_nbits) == 0);
269
+ }
270
+
271
+ /*! @abstract Accumulate \c n bits \c b to output stream \c s. We \b must have:
272
+ * 0 <= n < 2^n, and n + s->accum_nbits <= 32.
273
+ * @note The caller must ensure out_flush is called \b before the accumulator
274
+ * overflows to more than 32 bits. */
275
+ FSE_INLINE void fse_out_push32(fse_out_stream32 *s, fse_bit_count n,
276
+ uint32_t b) {
277
+ s->accum |= b << s->accum_nbits;
278
+ s->accum_nbits += n;
279
+
280
+ assert(s->accum_nbits >= 0 && s->accum_nbits <= 32);
281
+ assert(s->accum_nbits == 32 || (s->accum >> s->accum_nbits) == 0);
282
+ }
283
+
284
+ #if FSE_IOSTREAM_64
285
+ #define DEBUG_CHECK_INPUT_STREAM_PARAMETERS \
286
+ assert(s->accum_nbits >= 56 && s->accum_nbits < 64); \
287
+ assert((s->accum >> s->accum_nbits) == 0);
288
+ #else
289
+ #define DEBUG_CHECK_INPUT_STREAM_PARAMETERS \
290
+ assert(s->accum_nbits >= 24 && s->accum_nbits < 32); \
291
+ assert((s->accum >> s->accum_nbits) == 0);
292
+ #endif
293
+
294
+ /*! @abstract Initialize the fse input stream so that accum holds between 56
295
+ * and 63 bits. We never want to have 64 bits in the stream, because that allows
296
+ * us to avoid a special case in the fse_in_pull function (eliminating an
297
+ * unpredictable branch), while not requiring any additional fse_flush
298
+ * operations. This is why we have the special case for n == 0 (in which case
299
+ * we want to load only 7 bytes instead of 8). */
300
+ FSE_INLINE int fse_in_checked_init64(fse_in_stream64 *s, fse_bit_count n,
301
+ const uint8_t **pbuf,
302
+ const uint8_t *buf_start) {
303
+ if (n) {
304
+ if (*pbuf < buf_start + 8)
305
+ return -1; // out of range
306
+ *pbuf -= 8;
307
+ memcpy(&(s->accum), *pbuf, 8);
308
+ s->accum_nbits = n + 64;
309
+ } else {
310
+ if (*pbuf < buf_start + 7)
311
+ return -1; // out of range
312
+ *pbuf -= 7;
313
+ memcpy(&(s->accum), *pbuf, 7);
314
+ s->accum &= 0xffffffffffffff;
315
+ s->accum_nbits = n + 56;
316
+ }
317
+
318
+ if ((s->accum_nbits < 56 || s->accum_nbits >= 64) ||
319
+ ((s->accum >> s->accum_nbits) != 0)) {
320
+ return -1; // the incoming input is wrong (encoder should have zeroed the
321
+ // upper bits)
322
+ }
323
+
324
+ return 0; // OK
325
+ }
326
+
327
+ /*! @abstract Identical to previous function, but for 32-bit operation
328
+ * (resulting bit count is between 24 and 31 bits). */
329
+ FSE_INLINE int fse_in_checked_init32(fse_in_stream32 *s, fse_bit_count n,
330
+ const uint8_t **pbuf,
331
+ const uint8_t *buf_start) {
332
+ if (n) {
333
+ if (*pbuf < buf_start + 4)
334
+ return -1; // out of range
335
+ *pbuf -= 4;
336
+ memcpy(&(s->accum), *pbuf, 4);
337
+ s->accum_nbits = n + 32;
338
+ } else {
339
+ if (*pbuf < buf_start + 3)
340
+ return -1; // out of range
341
+ *pbuf -= 3;
342
+ memcpy(&(s->accum), *pbuf, 3);
343
+ s->accum &= 0xffffff;
344
+ s->accum_nbits = n + 24;
345
+ }
346
+
347
+ if ((s->accum_nbits < 24 || s->accum_nbits >= 32) ||
348
+ ((s->accum >> s->accum_nbits) != 0)) {
349
+ return -1; // the incoming input is wrong (encoder should have zeroed the
350
+ // upper bits)
351
+ }
352
+
353
+ return 0; // OK
354
+ }
355
+
356
+ /*! @abstract Read in new bytes from buffer to ensure that we have a full
357
+ * complement of bits in the stream object (again, between 56 and 63 bits).
358
+ * checking the new value of \c *pbuf remains >= \c buf_start.
359
+ * @return 0 if OK.
360
+ * @return -1 on failure. */
361
+ FSE_INLINE int fse_in_checked_flush64(fse_in_stream64 *s, const uint8_t **pbuf,
362
+ const uint8_t *buf_start) {
363
+ // Get number of bits to add to bring us into the desired range.
364
+ fse_bit_count nbits = (63 - s->accum_nbits) & -8;
365
+ // Convert bits to bytes and decrement buffer address, then load new data.
366
+ const uint8_t *buf = (*pbuf) - (nbits >> 3);
367
+ if (buf < buf_start) {
368
+ return -1; // out of range
369
+ }
370
+ *pbuf = buf;
371
+ uint64_t incoming;
372
+ memcpy(&incoming, buf, 8);
373
+ // Update the state object and verify its validity (in DEBUG).
374
+ s->accum = (s->accum << nbits) | fse_mask_lsb64(incoming, nbits);
375
+ s->accum_nbits += nbits;
376
+ DEBUG_CHECK_INPUT_STREAM_PARAMETERS
377
+ return 0; // OK
378
+ }
379
+
380
+ /*! @abstract Identical to previous function (but again, we're only filling
381
+ * a 32-bit field with between 24 and 31 bits). */
382
+ FSE_INLINE int fse_in_checked_flush32(fse_in_stream32 *s, const uint8_t **pbuf,
383
+ const uint8_t *buf_start) {
384
+ // Get number of bits to add to bring us into the desired range.
385
+ fse_bit_count nbits = (31 - s->accum_nbits) & -8;
386
+
387
+ if (nbits > 0) {
388
+ // Convert bits to bytes and decrement buffer address, then load new data.
389
+ const uint8_t *buf = (*pbuf) - (nbits >> 3);
390
+ if (buf < buf_start) {
391
+ return -1; // out of range
392
+ }
393
+
394
+ *pbuf = buf;
395
+
396
+ uint32_t incoming = *((uint32_t *)buf);
397
+
398
+ // Update the state object and verify its validity (in DEBUG).
399
+ s->accum = (s->accum << nbits) | fse_mask_lsb32(incoming, nbits);
400
+ s->accum_nbits += nbits;
401
+ }
402
+ DEBUG_CHECK_INPUT_STREAM_PARAMETERS
403
+ return 0; // OK
404
+ }
405
+
406
+ /*! @abstract Pull n bits out of the fse stream object. */
407
+ FSE_INLINE uint64_t fse_in_pull64(fse_in_stream64 *s, fse_bit_count n) {
408
+ assert(n >= 0 && n <= s->accum_nbits);
409
+ s->accum_nbits -= n;
410
+ uint64_t result = s->accum >> s->accum_nbits;
411
+ s->accum = fse_mask_lsb64(s->accum, s->accum_nbits);
412
+ return result;
413
+ }
414
+
415
+ /*! @abstract Pull n bits out of the fse stream object. */
416
+ FSE_INLINE uint32_t fse_in_pull32(fse_in_stream32 *s, fse_bit_count n) {
417
+ assert(n >= 0 && n <= s->accum_nbits);
418
+ s->accum_nbits -= n;
419
+ uint32_t result = s->accum >> s->accum_nbits;
420
+ s->accum = fse_mask_lsb32(s->accum, s->accum_nbits);
421
+ return result;
422
+ }
423
+
424
+ // MARK: - Encode/Decode
425
+
426
+ // Map to 32/64-bit implementations and types for I/O
427
+ #if FSE_IOSTREAM_64
428
+
429
+ typedef uint64_t fse_bits;
430
+ typedef fse_out_stream64 fse_out_stream;
431
+ typedef fse_in_stream64 fse_in_stream;
432
+ #define fse_mask_lsb fse_mask_lsb64
433
+ #define fse_extract_bits fse_extract_bits64
434
+ #define fse_out_init fse_out_init64
435
+ #define fse_out_flush fse_out_flush64
436
+ #define fse_out_finish fse_out_finish64
437
+ #define fse_out_push fse_out_push64
438
+ #define fse_in_init fse_in_checked_init64
439
+ #define fse_in_checked_init fse_in_checked_init64
440
+ #define fse_in_flush fse_in_checked_flush64
441
+ #define fse_in_checked_flush fse_in_checked_flush64
442
+ #define fse_in_flush2(_unused, _parameters, _unused2) 0 /* nothing */
443
+ #define fse_in_checked_flush2(_unused, _parameters) /* nothing */
444
+ #define fse_in_pull fse_in_pull64
445
+
446
+ #else
447
+
448
+ typedef uint32_t fse_bits;
449
+ typedef fse_out_stream32 fse_out_stream;
450
+ typedef fse_in_stream32 fse_in_stream;
451
+ #define fse_mask_lsb fse_mask_lsb32
452
+ #define fse_extract_bits fse_extract_bits32
453
+ #define fse_out_init fse_out_init32
454
+ #define fse_out_flush fse_out_flush32
455
+ #define fse_out_finish fse_out_finish32
456
+ #define fse_out_push fse_out_push32
457
+ #define fse_in_init fse_in_checked_init32
458
+ #define fse_in_checked_init fse_in_checked_init32
459
+ #define fse_in_flush fse_in_checked_flush32
460
+ #define fse_in_checked_flush fse_in_checked_flush32
461
+ #define fse_in_flush2 fse_in_checked_flush32
462
+ #define fse_in_checked_flush2 fse_in_checked_flush32
463
+ #define fse_in_pull fse_in_pull32
464
+
465
+ #endif
466
+
467
+ /*! @abstract Entry for one symbol in the encoder table (64b). */
468
+ typedef struct {
469
+ int16_t s0; // First state requiring a K-bit shift
470
+ int16_t k; // States S >= S0 are shifted K bits. States S < S0 are
471
+ // shifted K-1 bits
472
+ int16_t delta0; // Relative increment used to compute next state if S >= S0
473
+ int16_t delta1; // Relative increment used to compute next state if S < S0
474
+ } fse_encoder_entry;
475
+
476
+ /*! @abstract Entry for one state in the decoder table (32b). */
477
+ typedef struct { // DO NOT REORDER THE FIELDS
478
+ int8_t k; // Number of bits to read
479
+ uint8_t symbol; // Emitted symbol
480
+ int16_t delta; // Signed increment used to compute next state (+bias)
481
+ } fse_decoder_entry;
482
+
483
+ /*! @abstract Entry for one state in the value decoder table (64b). */
484
+ typedef struct { // DO NOT REORDER THE FIELDS
485
+ uint8_t total_bits; // state bits + extra value bits = shift for next decode
486
+ uint8_t value_bits; // extra value bits
487
+ int16_t delta; // state base (delta)
488
+ int32_t vbase; // value base
489
+ } fse_value_decoder_entry;
490
+
491
+ /*! @abstract Encode SYMBOL using the encoder table, and update \c *pstate,
492
+ * \c out.
493
+ * @note The caller must ensure we have enough bits available in the output
494
+ * stream accumulator. */
495
+ FSE_INLINE void fse_encode(fse_state *__restrict pstate,
496
+ const fse_encoder_entry *__restrict encoder_table,
497
+ fse_out_stream *__restrict out, uint8_t symbol) {
498
+ int s = *pstate;
499
+ fse_encoder_entry e = encoder_table[symbol];
500
+ int s0 = e.s0;
501
+ int k = e.k;
502
+ int delta0 = e.delta0;
503
+ int delta1 = e.delta1;
504
+
505
+ // Number of bits to write
506
+ int hi = s >= s0;
507
+ fse_bit_count nbits = hi ? k : (k - 1);
508
+ fse_state delta = hi ? delta0 : delta1;
509
+
510
+ // Write lower NBITS of state
511
+ fse_bits b = fse_mask_lsb(s, nbits);
512
+ fse_out_push(out, nbits, b);
513
+
514
+ // Update state with remaining bits and delta
515
+ *pstate = delta + (s >> nbits);
516
+ }
517
+
518
+ /*! @abstract Decode and return symbol using the decoder table, and update
519
+ * \c *pstate, \c in.
520
+ * @note The caller must ensure we have enough bits available in the input
521
+ * stream accumulator. */
522
+ FSE_INLINE uint8_t fse_decode(fse_state *__restrict pstate,
523
+ const int32_t *__restrict decoder_table,
524
+ fse_in_stream *__restrict in) {
525
+ int32_t e = decoder_table[*pstate];
526
+
527
+ // Update state from K bits of input + DELTA
528
+ *pstate = (fse_state)(e >> 16) + (fse_state)fse_in_pull(in, e & 0xff);
529
+
530
+ // Return the symbol for this state
531
+ return fse_extract_bits(e, 8, 8); // symbol
532
+ }
533
+
534
+ /*! @abstract Decode and return value using the decoder table, and update \c
535
+ * *pstate, \c in.
536
+ * \c value_decoder_table[nstates]
537
+ * @note The caller must ensure we have enough bits available in the input
538
+ * stream accumulator. */
539
+ FSE_INLINE int32_t
540
+ fse_value_decode(fse_state *__restrict pstate,
541
+ const fse_value_decoder_entry *value_decoder_table,
542
+ fse_in_stream *__restrict in) {
543
+ fse_value_decoder_entry entry = value_decoder_table[*pstate];
544
+ uint32_t state_and_value_bits = (uint32_t)fse_in_pull(in, entry.total_bits);
545
+ *pstate =
546
+ (fse_state)(entry.delta + (state_and_value_bits >> entry.value_bits));
547
+ return (int32_t)(entry.vbase +
548
+ fse_mask_lsb(state_and_value_bits, entry.value_bits));
549
+ }
550
+
551
+ // MARK: - Tables
552
+
553
+ // IMPORTANT: To properly decode an FSE encoded stream, both encoder/decoder
554
+ // tables shall be initialized with the same parameters, including the
555
+ // FREQ[NSYMBOL] array.
556
+ //
557
+
558
+ /*! @abstract Sanity check on frequency table, verify sum of \c freq
559
+ * is <= \c number_of_states. */
560
+ FSE_INLINE int fse_check_freq(const uint16_t *freq_table,
561
+ const size_t table_size,
562
+ const size_t number_of_states) {
563
+ size_t sum_of_freq = 0;
564
+ for (int i = 0; i < table_size; i++) {
565
+ sum_of_freq += freq_table[i];
566
+ }
567
+ return (sum_of_freq > number_of_states) ? -1 : 0;
568
+ }
569
+
570
+ /*! @abstract Initialize encoder table \c t[nsymbols].
571
+ *
572
+ * @param nstates
573
+ * sum \c freq[i]; the number of states (a power of 2).
574
+ *
575
+ * @param nsymbols
576
+ * the number of symbols.
577
+ *
578
+ * @param freq[nsymbols]
579
+ * is a normalized histogram of symbol frequencies, with \c freq[i] >= 0.
580
+ * Some symbols may have a 0 frequency. In that case they should not be
581
+ * present in the data.
582
+ */
583
+ void fse_init_encoder_table(int nstates, int nsymbols,
584
+ const uint16_t *__restrict freq,
585
+ fse_encoder_entry *__restrict t);
586
+
587
+ /*! @abstract Initialize decoder table \c t[nstates].
588
+ *
589
+ * @param nstates
590
+ * sum \c freq[i]; the number of states (a power of 2).
591
+ *
592
+ * @param nsymbols
593
+ * the number of symbols.
594
+ *
595
+ * @param feq[nsymbols]
596
+ * a normalized histogram of symbol frequencies, with \c freq[i] >= 0.
597
+ * Some symbols may have a 0 frequency. In that case they should not be
598
+ * present in the data.
599
+ *
600
+ * @return 0 if OK.
601
+ * @return -1 on failure.
602
+ */
603
+ int fse_init_decoder_table(int nstates, int nsymbols,
604
+ const uint16_t *__restrict freq,
605
+ int32_t *__restrict t);
606
+
607
+ /*! @abstract Initialize value decoder table \c t[nstates].
608
+ *
609
+ * @param nstates
610
+ * sum \cfreq[i]; the number of states (a power of 2).
611
+ *
612
+ * @param nsymbols
613
+ * the number of symbols.
614
+ *
615
+ * @param freq[nsymbols]
616
+ * a normalized histogram of symbol frequencies, with \c freq[i] >= 0.
617
+ * \c symbol_vbits[nsymbols] and \c symbol_vbase[nsymbols] are the number of
618
+ * value bits to read and the base value for each symbol.
619
+ * Some symbols may have a 0 frequency. In that case they should not be
620
+ * present in the data.
621
+ */
622
+ void fse_init_value_decoder_table(int nstates, int nsymbols,
623
+ const uint16_t *__restrict freq,
624
+ const uint8_t *__restrict symbol_vbits,
625
+ const int32_t *__restrict symbol_vbase,
626
+ fse_value_decoder_entry *__restrict t);
627
+
628
+ /*! @abstract Normalize a table \c t[nsymbols] of occurrences to
629
+ * \c freq[nsymbols]. */
630
+ void fse_normalize_freq(int nstates, int nsymbols, const uint32_t *__restrict t,
631
+ uint16_t *__restrict freq);