Skip to content

Commit a9482b5

Browse files
committed
Use macro for wrapping += and -=
The current auto-generated += and -= implementation is hard to read, and should be replaced with += where possible. That said, we cannot auto-replace it just yet because Rust behaves differently in debug mode, therefore we should use second best approach - a macro that clearly shows intention without all the boilerplate code. The only manual code are two macros in the src/enc/mod.rs Use this replacement file as described in other recent PRs to replace boilerplate code. <details> <summary>replacement file content</summary> ```diff @@ expression cond, expr; @@ if cond { - let _rhs = 1; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, 1); } @@ expression expr; @@ -{ - let _rhs = 1; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, 1); -} @@ expression expr; @@ -{ - let _rhs = 1u32; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs); +::wrapping_add!(expr, 1); -} @@ expression expr; @@ -{ - let _rhs = 1i32; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, 1); -} @@ expression expr; @@ -{ - let _rhs = 1; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as usize); +::wrapping_add!(expr, 1); -} @@ expression inc, expr; @@ -{ - let _rhs = inc; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, inc as u32); -} @@ expression inc, expr; @@ -{ - let _rhs = inc; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs); +::wrapping_add!(expr, inc); -} @@ expression inc, expr; @@ -{ - let _rhs = inc; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, inc as u32); -} @@ expression inc, expr; @@ -{ - let _rhs = inc; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as usize); +::wrapping_add!(expr, inc as usize); -} @@ expression expr; @@ -{ - let _rhs = 1; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_sub(_rhs as usize); +::wrapping_sub!(expr, 1); -} @@ expression expr_r, expr_l; @@ -{ - let _rhs = expr_r; - let _lhs = &mut expr_l; - *_lhs *= _rhs; +expr_l *= expr_r; -} @@ expression expr_r, expr_l; @@ -{ - let _rhs = expr_r; - let _lhs = &mut expr_l; - *_lhs += _rhs; +expr_l += expr_r; -} ``` </details>
1 parent b9a4857 commit a9482b5

12 files changed

+118
-314
lines changed

src/enc/backward_references/hq.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -1244,24 +1244,17 @@ fn ZopfliCostModelSetFromCommands<AllocF: Allocator<floatX>>(
12441244
let distcode: usize = ((commands[i]).dist_prefix_ as i32 & 0x3ff) as usize;
12451245
let cmdcode: usize = (commands[i]).cmd_prefix_ as usize;
12461246
let mut j: usize;
1247-
{
1248-
let _rhs = 1;
1249-
let _lhs = &mut histogram_cmd[cmdcode];
1250-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
1251-
}
1247+
::wrapping_add!(histogram_cmd[cmdcode], 1);
12521248
if cmdcode >= 128usize {
1253-
let _rhs = 1;
1254-
let _lhs = &mut histogram_dist[distcode];
1255-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
1249+
::wrapping_add!(histogram_dist[distcode], 1);
12561250
}
12571251
j = 0usize;
12581252
while j < inslength {
1259-
{
1260-
let _rhs = 1;
1261-
let _lhs = &mut histogram_literal
1262-
[(ringbuffer[(pos.wrapping_add(j) & ringbuffer_mask)] as usize)];
1263-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
1264-
}
1253+
::wrapping_add!(
1254+
histogram_literal
1255+
[(ringbuffer[(pos.wrapping_add(j) & ringbuffer_mask)] as usize)],
1256+
1
1257+
);
12651258
j = j.wrapping_add(1);
12661259
}
12671260
pos = pos.wrapping_add(inslength.wrapping_add(copylength));

src/enc/block_splitter.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,7 @@ fn ClusterBlocks<
562562
while i < length {
563563
{
564564
0i32;
565-
{
566-
let _rhs = 1;
567-
let _lhs = &mut block_lengths.slice_mut()[block_idx];
568-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
569-
}
565+
::wrapping_add!(block_lengths.slice_mut()[block_idx], 1);
570566
if i.wrapping_add(1) == length
571567
|| block_ids[i] as i32 != block_ids[i.wrapping_add(1)] as i32
572568
{

src/enc/brotli_bit_stream.rs

+8-23
Original file line numberDiff line numberDiff line change
@@ -923,11 +923,7 @@ pub fn BrotliStoreHuffmanTree(
923923
);
924924
i = 0usize;
925925
while i < huffman_tree_size {
926-
{
927-
let _rhs = 1;
928-
let _lhs = &mut huffman_tree_histogram[huffman_tree[i] as usize];
929-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
930-
}
926+
::wrapping_add!(huffman_tree_histogram[huffman_tree[i] as usize], 1);
931927
i = i.wrapping_add(1);
932928
}
933929
i = 0usize;
@@ -1665,15 +1661,9 @@ fn BuildAndStoreBlockSplitCode(
16651661
{
16661662
let type_code: usize = NextBlockTypeCode(&mut type_code_calculator, types[i]);
16671663
if i != 0usize {
1668-
let _rhs = 1;
1669-
let _lhs = &mut type_histo[type_code];
1670-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
1671-
}
1672-
{
1673-
let _rhs = 1;
1674-
let _lhs = &mut length_histo[BlockLengthPrefixCode(lengths[i]) as usize];
1675-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
1664+
::wrapping_add!(type_histo[type_code], 1);
16761665
}
1666+
::wrapping_add!(length_histo[BlockLengthPrefixCode(lengths[i]) as usize], 1);
16771667
}
16781668
i = i.wrapping_add(1);
16791669
}
@@ -1969,11 +1959,10 @@ fn EncodeContextMap<AllocU32: alloc::Allocator<u32>>(
19691959
let mut histogram: [u32; 272] = [0; 272];
19701960
i = 0usize;
19711961
while i < num_rle_symbols {
1972-
{
1973-
let _rhs = 1;
1974-
let _lhs = &mut histogram[(rle_symbols.slice()[i] & kSymbolMask) as usize];
1975-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
1976-
}
1962+
::wrapping_add!(
1963+
histogram[(rle_symbols.slice()[i] & kSymbolMask) as usize],
1964+
1
1965+
);
19771966
i = i.wrapping_add(1);
19781967
}
19791968
{
@@ -2868,11 +2857,7 @@ pub fn BrotliStoreMetaBlockFast<Cb, Alloc: BrotliAlloc>(
28682857
j = cmd.insert_len_ as usize;
28692858
while j != 0usize {
28702859
{
2871-
{
2872-
let _rhs = 1;
2873-
let _lhs = &mut histogram[input[(pos & mask)] as usize];
2874-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
2875-
}
2860+
::wrapping_add!(histogram[input[(pos & mask)] as usize], 1);
28762861
pos = pos.wrapping_add(1);
28772862
}
28782863
j = j.wrapping_sub(1);

src/enc/cluster.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,10 @@ pub fn BrotliHistogramCombine<
188188
let best_idx2: u32 = (pairs[0]).idx2;
189189
HistogramSelfAddHistogram(out, (best_idx1 as usize), (best_idx2 as usize));
190190
(out[(best_idx1 as usize)]).set_bit_cost((pairs[0]).cost_combo);
191-
{
192-
let _rhs = cluster_size[(best_idx2 as usize)];
193-
let _lhs = &mut cluster_size[(best_idx1 as usize)];
194-
*_lhs = (*_lhs).wrapping_add(_rhs);
195-
}
191+
::wrapping_add!(
192+
cluster_size[(best_idx1 as usize)],
193+
cluster_size[(best_idx2 as usize)]
194+
);
196195
i = 0usize;
197196
while i < symbols_size {
198197
{

0 commit comments

Comments
 (0)