gzippy 0.8.0

The fastest parallel gzip. Drop-in replacement for gzip and pigz, and a Rust library.
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//! Libdeflate-compatible Huffman Table Entry Format
//!
//! This module implements the EXACT entry format used by libdeflate,
//! which is the key to matching or exceeding its performance.
//!
//! ## Entry Format (matching libdeflate/lib/deflate_decompress.c lines 439-471)
//!
//! ### Literal/Length Table Entries
//!
//! ```text
//! Literals:
//!   Bit 31:     1 (HUFFDEC_LITERAL)
//!   Bit 23-16:  literal value
//!   Bit 15:     0 (!HUFFDEC_EXCEPTIONAL)
//!   Bit 14:     0 (!HUFFDEC_SUBTABLE_POINTER)
//!   Bit 13:     0 (!HUFFDEC_END_OF_BLOCK)
//!   Bit 11-8:   remaining codeword length [not used]
//!   Bit 3-0:    remaining codeword length
//!
//! Lengths:
//!   Bit 31:     0 (!HUFFDEC_LITERAL)
//!   Bit 24-16:  length base value (3-258)
//!   Bit 15:     0 (!HUFFDEC_EXCEPTIONAL)
//!   Bit 14:     0 (!HUFFDEC_SUBTABLE_POINTER)
//!   Bit 13:     0 (!HUFFDEC_END_OF_BLOCK)
//!   Bit 11-8:   remaining codeword length
//!   Bit 4-0:    remaining codeword length + number of extra bits
//!
//! End of block:
//!   Bit 31:     0 (!HUFFDEC_LITERAL)
//!   Bit 15:     1 (HUFFDEC_EXCEPTIONAL)
//!   Bit 14:     0 (!HUFFDEC_SUBTABLE_POINTER)
//!   Bit 13:     1 (HUFFDEC_END_OF_BLOCK)
//!   Bit 11-8:   remaining codeword length [not used]
//!   Bit 3-0:    remaining codeword length
//!
//! Subtable pointer:
//!   Bit 31:     0 (!HUFFDEC_LITERAL)
//!   Bit 30-16:  index of start of subtable
//!   Bit 15:     1 (HUFFDEC_EXCEPTIONAL)
//!   Bit 14:     1 (HUFFDEC_SUBTABLE_POINTER)
//!   Bit 13:     0 (!HUFFDEC_END_OF_BLOCK)
//!   Bit 11-8:   number of subtable bits
//!   Bit 3-0:    number of main table bits
//! ```
//!
//! ### Distance Table Entries
//!
//! ```text
//! Distances:
//!   Bit 31-16:  offset base value (1-32768)
//!   Bit 15:     0 (!HUFFDEC_EXCEPTIONAL)
//!   Bit 14:     0 (!HUFFDEC_SUBTABLE_POINTER)
//!   Bit 11-8:   remaining codeword length
//!   Bit 4-0:    remaining codeword length + number of extra bits
//!
//! Subtable pointer:
//!   Bit 31-16:  index of start of subtable
//!   Bit 15:     1 (HUFFDEC_EXCEPTIONAL)
//!   Bit 14:     1 (HUFFDEC_SUBTABLE_POINTER)
//!   Bit 11-8:   number of subtable bits
//!   Bit 3-0:    number of main table bits
//! ```

#![allow(dead_code)]
#![allow(clippy::needless_range_loop)]

/// Flag: Entry is a literal (bit 31)
pub const HUFFDEC_LITERAL: u32 = 0x8000_0000;

/// Flag: Entry is exceptional (subtable pointer or EOB) (bit 15)
pub const HUFFDEC_EXCEPTIONAL: u32 = 0x0000_8000;

/// Flag: Entry is a subtable pointer (bit 14)
pub const HUFFDEC_SUBTABLE_POINTER: u32 = 0x0000_4000;

/// Flag: Entry is end-of-block (bit 13)
pub const HUFFDEC_END_OF_BLOCK: u32 = 0x0000_2000;

/// A literal/length table entry
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct LitLenEntry(u32);

impl LitLenEntry {
    /// Create a literal entry
    #[inline(always)]
    pub const fn literal(value: u8, codeword_bits: u8) -> Self {
        // Bit 31: LITERAL flag
        // Bit 23-16: literal value
        // Bit 3-0: codeword bits (for consumption)
        Self(HUFFDEC_LITERAL | ((value as u32) << 16) | (codeword_bits as u32))
    }

