Skip to content

Commit f9662e8

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); -} ``` </details>
1 parent 85196be commit f9662e8

File tree

6 files changed

+29
-27
lines changed

6 files changed

+29
-27
lines changed

src/enc/bit_cost.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use alloc::SliceWrapperMut;
2-
32
use core::cmp::{max, min};
43

54
use super::super::alloc::SliceWrapper;
@@ -8,7 +7,6 @@ use super::util::{FastLog2, FastLog2u16};
87
use super::vectorization::Mem256i;
98
use crate::enc::floatX;
109

11-
1210
const BROTLI_REPEAT_ZERO_CODE_LENGTH: usize = 17;
1311
const BROTLI_CODE_LENGTH_CODES: usize = BROTLI_REPEAT_ZERO_CODE_LENGTH + 1;
1412

src/enc/block_splitter.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,7 @@ fn ClusterBlocks<
444444
i = 0usize;
445445
while i < length {
446446
{
447-
{
448-
let _rhs = 1;
449-
let _lhs = &mut block_lengths.slice_mut()[block_idx];
450-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
451-
}
447+
::wrapping_add!(block_lengths.slice_mut()[block_idx], 1);
452448
if i.wrapping_add(1) == length
453449
|| block_ids[i] as i32 != block_ids[i.wrapping_add(1)] as i32
454450
{

src/enc/cluster.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,10 @@ pub fn BrotliHistogramCombine<
169169
let best_idx2: u32 = (pairs[0]).idx2;
170170
HistogramSelfAddHistogram(out, (best_idx1 as usize), (best_idx2 as usize));
171171
(out[(best_idx1 as usize)]).set_bit_cost((pairs[0]).cost_combo);
172-
{
173-
let _rhs = cluster_size[(best_idx2 as usize)];
174-
let _lhs = &mut cluster_size[(best_idx1 as usize)];
175-
*_lhs = (*_lhs).wrapping_add(_rhs);
176-
}
172+
::wrapping_add!(
173+
cluster_size[(best_idx1 as usize)],
174+
cluster_size[(best_idx2 as usize)]
175+
);
177176
for i in 0usize..symbols_size {
178177
if symbols[i] == best_idx2 {
179178
symbols[i] = best_idx1;

src/enc/encode.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,16 +1728,8 @@ fn ChooseContextMap(
17281728
i = 0usize;
17291729
while i < 9usize {
17301730
{
1731-
{
1732-
let _rhs = bigram_histo[i];
1733-
let _lhs = &mut monogram_histo[i.wrapping_rem(3)];
1734-
*_lhs = (*_lhs).wrapping_add(_rhs);
1735-
}
1736-
{
1737-
let _rhs = bigram_histo[i];
1738-
let _lhs = &mut two_prefix_histo[i.wrapping_rem(6)];
1739-
*_lhs = (*_lhs).wrapping_add(_rhs);
1740-
}
1731+
::wrapping_add!(monogram_histo[i.wrapping_rem(3)], bigram_histo[i]);
1732+
::wrapping_add!(two_prefix_histo[i.wrapping_rem(6)], bigram_histo[i]);
17411733
}
17421734
i = i.wrapping_add(1);
17431735
}

src/enc/metablock.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,10 @@ fn BlockSplitterFinishBlock<
627627
xself.merge_last_count_ = 0usize;
628628
xself.target_block_size_ = xself.min_block_size_;
629629
} else {
630-
{
631-
let _rhs = xself.block_size_ as u32;
632-
let _lhs = &mut split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)];
633-
*_lhs = (*_lhs).wrapping_add(_rhs);
634-
}
630+
::wrapping_add!(
631+
split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)],
632+
xself.block_size_ as u32
633+
);
635634
histograms[xself.last_histogram_ix_[0]] = combined_histo[0].clone();
636635
xself.last_entropy_[0] = combined_entropy[0];
637636
if split.num_types == 1 {

src/enc/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,21 @@ where
346346
read_err?;
347347
Ok(total_out.unwrap())
348348
}
349+
350+
#[macro_export]
351+
macro_rules! wrapping_add {
352+
($expr:expr, $increment:expr) => {{
353+
let _rhs = $increment;
354+
let _lhs = &mut $expr;
355+
*_lhs = (*_lhs).wrapping_add(_rhs);
356+
}};
357+
}
358+
359+
#[macro_export]
360+
macro_rules! wrapping_sub {
361+
($expr:expr, $increment:expr) => {{
362+
let _rhs = $increment;
363+
let _lhs = &mut $expr;
364+
*_lhs = (*_lhs).wrapping_sub(_rhs);
365+
}};
366+
}

0 commit comments

Comments
 (0)