Skip to content

Commit a45907a

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 5ad7153 commit a45907a

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
@@ -170,11 +170,10 @@ pub fn BrotliHistogramCombine<
170170
let best_idx2: u32 = (pairs[0]).idx2;
171171
HistogramSelfAddHistogram(out, (best_idx1 as usize), (best_idx2 as usize));
172172
(out[(best_idx1 as usize)]).set_bit_cost((pairs[0]).cost_combo);
173-
{
174-
let _rhs = cluster_size[(best_idx2 as usize)];
175-
let _lhs = &mut cluster_size[(best_idx1 as usize)];
176-
*_lhs = (*_lhs).wrapping_add(_rhs);
177-
}
173+
::wrapping_add!(
174+
cluster_size[(best_idx1 as usize)],
175+
cluster_size[(best_idx2 as usize)]
176+
);
178177
for i in 0usize..symbols_size {
179178
if symbols[i] == best_idx2 {
180179
symbols[i] = best_idx1;

src/enc/encode.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,11 +1309,7 @@ fn ShouldCompress(
13091309
i = 0usize;
13101310
while i < t {
13111311
{
1312-
{
1313-
let _rhs = 1;
1314-
let _lhs = &mut literal_histo[data[(pos as usize & mask)] as usize];
1315-
*_lhs = (*_lhs).wrapping_add(_rhs as u32);
1316-
}
1312+
::wrapping_add!(literal_histo[data[(pos as usize & mask)] as usize], 1);
13171313
pos = pos.wrapping_add(kSampleRate);
13181314
}
13191315
i = i.wrapping_add(1);
@@ -1699,16 +1695,8 @@ fn ChooseContextMap(
16991695
i = 0usize;
17001696
while i < 9usize {
17011697
{
1702-
{
1703-
let _rhs = bigram_histo[i];
1704-
let _lhs = &mut monogram_histo[i.wrapping_rem(3)];
1705-
*_lhs = (*_lhs).wrapping_add(_rhs);
1706-
}
1707-
{
1708-
let _rhs = bigram_histo[i];
1709-
let _lhs = &mut two_prefix_histo[i.wrapping_rem(6)];
1710-
*_lhs = (*_lhs).wrapping_add(_rhs);
1711-
}
1698+
::wrapping_add!(monogram_histo[i.wrapping_rem(3)], bigram_histo[i]);
1699+
::wrapping_add!(two_prefix_histo[i.wrapping_rem(6)], bigram_histo[i]);
17121700
}
17131701
i = i.wrapping_add(1);
17141702
}

src/enc/metablock.rs

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

src/enc/mod.rs

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

0 commit comments

Comments
 (0)