    /// Create a length entry
    #[inline(always)]
    pub const fn length(base: u16, extra_bits: u8, codeword_bits: u8) -> Self {
        // Bit 24-16: length base (9 bits, max 258)
        // Bit 11-8: remaining codeword length (for saved_bitbuf)
        // Bit 4-0: total bits = codeword_bits + extra_bits
        let total_bits = codeword_bits + extra_bits;
        Self(((base as u32) << 16) | ((codeword_bits as u32) << 8) | (total_bits as u32))
    }

    /// Create an end-of-block entry
    #[inline(always)]
    pub const fn end_of_block(codeword_bits: u8) -> Self {
        Self(HUFFDEC_EXCEPTIONAL | HUFFDEC_END_OF_BLOCK | (codeword_bits as u32))
    }

    /// Create a subtable pointer entry
    #[inline(always)]
    pub const fn subtable_ptr(subtable_start: u16, subtable_bits: u8, main_table_bits: u8) -> Self {
        Self(
            ((subtable_start as u32) << 16)
                | HUFFDEC_EXCEPTIONAL
                | HUFFDEC_SUBTABLE_POINTER
                | ((subtable_bits as u32) << 8)
                | (main_table_bits as u32),
        )
    }

    /// Check if this is a literal (bit 31 set)
    /// Uses signed comparison: (entry as i32) < 0
    #[inline(always)]
    pub const fn is_literal(self) -> bool {
        (self.0 as i32) < 0
    }

    /// Check if this is exceptional (subtable or EOB)
    #[inline(always)]
    pub const fn is_exceptional(self) -> bool {
        (self.0 & HUFFDEC_EXCEPTIONAL) != 0
    }

    /// Check if this is a subtable pointer
    #[inline(always)]
    pub const fn is_subtable_ptr(self) -> bool {
        (self.0 & HUFFDEC_SUBTABLE_POINTER) != 0
    }

    /// Check if this is end-of-block
    #[inline(always)]
    pub const fn is_end_of_block(self) -> bool {
        (self.0 & HUFFDEC_END_OF_BLOCK) != 0
    }

    /// Get the literal value (bits 23-16)
    #[inline(always)]
    pub const fn literal_value(self) -> u8 {
        ((self.0 >> 16) & 0xFF) as u8
    }

    /// Get the length base value (bits 24-16)
    #[inline(always)]
    pub const fn length_base(self) -> u16 {
        ((self.0 >> 16) & 0x1FF) as u16
    }

    /// Get the codeword bits (bits 11-8 for lengths, bits 3-0 for others)
    #[inline(always)]
    pub const fn codeword_bits(self) -> u8 {
        if self.is_literal() || self.is_exceptional() {
            // For literals and EOB, bits 3-0 contain codeword bits
            (self.0 & 0xF) as u8
        } else {
            // For lengths, bits 11-8 contain codeword bits
            ((self.0 >> 8) & 0xF) as u8
        }
    }

    /// Get total bits to consume (bits 4-0)
    /// For literals: just codeword bits
    /// For lengths: codeword + extra bits
    #[inline(always)]
    pub const fn total_bits(self) -> u8 {
        (self.0 & 0x1F) as u8
    }

    /// Get subtable start index (bits 30-16)
    #[inline(always)]
    pub const fn subtable_start(self) -> u16 {
        ((self.0 >> 16) & 0x7FFF) as u16
    }

    /// Get subtable bits (bits 11-8)
    #[inline(always)]
    pub const fn subtable_bits(self) -> u8 {
        ((self.0 >> 8) & 0xF) as u8
    }

    /// Get main table bits (bits 3-0) for subtable entries
    #[inline(always)]
    pub const fn main_table_bits(self) -> u8 {
        (self.0 & 0xF) as u8
    }

    /// Get raw value for bit operations
    #[inline(always)]
    pub const fn raw(self) -> u32 {
        self.0
    }

    /// Create from raw u32 value
    #[inline(always)]
    pub const fn from_raw(raw: u32) -> Self {
        Self(raw)
    }

    /// Decode length value using saved_bitbuf
    /// Length = base + extra_bits_value
    /// Decode length from saved_bitbuf (branchless)
    #[inline(always)]
    pub fn decode_length(self, saved_bitbuf: u64) -> u32 {
        let base = self.length_base() as u32;
        let codeword_bits = self.codeword_bits();
        let total_bits = self.total_bits();
        let extra_bits = total_bits - codeword_bits;
        // Branchless: when extra_bits is 0, mask is 0 and extra_value is 0
        let extra_mask = (1u64 << extra_bits).wrapping_sub(1);
        let extra_value = (saved_bitbuf >> codeword_bits) & extra_mask;
        base + extra_value as u32
    }
}

