zstd-complete 0.3.0

A pure Rust Zstandard compressor and decompressor with bounded streaming I/O
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
use alloc::vec::Vec;

use super::table::{
    build_table_from_probabilities, build_table_from_probabilities_with_scratch, FSETable,
    FSETableBuildScratch,
};

pub fn build_table_from_data(
    data: impl Iterator<Item = u8>,
    max_log: u8,
    avoid_0_numbit: bool,
) -> FSETable {
    let mut counts = [0; 256];
    let mut max_symbol = 0;
    for symbol in data {
        let symbol = usize::from(symbol);
        counts[symbol] += 1;
        max_symbol = max_symbol.max(symbol);
    }
    build_table_from_counts(&counts[..=max_symbol], max_log, avoid_0_numbit)
}

pub(crate) fn build_table_from_data_with_scratch(
    data: impl Iterator<Item = u8>,
    max_log: u8,
    avoid_0_numbit: bool,
    scratch: &mut FSETableBuildScratch,
) -> Result<FSETable, crate::workspace::CapacityError> {
    let mut counts = [0; 256];
    let mut max_symbol = 0;
    for symbol in data {
        let symbol = usize::from(symbol);
        counts[symbol] += 1;
        max_symbol = max_symbol.max(symbol);
    }
    build_table_from_counts_with_scratch(&counts[..=max_symbol], max_log, avoid_0_numbit, scratch)
}

fn build_table_from_counts(counts: &[usize], max_log: u8, avoid_0_numbit: bool) -> FSETable {
    let (probs, acc_log) = normalized_probabilities_from_counts(counts, max_log, avoid_0_numbit);
    build_table_from_probabilities(&probs, acc_log)
}

fn build_table_from_counts_with_scratch(
    counts: &[usize],
    max_log: u8,
    avoid_0_numbit: bool,
    scratch: &mut FSETableBuildScratch,
) -> Result<FSETable, crate::workspace::CapacityError> {
    let mut probabilities = [0_i32; 256];
    let acc_log = normalized_probabilities_from_counts_into(
        counts,
        max_log,
        avoid_0_numbit,
        &mut probabilities[..counts.len()],
    );
    build_table_from_probabilities_with_scratch(&probabilities[..counts.len()], acc_log, scratch)
}

pub(crate) fn normalized_probabilities_from_counts_into(
    counts: &[usize],
    max_log: u8,
    avoid_0_numbit: bool,
    probabilities: &mut [i32],
) -> u8 {
    assert_eq!(counts.len(), probabilities.len());
    if max_log <= 6 {
        return old_normalize_counts_into(counts, max_log, avoid_0_numbit, probabilities);
    }

    let total = counts.iter().sum::<usize>();
    assert!(total > 0);
    let max_symbol = counts.iter().rposition(|count| *count > 0).unwrap_or(0);
    let acc_log = optimal_table_log(max_log, total, max_symbol);
    let low_prob_count = if total >= 2048 { -1 } else { 1 };
    if normalize_counts_with_total_into(counts, total, acc_log, low_prob_count, probabilities) {
        acc_log
    } else {
        old_normalize_counts_into(counts, max_log, avoid_0_numbit, probabilities)
    }
}

pub(crate) fn normalized_probabilities_from_counts(
    counts: &[usize],
    max_log: u8,
    avoid_0_numbit: bool,
) -> (Vec<i32>, u8) {
    let mut probabilities = alloc::vec![0_i32; counts.len()];
    let acc_log = normalized_probabilities_from_counts_into(
        counts,
        max_log,
        avoid_0_numbit,
        &mut probabilities,
    );
    (probabilities, acc_log)
}

pub(crate) fn normalize_counts_with_table_log(
    counts: &[usize],
    total: usize,
    table_log: u8,
    max_log: u8,
    low_prob_count: i32,
    avoid_0_numbit: bool,
) -> (Vec<i32>, u8) {
    normalize_counts_with_total(counts, total, table_log, low_prob_count)
        .map(|probs| (probs, table_log))
        .unwrap_or_else(|| old_normalize_counts(counts, max_log, avoid_0_numbit))
}

pub(crate) fn normalize_counts_with_table_log_into(
    counts: &[usize],
    total: usize,
    table_log: u8,
    max_log: u8,
    low_prob_count: i32,
    avoid_0_numbit: bool,
    probabilities: &mut [i32],
) -> u8 {
    assert_eq!(counts.len(), probabilities.len());
    if normalize_counts_with_total_into(counts, total, table_log, low_prob_count, probabilities) {
        table_log
    } else {
        old_normalize_counts_into(counts, max_log, avoid_0_numbit, probabilities)
    }
}

