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
#![allow(dead_code)]
use super::utf8_util::BrotliIsMostlyUTF8;
use super::util::FastLog2f64;

static kMinUTF8Ratio: super::util::floatX = 0.75 as super::util::floatX;

fn brotli_min_size_t(a: usize, b: usize) -> usize {
  if a < b { a } else { b }
}

fn UTF8Position(last: usize, c: usize, clamp: usize) -> usize {
  if c < 128i32 as (usize) {
    0i32 as (usize)
  } else if c >= 192i32 as (usize) {
    brotli_min_size_t(1i32 as (usize), clamp)
  } else if last < 0xe0i32 as (usize) {
    0i32 as (usize)
  } else {
    brotli_min_size_t(2i32 as (usize), clamp)
  }
}

fn DecideMultiByteStatsLevel(pos: usize, len: usize, mask: usize, data: &[u8]) -> usize {
  let mut counts: [usize; 3] = [0usize, 0usize, 0usize];
  let mut max_utf8: usize = 1usize;
  let mut last_c: usize = 0usize;
  let mut i: usize;
  i = 0usize;
  while i < len {
    {
      let c: usize = data[((pos.wrapping_add(i) & mask) as (usize))] as (usize);
      {
        let _rhs = 1;
        let _lhs = &mut counts[UTF8Position(last_c, c, 2usize)];
        *_lhs = (*_lhs).wrapping_add(_rhs as (usize));
      }
      last_c = c;
    }
    i = i.wrapping_add(1 as (usize));
  }
  if counts[2usize] < 500usize {
    max_utf8 = 1usize;
  }
  if counts[1usize].wrapping_add(counts[2usize]) < 25usize {
    max_utf8 = 0usize;
  }
  max_utf8
}

fn EstimateBitCostsForLiteralsUTF8(pos: usize,
                                   len: usize,
                                   mask: usize,
                                   data: &[u8],
                                   cost: &mut [super::util::floatX]) {
  let max_utf8: usize = DecideMultiByteStatsLevel(pos, len, mask, data);
  let mut histogram: [[usize; 256]; 3] = [[0; 256]; 3];
  let window_half: usize = 495usize;
  let in_window: usize = brotli_min_size_t(window_half, len);
  let mut in_window_utf8: [usize; 3] = [0usize, 0usize, 0usize];
  let mut i: usize;
  {
    let mut last_c: usize = 0usize;
    let mut utf8_pos: usize = 0usize;
    i = 0usize;
    while i < in_window {
      {
        let c: usize = data[((pos.wrapping_add(i) & mask) as (usize))] as (usize);
        {
          let _rhs = 1;
          let _lhs = &mut histogram[utf8_pos][c];
          *_lhs = (*_lhs).wrapping_add(_rhs as (usize));
        }
        {
          let _rhs = 1;
          let _lhs = &mut in_window_utf8[utf8_pos];
          *_lhs = (*_lhs).wrapping_add(_rhs as (usize));
        }
        utf8_pos = UTF8Position(last_c, c, max_utf8);
        last_c = c;
      }
      i = i.wrapping_add(1 as (usize));
    }
  }
  i = 0usize;
  while i < len {
    {
      if i >= window_half {
        let c: usize = (if i < window_half.wrapping_add(1usize) {
                          0i32
                        } else {
                          data[((pos.wrapping_add(i).wrapping_sub(window_half).wrapping_sub(1usize) &
                            mask) as (usize))] as (i32)
                        }) as (usize);
        let last_c: usize =
          (if i < window_half.wrapping_add(2usize) {
             0i32
           } else {
             data[((pos.wrapping_add(i).wrapping_sub(window_half).wrapping_sub(2usize) &
               mask) as (usize))] as (i32)
           }) as (usize);
        let utf8_pos2: usize = UTF8Position(last_c, c, max_utf8);
        {
          let _rhs = 1;
          let _lhs = &mut histogram[utf8_pos2][data[((pos.wrapping_add(i).wrapping_sub(window_half) & mask) as
                           (usize))] as (usize)];
          *_lhs = (*_lhs).wrapping_sub(_rhs as (usize));
        }
        {
          let _rhs = 1;
          let _lhs = &mut in_window_utf8[utf8_pos2];
          *_lhs = (*_lhs).wrapping_sub(_rhs as (usize));
        }
      }
      if i.wrapping_add(window_half) < len {
        let c: usize = data[((pos.wrapping_add(i)
            .wrapping_add(window_half)
            .wrapping_sub(1usize) & mask) as (usize))] as (usize);
        let last_c: usize = data[((pos.wrapping_add(i)
            .wrapping_add(window_half)
            .wrapping_sub(2usize) & mask) as (usize))] as (usize);
        let utf8_pos2: usize = UTF8Position(last_c, c, max_utf8);
        {
          let _rhs = 1;
          let _lhs = &mut histogram[utf8_pos2][data[((pos.wrapping_add(i).wrapping_add(window_half) & mask) as
                           (usize))] as (usize)];
          *_lhs = (*_lhs).wrapping_add(_rhs as (usize));
        }
        {
          let _rhs = 1;
          let _lhs = &mut in_window_utf8[utf8_pos2];
          *_lhs = (*_lhs).wrapping_add(_rhs as (usize));
        }
      }
      {
        let c: usize = (if i < 1usize {
                          0i32
                        } else {
                          data[((pos.wrapping_add(i).wrapping_sub(1usize) & mask) as (usize))] as
                          (i32)
                        }) as (usize);
        let last_c: usize = (if i < 2usize {
                               0i32
                             } else {
                               data[((pos.wrapping_add(i).wrapping_sub(2usize) & mask) as
                                (usize))] as (i32)
                             }) as (usize);
        let utf8_pos: usize = UTF8Position(last_c, c, max_utf8);
        let masked_pos: usize = pos.wrapping_add(i) & mask;
        let mut histo: usize = histogram[utf8_pos][data[(masked_pos as (usize))] as (usize)];
          //precision is vital here: lets keep double precision
        let mut lit_cost: f64;
        if histo == 0usize {
          histo = 1usize;
        }
        lit_cost = FastLog2f64(in_window_utf8[utf8_pos] as u64) as f64 - FastLog2f64(histo as u64) as f64;
        lit_cost = lit_cost + 0.02905;
        if lit_cost < 1.0 {
          lit_cost = lit_cost * 0.5;
          lit_cost = lit_cost + 0.5;
        }
        if i < 2000usize {
          lit_cost = lit_cost +
                     (0.7 - (2000usize).wrapping_sub(i) as (f64) / 2000.0 * 0.35 );
        }
        cost[(i as (usize))] = lit_cost as (super::util::floatX);
      }
    }
    i = i.wrapping_add(1 as (usize));
  }
}

