Skip to content

Commit

Permalink
Convert non-public bool-ints into bools
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nyurik authored and danielrh committed Mar 12, 2024
1 parent da54ef1 commit d7d8b2d
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 277 deletions.
16 changes: 6 additions & 10 deletions src/enc/backward_references/hash_to_binary_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,7 @@ where
let mut matches_offset = 0usize;
let cur_ix_masked: usize = cur_ix & ring_buffer_mask;
let max_comp_len: usize = core::cmp::min(max_length, 128usize);
let should_reroot_tree: i32 = if !!(max_length >= 128usize) {
1i32
} else {
0i32
};
let should_reroot_tree = max_length >= 128;
let key = xself.HashBytes(&data[cur_ix_masked..]);
let forest: &mut [u32] = xself.forest.slice_mut();
let mut prev_ix: usize = xself.buckets_.slice()[key] as usize;
Expand All @@ -451,7 +447,7 @@ where
let mut best_len_left: usize = 0usize;
let mut best_len_right: usize = 0usize;
let mut depth_remaining: usize;
if should_reroot_tree != 0 {
if should_reroot_tree {
xself.buckets_.slice_mut()[key] = cur_ix as u32;
}
depth_remaining = 64usize;
Expand All @@ -460,7 +456,7 @@ where
let backward: usize = cur_ix.wrapping_sub(prev_ix);
let prev_ix_masked: usize = prev_ix & ring_buffer_mask;
if backward == 0usize || backward > max_backward || depth_remaining == 0usize {
if should_reroot_tree != 0 {
if should_reroot_tree {
forest[node_left] = xself.invalid_pos_;
forest[node_right] = xself.invalid_pos_;
}
Expand All @@ -484,7 +480,7 @@ where
matches_offset += 1;
}
if len >= max_comp_len {
if should_reroot_tree != 0 {
if should_reroot_tree {
forest[node_left] = forest[LeftChildIndexH10!(xself, prev_ix)];
forest[node_right] = forest[RightChildIndexH10!(xself, prev_ix)];
}
Expand All @@ -494,14 +490,14 @@ where
> data[prev_ix_masked.wrapping_add(len)] as i32
{
best_len_left = len;
if should_reroot_tree != 0 {
if should_reroot_tree {
forest[node_left] = prev_ix as u32;
}
node_left = RightChildIndexH10!(xself, prev_ix);
prev_ix = forest[node_left] as usize;
} else {
best_len_right = len;
if should_reroot_tree != 0 {
if should_reroot_tree {
forest[node_right] = prev_ix as u32;
}
node_right = LeftChildIndexH10!(xself, prev_ix);
Expand Down
21 changes: 6 additions & 15 deletions src/enc/backward_references/hq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ pub fn BrotliZopfliCreateCommands(
let len_code: usize = ZopfliNodeLengthCode(next) as usize;
let max_distance: usize =
brotli_min_size_t(block_start.wrapping_add(pos), max_backward_limit);
let is_dictionary: i32 = if !!(distance > max_distance.wrapping_add(gap)) {
1i32
} else {
0i32
};
let is_dictionary = distance > max_distance.wrapping_add(gap);
let dist_code: usize = ZopfliNodeDistanceCode(next) as usize;
InitCommand(
&mut commands[i],
Expand All @@ -153,7 +149,7 @@ pub fn BrotliZopfliCreateCommands(
len_code,
dist_code,
);
if is_dictionary == 0 && (dist_code > 0usize) {
if !is_dictionary && dist_code > 0 {
dist_cache[3] = dist_cache[2];
dist_cache[2] = dist_cache[1];
dist_cache[1] = dist_cache[0];
Expand Down Expand Up @@ -882,12 +878,7 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
{
let mut match_: BackwardMatch = BackwardMatch(matches[j]);
let dist: usize = match_.distance() as usize;
let is_dictionary_match: i32 =
if !!(dist > max_distance.wrapping_add(gap)) {
1i32
} else {
0i32
};
let is_dictionary_match = dist > max_distance.wrapping_add(gap);
let dist_code: usize = dist.wrapping_add(16).wrapping_sub(1);
let mut dist_symbol: u16 = 0;
let mut distextra: u32 = 0;
Expand All @@ -908,13 +899,13 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
);
let max_match_len: usize = BackwardMatchLength(&mut match_);
if len < max_match_len
&& (is_dictionary_match != 0 || max_match_len > max_zopfli_len)
&& (is_dictionary_match || max_match_len > max_zopfli_len)
{
len = max_match_len;
}
while len <= max_match_len {
{
let len_code: usize = if is_dictionary_match != 0 {
let len_code: usize = if is_dictionary_match {
BackwardMatchLengthCode(&mut match_)
} else {
len
Expand Down Expand Up @@ -1480,7 +1471,7 @@ pub fn BrotliCreateHqZopfliBackwardReferences<
} else {
<Alloc as Allocator<u64>>::AllocatedMemory::default()
};
if !!(0i32 == 0) && (matches_size != 0usize) {
if matches_size != 0 {
for (dst, src) in new_array
.slice_mut()
.split_at_mut(matches_size)
Expand Down
48 changes: 24 additions & 24 deletions src/enc/backward_references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
let mut best_len: usize = best_len_in;
let cached_backward: usize = distance_cache[0] as usize;
let mut prev_ix: usize = cur_ix.wrapping_sub(cached_backward);
let mut is_match_found: i32 = 0i32;
let mut is_match_found = false;
out.len_x_code = 0usize;
if prev_ix < cur_ix {
prev_ix &= ring_buffer_mask as u32 as usize;
Expand All @@ -388,7 +388,7 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
self.buckets_.slice_mut()[key as usize] = cur_ix as u32;
return true;
} else {
is_match_found = 1i32;
is_match_found = true;
}
}
}
Expand Down Expand Up @@ -440,12 +440,12 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
out.distance = backward;
out.score = score;
compare_char = data[cur_ix_masked.wrapping_add(best_len)] as i32;
is_match_found = 1i32;
is_match_found = true;
}
}
}
}
if dictionary.is_some() && self.buckets_.USE_DICTIONARY() != 0 && (is_match_found == 0) {
if dictionary.is_some() && self.buckets_.USE_DICTIONARY() != 0 && !is_match_found {
is_match_found = SearchInStaticDictionary(
dictionary.unwrap(),
dictionary_hash,
Expand All @@ -455,13 +455,13 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
max_backward.wrapping_add(gap),
max_distance,
out,
1i32,
true,
);
}
self.buckets_.slice_mut()
[(key as usize).wrapping_add((cur_ix >> 3).wrapping_rem(bucket_sweep as usize))] =
cur_ix as u32;
is_match_found != 0
is_match_found
}
}
impl<AllocU32: alloc::Allocator<u32>> BasicHashComputer for H2Sub<AllocU32> {
Expand Down Expand Up @@ -735,7 +735,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
let cur_ix_masked: usize = cur_ix & ring_buffer_mask;
let mut best_score: u64 = out.score;
let mut best_len: usize = best_len_in;
let mut is_match_found: i32 = 0i32;
let mut is_match_found = false;
out.len_x_code = 0usize;
for i in 0..H9_NUM_LAST_DISTANCES_TO_CHECK {
let idx = kDistanceCacheIndex[i] as usize;
Expand Down Expand Up @@ -767,7 +767,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
out.len = best_len;
out.distance = backward;
out.score = best_score;
is_match_found = 1i32;
is_match_found = true;
}
}
}
Expand Down Expand Up @@ -821,7 +821,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
out.len = best_len;
out.distance = backward;
out.score = best_score;
is_match_found = 1;
is_match_found = true;
if cur_ix_masked.wrapping_add(best_len) > ring_buffer_mask {
break;
}
Expand All @@ -833,7 +833,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
bucket[*self_num_key as usize & H9_BLOCK_MASK] = cur_ix as u32;
*self_num_key = self_num_key.wrapping_add(1);
}
if is_match_found == 0 && dictionary.is_some() {
if !is_match_found && dictionary.is_some() {
let (_, cur_data) = data.split_at(cur_ix_masked);
is_match_found = SearchInStaticDictionary(
dictionary.unwrap(),
Expand All @@ -844,10 +844,10 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
max_backward.wrapping_add(gap),
max_distance,
out,
0i32,
false,
);
}
is_match_found != 0
is_match_found
}

fn Store(&mut self, data: &[u8], mask: usize, ix: usize) {
Expand Down Expand Up @@ -1665,7 +1665,7 @@ impl<
) -> bool {
let opts = self.Opts();
let cur_ix_masked: usize = cur_ix & ring_buffer_mask;
let mut is_match_found: i32 = 0i32;
let mut is_match_found = false;
let mut best_score: u64 = out.score;
let mut best_len: usize = out.len;
let mut i: usize;
Expand Down Expand Up @@ -1707,7 +1707,7 @@ impl<
out.len = best_len;
out.distance = backward;
out.score = best_score;
is_match_found = 1i32;
is_match_found = true;
}
}
}
Expand Down Expand Up @@ -1763,7 +1763,7 @@ impl<
out.len = best_len;
out.distance = backward;
out.score = best_score;
is_match_found = 1i32;
is_match_found = true;
}
}
}
Expand All @@ -1772,7 +1772,7 @@ impl<
cur_ix as u32;
*num_ref_mut = num_ref_mut.wrapping_add(1);
}
if is_match_found == 0 && dictionary.is_some() {
if !is_match_found && dictionary.is_some() {
let (_, cur_data) = data.split_at(cur_ix_masked);
is_match_found = SearchInStaticDictionary(
dictionary.unwrap(),
Expand All @@ -1783,10 +1783,10 @@ impl<
max_backward.wrapping_add(gap),
max_distance,
out,
0i32,
false,
);
}
is_match_found != 0
is_match_found
}
}

