Skip to content

Commit a473a18

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 462f73c commit a473a18

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
@@ -502,11 +502,7 @@ fn ClusterBlocks<
502502
i = 0usize;
503503
while i < length {
504504
{
505-
{
506-
let _rhs = 1;
507-
let _lhs = &mut block_lengths.slice_mut()[block_idx];
508-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
509-
}
505+
::wrapping_add!(block_lengths.slice_mut()[block_idx], 1);
510506
if i.wrapping_add(1) == length
511507
|| block_ids[i] as i32 != block_ids[i.wrapping_add(1)] as i32
512508
{

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
@@ -1382,11 +1382,7 @@ fn ShouldCompress(
13821382
i = 0usize;
13831383
while i < t {
13841384
{
1385-
{
1386-
let _rhs = 1;
1387-
let _lhs = &mut literal_histo[data[(pos as usize & mask)] as usize];
1388-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
1389-
}
1385+
::wrapping_add!(literal_histo[data[(pos as usize & mask)] as usize], 1);
13901386
pos = pos.wrapping_add(kSampleRate);
13911387
}
13921388
i = i.wrapping_add(1);
@@ -1779,16 +1775,8 @@ fn ChooseContextMap(
17791775
i = 0usize;
17801776
while i < 9usize {
17811777
{
1782-
{
1783-
let _rhs = bigram_histo[i];
1784-
let _lhs = &mut monogram_histo[i.wrapping_rem(3)];
1785-
*_lhs = (*_lhs).wrapping_add(_rhs);
1786-
}
1787-
{
1788-
let _rhs = bigram_histo[i];
1789-
let _lhs = &mut two_prefix_histo[i.wrapping_rem(6)];
1790-
*_lhs = (*_lhs).wrapping_add(_rhs);
1791-
}
1778+
::wrapping_add!(monogram_histo[i.wrapping_rem(3)], bigram_histo[i]);
1779+
::wrapping_add!(two_prefix_histo[i.wrapping_rem(6)], bigram_histo[i]);
17921780
}
17931781
i = i.wrapping_add(1);
17941782
}

src/enc/metablock.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,10 @@ fn BlockSplitterFinishBlock<
639639
xself.merge_last_count_ = 0usize;
640640
xself.target_block_size_ = xself.min_block_size_;
641641
} else {
642-
{
643-
let _rhs = xself.block_size_ as u32;
644-
let _lhs = &mut split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)];
645-
*_lhs = (*_lhs).wrapping_add(_rhs);
646-
}
642+
::wrapping_add!(
643+
split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)],
644+
xself.block_size_ as u32
645+
);
647646
histograms[xself.last_histogram_ix_[0]] = combined_histo[0].clone();
648647
xself.last_entropy_[0] = combined_entropy[0];
649648
if split.num_types == 1 {

src/enc/mod.rs

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

0 commit comments

Comments
 (0)