/// A distance table entry
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct DistEntry(u32);

impl DistEntry {
    /// Create a distance entry
    #[inline(always)]
    pub const fn distance(base: u16, extra_bits: u8, codeword_bits: u8) -> Self {
        // Bit 31-16: offset base value
        // Bit 11-8: remaining codeword length
        // Bit 4-0: total bits = codeword_bits + extra_bits
        let total_bits = codeword_bits + extra_bits;
        Self(((base as u32) << 16) | ((codeword_bits as u32) << 8) | (total_bits as u32))
    }

    /// Create a subtable pointer entry
    #[inline(always)]
    pub const fn subtable_ptr(subtable_start: u16, subtable_bits: u8, main_table_bits: u8) -> Self {
        Self(
            ((subtable_start as u32) << 16)
                | HUFFDEC_EXCEPTIONAL
                | HUFFDEC_SUBTABLE_POINTER
                | ((subtable_bits as u32) << 8)
                | (main_table_bits as u32),
        )
    }

    /// Check if this is exceptional (subtable pointer)
    #[inline(always)]
    pub const fn is_exceptional(self) -> bool {
        (self.0 & HUFFDEC_EXCEPTIONAL) != 0
    }

    /// Check if this is a subtable pointer
    #[inline(always)]
    pub const fn is_subtable_ptr(self) -> bool {
        (self.0 & HUFFDEC_SUBTABLE_POINTER) != 0
    }

    /// Get the distance base value (bits 31-16)
    #[inline(always)]
    pub const fn distance_base(self) -> u16 {
        ((self.0 >> 16) & 0xFFFF) as u16
    }

    /// Get the codeword bits (bits 11-8)
    #[inline(always)]
    pub const fn codeword_bits(self) -> u8 {
        ((self.0 >> 8) & 0xF) as u8
    }

    /// Get total bits to consume (bits 4-0)
    #[inline(always)]
    pub const fn total_bits(self) -> u8 {
        (self.0 & 0x1F) as u8
    }

    /// Get subtable start index (bits 31-16)
    #[inline(always)]
    pub const fn subtable_start(self) -> u16 {
        ((self.0 >> 16) & 0xFFFF) as u16
    }

    /// Get subtable bits (bits 11-8)
    #[inline(always)]
    pub const fn subtable_bits(self) -> u8 {
        ((self.0 >> 8) & 0xF) as u8
    }

    /// Get main table bits (bits 3-0) for subtable entries
    #[inline(always)]
    pub const fn main_table_bits(self) -> u8 {
        (self.0 & 0xF) as u8
    }

    /// Get raw value for bit operations
    #[inline(always)]
    pub const fn raw(self) -> u32 {
        self.0
    }

    /// Decode distance value using saved_bitbuf
    /// Distance = base + extra_bits_value
    /// Decode distance from saved_bitbuf (branchless, uses BMI2 when available)
    #[inline(always)]
    pub fn decode_distance(self, saved_bitbuf: u64) -> u32 {
        let base = self.distance_base() as u32;
        let codeword_bits = self.codeword_bits();
        let total_bits = self.total_bits();
        let extra_bits = total_bits - codeword_bits;
        // Use BMI2 _bzhi_u64 when available for faster bit extraction
        let extra_value = crate::decompress::inflate::bmi2::decode_extra_bits(
            saved_bitbuf,
            codeword_bits,
            extra_bits,
        ) as u32;
        base + extra_value
    }
}

/// Literal/Length decode table
#[derive(Clone)]
pub struct LitLenTable {
    /// Main table (size: 1 << table_bits)
    entries: Vec<LitLenEntry>,
    /// Number of bits for main table lookup
    table_bits: u8,
}

impl LitLenTable {
    /// Number of bits for main table lookup (11 bits = 8KB table).
    /// Benchmarked 11-15 on ARM64: 11-bit is fastest due to L1d cache locality.
    /// Larger tables (12-15 bit) reduce subtable lookups but cause cache misses.
    pub const TABLE_BITS: u8 = 11;
    /// Maximum number of subtable bits for codes longer than TABLE_BITS
    pub const MAX_SUBTABLE_BITS: u8 = 15 - Self::TABLE_BITS;

