Skip to content

Commit 769b39e

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 bf3db20 commit 769b39e

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

src/enc/block_splitter.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,7 @@ fn ClusterBlocks<
448448
i = 0usize;
449449
while i < length {
450450
{
451-
{
452-
let _rhs = 1;
453-
let _lhs = &mut block_lengths.slice_mut()[block_idx];
454-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
455-
}
451+
::wrapping_add!(block_lengths.slice_mut()[block_idx], 1);
456452
if i.wrapping_add(1) == length
457453
|| block_ids[i] as i32 != block_ids[i.wrapping_add(1)] as i32
458454
{

src/enc/cluster.rs

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

src/enc/encode.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,11 +1315,7 @@ fn ShouldCompress(
13151315
i = 0usize;
13161316
while i < t {
13171317
{
1318-
{
1319-
let _rhs = 1;
1320-
let _lhs = &mut literal_histo[data[(pos as usize & mask)] as usize];
1321-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
1322-
}
1318+
::wrapping_add!(literal_histo[data[(pos as usize & mask)] as usize], 1);
13231319
pos = pos.wrapping_add(kSampleRate);
13241320
}
13251321
i = i.wrapping_add(1);
@@ -1705,16 +1701,8 @@ fn ChooseContextMap(
17051701
i = 0usize;
17061702
while i < 9usize {
17071703
{
1708-
{
1709-
let _rhs = bigram_histo[i];
1710-
let _lhs = &mut monogram_histo[i.wrapping_rem(3)];
1711-
*_lhs = (*_lhs).wrapping_add(_rhs);
1712-
}
1713-
{
1714-
let _rhs = bigram_histo[i];
1715-
let _lhs = &mut two_prefix_histo[i.wrapping_rem(6)];
1716-
*_lhs = (*_lhs).wrapping_add(_rhs);
1717-
}
1704+
::wrapping_add!(monogram_histo[i.wrapping_rem(3)], bigram_histo[i]);
1705+
::wrapping_add!(two_prefix_histo[i.wrapping_rem(6)], bigram_histo[i]);
17181706
}
17191707
i = i.wrapping_add(1);
17201708
}

src/enc/metablock.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,10 @@ fn BlockSplitterFinishBlock<
636636
xself.merge_last_count_ = 0usize;
637637
xself.target_block_size_ = xself.min_block_size_;
638638
} else {
639-
{
640-
let _rhs = xself.block_size_ as u32;
641-
let _lhs = &mut split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)];
642-
*_lhs = (*_lhs).wrapping_add(_rhs);
643-
}
639+
::wrapping_add!(
640+
split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)],
641+
xself.block_size_ as u32
642+
);
644643
histograms[xself.last_histogram_ix_[0]] = combined_histo[0].clone();
645644
xself.last_entropy_[0] = combined_entropy[0];
646645
if split.num_types == 1 {

src/enc/mod.rs

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

0 commit comments

Comments
 (0)