/// C-width counterpart used by the native sequence-count transaction.
/// zstd keeps these bounded histograms as `unsigned` through normalization.
#[cfg(test)]
pub(crate) fn normalize_u32_counts_with_table_log(
    counts: &[u32],
    total: u32,
    table_log: u8,
    max_log: u8,
    low_prob_count: i32,
    avoid_0_numbit: bool,
) -> (Vec<i32>, u8) {
    normalize_u32_counts_with_total(counts, total, table_log, low_prob_count)
        .map(|probs| (probs, table_log))
        .unwrap_or_else(|| {
            let widened = counts
                .iter()
                .copied()
                .map(|count| count as usize)
                .collect::<Vec<_>>();
            old_normalize_counts(&widened, max_log, avoid_0_numbit)
        })
}

pub(crate) fn normalize_u32_counts_with_table_log_into(
    counts: &[u32],
    total: u32,
    table_log: u8,
    max_log: u8,
    low_prob_count: i32,
    avoid_0_numbit: bool,
    probabilities: &mut [i32],
) -> u8 {
    assert_eq!(counts.len(), probabilities.len());
    if normalize_u32_counts_with_total_into(counts, total, table_log, low_prob_count, probabilities)
    {
        table_log
    } else {
        let mut widened = [0_usize; 256];
        for (destination, source) in widened.iter_mut().zip(counts.iter().copied()) {
            *destination = source as usize;
        }
        old_normalize_counts_into(
            &widened[..counts.len()],
            max_log,
            avoid_0_numbit,
            probabilities,
        )
    }
}

/// Build the FSE table used for Huffman weight descriptions.
///
/// zstd's `HUF_compressWeights()` normalizes low-probability symbols as `1`
/// instead of `-1`, which can produce a larger NCount header but a shorter
/// compressed weight stream.
pub(crate) fn build_huffman_weight_table_from_data(data: &[u8], max_log: u8) -> FSETable {
    let mut counts = [0; 256];
    let mut max_symbol = 0;
    for symbol in data {
        let symbol = usize::from(*symbol);
        counts[symbol] += 1;
        max_symbol = max_symbol.max(symbol);
    }

    let counts = &counts[..=max_symbol];
    let total = data.len();
    let acc_log = optimal_table_log(max_log, total, max_symbol);
    let (probs, acc_log) = normalize_counts_with_total(counts, total, acc_log, 1)
        .map(|probs| (probs, acc_log))
        .unwrap_or_else(|| old_normalize_counts(counts, max_log, false));
    build_table_from_probabilities(&probs, acc_log)
}

pub(crate) fn build_huffman_weight_table_from_data_with_scratch(
    data: &[u8],
    max_log: u8,
    scratch: &mut FSETableBuildScratch,
) -> Result<FSETable, crate::workspace::CapacityError> {
    let mut counts = [0; 256];
    let mut max_symbol = 0;
    for symbol in data {
        let symbol = usize::from(*symbol);
        counts[symbol] += 1;
        max_symbol = max_symbol.max(symbol);
    }

    let counts = &counts[..=max_symbol];
    let total = data.len();
    let acc_log = optimal_table_log(max_log, total, max_symbol);
    let mut probabilities = [0_i32; 256];
    let probabilities = &mut probabilities[..counts.len()];
    let acc_log = if normalize_counts_with_total_into(counts, total, acc_log, 1, probabilities) {
        acc_log
    } else {
        old_normalize_counts_into(counts, max_log, false, probabilities)
    };
    build_table_from_probabilities_with_scratch(probabilities, acc_log, scratch)
}

pub(crate) fn optimal_table_log(max_log: u8, total: usize, max_symbol: usize) -> u8 {
    const MIN_TABLE_LOG: u8 = 5;
    // zstd's default FSE_MAX_MEMORY_USAGE is 14, so FSE_MAX_TABLELOG is 12.
    const MAX_TABLE_LOG: u8 = 12;

    let max_bits_src = (total - 1).ilog2().saturating_sub(2);
    let min_bits_src = total.ilog2() + 1;
    let min_bits_symbols = if max_symbol == 0 {
        0
    } else {
        max_symbol.ilog2() + 2
    };
    let min_bits = min_bits_src.min(min_bits_symbols);
    let table_log = u32::from(max_log).min(max_bits_src).max(min_bits);
    table_log.clamp(u32::from(MIN_TABLE_LOG), u32::from(MAX_TABLE_LOG)) as u8
}

