• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0
2 // ----------------------------------------------------------------------------
3 // Copyright 2011-2021 Arm Limited
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 // use this file except in compliance with the License. You may obtain a copy
7 // of the License at:
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 // License for the specific language governing permissions and limitations
15 // under the License.
16 // ----------------------------------------------------------------------------
17 
18 /**
19  * @brief Functions for encoding/decoding Bounded Integer Sequence Encoding.
20  */
21 
22 #include "astcenc_internal.h"
23 
24 #include <array>
25 
26 /** @brief Unpacked quint triplets <low,middle,high> for each packed value */
27 static const uint8_t quints_of_integer[128][3] {
28 	{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0},
29 	{4, 0, 0}, {0, 4, 0}, {4, 4, 0}, {4, 4, 4},
30 	{0, 1, 0}, {1, 1, 0}, {2, 1, 0}, {3, 1, 0},
31 	{4, 1, 0}, {1, 4, 0}, {4, 4, 1}, {4, 4, 4},
32 	{0, 2, 0}, {1, 2, 0}, {2, 2, 0}, {3, 2, 0},
33 	{4, 2, 0}, {2, 4, 0}, {4, 4, 2}, {4, 4, 4},
34 	{0, 3, 0}, {1, 3, 0}, {2, 3, 0}, {3, 3, 0},
35 	{4, 3, 0}, {3, 4, 0}, {4, 4, 3}, {4, 4, 4},
36 	{0, 0, 1}, {1, 0, 1}, {2, 0, 1}, {3, 0, 1},
37 	{4, 0, 1}, {0, 4, 1}, {4, 0, 4}, {0, 4, 4},
38 	{0, 1, 1}, {1, 1, 1}, {2, 1, 1}, {3, 1, 1},
39 	{4, 1, 1}, {1, 4, 1}, {4, 1, 4}, {1, 4, 4},
40 	{0, 2, 1}, {1, 2, 1}, {2, 2, 1}, {3, 2, 1},
41 	{4, 2, 1}, {2, 4, 1}, {4, 2, 4}, {2, 4, 4},
42 	{0, 3, 1}, {1, 3, 1}, {2, 3, 1}, {3, 3, 1},
43 	{4, 3, 1}, {3, 4, 1}, {4, 3, 4}, {3, 4, 4},
44 	{0, 0, 2}, {1, 0, 2}, {2, 0, 2}, {3, 0, 2},
45 	{4, 0, 2}, {0, 4, 2}, {2, 0, 4}, {3, 0, 4},
46 	{0, 1, 2}, {1, 1, 2}, {2, 1, 2}, {3, 1, 2},
47 	{4, 1, 2}, {1, 4, 2}, {2, 1, 4}, {3, 1, 4},
48 	{0, 2, 2}, {1, 2, 2}, {2, 2, 2}, {3, 2, 2},
49 	{4, 2, 2}, {2, 4, 2}, {2, 2, 4}, {3, 2, 4},
50 	{0, 3, 2}, {1, 3, 2}, {2, 3, 2}, {3, 3, 2},
51 	{4, 3, 2}, {3, 4, 2}, {2, 3, 4}, {3, 3, 4},
52 	{0, 0, 3}, {1, 0, 3}, {2, 0, 3}, {3, 0, 3},
53 	{4, 0, 3}, {0, 4, 3}, {0, 0, 4}, {1, 0, 4},
54 	{0, 1, 3}, {1, 1, 3}, {2, 1, 3}, {3, 1, 3},
55 	{4, 1, 3}, {1, 4, 3}, {0, 1, 4}, {1, 1, 4},
56 	{0, 2, 3}, {1, 2, 3}, {2, 2, 3}, {3, 2, 3},
57 	{4, 2, 3}, {2, 4, 3}, {0, 2, 4}, {1, 2, 4},
58 	{0, 3, 3}, {1, 3, 3}, {2, 3, 3}, {3, 3, 3},
59 	{4, 3, 3}, {3, 4, 3}, {0, 3, 4}, {1, 3, 4}
60 };
61 
62 /** @brief Packed quint values for each unpacked value, indexed [hi][mid][lo]. */
63 static const uint8_t integer_of_quints[5][5][5] {
64 	{
65 		{0, 1, 2, 3, 4},
66 		{8, 9, 10, 11, 12},
67 		{16, 17, 18, 19, 20},
68 		{24, 25, 26, 27, 28},
69 		{5, 13, 21, 29, 6}
70 	},
71 	{
72 		{32, 33, 34, 35, 36},
73 		{40, 41, 42, 43, 44},
74 		{48, 49, 50, 51, 52},
75 		{56, 57, 58, 59, 60},
76 		{37, 45, 53, 61, 14}
77 	},
78 	{
79 		{64, 65, 66, 67, 68},
80 		{72, 73, 74, 75, 76},
81 		{80, 81, 82, 83, 84},
82 		{88, 89, 90, 91, 92},
83 		{69, 77, 85, 93, 22}
84 	},
85 	{
86 		{96, 97, 98, 99, 100},
87 		{104, 105, 106, 107, 108},
88 		{112, 113, 114, 115, 116},
89 		{120, 121, 122, 123, 124},
90 		{101, 109, 117, 125, 30}
91 	},
92 	{
93 		{102, 103, 70, 71, 38},
94 		{110, 111, 78, 79, 46},
95 		{118, 119, 86, 87, 54},
96 		{126, 127, 94, 95, 62},
97 		{39, 47, 55, 63, 31}
98 	}
99 };
100 
101 /** @brief Unpacked trit quintuplets <low,...,high> for each packed value */
102 static const uint8_t trits_of_integer[256][5] {
103 	{0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {2, 0, 0, 0, 0}, {0, 0, 2, 0, 0},
104 	{0, 1, 0, 0, 0}, {1, 1, 0, 0, 0}, {2, 1, 0, 0, 0}, {1, 0, 2, 0, 0},
105 	{0, 2, 0, 0, 0}, {1, 2, 0, 0, 0}, {2, 2, 0, 0, 0}, {2, 0, 2, 0, 0},
106 	{0, 2, 2, 0, 0}, {1, 2, 2, 0, 0}, {2, 2, 2, 0, 0}, {2, 0, 2, 0, 0},
107 	{0, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {2, 0, 1, 0, 0}, {0, 1, 2, 0, 0},
108 	{0, 1, 1, 0, 0}, {1, 1, 1, 0, 0}, {2, 1, 1, 0, 0}, {1, 1, 2, 0, 0},
109 	{0, 2, 1, 0, 0}, {1, 2, 1, 0, 0}, {2, 2, 1, 0, 0}, {2, 1, 2, 0, 0},
110 	{0, 0, 0, 2, 2}, {1, 0, 0, 2, 2}, {2, 0, 0, 2, 2}, {0, 0, 2, 2, 2},
111 	{0, 0, 0, 1, 0}, {1, 0, 0, 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{1, 2, 1, 2, 1}, {2, 2, 1, 2, 1}, {2, 1, 2, 2, 1},
158 	{0, 2, 1, 2, 2}, {1, 2, 1, 2, 2}, {2, 2, 1, 2, 2}, {2, 1, 2, 2, 2},
159 	{0, 0, 0, 1, 2}, {1, 0, 0, 1, 2}, {2, 0, 0, 1, 2}, {0, 0, 2, 1, 2},
160 	{0, 1, 0, 1, 2}, {1, 1, 0, 1, 2}, {2, 1, 0, 1, 2}, {1, 0, 2, 1, 2},
161 	{0, 2, 0, 1, 2}, {1, 2, 0, 1, 2}, {2, 2, 0, 1, 2}, {2, 0, 2, 1, 2},
162 	{0, 2, 2, 1, 2}, {1, 2, 2, 1, 2}, {2, 2, 2, 1, 2}, {2, 0, 2, 1, 2},
163 	{0, 0, 1, 1, 2}, {1, 0, 1, 1, 2}, {2, 0, 1, 1, 2}, {0, 1, 2, 1, 2},
164 	{0, 1, 1, 1, 2}, {1, 1, 1, 1, 2}, {2, 1, 1, 1, 2}, {1, 1, 2, 1, 2},
165 	{0, 2, 1, 1, 2}, {1, 2, 1, 1, 2}, {2, 2, 1, 1, 2}, {2, 1, 2, 1, 2},
166 	{0, 2, 2, 2, 2}, {1, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 1, 2, 2, 2}
167 };
168 
169 /** @brief Packed trit values for each unpacked value, indexed [hi][][][][lo]. */
170 static const uint8_t integer_of_trits[3][3][3][3][3] {
171 	{
172 		{
173 			{
174 				{0, 1, 2},
175 				{4, 5, 6},
176 				{8, 9, 10}
177 			},
178 			{
179 				{16, 17, 18},
180 				{20, 21, 22},
181 				{24, 25, 26}
182 			},
183 			{
184 				{3, 7, 15},
185 				{19, 23, 27},
186 				{12, 13, 14}
187 			}
188 		},
189 		{
190 			{
191 				{32, 33, 34},
192 				{36, 37, 38},
193 				{40, 41, 42}
194 			},
195 			{
196 				{48, 49, 50},
197 				{52, 53, 54},
198 				{56, 57, 58}
199 			},
200 			{
201 				{35, 39, 47},
202 				{51, 55, 59},
203 				{44, 45, 46}
204 			}
205 		},
206 		{
207 			{
208 				{64, 65, 66},
209 				{68, 69, 70},
210 				{72, 73, 74}
211 			},
212 			{
213 				{80, 81, 82},
214 				{84, 85, 86},
215 				{88, 89, 90}
216 			},
217 			{
218 				{67, 71, 79},
219 				{83, 87, 91},
220 				{76, 77, 78}
221 			}
222 		}
223 	},
224 	{
225 		{
226 			{
227 				{128, 129, 130},
228 				{132, 133, 134},
229 				{136, 137, 138}
230 			},
231 			{
232 				{144, 145, 146},
233 				{148, 149, 150},
234 				{152, 153, 154}
235 			},
236 			{
237 				{131, 135, 143},
238 				{147, 151, 155},
239 				{140, 141, 142}
240 			}
241 		},
242 		{
243 			{
244 				{160, 161, 162},
245 				{164, 165, 166},
246 				{168, 169, 170}
247 			},
248 			{
249 				{176, 177, 178},
250 				{180, 181, 182},
251 				{184, 185, 186}
252 			},
253 			{
254 				{163, 167, 175},
255 				{179, 183, 187},
256 				{172, 173, 174}
257 			}
258 		},
259 		{
260 			{
261 				{192, 193, 194},
262 				{196, 197, 198},
263 				{200, 201, 202}
264 			},
265 			{
266 				{208, 209, 210},
267 				{212, 213, 214},
268 				{216, 217, 218}
269 			},
270 			{
271 				{195, 199, 207},
272 				{211, 215, 219},
273 				{204, 205, 206}
274 			}
275 		}
276 	},
277 	{
278 		{
279 			{
280 				{96, 97, 98},
281 				{100, 101, 102},
282 				{104, 105, 106}
283 			},
284 			{
285 				{112, 113, 114},
286 				{116, 117, 118},
287 				{120, 121, 122}
288 			},
289 			{
290 				{99, 103, 111},
291 				{115, 119, 123},
292 				{108, 109, 110}
293 			}
294 		},
295 		{
296 			{
297 				{224, 225, 226},
298 				{228, 229, 230},
299 				{232, 233, 234}
300 			},
301 			{
302 				{240, 241, 242},
303 				{244, 245, 246},
304 				{248, 249, 250}
305 			},
306 			{
307 				{227, 231, 239},
308 				{243, 247, 251},
309 				{236, 237, 238}
310 			}
311 		},
312 		{
313 			{
314 				{28, 29, 30},
315 				{60, 61, 62},
316 				{92, 93, 94}
317 			},
318 			{
319 				{156, 157, 158},
320 				{188, 189, 190},
321 				{220, 221, 222}
322 			},
323 			{
324 				{31, 63, 127},
325 				{159, 191, 255},
326 				{252, 253, 254}
327 			}
328 		}
329 	}
330 };
331 
332 /**
333  * @brief The number of bits, trits, and quints needed for a quant level.
334  */
335 struct btq_count
336 {
337 	/** @brief The quantization level. */
338 	uint8_t quant;
339 
340 	/** @brief The number of bits. */
341 	uint8_t bits;
342 
343 	/** @brief The number of trits. */
344 	uint8_t trits;
345 
346 	/** @brief The number of quints. */
347 	uint8_t quints;
348 };
349 
350 /**
351  * @brief The table of bits, trits, and quints needed for a quant encode.
352  */
353 static const std::array<btq_count, 21> btq_counts {{
354 	{   QUANT_2, 1, 0, 0 },
355 	{   QUANT_3, 0, 1, 0 },
356 	{   QUANT_4, 2, 0, 0 },
357 	{   QUANT_5, 0, 0, 1 },
358 	{   QUANT_6, 1, 1, 0 },
359 	{   QUANT_8, 3, 0, 0 },
360 	{  QUANT_10, 1, 0, 1 },
361 	{  QUANT_12, 2, 1, 0 },
362 	{  QUANT_16, 4, 0, 0 },
363 	{  QUANT_20, 2, 0, 1 },
364 	{  QUANT_24, 3, 1, 0 },
365 	{  QUANT_32, 5, 0, 0 },
366 	{  QUANT_40, 3, 0, 1 },
367 	{  QUANT_48, 4, 1, 0 },
368 	{  QUANT_64, 6, 0, 0 },
369 	{  QUANT_80, 4, 0, 1 },
370 	{  QUANT_96, 5, 1, 0 },
371 	{ QUANT_128, 7, 0, 0 },
372 	{ QUANT_160, 5, 0, 1 },
373 	{ QUANT_192, 6, 1, 0 },
374 	{ QUANT_256, 8, 0, 0 }
375 }};
376 
377 /**
378  * @brief The sequence scale, round, and divisors needed to compute sizing.
379  *
380  * The length of a quantized sequence in bits is:
381  *     (scale * <sequence_len> + round) / divisor
382  */
383 struct ise_size
384 {
385 	/** @brief The quantization level. */
386 	uint8_t quant;
387 
388 	/** @brief The scaling parameter. */
389 	uint8_t scale;
390 
391 	/** @brief The rounding parameter. */
392 	uint8_t round;
393 
394 	/** @brief The divisor parameter. */
395 	uint8_t divisor;
396 };
397 
398 /**
399  * @brief The table of scale, round, and divisors needed for quant sizing.
400  */
401 static const std::array<ise_size, 21> ise_sizes {{
402 	{   QUANT_2,  1, 0, 1 },
403 	{   QUANT_3,  8, 4, 5 },
404 	{   QUANT_4,  2, 0, 1 },
405 	{   QUANT_5,  7, 2, 3 },
406 	{   QUANT_6, 13, 4, 5 },
407 	{   QUANT_8,  3, 0, 1 },
408 	{  QUANT_10, 10, 2, 3 },
409 	{  QUANT_12, 18, 4, 5 },
410 	{  QUANT_16,  4, 0, 1 },
411 	{  QUANT_20, 13, 2, 3 },
412 	{  QUANT_24, 23, 4, 5 },
413 	{  QUANT_32,  5, 0, 1 },
414 	{  QUANT_40, 16, 2, 3 },
415 	{  QUANT_48, 28, 4, 5 },
416 	{  QUANT_64,  6, 0, 1 },
417 	{  QUANT_80, 19, 2, 3 },
418 	{  QUANT_96, 33, 4, 5 },
419 	{ QUANT_128,  7, 0, 1 },
420 	{ QUANT_160, 22, 2, 3 },
421 	{ QUANT_192, 38, 4, 5 },
422 	{ QUANT_256,  8, 0, 1 }
423 }};
424 
425 /* See header for documentation. */
get_ise_sequence_bitcount(unsigned int character_count,quant_method quant_level)426 unsigned int get_ise_sequence_bitcount(
427 	unsigned int character_count,
428 	quant_method quant_level
429 ) {
430 	// Cope with out-of bounds values - input might be invalid
431 	if (static_cast<size_t>(quant_level) >= ise_sizes.size())
432 	{
433 		// Arbitrary large number that's more than an ASTC block can hold
434 		return 1024;
435 	}
436 
437 	auto& entry = ise_sizes[quant_level];
438 	return (entry.scale * character_count + entry.round) / entry.divisor;
439 }
440 
441 /**
442  * @brief Write up to 8 bits at an arbitrary bit offset.
443  *
444  * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may
445  * span two separate bytes in memory.
446  *
447  * @param         value       The value to write.
448  * @param         bitcount    The number of bits to write, starting from LSB.
449  * @param         bitoffset   The bit offset to store at, between 0 and 7.
450  * @param[in,out] ptr         The data pointer to write to.
451  */
write_bits(unsigned int value,unsigned int bitcount,unsigned int bitoffset,uint8_t ptr[2])452 static inline void write_bits(
453 	unsigned int value,
454 	unsigned int bitcount,
455 	unsigned int bitoffset,
456 	uint8_t ptr[2]
457 ) {
458 	unsigned int mask = (1 << bitcount) - 1;
459 	value &= mask;
460 	ptr += bitoffset >> 3;
461 	bitoffset &= 7;
462 	value <<= bitoffset;
463 	mask <<= bitoffset;
464 	mask = ~mask;
465 
466 	ptr[0] &= mask;
467 	ptr[0] |= value;
468 	ptr[1] &= mask >> 8;
469 	ptr[1] |= value >> 8;
470 }
471 
472 /**
473  * @brief Read up to 8 bits at an arbitrary bit offset.
474  *
475  * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may
476  * span two separate bytes in memory.
477  *
478  * @param         bitcount    The number of bits to read.
479  * @param         bitoffset   The bit offset to read from, between 0 and 7.
480  * @param[in,out] ptr         The data pointer to read from.
481  *
482  * @return The read value.
483  */
read_bits(unsigned int bitcount,unsigned int bitoffset,const uint8_t * ptr)484 static inline unsigned int read_bits(
485 	unsigned int bitcount,
486 	unsigned int bitoffset,
487 	const uint8_t* ptr
488 ) {
489 	unsigned int mask = (1 << bitcount) - 1;
490 	ptr += bitoffset >> 3;
491 	bitoffset &= 7;
492 	unsigned int value = ptr[0] | (ptr[1] << 8);
493 	value >>= bitoffset;
494 	value &= mask;
495 	return value;
496 }
497 
498 /* See header for documentation. */
encode_ise(quant_method quant_level,unsigned int character_count,const uint8_t * input_data,uint8_t * output_data,unsigned int bit_offset)499 void encode_ise(
500 	quant_method quant_level,
501 	unsigned int character_count,
502 	const uint8_t* input_data,
503 	uint8_t* output_data,
504 	unsigned int bit_offset
505 ) {
506 	promise(character_count > 0);
507 
508 	unsigned int bits = btq_counts[quant_level].bits;
509 	unsigned int trits = btq_counts[quant_level].trits;
510 	unsigned int quints = btq_counts[quant_level].quints;
511 	unsigned int mask = (1 << bits) - 1;
512 
513 	// Write out trits and bits
514 	if (trits)
515 	{
516 		unsigned int i = 0;
517 		unsigned int full_trit_blocks = character_count / 5;
518 
519 		for (unsigned int j = 0; j < full_trit_blocks; j++)
520 		{
521 			unsigned int i4 = input_data[i + 4] >> bits;
522 			unsigned int i3 = input_data[i + 3] >> bits;
523 			unsigned int i2 = input_data[i + 2] >> bits;
524 			unsigned int i1 = input_data[i + 1] >> bits;
525 			unsigned int i0 = input_data[i + 0] >> bits;
526 
527 			uint8_t T = integer_of_trits[i4][i3][i2][i1][i0];
528 
529 			// The max size of a trit bit count is 6, so we can always safely
530 			// pack a single MX value with the following 1 or 2 T bits.
531 			uint8_t pack;
532 
533 			// Element 0 + T0 + T1
534 			pack = (input_data[i++] & mask) | (((T >> 0) & 0x3) << bits);
535 			write_bits(pack, bits + 2, bit_offset, output_data);
536 			bit_offset += bits + 2;
537 
538 			// Element 1 + T2 + T3
539 			pack = (input_data[i++] & mask) | (((T >> 2) & 0x3) << bits);
540 			write_bits(pack, bits + 2, bit_offset, output_data);
541 			bit_offset += bits + 2;
542 
543 			// Element 2 + T4
544 			pack = (input_data[i++] & mask) | (((T >> 4) & 0x1) << bits);
545 			write_bits(pack, bits + 1, bit_offset, output_data);
546 			bit_offset += bits + 1;
547 
548 			// Element 3 + T5 + T6
549 			pack = (input_data[i++] & mask) | (((T >> 5) & 0x3) << bits);
550 			write_bits(pack, bits + 2, bit_offset, output_data);
551 			bit_offset += bits + 2;
552 
553 			// Element 4 + T7
554 			pack = (input_data[i++] & mask) | (((T >> 7) & 0x1) << bits);
555 			write_bits(pack, bits + 1, bit_offset, output_data);
556 			bit_offset += bits + 1;
557 		}
558 
559 		// Loop tail for a partial block
560 		if (i != character_count)
561 		{
562 			// i4 cannot be present - we know the block is partial
563 			// i0 must be present - we know the block isn't empty
564 			unsigned int i4 =                            0;
565 			unsigned int i3 = i + 3 >= character_count ? 0 : input_data[i + 3] >> bits;
566 			unsigned int i2 = i + 2 >= character_count ? 0 : input_data[i + 2] >> bits;
567 			unsigned int i1 = i + 1 >= character_count ? 0 : input_data[i + 1] >> bits;
568 			unsigned int i0 =                                input_data[i + 0] >> bits;
569 
570 			uint8_t T = integer_of_trits[i4][i3][i2][i1][i0];
571 
572 			for (unsigned int j = 0; i < character_count; i++, j++)
573 			{
574 				// Truncated table as this iteration is always partital
575 				static const uint8_t tbits[4]  { 2, 2, 1, 2 };
576 				static const uint8_t tshift[4] { 0, 2, 4, 5 };
577 
578 				uint8_t pack = (input_data[i] & mask) |
579 				               (((T >> tshift[j]) & ((1 << tbits[j]) - 1)) << bits);
580 
581 				write_bits(pack, bits + tbits[j], bit_offset, output_data);
582 				bit_offset += bits + tbits[j];
583 			}
584 		}
585 	}
586 	// Write out quints and bits
587 	else if (quints)
588 	{
589 		unsigned int i = 0;
590 		unsigned int full_quint_blocks = character_count / 3;
591 
592 		for (unsigned int j = 0; j < full_quint_blocks; j++)
593 		{
594 			unsigned int i2 = input_data[i + 2] >> bits;
595 			unsigned int i1 = input_data[i + 1] >> bits;
596 			unsigned int i0 = input_data[i + 0] >> bits;
597 
598 			uint8_t T = integer_of_quints[i2][i1][i0];
599 
600 			// The max size of a quint bit count is 5, so we can always safely
601 			// pack a single M value with the following 2 or 3 T bits.
602 			uint8_t pack;
603 
604 			// Element 0
605 			pack = (input_data[i++] & mask) | (((T >> 0) & 0x7) << bits);
606 			write_bits(pack, bits + 3, bit_offset, output_data);
607 			bit_offset += bits + 3;
608 
609 			// Element 1
610 			pack = (input_data[i++] & mask) | (((T >> 3) & 0x3) << bits);
611 			write_bits(pack, bits + 2, bit_offset, output_data);
612 			bit_offset += bits + 2;
613 
614 			// Element 2
615 			pack = (input_data[i++] & mask) | (((T >> 5) & 0x3) << bits);
616 			write_bits(pack, bits + 2, bit_offset, output_data);
617 			bit_offset += bits + 2;
618 		}
619 
620 		// Loop tail for a partial block
621 		if (i != character_count)
622 		{
623 			// i2 cannot be present - we know the block is partial
624 			// i0 must be present - we know the block isn't empty
625 			unsigned int i2 =                            0;
626 			unsigned int i1 = i + 1 >= character_count ? 0 : input_data[i + 1] >> bits;
627 			unsigned int i0 =                                input_data[i + 0] >> bits;
628 
629 			uint8_t T = integer_of_quints[i2][i1][i0];
630 
631 			for (unsigned int j = 0; i < character_count; i++, j++)
632 			{
633 				// Truncated table as this iteration is always partital
634 				static const uint8_t tbits[2]  { 3, 2 };
635 				static const uint8_t tshift[2] { 0, 3 };
636 
637 				uint8_t pack = (input_data[i] & mask) |
638 				               (((T >> tshift[j]) & ((1 << tbits[j]) - 1)) << bits);
639 
640 				write_bits(pack, bits + tbits[j], bit_offset, output_data);
641 				bit_offset += bits + tbits[j];
642 			}
643 		}
644 	}
645 	// Write out just bits
646 	else
647 	{
648 		promise(character_count > 0);
649 		for (unsigned int i = 0; i < character_count; i++)
650 		{
651 			write_bits(input_data[i], bits, bit_offset, output_data);
652 			bit_offset += bits;
653 		}
654 	}
655 }
656 
657 /* See header for documentation. */
decode_ise(quant_method quant_level,unsigned int character_count,const uint8_t * input_data,uint8_t * output_data,unsigned int bit_offset)658 void decode_ise(
659 	quant_method quant_level,
660 	unsigned int character_count,
661 	const uint8_t* input_data,
662 	uint8_t* output_data,
663 	unsigned int bit_offset
664 ) {
665 	promise(character_count > 0);
666 
667 	// Note: due to how the trit/quint-block unpacking is done in this function, we may write more
668 	// temporary results than the number of outputs. The maximum actual number of results is 64 bit,
669 	// but we keep 4 additional character_count of padding.
670 	uint8_t results[68];
671 	uint8_t tq_blocks[22] { 0 }; // Trit-blocks or quint-blocks, must be zeroed
672 
673 	unsigned int bits = btq_counts[quant_level].bits;
674 	unsigned int trits = btq_counts[quant_level].trits;
675 	unsigned int quints = btq_counts[quant_level].quints;
676 
677 	unsigned int lcounter = 0;
678 	unsigned int hcounter = 0;
679 
680 	// Collect bits for each element, as well as bits for any trit-blocks and quint-blocks.
681 	for (unsigned int i = 0; i < character_count; i++)
682 	{
683 		results[i] = static_cast<uint8_t>(read_bits(bits, bit_offset, input_data));
684 		bit_offset += bits;
685 
686 		if (trits)
687 		{
688 			static const unsigned int bits_to_read[5]  { 2, 2, 1, 2, 1 };
689 			static const unsigned int block_shift[5]   { 0, 2, 4, 5, 7 };
690 			static const unsigned int next_lcounter[5] { 1, 2, 3, 4, 0 };
691 			static const unsigned int hcounter_incr[5] { 0, 0, 0, 0, 1 };
692 			unsigned int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data);
693 			bit_offset += bits_to_read[lcounter];
694 			tq_blocks[hcounter] |= tdata << block_shift[lcounter];
695 			hcounter += hcounter_incr[lcounter];
696 			lcounter = next_lcounter[lcounter];
697 		}
698 
699 		if (quints)
700 		{
701 			static const unsigned int bits_to_read[3]  { 3, 2, 2 };
702 			static const unsigned int block_shift[3]   { 0, 3, 5 };
703 			static const unsigned int next_lcounter[3] { 1, 2, 0 };
704 			static const unsigned int hcounter_incr[3] { 0, 0, 1 };
705 			unsigned int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data);
706 			bit_offset += bits_to_read[lcounter];
707 			tq_blocks[hcounter] |= tdata << block_shift[lcounter];
708 			hcounter += hcounter_incr[lcounter];
709 			lcounter = next_lcounter[lcounter];
710 		}
711 	}
712 
713 	// Unpack trit-blocks or quint-blocks as needed
714 	if (trits)
715 	{
716 		unsigned int trit_blocks = (character_count + 4) / 5;
717 		for (unsigned int i = 0; i < trit_blocks; i++)
718 		{
719 			const uint8_t *tritptr = trits_of_integer[tq_blocks[i]];
720 			results[5 * i    ] |= tritptr[0] << bits;
721 			results[5 * i + 1] |= tritptr[1] << bits;
722 			results[5 * i + 2] |= tritptr[2] << bits;
723 			results[5 * i + 3] |= tritptr[3] << bits;
724 			results[5 * i + 4] |= tritptr[4] << bits;
725 		}
726 	}
727 
728 	if (quints)
729 	{
730 		unsigned int quint_blocks = (character_count + 2) / 3;
731 		for (unsigned int i = 0; i < quint_blocks; i++)
732 		{
733 			const uint8_t *quintptr = quints_of_integer[tq_blocks[i]];
734 			results[3 * i    ] |= quintptr[0] << bits;
735 			results[3 * i + 1] |= quintptr[1] << bits;
736 			results[3 * i + 2] |= quintptr[2] << bits;
737 		}
738 	}
739 
740 	for (unsigned int i = 0; i < character_count; i++)
741 	{
742 		output_data[i] = results[i];
743 	}
744 }
745