    /// Build a literal/length decode table from code lengths
    pub fn build(code_lengths: &[u8]) -> Option<Self> {
        let table_bits = Self::TABLE_BITS;
        let main_size = 1usize << table_bits;

        // Count codes of each length
        let mut count = [0u16; 16];
        for &len in code_lengths {
            if len > 0 && len <= 15 {
                count[len as usize] += 1;
            }
        }

        // Compute first code for each length
        let mut first_code = [0u32; 16];
        let mut code = 0u32;
        for len in 1..=15 {
            code = (code + count[len - 1] as u32) << 1;
            first_code[len] = code;
        }

        // Allocate table with space for subtables
        let max_subtable_entries = (1usize << Self::MAX_SUBTABLE_BITS)
            * code_lengths.iter().filter(|&&l| l > table_bits).count();
        let mut entries = vec![LitLenEntry(0); main_size + max_subtable_entries];
        let mut subtable_next = main_size;

        // Assign codes to symbols
        let mut next_code = first_code;
        for (symbol, &len) in code_lengths.iter().enumerate() {
            if len == 0 {
                continue;
            }
            let len = len as usize;
            let codeword = next_code[len];
            next_code[len] += 1;

            // Reverse the codeword bits for table lookup
            let reversed = reverse_bits(codeword, len as u8);

            if len <= table_bits as usize {
                // Check if this is a length code that can be pre-expanded
                if let Some((base, extra_bits)) = get_length_info(symbol) {
                    let total_bits = len + extra_bits as usize;
                    if total_bits <= table_bits as usize && extra_bits > 0 {
                        // PRE-EXPAND: Create separate entries for each extra bit combination
                        // This eliminates runtime extra bit reading for these codes
                        let num_expansions = 1usize << extra_bits;
                        for extra_val in 0..num_expansions {
                            let final_length = base + extra_val as u16;
                            let entry =
                                create_preexpanded_length_entry(final_length, total_bits as u8);

                            // The expanded code is: reversed_codeword | (extra_val << codeword_len)
                            let expanded_code = (reversed as usize) | (extra_val << len);
                            let stride = 1usize << total_bits;
                            let mut idx = expanded_code;
                            while idx < main_size {
                                entries[idx] = entry;
                                idx += stride;
                            }
                        }
                        continue; // Skip the normal entry creation
                    }
                }

                // Direct entry in main table - replicate for all suffixes
                let entry = create_litlen_entry(symbol, len as u8);
                let stride = 1usize << len;
                let mut idx = reversed as usize;
                while idx < main_size {
                    entries[idx] = entry;
                    idx += stride;
                }
            } else {
                // Need subtable
                let main_idx = (reversed & ((1 << table_bits) - 1)) as usize;
                let extra_bits = len - table_bits as usize;

                // Check if subtable already exists
                if !entries[main_idx].is_subtable_ptr() {
                    // Create new subtable
                    let subtable_start = subtable_next as u16;
                    let subtable_bits = Self::MAX_SUBTABLE_BITS;
                    entries[main_idx] =
                        LitLenEntry::subtable_ptr(subtable_start, subtable_bits, table_bits);
                    subtable_next += 1usize << subtable_bits;
                }

                // Fill subtable entry
                let subtable_start = entries[main_idx].subtable_start() as usize;
                let subtable_bits = entries[main_idx].subtable_bits() as usize;
                let subtable_idx = (reversed >> table_bits) as usize;

                // For subtable entries, store (len - table_bits) not full len
                // This is the subtable portion only
                let subtable_len = (len - table_bits as usize) as u8;
                let entry = create_litlen_entry(symbol, subtable_len);
                let stride = 1usize << extra_bits;
                let mut idx = subtable_idx;
                while idx < (1usize << subtable_bits) {
                    entries[subtable_start + idx] = entry;
                    idx += stride;
                }
            }
        }

        entries.truncate(subtable_next);

        Some(Self {
            entries,
            table_bits,
        })
    }

    /// Look up an entry by bit pattern (unsafe unchecked for max speed)
    #[inline(always)]
    pub fn lookup(&self, bits: u64) -> LitLenEntry {
        // Use const TABLE_BITS for compile-time optimization
        const MASK: usize = (1usize << LitLenTable::TABLE_BITS) - 1;
        let idx = (bits as usize) & MASK;
        // SAFETY: idx is masked to be within table_bits range,
        // and entries is always at least (1 << table_bits) in size
        unsafe { *self.entries.get_unchecked(idx) }
    }

    /// Get raw pointer to entries for even faster access
    #[inline(always)]
    pub fn entries_ptr(&self) -> *const LitLenEntry {
        self.entries.as_ptr()
    }

