Skip to content

Commit

Permalink
Make hex numbers more readable
Browse files Browse the repository at this point in the history
When a hex number is used with bit operations, it tends to be less readable as it gets longer.  Hex numbers should have each 4 digits separated by a `_`, and separate from the type - so that we can easily see if the number group has 3 or 4 digits.
  • Loading branch information
nyurik authored and danielrh committed Apr 8, 2024
1 parent 2ff7b21 commit 6c4cd18
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 84 deletions.
16 changes: 8 additions & 8 deletions src/enc/backward_references/hq.rs
Expand Up @@ -70,7 +70,7 @@ pub fn BrotliInitZopfliNodes(array: &mut [ZopfliNode], length: usize) {
impl ZopfliNode {
#[inline(always)]
fn copy_length(&self) -> u32 {
self.length & 0x1ffffffu32
self.length & 0x01ff_ffff
}

#[inline(always)]
Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn BrotliZopfliCreateCommands(
{
let next: &ZopfliNode = &nodes[pos.wrapping_add(offset as usize)];
let copy_length = next.copy_length() as usize;
let mut insert_length: usize = (next.dcode_insert_length & 0x7ffffff) as usize;
let mut insert_length: usize = (next.dcode_insert_length & 0x07ff_ffff) as usize;
pos = pos.wrapping_add(insert_length);
offset = match next.u {
Union1::next(off) => off,
Expand Down Expand Up @@ -458,7 +458,7 @@ fn ComputeDistanceShortcut(
nodes: &[ZopfliNode],
) -> u32 {
let clen: usize = nodes[pos].copy_length() as usize;
let ilen: usize = ((nodes[pos]).dcode_insert_length) as usize & 0x7ffffff;
let ilen: usize = ((nodes[pos]).dcode_insert_length) as usize & 0x07ff_ffff;
let dist: usize = nodes[pos].copy_distance() as usize;
if pos == 0usize {
0u32
Expand Down Expand Up @@ -494,7 +494,7 @@ fn ComputeDistanceCache(
_ => 0,
} as usize;
while idx < 4i32 && (p > 0usize) {
let ilen: usize = ((nodes[p]).dcode_insert_length) as usize & 0x7ffffff;
let ilen: usize = ((nodes[p]).dcode_insert_length) as usize & 0x07ff_ffff;
let clen = nodes[p].copy_length() as usize;
let dist = nodes[p].copy_distance() as usize;
dist_cache[({
Expand Down Expand Up @@ -821,7 +821,7 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
let distnumextra: u32 = u32::from(dist_symbol) >> 10;
let dist_cost = base_cost
+ (distnumextra as floatX)
+ model.get_distance_cost((dist_symbol as i32 & 0x3ff) as usize);
+ model.get_distance_cost((dist_symbol as i32 & 0x03ff) as usize);
let max_match_len: usize = BackwardMatchLength(&mut match_);
if len < max_match_len
&& (is_dictionary_match || max_match_len > max_zopfli_len)
Expand Down Expand Up @@ -873,15 +873,15 @@ impl ZopfliNode {
#[inline(always)]
fn command_length(&self) -> u32 {
self.copy_length()
.wrapping_add(self.dcode_insert_length & 0x7ffffff)
.wrapping_add(self.dcode_insert_length & 0x07ff_ffff)
}
}

#[inline(always)]
fn ComputeShortestPathFromNodes(num_bytes: usize, nodes: &mut [ZopfliNode]) -> usize {
let mut index: usize = num_bytes;
let mut num_commands: usize = 0usize;
while ((nodes[index]).dcode_insert_length & 0x7ffffff) == 0 && ((nodes[index]).length == 1u32) {
while (nodes[index].dcode_insert_length & 0x07ff_ffff) == 0 && nodes[index].length == 1 {
index = index.wrapping_sub(1);
}
nodes[index].u = Union1::next(!(0u32));
Expand Down Expand Up @@ -1147,7 +1147,7 @@ impl<AllocF: Allocator<floatX>> ZopfliCostModel<AllocF> {
{
let inslength: usize = (commands[i]).insert_len_ as usize;
let copylength: usize = CommandCopyLen(&commands[i]) as usize;
let distcode: usize = ((commands[i]).dist_prefix_ as i32 & 0x3ff) as usize;
let distcode: usize = (commands[i].dist_prefix_ as i32 & 0x03ff) as usize;
let cmdcode: usize = (commands[i]).cmd_prefix_ as usize;
{
let _rhs = 1;
Expand Down
50 changes: 22 additions & 28 deletions src/enc/backward_references/mod.rs
Expand Up @@ -16,21 +16,14 @@ use super::static_dict::{
use super::util::{floatX, Log2FloorNonZero};
use core::cmp::{max, min};

static kBrotliMinWindowBits: i32 = 10i32;

static kBrotliMaxWindowBits: i32 = 24i32;

pub static kInvalidMatch: u32 = 0xfffffffu32;

static kCutoffTransformsCount: u32 = 10u32;

static kCutoffTransforms: u64 = 0x71b520au64 << 32 | 0xda2d3200u32 as (u64);

pub static kHashMul32: u32 = 0x1e35a7bdu32;

pub static kHashMul64: u64 = 0x1e35a7bdu64 << 32 | 0x1e35a7bdu64;

pub static kHashMul64Long: u64 = 0x1fe35a7bu32 as (u64) << 32 | 0xd3579bd3u32 as (u64);
static kBrotliMinWindowBits: i32 = 10;
static kBrotliMaxWindowBits: i32 = 24;
pub static kInvalidMatch: u32 = 0x0fff_ffff;
static kCutoffTransformsCount: u32 = 10;
static kCutoffTransforms: u64 = 0x071b_520a_da2d_3200;
pub static kHashMul32: u32 = 0x1e35_a7bd;
pub static kHashMul64: u64 = 0x1e35_a7bd_1e35_a7bd;
pub static kHashMul64Long: u64 = 0x1fe3_5a7b_d357_9bd3;

#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[repr(C)]
Expand Down Expand Up @@ -946,8 +939,8 @@ impl AdvHashSpecialization for HQ5Sub {
}
#[inline(always)]
fn get_hash_mask(&self) -> u64 {
//return 0xffffffffffffffffu64;
0xffffffffu64 // make it 32 bit
//return 0xffff_ffff_ffff_ffff;
0xffff_ffff // make it 32 bit
}
#[inline(always)]
fn get_k_hash_mul(&self) -> u64 {
Expand Down Expand Up @@ -993,8 +986,8 @@ impl AdvHashSpecialization for HQ7Sub {
}
#[inline(always)]
fn get_hash_mask(&self) -> u64 {
//return 0xffffffffffffffffu64;
0xffffffffu64 // make it 32 bit
//return 0xffff_ffff_ffff_ffff;
0xffff_ffff // make it 32 bit
}
#[inline(always)]
fn get_k_hash_mul(&self) -> u64 {
Expand Down Expand Up @@ -1041,8 +1034,8 @@ impl AdvHashSpecialization for H5Sub {
self.block_mask_
}
fn get_hash_mask(&self) -> u64 {
//return 0xffffffffffffffffu64;
0xffffffffu64 // make it 32 bit
//return 0xffff_ffff_ffff_ffff;
0xffff_ffff // make it 32 bit
}
fn get_k_hash_mul(&self) -> u64 {
kHashMul32 as u64
Expand Down Expand Up @@ -1115,7 +1108,8 @@ impl AdvHashSpecialization for H6Sub {
}

fn BackwardReferencePenaltyUsingLastDistance(distance_short_code: usize) -> u64 {
(39u64).wrapping_add((0x1ca10u64 >> (distance_short_code & 0xeusize) & 0xeu64))
// FIXME?: double bitwise AND with the same value?
(39u64).wrapping_add((0x0001_ca10_u64 >> (distance_short_code & 0x0e) & 0x0e))
}

impl<
Expand Down Expand Up @@ -1146,7 +1140,7 @@ impl<
let chunk_count = (ix_end - ix_start) / 4;
for chunk_id in 0..chunk_count {
let i = (ix_start + chunk_id * 4) & mask;
let ffffffff = 0xffffffff;
let ffffffff = 0xffff_ffff;
let word = u64::from(data[i])
| (u64::from(data[i + 1]) << 8)
| (u64::from(data[i + 2]) << 16)
Expand Down Expand Up @@ -1229,7 +1223,7 @@ impl<
);
for quad_index in 0..(REG_SIZE >> 2) {
let i = quad_index << 2;
let ffffffff = 0xffffffff;
let ffffffff = 0xffff_ffff;
let word = u64::from(data64[i])
| (u64::from(data64[i + 1]) << 8)
| (u64::from(data64[i + 2]) << 16)
Expand Down Expand Up @@ -1311,7 +1305,7 @@ impl<
.clone_from_slice(data.split_at(ix_offset).1.split_at(REG_SIZE + lookahead4).0);
for quad_index in 0..(REG_SIZE >> 2) {
let i = quad_index << 2;
let ffffffff = 0xffffffff;
let ffffffff = 0xffff_ffff;
let word = u64::from(data64[i])
| (u64::from(data64[i + 1]) << 8)
| (u64::from(data64[i + 2]) << 16)
Expand Down Expand Up @@ -1505,13 +1499,13 @@ impl<
| (u64::from(data[li + 7]) << 56);
let hi = (ix + 8) & mask;
let hword = u64::from(data[hi]) | (u64::from(data[hi + 1]) << 8);
let mixed0 = ((((lword & 0xffffffff) * self.specialization.get_k_hash_mul())
let mixed0 = ((((lword & 0xffff_ffff) * self.specialization.get_k_hash_mul())
& self.specialization.get_hash_mask())
>> shift) as usize;
let mixed1 = (((((lword >> 16) & 0xffffffff) * self.specialization.get_k_hash_mul())
let mixed1 = (((((lword >> 16) & 0xffff_ffff) * self.specialization.get_k_hash_mul())
& self.specialization.get_hash_mask())
>> shift) as usize;
let mixed2 = (((((lword >> 32) & 0xffffffff) * self.specialization.get_k_hash_mul())
let mixed2 = (((((lword >> 32) & 0xffff_ffff) * self.specialization.get_k_hash_mul())
& self.specialization.get_hash_mask())
>> shift) as usize;
let mixed3 = ((((((hword & 0xffff) << 16) | ((lword >> 48) & 0xffff))
Expand Down
10 changes: 5 additions & 5 deletions src/enc/backward_references/test.rs
Expand Up @@ -168,25 +168,25 @@ fn test_bulk_store_range_off_spec() {
assert!(hasher_d == hasher_c);
hasher_a.BulkStoreRange(
RANDOM_THEN_UNICODE,
0xfff,
0x0fff,
15,
RANDOM_THEN_UNICODE.len() - 8,
);
hasher_c.BulkStoreRange(
RANDOM_THEN_UNICODE,
0xfff,
0x0fff,
15,
RANDOM_THEN_UNICODE.len() - 8,
);
hasher_c.BulkStoreRange(
RANDOM_THEN_UNICODE,
0xfff,
0x0fff,
RANDOM_THEN_UNICODE.len(),
RANDOM_THEN_UNICODE.len() - 8,
); // noop
for i in 15..RANDOM_THEN_UNICODE.len() - 8 {
hasher_b.Store(RANDOM_THEN_UNICODE, 0xfff, i);
hasher_d.Store(RANDOM_THEN_UNICODE, 0xfff, i);
hasher_b.Store(RANDOM_THEN_UNICODE, 0x0fff, i);
hasher_d.Store(RANDOM_THEN_UNICODE, 0x0fff, i);
}
assert_eq!(hasher_a.buckets.slice(), hasher_c.buckets.slice());
assert_eq!(hasher_b.buckets.slice(), hasher_d.buckets.slice());
Expand Down
4 changes: 2 additions & 2 deletions src/enc/block_splitter.rs
Expand Up @@ -125,7 +125,7 @@ fn CountLiterals(cmds: &[Command], num_commands: usize) -> usize {
}

fn CommandCopyLen(xself: &Command) -> u32 {
xself.copy_len_ & 0x1ffffffu32
xself.copy_len_ & 0x01ff_ffff
}

fn CopyLiteralsToByteArray(
Expand Down Expand Up @@ -973,7 +973,7 @@ pub fn BrotliSplitBlock<
for i in 0usize..num_commands {
let cmd = &cmds[i];
if CommandCopyLen(cmd) != 0 && (cmd.cmd_prefix_ as i32 >= 128i32) {
distance_prefixes.slice_mut()[j] = cmd.dist_prefix_ & 0x3ff;
distance_prefixes.slice_mut()[j] = cmd.dist_prefix_ & 0x03ff;
j = j.wrapping_add(1);
}
}
Expand Down
28 changes: 9 additions & 19 deletions src/enc/brotli_bit_stream.rs
Expand Up @@ -962,12 +962,7 @@ pub fn BrotliStoreHuffmanTree(
}

fn StoreStaticCodeLengthCode(storage_ix: &mut usize, storage: &mut [u8]) {
BrotliWriteBits(
40,
0xffu32 as (u64) << 32 | 0x55555554u32 as (u64),
storage_ix,
storage,
);
BrotliWriteBits(40, 0xff_5555_5554, storage_ix, storage);
}

pub struct SimpleSortHuffmanTree {}
Expand Down Expand Up @@ -1998,7 +1993,7 @@ impl Command {
fn copy_len_code(&self) -> u32 {
let modifier = self.copy_len_ >> 25;
let delta: i32 = ((modifier | ((modifier & 0x40) << 1)) as u8) as i8 as i32;
((self.copy_len_ & 0x1ffffff) as i32 + delta) as u32
((self.copy_len_ & 0x01ff_ffff) as i32 + delta) as u32
}
}

Expand Down Expand Up @@ -2095,7 +2090,7 @@ impl<Alloc: Allocator<u8> + Allocator<u16>> BlockEncoder<'_, Alloc> {
}

fn CommandCopyLen(xself: &Command) -> u32 {
xself.copy_len_ & 0x1ffffffu32
xself.copy_len_ & 0x01ff_ffff
}

fn CommandDistanceContext(xself: &Command) -> u32 {
Expand Down Expand Up @@ -2325,7 +2320,7 @@ pub fn BrotliStoreMetaBlock<Alloc: BrotliAlloc, Cb>(
prev_byte2 = input[(pos.wrapping_sub(2) & mask)];
prev_byte = input[(pos.wrapping_sub(1) & mask)];
if cmd.cmd_prefix_ as i32 >= 128i32 {
let dist_code: usize = cmd.dist_prefix_ as usize & 0x3ff;
let dist_code: usize = cmd.dist_prefix_ as usize & 0x03ff;
let distnumextra: u32 = u32::from(cmd.dist_prefix_) >> 10; //FIXME: from command
let distextra: u64 = cmd.dist_extra_ as (u64);
if mb.distance_context_map_size == 0usize {
Expand Down Expand Up @@ -2378,7 +2373,7 @@ fn BuildHistograms(
}
pos = pos.wrapping_add(CommandCopyLen(&cmd) as usize);
if CommandCopyLen(&cmd) != 0 && (cmd.cmd_prefix_ as i32 >= 128i32) {
HistogramAddItem(dist_histo, cmd.dist_prefix_ as usize & 0x3ff);
HistogramAddItem(dist_histo, cmd.dist_prefix_ as usize & 0x03ff);
}
}
}
Expand Down Expand Up @@ -2425,7 +2420,7 @@ fn StoreDataWithHuffmanCodes(
}
pos = pos.wrapping_add(CommandCopyLen(&cmd) as usize);
if CommandCopyLen(&cmd) != 0 && (cmd.cmd_prefix_ as i32 >= 128i32) {
let dist_code: usize = cmd.dist_prefix_ as usize & 0x3ff;
let dist_code: usize = cmd.dist_prefix_ as usize & 0x03ff;
let distnumextra: u32 = u32::from(cmd.dist_prefix_) >> 10;
let distextra: u32 = cmd.dist_extra_;
BrotliWriteBits(
Expand Down Expand Up @@ -2559,17 +2554,12 @@ pub fn BrotliStoreMetaBlockTrivial<Alloc: BrotliAlloc, Cb>(
}

fn StoreStaticCommandHuffmanTree(storage_ix: &mut usize, storage: &mut [u8]) {
BrotliWriteBits(
56,
0x926244u32 as (u64) << 32 | 0x16307003,
storage_ix,
storage,
);
BrotliWriteBits(3, 0x0u64, storage_ix, storage);
BrotliWriteBits(56, 0x0092_6244_1630_7003, storage_ix, storage);
BrotliWriteBits(3, 0, storage_ix, storage);
}

fn StoreStaticDistanceHuffmanTree(storage_ix: &mut usize, storage: &mut [u8]) {
BrotliWriteBits(28, 0x369dc03u64, storage_ix, storage);
BrotliWriteBits(28, 0x0369_dc03, storage_ix, storage);
}

struct BlockSplitRef<'a> {
Expand Down
6 changes: 3 additions & 3 deletions src/enc/command.rs
Expand Up @@ -21,7 +21,7 @@ pub struct Command {
}

pub fn CommandCopyLen(xself: &Command) -> u32 {
xself.copy_len_ & 0x1ffffffu32
xself.copy_len_ & 0x01ff_ffff
}

pub fn CommandDistanceContext(xself: &Command) -> u32 {
Expand All @@ -45,9 +45,9 @@ pub fn ComputeDistanceCode(distance: usize, max_distance: usize, dist_cache: &[i
} else if distance == dist_cache[1] as usize {
return 1;
} else if offset0 < 7usize {
return (0x9750468i32 >> (4usize).wrapping_mul(offset0) & 0xfi32) as usize;
return (0x0975_0468_i32 >> (4usize).wrapping_mul(offset0) & 0xfi32) as usize;
} else if offset1 < 7usize {
return (0xfdb1acei32 >> (4usize).wrapping_mul(offset1) & 0xfi32) as usize;
return (0x0fdb_1ace_i32 >> (4usize).wrapping_mul(offset1) & 0xfi32) as usize;
} else if distance == dist_cache[2] as usize {
return 2usize;
} else if distance == dist_cache[3] as usize {
Expand Down
12 changes: 6 additions & 6 deletions src/enc/encode.rs
Expand Up @@ -359,8 +359,8 @@ pub const BROTLI_LARGE_MAX_WBITS: u32 = 30;

pub const BROTLI_MAX_DISTANCE_BITS: u32 = 24;
pub const BROTLI_MAX_WINDOW_BITS: usize = BROTLI_MAX_DISTANCE_BITS as usize;
pub const BROTLI_MAX_DISTANCE: usize = 0x3FFFFFC;
pub const BROTLI_MAX_ALLOWED_DISTANCE: usize = 0x7FFFFFC;
pub const BROTLI_MAX_DISTANCE: usize = 0x03ff_fffc;
pub const BROTLI_MAX_ALLOWED_DISTANCE: usize = 0x07ff_fffc;
pub const BROTLI_NUM_DISTANCE_SHORT_CODES: u32 = 16;
pub fn BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX: u32, NDIRECT: u32, MAXNBITS: u32) -> u32 {
BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) + ((MAXNBITS) << ((NPOSTFIX) + 1))
Expand Down Expand Up @@ -417,7 +417,7 @@ impl<Alloc: BrotliAlloc> BrotliEncoderStateStruct<Alloc> {

let mask = self.ringbuffer_.mask_;
let max_backward_distance: u64 = (1u64 << self.params.lgwin) - BROTLI_WINDOW_GAP as u64;
let last_copy_len = u64::from(last_command.copy_len_) & 0x1ffffff;
let last_copy_len = u64::from(last_command.copy_len_) & 0x01ff_ffff;
let last_processed_pos: u64 = self.last_processed_pos_ - last_copy_len;
let max_distance: u64 = if last_processed_pos < max_backward_distance {
last_processed_pos
Expand Down Expand Up @@ -447,9 +447,9 @@ impl<Alloc: BrotliAlloc> BrotliEncoderStateStruct<Alloc> {
/* The copy length is at most the metablock size, and thus expressible. */
GetLengthCode(
last_command.insert_len_ as usize,
((last_command.copy_len_ & 0x1FFFFFF) as i32
((last_command.copy_len_ & 0x01ff_ffff) as i32
+ (last_command.copy_len_ >> 25) as i32) as usize,
((last_command.dist_prefix_ & 0x3FF) == 0) as i32,
((last_command.dist_prefix_ & 0x03ff) == 0) as i32,
&mut last_command.cmd_prefix_,
);
}
Expand Down Expand Up @@ -3100,7 +3100,7 @@ impl<Alloc: BrotliAlloc> BrotliEncoderStateStruct<Alloc> {
}

pub fn BrotliEncoderVersion() -> u32 {
0x1000f01u32
0x0100_0f01
}

impl<Alloc: BrotliAlloc> BrotliEncoderStateStruct<Alloc> {
Expand Down
2 changes: 1 addition & 1 deletion src/enc/entropy_encode.rs
Expand Up @@ -430,7 +430,7 @@ fn BrotliWriteHuffmanTreeRepetitions(
repetitions = repetitions.wrapping_sub(3);
loop {
tree[*tree_size] = 16u8;
extra_bits_data[*tree_size] = (repetitions & 0x3usize) as u8;
extra_bits_data[*tree_size] = (repetitions & 0x03) as u8;
*tree_size = tree_size.wrapping_add(1);
repetitions >>= 2i32;
if repetitions == 0usize {
Expand Down
2 changes: 1 addition & 1 deletion src/enc/find_stride.rs
Expand Up @@ -605,7 +605,7 @@ impl<AllocU32: alloc::Allocator<u32>> EntropyPyramid<AllocU32> {
.split_at(input.len() >> 3)
.1,
scratch,
0xa,
0x0a,
Some(5..7),
Some(7..0xa),
);
Expand Down

0 comments on commit 6c4cd18

Please sign in to comment.