// Copyright 2026 The Wuffs Authors. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. // // SPDX-License-Identifier: Apache-2.0 OR MIT // -------- // This file mirrors compress_fragment_two_pass.c, the quality 1 (two-pass) // encoder. Like encode_fragment_fast.wuffs, the C code's pointers are // base.u64 offsets from the start of the fragment (the C code's base_ip) and // a last_distance of 0 means the C code's -1. // // The C code's commands and literals pointers (into command_buf and // literal_buf) are the num_commands and num_literals fields. // Mirrors Hash in compress_fragment_two_pass.c. pri func encoder.hash_two_pass(input: roslice base.u8, i: base.u64, shift: base.u32, length: base.u32) base.u32 { var h : base.u64 h = (this.load64le(s: args.input, i: args.i) ~mod<< ((64 ~mod- (8 ~mod* args.length)) & 63)) ~mod* HASH_MUL32 return ((h >> (args.shift & 63)) & 0xFFFF_FFFF) as base.u32 } // Mirrors HashBytesAtOffset in compress_fragment_two_pass.c. pri func encoder.hash_bytes_at_offset_two_pass(v: base.u64, offset: base.u32, shift: base.u32, length: base.u32) base.u32 { var h : base.u64 h = ((args.v >> ((8 ~mod* args.offset) & 63)) ~mod<< ((64 ~mod- (8 ~mod* args.length)) & 63)) ~mod* HASH_MUL32 return ((h >> (args.shift & 63)) & 0xFFFF_FFFF) as base.u32 } // Mirrors IsMatch in compress_fragment_two_pass.c. pri func encoder.is_match_two_pass(input: roslice base.u8, i1: base.u64, i2: base.u64, length: base.u32) base.bool { var q1 : roslice base.u8 var q2 : roslice base.u8 if (args.i1 >= args.input.length()) or (args.i2 >= args.input.length()) { return false } if this.load32le(s: args.input, i: args.i1) <> this.load32le(s: args.input, i: args.i2) { return false } if args.length == 4 { return true } q1 = args.input[args.i1 ..] q2 = args.input[args.i2 ..] if (q1.length() >= 6) and (q2.length() >= 6) { return (q1[4] == q2[4]) and (q1[5] == q2[5]) } return false } // Mirrors BuildAndStoreCommandPrefixCode in compress_fragment_two_pass.c. // Note that its symbol order differs from the one in compress_fragment.c. pri func encoder.build_and_store_command_prefix_code_two_pass!(s: slice base.u8) { var i : base.u32 this.tmp_depth[.. 704].bulk_memset!(byte_value: 0) this.create_huffman_tree!(src: 0, off: 0, length: 64, tree_limit: 15, depth: this.cmd_depth[.. 64]) this.create_huffman_tree!(src: 0, off: 64, length: 64, tree_limit: 14, depth: this.cmd_depth[64 ..]) // We have to jump through a few hoops here in order to compute the // command bits because the symbols are in a different order than in the // full alphabet. This looks complicated, but having the symbols in this // order in the command bits saves a few branches in the Emit* functions. this.tmp_depth[0 .. 24].copy_from_slice!(s: this.cmd_depth[24 .. 48]) this.tmp_depth[24 .. 32].copy_from_slice!(s: this.cmd_depth[0 .. 8]) this.tmp_depth[32 .. 40].copy_from_slice!(s: this.cmd_depth[48 .. 56]) this.tmp_depth[40 .. 48].copy_from_slice!(s: this.cmd_depth[8 .. 16]) this.tmp_depth[48 .. 56].copy_from_slice!(s: this.cmd_depth[56 .. 64]) this.tmp_depth[56 .. 64].copy_from_slice!(s: this.cmd_depth[16 .. 24]) this.convert_bit_depths_to_symbols!(depth: this.tmp_depth[..], len: 64, dst: 0) this.copy_tmp_bits_to_cmd_bits!(dst: 0, src: 24, n: 8) this.copy_tmp_bits_to_cmd_bits!(dst: 8, src: 40, n: 8) this.copy_tmp_bits_to_cmd_bits!(dst: 16, src: 56, n: 8) this.copy_tmp_bits_to_cmd_bits!(dst: 24, src: 0, n: 24) this.copy_tmp_bits_to_cmd_bits!(dst: 48, src: 32, n: 8) this.copy_tmp_bits_to_cmd_bits!(dst: 56, src: 48, n: 8) this.convert_bit_depths_to_symbols!(depth: this.cmd_depth[64 ..], len: 64, dst: 1) // Create the bit length array for the full command alphabet. this.tmp_depth[.. 64].bulk_memset!(byte_value: 0) this.tmp_depth[0 .. 8].copy_from_slice!(s: this.cmd_depth[24 .. 32]) this.tmp_depth[64 .. 72].copy_from_slice!(s: this.cmd_depth[32 .. 40]) this.tmp_depth[128 .. 136].copy_from_slice!(s: this.cmd_depth[40 .. 48]) this.tmp_depth[192 .. 200].copy_from_slice!(s: this.cmd_depth[48 .. 56]) this.tmp_depth[384 .. 392].copy_from_slice!(s: this.cmd_depth[56 .. 64]) i = 0 while i < 8 { this.tmp_depth[128 + (8 * i)] = this.cmd_depth[i] this.tmp_depth[256 + (8 * i)] = this.cmd_depth[8 + i] this.tmp_depth[448 + (8 * i)] = this.cmd_depth[16 + i] i ~mod+= 1 } this.store_huffman_tree!(depths: this.tmp_depth[..], num: 704, s: args.s) this.store_huffman_tree!(depths: this.cmd_depth[64 ..], num: 64, s: args.s) } // push_command appends to command_buf. pri func encoder.push_command!(c: base.u32) { this.command_buf[this.num_commands & 0x1_FFFF] = args.c this.num_commands ~mod+= 1 } // Mirrors EmitInsertLen in compress_fragment_two_pass.c. pri func encoder.emit_insert_len_two_pass!(insertlen: base.u32) { var tail : base.u32 var nbits : base.u32 var prefix : base.u32 var extra : base.u32 if args.insertlen < 6 { this.push_command!(c: args.insertlen) } else if args.insertlen < 130 { tail = args.insertlen - 2 nbits = this.log2_floor_non_zero(n: tail as base.u64) ~mod- 1 prefix = tail >> (nbits & 31) extra = tail ~mod- (prefix ~mod<< (nbits & 31)) this.push_command!(c: ((((nbits ~mod<< 1) ~mod+ prefix) ~mod+ 2) | (extra ~mod<< 8))) } else if args.insertlen < 2114 { tail = args.insertlen - 66 nbits = this.log2_floor_non_zero(n: tail as base.u64) extra = tail ~mod- ((1 as base.u32) << nbits) this.push_command!(c: ((nbits + 10) | (extra ~mod<< 8))) } else if args.insertlen < 6210 { this.push_command!(c: (21 | ((args.insertlen - 2114) ~mod<< 8))) } else if args.insertlen < 22594 { this.push_command!(c: (22 | ((args.insertlen - 6210) ~mod<< 8))) } else { this.push_command!(c: (23 | ((args.insertlen - 22594) ~mod<< 8))) } } // Mirrors EmitCopyLen in compress_fragment_two_pass.c. pri func encoder.emit_copy_len_two_pass!(copylen: base.u64) { var tail : base.u64 var nbits : base.u32 var prefix : base.u64 var extra : base.u64 if args.copylen < 10 { this.push_command!(c: ((args.copylen + 38) & 0xFFFF_FFFF) as base.u32) } else if args.copylen < 134 { tail = args.copylen - 6 nbits = this.log2_floor_non_zero(n: tail) ~mod- 1 prefix = tail >> (nbits & 63) extra = tail ~mod- (prefix ~mod<< (nbits & 63)) this.push_command!(c: ((((((nbits as base.u64) ~mod<< 1) ~mod+ prefix) ~mod+ 44) | (extra ~mod<< 8)) & 0xFFFF_FFFF) as base.u32) } else if args.copylen < 2118 { tail = args.copylen - 70 nbits = this.log2_floor_non_zero(n: tail) extra = tail ~mod- ((1 as base.u64) << nbits) this.push_command!(c: ((((nbits as base.u64) + 52) | (extra ~mod<< 8)) & 0xFFFF_FFFF) as base.u32) } else { this.push_command!(c: ((63 | ((args.copylen ~mod- 2118) ~mod<< 8)) & 0xFFFF_FFFF) as base.u32) } } // Mirrors EmitCopyLenLastDistance in compress_fragment_two_pass.c. pri func encoder.emit_copy_len_last_distance_two_pass!(copylen: base.u64) { var tail : base.u64 var nbits : base.u32 var prefix : base.u64 var extra : base.u64 if args.copylen < 12 { this.push_command!(c: ((args.copylen + 20) & 0xFFFF_FFFF) as base.u32) } else if args.copylen < 72 { tail = args.copylen - 8 nbits = this.log2_floor_non_zero(n: tail) ~mod- 1 prefix = tail >> (nbits & 63) extra = tail ~mod- (prefix ~mod<< (nbits & 63)) this.push_command!(c: ((((((nbits as base.u64) ~mod<< 1) ~mod+ prefix) ~mod+ 28) | (extra ~mod<< 8)) & 0xFFFF_FFFF) as base.u32) } else if args.copylen < 136 { tail = args.copylen - 8 this.push_command!(c: (((tail >> 5) + 54) | ((tail & 31) << 8)) as base.u32) this.push_command!(c: 64) } else if args.copylen < 2120 { tail = args.copylen - 72 nbits = this.log2_floor_non_zero(n: tail) extra = tail ~mod- ((1 as base.u64) << nbits) this.push_command!(c: ((((nbits as base.u64) + 52) | (extra ~mod<< 8)) & 0xFFFF_FFFF) as base.u32) this.push_command!(c: 64) } else { this.push_command!(c: ((63 | ((args.copylen ~mod- 2120) ~mod<< 8)) & 0xFFFF_FFFF) as base.u32) this.push_command!(c: 64) } } // Mirrors EmitDistance in compress_fragment_two_pass.c. pri func encoder.emit_distance_two_pass!(distance: base.u32) { var d : base.u32 var nbits : base.u32 var prefix : base.u32 var offset : base.u32 var distcode : base.u32 var extra : base.u32 d = args.distance ~mod+ 3 nbits = this.log2_floor_non_zero(n: d as base.u64) ~mod- 1 prefix = (d >> (nbits & 31)) & 1 offset = (2 + prefix) ~mod<< (nbits & 31) distcode = ((2 ~mod* (nbits ~mod- 1)) ~mod+ prefix) ~mod+ 80 extra = d ~mod- offset this.push_command!(c: distcode | (extra ~mod<< 8)) } // push_literals appends args.input[args.from .. args.from + args.n] to // literal_buf, the C code's "memcpy(*literals, next_emit, insert)". pri func encoder.push_literals!(input: roslice base.u8, from: base.u64, n: base.u64) { var j : base.u64 j = 0 while j < args.n { if (args.from ~mod+ j) >= args.input.length() { break } this.literal_buf[this.num_literals & 0x1_FFFF] = args.input[args.from ~mod+ j] this.num_literals ~mod+= 1 j ~mod+= 1 } } // update_table_two_pass updates the hash table with the hashes of some // positions within the last copy (ending at ip), and returns the next // candidate. The C code has two copies of this block, which differ (for // min_match == 4) in the third hash's offset: third_offset is 0 for the first // copy and 2 for the second copy. pri func encoder.update_table_two_pass!(input: roslice base.u8, ip: base.u64, shift: base.u32, min_match: base.u32, third_offset: base.u32) base.u64 { var input_bytes : base.u64 var prev_hash : base.u32 var cur_hash : base.u32 var candidate : base.u64 var mm : base.u32 mm = args.min_match if mm == 4 { input_bytes = this.load64le(s: args.input, i: args.ip ~mod- 3) cur_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 3, shift: args.shift, length: mm) prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 0, shift: args.shift, length: mm) this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 3) & 0xFFFF_FFFF) as base.u32 prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 1, shift: args.shift, length: mm) this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 2) & 0xFFFF_FFFF) as base.u32 prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: args.third_offset, shift: args.shift, length: mm) this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 1) & 0xFFFF_FFFF) as base.u32 } else { input_bytes = this.load64le(s: args.input, i: args.ip ~mod- 5) prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 0, shift: args.shift, length: mm) this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 5) & 0xFFFF_FFFF) as base.u32 prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 1, shift: args.shift, length: mm) this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 4) & 0xFFFF_FFFF) as base.u32 prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 2, shift: args.shift, length: mm) this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 3) & 0xFFFF_FFFF) as base.u32 input_bytes = this.load64le(s: args.input, i: args.ip ~mod- 2) cur_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 2, shift: args.shift, length: mm) prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 0, shift: args.shift, length: mm) this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 2) & 0xFFFF_FFFF) as base.u32 prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 1, shift: args.shift, length: mm) this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 1) & 0xFFFF_FFFF) as base.u32 } candidate = this.hash_table[cur_hash & 0x1_FFFF] as base.u64 this.hash_table[cur_hash & 0x1_FFFF] = (args.ip & 0xFFFF_FFFF) as base.u32 return candidate } // Mirrors CreateCommands in compress_fragment_two_pass.c. The block is the // block_size bytes at args.input[args.from ..]. input_size is the number of // bytes remaining in the fragment, from args.from. pri func encoder.create_commands!(input: roslice base.u8, from: base.u64, block_size: base.u64, input_size: base.u64, table_bits: base.u32, min_match: base.u32) { var ip : base.u64 var shift : base.u32 var ip_end : base.u64 var next_emit : base.u64 var last_distance : base.u64 var len_limit : base.u64 var ip_limit : base.u64 var next_hash : base.u32 var hash : base.u32 var skip : base.u32 var next_ip : base.u64 var candidate : base.u64 var base_ip : base.u64 var matched : base.u64 var distance : base.u64 var insert : base.u64 var mm : base.u64 this.num_commands = 0 this.num_literals = 0 mm = args.min_match as base.u64 ip = args.from shift = 64 ~mod- args.table_bits ip_end = args.from ~mod+ args.block_size next_emit = args.from if args.block_size >= 16 { // For the last block, we need to keep a 16 bytes margin so that we // can be sure that all distances are at most window size - 16. For // all other blocks, we only need to keep a margin of 5 bytes so that // we don't go over the block size with a copy. len_limit = args.block_size ~mod- mm len_limit = len_limit.min(no_more_than: args.input_size ~mod- 16) ip_limit = args.from ~mod+ len_limit ip ~mod+= 1 next_hash = this.hash_two_pass(input: args.input, i: ip, shift: shift, length: args.min_match) while.outer true { // Step 1: Scan forward in the input looking for a 6-byte-long // match. If we get close to exhausting the input then goto // emit_remainder. skip = 32 next_ip = ip while.trawl true { hash = next_hash ip = next_ip next_ip = ip ~mod+ ((skip >> 5) as base.u64) skip ~mod+= 1 if next_ip > ip_limit { break.outer } next_hash = this.hash_two_pass(input: args.input, i: next_ip, shift: shift, length: args.min_match) if (last_distance > 0) and (last_distance <= ip) { candidate = ip ~mod- last_distance if this.is_match_two_pass(input: args.input, i1: ip, i2: candidate, length: args.min_match) { this.hash_table[hash & 0x1_FFFF] = (ip & 0xFFFF_FFFF) as base.u32 // Check copy distance. If candidate is not feasible, // continue search. if (ip ~mod- candidate) > FRAGMENT_MAX_DISTANCE { continue.trawl } break.trawl } } candidate = this.hash_table[hash & 0x1_FFFF] as base.u64 this.hash_table[hash & 0x1_FFFF] = (ip & 0xFFFF_FFFF) as base.u32 if this.is_match_two_pass(input: args.input, i1: ip, i2: candidate, length: args.min_match) { if (ip ~mod- candidate) > FRAGMENT_MAX_DISTANCE { continue.trawl } break.trawl } }.trawl // Step 2: Emit the found match together with the literal bytes // from "next_emit", and then see if we can find a next match // immediately afterwards. Repeat until we find no match for the // input without emitting some literal bytes. base_ip = ip matched = mm ~mod+ this.find_match_length_with_limit( s: args.input, i1: candidate ~mod+ mm, i2: ip ~mod+ mm, limit: (ip_end ~mod- ip) ~mod- mm) distance = base_ip ~mod- candidate insert = base_ip ~mod- next_emit ip ~mod+= matched this.emit_insert_len_two_pass!(insertlen: (insert & 0xFFFF_FFFF) as base.u32) this.push_literals!(input: args.input, from: next_emit, n: insert) if distance == last_distance { this.push_command!(c: 64) } else { this.emit_distance_two_pass!(distance: (distance & 0xFFFF_FFFF) as base.u32) last_distance = distance } this.emit_copy_len_last_distance_two_pass!(copylen: matched) next_emit = ip if ip >= ip_limit { break.outer } candidate = this.update_table_two_pass!( input: args.input, ip: ip, shift: shift, min_match: args.min_match, third_offset: 0) while (ip ~mod- candidate) <= FRAGMENT_MAX_DISTANCE { if not this.is_match_two_pass(input: args.input, i1: ip, i2: candidate, length: args.min_match) { break } // We have a 6-byte match at ip, and no need to emit any // literal bytes prior to ip. base_ip = ip matched = mm ~mod+ this.find_match_length_with_limit( s: args.input, i1: candidate ~mod+ mm, i2: ip ~mod+ mm, limit: (ip_end ~mod- ip) ~mod- mm) ip ~mod+= matched last_distance = base_ip ~mod- candidate this.emit_copy_len_two_pass!(copylen: matched) this.emit_distance_two_pass!(distance: (last_distance & 0xFFFF_FFFF) as base.u32) next_emit = ip if ip >= ip_limit { break.outer } candidate = this.update_table_two_pass!( input: args.input, ip: ip, shift: shift, min_match: args.min_match, third_offset: 2) } ip ~mod+= 1 next_hash = this.hash_two_pass(input: args.input, i: ip, shift: shift, length: args.min_match) }.outer } // emit_remainder: // Emit the remaining bytes as literals. if next_emit < ip_end { insert = ip_end ~mod- next_emit this.emit_insert_len_two_pass!(insertlen: (insert & 0xFFFF_FFFF) as base.u32) this.push_literals!(input: args.input, from: next_emit, n: insert) } } // Mirrors StoreCommands in compress_fragment_two_pass.c. pri func encoder.store_commands!(s: slice base.u8) { var i : base.u32 var j : base.u32 var cmd : base.u32 var code : base.u32 var extra : base.u32 var insert : base.u32 var lit_i : base.u32 var lit : base.u8 this.histogram[.. 256].bulk_memset!(byte_value: 0) this.cmd_depth[.. 128].bulk_memset!(byte_value: 0) this.cmd_bits[.. 128].bulk_memset!(byte_value: 0) this.cmd_histo[.. 128].bulk_memset!(byte_value: 0) i = 0 while i < this.num_literals { this.histogram[this.literal_buf[i & 0x1_FFFF]] ~mod+= 1 i ~mod+= 1 } this.build_and_store_huffman_tree_fast!(histogram_total: this.num_literals as base.u64, max_bits: 8, s: args.s) i = 0 while i < this.num_commands { code = this.command_buf[i & 0x1_FFFF] & 0xFF this.cmd_histo[code & 127] ~mod+= 1 i ~mod+= 1 } this.cmd_histo[1] ~mod+= 1 this.cmd_histo[2] ~mod+= 1 this.cmd_histo[64] ~mod+= 1 this.cmd_histo[84] ~mod+= 1 this.build_and_store_command_prefix_code_two_pass!(s: args.s) i = 0 while i < this.num_commands { cmd = this.command_buf[i & 0x1_FFFF] code = cmd & 0x7F extra = cmd >> 8 this.write_bits!(n_bits: this.cmd_depth[code] as base.u32, bits: this.cmd_bits[code] as base.u64, s: args.s) this.write_bits!(n_bits: TWO_PASS_NUM_EXTRA_BITS[code], bits: extra as base.u64, s: args.s) if code < 24 { insert = TWO_PASS_INSERT_OFFSET[code] ~mod+ extra j = 0 while j < insert { lit = this.literal_buf[lit_i & 0x1_FFFF] this.write_bits!(n_bits: this.lit_depth[lit] as base.u32, bits: this.lit_bits[lit] as base.u64, s: args.s) lit_i ~mod+= 1 j ~mod+= 1 } } i ~mod+= 1 } } // Mirrors ShouldCompress in compress_fragment_two_pass.c, for the input_size // bytes at args.input[args.from ..]. pri func encoder.should_compress_two_pass!(input: roslice base.u8, from: base.u64, input_size: base.u64, num_literals: base.u64) base.bool { var corpus_size : base.u64 var max_total_bit_cost : base.u64 var i : base.u64 corpus_size = this.bc.f64_from_u64(a: args.input_size) // MIN_RATIO is 0.98 (0x3FEF_5C28_F5C2_8F5C). if this.bc.f64_lt( a: this.bc.f64_from_u64(a: args.num_literals), b: this.bc.f64_mul(a: 0x3FEF_5C28_F5C2_8F5C, b: corpus_size)) <> 0 { return true } // max_total_bit_cost = corpus_size * 8 * MIN_RATIO / SAMPLE_RATE, where // SAMPLE_RATE is 43 and 8.0 is 0x4020_0000_0000_0000. max_total_bit_cost = this.sf.f64_div( a: this.bc.f64_mul( a: this.bc.f64_mul(a: corpus_size, b: 0x4020_0000_0000_0000), b: 0x3FEF_5C28_F5C2_8F5C), b: 0x4045_8000_0000_0000) this.histogram[.. 256].bulk_memset!(byte_value: 0) i = 0 while i < args.input_size { if (args.from ~mod+ i) < args.input.length() { this.histogram[args.input[args.from ~mod+ i]] ~mod+= 1 } i ~mod+= 43 } return this.bc.f64_lt(a: this.bits_entropy_of_histogram(), b: max_total_bit_cost) <> 0 } // Mirrors EmitUncompressedMetaBlock in compress_fragment_two_pass.c (which, // unlike the one in compress_fragment.c, does not rewind). pri func encoder.emit_uncompressed_meta_block_two_pass!(input: roslice base.u8, from: base.u64, input_size: base.u64, s: slice base.u8) { var p : base.u64 this.store_meta_block_header!(len: args.input_size, is_uncompressed: true, s: args.s) this.align_to_byte!() this.copy_bytes!(input: args.input, begin: args.from, end: args.from ~mod+ args.input_size, s: args.s) this.bw_ix ~mod+= args.input_size ~mod<< 3 p = this.bw_ix >> 3 if p < args.s.length() { args.s[p] = 0 } } // Mirrors BrotliCompressFragmentTwoPass and BrotliCompressFragmentTwoPassImpl // in compress_fragment_two_pass.c. pri func encoder.compress_fragment_two_pass!(input: roslice base.u8, is_last: base.bool, table_bits: base.u32, s: slice base.u8) { var initial_ix : base.u64 var min_match : base.u32 var from : base.u64 var input_size : base.u64 var block_size : base.u64 var compress : base.bool initial_ix = this.bw_ix min_match = 6 if args.table_bits <= 15 { min_match = 4 } input_size = args.input.length() while input_size > 0 { block_size = input_size.min(no_more_than: TWO_PASS_BLOCK_SIZE as base.u64) this.create_commands!( input: args.input, from: from, block_size: block_size, input_size: input_size, table_bits: args.table_bits, min_match: min_match) compress = this.should_compress_two_pass!( input: args.input, from: from, input_size: block_size, num_literals: this.num_literals as base.u64) if compress { this.store_meta_block_header!(len: block_size, is_uncompressed: false, s: args.s) // No block splits, no contexts. this.write_bits!(n_bits: 13, bits: 0, s: args.s) this.store_commands!(s: args.s) } else { // Since we did not find many backward references and the entropy // of the data is close to 8 bits, we can simply emit an // uncompressed block. This makes compression speed of // uncompressible data about 3x faster. this.emit_uncompressed_meta_block_two_pass!(input: args.input, from: from, input_size: block_size, s: args.s) } from ~mod+= block_size input_size ~mod-= block_size } // If output is larger than single uncompressed block, rewrite it. if (this.bw_ix ~mod- initial_ix) > (31 ~mod+ (args.input.length() ~mod<< 3)) { this.rewind_bit_position!(new_ix: initial_ix, s: args.s) this.emit_uncompressed_meta_block_two_pass!(input: args.input, from: 0, input_size: args.input.length(), s: args.s) } if args.is_last { this.write_bits!(n_bits: 1, bits: 1, s: args.s) // islast this.write_bits!(n_bits: 1, bits: 1, s: args.s) // isempty this.align_to_byte!() } }