1
+ use alloc:: SliceWrapperMut ;
2
+
1
3
use core:: cmp:: { max, min} ;
2
4
3
5
use super :: super :: alloc:: SliceWrapper ;
@@ -6,53 +8,41 @@ use super::util::{FastLog2, FastLog2u16};
6
8
use super :: vectorization:: Mem256i ;
7
9
use crate :: enc:: floatX;
8
10
9
- pub fn ShannonEntropy ( mut population : & [ u32 ] , size : usize , total : & mut usize ) -> floatX {
10
- let mut sum: usize = 0usize ;
11
+
12
+ const BROTLI_REPEAT_ZERO_CODE_LENGTH : usize = 17 ;
13
+ const BROTLI_CODE_LENGTH_CODES : usize = BROTLI_REPEAT_ZERO_CODE_LENGTH + 1 ;
14
+
15
+ pub fn ShannonEntropy ( mut population : & [ u32 ] , size : usize ) -> ( floatX , usize ) {
16
+ let mut sum: usize = 0 ;
11
17
let mut retval: floatX = 0.0 ;
12
- let mut p : usize ;
13
- if size & 1 != 0 && !population. is_empty ( ) {
14
- p = population[ 0 ] as usize ;
18
+
19
+ if ( size & 1 ) != 0 && !population. is_empty ( ) {
20
+ let p = population[ 0 ] as usize ;
15
21
population = population. split_at ( 1 ) . 1 ;
16
22
sum = sum. wrapping_add ( p) ;
17
- retval -= ( p as floatX ) * FastLog2u16 ( p as u16 ) ;
23
+ retval -= p as floatX * FastLog2u16 ( p as u16 ) ;
18
24
}
19
25
for pop_iter in population. split_at ( ( size >> 1 ) << 1 ) . 0 {
20
- p = * pop_iter as usize ;
26
+ let p = * pop_iter as usize ;
21
27
sum = sum. wrapping_add ( p) ;
22
- retval -= ( p as floatX ) * FastLog2u16 ( p as u16 ) ;
28
+ retval -= p as floatX * FastLog2u16 ( p as u16 ) ;
23
29
}
24
30
if sum != 0 {
25
- retval += ( sum as floatX ) * FastLog2 ( sum as u64 ) ; // not sure it's 16 bit
31
+ retval += sum as floatX * FastLog2 ( sum as u64 ) ; // not sure it's 16 bit
26
32
}
27
- * total = sum ;
28
- retval
33
+
34
+ ( retval, sum )
29
35
}
30
36
31
37
#[ inline( always) ]
32
38
pub fn BitsEntropy ( population : & [ u32 ] , size : usize ) -> floatX {
33
- let mut sum: usize = 0 ;
34
- let retval = ShannonEntropy ( population, size, & mut sum) ;
35
- floatX:: max ( retval, sum as floatX )
39
+ let ( mut retval, sum) = ShannonEntropy ( population, size) ;
40
+ if retval < sum as floatX {
41
+ retval = sum as floatX ;
42
+ }
43
+ retval
36
44
}
37
45
38
- const BROTLI_REPEAT_ZERO_CODE_LENGTH : usize = 17 ;
39
- const BROTLI_CODE_LENGTH_CODES : usize = BROTLI_REPEAT_ZERO_CODE_LENGTH + 1 ;
40
- /*
41
- use std::io::{self, Error, ErrorKind, Read, Write};
42
-
43
- macro_rules! println_stderr(
44
- ($($val:tt)*) => { {
45
- writeln!(&mut ::std::io::stderr(), $($val)*).unwrap();
46
- } }
47
- );
48
- */
49
-
50
- #[ cfg( feature = "vector_scratch_space" ) ]
51
- const vectorize_population_cost: bool = true ;
52
-
53
- #[ cfg( not( feature = "vector_scratch_space" ) ) ]
54
- const vectorize_population_cost: bool = false ;
55
-
56
46
#[ allow( clippy:: excessive_precision) ]
57
47
fn CostComputation < T : SliceWrapper < Mem256i > > (
58
48
depth_histo : & mut [ u32 ; BROTLI_CODE_LENGTH_CODES ] ,
@@ -85,8 +75,6 @@ fn CostComputation<T: SliceWrapper<Mem256i>>(
85
75
bits
86
76
}
87
77
88
- use alloc:: SliceWrapperMut ;
89
-
90
78
pub fn BrotliPopulationCost < HistogramType : SliceWrapper < u32 > + CostAccessors > (
91
79
histogram : & HistogramType ,
92
80
nnz_data : & mut HistogramType :: i32vec ,
@@ -95,72 +83,68 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
95
83
static kTwoSymbolHistogramCost: floatX = 20.0 ;
96
84
static kThreeSymbolHistogramCost: floatX = 28.0 ;
97
85
static kFourSymbolHistogramCost: floatX = 37.0 ;
86
+
98
87
let data_size: usize = histogram. slice ( ) . len ( ) ;
99
- let mut count: i32 = 0i32 ;
88
+ let mut count = 0 ;
100
89
let mut s: [ usize ; 5 ] = [ 0 ; 5 ] ;
101
-
102
90
let mut bits: floatX = 0.0 ;
103
- let mut i : usize ;
104
- if histogram. total_count ( ) == 0usize {
91
+
92
+ if histogram. total_count ( ) == 0 {
105
93
return kOneSymbolHistogramCost;
106
94
}
107
- i = 0usize ;
108
- ' break1: while i < data_size {
109
- {
110
- if histogram. slice ( ) [ i] > 0u32 {
111
- s[ count as usize ] = i;
112
- count += 1 ;
113
- if count > 4i32 {
114
- break ' break1;
115
- }
95
+ for i in 0 ..data_size {
96
+ if histogram. slice ( ) [ i] > 0 {
97
+ s[ count] = i;
98
+ count += 1 ;
99
+ if count > 4 {
100
+ break ;
116
101
}
117
102
}
118
- i = i. wrapping_add ( 1 ) ;
119
- }
120
- if count == 1i32 {
121
- return kOneSymbolHistogramCost;
122
103
}
123
- if count == 2i32 {
124
- return kTwoSymbolHistogramCost + ( histogram. total_count ( ) as floatX ) ;
125
- }
126
- if count == 3i32 {
127
- let histo0: u32 = histogram. slice ( ) [ s[ 0 ] ] ;
128
- let histo1: u32 = histogram. slice ( ) [ s[ 1 ] ] ;
129
- let histo2: u32 = histogram. slice ( ) [ s[ 2 ] ] ;
130
- let histomax: u32 = max ( histo0, max ( histo1, histo2) ) ;
131
- return kThreeSymbolHistogramCost
132
- + ( ( 2u32 ) . wrapping_mul ( histo0. wrapping_add ( histo1) . wrapping_add ( histo2) ) as floatX )
133
- - ( histomax as floatX ) ;
134
- }
135
- if count == 4i32 {
136
- let mut histo: [ u32 ; 4 ] = [ 0 ; 4 ] ;
137
-
138
- for i in 0usize ..4usize {
139
- histo[ i] = histogram. slice ( ) [ s[ i] ] ;
104
+ match count {
105
+ 1 => return kOneSymbolHistogramCost,
106
+ 2 => return kTwoSymbolHistogramCost + histogram. total_count ( ) as floatX ,
107
+ 3 => {
108
+ let histo0: u32 = histogram. slice ( ) [ s[ 0 ] ] ;
109
+ let histo1: u32 = histogram. slice ( ) [ s[ 1 ] ] ;
110
+ let histo2: u32 = histogram. slice ( ) [ s[ 2 ] ] ;
111
+ let histomax: u32 = max ( histo0, max ( histo1, histo2) ) ;
112
+ return kThreeSymbolHistogramCost
113
+ + ( 2u32 ) . wrapping_mul ( histo0. wrapping_add ( histo1) . wrapping_add ( histo2) ) as floatX
114
+ - histomax as floatX ;
140
115
}
141
- for i in 0 ..4 {
142
- for j in i + 1 ..4 {
143
- if histo[ j] > histo[ i] {
144
- histo. swap ( j, i) ;
116
+ 4 => {
117
+ let mut histo: [ u32 ; 4 ] = [ 0 ; 4 ] ;
118
+
119
+ for i in 0 ..4 {
120
+ histo[ i] = histogram. slice ( ) [ s[ i] ] ;
121
+ }
122
+ for i in 0 ..4 {
123
+ for j in i + 1 ..4 {
124
+ if histo[ j] > histo[ i] {
125
+ histo. swap ( j, i) ;
126
+ }
145
127
}
146
128
}
129
+ let h23: u32 = histo[ 2 ] . wrapping_add ( histo[ 3 ] ) ;
130
+ let histomax: u32 = max ( h23, histo[ 0 ] ) ;
131
+ return kFourSymbolHistogramCost
132
+ + ( 3u32 ) . wrapping_mul ( h23) as floatX
133
+ + ( 2u32 ) . wrapping_mul ( histo[ 0 ] . wrapping_add ( histo[ 1 ] ) ) as floatX
134
+ - histomax as floatX ;
147
135
}
148
- let h23: u32 = histo[ 2 ] . wrapping_add ( histo[ 3 ] ) ;
149
- let histomax: u32 = max ( h23, histo[ 0 ] ) ;
150
- return kFourSymbolHistogramCost
151
- + ( ( 3u32 ) . wrapping_mul ( h23) as floatX )
152
- + ( ( 2u32 ) . wrapping_mul ( histo[ 0 ] . wrapping_add ( histo[ 1 ] ) ) as floatX )
153
- - ( histomax as floatX ) ;
136
+ _ => { }
154
137
}
155
- if vectorize_population_cost {
138
+
139
+ if cfg ! ( feature = "vector_scratch_space" ) {
156
140
// vectorization failed: it's faster to do things inline than split into two loops
157
141
let mut nnz: usize = 0 ;
158
142
let mut depth_histo = [ 0u32 ; 18 ] ;
159
143
let total_count = histogram. total_count ( ) as floatX ;
160
144
let log2total = FastLog2 ( histogram. total_count ( ) as u64 ) ;
161
- i = 0usize ;
145
+ let mut i : usize = 0 ;
162
146
while i < data_size {
163
- if histogram. slice ( ) [ i] > 0u32 {
147
+ if histogram. slice ( ) [ i] > 0 {
164
148
let nnz_val = & mut nnz_data. slice_mut ( ) [ nnz >> 3 ] ;
165
149
nnz_val[ nnz & 7 ] = histogram. slice ( ) [ i] as i32 ;
166
150
i += 1 ;
@@ -178,14 +162,14 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
178
162
break ;
179
163
}
180
164
if reps < 3 {
181
- depth_histo[ 0 ] += reps
165
+ depth_histo[ 0 ] += reps;
182
166
} else {
183
167
reps -= 2 ;
184
168
let mut depth_histo_adds: u32 = 0 ;
185
- while reps > 0u32 {
169
+ while reps > 0 {
186
170
depth_histo_adds += 1 ;
187
171
bits += 3.0 ;
188
- reps >>= 3i32 ;
172
+ reps >>= 3 ;
189
173
}
190
174
depth_histo[ BROTLI_REPEAT_ZERO_CODE_LENGTH ] += depth_histo_adds;
191
175
}
@@ -195,7 +179,7 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
195
179
} else {
196
180
let mut max_depth: usize = 1 ;
197
181
let mut depth_histo = [ 0u32 ; 18 ] ;
198
- let log2total = FastLog2 ( histogram. total_count ( ) as u64 ) ; // 64 bit here
182
+ let log2total: floatX = FastLog2 ( histogram. total_count ( ) as u64 ) ; // 64 bit here
199
183
let mut reps: u32 = 0 ;
200
184
for histo in histogram. slice ( ) [ ..data_size] . iter ( ) {
201
185
if * histo != 0 {
@@ -204,7 +188,7 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
204
188
depth_histo[ 0 ] += reps;
205
189
} else {
206
190
reps -= 2 ;
207
- while reps > 0u32 {
191
+ while reps > 0 {
208
192
depth_histo[ 17 ] += 1 ;
209
193
bits += 3.0 ;
210
194
reps >>= 3 ;
@@ -213,8 +197,8 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
213
197
reps = 0 ;
214
198
}
215
199
let log2p = log2total - FastLog2u16 ( * histo as u16 ) ;
216
- let mut depth: usize = ( log2p + 0.5 ) as usize ;
217
- bits += ( * histo as floatX ) * log2p;
200
+ let mut depth = ( log2p + 0.5 ) as usize ;
201
+ bits += * histo as floatX * log2p;
218
202
depth = min ( depth, 15 ) ;
219
203
max_depth = max ( depth, max_depth) ;
220
204
depth_histo[ depth] += 1 ;
@@ -223,17 +207,7 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
223
207
}
224
208
}
225
209
bits += ( 18usize ) . wrapping_add ( ( 2usize ) . wrapping_mul ( max_depth) ) as floatX ;
226
- bits += BitsEntropy ( & depth_histo[ ..] , 18usize ) ;
210
+ bits += BitsEntropy ( & depth_histo[ ..] , 18 ) ;
227
211
}
228
212
bits
229
213
}
230
- /*
231
- fn HistogramDataSizeCommand() -> usize {
232
- 704usize
233
- }*/
234
-
235
- /*
236
- fn HistogramDataSizeDistance() -> usize {
237
- 520usize
238
- }
239
- */
0 commit comments