Expand Down Expand Up @@ -1936,19 +1936,19 @@ fn SearchInStaticDictionary<HasherType: AnyHasher>(
max_backward: usize,
max_distance: usize,
out: &mut HasherSearchResult,
shallow: i32,
) -> i32 {
shallow: bool,
) -> bool {
let mut key: usize;
let mut i: usize;
let mut is_match_found: i32 = 0i32;
let mut is_match_found = false;
let opts = handle.Opts();
let xself: &mut Struct1 = handle.GetHasherCommon();
if xself.dict_num_matches < xself.dict_num_lookups >> 7 {
return 0i32;
return false;
}
key = (Hash14(data) << 1) as usize; //FIXME: works for any kind of hasher??
i = 0usize;
while i < if shallow != 0 { 1u32 } else { 2u32 } as usize {
while i < if shallow { 1 } else { 2 } {
{
let item: usize = dictionary_hash[key] as usize;
xself.dict_num_lookups = xself.dict_num_lookups.wrapping_add(1);
Expand All @@ -1965,7 +1965,7 @@ fn SearchInStaticDictionary<HasherType: AnyHasher>(
);
if item_matches != 0 {
xself.dict_num_matches = xself.dict_num_matches.wrapping_add(1);
is_match_found = 1i32;
is_match_found = true;
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/enc/brotli_bit_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1977,13 +1977,9 @@ fn EncodeContextMap<AllocU32: alloc::Allocator<u32>>(
i = i.wrapping_add(1);
}
{
let use_rle: i32 = if !!(max_run_length_prefix > 0u32) {
1i32
} else {
0i32
};
BrotliWriteBits(1, use_rle as (u64), storage_ix, storage);
if use_rle != 0 {
let use_rle = max_run_length_prefix > 0;
BrotliWriteBits(1, u64::from(use_rle), storage_ix, storage);
if use_rle {
BrotliWriteBits(
4,
max_run_length_prefix.wrapping_sub(1) as (u64),
Expand Down
4 changes: 2 additions & 2 deletions src/enc/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ fn brotli_max_double(a: super::util::floatX, b: super::util::floatX) -> super::u
#[inline(always)]
fn HistogramPairIsLess(p1: &HistogramPair, p2: &HistogramPair) -> bool {
if p1.cost_diff != p2.cost_diff {
!!(p1.cost_diff > p2.cost_diff)
p1.cost_diff > p2.cost_diff
} else {
!!(p1.idx2.wrapping_sub(p1.idx1) > p2.idx2.wrapping_sub(p2.idx1))
p1.idx2.wrapping_sub(p1.idx1) > p2.idx2.wrapping_sub(p2.idx1)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/enc/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ pub fn InitCommand(
GetLengthCode(
insertlen,
copylen_code,
if !!((xself.dist_prefix_ as i32 & 0x3ff) == 0i32) {
1i32
if (xself.dist_prefix_ & 0x3ff) == 0 {
1
} else {
0i32
0
},
&mut xself.cmd_prefix_,
);
Expand Down
Loading

0 comments on commit d7d8b2d

Please sign in to comment.