Skip to content

Commit d7d8b2d

Browse files
nyurikdanielrh
authored andcommitted
Convert non-public bool-ints into bools
Make code more readable by using real bool types where it does not change the public interface. I think we should change all these in public API too, but that's a separate PR.
1 parent da54ef1 commit d7d8b2d

12 files changed

+186
-277
lines changed

src/enc/backward_references/hash_to_binary_tree.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,7 @@ where
438438
let mut matches_offset = 0usize;
439439
let cur_ix_masked: usize = cur_ix & ring_buffer_mask;
440440
let max_comp_len: usize = core::cmp::min(max_length, 128usize);
441-
let should_reroot_tree: i32 = if !!(max_length >= 128usize) {
442-
1i32
443-
} else {
444-
0i32
445-
};
441+
let should_reroot_tree = max_length >= 128;
446442
let key = xself.HashBytes(&data[cur_ix_masked..]);
447443
let forest: &mut [u32] = xself.forest.slice_mut();
448444
let mut prev_ix: usize = xself.buckets_.slice()[key] as usize;
@@ -451,7 +447,7 @@ where
451447
let mut best_len_left: usize = 0usize;
452448
let mut best_len_right: usize = 0usize;
453449
let mut depth_remaining: usize;
454-
if should_reroot_tree != 0 {
450+
if should_reroot_tree {
455451
xself.buckets_.slice_mut()[key] = cur_ix as u32;
456452
}
457453
depth_remaining = 64usize;
@@ -460,7 +456,7 @@ where
460456
let backward: usize = cur_ix.wrapping_sub(prev_ix);
461457
let prev_ix_masked: usize = prev_ix & ring_buffer_mask;
462458
if backward == 0usize || backward > max_backward || depth_remaining == 0usize {
463-
if should_reroot_tree != 0 {
459+
if should_reroot_tree {
464460
forest[node_left] = xself.invalid_pos_;
465461
forest[node_right] = xself.invalid_pos_;
466462
}
@@ -484,7 +480,7 @@ where
484480
matches_offset += 1;
485481
}
486482
if len >= max_comp_len {
487-
if should_reroot_tree != 0 {
483+
if should_reroot_tree {
488484
forest[node_left] = forest[LeftChildIndexH10!(xself, prev_ix)];
489485
forest[node_right] = forest[RightChildIndexH10!(xself, prev_ix)];
490486
}
@@ -494,14 +490,14 @@ where
494490
> data[prev_ix_masked.wrapping_add(len)] as i32
495491
{
496492
best_len_left = len;
497-
if should_reroot_tree != 0 {
493+
if should_reroot_tree {
498494
forest[node_left] = prev_ix as u32;
499495
}
500496
node_left = RightChildIndexH10!(xself, prev_ix);
501497
prev_ix = forest[node_left] as usize;
502498
} else {
503499
best_len_right = len;
504-
if should_reroot_tree != 0 {
500+
if should_reroot_tree {
505501
forest[node_right] = prev_ix as u32;
506502
}
507503
node_right = LeftChildIndexH10!(xself, prev_ix);

src/enc/backward_references/hq.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ pub fn BrotliZopfliCreateCommands(
139139
let len_code: usize = ZopfliNodeLengthCode(next) as usize;
140140
let max_distance: usize =
141141
brotli_min_size_t(block_start.wrapping_add(pos), max_backward_limit);
142-
let is_dictionary: i32 = if !!(distance > max_distance.wrapping_add(gap)) {
143-
1i32
144-
} else {
145-
0i32
146-
};
142+
let is_dictionary = distance > max_distance.wrapping_add(gap);
147143
let dist_code: usize = ZopfliNodeDistanceCode(next) as usize;
148144
InitCommand(
149145
&mut commands[i],
@@ -153,7 +149,7 @@ pub fn BrotliZopfliCreateCommands(
153149
len_code,
154150
dist_code,
155151
);
156-
if is_dictionary == 0 && (dist_code > 0usize) {
152+
if !is_dictionary && dist_code > 0 {
157153
dist_cache[3] = dist_cache[2];
158154
dist_cache[2] = dist_cache[1];
159155
dist_cache[1] = dist_cache[0];
@@ -882,12 +878,7 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
882878
{
883879
let mut match_: BackwardMatch = BackwardMatch(matches[j]);
884880
let dist: usize = match_.distance() as usize;
885-
let is_dictionary_match: i32 =
886-
if !!(dist > max_distance.wrapping_add(gap)) {
887-
1i32
888-
} else {
889-
0i32
890-
};
881+
let is_dictionary_match = dist > max_distance.wrapping_add(gap);
891882
let dist_code: usize = dist.wrapping_add(16).wrapping_sub(1);
892883
let mut dist_symbol: u16 = 0;
893884
let mut distextra: u32 = 0;
@@ -908,13 +899,13 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
908899
);
909900
let max_match_len: usize = BackwardMatchLength(&mut match_);
910901
if len < max_match_len
911-
&& (is_dictionary_match != 0 || max_match_len > max_zopfli_len)
902+
&& (is_dictionary_match || max_match_len > max_zopfli_len)
912903
{
913904
len = max_match_len;
914905
}
915906
while len <= max_match_len {
916907
{
917-
let len_code: usize = if is_dictionary_match != 0 {
908+
let len_code: usize = if is_dictionary_match {
918909
BackwardMatchLengthCode(&mut match_)
919910
} else {
920911
len
@@ -1480,7 +1471,7 @@ pub fn BrotliCreateHqZopfliBackwardReferences<
14801471
} else {
14811472
<Alloc as Allocator<u64>>::AllocatedMemory::default()
14821473
};
1483-
if !!(0i32 == 0) && (matches_size != 0usize) {
1474+
if matches_size != 0 {
14841475
for (dst, src) in new_array
14851476
.slice_mut()
14861477
.split_at_mut(matches_size)

src/enc/backward_references/mod.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
367367
let mut best_len: usize = best_len_in;
368368
let cached_backward: usize = distance_cache[0] as usize;
369369
let mut prev_ix: usize = cur_ix.wrapping_sub(cached_backward);
370-
let mut is_match_found: i32 = 0i32;
370+
let mut is_match_found = false;
371371
out.len_x_code = 0usize;
372372
if prev_ix < cur_ix {
373373
prev_ix &= ring_buffer_mask as u32 as usize;
@@ -388,7 +388,7 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
388388
self.buckets_.slice_mut()[key as usize] = cur_ix as u32;
389389
return true;
390390
} else {
391-
is_match_found = 1i32;
391+
is_match_found = true;
392392
}
393393
}
394394
}
@@ -440,12 +440,12 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
440440
out.distance = backward;
441441
out.score = score;
442442
compare_char = data[cur_ix_masked.wrapping_add(best_len)] as i32;
443-
is_match_found = 1i32;
443+
is_match_found = true;
444444
}
445445
}
446446
}
447447
}
448-
if dictionary.is_some() && self.buckets_.USE_DICTIONARY() != 0 && (is_match_found == 0) {
448+
if dictionary.is_some() && self.buckets_.USE_DICTIONARY() != 0 && !is_match_found {
449449
is_match_found = SearchInStaticDictionary(
450450
dictionary.unwrap(),
451451
dictionary_hash,
@@ -455,13 +455,13 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
455455
max_backward.wrapping_add(gap),
456456
max_distance,
457457
out,
458-
1i32,
458+
true,
459459
);
460460
}
461461
self.buckets_.slice_mut()
462462
[(key as usize).wrapping_add((cur_ix >> 3).wrapping_rem(bucket_sweep as usize))] =
463463
cur_ix as u32;
464-
is_match_found != 0
464+
is_match_found
465465
}
466466
}
467467
impl<AllocU32: alloc::Allocator<u32>> BasicHashComputer for H2Sub<AllocU32> {
@@ -735,7 +735,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
735735
let cur_ix_masked: usize = cur_ix & ring_buffer_mask;
736736
let mut best_score: u64 = out.score;
737737
let mut best_len: usize = best_len_in;
738-
let mut is_match_found: i32 = 0i32;
738+
let mut is_match_found = false;
739739
out.len_x_code = 0usize;
740740
for i in 0..H9_NUM_LAST_DISTANCES_TO_CHECK {
741741
let idx = kDistanceCacheIndex[i] as usize;
@@ -767,7 +767,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
767767
out.len = best_len;
768768
out.distance = backward;
769769
out.score = best_score;
770-
is_match_found = 1i32;
770+
is_match_found = true;
771771
}
772772
}
773773
}
@@ -821,7 +821,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
821821
out.len = best_len;
822822
out.distance = backward;
823823
out.score = best_score;
824-
is_match_found = 1;
824+
is_match_found = true;
825825
if cur_ix_masked.wrapping_add(best_len) > ring_buffer_mask {
826826
break;
827827
}
@@ -833,7 +833,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
833833
bucket[*self_num_key as usize & H9_BLOCK_MASK] = cur_ix as u32;
834834
*self_num_key = self_num_key.wrapping_add(1);
835835
}
836-
if is_match_found == 0 && dictionary.is_some() {
836+
if !is_match_found && dictionary.is_some() {
837837
let (_, cur_data) = data.split_at(cur_ix_masked);
838838
is_match_found = SearchInStaticDictionary(
839839
dictionary.unwrap(),
@@ -844,10 +844,10 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
844844
max_backward.wrapping_add(gap),
845845
max_distance,
846846
out,
847-
0i32,
847+
false,
848848
);
849849
}
850-
is_match_found != 0
850+
is_match_found
851851
}
852852

853853
fn Store(&mut self, data: &[u8], mask: usize, ix: usize) {
@@ -1665,7 +1665,7 @@ impl<
16651665
) -> bool {
16661666
let opts = self.Opts();
16671667
let cur_ix_masked: usize = cur_ix & ring_buffer_mask;
1668-
let mut is_match_found: i32 = 0i32;
1668+
let mut is_match_found = false;
16691669
let mut best_score: u64 = out.score;
16701670
let mut best_len: usize = out.len;
16711671
let mut i: usize;
@@ -1707,7 +1707,7 @@ impl<
17071707
out.len = best_len;
17081708
out.distance = backward;
17091709
out.score = best_score;
1710-
is_match_found = 1i32;
1710+
is_match_found = true;
17111711
}
17121712
}
17131713
}
@@ -1763,7 +1763,7 @@ impl<
17631763
out.len = best_len;
17641764
out.distance = backward;
17651765
out.score = best_score;
1766-
is_match_found = 1i32;
1766+
is_match_found = true;
17671767
}
17681768
}
17691769
}
@@ -1772,7 +1772,7 @@ impl<
17721772
cur_ix as u32;
17731773
*num_ref_mut = num_ref_mut.wrapping_add(1);
17741774
}
1775-
if is_match_found == 0 && dictionary.is_some() {
1775+
if !is_match_found && dictionary.is_some() {
17761776
let (_, cur_data) = data.split_at(cur_ix_masked);
17771777
is_match_found = SearchInStaticDictionary(
17781778
dictionary.unwrap(),
@@ -1783,10 +1783,10 @@ impl<
17831783
max_backward.wrapping_add(gap),
17841784
max_distance,
17851785
out,
1786-
0i32,
1786+
false,
17871787
);
17881788
}
1789-
is_match_found != 0
1789+
is_match_found
17901790
}
17911791
}
17921792

