Skip to main content

brotli/enc/
histogram.rs

1use core;
2use core::cmp::min;
3
4use super::super::alloc;
5use super::super::alloc::{SliceWrapper, SliceWrapperMut};
6use super::block_split::BlockSplit;
7use super::command::Command;
8use super::constants::{
9    kSigned3BitContextLookup, kUTF8ContextLookup, BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS,
10};
11use super::util::floatX;
12use super::vectorization::Mem256i;
13
14//#[derive(Clone)] clone is broken for arrays > 32
15pub struct HistogramLiteral {
16    pub data_: [u32; 256],
17    pub total_count_: usize,
18    pub bit_cost_: floatX,
19}
20impl Clone for HistogramLiteral {
21    #[inline(always)]
22    fn clone(&self) -> HistogramLiteral {
23        HistogramLiteral {
24            data_: self.data_,
25            total_count_: self.total_count_,
26            bit_cost_: self.bit_cost_,
27        }
28    }
29}
30impl Default for HistogramLiteral {
31    #[inline(always)]
32    fn default() -> HistogramLiteral {
33        HistogramLiteral {
34            data_: [0; 256],
35            total_count_: 0,
36            bit_cost_: 3.402e+38,
37        }
38    }
39}
40//#[derive(Clone)] clone is broken for arrays > 32
41pub struct HistogramCommand {
42    pub data_: [u32; 704],
43    pub total_count_: usize,
44    pub bit_cost_: floatX,
45}
46impl Clone for HistogramCommand {
47    #[inline(always)]
48    fn clone(&self) -> HistogramCommand {
49        HistogramCommand {
50            data_: self.data_,
51            total_count_: self.total_count_,
52            bit_cost_: self.bit_cost_,
53        }
54    }
55}
56impl Default for HistogramCommand {
57    #[inline(always)]
58    fn default() -> HistogramCommand {
59        HistogramCommand {
60            data_: [0; 704],
61            total_count_: 0,
62            bit_cost_: 3.402e+38,
63        }
64    }
65}
66//#[derive(Clone)] // #derive is broken for arrays > 32
67
68pub struct HistogramDistance {
69    pub data_: [u32; BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS],
70    pub total_count_: usize,
71    pub bit_cost_: floatX,
72}
73impl Clone for HistogramDistance {
74    fn clone(&self) -> HistogramDistance {
75        HistogramDistance {
76            data_: self.data_,
77            total_count_: self.total_count_,
78            bit_cost_: self.bit_cost_,
79        }
80    }
81}
82impl Default for HistogramDistance {
83    fn default() -> HistogramDistance {
84        HistogramDistance {
85            data_: [0; BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS],
86            total_count_: 0,
87            bit_cost_: 3.402e+38,
88        }
89    }
90}
91
92pub trait CostAccessors {
93    type i32vec: Sized + SliceWrapper<Mem256i> + SliceWrapperMut<Mem256i>;
94    fn make_nnz_storage() -> Self::i32vec;
95    fn total_count(&self) -> usize;
96    fn bit_cost(&self) -> floatX;
97    fn set_bit_cost(&mut self, cost: floatX);
98    fn set_total_count(&mut self, count: usize);
99}
100impl SliceWrapper<u32> for HistogramLiteral {
101    #[inline(always)]
102    fn slice(&self) -> &[u32] {
103        &self.data_[..]
104    }
105}
106impl SliceWrapperMut<u32> for HistogramLiteral {
107    #[inline(always)]
108    fn slice_mut(&mut self) -> &mut [u32] {
109        &mut self.data_[..]
110    }
111}
112pub struct Array264i([Mem256i; 33]);
113impl SliceWrapperMut<Mem256i> for Array264i {
114    #[inline(always)]
115    fn slice_mut(&mut self) -> &mut [Mem256i] {
116        &mut self.0[..]
117    }
118}
119
120impl SliceWrapper<Mem256i> for Array264i {
121    #[inline(always)]
122    fn slice(&self) -> &[Mem256i] {
123        &self.0[..]
124    }
125}
126impl Default for Array264i {
127    #[inline(always)]
128    fn default() -> Array264i {
129        Array264i([Mem256i::default(); 33])
130    }
131}
132pub struct Array528i([Mem256i; 66]);
133impl SliceWrapperMut<Mem256i> for Array528i {
134    #[inline(always)]
135    fn slice_mut(&mut self) -> &mut [Mem256i] {
136        &mut self.0[..]
137    }
138}
139impl SliceWrapper<Mem256i> for Array528i {
140    #[inline(always)]
141    fn slice(&self) -> &[Mem256i] {
142        &self.0[..]
143    }
144}
145impl Default for Array528i {
146    #[inline(always)]
147    fn default() -> Array528i {
148        Array528i([Mem256i::default(); 66])
149    }
150}
151
152pub struct Array712i([Mem256i; 89]);
153impl SliceWrapperMut<Mem256i> for Array712i {
154    #[inline(always)]
155    fn slice_mut(&mut self) -> &mut [Mem256i] {
156        &mut self.0[..]
157    }
158}
159impl SliceWrapper<Mem256i> for Array712i {
160    #[inline(always)]
161    fn slice(&self) -> &[Mem256i] {
162        &self.0[..]
163    }
164}
165impl Default for Array712i {
166    #[inline(always)]
167    fn default() -> Array712i {
168        Array712i([Mem256i::default(); 89])
169    }
170}
171
172pub struct EmptyIVec {}
173
174impl SliceWrapperMut<Mem256i> for EmptyIVec {
175    #[inline(always)]
176    fn slice_mut(&mut self) -> &mut [Mem256i] {
177        &mut []
178    }
179}
180impl SliceWrapper<Mem256i> for EmptyIVec {
181    #[inline(always)]
182    fn slice(&self) -> &[Mem256i] {
183        &[]
184    }
185}
186
187impl Default for EmptyIVec {
188    #[inline(always)]
189    fn default() -> EmptyIVec {
190        EmptyIVec {}
191    }
192}
193
194#[cfg(feature = "vector_scratch_space")]
195pub type HistogramLiteralScratch = Array264i;
196
197#[cfg(not(feature = "vector_scratch_space"))]
198pub type HistogramLiteralScratch = EmptyIVec;
199
200impl CostAccessors for HistogramLiteral {
201    type i32vec = HistogramLiteralScratch;
202    fn make_nnz_storage() -> Self::i32vec {
203        HistogramLiteralScratch::default()
204    }
205    #[inline(always)]
206    fn total_count(&self) -> usize {
207        self.total_count_
208    }
209    #[inline(always)]
210    fn bit_cost(&self) -> floatX {
211        self.bit_cost_
212    }
213    #[inline(always)]
214    fn set_bit_cost(&mut self, data: floatX) {
215        self.bit_cost_ = data;
216    }
217    #[inline(always)]
218    fn set_total_count(&mut self, data: usize) {
219        self.total_count_ = data;
220    }
221}
222
223impl SliceWrapper<u32> for HistogramCommand {
224    #[inline(always)]
225    fn slice(&self) -> &[u32] {
226        &self.data_[..]
227    }
228}
229impl SliceWrapperMut<u32> for HistogramCommand {
230    #[inline(always)]
231    fn slice_mut(&mut self) -> &mut [u32] {
232        &mut self.data_[..]
233    }
234}
235
236#[cfg(feature = "vector_scratch_space")]
237pub type HistogramCommandScratch = Array712i;
238
239#[cfg(not(feature = "vector_scratch_space"))]
240pub type HistogramCommandScratch = EmptyIVec;
241
242impl CostAccessors for HistogramCommand {
243    type i32vec = HistogramCommandScratch;
244    fn make_nnz_storage() -> Self::i32vec {
245        HistogramCommandScratch::default()
246    }
247    #[inline(always)]
248    fn total_count(&self) -> usize {
249        self.total_count_
250    }
251    #[inline(always)]
252    fn bit_cost(&self) -> floatX {
253        self.bit_cost_
254    }
255    #[inline(always)]
256    fn set_bit_cost(&mut self, data: floatX) {
257        self.bit_cost_ = data;
258    }
259    #[inline(always)]
260    fn set_total_count(&mut self, data: usize) {
261        self.total_count_ = data;
262    }
263}
264
265impl SliceWrapper<u32> for HistogramDistance {
266    #[inline(always)]
267    fn slice(&self) -> &[u32] {
268        &self.data_[..]
269    }
270}
271impl SliceWrapperMut<u32> for HistogramDistance {
272    #[inline(always)]
273    fn slice_mut(&mut self) -> &mut [u32] {
274        &mut self.data_[..]
275    }
276}
277
278#[cfg(feature = "vector_scratch_space")]
279pub type HistogramDistanceScratch = Array528i;
280
281#[cfg(not(feature = "vector_scratch_space"))]
282pub type HistogramDistanceScratch = EmptyIVec;
283
284impl CostAccessors for HistogramDistance {
285    type i32vec = HistogramDistanceScratch;
286    fn make_nnz_storage() -> Self::i32vec {
287        HistogramDistanceScratch::default()
288    }
289
290    #[inline(always)]
291    fn total_count(&self) -> usize {
292        self.total_count_
293    }
294    #[inline(always)]
295    fn bit_cost(&self) -> floatX {
296        self.bit_cost_
297    }
298    #[inline(always)]
299    fn set_bit_cost(&mut self, data: floatX) {
300        self.bit_cost_ = data;
301    }
302    #[inline(always)]
303    fn set_total_count(&mut self, data: usize) {
304        self.total_count_ = data;
305    }
306}
307
308#[derive(Copy, Clone)]
309pub enum ContextType {
310    CONTEXT_LSB6 = 0,
311    CONTEXT_MSB6 = 1,
312    CONTEXT_UTF8 = 2,
313    CONTEXT_SIGNED = 3,
314}
315
316impl Default for ContextType {
317    #[inline(always)]
318    fn default() -> ContextType {
319        ContextType::CONTEXT_LSB6
320    }
321}
322
323pub struct BlockSplitIterator<'a, Alloc: alloc::Allocator<u8> + 'a + alloc::Allocator<u32> + 'a> {
324    pub split_: &'a BlockSplit<Alloc>,
325    pub idx_: usize,
326    pub type_: usize,
327    pub length_: usize,
328}
329
330impl<'a, Alloc: alloc::Allocator<u8> + alloc::Allocator<u32> + 'a> BlockSplitIterator<'a, Alloc> {
331    fn new(split: &'a BlockSplit<Alloc>) -> Self {
332        Self {
333            split_: split,
334            idx_: 0,
335            type_: 0,
336            length_: if !split.lengths.slice().is_empty() {
337                split.lengths.slice()[0] as usize
338            } else {
339                0
340            },
341        }
342    }
343
344    fn next(&mut self) {
345        if self.length_ == 0 {
346            self.idx_ = self.idx_.wrapping_add(1);
347            self.type_ = self.split_.types.slice()[self.idx_] as usize;
348            self.length_ = self.split_.lengths.slice()[self.idx_] as usize;
349        }
350        self.length_ = self.length_.wrapping_sub(1);
351    }
352}
353
354pub fn HistogramAddItem<HistogramType: SliceWrapper<u32> + SliceWrapperMut<u32> + CostAccessors>(
355    xself: &mut HistogramType,
356    val: usize,
357) {
358    {
359        let _rhs = 1;
360        let _lhs = &mut xself.slice_mut()[val];
361        let val = (*_lhs).wrapping_add(_rhs as u32);
362        *_lhs = val;
363    }
364    let new_count = xself.total_count().wrapping_add(1);
365    xself.set_total_count(new_count);
366}
367pub fn HistogramAddVector<
368    HistogramType: SliceWrapper<u32> + SliceWrapperMut<u32> + CostAccessors,
369    IntegerType: Sized + Clone,
370>(
371    xself: &mut HistogramType,
372    p: &[IntegerType],
373    n: usize,
374) where
375    u64: core::convert::From<IntegerType>,
376{
377    let new_tc = xself.total_count().wrapping_add(n);
378    xself.set_total_count(new_tc);
379    for p_item in p[..n].iter() {
380        let _rhs = 1;
381        let index: usize = u64::from(p_item.clone()) as usize;
382        let _lhs = &mut xself.slice_mut()[index];
383        *_lhs = (*_lhs).wrapping_add(_rhs as u32);
384    }
385}
386
387#[inline(always)]
388pub fn HistogramClear<HistogramType: SliceWrapperMut<u32> + CostAccessors>(
389    xself: &mut HistogramType,
390) {
391    for data_elem in xself.slice_mut().iter_mut() {
392        *data_elem = 0;
393    }
394    xself.set_total_count(0);
395    xself.set_bit_cost(3.402e+38);
396}
397pub fn ClearHistograms<HistogramType: SliceWrapperMut<u32> + CostAccessors>(
398    array: &mut [HistogramType],
399    length: usize,
400) {
401    for item in array[..length].iter_mut() {
402        HistogramClear(item)
403    }
404}
405
406#[inline(always)]
407pub fn HistogramAddHistogram<
408    HistogramType: SliceWrapperMut<u32> + SliceWrapper<u32> + CostAccessors,
409>(
410    xself: &mut HistogramType,
411    v: &HistogramType,
412) {
413    let old_total_count = xself.total_count();
414    xself.set_total_count(old_total_count + (*v).total_count());
415    let h0 = xself.slice_mut();
416    let h1 = v.slice();
417    let n = min(h0.len(), h1.len());
418    for i in 0..n {
419        let h0val = &mut h0[i];
420        let val = h0val.wrapping_add(h1[i]);
421        *h0val = val;
422    }
423}
424pub fn HistogramSelfAddHistogram<
425    HistogramType: SliceWrapperMut<u32> + SliceWrapper<u32> + CostAccessors,
426>(
427    xself: &mut [HistogramType],
428    i0: usize,
429    i1: usize,
430) {
431    let tc_new = xself[i1].total_count();
432    let tc_old = xself[i0].total_count();
433    xself[i0].set_total_count(tc_old.wrapping_add(tc_new));
434    let h0 = xself[i0].slice().len();
435    let h0a = xself[i0].slice().len();
436    let h1 = xself[i1].slice().len();
437    let n = min(h0, min(h0a, h1));
438    for h_index in 0..n {
439        let val = xself[i0].slice()[h_index].wrapping_add(xself[i1].slice()[h_index]);
440        xself[i0].slice_mut()[h_index] = val;
441    }
442}
443
444#[inline(always)]
445pub fn Context(p1: u8, p2: u8, mode: ContextType) -> u8 {
446    match mode {
447        ContextType::CONTEXT_SIGNED => {
448            (((kSigned3BitContextLookup[p1 as usize] as i32) << 3)
449                + kSigned3BitContextLookup[p2 as usize] as i32) as u8
450        }
451        ContextType::CONTEXT_UTF8 => {
452            (kUTF8ContextLookup[p1 as usize] as i32
453                | kUTF8ContextLookup[(p2 as i32 + 256i32) as usize] as i32) as u8
454        }
455        ContextType::CONTEXT_MSB6 => (p1 as i32 >> 2) as u8,
456        ContextType::CONTEXT_LSB6 => (p1 as i32 & 0x3fi32) as u8, /* else {
457                                                                  0u8
458                                                                  }*/
459    }
460}
461
462pub fn BrotliBuildHistogramsWithContext<'a, Alloc: alloc::Allocator<u8> + alloc::Allocator<u32>>(
463    cmds: &[Command],
464    num_commands: usize,
465    literal_split: &BlockSplit<Alloc>,
466    insert_and_copy_split: &BlockSplit<Alloc>,
467    dist_split: &BlockSplit<Alloc>,
468    ringbuffer: &[u8],
469    start_pos: usize,
470    mask: usize,
471    mut prev_byte: u8,
472    mut prev_byte2: u8,
473    context_modes: &[ContextType],
474    literal_histograms: &mut [HistogramLiteral],
475    insert_and_copy_histograms: &mut [HistogramCommand],
476    copy_dist_histograms: &mut [HistogramDistance],
477) {
478    let mut pos: usize = start_pos;
479    let mut literal_it: BlockSplitIterator<Alloc>;
480    let mut insert_and_copy_it: BlockSplitIterator<Alloc>;
481    let mut dist_it: BlockSplitIterator<Alloc>;
482    literal_it = BlockSplitIterator::new(literal_split);
483    insert_and_copy_it = BlockSplitIterator::new(insert_and_copy_split);
484    dist_it = BlockSplitIterator::new(dist_split);
485    for i in 0usize..num_commands {
486        let cmd = &cmds[i];
487        let mut j: usize;
488        insert_and_copy_it.next();
489        HistogramAddItem(
490            &mut insert_and_copy_histograms[insert_and_copy_it.type_],
491            cmd.cmd_prefix_ as usize,
492        );
493        j = cmd.insert_len_ as usize;
494        while j != 0usize {
495            {
496                literal_it.next();
497                let context: usize = if !context_modes.is_empty() {
498                    (literal_it.type_ << 6).wrapping_add(Context(
499                        prev_byte,
500                        prev_byte2,
501                        context_modes[literal_it.type_],
502                    ) as usize)
503                } else {
504                    literal_it.type_
505                };
506                HistogramAddItem(
507                    &mut literal_histograms[(context as usize)],
508                    ringbuffer[(pos & mask)] as usize,
509                );
510                prev_byte2 = prev_byte;
511                prev_byte = ringbuffer[(pos & mask)];
512                pos = pos.wrapping_add(1);
513            }
514            j = j.wrapping_sub(1);
515        }
516        pos = pos.wrapping_add(cmd.copy_len() as usize);
517        if cmd.copy_len() != 0 {
518            prev_byte2 = ringbuffer[(pos.wrapping_sub(2) & mask)];
519            prev_byte = ringbuffer[(pos.wrapping_sub(1) & mask)];
520            if cmd.cmd_prefix_ as i32 >= 128i32 {
521                dist_it.next();
522                let context: usize =
523                    (dist_it.type_ << 2).wrapping_add(cmd.distance_context() as usize);
524                HistogramAddItem(
525                    &mut copy_dist_histograms[(context as usize)],
526                    cmd.dist_prefix_ as usize & 0x3ff,
527                );
528            }
529        }
530    }
531}