pub fn BrotliEstimateBitCostsForLiterals(pos: usize,
                                         len: usize,
                                         mask: usize,
                                         data: &[u8],
                                         cost: &mut [super::util::floatX]) {
  if BrotliIsMostlyUTF8(data, pos, mask, len, kMinUTF8Ratio) != 0 {
    EstimateBitCostsForLiteralsUTF8(pos, len, mask, data, cost);
  } else {
    let mut histogram: [usize; 256] = [0; 256];

    let window_half: usize = 2000usize;
    let mut in_window: usize = brotli_min_size_t(window_half, len);
    let mut i: usize;
    i = 0usize;
    while i < in_window {
      {
        let _rhs = 1;
        let _lhs = &mut histogram[data[((pos.wrapping_add(i) & mask) as (usize))] as (usize)];
        *_lhs = (*_lhs).wrapping_add(_rhs as (usize));
      }
      i = i.wrapping_add(1 as (usize));
    }
    i = 0usize;
    while i < len {
      {
        let mut histo: usize;
        if i >= window_half {
          {
            let _rhs = 1;
            let _lhs = &mut histogram[data[((pos.wrapping_add(i).wrapping_sub(window_half) &
                              mask) as (usize))] as (usize)];
            *_lhs = (*_lhs).wrapping_sub(_rhs as (usize));
          }
          in_window = in_window.wrapping_sub(1 as (usize));
        }
        if i.wrapping_add(window_half) < len {
          {
            let _rhs = 1;
            let _lhs = &mut histogram[data[((pos.wrapping_add(i).wrapping_add(window_half) &
                              mask) as (usize))] as (usize)];
            *_lhs = (*_lhs).wrapping_add(_rhs as (usize));
          }
          in_window = in_window.wrapping_add(1 as (usize));
        }
        histo = histogram[data[((pos.wrapping_add(i) & mask) as (usize))] as (usize)];
        if histo == 0usize {
          histo = 1usize;
        }
        {
          //precision is vital here: lets keep double precision
          let mut lit_cost: f64 = FastLog2f64(in_window as u64) as f64 - FastLog2f64(histo as u64) as f64;
          lit_cost = lit_cost + 0.029;
          if lit_cost < 1.0 {
            lit_cost = lit_cost * 0.5;
            lit_cost = lit_cost + 0.5;
          }
          cost[(i as (usize))] = lit_cost as (super::util::floatX);
        }
      }
      i = i.wrapping_add(1 as (usize));
    }
  }
}