@@ -1936,19 +1936,19 @@ fn SearchInStaticDictionary<HasherType: AnyHasher>(
19361936
max_backward: usize,
19371937
max_distance: usize,
19381938
out: &mut HasherSearchResult,
1939-
shallow: i32,
1940-
) -> i32 {
1939+
shallow: bool,
1940+
) -> bool {
19411941
let mut key: usize;
19421942
let mut i: usize;
1943-
let mut is_match_found: i32 = 0i32;
1943+
let mut is_match_found = false;
19441944
let opts = handle.Opts();
19451945
let xself: &mut Struct1 = handle.GetHasherCommon();
19461946
if xself.dict_num_matches < xself.dict_num_lookups >> 7 {
1947-
return 0i32;
1947+
return false;
19481948
}
19491949
key = (Hash14(data) << 1) as usize; //FIXME: works for any kind of hasher??
19501950
i = 0usize;
1951-
while i < if shallow != 0 { 1u32 } else { 2u32 } as usize {
1951+
while i < if shallow { 1 } else { 2 } {
19521952
{
19531953
let item: usize = dictionary_hash[key] as usize;
19541954
xself.dict_num_lookups = xself.dict_num_lookups.wrapping_add(1);
@@ -1965,7 +1965,7 @@ fn SearchInStaticDictionary<HasherType: AnyHasher>(
19651965
);
19661966
if item_matches != 0 {
19671967
xself.dict_num_matches = xself.dict_num_matches.wrapping_add(1);
1968-
is_match_found = 1i32;
1968+
is_match_found = true;
19691969
}
19701970
}
19711971
}

src/enc/brotli_bit_stream.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,13 +1977,9 @@ fn EncodeContextMap<AllocU32: alloc::Allocator<u32>>(
19771977
i = i.wrapping_add(1);
19781978
}
19791979
{
1980-
let use_rle: i32 = if !!(max_run_length_prefix > 0u32) {
1981-
1i32
1982-
} else {
1983-
0i32
1984-
};
1985-
BrotliWriteBits(1, use_rle as (u64), storage_ix, storage);
1986-
if use_rle != 0 {
1980+
let use_rle = max_run_length_prefix > 0;
1981+
BrotliWriteBits(1, u64::from(use_rle), storage_ix, storage);
1982+
if use_rle {
19871983
BrotliWriteBits(
19881984
4,
19891985
max_run_length_prefix.wrapping_sub(1) as (u64),

src/enc/cluster.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ fn brotli_max_double(a: super::util::floatX, b: super::util::floatX) -> super::u
4848
#[inline(always)]
4949
fn HistogramPairIsLess(p1: &HistogramPair, p2: &HistogramPair) -> bool {
5050
if p1.cost_diff != p2.cost_diff {
51-
!!(p1.cost_diff > p2.cost_diff)
51+
p1.cost_diff > p2.cost_diff
5252
} else {
53-
!!(p1.idx2.wrapping_sub(p1.idx1) > p2.idx2.wrapping_sub(p2.idx1))
53+
p1.idx2.wrapping_sub(p1.idx1) > p2.idx2.wrapping_sub(p2.idx1)
5454
}
5555
}
5656

src/enc/command.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ pub fn InitCommand(
403403
GetLengthCode(
404404
insertlen,
405405
copylen_code,
406-
if !!((xself.dist_prefix_ as i32 & 0x3ff) == 0i32) {
407-
1i32
406+
if (xself.dist_prefix_ & 0x3ff) == 0 {
407+
1
408408
} else {
409-
0i32
409+
0
410410
},
411411
&mut xself.cmd_prefix_,
412412
);

0 commit comments

Comments
 (0)