Skip to content

Commit

Permalink
Use macro for wrapping += and -=
Browse files Browse the repository at this point in the history
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);
-}
```
</details>
  • Loading branch information
nyurik committed Mar 15, 2024
1 parent d7d8b2d commit e594745
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 285 deletions.
21 changes: 7 additions & 14 deletions src/enc/backward_references/hq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,24 +1247,17 @@ fn ZopfliCostModelSetFromCommands<AllocF: Allocator<floatX>>(
let distcode: usize = ((commands[i]).dist_prefix_ as i32 & 0x3ff) as usize;
let cmdcode: usize = (commands[i]).cmd_prefix_ as usize;
let mut j: usize;
{
let _rhs = 1;
let _lhs = &mut histogram_cmd[cmdcode];
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
}
::wrapping_add!(histogram_cmd[cmdcode], 1);
if cmdcode >= 128usize {
let _rhs = 1;
let _lhs = &mut histogram_dist[distcode];
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
::wrapping_add!(histogram_dist[distcode], 1);
}
j = 0usize;
while j < inslength {
{
let _rhs = 1;
let _lhs = &mut histogram_literal
[(ringbuffer[(pos.wrapping_add(j) & ringbuffer_mask)] as usize)];
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
}
::wrapping_add!(
histogram_literal
[(ringbuffer[(pos.wrapping_add(j) & ringbuffer_mask)] as usize)],
1
);
j = j.wrapping_add(1);
}
pos = pos.wrapping_add(inslength.wrapping_add(copylength));
Expand Down
6 changes: 1 addition & 5 deletions src/enc/block_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,7 @@ fn ClusterBlocks<
while i < length {
{
0i32;
{
let _rhs = 1;
let _lhs = &mut block_lengths.slice_mut()[block_idx];
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
}
::wrapping_add!(block_lengths.slice_mut()[block_idx], 1);
if i.wrapping_add(1) == length
|| block_ids[i] as i32 != block_ids[i.wrapping_add(1)] as i32
{
Expand Down
31 changes: 8 additions & 23 deletions src/enc/brotli_bit_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,11 +923,7 @@ pub fn BrotliStoreHuffmanTree(
);
i = 0usize;
while i < huffman_tree_size {
{
let _rhs = 1;
let _lhs = &mut huffman_tree_histogram[huffman_tree[i] as usize];
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
}
::wrapping_add!(huffman_tree_histogram[huffman_tree[i] as usize], 1);
i = i.wrapping_add(1);
}
i = 0usize;
Expand Down Expand Up @@ -1665,15 +1661,9 @@ fn BuildAndStoreBlockSplitCode(
{
let type_code: usize = NextBlockTypeCode(&mut type_code_calculator, types[i]);
if i != 0usize {
let _rhs = 1;
let _lhs = &mut type_histo[type_code];
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
}
{
let _rhs = 1;
let _lhs = &mut length_histo[BlockLengthPrefixCode(lengths[i]) as usize];
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
::wrapping_add!(type_histo[type_code], 1);
}
::wrapping_add!(length_histo[BlockLengthPrefixCode(lengths[i]) as usize], 1);
}
i = i.wrapping_add(1);
}
Expand Down Expand Up @@ -1969,11 +1959,10 @@ fn EncodeContextMap<AllocU32: alloc::Allocator<u32>>(
let mut histogram: [u32; 272] = [0; 272];
i = 0usize;
while i < num_rle_symbols {
{
let _rhs = 1;
let _lhs = &mut histogram[(rle_symbols.slice()[i] & kSymbolMask) as usize];
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
}
::wrapping_add!(
histogram[(rle_symbols.slice()[i] & kSymbolMask) as usize],
1
);
i = i.wrapping_add(1);
}
{
Expand Down Expand Up @@ -2868,11 +2857,7 @@ pub fn BrotliStoreMetaBlockFast<Cb, Alloc: BrotliAlloc>(
j = cmd.insert_len_ as usize;
while j != 0usize {
{
{
let _rhs = 1;
let _lhs = &mut histogram[input[(pos & mask)] as usize];
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
}
::wrapping_add!(histogram[input[(pos & mask)] as usize], 1);
pos = pos.wrapping_add(1);
}
j = j.wrapping_sub(1);
Expand Down
9 changes: 4 additions & 5 deletions src/enc/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,10 @@ pub fn BrotliHistogramCombine<
let best_idx2: u32 = (pairs[0]).idx2;
HistogramSelfAddHistogram(out, (best_idx1 as usize), (best_idx2 as usize));
(out[(best_idx1 as usize)]).set_bit_cost((pairs[0]).cost_combo);
{
let _rhs = cluster_size[(best_idx2 as usize)];
let _lhs = &mut cluster_size[(best_idx1 as usize)];
*_lhs = (*_lhs).wrapping_add(_rhs);
}
::wrapping_add!(
cluster_size[(best_idx1 as usize)],
cluster_size[(best_idx2 as usize)]
);
i = 0usize;
while i < symbols_size {
{
Expand Down
Loading

0 comments on commit e594745

Please sign in to comment.