    /// Look up entry by direct index (for SIMD parallel decode)
    #[inline(always)]
    pub fn lookup_by_index(&self, idx: usize) -> LitLenEntry {
        // SAFETY: caller must ensure idx is within table bounds (0..2048 for main table)
        unsafe { *self.entries.get_unchecked(idx) }
    }

    /// Look up a subtable entry (unsafe unchecked for max speed)
    #[inline(always)]
    pub fn lookup_subtable(&self, entry: LitLenEntry, bits: u64) -> LitLenEntry {
        let subtable_start = entry.subtable_start() as usize;
        let subtable_bits = entry.subtable_bits();
        let main_bits = entry.main_table_bits();
        let idx = ((bits >> main_bits) as usize) & ((1usize << subtable_bits) - 1);
        // SAFETY: subtable entries are allocated during build
        unsafe { *self.entries.get_unchecked(subtable_start + idx) }
    }

    /// Resolve an entry (handle subtables)
    #[inline(always)]
    pub fn resolve(&self, bits: u64) -> LitLenEntry {
        let entry = self.lookup(bits);
        if entry.is_subtable_ptr() {
            self.lookup_subtable(entry, bits)
        } else {
            entry
        }
    }

    /// Resolve an entry and return total bits consumed (for double-literal cache)
    /// Returns (entry, total_bits) where total_bits includes TABLE_BITS for subtable entries
    #[inline(always)]
    pub fn resolve_with_total_bits(&self, bits: u64) -> (LitLenEntry, u8) {
        let entry = self.lookup(bits);
        if entry.is_subtable_ptr() {
            let subtable_entry = self.lookup_subtable(entry, bits);
            let total_bits = Self::TABLE_BITS + subtable_entry.codeword_bits();
            (subtable_entry, total_bits)
        } else {
            (entry, entry.codeword_bits())
        }
    }
}

/// Distance decode table
#[derive(Clone)]
pub struct DistTable {
    /// Main table (size: 1 << table_bits)
    entries: Vec<DistEntry>,
    /// Number of bits for main table lookup
    table_bits: u8,
}

impl DistTable {
    /// Number of bits for main table
    pub const TABLE_BITS: u8 = 8;
    /// Maximum number of subtable bits
    pub const MAX_SUBTABLE_BITS: u8 = 7; // 15 - 8

    /// Build a distance decode table from code lengths
    pub fn build(code_lengths: &[u8]) -> Option<Self> {
        let table_bits = Self::TABLE_BITS;
        let main_size = 1usize << table_bits;

        // Count codes of each length
        let mut count = [0u16; 16];
        for &len in code_lengths {
            if len > 0 && len <= 15 {
                count[len as usize] += 1;
            }
        }

        // Compute first code for each length
        let mut first_code = [0u32; 16];
        let mut code = 0u32;
        for len in 1..=15 {
            code = (code + count[len - 1] as u32) << 1;
            first_code[len] = code;
        }

        // Allocate table with space for subtables
        let max_subtable_entries = (1usize << Self::MAX_SUBTABLE_BITS)
            * code_lengths.iter().filter(|&&l| l > table_bits).count();
        let mut entries = vec![DistEntry(0); main_size + max_subtable_entries];
        let mut subtable_next = main_size;

        // Assign codes to symbols
        let mut next_code = first_code;
        for (symbol, &len) in code_lengths.iter().enumerate() {
            if len == 0 {
                continue;
            }
            let len = len as usize;
            let codeword = next_code[len];
            next_code[len] += 1;

            // Reverse the codeword bits for table lookup
            let reversed = reverse_bits(codeword, len as u8);

            if len <= table_bits as usize {
                // Direct entry in main table - replicate for all suffixes
                let entry = create_dist_entry(symbol, len as u8);
                let stride = 1usize << len;
                let mut idx = reversed as usize;
                while idx < main_size {
                    entries[idx] = entry;
                    idx += stride;
                }
            } else {
                // Need subtable
                let main_idx = (reversed & ((1 << table_bits) - 1)) as usize;
                let extra_bits = len - table_bits as usize;

                // Check if subtable already exists
                if !entries[main_idx].is_subtable_ptr() {
                    // Create new subtable
                    let subtable_start = subtable_next as u16;
                    let subtable_bits = Self::MAX_SUBTABLE_BITS;
                    entries[main_idx] =
                        DistEntry::subtable_ptr(subtable_start, subtable_bits, table_bits);
                    subtable_next += 1usize << subtable_bits;
                }

                // Fill subtable entry
                let subtable_start = entries[main_idx].subtable_start() as usize;
                let subtable_bits = entries[main_idx].subtable_bits() as usize;
                let subtable_idx = (reversed >> table_bits) as usize;

                // For subtable entries, store (len - table_bits) not full len
                let subtable_len = (len - table_bits as usize) as u8;
                let entry = create_dist_entry(symbol, subtable_len);
                let stride = 1usize << extra_bits;
                let mut idx = subtable_idx;
                while idx < (1usize << subtable_bits) {
                    entries[subtable_start + idx] = entry;
                    idx += stride;
                }
            }
        }

        entries.truncate(subtable_next);
        Some(Self {
            entries,
            table_bits,
        })
    }

