@@ -331,14 +331,27 @@ static const uint8_t integer_of_trits[3][3][3][3][3] = {
331331 }
332332};
333333
334+ /* *
335+ * @brief The number of bits, trits, and quints needed for a quant level.
336+ */
334337struct btq_count {
338+ /* *< The quantization level. */
335339 uint8_t quant;
340+
341+ /* *< The number of bits. */
336342 uint8_t bits;
343+
344+ /* *< The number of trits. */
337345 uint8_t trits;
346+
347+ /* *< The number of quints. */
338348 uint8_t quints;
339349};
340350
341- static std::array<btq_count, 21 > btq_counts = {{
351+ /* *
352+ * @brief The table of bits, trits, and quints needed for a quant encode.
353+ */
354+ static const std::array<btq_count, 21 > btq_counts = {{
342355 { QUANT_2, 1 , 0 , 0 },
343356 { QUANT_3, 0 , 1 , 0 },
344357 { QUANT_4, 2 , 0 , 0 },
@@ -362,6 +375,69 @@ static std::array<btq_count, 21> btq_counts = {{
362375 { QUANT_256, 8 , 0 , 0 }
363376}};
364377
378+ /* *
379+ * @brief The sequence scale, round, and divisors needed to compute sizing.
380+ *
381+ * The length of a quantized sequence in bits is:
382+ * (scale * <sequence_len> + round) / divisor
383+ */
384+ struct ise_size {
385+ /* *< The quantization level. */
386+ uint8_t quant;
387+
388+ /* *< The scaling parameter. */
389+ uint8_t scale;
390+
391+ /* *< The rounding parameter. */
392+ uint8_t round;
393+
394+ /* *< 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. */
426+ int get_ise_sequence_bitcount (
427+ int items,
428+ quantization_method quant
429+ ) {
430+ // Cope with out-of bounds values - input might be invalid
431+ if (static_cast <size_t >(quant) >= 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];
438+ return (entry.scale * items + entry.round ) / entry.divisor ;
439+ }
440+
365441// routine to write up to 8 bits
366442static inline void write_bits (
367443 int value,
@@ -455,7 +531,7 @@ void encode_ise(
455531 if (trits)
456532 {
457533 static const int bits_to_write[5 ] = { 2 , 2 , 1 , 2 , 1 };
458- static const int block_shift[5 ] = { 0 , 2 , 4 , 5 , 7 };
534+ static const int block_shift[5 ] = { 0 , 2 , 4 , 5 , 7 };
459535 static const int next_lcounter[5 ] = { 1 , 2 , 3 , 4 , 0 };
460536 static const int hcounter_incr[5 ] = { 0 , 0 , 0 , 0 , 1 };
461537 write_bits (tq_blocks[hcounter] >> block_shift[lcounter], bits_to_write[lcounter], bit_offset, output_data);
@@ -467,7 +543,7 @@ void encode_ise(
467543 if (quints)
468544 {
469545 static const int bits_to_write[3 ] = { 3 , 2 , 2 };
470- static const int block_shift[3 ] = { 0 , 3 , 5 };
546+ static const int block_shift[3 ] = { 0 , 3 , 5 };
471547 static const int next_lcounter[3 ] = { 1 , 2 , 0 };
472548 static const int hcounter_incr[3 ] = { 0 , 0 , 1 };
473549 write_bits (tq_blocks[hcounter] >> block_shift[lcounter], bits_to_write[lcounter], bit_offset, output_data);
@@ -513,8 +589,8 @@ void decode_ise(
513589
514590 if (trits)
515591 {
516- static const int bits_to_read[5 ] = { 2 , 2 , 1 , 2 , 1 };
517- static const int block_shift[5 ] = { 0 , 2 , 4 , 5 , 7 };
592+ static const int bits_to_read[5 ] = { 2 , 2 , 1 , 2 , 1 };
593+ static const int block_shift[5 ] = { 0 , 2 , 4 , 5 , 7 };
518594 static const int next_lcounter[5 ] = { 1 , 2 , 3 , 4 , 0 };
519595 static const int hcounter_incr[5 ] = { 0 , 0 , 0 , 0 , 1 };
520596 int tdata = read_bits (bits_to_read[lcounter], bit_offset, input_data);
@@ -526,8 +602,8 @@ void decode_ise(
526602
527603 if (quints)
528604 {
529- static const int bits_to_read[3 ] = { 3 , 2 , 2 };
530- static const int block_shift[3 ] = { 0 , 3 , 5 };
605+ static const int bits_to_read[3 ] = { 3 , 2 , 2 };
606+ static const int block_shift[3 ] = { 0 , 3 , 5 };
531607 static const int next_lcounter[3 ] = { 1 , 2 , 0 };
532608 static const int hcounter_incr[3 ] = { 0 , 0 , 1 };
533609 int tdata = read_bits (bits_to_read[lcounter], bit_offset, input_data);
@@ -545,7 +621,7 @@ void decode_ise(
545621 for (int i = 0 ; i < trit_blocks; i++)
546622 {
547623 const uint8_t *tritptr = trits_of_integer[tq_blocks[i]];
548- results[5 * i] |= tritptr[0 ] << bits;
624+ results[5 * i ] |= tritptr[0 ] << bits;
549625 results[5 * i + 1 ] |= tritptr[1 ] << bits;
550626 results[5 * i + 2 ] |= tritptr[2 ] << bits;
551627 results[5 * i + 3 ] |= tritptr[3 ] << bits;
@@ -559,7 +635,7 @@ void decode_ise(
559635 for (int i = 0 ; i < quint_blocks; i++)
560636 {
561637 const uint8_t *quintptr = quints_of_integer[tq_blocks[i]];
562- results[3 * i] |= quintptr[0 ] << bits;
638+ results[3 * i ] |= quintptr[0 ] << bits;
563639 results[3 * i + 1 ] |= quintptr[1 ] << bits;
564640 results[3 * i + 2 ] |= quintptr[2 ] << bits;
565641 }
@@ -570,58 +646,3 @@ void decode_ise(
570646 output_data[i] = results[i];
571647 }
572648}
573-
574- int compute_ise_bitcount (
575- int items,
576- quantization_method quant
577- ) {
578- // Values in this enum are from an external data source, so not guaranteed
579- // to be bounded to the enum values for invalid block encodings
580- switch (static_cast <int >(quant))
581- {
582- case QUANT_2:
583- return items;
584- case QUANT_3:
585- return (8 * items + 4 ) / 5 ;
586- case QUANT_4:
587- return 2 * items;
588- case QUANT_5:
589- return (7 * items + 2 ) / 3 ;
590- case QUANT_6:
591- return (13 * items + 4 ) / 5 ;
592- case QUANT_8:
593- return 3 * items;
594- case QUANT_10:
595- return (10 * items + 2 ) / 3 ;
596- case QUANT_12:
597- return (18 * items + 4 ) / 5 ;
598- case QUANT_16:
599- return items * 4 ;
600- case QUANT_20:
601- return (13 * items + 2 ) / 3 ;
602- case QUANT_24:
603- return (23 * items + 4 ) / 5 ;
604- case QUANT_32:
605- return 5 * items;
606- case QUANT_40:
607- return (16 * items + 2 ) / 3 ;
608- case QUANT_48:
609- return (28 * items + 4 ) / 5 ;
610- case QUANT_64:
611- return 6 * items;
612- case QUANT_80:
613- return (19 * items + 2 ) / 3 ;
614- case QUANT_96:
615- return (33 * items + 4 ) / 5 ;
616- case QUANT_128:
617- return 7 * items;
618- case QUANT_160:
619- return (22 * items + 2 ) / 3 ;
620- case QUANT_192:
621- return (38 * items + 4 ) / 5 ;
622- case QUANT_256:
623- return 8 * items;
624- default :
625- return 100000 ;
626- }
627- }
0 commit comments