2323
2424#include < array>
2525
26- // unpacked quint triplets <low,middle,high> for each packed-quint value
26+ /* * @brief Unpacked quint triplets <low,middle,high> for each packed value */
2727static const uint8_t quints_of_integer[128 ][3 ] = {
2828 {0 , 0 , 0 }, {1 , 0 , 0 }, {2 , 0 , 0 }, {3 , 0 , 0 },
2929 {4 , 0 , 0 }, {0 , 4 , 0 }, {4 , 4 , 0 }, {4 , 4 , 4 },
@@ -59,8 +59,7 @@ static const uint8_t quints_of_integer[128][3] = {
5959 {4 , 3 , 3 }, {3 , 4 , 3 }, {0 , 3 , 4 }, {1 , 3 , 4 }
6060};
6161
62- // packed quint-value for every unpacked quint-triplet
63- // indexed by [high][middle][low]
62+ /* * @brief Packed quint values for each unpacked value, indexed [hi][mid][lo]. */
6463static const uint8_t integer_of_quints[5 ][5 ][5 ] = {
6564 {
6665 {0 , 1 , 2 , 3 , 4 },
@@ -99,7 +98,7 @@ static const uint8_t integer_of_quints[5][5][5] = {
9998 }
10099};
101100
102- // unpacked trit quintuplets <low,_,_,_, high> for each packed-quint value
101+ /* * @brief Unpacked trit quintuplets <low,..., high> for each packed value */
103102static const uint8_t trits_of_integer[256 ][5 ] = {
104103 {0 , 0 , 0 , 0 , 0 }, {1 , 0 , 0 , 0 , 0 }, {2 , 0 , 0 , 0 , 0 }, {0 , 0 , 2 , 0 , 0 },
105104 {0 , 1 , 0 , 0 , 0 }, {1 , 1 , 0 , 0 , 0 }, {2 , 1 , 0 , 0 , 0 }, {1 , 0 , 2 , 0 , 0 },
@@ -167,8 +166,7 @@ static const uint8_t trits_of_integer[256][5] = {
167166 {0 , 2 , 2 , 2 , 2 }, {1 , 2 , 2 , 2 , 2 }, {2 , 2 , 2 , 2 , 2 }, {2 , 1 , 2 , 2 , 2 }
168167};
169168
170- // packed trit-value for every unpacked trit-quintuplet
171- // indexed by [high][][][][low]
169+ /* * @brief Packed trit values for each unpacked value, indexed [hi][][][][lo]. */
172170static const uint8_t integer_of_trits[3 ][3 ][3 ][3 ][3 ] = {
173171 {
174172 {
@@ -424,26 +422,36 @@ static const std::array<ise_size, 21> ise_sizes = {{
424422
425423/* See header for documentation. */
426424int get_ise_sequence_bitcount (
427- int items ,
428- quant_method quant
425+ int character_count ,
426+ quant_method quant_level
429427) {
430428 // Cope with out-of bounds values - input might be invalid
431- if (static_cast <size_t >(quant ) >= ise_sizes.size ())
429+ if (static_cast <size_t >(quant_level ) >= ise_sizes.size ())
432430 {
433431 // Arbitrary large number that's more than an ASTC block can hold
434432 return 1024 ;
435433 }
436434
437- auto & entry = ise_sizes[quant ];
438- return (entry.scale * items + entry.round ) / entry.divisor ;
435+ auto & entry = ise_sizes[quant_level ];
436+ return (entry.scale * character_count + entry.round ) / entry.divisor ;
439437}
440438
441- // routine to write up to 8 bits
439+ /* *
440+ * @brief Write up to 8 bits at an arbitrary bit offset.
441+ *
442+ * The stored value is at most 8 bits, but can be stored at an offset of
443+ * between 0 and 7 bits so may span two separate bytes in memory.
444+ *
445+ * @param value The value to write.
446+ * @param bitcount The number of bits to write, starting from LSB.
447+ * @param bitoffset The bit offset to store at, between 0 and 7.
448+ * @param[in,out] ptr The data pointer to write to.
449+ */
442450static inline void write_bits (
443451 int value,
444452 int bitcount,
445453 int bitoffset,
446- uint8_t * ptr
454+ uint8_t ptr[ 2 ]
447455) {
448456 int mask = (1 << bitcount) - 1 ;
449457 value &= mask;
@@ -459,7 +467,18 @@ static inline void write_bits(
459467 ptr[1 ] |= value >> 8 ;
460468}
461469
462- // routine to read up to 8 bits
470+ /* *
471+ * @brief Read up to 8 bits at an arbitrary bit offset.
472+ *
473+ * The stored value is at most 8 bits, but can be stored at an offset of
474+ * between 0 and 7 bits so may span two separate bytes in memory.
475+ *
476+ * @param bitcount The number of bits to read.
477+ * @param bitoffset The bit offset to read from, between 0 and 7.
478+ * @param[in,out] ptr The data pointer to read from.
479+ *
480+ * @return The read value.
481+ */
463482static inline int read_bits (
464483 int bitcount,
465484 int bitoffset,
@@ -474,14 +493,15 @@ static inline int read_bits(
474493 return value;
475494}
476495
496+ /* See header for details. */
477497void encode_ise (
478- int quant_level,
479- int elements ,
498+ quant_method quant_level,
499+ int character_count ,
480500 const uint8_t * input_data,
481501 uint8_t * output_data,
482502 int bit_offset
483503) {
484- promise (elements > 0 );
504+ promise (character_count > 0 );
485505
486506 int bits = btq_counts[quant_level].bits ;
487507 int trits = btq_counts[quant_level].trits ;
@@ -492,7 +512,7 @@ void encode_ise(
492512 if (trits)
493513 {
494514 int i = 0 ;
495- int full_trit_blocks = elements / 5 ;
515+ int full_trit_blocks = character_count / 5 ;
496516
497517 for (int j = 0 ; j < full_trit_blocks; j++)
498518 {
@@ -535,19 +555,19 @@ void encode_ise(
535555 }
536556
537557 // Loop tail for a partial block
538- if (i != elements )
558+ if (i != character_count )
539559 {
540560 // i4 cannot be present - we know the block is partial
541561 // i0 must be present - we know the block isn't empty
542562 int i4 = 0 ;
543- int i3 = i + 3 >= elements ? 0 : input_data[i + 3 ] >> bits;
544- int i2 = i + 2 >= elements ? 0 : input_data[i + 2 ] >> bits;
545- int i1 = i + 1 >= elements ? 0 : input_data[i + 1 ] >> bits;
563+ int i3 = i + 3 >= character_count ? 0 : input_data[i + 3 ] >> bits;
564+ int i2 = i + 2 >= character_count ? 0 : input_data[i + 2 ] >> bits;
565+ int i1 = i + 1 >= character_count ? 0 : input_data[i + 1 ] >> bits;
546566 int i0 = input_data[i + 0 ] >> bits;
547567
548568 uint8_t T = integer_of_trits[i4][i3][i2][i1][i0];
549569
550- for (int j = 0 ; i < elements ; i++, j++)
570+ for (int j = 0 ; i < character_count ; i++, j++)
551571 {
552572 // Truncated table as this iteration is always partital
553573 static const uint8_t tbits[4 ] { 2 , 2 , 1 , 2 };
@@ -565,7 +585,7 @@ void encode_ise(
565585 else if (quints)
566586 {
567587 int i = 0 ;
568- int full_quint_blocks = elements / 3 ;
588+ int full_quint_blocks = character_count / 3 ;
569589
570590 for (int j = 0 ; j < full_quint_blocks; j++)
571591 {
@@ -596,17 +616,17 @@ void encode_ise(
596616 }
597617
598618 // Loop tail for a partial block
599- if (i != elements )
619+ if (i != character_count )
600620 {
601621 // i2 cannot be present - we know the block is partial
602622 // i0 must be present - we know the block isn't empty
603623 int i2 = 0 ;
604- int i1 = i + 1 >= elements ? 0 : input_data[i + 1 ] >> bits;
624+ int i1 = i + 1 >= character_count ? 0 : input_data[i + 1 ] >> bits;
605625 int i0 = input_data[i + 0 ] >> bits;
606626
607627 uint8_t T = integer_of_quints[i2][i1][i0];
608628
609- for (int j = 0 ; i < elements ; i++, j++)
629+ for (int j = 0 ; i < character_count ; i++, j++)
610630 {
611631 // Truncated table as this iteration is always partital
612632 static const uint8_t tbits[2 ] { 3 , 2 };
@@ -623,27 +643,28 @@ void encode_ise(
623643 // Write out just bits
624644 else
625645 {
626- promise (elements > 0 );
627- for (int i = 0 ; i < elements ; i++)
646+ promise (character_count > 0 );
647+ for (int i = 0 ; i < character_count ; i++)
628648 {
629649 write_bits (input_data[i], bits, bit_offset, output_data);
630650 bit_offset += bits;
631651 }
632652 }
633653}
634654
655+ /* See header for details. */
635656void decode_ise (
636- int quant_level,
637- int elements ,
657+ quant_method quant_level,
658+ int character_count ,
638659 const uint8_t * input_data,
639660 uint8_t * output_data,
640661 int bit_offset
641662) {
642- promise (elements > 0 );
663+ promise (character_count > 0 );
643664
644665 // note: due to how the trit/quint-block unpacking is done in this function,
645666 // we may write more temporary results than the number of outputs
646- // The maximum actual number of results is 64 bit, but we keep 4 additional elements
667+ // The maximum actual number of results is 64 bit, but we keep 4 additional character_count
647668 // of padding.
648669 uint8_t results[68 ];
649670 uint8_t tq_blocks[22 ]; // trit-blocks or quint-blocks
@@ -662,7 +683,7 @@ void decode_ise(
662683 }
663684
664685 // collect bits for each element, as well as bits for any trit-blocks and quint-blocks.
665- for (int i = 0 ; i < elements ; i++)
686+ for (int i = 0 ; i < character_count ; i++)
666687 {
667688 results[i] = read_bits (bits, bit_offset, input_data);
668689 bit_offset += bits;
@@ -697,7 +718,7 @@ void decode_ise(
697718 // unpack trit-blocks or quint-blocks as needed
698719 if (trits)
699720 {
700- int trit_blocks = (elements + 4 ) / 5 ;
721+ int trit_blocks = (character_count + 4 ) / 5 ;
701722 for (int i = 0 ; i < trit_blocks; i++)
702723 {
703724 const uint8_t *tritptr = trits_of_integer[tq_blocks[i]];
@@ -711,7 +732,7 @@ void decode_ise(
711732
712733 if (quints)
713734 {
714- int quint_blocks = (elements + 2 ) / 3 ;
735+ int quint_blocks = (character_count + 2 ) / 3 ;
715736 for (int i = 0 ; i < quint_blocks; i++)
716737 {
717738 const uint8_t *quintptr = quints_of_integer[tq_blocks[i]];
@@ -721,7 +742,7 @@ void decode_ise(
721742 }
722743 }
723744
724- for (int i = 0 ; i < elements ; i++)
745+ for (int i = 0 ; i < character_count ; i++)
725746 {
726747 output_data[i] = results[i];
727748 }
0 commit comments