    /// Look up an entry by bit pattern (unsafe unchecked for max speed)
    #[inline(always)]
    pub fn lookup(&self, bits: u64) -> DistEntry {
        // Use const TABLE_BITS for compile-time optimization
        const MASK: usize = (1usize << DistTable::TABLE_BITS) - 1;
        let idx = (bits as usize) & MASK;
        // SAFETY: idx is masked to be within table_bits range
        unsafe { *self.entries.get_unchecked(idx) }
    }

    /// Look up a subtable entry (unsafe unchecked for max speed)
    #[inline(always)]
    pub fn lookup_subtable(&self, entry: DistEntry, bits: u64) -> DistEntry {
        let subtable_start = entry.subtable_start() as usize;
        let subtable_bits = entry.subtable_bits();
        let main_bits = entry.main_table_bits();
        let idx = ((bits >> main_bits) as usize) & ((1usize << subtable_bits) - 1);
        // SAFETY: subtable entries are allocated during build
        unsafe { *self.entries.get_unchecked(subtable_start + idx) }
    }

    /// Look up subtable entry from already-shifted bitbuf (libdeflate fastloop pattern)
    /// Use when bitbuf has already been shifted by main table bits
    #[inline(always)]
    pub fn lookup_subtable_direct(&self, entry: DistEntry, shifted_bits: u64) -> DistEntry {
        let subtable_start = entry.subtable_start() as usize;
        let subtable_bits = entry.subtable_bits();
        let idx = (shifted_bits as usize) & ((1usize << subtable_bits) - 1);
        // SAFETY: subtable entries are allocated during build
        unsafe { *self.entries.get_unchecked(subtable_start + idx) }
    }

    /// Resolve an entry (handle subtables)
    #[inline(always)]
    pub fn resolve(&self, bits: u64) -> DistEntry {
        let entry = self.lookup(bits);
        if entry.is_subtable_ptr() {
            self.lookup_subtable(entry, bits)
        } else {
            entry
        }
    }
}

// =============================================================================
// Helper functions
// =============================================================================

/// Reverse the bottom `n` bits of `code`
fn reverse_bits(code: u32, n: u8) -> u32 {
    let mut result = 0u32;
    let mut code = code;
    for _ in 0..n {
        result = (result << 1) | (code & 1);
        code >>= 1;
    }
    result
}

/// Length base values and extra bits (RFC 1951)
pub const LENGTH_TABLE: [(u16, u8); 29] = [
    (3, 0),
    (4, 0),
    (5, 0),
    (6, 0),
    (7, 0),
    (8, 0),
    (9, 0),
    (10, 0), // 257-264
    (11, 1),
    (13, 1),
    (15, 1),
    (17, 1), // 265-268
    (19, 2),
    (23, 2),
    (27, 2),
    (31, 2), // 269-272
    (35, 3),
    (43, 3),
    (51, 3),
    (59, 3), // 273-276
    (67, 4),
    (83, 4),
    (99, 4),
    (115, 4), // 277-280
    (131, 5),
    (163, 5),
    (195, 5),
    (227, 5), // 281-284
    (258, 0), // 285
];

/// Distance base values and extra bits (RFC 1951)
pub const DISTANCE_TABLE: [(u16, u8); 30] = [
    (1, 0),
    (2, 0),
    (3, 0),
    (4, 0), // 0-3
    (5, 1),
    (7, 1), // 4-5
    (9, 2),
    (13, 2), // 6-7
    (17, 3),
    (25, 3), // 8-9
    (33, 4),
    (49, 4), // 10-11
    (65, 5),
    (97, 5), // 12-13
    (129, 6),
    (193, 6), // 14-15
    (257, 7),
    (385, 7), // 16-17
    (513, 8),
    (769, 8), // 18-19
    (1025, 9),
    (1537, 9), // 20-21
    (2049, 10),
    (3073, 10), // 22-23
    (4097, 11),
    (6145, 11), // 24-25
    (8193, 12),
    (12289, 12), // 26-27
    (16385, 13),
    (24577, 13), // 28-29
];