pub(crate) fn optimal_table_log_u32(max_log: u8, total: u32, max_symbol: u32) -> u8 {
    const MIN_TABLE_LOG: u8 = 5;
    const MAX_TABLE_LOG: u8 = 12;

    let max_bits_src = (total - 1).ilog2().saturating_sub(2);
    let min_bits_src = total.ilog2() + 1;
    let min_bits_symbols = if max_symbol == 0 {
        0
    } else {
        max_symbol.ilog2() + 2
    };
    let min_bits = min_bits_src.min(min_bits_symbols);
    let table_log = u32::from(max_log).min(max_bits_src).max(min_bits);
    table_log.clamp(u32::from(MIN_TABLE_LOG), u32::from(MAX_TABLE_LOG)) as u8
}

#[cfg(test)]
pub(super) fn normalize_counts(
    counts: &[usize],
    table_log: u8,
    low_prob_count: i32,
) -> Option<Vec<i32>> {
    let total = counts.iter().sum::<usize>();
    normalize_counts_with_total(counts, total, table_log, low_prob_count)
}

fn normalize_counts_with_total(
    counts: &[usize],
    total: usize,
    table_log: u8,
    low_prob_count: i32,
) -> Option<Vec<i32>> {
    let mut normalized = alloc::vec![0_i32; counts.len()];
    normalize_counts_with_total_into(counts, total, table_log, low_prob_count, &mut normalized)
        .then_some(normalized)
}

fn normalize_counts_with_total_into(
    counts: &[usize],
    total: usize,
    table_log: u8,
    low_prob_count: i32,
    normalized: &mut [i32],
) -> bool {
    debug_assert_eq!(total, counts.iter().sum::<usize>());
    debug_assert_eq!(counts.len(), normalized.len());
    normalized.fill(0);
    let table_size = 1i32 << table_log;
    let low_threshold = total >> table_log;
    let scale = 62 - table_log;
    let step = (1u64 << 62) / total as u64;
    let v_step = 1u64 << (scale - 20);
    let rtb_table = [
        0u64, 473_195, 504_333, 520_860, 550_000, 700_000, 750_000, 830_000,
    ];

    let mut still_to_distribute = table_size;
    let mut largest = 0usize;
    let mut largest_probability = 0i32;

    for (symbol, count) in counts.iter().copied().enumerate() {
        if count == 0 {
            continue;
        }
        if count == total {
            normalized[symbol] = table_size;
            return true;
        }
        if count <= low_threshold {
            normalized[symbol] = low_prob_count;
            still_to_distribute -= 1;
            continue;
        }

        let scaled = count as u64 * step;
        let mut probability = (scaled >> scale) as i32;
        if probability < 8 {
            let rest_to_beat = v_step * rtb_table[probability as usize];
            if scaled - ((probability as u64) << scale) > rest_to_beat {
                probability += 1;
            }
        }
        if probability > largest_probability {
            largest_probability = probability;
            largest = symbol;
        }
        normalized[symbol] = probability;
        still_to_distribute -= probability;
    }

    if -still_to_distribute >= normalized[largest] >> 1 {
        normalize_counts_slow_into(counts, total, table_log, low_prob_count, normalized)
    } else {
        normalized[largest] += still_to_distribute;
        true
    }
}

#[cfg(test)]
fn normalize_u32_counts_with_total(
    counts: &[u32],
    total: u32,
    table_log: u8,
    low_prob_count: i32,
) -> Option<Vec<i32>> {
    let mut normalized = alloc::vec![0_i32; counts.len()];
    normalize_u32_counts_with_total_into(counts, total, table_log, low_prob_count, &mut normalized)
        .then_some(normalized)
}

