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
use core::cmp::min;

use super::util::{floatX, FastLog2f64};
use crate::enc::utf8_util::is_mostly_utf8;

static kMinUTF8Ratio: floatX = 0.75;

fn UTF8Position(last: usize, c: usize, clamp: usize) -> usize {
    if c < 128usize {
        0usize
    } else if c >= 192usize {
        min(1usize, clamp)
    } else if last < 0xe0usize {
        0usize
    } else {
        min(2usize, clamp)
    }
}

fn DecideMultiByteStatsLevel(pos: usize, len: usize, mask: usize, data: &[u8]) -> usize {
    let mut counts = [0usize; 3];
    let mut max_utf8: usize = 1;
    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;
            {
                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);
    }
    if counts[2] < 500usize {
        max_utf8 = 1;
    }
    if counts[1].wrapping_add(counts[2]) < 25usize {
        max_utf8 = 0usize;
    }
    max_utf8
}

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

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

        let window_half: usize = 2000usize;
        let mut in_window: usize = min(window_half, len);
        let mut i: usize;
        for i in 0usize..in_window {
            let _rhs = 1;
            let _lhs = &mut histogram[data[(pos.wrapping_add(i) & mask)] as usize];
            *_lhs = (*_lhs).wrapping_add(_rhs 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];
                        *_lhs = (*_lhs).wrapping_sub(_rhs as usize);
                    }
                    in_window = in_window.wrapping_sub(1);
                }
                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];
                        *_lhs = (*_lhs).wrapping_add(_rhs as usize);
                    }
                    in_window = in_window.wrapping_add(1);
                }
                histo = histogram[data[(pos.wrapping_add(i) & mask)] as usize];
                if histo == 0usize {
                    histo = 1;
                }
                {
                    //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 += 0.029;
                    if lit_cost < 1.0 {
                        lit_cost *= 0.5;
                        lit_cost += 0.5;
                    }
                    cost[i] = lit_cost as floatX;
                }
            }
            i = i.wrapping_add(1);
        }
    }
}