/// Create a literal/length entry for a symbol
fn create_litlen_entry(symbol: usize, codeword_bits: u8) -> LitLenEntry {
    if symbol < 256 {
        // Literal
        LitLenEntry::literal(symbol as u8, codeword_bits)
    } else if symbol == 256 {
        // End of block
        LitLenEntry::end_of_block(codeword_bits)
    } else if symbol <= 285 {
        // Length code
        let idx = symbol - 257;
        let (base, extra) = LENGTH_TABLE[idx];
        LitLenEntry::length(base, extra, codeword_bits)
    } else {
        // Invalid symbol, create a dummy entry
        LitLenEntry(0)
    }
}

/// Create a pre-expanded length entry where length is already computed
/// Used when codeword + extra_bits fits in the table, avoiding runtime extra bit reading
#[inline(always)]
fn create_preexpanded_length_entry(length: u16, total_bits: u8) -> LitLenEntry {
    // For pre-expanded entries:
    // - Bit 24-16: final length value (not base, but actual length)
    // - Bit 11-8: total_bits (for saved_bitbuf, same as bits 4-0)
    // - Bit 4-0: total_bits (no extra bits to read at decode time)
    // The key insight: extra_bits field = 0, so decode_length just returns base
    LitLenEntry(((length as u32) << 16) | ((total_bits as u32) << 8) | (total_bits as u32))
}

/// Check if a length symbol can be pre-expanded and return the expansion info
/// Returns Some((base, extra_bits)) if symbol is a length code, None otherwise
fn get_length_info(symbol: usize) -> Option<(u16, u8)> {
    if (257..=285).contains(&symbol) {
        let idx = symbol - 257;
        Some(LENGTH_TABLE[idx])
    } else {
        None
    }
}

