Skip to content

Commit

Permalink
Merge pull request #205 from nyurik/bool-return
Browse files Browse the repository at this point in the history
Use bool instead of i32
  • Loading branch information
danielrh authored May 19, 2024
2 parents dac157a + 3f7f482 commit a511970
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 74 deletions.
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ build-simd:

# Build the brotli-ffi crate (in ./c dir)
build-ffi:
# TODO: The c/Cargo.toml does not depend on the **unpublished** main crate, so its build never actually gets tested
RUSTFLAGS='-D warnings' cargo build --features ffi-api
RUSTFLAGS='-D warnings' cargo build --workspace --all-targets --bins --tests --lib --benches --examples --manifest-path c/Cargo.toml
# For now, use original make file for building/testing the FFI crate
cd c && make
Expand Down
10 changes: 5 additions & 5 deletions src/enc/backward_references/hq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,15 +1030,15 @@ pub fn BrotliCreateZopfliBackwardReferences<
}
}

fn SetCost(histogram: &[u32], histogram_size: usize, literal_histogram: i32, cost: &mut [floatX]) {
fn SetCost(histogram: &[u32], histogram_size: usize, literal_histogram: bool, cost: &mut [floatX]) {
let mut sum: u64 = 0;
for i in 0..histogram_size {
sum = sum.wrapping_add(u64::from(histogram[i]));
}
let log2sum = FastLog2(sum);

let mut missing_symbol_sum = sum;
if literal_histogram == 0 {
if !literal_histogram {
for i in 0..histogram_size {
if histogram[i] == 0 {
missing_symbol_sum = missing_symbol_sum.wrapping_add(1);
Expand Down Expand Up @@ -1107,19 +1107,19 @@ impl<AllocF: Allocator<floatX>> ZopfliCostModel<AllocF> {
SetCost(
&histogram_literal[..],
BROTLI_NUM_LITERAL_SYMBOLS,
1i32,
true,
&mut cost_literal,
);
SetCost(
&histogram_cmd[..],
BROTLI_NUM_COMMAND_SYMBOLS,
0i32,
false,
&mut cost_cmd[..],
);
SetCost(
&histogram_dist[..],
self.distance_histogram_size as usize,
0i32,
false,
self.cost_dist_.slice_mut(),
);
for i in 0usize..704usize {
Expand Down
10 changes: 5 additions & 5 deletions src/enc/brotli_bit_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,15 +1555,15 @@ fn StoreBlockSwitch(
code: &mut BlockSplitCode,
block_len: u32,
block_type: u8,
is_first_block: i32,
is_first_block: bool,
storage_ix: &mut usize,
storage: &mut [u8],
) {
let typecode: usize = NextBlockTypeCode(&mut code.type_code_calculator, block_type);
let mut lencode: usize = 0;
let mut len_nextra: u32 = 0;
let mut len_extra: u32 = 0;
if is_first_block == 0 {
if !is_first_block {
BrotliWriteBits(
code.type_depths[typecode] as u8,
code.type_bits[typecode] as (u64),
Expand Down Expand Up @@ -1634,7 +1634,7 @@ fn BuildAndStoreBlockSplitCode(
storage_ix,
storage,
);
StoreBlockSwitch(code, lengths[0], types[0], 1i32, storage_ix, storage);
StoreBlockSwitch(code, lengths[0], types[0], true, storage_ix, storage);
}
}

Expand Down Expand Up @@ -1962,7 +1962,7 @@ impl<Alloc: Allocator<u8> + Allocator<u16>> BlockEncoder<'_, Alloc> {
&mut self.block_split_code_,
block_len,
block_type,
0i32,
false,
storage_ix,
storage,
);
Expand Down Expand Up @@ -2059,7 +2059,7 @@ impl<Alloc: Allocator<u8> + Allocator<u16>> BlockEncoder<'_, Alloc> {
&mut self.block_split_code_,
block_len,
block_type,
0,
false,
storage_ix,
storage,
);
Expand Down
10 changes: 5 additions & 5 deletions src/enc/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn BrotliCompareAndPushToQueue<
pairs: &mut [HistogramPair],
num_pairs: &mut usize,
) {
let mut is_good_pair: i32 = 0i32;
let mut is_good_pair = false;
let mut p: HistogramPair = HistogramPair {
idx1: 0,
idx2: 0,
Expand All @@ -85,10 +85,10 @@ fn BrotliCompareAndPushToQueue<
p.cost_diff -= (out[idx2 as usize]).bit_cost();
if (out[idx1 as usize]).total_count() == 0usize {
p.cost_combo = (out[idx2 as usize]).bit_cost();
is_good_pair = 1i32;
is_good_pair = true;
} else if (out[idx2 as usize]).total_count() == 0usize {
p.cost_combo = (out[idx1 as usize]).bit_cost();
is_good_pair = 1i32;
is_good_pair = true;
} else {
let threshold: super::util::floatX = if *num_pairs == 0usize {
1e38 as super::util::floatX
Expand All @@ -101,10 +101,10 @@ fn BrotliCompareAndPushToQueue<
let cost_combo: super::util::floatX = BrotliPopulationCost(&combo, scratch_space);
if cost_combo < threshold - p.cost_diff {
p.cost_combo = cost_combo;
is_good_pair = 1i32;
is_good_pair = true;
}
}
if is_good_pair != 0 {
if is_good_pair {
p.cost_diff += p.cost_combo;
if *num_pairs > 0usize && HistogramPairIsLess(&pairs[0], &p) {
/* Replace the top of the queue if needed. */
Expand Down
11 changes: 6 additions & 5 deletions src/enc/compress_fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ use core::cmp::min;
// examples: IsMatch checks p1[4] and p1[5]
// the hoops that BuildAndStoreCommandPrefixCode goes through are subtly different in order
// (eg memcpy x+24, y instead of +24, y+40
// pretty much assume compress_fragment_two_pass is a trap! except for BrotliStoreMetaBlockHeader
// pretty much assume compress_fragment_two_pass is a trap! except for store_meta_block_header
use super::super::alloc;
use super::backward_references::kHashMul32;
use super::brotli_bit_stream::{BrotliBuildAndStoreHuffmanTreeFast, BrotliStoreHuffmanTree};
use super::compress_fragment_two_pass::{memcpy, BrotliStoreMetaBlockHeader, BrotliWriteBits};
use super::compress_fragment_two_pass::{memcpy, BrotliWriteBits};
use super::entropy_encode::{
BrotliConvertBitDepthsToSymbols, BrotliCreateHuffmanTree, HuffmanTree,
};
use super::static_dict::{
FindMatchLengthWithLimit, BROTLI_UNALIGNED_LOAD32, BROTLI_UNALIGNED_LOAD64,
};
use super::util::{FastLog2, Log2FloorNonZero};
use crate::enc::compress_fragment_two_pass::store_meta_block_header;
//use super::super::alloc::{SliceWrapper, SliceWrapperMut};

//static kHashMul32: u32 = 0x1e35a7bdu32;
Expand Down Expand Up @@ -242,7 +243,7 @@ fn EmitUncompressedMetaBlock(
storage: &mut [u8],
) {
RewindBitPosition(storage_ix_start, storage_ix, storage);
BrotliStoreMetaBlockHeader(len, 1i32, storage_ix, storage);
store_meta_block_header(len, true, storage_ix, storage);
*storage_ix = storage_ix.wrapping_add(7u32 as usize) & !7u32 as usize;
memcpy(storage, (*storage_ix >> 3), begin, 0, len);
*storage_ix = storage_ix.wrapping_add(len << 3);
Expand Down Expand Up @@ -683,7 +684,7 @@ fn BrotliCompressFragmentFastImpl<AllocHT: alloc::Allocator<HuffmanTree>>(
let mut input_index = 0usize;
let mut last_distance: i32;
let shift: usize = (64u32 as usize).wrapping_sub(table_bits);
BrotliStoreMetaBlockHeader(block_size, 0i32, storage_ix, storage);
store_meta_block_header(block_size, false, storage_ix, storage);
BrotliWriteBits(13usize, 0, storage_ix, storage);
literal_ratio = BuildAndStoreLiteralPrefixCode(
m,
Expand Down Expand Up @@ -1026,7 +1027,7 @@ fn BrotliCompressFragmentFastImpl<AllocHT: alloc::Allocator<HuffmanTree>>(
block_size = min(input_size, kFirstBlockSize);
total_block_size = block_size;
mlen_storage_ix = storage_ix.wrapping_add(3);
BrotliStoreMetaBlockHeader(block_size, 0i32, storage_ix, storage);
store_meta_block_header(block_size, false, storage_ix, storage);
BrotliWriteBits(13usize, 0, storage_ix, storage);
literal_ratio = BuildAndStoreLiteralPrefixCode(
m,
Expand Down
33 changes: 22 additions & 11 deletions src/enc/compress_fragment_two_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn CreateCommands(
);
let ip_limit: usize = input_index.wrapping_add(len_limit);
let mut next_hash: u32;
let mut goto_emit_remainder: i32 = 0i32;
let mut goto_emit_remainder = false;
next_hash = Hash(
&base_ip[{
ip_index = ip_index.wrapping_add(1);
Expand All @@ -194,7 +194,7 @@ fn CreateCommands(
shift,
min_match,
);
while goto_emit_remainder == 0 {
while !goto_emit_remainder {
let mut skip: u32 = 32u32;
let mut next_ip: usize = ip_index;
let mut candidate: usize = 0;
Expand All @@ -208,7 +208,7 @@ fn CreateCommands(
ip_index = next_ip;
next_ip = ip_index.wrapping_add(bytes_between_hash_lookups as usize);
if next_ip > ip_limit {
goto_emit_remainder = 1i32;
goto_emit_remainder = true;
{
break 'break3;
}
Expand All @@ -233,12 +233,12 @@ fn CreateCommands(
}
if !(ip_index.wrapping_sub(candidate)
> (1usize << 18).wrapping_sub(16) as isize as usize
&& (goto_emit_remainder == 0))
&& !goto_emit_remainder)
{
break;
}
}
if goto_emit_remainder != 0 {
if goto_emit_remainder {
break;
}
{
Expand Down Expand Up @@ -269,7 +269,7 @@ fn CreateCommands(
*num_commands += EmitCopyLenLastDistance(matched, commands);
next_emit = ip_index;
if ip_index >= ip_limit {
goto_emit_remainder = 1i32;
goto_emit_remainder = true;
{
break;
}
Expand Down Expand Up @@ -325,7 +325,7 @@ fn CreateCommands(
*num_commands += EmitDistance(last_distance as u32, commands);
next_emit = ip_index;
if ip_index >= ip_limit {
goto_emit_remainder = 1i32;
goto_emit_remainder = true;
{
break;
}
Expand Down Expand Up @@ -365,7 +365,7 @@ fn CreateCommands(
table[(cur_hash as usize)] = ip_index as i32;
}
}
if goto_emit_remainder == 0 {
if !goto_emit_remainder {
next_hash = Hash(
&base_ip[{
ip_index = ip_index.wrapping_add(1);
Expand Down Expand Up @@ -418,11 +418,22 @@ pub fn BrotliWriteBits(n_bits: usize, bits: u64, pos: &mut usize, array: &mut [u
BROTLI_UNALIGNED_STORE64(p, v);
*pos = pos.wrapping_add(n_bits);
}

#[deprecated(note = "use store_meta_block_header instead")]
pub fn BrotliStoreMetaBlockHeader(
len: usize,
is_uncompressed: i32,
storage_ix: &mut usize,
storage: &mut [u8],
) {
store_meta_block_header(len, is_uncompressed != 0, storage_ix, storage);
}

pub(crate) fn store_meta_block_header(
len: usize,
is_uncompressed: bool,
storage_ix: &mut usize,
storage: &mut [u8],
) {
let mut nibbles: u64 = 6;
BrotliWriteBits(1, 0, storage_ix, storage);
Expand All @@ -438,7 +449,7 @@ pub fn BrotliStoreMetaBlockHeader(
storage_ix,
storage,
);
BrotliWriteBits(1usize, is_uncompressed as (u64), storage_ix, storage);
BrotliWriteBits(1, u64::from(is_uncompressed), storage_ix, storage);
}

pub fn memcpy<T: Sized + Clone>(
Expand Down Expand Up @@ -639,7 +650,7 @@ fn EmitUncompressedMetaBlock(
storage_ix: &mut usize,
storage: &mut [u8],
) {
BrotliStoreMetaBlockHeader(input_size, 1i32, storage_ix, storage);
store_meta_block_header(input_size, true, storage_ix, storage);
*storage_ix = storage_ix.wrapping_add(7u32 as usize) & !7u32 as usize;
memcpy(storage, (*storage_ix >> 3), input, 0, input_size);
*storage_ix = storage_ix.wrapping_add(input_size << 3);
Expand Down Expand Up @@ -683,7 +694,7 @@ fn BrotliCompressFragmentTwoPassImpl<AllocHT: alloc::Allocator<HuffmanTree>>(
);
}
if ShouldCompress(&base_ip[input_index..], block_size, num_literals) {
BrotliStoreMetaBlockHeader(block_size, 0i32, storage_ix, storage);
store_meta_block_header(block_size, false, storage_ix, storage);
BrotliWriteBits(13usize, 0, storage_ix, storage);
StoreCommands(
m,
Expand Down
Loading

0 comments on commit a511970

Please sign in to comment.