forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberCompress.h
315 lines (266 loc) · 8.73 KB
/
NumberCompress.h
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NumberCompress_H
#define NumberCompress_H
#include <stdint.h>
/* right now 3 implementations:
* - fixed 4 bits: 15 peers
* - fixed 8 bits: 240 peers
* - dynamically 4-10 bits: 256 peers
*/
#ifndef CJDNS_MAX_PEERS
#error CJDNS_MAX_PEERS needs to be defined.
#endif
#if CJDNS_MAX_PEERS > 256
#error "Not more than 256 peers supported"
#endif
/* public interface */
static inline uint32_t NumberCompress_bitsUsedForLabel(const uint64_t label);
static inline uint32_t NumberCompress_bitsUsedForNumber(const uint32_t number);
static inline uint64_t NumberCompress_getCompressed(const uint32_t number,
const uint32_t bitsUsed);
static inline uint32_t NumberCompress_getDecompressed(const uint64_t label,
const uint32_t bitsUsed);
// also a constant NumberCompress_INTERFACES defining the maximum number of interfaces
/* Number 1 is always encoded as the 4 bits 0001; and each label ending in 0001 is decoded as
* 4 bit number 1
* This is required by the protocol. */
/* implementations - select one based on CJDNS_MAX_PEERS
*
* they are all accesible with their internal names for unit testing
*/
/**********************
* Fixed 4 bit scheme: 15 peers + 1 router
**********************/
# define NumberCompress_4_INTERFACES 16
static inline uint32_t NumberCompress_4_bitsUsedForLabel(const uint64_t label)
{
return 4;
}
static inline uint32_t NumberCompress_4_bitsUsedForNumber(const uint32_t number)
{
return 4;
}
static inline uint64_t NumberCompress_4_getCompressed(const uint32_t number,
const uint32_t bitsUsed)
{
return number;
}
static inline uint32_t NumberCompress_4_getDecompressed(const uint64_t label,
const uint32_t bitsUsed)
{
return label & 0xf;
}
/**********************
* (Fixed) 8 bit scheme: 240 peers + 1 router
**********************/
// Basic idea: encode number XXXXYYYY as YYYY(XXXX)'; (XXXX)' is XXXX+1 if XXXX != 0, otherwise XXXX
// that way XXXX is never 1
// so map numbers 16-239 -> 32-255, and 240 <-> 1, then swap nibbles
# define NumberCompress_8_INTERFACES 241
static inline uint32_t NumberCompress_8_bitsUsedForLabel(const uint64_t label)
{
if (1 == (label & 0xf)) {
return 4;
}
return 8;
}
static inline uint32_t NumberCompress_8_bitsUsedForNumber(const uint32_t number)
{
if (1 == number) {
return 4;
}
return 8;
}
static inline uint64_t NumberCompress_8_getCompressed(const uint32_t number,
const uint32_t bitsUsed)
{
if (1 == number) {
return 1;
}
if (240 == number) {
return 0x10;
}
uint32_t low = number & 0xf;
uint32_t high = (number >> 4) & 0xf;
if (high > 0) {
++high;
}
return (low << 4) + high;
}
static inline uint32_t NumberCompress_8_getDecompressed(const uint64_t label,
const uint32_t bitsUsed)
{
uint32_t low = label & 0xf;
if (1 == low) {
return 1;
}
if (0x10 == (label & 0xff)) {
return 240;
}
uint32_t high = (label >> 4) & 0xf;
if (low > 0) {
--low; // low != 1
}
return (low << 4) + high;
}
/*
* Dynamic number compression scheme:
*
* scheme data suffix range bits used
* route 0001 1 4 (number 1 always encoded as 0001)
* 0 000-111 1 0-7 4 (swapped number 0 and 1)
* 1 00000-11111 10 0-32 7 (skip the number 1)
* 2 00000000-11111111 00 0-256 10 (skip the number 1)
*/
# define NumberCompress_dyn_INTERFACES 257
static inline uint32_t NumberCompress_dyn_bitsUsedForLabel(const uint64_t label)
{
if (0 != (label & 0x1)) {
return 4;
}
if (0 != (label & 0x2)) {
return 7;
}
return 10;
}
static inline uint32_t NumberCompress_dyn_bitsUsedForNumber(const uint32_t number)
{
if (number < 8) {
return 4;
} else if (number < 33) {
return 7;
} else {
return 10;
}
}
static inline uint64_t NumberCompress_dyn_getCompressed(const uint32_t number,
const uint32_t bitsUsed)
{
if (1 == number) {
return 1;
}
switch (bitsUsed) {
case 4:
if (0 == number) {
return 3;
}
return (number << 1) | 1;
case 7:
if (0 == number) {
return 2;
}
// skip the number 1
return ((number-1) << 2) | 2;
case 10:
if (0 == number) {
return 0;
}
// skip the number 1
return ((number-1) << 2);
default: return 0;
}
}
static inline uint32_t NumberCompress_dyn_getDecompressed(const uint64_t label,
const uint32_t bitsUsed)
{
uint32_t number;
switch (bitsUsed) {
case 4:
number = (label >> 1) & 0x7u;
if (0 == number) {
return 1;
}
if (1 == number) {
return 0;
}
return number;
case 7:
number = (label >> 2) & 0x1fu;
if (0 != number) {
++number; // skip the number 1
}
return number;
case 10:
number = (label >> 2) & 0xffu;
if (0 != number) {
++number; // skip the number 1
}
return number;
default: return 0;
}
}
#if CJDNS_MAX_PEERS < 16
# define NumberCompress_INTERFACES NumberCompress_4_INTERFACES
static inline uint32_t NumberCompress_bitsUsedForLabel(const uint64_t label)
{
return NumberCompress_4_bitsUsedForLabel(label);
}
static inline uint32_t NumberCompress_bitsUsedForNumber(const uint32_t number)
{
return NumberCompress_4_bitsUsedForNumber(number);
}
static inline uint64_t NumberCompress_getCompressed(const uint32_t number,
const uint32_t bitsUsed)
{
return NumberCompress_4_getCompressed(number, bitsUsed);
}
static inline uint32_t NumberCompress_getDecompressed(const uint64_t label,
const uint32_t bitsUsed)
{
return NumberCompress_4_getDecompressed(label, bitsUsed);
}
#elif CJDNS_MAX_PEERS < 241
# define NumberCompress_INTERFACES NumberCompress_8_INTERFACES
static inline uint32_t NumberCompress_bitsUsedForLabel(const uint64_t label)
{
return NumberCompress_8_bitsUsedForLabel(label);
}
static inline uint32_t NumberCompress_bitsUsedForNumber(const uint32_t number)
{
return NumberCompress_8_bitsUsedForNumber(number);
}
static inline uint64_t NumberCompress_getCompressed(const uint32_t number,
const uint32_t bitsUsed)
{
return NumberCompress_8_getCompressed(number, bitsUsed);
}
static inline uint32_t NumberCompress_getDecompressed(const uint64_t label,
const uint32_t bitsUsed)
{
return NumberCompress_8_getDecompressed(label, bitsUsed);
}
#else
# define NumberCompress_INTERFACES NumberCompress_dyn_INTERFACES
static inline uint32_t NumberCompress_bitsUsedForLabel(const uint64_t label)
{
return NumberCompress_dyn_bitsUsedForLabel(label);
}
static inline uint32_t NumberCompress_bitsUsedForNumber(const uint32_t number)
{
return NumberCompress_dyn_bitsUsedForNumber(number);
}
static inline uint64_t NumberCompress_getCompressed(const uint32_t number,
const uint32_t bitsUsed)
{
return NumberCompress_dyn_getCompressed(number, bitsUsed);
}
static inline uint32_t NumberCompress_getDecompressed(const uint64_t label,
const uint32_t bitsUsed)
{
return NumberCompress_dyn_getDecompressed(label, bitsUsed);
}
#endif
#endif