/// Create a distance entry for a symbol
fn create_dist_entry(symbol: usize, codeword_bits: u8) -> DistEntry {
    if symbol < 30 {
        let (base, extra) = DISTANCE_TABLE[symbol];
        DistEntry::distance(base, extra, codeword_bits)
    } else {
        // Invalid symbol, create a dummy entry
        DistEntry(0)
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_literal_entry() {
        let entry = LitLenEntry::literal(b'A', 8);
        assert!(entry.is_literal());
        assert!(!entry.is_exceptional());
        assert!(!entry.is_subtable_ptr());
        assert!(!entry.is_end_of_block());
        assert_eq!(entry.literal_value(), b'A');
        assert_eq!(entry.total_bits(), 8);
    }

    #[test]
    fn test_length_entry() {
        // Symbol 257 = length 3, 0 extra bits
        let entry = LitLenEntry::length(3, 0, 7);
        assert!(!entry.is_literal());
        assert!(!entry.is_exceptional());
        assert_eq!(entry.length_base(), 3);
        assert_eq!(entry.codeword_bits(), 7);
        assert_eq!(entry.total_bits(), 7);
    }

    #[test]
    fn test_length_with_extra() {
        // Symbol 265 = length 11-12, 1 extra bit
        let entry = LitLenEntry::length(11, 1, 7);
        assert_eq!(entry.length_base(), 11);
        assert_eq!(entry.codeword_bits(), 7);
        assert_eq!(entry.total_bits(), 8);

        // Decode with extra bit = 1 -> length = 11 + 1 = 12
        let length = entry.decode_length(0b1_0000000); // 7 codeword bits, then 1 extra bit
        assert_eq!(length, 12);
    }

    #[test]
    fn test_eob_entry() {
        let entry = LitLenEntry::end_of_block(7);
        assert!(!entry.is_literal());
        assert!(entry.is_exceptional());
        assert!(!entry.is_subtable_ptr());
        assert!(entry.is_end_of_block());
        assert_eq!(entry.total_bits(), 7);
    }

    #[test]
    fn test_subtable_ptr() {
        let entry = LitLenEntry::subtable_ptr(2048, 4, 11);
        assert!(!entry.is_literal());
        assert!(entry.is_exceptional());
        assert!(entry.is_subtable_ptr());
        assert!(!entry.is_end_of_block());
        assert_eq!(entry.subtable_start(), 2048);
        assert_eq!(entry.subtable_bits(), 4);
        assert_eq!(entry.main_table_bits(), 11);
    }

    #[test]
    fn test_distance_entry() {
        // Distance code 0 = distance 1, 0 extra bits
        let entry = DistEntry::distance(1, 0, 5);
        assert!(!entry.is_exceptional());
        assert_eq!(entry.distance_base(), 1);
        assert_eq!(entry.codeword_bits(), 5);
        assert_eq!(entry.total_bits(), 5);
    }

    #[test]
    fn test_distance_with_extra() {
        // Distance code 29 = distance 24577-32768, 13 extra bits
        let entry = DistEntry::distance(24577, 13, 5);
        assert_eq!(entry.distance_base(), 24577);
        assert_eq!(entry.codeword_bits(), 5);
        assert_eq!(entry.total_bits(), 18);

        // Decode with extra bits = 0x1FFF (max) -> distance = 24577 + 8191 = 32768
        let dist = entry.decode_distance(0b11_1111_1111_1110_0000); // 5 codeword bits, 13 extra
        assert_eq!(dist, 32768);
    }

    #[test]
    fn test_signed_literal_check() {
        // The key optimization: use (entry as i32) < 0 for literal check
        let literal = LitLenEntry::literal(b'X', 8);
        let length = LitLenEntry::length(3, 0, 7);
        let eob = LitLenEntry::end_of_block(7);

        // Signed comparison
        assert!((literal.raw() as i32) < 0);
        assert!((length.raw() as i32) >= 0);
        assert!((eob.raw() as i32) >= 0);
    }

    #[test]
    fn test_build_fixed_litlen_table() {
        // Build fixed Huffman table for lit/len
        let mut code_lengths = [0u8; 288];
        for i in 0..144 {
            code_lengths[i] = 8;
        }
        for i in 144..256 {
            code_lengths[i] = 9;
        }
        for i in 256..280 {
            code_lengths[i] = 7;
        }
        for i in 280..288 {
            code_lengths[i] = 8;
        }

        let table = LitLenTable::build(&code_lengths).expect("Failed to build table");

        // Test that we can look up entries
        // Check literal 'A' (65) which has 8-bit code
        // Fixed Huffman: symbols 0-143 get codes 00110000 + symbol (8 bits)
        // Symbol 65 = 0x41 + 0x30 = 0x71 = 01110001 binary
        // Reversed: 10001110 = 0x8E
        let entry = table.resolve(0x8E);
        assert!(
            entry.is_literal(),
            "Entry 0x8E should be literal, got {:08X}",
            entry.raw()
        );
        assert_eq!(entry.literal_value(), 65, "Should decode to 'A'");

        // Check end of block (symbol 256) which has 7-bit code
        // Fixed: symbols 256-279 get codes 0000000 + (symbol - 256) (7 bits)
        // Symbol 256 = 0000000
        // Reversed: 0000000 = 0x00
        let eob = table.resolve(0x00);
        assert!(
            eob.is_end_of_block(),
            "Entry 0x00 should be EOB, got {:08X}",
            eob.raw()
        );
    }

    #[test]
    fn test_build_fixed_dist_table() {
        // Build fixed Huffman table for distances (all 5-bit codes)
        let code_lengths = [5u8; 32];

        let table = DistTable::build(&code_lengths).expect("Failed to build table");

        // Test lookup for distance code 0
        let entry = table.resolve(0b00000); // 5-bit code for 0
        assert!(!entry.is_exceptional());
        assert_eq!(entry.distance_base(), 1);
        assert_eq!(entry.total_bits(), 5);
    }

    #[test]
    fn test_entry_format_matches_libdeflate() {
        // Verify our entry format exactly matches libdeflate's
        // From libdeflate: ENTRY(literal) = HUFFDEC_LITERAL | ((u32)literal << 16)

        let entry = LitLenEntry::literal(65, 8); // 'A'
        assert_eq!(entry.raw() & HUFFDEC_LITERAL, HUFFDEC_LITERAL);
        assert_eq!((entry.raw() >> 16) & 0xFF, 65);

        // EOB: HUFFDEC_EXCEPTIONAL | HUFFDEC_END_OF_BLOCK
        let eob = LitLenEntry::end_of_block(7);
        assert_eq!(
            eob.raw() & (HUFFDEC_EXCEPTIONAL | HUFFDEC_END_OF_BLOCK),
            HUFFDEC_EXCEPTIONAL | HUFFDEC_END_OF_BLOCK
        );
    }
}