fn normalize_u32_counts_with_total_into(
    counts: &[u32],
    total: u32,
    table_log: u8,
    low_prob_count: i32,
    normalized: &mut [i32],
) -> bool {
    debug_assert_eq!(total, counts.iter().sum::<u32>());
    debug_assert_eq!(counts.len(), normalized.len());
    normalized.fill(0);
    let table_size = 1i32 << table_log;
    let low_threshold = total >> table_log;
    let scale = 62 - table_log;
    let step = (1u64 << 62) / u64::from(total);
    let v_step = 1u64 << (scale - 20);
    let rtb_table = [
        0u64, 473_195, 504_333, 520_860, 550_000, 700_000, 750_000, 830_000,
    ];

    let mut still_to_distribute = table_size;
    let mut largest = 0usize;
    let mut largest_probability = 0i32;

    for (symbol, count) in counts.iter().copied().enumerate() {
        if count == 0 {
            continue;
        }
        if count == total {
            normalized[symbol] = table_size;
            return true;
        }
        if count <= low_threshold {
            normalized[symbol] = low_prob_count;
            still_to_distribute -= 1;
            continue;
        }

        let scaled = u64::from(count) * step;
        let mut probability = (scaled >> scale) as i32;
        if probability < 8 {
            let rest_to_beat = v_step * rtb_table[probability as usize];
            if scaled - ((probability as u64) << scale) > rest_to_beat {
                probability += 1;
            }
        }
        if probability > largest_probability {
            largest_probability = probability;
            largest = symbol;
        }
        normalized[symbol] = probability;
        still_to_distribute -= probability;
    }

    if -still_to_distribute >= normalized[largest] >> 1 {
        normalize_u32_counts_slow_into(counts, total, table_log, low_prob_count, normalized)
    } else {
        normalized[largest] += still_to_distribute;
        true
    }
}

fn normalize_u32_counts_slow_into(
    counts: &[u32],
    total: u32,
    table_log: u8,
    low_prob_count: i32,
    normalized: &mut [i32],
) -> bool {
    const NOT_YET_ASSIGNED: i32 = -2;

    normalized.fill(0);
    let mut distributed = 0u32;
    let mut remaining_total = total;
    let mut low_one = (remaining_total * 3) >> (table_log + 1);
    let low_threshold = remaining_total >> table_log;

    for (symbol, count) in counts.iter().copied().enumerate() {
        if count == 0 {
            continue;
        }
        if count <= low_threshold {
            normalized[symbol] = low_prob_count;
            distributed += 1;
            remaining_total -= count;
            continue;
        }
        if count <= low_one {
            normalized[symbol] = 1;
            distributed += 1;
            remaining_total -= count;
            continue;
        }
        normalized[symbol] = NOT_YET_ASSIGNED;
    }

    let mut to_distribute = (1u32 << table_log) - distributed;
    if to_distribute == 0 {
        return true;
    }

    if remaining_total / to_distribute > low_one {
        low_one = (remaining_total * 3) / (to_distribute * 2);
        for (symbol, count) in counts.iter().copied().enumerate() {
            if normalized[symbol] == NOT_YET_ASSIGNED && count <= low_one {
                normalized[symbol] = 1;
                distributed += 1;
                remaining_total -= count;
            }
        }
        to_distribute = (1u32 << table_log) - distributed;
    }

    if distributed as usize == counts.len() {
        let max_symbol = counts
            .iter()
            .copied()
            .enumerate()
            .max_by_key(|(_, count)| *count)
            .map(|(symbol, _)| symbol)
            .expect("a non-empty histogram has a maximum");
        normalized[max_symbol] += to_distribute as i32;
        return true;
    }

    if remaining_total == 0 {
        let mut symbol = 0usize;
        while to_distribute > 0 {
            if normalized[symbol] > 0 {
                normalized[symbol] += 1;
                to_distribute -= 1;
            }
            symbol = (symbol + 1) % counts.len();
        }
        return true;
    }

    let v_step_log = 62 - table_log;
    let mid = (1u64 << (v_step_log - 1)) - 1;
    let r_step =
        (((1u64 << v_step_log) * u64::from(to_distribute)) + mid) / u64::from(remaining_total);
    let mut tmp_total = mid;
    for (symbol, count) in counts.iter().copied().enumerate() {
        if normalized[symbol] == NOT_YET_ASSIGNED {
            let end = tmp_total + u64::from(count) * r_step;
            let start = tmp_total >> v_step_log;
            let finish = end >> v_step_log;
            let weight = finish - start;
            if weight < 1 {
                return false;
            }
            normalized[symbol] = weight as i32;
            tmp_total = end;
        }
    }

    true
}

