diff --git a/src/enc/block_splitter.rs b/src/enc/block_splitter.rs index c890de1b..68a80639 100755 --- a/src/enc/block_splitter.rs +++ b/src/enc/block_splitter.rs @@ -498,11 +498,7 @@ fn ClusterBlocks< i = 0usize; while i < length { { - { - 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 { diff --git a/src/enc/cluster.rs b/src/enc/cluster.rs index b0e80658..ac3f733f 100755 --- a/src/enc/cluster.rs +++ b/src/enc/cluster.rs @@ -167,11 +167,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)] + ); for i in 0usize..symbols_size { if symbols[i] == best_idx2 { symbols[i] = best_idx1; diff --git a/src/enc/encode.rs b/src/enc/encode.rs index 68b81805..5bf076f2 100755 --- a/src/enc/encode.rs +++ b/src/enc/encode.rs @@ -1315,11 +1315,7 @@ fn ShouldCompress( i = 0usize; while i < t { { - { - let _rhs = 1; - let _lhs = &mut literal_histo[data[(pos as usize & mask)] as usize]; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); - } + ::wrapping_add!(literal_histo[data[(pos as usize & mask)] as usize], 1); pos = pos.wrapping_add(kSampleRate); } i = i.wrapping_add(1); @@ -1705,16 +1701,8 @@ fn ChooseContextMap( i = 0usize; while i < 9usize { { - { - let _rhs = bigram_histo[i]; - let _lhs = &mut monogram_histo[i.wrapping_rem(3)]; - *_lhs = (*_lhs).wrapping_add(_rhs); - } - { - let _rhs = bigram_histo[i]; - let _lhs = &mut two_prefix_histo[i.wrapping_rem(6)]; - *_lhs = (*_lhs).wrapping_add(_rhs); - } + ::wrapping_add!(monogram_histo[i.wrapping_rem(3)], bigram_histo[i]); + ::wrapping_add!(two_prefix_histo[i.wrapping_rem(6)], bigram_histo[i]); } i = i.wrapping_add(1); } diff --git a/src/enc/metablock.rs b/src/enc/metablock.rs index 6bba2573..3dd6b9c9 100755 --- a/src/enc/metablock.rs +++ b/src/enc/metablock.rs @@ -636,11 +636,10 @@ fn BlockSplitterFinishBlock< xself.merge_last_count_ = 0usize; xself.target_block_size_ = xself.min_block_size_; } else { - { - let _rhs = xself.block_size_ as u32; - let _lhs = &mut split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)]; - *_lhs = (*_lhs).wrapping_add(_rhs); - } + ::wrapping_add!( + split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)], + xself.block_size_ as u32 + ); histograms[xself.last_histogram_ix_[0]] = combined_histo[0].clone(); xself.last_entropy_[0] = combined_entropy[0]; if split.num_types == 1 { diff --git a/src/enc/mod.rs b/src/enc/mod.rs index f6b615c3..64b02f8d 100755 --- a/src/enc/mod.rs +++ b/src/enc/mod.rs @@ -343,3 +343,21 @@ where read_err?; Ok(total_out.unwrap()) } + +#[macro_export] +macro_rules! wrapping_add { + ($expr:expr, $increment:expr) => {{ + let _rhs = $increment; + let _lhs = &mut $expr; + *_lhs = (*_lhs).wrapping_add(_rhs); + }}; +} + +#[macro_export] +macro_rules! wrapping_sub { + ($expr:expr, $increment:expr) => {{ + let _rhs = $increment; + let _lhs = &mut $expr; + *_lhs = (*_lhs).wrapping_sub(_rhs); + }}; +}