Skip to content

Commit 6c4cd18

Browse files
nyurikdanielrh
authored andcommitted
Make hex numbers more readable
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.
1 parent 2ff7b21 commit 6c4cd18

18 files changed

+68
-84
lines changed

src/enc/backward_references/hq.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn BrotliInitZopfliNodes(array: &mut [ZopfliNode], length: usize) {
7070
impl ZopfliNode {
7171
#[inline(always)]
7272
fn copy_length(&self) -> u32 {
73-
self.length & 0x1ffffffu32
73+
self.length & 0x01ff_ffff
7474
}
7575

7676
#[inline(always)]
@@ -121,7 +121,7 @@ pub fn BrotliZopfliCreateCommands(
121121
{
122122
let next: &ZopfliNode = &nodes[pos.wrapping_add(offset as usize)];
123123
let copy_length = next.copy_length() as usize;
124-
let mut insert_length: usize = (next.dcode_insert_length & 0x7ffffff) as usize;
124+
let mut insert_length: usize = (next.dcode_insert_length & 0x07ff_ffff) as usize;
125125
pos = pos.wrapping_add(insert_length);
126126
offset = match next.u {
127127
Union1::next(off) => off,
@@ -458,7 +458,7 @@ fn ComputeDistanceShortcut(
458458
nodes: &[ZopfliNode],
459459
) -> u32 {
460460
let clen: usize = nodes[pos].copy_length() as usize;
461-
let ilen: usize = ((nodes[pos]).dcode_insert_length) as usize & 0x7ffffff;
461+
let ilen: usize = ((nodes[pos]).dcode_insert_length) as usize & 0x07ff_ffff;
462462
let dist: usize = nodes[pos].copy_distance() as usize;
463463
if pos == 0usize {
464464
0u32
@@ -494,7 +494,7 @@ fn ComputeDistanceCache(
494494
_ => 0,
495495
} as usize;
496496
while idx < 4i32 && (p > 0usize) {
497-
let ilen: usize = ((nodes[p]).dcode_insert_length) as usize & 0x7ffffff;
497+
let ilen: usize = ((nodes[p]).dcode_insert_length) as usize & 0x07ff_ffff;
498498
let clen = nodes[p].copy_length() as usize;
499499
let dist = nodes[p].copy_distance() as usize;
500500
dist_cache[({
@@ -821,7 +821,7 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
821821
let distnumextra: u32 = u32::from(dist_symbol) >> 10;
822822
let dist_cost = base_cost
823823
+ (distnumextra as floatX)
824-
+ model.get_distance_cost((dist_symbol as i32 & 0x3ff) as usize);
824+
+ model.get_distance_cost((dist_symbol as i32 & 0x03ff) as usize);
825825
let max_match_len: usize = BackwardMatchLength(&mut match_);
826826
if len < max_match_len
827827
&& (is_dictionary_match || max_match_len > max_zopfli_len)
@@ -873,15 +873,15 @@ impl ZopfliNode {
873873
#[inline(always)]
874874
fn command_length(&self) -> u32 {
875875
self.copy_length()
876-
.wrapping_add(self.dcode_insert_length & 0x7ffffff)
876+
.wrapping_add(self.dcode_insert_length & 0x07ff_ffff)
877877
}
878878
}
879879

880880
#[inline(always)]
881881
fn ComputeShortestPathFromNodes(num_bytes: usize, nodes: &mut [ZopfliNode]) -> usize {
882882
let mut index: usize = num_bytes;
883883
let mut num_commands: usize = 0usize;
884-
while ((nodes[index]).dcode_insert_length & 0x7ffffff) == 0 && ((nodes[index]).length == 1u32) {
884+
while (nodes[index].dcode_insert_length & 0x07ff_ffff) == 0 && nodes[index].length == 1 {
885885
index = index.wrapping_sub(1);
886886
}
887887
nodes[index].u = Union1::next(!(0u32));
@@ -1147,7 +1147,7 @@ impl<AllocF: Allocator<floatX>> ZopfliCostModel<AllocF> {
11471147
{
11481148
let inslength: usize = (commands[i]).insert_len_ as usize;
11491149
let copylength: usize = CommandCopyLen(&commands[i]) as usize;
1150-
let distcode: usize = ((commands[i]).dist_prefix_ as i32 & 0x3ff) as usize;
1150+
let distcode: usize = (commands[i].dist_prefix_ as i32 & 0x03ff) as usize;
11511151
let cmdcode: usize = (commands[i]).cmd_prefix_ as usize;
11521152
{
11531153
let _rhs = 1;

src/enc/backward_references/mod.rs

+22-28
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@ use super::static_dict::{
1616
use super::util::{floatX, Log2FloorNonZero};
1717
use core::cmp::{max, min};
1818

19-
static kBrotliMinWindowBits: i32 = 10i32;
20-
21-
static kBrotliMaxWindowBits: i32 = 24i32;
22-
23-
pub static kInvalidMatch: u32 = 0xfffffffu32;
24-
25-
static kCutoffTransformsCount: u32 = 10u32;
26-
27-
static kCutoffTransforms: u64 = 0x71b520au64 << 32 | 0xda2d3200u32 as (u64);
28-
29-
pub static kHashMul32: u32 = 0x1e35a7bdu32;
30-
31-
pub static kHashMul64: u64 = 0x1e35a7bdu64 << 32 | 0x1e35a7bdu64;
32-
33-
pub static kHashMul64Long: u64 = 0x1fe35a7bu32 as (u64) << 32 | 0xd3579bd3u32 as (u64);
19+
static kBrotliMinWindowBits: i32 = 10;
20+
static kBrotliMaxWindowBits: i32 = 24;
21+
pub static kInvalidMatch: u32 = 0x0fff_ffff;
22+
static kCutoffTransformsCount: u32 = 10;
23+
static kCutoffTransforms: u64 = 0x071b_520a_da2d_3200;
24+
pub static kHashMul32: u32 = 0x1e35_a7bd;
25+
pub static kHashMul64: u64 = 0x1e35_a7bd_1e35_a7bd;
26+
pub static kHashMul64Long: u64 = 0x1fe3_5a7b_d357_9bd3;
3427

3528
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
3629
#[repr(C)]
@@ -946,8 +939,8 @@ impl AdvHashSpecialization for HQ5Sub {
946939
}
947940
#[inline(always)]
948941
fn get_hash_mask(&self) -> u64 {
949-
//return 0xffffffffffffffffu64;
950-
0xffffffffu64 // make it 32 bit
942+
//return 0xffff_ffff_ffff_ffff;
943+
0xffff_ffff // make it 32 bit
951944
}
952945
#[inline(always)]
953946
fn get_k_hash_mul(&self) -> u64 {
@@ -993,8 +986,8 @@ impl AdvHashSpecialization for HQ7Sub {
993986
}
994987
#[inline(always)]
995988
fn get_hash_mask(&self) -> u64 {
996-
//return 0xffffffffffffffffu64;
997-
0xffffffffu64 // make it 32 bit
989+
//return 0xffff_ffff_ffff_ffff;
990+
0xffff_ffff // make it 32 bit
998991
}
999992
#[inline(always)]
1000993
fn get_k_hash_mul(&self) -> u64 {
@@ -1041,8 +1034,8 @@ impl AdvHashSpecialization for H5Sub {
10411034
self.block_mask_
10421035
}
10431036
fn get_hash_mask(&self) -> u64 {
1044-
//return 0xffffffffffffffffu64;
1045-
0xffffffffu64 // make it 32 bit
1037+
//return 0xffff_ffff_ffff_ffff;
1038+
0xffff_ffff // make it 32 bit
10461039
}
10471040
fn get_k_hash_mul(&self) -> u64 {
10481041
kHashMul32 as u64
@@ -1115,7 +1108,8 @@ impl AdvHashSpecialization for H6Sub {
11151108
}
11161109

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

11211115
impl<
@@ -1146,7 +1140,7 @@ impl<
11461140
let chunk_count = (ix_end - ix_start) / 4;
11471141
for chunk_id in 0..chunk_count {
11481142
let i = (ix_start + chunk_id * 4) & mask;
1149-
let ffffffff = 0xffffffff;
1143+
let ffffffff = 0xffff_ffff;
11501144
let word = u64::from(data[i])
11511145
| (u64::from(data[i + 1]) << 8)
11521146
| (u64::from(data[i + 2]) << 16)
@@ -1229,7 +1223,7 @@ impl<
12291223
);
12301224
for quad_index in 0..(REG_SIZE >> 2) {
12311225
let i = quad_index << 2;
1232-
let ffffffff = 0xffffffff;
1226+
let ffffffff = 0xffff_ffff;
12331227
let word = u64::from(data64[i])
12341228
| (u64::from(data64[i + 1]) << 8)
12351229
| (u64::from(data64[i + 2]) << 16)
@@ -1311,7 +1305,7 @@ impl<
13111305
.clone_from_slice(data.split_at(ix_offset).1.split_at(REG_SIZE + lookahead4).0);
13121306
for quad_index in 0..(REG_SIZE >> 2) {
13131307
let i = quad_index << 2;
1314-
let ffffffff = 0xffffffff;
1308+
let ffffffff = 0xffff_ffff;
13151309
let word = u64::from(data64[i])
13161310
| (u64::from(data64[i + 1]) << 8)
13171311
| (u64::from(data64[i + 2]) << 16)
@@ -1505,13 +1499,13 @@ impl<
15051499
| (u64::from(data[li + 7]) << 56);
15061500
let hi = (ix + 8) & mask;
15071501
let hword = u64::from(data[hi]) | (u64::from(data[hi + 1]) << 8);
1508-
let mixed0 = ((((lword & 0xffffffff) * self.specialization.get_k_hash_mul())
1502+
let mixed0 = ((((lword & 0xffff_ffff) * self.specialization.get_k_hash_mul())
15091503
& self.specialization.get_hash_mask())
15101504
>> shift) as usize;
1511-
let mixed1 = (((((lword >> 16) & 0xffffffff) * self.specialization.get_k_hash_mul())
1505+
let mixed1 = (((((lword >> 16) & 0xffff_ffff) * self.specialization.get_k_hash_mul())
15121506
& self.specialization.get_hash_mask())
15131507
>> shift) as usize;
1514-
let mixed2 = (((((lword >> 32) & 0xffffffff) * self.specialization.get_k_hash_mul())
1508+
let mixed2 = (((((lword >> 32) & 0xffff_ffff) * self.specialization.get_k_hash_mul())
15151509
& self.specialization.get_hash_mask())
15161510
>> shift) as usize;
15171511
let mixed3 = ((((((hword & 0xffff) << 16) | ((lword >> 48) & 0xffff))

src/enc/backward_references/test.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,25 @@ fn test_bulk_store_range_off_spec() {
168168
assert!(hasher_d == hasher_c);
169169
hasher_a.BulkStoreRange(
170170
RANDOM_THEN_UNICODE,
171-
0xfff,
171+
0x0fff,
172172
15,
173173
RANDOM_THEN_UNICODE.len() - 8,
174174
);
175175
hasher_c.BulkStoreRange(
176176
RANDOM_THEN_UNICODE,
177-
0xfff,
177+
0x0fff,
178178
15,
179179
RANDOM_THEN_UNICODE.len() - 8,
180180
);
181181
hasher_c.BulkStoreRange(
182182
RANDOM_THEN_UNICODE,
183-
0xfff,
183+
0x0fff,
184184
RANDOM_THEN_UNICODE.len(),
185185
RANDOM_THEN_UNICODE.len() - 8,
186186
); // noop
187187
for i in 15..RANDOM_THEN_UNICODE.len() - 8 {
188-
hasher_b.Store(RANDOM_THEN_UNICODE, 0xfff, i);
189-
hasher_d.Store(RANDOM_THEN_UNICODE, 0xfff, i);
188+
hasher_b.Store(RANDOM_THEN_UNICODE, 0x0fff, i);
189+
hasher_d.Store(RANDOM_THEN_UNICODE, 0x0fff, i);
190190
}
191191
assert_eq!(hasher_a.buckets.slice(), hasher_c.buckets.slice());
192192
assert_eq!(hasher_b.buckets.slice(), hasher_d.buckets.slice());

src/enc/block_splitter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn CountLiterals(cmds: &[Command], num_commands: usize) -> usize {
125125
}
126126

127127
fn CommandCopyLen(xself: &Command) -> u32 {
128-
xself.copy_len_ & 0x1ffffffu32
128+
xself.copy_len_ & 0x01ff_ffff
129129
}
130130

131131
fn CopyLiteralsToByteArray(
@@ -973,7 +973,7 @@ pub fn BrotliSplitBlock<
973973
for i in 0usize..num_commands {
974974
let cmd = &cmds[i];
975975
if CommandCopyLen(cmd) != 0 && (cmd.cmd_prefix_ as i32 >= 128i32) {
976-
distance_prefixes.slice_mut()[j] = cmd.dist_prefix_ & 0x3ff;
976+
distance_prefixes.slice_mut()[j] = cmd.dist_prefix_ & 0x03ff;
977977
j = j.wrapping_add(1);
978978
}
979979
}

src/enc/brotli_bit_stream.rs

+9-19
Original file line numberDiff line numberDiff line change
@@ -962,12 +962,7 @@ pub fn BrotliStoreHuffmanTree(
962962
}
963963

964964
fn StoreStaticCodeLengthCode(storage_ix: &mut usize, storage: &mut [u8]) {
965-
BrotliWriteBits(
966-
40,
967-
0xffu32 as (u64) << 32 | 0x55555554u32 as (u64),
968-
storage_ix,
969-
storage,
970-
);
965+
BrotliWriteBits(40, 0xff_5555_5554, storage_ix, storage);
971966
}
972967

973968
pub struct SimpleSortHuffmanTree {}
@@ -1998,7 +1993,7 @@ impl Command {
19981993
fn copy_len_code(&self) -> u32 {
19991994
let modifier = self.copy_len_ >> 25;
20001995
let delta: i32 = ((modifier | ((modifier & 0x40) << 1)) as u8) as i8 as i32;
2001-
((self.copy_len_ & 0x1ffffff) as i32 + delta) as u32
1996+
((self.copy_len_ & 0x01ff_ffff) as i32 + delta) as u32
20021997
}
20031998
}
20041999

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

20972092
fn CommandCopyLen(xself: &Command) -> u32 {
2098-
xself.copy_len_ & 0x1ffffffu32
2093+
xself.copy_len_ & 0x01ff_ffff
20992094
}
21002095

21012096
fn CommandDistanceContext(xself: &Command) -> u32 {
@@ -2325,7 +2320,7 @@ pub fn BrotliStoreMetaBlock<Alloc: BrotliAlloc, Cb>(
23252320
prev_byte2 = input[(pos.wrapping_sub(2) & mask)];
23262321
prev_byte = input[(pos.wrapping_sub(1) & mask)];
23272322
if cmd.cmd_prefix_ as i32 >= 128i32 {
2328-
let dist_code: usize = cmd.dist_prefix_ as usize & 0x3ff;
2323+
let dist_code: usize = cmd.dist_prefix_ as usize & 0x03ff;
23292324
let distnumextra: u32 = u32::from(cmd.dist_prefix_) >> 10; //FIXME: from command
23302325
let distextra: u64 = cmd.dist_extra_ as (u64);
23312326
if mb.distance_context_map_size == 0usize {
@@ -2378,7 +2373,7 @@ fn BuildHistograms(
23782373
}
23792374
pos = pos.wrapping_add(CommandCopyLen(&cmd) as usize);
23802375
if CommandCopyLen(&cmd) != 0 && (cmd.cmd_prefix_ as i32 >= 128i32) {
2381-
HistogramAddItem(dist_histo, cmd.dist_prefix_ as usize & 0x3ff);
2376+
HistogramAddItem(dist_histo, cmd.dist_prefix_ as usize & 0x03ff);
23822377
}
23832378
}
23842379
}
@@ -2425,7 +2420,7 @@ fn StoreDataWithHuffmanCodes(
24252420
}
24262421
pos = pos.wrapping_add(CommandCopyLen(&cmd) as usize);
24272422
if CommandCopyLen(&cmd) != 0 && (cmd.cmd_prefix_ as i32 >= 128i32) {
2428-
let dist_code: usize = cmd.dist_prefix_ as usize & 0x3ff;
2423+
let dist_code: usize = cmd.dist_prefix_ as usize & 0x03ff;
24292424
let distnumextra: u32 = u32::from(cmd.dist_prefix_) >> 10;
24302425
let distextra: u32 = cmd.dist_extra_;
24312426
BrotliWriteBits(
@@ -2559,17 +2554,12 @@ pub fn BrotliStoreMetaBlockTrivial<Alloc: BrotliAlloc, Cb>(
25592554
}
25602555

25612556
fn StoreStaticCommandHuffmanTree(storage_ix: &mut usize, storage: &mut [u8]) {
2562-
BrotliWriteBits(
2563-
56,
2564-
0x926244u32 as (u64) << 32 | 0x16307003,
2565-
storage_ix,
2566-
storage,
2567-
);
2568-
BrotliWriteBits(3, 0x0u64, storage_ix, storage);
2557+
BrotliWriteBits(56, 0x0092_6244_1630_7003, storage_ix, storage);
2558+
BrotliWriteBits(3, 0, storage_ix, storage);
25692559
}
25702560

25712561
fn StoreStaticDistanceHuffmanTree(storage_ix: &mut usize, storage: &mut [u8]) {
2572-
BrotliWriteBits(28, 0x369dc03u64, storage_ix, storage);
2562+
BrotliWriteBits(28, 0x0369_dc03, storage_ix, storage);
25732563
}
25742564

25752565
struct BlockSplitRef<'a> {

src/enc/command.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct Command {
2121
}
2222

2323
pub fn CommandCopyLen(xself: &Command) -> u32 {
24-
xself.copy_len_ & 0x1ffffffu32
24+
xself.copy_len_ & 0x01ff_ffff
2525
}
2626

2727
pub fn CommandDistanceContext(xself: &Command) -> u32 {
@@ -45,9 +45,9 @@ pub fn ComputeDistanceCode(distance: usize, max_distance: usize, dist_cache: &[i
4545
} else if distance == dist_cache[1] as usize {
4646
return 1;
4747
} else if offset0 < 7usize {
48-
return (0x9750468i32 >> (4usize).wrapping_mul(offset0) & 0xfi32) as usize;
48+
return (0x0975_0468_i32 >> (4usize).wrapping_mul(offset0) & 0xfi32) as usize;
4949
} else if offset1 < 7usize {
50-
return (0xfdb1acei32 >> (4usize).wrapping_mul(offset1) & 0xfi32) as usize;
50+
return (0x0fdb_1ace_i32 >> (4usize).wrapping_mul(offset1) & 0xfi32) as usize;
5151
} else if distance == dist_cache[2] as usize {
5252
return 2usize;
5353
} else if distance == dist_cache[3] as usize {

src/enc/encode.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ pub const BROTLI_LARGE_MAX_WBITS: u32 = 30;
359359

360360
pub const BROTLI_MAX_DISTANCE_BITS: u32 = 24;
361361
pub const BROTLI_MAX_WINDOW_BITS: usize = BROTLI_MAX_DISTANCE_BITS as usize;
362-
pub const BROTLI_MAX_DISTANCE: usize = 0x3FFFFFC;
363-
pub const BROTLI_MAX_ALLOWED_DISTANCE: usize = 0x7FFFFFC;
362+
pub const BROTLI_MAX_DISTANCE: usize = 0x03ff_fffc;
363+
pub const BROTLI_MAX_ALLOWED_DISTANCE: usize = 0x07ff_fffc;
364364
pub const BROTLI_NUM_DISTANCE_SHORT_CODES: u32 = 16;
365365
pub fn BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX: u32, NDIRECT: u32, MAXNBITS: u32) -> u32 {
366366
BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) + ((MAXNBITS) << ((NPOSTFIX) + 1))
@@ -417,7 +417,7 @@ impl<Alloc: BrotliAlloc> BrotliEncoderStateStruct<Alloc> {
417417

418418
let mask = self.ringbuffer_.mask_;
419419
let max_backward_distance: u64 = (1u64 << self.params.lgwin) - BROTLI_WINDOW_GAP as u64;
420-
let last_copy_len = u64::from(last_command.copy_len_) & 0x1ffffff;
420+
let last_copy_len = u64::from(last_command.copy_len_) & 0x01ff_ffff;
421421
let last_processed_pos: u64 = self.last_processed_pos_ - last_copy_len;
422422
let max_distance: u64 = if last_processed_pos < max_backward_distance {
423423
last_processed_pos
@@ -447,9 +447,9 @@ impl<Alloc: BrotliAlloc> BrotliEncoderStateStruct<Alloc> {
447447
/* The copy length is at most the metablock size, and thus expressible. */
448448
GetLengthCode(
449449
last_command.insert_len_ as usize,
450-
((last_command.copy_len_ & 0x1FFFFFF) as i32
450+
((last_command.copy_len_ & 0x01ff_ffff) as i32
451451
+ (last_command.copy_len_ >> 25) as i32) as usize,
452-
((last_command.dist_prefix_ & 0x3FF) == 0) as i32,
452+
((last_command.dist_prefix_ & 0x03ff) == 0) as i32,
453453
&mut last_command.cmd_prefix_,
454454
);
455455
}
@@ -3100,7 +3100,7 @@ impl<Alloc: BrotliAlloc> BrotliEncoderStateStruct<Alloc> {
31003100
}
31013101

31023102
pub fn BrotliEncoderVersion() -> u32 {
3103-
0x1000f01u32
3103+
0x0100_0f01
31043104
}
31053105

31063106
impl<Alloc: BrotliAlloc> BrotliEncoderStateStruct<Alloc> {

src/enc/entropy_encode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ fn BrotliWriteHuffmanTreeRepetitions(
430430
repetitions = repetitions.wrapping_sub(3);
431431
loop {
432432
tree[*tree_size] = 16u8;
433-
extra_bits_data[*tree_size] = (repetitions & 0x3usize) as u8;
433+
extra_bits_data[*tree_size] = (repetitions & 0x03) as u8;
434434
*tree_size = tree_size.wrapping_add(1);
435435
repetitions >>= 2i32;
436436
if repetitions == 0usize {

src/enc/find_stride.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ impl<AllocU32: alloc::Allocator<u32>> EntropyPyramid<AllocU32> {
605605
.split_at(input.len() >> 3)
606606
.1,
607607
scratch,
608-
0xa,
608+
0x0a,
609609
Some(5..7),
610610
Some(7..0xa),
611611
);

0 commit comments

Comments
 (0)