fn normalize_counts_slow_into(
    counts: &[usize],
    total: usize,
    table_log: u8,
    low_prob_count: i32,
    normalized: &mut [i32],
) -> bool {
    const NOT_YET_ASSIGNED: i32 = -2;

    normalized.fill(0);
    let mut distributed = 0usize;
    let mut remaining_total = total;
    let mut low_one = (remaining_total * 3) >> (table_log + 1);
    let low_threshold = remaining_total >> table_log;

    for (symbol, count) in counts.iter().copied().enumerate() {
        if count == 0 {
            continue;
        }
        if count <= low_threshold {
            normalized[symbol] = low_prob_count;
            distributed += 1;
            remaining_total -= count;
            continue;
        }
        if count <= low_one {
            normalized[symbol] = 1;
            distributed += 1;
            remaining_total -= count;
            continue;
        }
        normalized[symbol] = NOT_YET_ASSIGNED;
    }

    let mut to_distribute = (1usize << table_log) - distributed;
    if to_distribute == 0 {
        return true;
    }

    if remaining_total / to_distribute > low_one {
        low_one = (remaining_total * 3) / (to_distribute * 2);
        for (symbol, count) in counts.iter().copied().enumerate() {
            if normalized[symbol] == NOT_YET_ASSIGNED && count <= low_one {
                normalized[symbol] = 1;
                distributed += 1;
                remaining_total -= count;
            }
        }
        to_distribute = (1usize << table_log) - distributed;
    }

    if distributed == counts.len() {
        let max_symbol = counts
            .iter()
            .copied()
            .enumerate()
            .max_by_key(|(_, count)| *count)
            .map(|(symbol, _)| symbol)
            .expect("a non-empty histogram has a maximum");
        normalized[max_symbol] += to_distribute as i32;
        return true;
    }

    if remaining_total == 0 {
        let mut symbol = 0usize;
        while to_distribute > 0 {
            if normalized[symbol] > 0 {
                normalized[symbol] += 1;
                to_distribute -= 1;
            }
            symbol = (symbol + 1) % counts.len();
        }
        return true;
    }

    let v_step_log = 62 - table_log;
    let mid = (1u64 << (v_step_log - 1)) - 1;
    let r_step = (((1u64 << v_step_log) * to_distribute as u64) + mid) / remaining_total as u64;
    let mut tmp_total = mid;
    for (symbol, count) in counts.iter().copied().enumerate() {
        if normalized[symbol] == NOT_YET_ASSIGNED {
            let end = tmp_total + count as u64 * r_step;
            let start = tmp_total >> v_step_log;
            let finish = end >> v_step_log;
            let weight = finish - start;
            if weight < 1 {
                return false;
            }
            normalized[symbol] = weight as i32;
            tmp_total = end;
        }
    }

    true
}

fn old_normalize_counts(counts: &[usize], max_log: u8, avoid_0_numbit: bool) -> (Vec<i32>, u8) {
    let mut probs = alloc::vec![0i32; counts.len()];
    let acc_log = old_normalize_counts_into(counts, max_log, avoid_0_numbit, &mut probs);
    (probs, acc_log)
}

fn old_normalize_counts_into(
    counts: &[usize],
    max_log: u8,
    avoid_0_numbit: bool,
    probs: &mut [i32],
) -> u8 {
    debug_assert_eq!(counts.len(), probs.len());
    probs.fill(0);
    let mut min_count = 0;
    for (idx, count) in counts.iter().copied().enumerate() {
        probs[idx] = count as i32;
        if count > 0 && (count < min_count || min_count == 0) {
            min_count = count;
        }
    }

    min_count -= 1;
    let mut max_prob = 0i32;
    for prob in probs.iter_mut() {
        if *prob > 0 {
            *prob -= min_count as i32;
        }
        max_prob = max_prob.max(*prob);
    }

    if max_prob > 0 && max_prob as usize > probs.len() {
        let divisor = max_prob / (probs.len() as i32);
        for prob in probs.iter_mut() {
            if *prob > 0 {
                *prob = (*prob / divisor).max(1)
            }
        }
    }

    let sum = probs.iter().sum::<i32>();
    assert!(sum > 0);
    let sum = sum as usize;
    let acc_log = (sum.ilog2() as u8 + 1).max(5);
    let acc_log = u8::min(acc_log, max_log);

    if sum < 1 << acc_log {
        let diff = (1 << acc_log) - sum;
        let max = probs.iter_mut().max().unwrap();
        *max += diff as i32;
    } else {
        let mut diff = sum - (1 << acc_log);
        while diff > 0 {
            let min = probs.iter_mut().filter(|prob| **prob > 1).min().unwrap();
            let decrease = usize::min(*min as usize - 1, diff);
            diff -= decrease;
            *min -= decrease as i32;
        }
    }
    let max = probs.iter_mut().max().unwrap();
    if avoid_0_numbit && *max > 1 << (acc_log - 1) {
        let redistribute = *max - (1 << (acc_log - 1));
        *max -= redistribute;
        let max = *max;

        let second_max = *probs.iter_mut().filter(|x| **x != max).max().unwrap();
        let second_max = probs.iter_mut().find(|x| **x == second_max).unwrap();
        *second_max += redistribute;
        assert!(*second_max <= max);
    }

    acc_log
}