mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 16:47:28 +01:00
Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7121776bf0 | ||
|
|
fbae955a74 | ||
|
|
044437a8ec | ||
|
|
8ac9e2fd4c | ||
|
|
d95d2ea7f9 | ||
|
|
c9bb73e3bf | ||
|
|
7d040276e5 | ||
|
|
c17d9a9147 | ||
|
|
2fc6b96e38 | ||
|
|
bc1b5fcd49 | ||
|
|
5d6de9880f |
No files matched your search
-107
@@ -1,107 +0,0 @@
|
||||
# M7b handoff: brotli differential fuzzing
|
||||
|
||||
Branch m7b-fuzz (worktree /home/user/wuffs-m7b). Stopped 2026-10-27 16:47 UTC at the
|
||||
coordinator's request. Scratch area (logs, binaries, repro files):
|
||||
SC = the agent session's scratchpad directory, subdirectory m7b (under /tmp; ask the coordinator for the path)
|
||||
(session scratch, may not survive; the regression tests below cover divergences 1-3).
|
||||
|
||||
## Harnesses and how to run them
|
||||
|
||||
1. script/manual-test-brotli-fuzz.c: differential fuzzer against libbrotli v1.2.1.
|
||||
Modes: -dec (mutated streams, random I/O chunking, output limits, workbuf lengths, LW quirk,
|
||||
0-15 raw + serialized dictionaries), -enc (random inputs, params, plans, dst limits, dicts,
|
||||
q10-11 workbufs; byte-exact output + round trip through both decoders), -api (edge cases and
|
||||
random step sequences), -replay=PATH (re-run a saved decoder failure).
|
||||
Options: -seed=S -n=N -secs=T -q=LO-HI -nohq -out=DIR -data=DIR.
|
||||
Build (from the repo root; libbrotlidec must be built with -DBROTLI_EXPERIMENTAL, see the
|
||||
file's header comment; SC/exp/libbrotlidecexp.a is that build):
|
||||
gcc -O2 -g -std=c99 -I/home/user/brotli-ref/c/include -I/home/user/brotli-ref/c \
|
||||
script/manual-test-brotli-fuzz.c /home/user/brotli-ref-build/libbrotlienc.a \
|
||||
SC/exp/libbrotlidecexp.a -lm -o SC/bfuzz
|
||||
Shards: SC/shard.sh MODE SEED SECS [args] (log SC/logs/MODE-SEED.log, summary line appended
|
||||
to SC/results-MODE.txt); SC/chain.sh "MODE|ARGS|SEED"... runs shards one after another and
|
||||
skips seeds already in the results file. Use at most 2 shards at once. A shard's counts are
|
||||
only printed when it ends (a killed shard counts nothing).
|
||||
Known/accepted classes (not failures): decoder "known BLOCK_LENGTH" (divergence 1); encoder
|
||||
both_rejected (SetParameter vs "#bad argument"), both_flint_failed (stream offset flush
|
||||
failure, README "Encoder Operation Mapping"), c_state_corrupted (divergence 2), uninit_tail
|
||||
(divergence 4).
|
||||
2. fuzz/c/std/brotli_fuzzer.c: libFuzzer/FUZZLIB_MAIN target; now also exercises LW, dicts and an
|
||||
encoder round trip; 128 MiB workbuf. Seeds: fuzz/c/std/seed_corpora.txt (brotli line).
|
||||
3. Replay helpers (scratch only): SC/rep/encrep SRC TXT (re-runs a saved encoder failure:
|
||||
SRC = saved .br input, TXT = its description), SC/rep/apirep.c, SC/declog (libbrotlidec built
|
||||
with BROTLI_ENABLE_LOG + d/d2 drivers), SC/dbg2 and SC/dbg3 (instrumented C encoder and Wuffs
|
||||
snapshot copies that print commands, bit positions, tree sizes and workbuf layout).
|
||||
|
||||
## Seeding bug (affects all shards before a856d13)
|
||||
|
||||
Seeding SplitMix64 with seed * gamma made seed N+1 replay seed N's sequence shifted by one
|
||||
number, so shards with consecutive seeds repeated each other's cases (encoder shards 222 and 223
|
||||
hit the same config). Fixed in a856d13 (seed hashed). All counts below are from shards run after
|
||||
the fix; the earlier shards (decoder 101-109, encoder 201-208/221-223/301-302, API 401-403,
|
||||
history in SC/HANDOFF-old.md and SC/logs-oldseed/) are NOT counted as distinct inputs. They
|
||||
found divergences 2 and 3, which stand.
|
||||
|
||||
## Campaign counts (seeds hashed; 20-minute shards; "pre-merge" = before 73de2fe, the merge
|
||||
## of 24807c6 speed work; "post-merge" = 73de2fe + later)
|
||||
|
||||
Campaign 1, decoder (each input decoded 4 ways: Wuffs and C, one-shot and chunked):
|
||||
pre-merge 1101 685,184 / 1102 724,096 / 1103 735,936 / 1104 722,560 = 2,867,776 inputs,
|
||||
0 failures, 6 known BLOCK_LENGTH.
|
||||
post-merge 1105 767,488 / 1106 750,528 / 1107 781,632 / 1108 806,976 = 3,106,624 inputs,
|
||||
0 failures, 9 known BLOCK_LENGTH.
|
||||
total 5,974,400 mutated inputs (target 5M: met; post-merge target 2M: met).
|
||||
Campaign 2, encoder:
|
||||
pre-merge 1201 (q0-9) 87,344, 1221 (q0-4) 96,192, 1202 (q0-9) 99,392, 1222 (q0-4) 95,936
|
||||
= 378,864 configs (q0-4 303,081; q5-9 75,783; q10-11 0), 0 failures,
|
||||
348,531 round trips, 94,910 with dicts, 75,212 LW, 30,331 stream offset,
|
||||
75 flint both-fail, 2 c_state_corrupted. Shard 1201 ran before the dbd5a00 fix.
|
||||
post-merge 1301 (q10-11) 2,960 (308 with dicts, 3 skipped >3 GiB), 1203 (q0-9) 93,888,
|
||||
1223 (q0-4) 97,248, 1204 (q0-9) 101,072 = 295,168 configs (q0-4 212,493;
|
||||
q5-9 79,715; q10-11 2,960), 1 failure (divergence 4, now classified),
|
||||
271,641 round trips, 73,212 with dicts, 58,702 LW, 23,522 stream offset,
|
||||
49 flint both-fail, 1 c_state_corrupted.
|
||||
total 674,032 configs (target 1M: NOT met; ~4 more q0-9/q0-4 shards and more q10-11).
|
||||
Campaign 3, API: no post-seed-fix shard completed (1401 was stopped). Old-seed shards 401-403:
|
||||
682,258 checks; 401's 65 failures were a harness artifact (fixed 0e0674d), 402/403 clean.
|
||||
Remaining: run -api shards with hashed seeds (e.g. 1401, 1402).
|
||||
Campaign 4, fuzz/c/std/brotli_fuzzer.c (pre-merge code): gcc ASan/UBSan FUZZLIB_MAIN over 109
|
||||
seed files PASS; clang libFuzzer + ASan/UBSan 1 hour: 35,889 runs, 1,125 new units, peak RSS
|
||||
558 MB, 0 crashes (slow-unit artifacts only, slowest 21 s), plus an aborted ~5,170-run start.
|
||||
Not rerun post-merge.
|
||||
|
||||
## Divergences
|
||||
|
||||
1. Decoder, chunked input only: C BLOCK_LENGTH_1 (-9) where Wuffs reports "#bad metablock
|
||||
length" (-10). Root cause: libbrotlidec's internal input buffer after a rolled-back
|
||||
command/distance/block switch read makes extra NEEDS_MORE_INPUT returns and length checks.
|
||||
Not a Wuffs bug; documented (README "Status Mapping"), test
|
||||
test_mimic_brotli_decode_split_block_length (2241af9). Saved cases SC/out/fail-known-*.
|
||||
2. Encoder q0/q1, metadata with no dst room then a flush: libbrotlienc bug (stale next_out_
|
||||
= tiny_buf_+16 overwrites remaining_metadata_bytes_; later calls fail). Wuffs not changed;
|
||||
documented, test test_mimic_brotli_encode_metadata_stale_pointer, harness class
|
||||
c_state_corrupted (8a8df66). Repros: SC/rep/stale.c, SC/out/fail-enc-980061935075926891-1.br.
|
||||
3. Encoder q2 (BrotliStoreMetaBlockFast, >128 commands): REAL WUFFS BUG, invalid stream.
|
||||
build_and_store_huffman_tree_fast_generic indexed the 704-entry ZERO_REPS/NON_ZERO_REPS
|
||||
tables with reps & 511. Fixed dbd5a00 (reps.min(no_more_than: 703)); tests
|
||||
test_wuffs_brotli_encode_long_zero_run and test_mimic_brotli_encode_long_zero_run (both fail
|
||||
before the fix). Repro: SC/out/fail-enc-17396939997611138402-1.br, minimized SC/rep/min.bin.
|
||||
4. Encoder q3 lgwin 13, multi-step plan (post-merge shard 1204): outputs differ, both decode.
|
||||
Both encoders read the uninitialized ring buffer tail after a short first write (C: malloc'd
|
||||
memory in RingBufferWrite's lazy allocation; Wuffs: workbuf bytes). With equal memory
|
||||
contents (zeroed, or MALLOC_PERTURB_=84 vs a 0xAB-filled workbuf) the outputs are equal; the
|
||||
same happens with pre-merge code. Not a Wuffs mirroring bug; documented in README, harness
|
||||
class uninit_tail (8dcecc6, reruns with zeroed memory and checks Wuffs' output decodes).
|
||||
Repro: SC/out/fail-enc-13322404997902801073-1.br (+.txt); FILLWB=255 SC/rep/encrep SRC TXT.
|
||||
5. Harness-only: API mode zero-length dst calls with a stream offset (fixed 0e0674d); stream
|
||||
offset flush failure accepted when both fail identically (0c23849, README note 6fb1f22).
|
||||
|
||||
## Remaining
|
||||
|
||||
- Encoder campaign to >= 1M configs (674,032 now): more q0-9/q0-4 shards with hashed seeds
|
||||
(e.g. 1205, 1224, 1206, 1225), plus q10-11 shards (1302+), on the merged code.
|
||||
- API campaign with hashed seeds (1401, 1402).
|
||||
- Optionally rerun brotli_fuzzer (libFuzzer) on the merged code.
|
||||
- `wuffs test -mimic std/brotli` on gcc and clang (full, ~30 min each) was NOT run after the
|
||||
merges and fixes; only the new focused tests were run (gcc, PASS). `wuffs gen` and
|
||||
`wuffsfmt -l std/brotli` are clean at HEAD.
|
||||
+4
-1
@@ -15,10 +15,12 @@ The LICENSE has changed from a single license (Apache 2) to a dual license
|
||||
- Added `base.rect_ie_i32`.
|
||||
- Added `compact_retaining` and `dst_history_retain_length`.
|
||||
- Added `example/toy-aux-image`.
|
||||
- Added `example/brcat`.
|
||||
- Added `example/mzcat`.
|
||||
- Added `get_quirk(key: u32) u64`.
|
||||
- Added `std/brotli`.
|
||||
- Added a `std/brotli` encoder (qualities 0 and 1).
|
||||
- Added a `std/brotli` encoder (qualities 0 to 11, byte-exact with libbrotlienc
|
||||
v1.2.1).
|
||||
- Added `std/crc64`.
|
||||
- Added `std/etc2`.
|
||||
- Added `std/handsum`.
|
||||
@@ -28,6 +30,7 @@ The LICENSE has changed from a single license (Apache 2) to a dual license
|
||||
- Added `std/netpbm`.
|
||||
- Added `std/qoi`.
|
||||
- Added `std/sha256`.
|
||||
- Added `std/softfloat`.
|
||||
- Added `std/thumbhash`.
|
||||
- Added `std/vp8`.
|
||||
- Added `std/webp`.
|
||||
|
||||
@@ -0,0 +1,357 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, 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
|
||||
|
||||
// ----------------
|
||||
|
||||
/*
|
||||
brcat decodes (or, with -encode, encodes) brotli from stdin to stdout. It is
|
||||
similar to "brotli -dc" (or "brotli -c"), with fewer options.
|
||||
|
||||
Flags:
|
||||
-encode encode instead of decode.
|
||||
-large-window use large-window brotli (RFC 7932 does not cover it).
|
||||
-lgwin=N the encoder's window size, 10 to 24 (or 30 with
|
||||
-large-window). The default is 22.
|
||||
-quality=N the encoder's quality, 0 to 11. The default is 11.
|
||||
-fail-if-unsandboxed exit with an error if the seccomp sandbox failed.
|
||||
|
||||
The encoder's output is byte-for-byte identical, on every platform, to what
|
||||
the brotli reference implementation's library (libbrotlienc v1.2.1) produces
|
||||
for the same parameters and the same sequence of calls. That output depends
|
||||
on how the input is split into calls (for example, the first call's length is
|
||||
the default size hint), so it can differ from the "brotli" command line tool's,
|
||||
which reads 512 KiB at a time and sets a size hint. Like that reference
|
||||
implementation, qualities 10 and 11 are much slower than lower qualities.
|
||||
|
||||
To run:
|
||||
|
||||
$CC brcat.c && ./a.out -encode < ../../test/data/romeo.txt | ./a.out; rm -f a.out
|
||||
|
||||
for a C compiler $CC, such as clang or gcc.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// Wuffs ships as a "single file C library" or "header file library" as per
|
||||
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
|
||||
// To use that single file as a "foo.c"-like implementation, instead of a
|
||||
// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
|
||||
// compiling it.
|
||||
#define WUFFS_IMPLEMENTATION
|
||||
|
||||
// Defining the WUFFS_CONFIG__STATIC_FUNCTIONS macro is optional, but when
|
||||
// combined with WUFFS_IMPLEMENTATION, it demonstrates making all of Wuffs'
|
||||
// functions have static storage.
|
||||
#define WUFFS_CONFIG__STATIC_FUNCTIONS
|
||||
|
||||
// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
|
||||
// release/c/etc.c choose which parts of Wuffs to build. std/brotli's encoder
|
||||
// uses std/softfloat.
|
||||
#define WUFFS_CONFIG__MODULES
|
||||
#define WUFFS_CONFIG__MODULE__BASE
|
||||
#define WUFFS_CONFIG__MODULE__BROTLI
|
||||
#define WUFFS_CONFIG__MODULE__SOFTFLOAT
|
||||
|
||||
// If building this program in an environment that doesn't easily accommodate
|
||||
// relative includes, you can use the script/inline-c-relative-includes.go
|
||||
// program to generate a stand-alone C file.
|
||||
#include "../../release/c/wuffs-unsupported-snapshot.c"
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <linux/seccomp.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/syscall.h>
|
||||
#define WUFFS_EXAMPLE_USE_SECCOMP
|
||||
#endif
|
||||
|
||||
#ifndef DST_BUFFER_ARRAY_SIZE
|
||||
#define DST_BUFFER_ARRAY_SIZE (1024 * 1024)
|
||||
#endif
|
||||
|
||||
#ifndef SRC_BUFFER_ARRAY_SIZE
|
||||
#define SRC_BUFFER_ARRAY_SIZE (1024 * 1024)
|
||||
#endif
|
||||
|
||||
uint8_t g_dst_buffer_array[DST_BUFFER_ARRAY_SIZE];
|
||||
uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE];
|
||||
|
||||
// The workbuf is allocated (with malloc) before entering the sandbox, as its
|
||||
// length depends on the flags.
|
||||
wuffs_base__slice_u8 g_workbuf = {0};
|
||||
|
||||
static bool g_sandboxed = false;
|
||||
|
||||
wuffs_base__io_transformer* g_io_transformer = NULL;
|
||||
union {
|
||||
wuffs_brotli__decoder dec;
|
||||
wuffs_brotli__encoder enc;
|
||||
} g_codec;
|
||||
|
||||
// ----
|
||||
|
||||
struct {
|
||||
int remaining_argc;
|
||||
char** remaining_argv;
|
||||
|
||||
bool encode;
|
||||
bool fail_if_unsandboxed;
|
||||
bool large_window;
|
||||
uint32_t lgwin;
|
||||
uint32_t quality;
|
||||
} g_flags = {0};
|
||||
|
||||
const char* //
|
||||
parse_flags(int argc, char** argv) {
|
||||
g_flags.lgwin = 22;
|
||||
g_flags.quality = 11;
|
||||
|
||||
int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
|
||||
for (; c < argc; c++) {
|
||||
char* arg = argv[c];
|
||||
if (*arg++ != '-') {
|
||||
break;
|
||||
}
|
||||
|
||||
// A double-dash "--foo" is equivalent to a single-dash "-foo". As special
|
||||
// cases, a bare "-" is not a flag (some programs may interpret it as
|
||||
// stdin) and a bare "--" means to stop parsing flags.
|
||||
if (*arg == '\x00') {
|
||||
break;
|
||||
} else if (*arg == '-') {
|
||||
arg++;
|
||||
if (*arg == '\x00') {
|
||||
c++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp(arg, "encode")) {
|
||||
g_flags.encode = true;
|
||||
continue;
|
||||
} else if (!strcmp(arg, "fail-if-unsandboxed")) {
|
||||
g_flags.fail_if_unsandboxed = true;
|
||||
continue;
|
||||
} else if (!strcmp(arg, "large-window")) {
|
||||
g_flags.large_window = true;
|
||||
continue;
|
||||
} else if (!strncmp(arg, "lgwin=", 6)) {
|
||||
char* end = NULL;
|
||||
long n = strtol(arg + 6, &end, 10);
|
||||
if ((end == arg + 6) || (*end != '\x00') || (n < 10) || (n > 30)) {
|
||||
return "main: bad -lgwin flag value";
|
||||
}
|
||||
g_flags.lgwin = (uint32_t)n;
|
||||
continue;
|
||||
} else if (!strncmp(arg, "quality=", 8)) {
|
||||
char* end = NULL;
|
||||
long n = strtol(arg + 8, &end, 10);
|
||||
if ((end == arg + 8) || (*end != '\x00') || (n < 0) || (n > 11)) {
|
||||
return "main: bad -quality flag value";
|
||||
}
|
||||
g_flags.quality = (uint32_t)n;
|
||||
continue;
|
||||
}
|
||||
|
||||
return "main: unrecognized flag argument";
|
||||
}
|
||||
|
||||
if ((g_flags.lgwin > 24) && !g_flags.large_window) {
|
||||
return "main: -lgwin above 24 needs -large-window";
|
||||
}
|
||||
|
||||
g_flags.remaining_argc = argc - c;
|
||||
g_flags.remaining_argv = argv + c;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// ----
|
||||
|
||||
// ignore_return_value suppresses errors from -Wall -Werror.
|
||||
static void //
|
||||
ignore_return_value(int ignored) {}
|
||||
|
||||
const char* //
|
||||
initialize_io_transformer(void) {
|
||||
wuffs_base__status status;
|
||||
if (g_flags.encode) {
|
||||
status = wuffs_brotli__encoder__initialize(
|
||||
&g_codec.enc, sizeof g_codec.enc, WUFFS_VERSION,
|
||||
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED);
|
||||
if (!wuffs_base__status__is_ok(&status)) {
|
||||
return wuffs_base__status__message(&status);
|
||||
}
|
||||
g_io_transformer =
|
||||
wuffs_brotli__encoder__upcast_as__wuffs_base__io_transformer(
|
||||
&g_codec.enc);
|
||||
wuffs_base__io_transformer__set_quirk(
|
||||
g_io_transformer, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, g_flags.quality);
|
||||
wuffs_base__io_transformer__set_quirk(
|
||||
g_io_transformer, WUFFS_BROTLI__QUIRK_LGWIN, g_flags.lgwin);
|
||||
} else {
|
||||
status = wuffs_brotli__decoder__initialize(
|
||||
&g_codec.dec, sizeof g_codec.dec, WUFFS_VERSION,
|
||||
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED);
|
||||
if (!wuffs_base__status__is_ok(&status)) {
|
||||
return wuffs_base__status__message(&status);
|
||||
}
|
||||
g_io_transformer =
|
||||
wuffs_brotli__decoder__upcast_as__wuffs_base__io_transformer(
|
||||
&g_codec.dec);
|
||||
}
|
||||
if (g_flags.large_window) {
|
||||
wuffs_base__io_transformer__set_quirk(g_io_transformer,
|
||||
WUFFS_BROTLI__QUIRK_LARGE_WINDOW, 1);
|
||||
}
|
||||
|
||||
// The workbuf_len depends on the quirks. At qualities 10 and 11, the
|
||||
// encoder's maximum (the C code's worst case) can be far larger than what
|
||||
// real inputs need, so use the minimum (a practical bound) there. An input
|
||||
// that needs more than that fails with "#bad workbuf length".
|
||||
wuffs_base__range_ii_u64 wbl =
|
||||
wuffs_base__io_transformer__workbuf_len(g_io_transformer);
|
||||
uint64_t n = (g_flags.encode && (g_flags.quality >= 10)) ? wbl.min_incl
|
||||
: wbl.max_incl;
|
||||
if (n > SIZE_MAX) {
|
||||
return "main: workbuf is too large";
|
||||
} else if (n > 0) {
|
||||
g_workbuf.ptr = malloc((size_t)n);
|
||||
if (!g_workbuf.ptr) {
|
||||
return "main: out of memory";
|
||||
}
|
||||
g_workbuf.len = (size_t)n;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* //
|
||||
main1(void) {
|
||||
wuffs_base__io_buffer dst;
|
||||
dst.data.ptr = g_dst_buffer_array;
|
||||
dst.data.len = DST_BUFFER_ARRAY_SIZE;
|
||||
dst.meta.wi = 0;
|
||||
dst.meta.ri = 0;
|
||||
dst.meta.pos = 0;
|
||||
dst.meta.closed = false;
|
||||
|
||||
wuffs_base__io_buffer src;
|
||||
src.data.ptr = g_src_buffer_array;
|
||||
src.data.len = SRC_BUFFER_ARRAY_SIZE;
|
||||
src.meta.wi = 0;
|
||||
src.meta.ri = 0;
|
||||
src.meta.pos = 0;
|
||||
src.meta.closed = false;
|
||||
|
||||
while (true) {
|
||||
const int stdin_fd = 0;
|
||||
ssize_t n =
|
||||
read(stdin_fd, src.data.ptr + src.meta.wi, src.data.len - src.meta.wi);
|
||||
if (n < 0) {
|
||||
if (errno != EINTR) {
|
||||
return strerror(errno);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
src.meta.wi += n;
|
||||
if (n == 0) {
|
||||
src.meta.closed = true;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
wuffs_base__status status = wuffs_base__io_transformer__transform_io(
|
||||
g_io_transformer, &dst, &src, g_workbuf);
|
||||
|
||||
if (dst.meta.ri < dst.meta.wi) {
|
||||
// TODO: handle EINTR and other write errors; see "man 2 write".
|
||||
const int stdout_fd = 1;
|
||||
ignore_return_value(write(stdout_fd, g_dst_buffer_array + dst.meta.ri,
|
||||
dst.meta.wi - dst.meta.ri));
|
||||
dst.meta.ri = dst.meta.wi;
|
||||
// Neither the brotli decoder nor encoder re-reads dst (their
|
||||
// dst_history_retain_length is 0), as the decoder keeps its history
|
||||
// in the workbuf.
|
||||
wuffs_base__io_buffer__compact(&dst);
|
||||
}
|
||||
|
||||
if (status.repr == wuffs_base__suspension__short_read) {
|
||||
break;
|
||||
}
|
||||
if (status.repr == wuffs_base__suspension__short_write) {
|
||||
continue;
|
||||
}
|
||||
return wuffs_base__status__message(&status);
|
||||
}
|
||||
|
||||
wuffs_base__io_buffer__compact(&src);
|
||||
}
|
||||
}
|
||||
|
||||
int //
|
||||
compute_exit_code(const char* status_msg) {
|
||||
if (!status_msg) {
|
||||
return 0;
|
||||
}
|
||||
size_t n = strnlen(status_msg, 2047);
|
||||
if (n >= 2047) {
|
||||
status_msg = "main: internal error: error message is too long";
|
||||
n = strlen(status_msg);
|
||||
}
|
||||
const int stderr_fd = 2;
|
||||
ignore_return_value(write(stderr_fd, status_msg, n));
|
||||
ignore_return_value(write(stderr_fd, "\n", 1));
|
||||
// Return an exit code of 1 for regular (foreseen) errors, e.g. badly
|
||||
// formatted or unsupported input.
|
||||
//
|
||||
// Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
|
||||
// run-time checks found that an internal invariant did not hold.
|
||||
return strstr(status_msg, "internal error:") ? 2 : 1;
|
||||
}
|
||||
|
||||
int //
|
||||
main(int argc, char** argv) {
|
||||
const char* z = parse_flags(argc, argv);
|
||||
if (!z) {
|
||||
if (g_flags.remaining_argc > 0) {
|
||||
z = "main: bad argument: use \"program < input\", not \"program input\"";
|
||||
} else {
|
||||
z = initialize_io_transformer();
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(WUFFS_EXAMPLE_USE_SECCOMP)
|
||||
// Enter the sandbox after the (malloc) workbuf allocation.
|
||||
if (!z) {
|
||||
g_sandboxed = prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT) >= 0;
|
||||
if (!g_sandboxed) {
|
||||
const int stderr_fd = 2;
|
||||
const char* msg = "SECCOMP_MODE_STRICT sandbox failed.\n";
|
||||
ignore_return_value(write(stderr_fd, msg, strlen(msg)));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!z) {
|
||||
if (g_flags.fail_if_unsandboxed && !g_sandboxed) {
|
||||
z = "main: unsandboxed";
|
||||
} else {
|
||||
z = main1();
|
||||
}
|
||||
}
|
||||
int exit_code = compute_exit_code(z);
|
||||
|
||||
#if defined(WUFFS_EXAMPLE_USE_SECCOMP)
|
||||
// Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by
|
||||
// either calling _exit or returning from main. SECCOMP_MODE_STRICT allows
|
||||
// only SYS_exit.
|
||||
syscall(SYS_exit, exit_code);
|
||||
#endif
|
||||
return exit_code;
|
||||
}
|
||||
@@ -27,10 +27,11 @@ rm -f ./a.out
|
||||
|
||||
It should print "PASS", amongst other information, and exit(0).
|
||||
|
||||
As well as decoding its input (with or without the large-window quirk and
|
||||
dictionaries), it also encodes the input (with encoder parameters, chunking,
|
||||
flushes, metadata blocks and dst limits derived from the input's hash),
|
||||
decodes the result and checks that the round trip reproduces the input.
|
||||
As well as decoding its input (with or without the large-window quirk,
|
||||
metadata reporting and dictionaries), it also encodes the input (with encoder
|
||||
parameters, chunking, flushes, metadata blocks and dst limits derived from the
|
||||
input's hash), decodes the result and checks that the round trip reproduces
|
||||
the input.
|
||||
*/
|
||||
|
||||
#pragma clang diagnostic pop
|
||||
@@ -131,7 +132,10 @@ static const uint8_t g_serialized_dictionary[] = {
|
||||
// at qualities 10 and 11, which are slow.
|
||||
#define ENC_MAX_INPUT 65536
|
||||
#define ENC_MAX_INPUT_HQ 4096
|
||||
#define ENC_DST_ARRAY_SIZE (2 * ENC_MAX_INPUT + 65536)
|
||||
// With 1-byte chunks and frequent flushes, each input byte can become its own
|
||||
// metablock plus a padding block (about 4 bytes per input byte at qualities 0
|
||||
// and 1), so the dst buffer is sized well above that.
|
||||
#define ENC_DST_ARRAY_SIZE (8 * ENC_MAX_INPUT + 65536)
|
||||
|
||||
static wuffs_brotli__encoder g_encoder;
|
||||
static wuffs_brotli__decoder g_rt_decoder;
|
||||
@@ -387,6 +391,9 @@ fuzz(wuffs_base__io_buffer* src, uint64_t hash) {
|
||||
// Enable large-window brotli for half of the inputs.
|
||||
wuffs_brotli__decoder__set_quirk(&dec, WUFFS_BROTLI__QUIRK_LARGE_WINDOW,
|
||||
(hash >> 1) & 1);
|
||||
// Report metadata blocks for half of the inputs.
|
||||
wuffs_brotli__decoder__set_quirk(&dec, WUFFS_BROTLI__QUIRK_REPORT_METADATA,
|
||||
(hash >> 40) & 1);
|
||||
|
||||
wuffs_base__slice_u8 workbuf =
|
||||
wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE);
|
||||
@@ -456,7 +463,13 @@ fuzz(wuffs_base__io_buffer* src, uint64_t hash) {
|
||||
status = wuffs_brotli__decoder__transform_io(&dec, &limited_dst,
|
||||
&limited_src, workbuf);
|
||||
src->meta.ri = limited_src.meta.ri;
|
||||
if (status.repr == wuffs_base__suspension__short_read) {
|
||||
if (status.repr == wuffs_base__note__metadata_reported) {
|
||||
if (wuffs_brotli__decoder__metadata_length(&dec) > 0x1000000) {
|
||||
fprintf(stderr, "wuffs_brotli__decoder__metadata_length too large\n");
|
||||
intentional_segfault();
|
||||
}
|
||||
continue;
|
||||
} else if (status.repr == wuffs_base__suspension__short_read) {
|
||||
if (limited && (src->meta.ri < src->meta.wi)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
+1690
-374
@@ -9253,6 +9253,8 @@ extern const char wuffs_brotli__error__unsupported_serialized_encoder_dictionary
|
||||
|
||||
#define WUFFS_BROTLI__QUIRK_LARGE_WINDOW 716673024u
|
||||
|
||||
#define WUFFS_BROTLI__QUIRK_REPORT_METADATA 716673034u
|
||||
|
||||
#define WUFFS_BROTLI__ENCODER_DST_HISTORY_RETAIN_LENGTH_MAX_INCL_WORST_CASE 0u
|
||||
|
||||
#define WUFFS_BROTLI__QUIRK_ENCODER_QUALITY 716673025u
|
||||
@@ -9383,6 +9385,11 @@ wuffs_brotli__decoder__set_quirk(
|
||||
uint32_t a_key,
|
||||
uint64_t a_value);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__decoder__metadata_length(
|
||||
const wuffs_brotli__decoder* self);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__optional_u63
|
||||
wuffs_brotli__decoder__dst_history_retain_length(
|
||||
@@ -9612,6 +9619,10 @@ struct wuffs_brotli__decoder__struct {
|
||||
uint32_t f_cmd_state;
|
||||
bool f_quirk_large_window;
|
||||
bool f_large_window;
|
||||
bool f_quirk_report_metadata;
|
||||
uint32_t f_md_length;
|
||||
bool f_md_note_pending;
|
||||
bool f_md_copying;
|
||||
bool f_started;
|
||||
uint64_t f_dict_total;
|
||||
uint32_t f_num_chunks;
|
||||
@@ -9634,6 +9645,7 @@ struct wuffs_brotli__decoder__struct {
|
||||
|
||||
uint32_t p_transform_io;
|
||||
uint32_t p_do_transform_io;
|
||||
uint32_t p_report_metadata;
|
||||
uint32_t p_read_bits;
|
||||
uint32_t p_read_bits32;
|
||||
uint32_t p_read_symbol;
|
||||
@@ -9852,6 +9864,11 @@ struct wuffs_brotli__decoder__struct {
|
||||
return wuffs_brotli__decoder__set_quirk(this, a_key, a_value);
|
||||
}
|
||||
|
||||
inline uint64_t
|
||||
metadata_length() const {
|
||||
return wuffs_brotli__decoder__metadata_length(this);
|
||||
}
|
||||
|
||||
inline wuffs_base__optional_u63
|
||||
dst_history_retain_length() const {
|
||||
return wuffs_brotli__decoder__dst_history_retain_length(this);
|
||||
@@ -9895,10 +9912,14 @@ struct wuffs_brotli__bit_cost__struct {
|
||||
wuffs_base__vtable null_vtable;
|
||||
|
||||
uint64_t f_log2_valid[1024];
|
||||
uint32_t f_pc_call;
|
||||
uint32_t f_pc_valid[256];
|
||||
} private_impl;
|
||||
|
||||
struct {
|
||||
wuffs_softfloat__arith f_sf;
|
||||
uint64_t f_pc_term[256];
|
||||
uint8_t f_pc_depth[256];
|
||||
uint64_t f_log2_cache[65536];
|
||||
} private_data;
|
||||
|
||||
@@ -9946,6 +9967,7 @@ struct wuffs_brotli__metablock_builder__struct {
|
||||
wuffs_brotli__bit_cost f_bc;
|
||||
uint64_t f_cost[256];
|
||||
uint64_t f_insert_cost[35200];
|
||||
uint64_t f_capped_plus[35200];
|
||||
uint16_t f_new_id[256];
|
||||
uint8_t f_good_for_rle[704];
|
||||
} private_data;
|
||||
@@ -42949,47 +42971,32 @@ wuffs_softfloat__arith__f32_add_mags(
|
||||
const wuffs_softfloat__arith* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
uint32_t v_ea = 0;
|
||||
uint32_t v_eb = 0;
|
||||
uint32_t v_ma = 0;
|
||||
uint32_t v_mb = 0;
|
||||
uint32_t v_x = 0;
|
||||
uint32_t v_y = 0;
|
||||
uint32_t v_ex = 0;
|
||||
uint32_t v_d = 0;
|
||||
uint64_t v_s = 0;
|
||||
uint32_t v_z = 0;
|
||||
uint32_t v_rb = 0;
|
||||
|
||||
v_ea = ((a_a >> 23u) & 255u);
|
||||
v_eb = ((a_b >> 23u) & 255u);
|
||||
v_ma = (((a_a & 8388607u) | 8388608u) << 6u);
|
||||
v_mb = (((a_b & 8388607u) | 8388608u) << 6u);
|
||||
if (v_ea < v_eb) {
|
||||
v_d = (((uint32_t)(v_eb - v_ea)) & 255u);
|
||||
v_ea = v_eb;
|
||||
v_z = v_ma;
|
||||
v_ma = v_mb;
|
||||
v_mb = v_z;
|
||||
v_x = (a_a & 2147483647u);
|
||||
v_y = (a_b & 2147483647u);
|
||||
if (v_y > v_x) {
|
||||
v_x = (a_b & 2147483647u);
|
||||
v_y = (a_a & 2147483647u);
|
||||
}
|
||||
v_ex = ((v_x >> 23u) & 255u);
|
||||
v_d = ((uint32_t)(v_ex - ((v_y >> 23u) & 255u)));
|
||||
if (v_d >= 25u) {
|
||||
return ((a_a & 2147483648u) | v_x);
|
||||
}
|
||||
v_s = ((uint64_t)((((uint64_t)(((v_x & 8388607u) | 8388608u))) << 39u) + ((((uint64_t)(((v_y & 8388607u) | 8388608u))) << 39u) >> (v_d & 31u))));
|
||||
if (v_s >= 9223372036854775808u) {
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(549755813887u + ((v_s >> 40u) & 1u))))) >> 40u);
|
||||
v_z = ((uint32_t)(((uint32_t)(v_ex << 23u)) + ((uint32_t)((v_s & 33554431u)))));
|
||||
} else {
|
||||
v_d = (((uint32_t)(v_ea - v_eb)) & 255u);
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(274877906943u + ((v_s >> 39u) & 1u))))) >> 39u);
|
||||
v_z = ((uint32_t)(((uint32_t)(((uint32_t)(v_ex - 1u)) << 23u)) + ((uint32_t)((v_s & 33554431u)))));
|
||||
}
|
||||
if (v_d >= 31u) {
|
||||
v_mb = 1u;
|
||||
} else if (v_d > 0u) {
|
||||
if (((uint32_t)(v_mb << (32u - v_d))) != 0u) {
|
||||
v_mb = ((v_mb >> v_d) | 1u);
|
||||
} else {
|
||||
v_mb = (v_mb >> v_d);
|
||||
}
|
||||
}
|
||||
v_z = ((v_ma & 1073741823u) + (v_mb & 1073741823u));
|
||||
if (v_z >= 1073741824u) {
|
||||
v_ea += 1u;
|
||||
v_z = ((v_z >> 1u) | (v_z & 1u));
|
||||
}
|
||||
v_rb = (v_z & 63u);
|
||||
v_z = (((uint32_t)(v_z + 32u)) >> 6u);
|
||||
if (v_rb == 32u) {
|
||||
v_z &= 4294967294u;
|
||||
}
|
||||
v_z = ((uint32_t)(((uint32_t)(((uint32_t)(v_ea - 1u)) << 23u)) + v_z));
|
||||
if (v_z >= 2139095040u) {
|
||||
v_z = 2139095040u;
|
||||
}
|
||||
@@ -77146,6 +77153,14 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
|
||||
#define WUFFS_BROTLI__F64_1E99 6088095589093318446u
|
||||
|
||||
static const uint32_t
|
||||
WUFFS_BROTLI__LOG2_DEBRUIJN32[32] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
0u, 9u, 1u, 10u, 13u, 21u, 2u, 29u,
|
||||
11u, 14u, 16u, 18u, 22u, 25u, 3u, 30u,
|
||||
8u, 12u, 20u, 28u, 15u, 17u, 24u, 7u,
|
||||
19u, 27u, 23u, 6u, 26u, 5u, 4u, 31u,
|
||||
};
|
||||
|
||||
#define WUFFS_BROTLI__LITERAL_BLOCK_SWITCH_COST 4628602664527698330u
|
||||
|
||||
#define WUFFS_BROTLI__COMMAND_BLOCK_SWITCH_COST 4623789442425946112u
|
||||
@@ -77240,6 +77255,14 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
|
||||
#define WUFFS_BROTLI__TREE_NONE 65535u
|
||||
|
||||
static const uint32_t
|
||||
WUFFS_BROTLI__F32_OF_SMALL_INT[32] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
0u, 1065353216u, 1073741824u, 1077936128u, 1082130432u, 1084227584u, 1086324736u, 1088421888u,
|
||||
1090519040u, 1091567616u, 1092616192u, 1093664768u, 1094713344u, 1095761920u, 1096810496u, 1097859072u,
|
||||
1098907648u, 1099431936u, 1099956224u, 1100480512u, 1101004800u, 1101529088u, 1102053376u, 1102577664u,
|
||||
1103101952u, 1103626240u, 1104150528u, 1104674816u, 1105199104u, 1105723392u, 1106247680u, 1106771968u,
|
||||
};
|
||||
|
||||
#define WUFFS_BROTLI__FRAGMENT_MAX_DISTANCE 262128u
|
||||
|
||||
#define WUFFS_BROTLI__HASH_MUL32 506832829u
|
||||
@@ -77324,6 +77347,18 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
|
||||
#define WUFFS_BROTLI__HQ_SHADOW_MATCHES 256u
|
||||
|
||||
static const uint8_t
|
||||
WUFFS_BROTLI__CTZ64_DEBRUIJN[64] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
0u, 1u, 48u, 2u, 57u, 49u, 28u, 3u,
|
||||
61u, 58u, 50u, 42u, 38u, 29u, 17u, 4u,
|
||||
62u, 55u, 59u, 36u, 53u, 51u, 43u, 22u,
|
||||
45u, 39u, 33u, 30u, 24u, 18u, 12u, 5u,
|
||||
63u, 47u, 56u, 27u, 60u, 41u, 37u, 16u,
|
||||
54u, 35u, 52u, 21u, 44u, 32u, 23u, 11u,
|
||||
46u, 26u, 40u, 15u, 34u, 20u, 31u, 10u,
|
||||
25u, 14u, 19u, 9u, 13u, 8u, 7u, 6u,
|
||||
};
|
||||
|
||||
#define WUFFS_BROTLI__Z_INFINITY 2130692510u
|
||||
|
||||
#define WUFFS_BROTLI__Z_F32_1 1065353216u
|
||||
@@ -77384,10 +77419,18 @@ WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_brotli__decoder__do_transform_io(
|
||||
wuffs_brotli__decoder* self,
|
||||
wuffs_base__io_buffer* a_dst,
|
||||
wuffs_base__io_buffer* a_src,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
wuffs_base__slice_u8 a_dictbuf);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_brotli__decoder__report_metadata(
|
||||
wuffs_brotli__decoder* self,
|
||||
wuffs_base__io_buffer* a_dst,
|
||||
wuffs_base__io_buffer* a_src);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__decoder__initialize_codes(
|
||||
@@ -77736,6 +77779,14 @@ wuffs_brotli__decoder__take_distance_from_ring_buffer(
|
||||
wuffs_brotli__decoder* self,
|
||||
uint32_t a_code);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__decoder__copy16(
|
||||
wuffs_brotli__decoder* self,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
uint64_t a_d,
|
||||
uint64_t a_s);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__encoder__cmd_insert_len(
|
||||
@@ -78986,6 +79037,195 @@ wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(
|
||||
uint32_t a_src,
|
||||
uint32_t a_n);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__clz64(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_add(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_sub(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_add_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_sub_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_key(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_lt(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_lt_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_le(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_le_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_gt(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_gt_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_add(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_sub(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_add_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_sub_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_key(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_lt(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_lt_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_gt(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_gt_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_ge(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_ge_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_mul(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_from_u64(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_from_u32(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_to_u64_trunc(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_from_u32(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__encoder__hash_fast(
|
||||
@@ -79440,6 +79680,20 @@ wuffs_brotli__encoder__hasher_store(
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_ix);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_store_h58(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_ix);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__encoder__hash_h58(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_ix);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_store_range(
|
||||
@@ -79543,17 +79797,6 @@ wuffs_brotli__encoder__find_last_distances(
|
||||
uint64_t a_max_length,
|
||||
uint64_t a_max_backward);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__try_bucket_entry(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_cur_ix,
|
||||
uint64_t a_prev_ix_in,
|
||||
uint64_t a_best_len,
|
||||
uint64_t a_max_length,
|
||||
uint64_t a_max_backward);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__find_longest_match_h5(
|
||||
@@ -80335,6 +80578,12 @@ wuffs_brotli__encoder__hq_extend_var_region(
|
||||
wuffs_brotli__encoder* self,
|
||||
uint64_t a_wb_len);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__ctz64(
|
||||
const wuffs_brotli__encoder* self,
|
||||
uint64_t a_x);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint8_t
|
||||
wuffs_brotli__encoder__byte_at(
|
||||
@@ -81135,6 +81384,8 @@ wuffs_brotli__decoder__get_quirk(
|
||||
|
||||
if ((a_key == 716673024u) && self->private_impl.f_quirk_large_window) {
|
||||
return 1u;
|
||||
} else if ((a_key == 716673034u) && self->private_impl.f_quirk_report_metadata) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
@@ -81163,10 +81414,33 @@ wuffs_brotli__decoder__set_quirk(
|
||||
}
|
||||
self->private_impl.f_quirk_large_window = (a_value > 0u);
|
||||
return wuffs_base__make_status(NULL);
|
||||
} else if (a_key == 716673034u) {
|
||||
if (self->private_impl.f_started) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
|
||||
}
|
||||
self->private_impl.f_quirk_report_metadata = (a_value > 0u);
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
|
||||
}
|
||||
|
||||
// -------- func brotli.decoder.metadata_length
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__decoder__metadata_length(
|
||||
const wuffs_brotli__decoder* self) {
|
||||
if (!self) {
|
||||
return 0;
|
||||
}
|
||||
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
|
||||
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ((uint64_t)(self->private_impl.f_md_length));
|
||||
}
|
||||
|
||||
// -------- func brotli.decoder.dst_history_retain_length
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -81249,7 +81523,11 @@ wuffs_brotli__decoder__transform_io(
|
||||
goto exit;
|
||||
}
|
||||
{
|
||||
wuffs_base__status t_0 = wuffs_brotli__decoder__do_transform_io(self, a_src, wuffs_base__slice_u8__subslice_i(a_workbuf, self->private_impl.f_dict_total), wuffs_base__slice_u8__subslice_j(a_workbuf, self->private_impl.f_dict_total));
|
||||
wuffs_base__status t_0 = wuffs_brotli__decoder__do_transform_io(self,
|
||||
a_dst,
|
||||
a_src,
|
||||
wuffs_base__slice_u8__subslice_i(a_workbuf, self->private_impl.f_dict_total),
|
||||
wuffs_base__slice_u8__subslice_j(a_workbuf, self->private_impl.f_dict_total));
|
||||
v_status = t_0;
|
||||
}
|
||||
if (self->private_impl.f_dict_total > ((uint64_t)(a_workbuf.len))) {
|
||||
@@ -81270,7 +81548,11 @@ wuffs_brotli__decoder__transform_io(
|
||||
status = wuffs_base__make_status(NULL);
|
||||
goto ok;
|
||||
} else if (v_status.repr == wuffs_base__suspension__short_write) {
|
||||
if (wuffs_brotli__decoder__is_fully_flushed(self)) {
|
||||
if (self->private_impl.f_md_note_pending) {
|
||||
self->private_impl.f_md_note_pending = false;
|
||||
status = wuffs_base__make_status(wuffs_base__note__metadata_reported);
|
||||
goto ok;
|
||||
} else if (wuffs_brotli__decoder__is_fully_flushed(self) && ! self->private_impl.f_md_copying) {
|
||||
continue;
|
||||
}
|
||||
} else if (v_status.repr == wuffs_base__suspension__short_read) {
|
||||
@@ -81367,6 +81649,7 @@ WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_brotli__decoder__do_transform_io(
|
||||
wuffs_brotli__decoder* self,
|
||||
wuffs_base__io_buffer* a_dst,
|
||||
wuffs_base__io_buffer* a_src,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
wuffs_base__slice_u8 a_dictbuf) {
|
||||
@@ -81435,15 +81718,29 @@ wuffs_brotli__decoder__do_transform_io(
|
||||
}
|
||||
}
|
||||
if (self->private_impl.f_is_metadata) {
|
||||
self->private_data.s_do_transform_io.scratch = ((uint64_t)(self->private_impl.f_mb_remaining));
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
if (self->private_data.s_do_transform_io.scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
|
||||
self->private_data.s_do_transform_io.scratch -= ((uint64_t)(io2_a_src - iop_a_src));
|
||||
iop_a_src = io2_a_src;
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
|
||||
goto suspend;
|
||||
if (self->private_impl.f_quirk_report_metadata) {
|
||||
if (a_src) {
|
||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
|
||||
status = wuffs_brotli__decoder__report_metadata(self, a_dst, a_src);
|
||||
if (a_src) {
|
||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
||||
}
|
||||
if (status.repr) {
|
||||
goto suspend;
|
||||
}
|
||||
} else {
|
||||
self->private_data.s_do_transform_io.scratch = ((uint64_t)(self->private_impl.f_mb_remaining));
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
if (self->private_data.s_do_transform_io.scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
|
||||
self->private_data.s_do_transform_io.scratch -= ((uint64_t)(io2_a_src - iop_a_src));
|
||||
iop_a_src = io2_a_src;
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
|
||||
goto suspend;
|
||||
}
|
||||
iop_a_src += self->private_data.s_do_transform_io.scratch;
|
||||
}
|
||||
iop_a_src += self->private_data.s_do_transform_io.scratch;
|
||||
self->private_impl.f_mb_remaining = 0u;
|
||||
} else if (self->private_impl.f_mb_remaining > 0u) {
|
||||
wuffs_brotli__decoder__calculate_ring_buffer_size(self);
|
||||
@@ -81451,7 +81748,7 @@ wuffs_brotli__decoder__do_transform_io(
|
||||
if (a_src) {
|
||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
|
||||
status = wuffs_brotli__decoder__copy_uncompressed(self, a_src, a_workbuf);
|
||||
if (a_src) {
|
||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
||||
@@ -81463,7 +81760,7 @@ wuffs_brotli__decoder__do_transform_io(
|
||||
if (a_src) {
|
||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
||||
}
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
|
||||
status = wuffs_brotli__decoder__decode_compressed_metablock(self, a_src, a_workbuf, a_dictbuf);
|
||||
if (a_src) {
|
||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
||||
@@ -81507,6 +81804,92 @@ wuffs_brotli__decoder__do_transform_io(
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func brotli.decoder.report_metadata
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_brotli__decoder__report_metadata(
|
||||
wuffs_brotli__decoder* self,
|
||||
wuffs_base__io_buffer* a_dst,
|
||||
wuffs_base__io_buffer* a_src) {
|
||||
wuffs_base__status status = wuffs_base__make_status(NULL);
|
||||
|
||||
uint32_t v_n = 0;
|
||||
|
||||
uint8_t* iop_a_dst = NULL;
|
||||
uint8_t* io0_a_dst WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
uint8_t* io1_a_dst WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
uint8_t* io2_a_dst WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
if (a_dst && a_dst->data.ptr) {
|
||||
io0_a_dst = a_dst->data.ptr;
|
||||
io1_a_dst = io0_a_dst + a_dst->meta.wi;
|
||||
iop_a_dst = io1_a_dst;
|
||||
io2_a_dst = io0_a_dst + a_dst->data.len;
|
||||
if (a_dst->meta.closed) {
|
||||
io2_a_dst = iop_a_dst;
|
||||
}
|
||||
}
|
||||
const uint8_t* iop_a_src = NULL;
|
||||
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
if (a_src && a_src->data.ptr) {
|
||||
io0_a_src = a_src->data.ptr;
|
||||
io1_a_src = io0_a_src + a_src->meta.ri;
|
||||
iop_a_src = io1_a_src;
|
||||
io2_a_src = io0_a_src + a_src->meta.wi;
|
||||
}
|
||||
|
||||
uint32_t coro_susp_point = self->private_impl.p_report_metadata;
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
while ( ! wuffs_brotli__decoder__is_fully_flushed(self)) {
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_write);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
|
||||
}
|
||||
self->private_impl.f_md_length = wuffs_base__u32__min(self->private_impl.f_mb_remaining, 16777216u);
|
||||
self->private_impl.f_md_note_pending = true;
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_write);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2);
|
||||
self->private_impl.f_md_copying = true;
|
||||
while (self->private_impl.f_mb_remaining > 0u) {
|
||||
if (((uint64_t)(io2_a_dst - iop_a_dst)) <= 0u) {
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_write);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3);
|
||||
continue;
|
||||
} else if (((uint64_t)(io2_a_src - iop_a_src)) <= 0u) {
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(4);
|
||||
continue;
|
||||
}
|
||||
v_n = wuffs_private_impl__io_writer__limited_copy_u32_from_reader(
|
||||
&iop_a_dst, io2_a_dst,((uint32_t)(self->private_impl.f_mb_remaining)), &iop_a_src, io2_a_src);
|
||||
self->private_impl.f_mb_remaining -= v_n;
|
||||
}
|
||||
self->private_impl.f_md_copying = false;
|
||||
|
||||
ok:
|
||||
self->private_impl.p_report_metadata = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.p_report_metadata = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
|
||||
|
||||
goto exit;
|
||||
exit:
|
||||
if (a_dst && a_dst->data.ptr) {
|
||||
a_dst->meta.wi = ((size_t)(iop_a_dst - a_dst->data.ptr));
|
||||
}
|
||||
if (a_src && a_src->data.ptr) {
|
||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func brotli.decoder.initialize_codes
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -85188,6 +85571,13 @@ wuffs_brotli__decoder__process_commands_fast(
|
||||
uint64_t v_dhi = 0;
|
||||
uint64_t v_slo = 0;
|
||||
uint64_t v_shi = 0;
|
||||
uint32_t v_cm_slice = 0;
|
||||
uint32_t v_wmask = 0;
|
||||
uint32_t v_pos = 0;
|
||||
uint32_t v_lit_n = 0;
|
||||
uint32_t v_lit_i = 0;
|
||||
uint64_t v_wi = 0;
|
||||
bool v_copied16 = false;
|
||||
|
||||
const uint8_t* iop_a_src = NULL;
|
||||
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
@@ -85284,25 +85674,32 @@ wuffs_brotli__decoder__process_commands_fast(
|
||||
}
|
||||
v_p1 = ((uint32_t)(wuffs_brotli__decoder__get_byte(self, a_workbuf, 1u)));
|
||||
v_p2 = ((uint32_t)(wuffs_brotli__decoder__get_byte(self, a_workbuf, 2u)));
|
||||
while (self->private_impl.f_insert_length > 0u) {
|
||||
if ((self->private_impl.f_block_length[0u] == 0u) || (((uint32_t)(self->private_impl.f_pos + 1u)) >= self->private_impl.f_rb_size)) {
|
||||
goto label__outer__break;
|
||||
}
|
||||
v_mode_offset = (self->private_impl.f_context_mode << 9u);
|
||||
v_cm_slice = self->private_impl.f_context_map_slice;
|
||||
v_wmask = ((uint32_t)(self->private_impl.f_window_size - 1u));
|
||||
v_pos = self->private_impl.f_pos;
|
||||
v_lit_n = wuffs_base__u32__min(self->private_impl.f_insert_length, self->private_impl.f_block_length[0u]);
|
||||
if (((uint32_t)(v_pos + 1u)) >= self->private_impl.f_rb_size) {
|
||||
v_lit_n = 0u;
|
||||
} else {
|
||||
v_lit_n = wuffs_base__u32__min(v_lit_n, ((uint32_t)(((uint32_t)(self->private_impl.f_rb_size - v_pos)) - 1u)));
|
||||
}
|
||||
v_lit_i = 0u;
|
||||
while (v_lit_i < v_lit_n) {
|
||||
if (v_n_bits < 24u) {
|
||||
if (((uint64_t)(io2_a_src - iop_a_src)) < 8u) {
|
||||
goto label__outer__break;
|
||||
break;
|
||||
}
|
||||
v_bits |= ((uint64_t)(wuffs_base__peek_u64le__no_bounds_check(iop_a_src) << v_n_bits));
|
||||
iop_a_src += ((63u - v_n_bits) >> 3u);
|
||||
v_n_bits |= 56u;
|
||||
}
|
||||
v_mode_offset = (self->private_impl.f_context_mode << 9u);
|
||||
v_ctx = ((uint32_t)(((uint8_t)(WUFFS_BROTLI__CONTEXT_LOOKUP[(v_mode_offset | v_p1)] | WUFFS_BROTLI__CONTEXT_LOOKUP[(v_mode_offset | 256u | v_p2)]))));
|
||||
v_tree = ((uint32_t)(self->private_data.f_context_maps[(self->private_impl.f_context_map_slice + v_ctx)]));
|
||||
v_tree = ((uint32_t)(self->private_data.f_context_maps[(v_cm_slice + v_ctx)]));
|
||||
v_i = ((uint32_t)((v_tree * 632u) + ((uint32_t)((v_bits & 255u)))));
|
||||
if (v_i >= 678034u) {
|
||||
v_ret = wuffs_base__make_status(wuffs_brotli__error__internal_error_inconsistent_huffman_table);
|
||||
goto label__outer__break;
|
||||
break;
|
||||
}
|
||||
v_e = ((uint32_t)(self->private_data.f_huffs[v_i]));
|
||||
v_e_bits = (v_e & 15u);
|
||||
@@ -85310,23 +85707,34 @@ wuffs_brotli__decoder__process_commands_fast(
|
||||
v_i = ((uint32_t)(((uint32_t)((v_tree * 632u) + (v_e >> 4u))) + (((uint32_t)(((v_bits >> 8u) & 32767u))) & ((((uint32_t)(1u)) << (v_e_bits - 8u)) - 1u))));
|
||||
if (v_i >= 678034u) {
|
||||
v_ret = wuffs_base__make_status(wuffs_brotli__error__internal_error_inconsistent_huffman_table);
|
||||
goto label__outer__break;
|
||||
break;
|
||||
}
|
||||
v_e = ((uint32_t)(self->private_data.f_huffs[v_i]));
|
||||
v_e_bits = ((v_e & 15u) + 8u);
|
||||
}
|
||||
if (v_n_bits < v_e_bits) {
|
||||
v_ret = wuffs_base__make_status(wuffs_brotli__error__internal_error_inconsistent_bit_reader);
|
||||
goto label__outer__break;
|
||||
break;
|
||||
}
|
||||
v_bits >>= v_e_bits;
|
||||
v_n_bits -= v_e_bits;
|
||||
self->private_impl.f_symbol = (v_e >> 4u);
|
||||
wuffs_brotli__decoder__put_byte(self, a_workbuf, ((uint8_t)(self->private_impl.f_symbol)));
|
||||
v_p2 = v_p1;
|
||||
v_p1 = (self->private_impl.f_symbol & 255u);
|
||||
self->private_impl.f_block_length[0u] -= 1u;
|
||||
self->private_impl.f_insert_length -= 1u;
|
||||
v_p1 = ((v_e >> 4u) & 255u);
|
||||
v_wi = ((uint64_t)((v_pos & v_wmask)));
|
||||
if (v_wi < ((uint64_t)(a_workbuf.len))) {
|
||||
a_workbuf.ptr[v_wi] = ((uint8_t)(v_p1));
|
||||
}
|
||||
v_pos += 1u;
|
||||
v_lit_i += 1u;
|
||||
}
|
||||
if (v_lit_i > 0u) {
|
||||
self->private_impl.f_symbol = v_p1;
|
||||
self->private_impl.f_pos = v_pos;
|
||||
self->private_impl.f_block_length[0u] -= v_lit_i;
|
||||
self->private_impl.f_insert_length -= v_lit_i;
|
||||
}
|
||||
if (wuffs_base__status__is_error(&v_ret) || (self->private_impl.f_insert_length > 0u)) {
|
||||
break;
|
||||
}
|
||||
self->private_impl.f_cmd_state = 2u;
|
||||
if ((self->private_impl.f_mb_remaining == 0u) || (self->private_impl.f_mb_remaining >= 2147483648u)) {
|
||||
@@ -85412,6 +85820,15 @@ wuffs_brotli__decoder__process_commands_fast(
|
||||
self->private_impl.f_dist_rb_idx += 1u;
|
||||
self->private_impl.f_mb_remaining -= v_n;
|
||||
v_k64 = 0u;
|
||||
if ((v_n <= 16u) &&
|
||||
(v_distance >= 16u) &&
|
||||
(((uint64_t)(v_dst_i + 16u)) <= ((uint64_t)(self->private_impl.f_rb_size))) &&
|
||||
(((uint64_t)(v_src_i + 16u)) <= ((uint64_t)(self->private_impl.f_window_size)))) {
|
||||
v_copied16 = wuffs_brotli__decoder__copy16(self, a_workbuf, v_dst_i, v_src_i);
|
||||
if (v_copied16) {
|
||||
v_k64 = ((uint64_t)(v_n));
|
||||
}
|
||||
}
|
||||
while (v_k64 < ((uint64_t)(v_n))) {
|
||||
v_n64 = ((uint64_t)(((uint64_t)(v_n)) - v_k64));
|
||||
v_n64 = wuffs_base__u64__min(v_n64, ((uint64_t)(v_distance)));
|
||||
@@ -85441,7 +85858,6 @@ wuffs_brotli__decoder__process_commands_fast(
|
||||
break;
|
||||
}
|
||||
}
|
||||
label__outer__break:;
|
||||
while (v_n_bits >= 8u) {
|
||||
if ( ! (iop_a_src > io1_a_src)) {
|
||||
status = wuffs_base__make_status(wuffs_brotli__error__internal_error_inconsistent_bit_reader);
|
||||
@@ -85592,6 +86008,43 @@ wuffs_brotli__decoder__take_distance_from_ring_buffer(
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.decoder.copy16
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__decoder__copy16(
|
||||
wuffs_brotli__decoder* self,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
uint64_t a_d,
|
||||
uint64_t a_s) {
|
||||
uint64_t v_lo = 0;
|
||||
uint64_t v_hi = 0;
|
||||
wuffs_base__slice_u8 v_q = {0};
|
||||
wuffs_base__slice_u8 v_r = {0};
|
||||
|
||||
if ((a_s > ((uint64_t)(a_workbuf.len))) || (a_d > ((uint64_t)(a_workbuf.len)))) {
|
||||
return false;
|
||||
}
|
||||
v_q = wuffs_base__slice_u8__subslice_i(a_workbuf, a_s);
|
||||
if (((uint64_t)(v_q.len)) >= 16u) {
|
||||
v_lo = wuffs_base__peek_u64le__no_bounds_check(v_q.ptr);
|
||||
v_q = wuffs_base__slice_u8__subslice_i(v_q, 8u);
|
||||
if (((uint64_t)(v_q.len)) >= 8u) {
|
||||
v_hi = wuffs_base__peek_u64le__no_bounds_check(v_q.ptr);
|
||||
v_q = wuffs_base__slice_u8__subslice_i(a_workbuf, a_d);
|
||||
if (((uint64_t)(v_q.len)) >= 16u) {
|
||||
v_r = wuffs_base__slice_u8__subslice_i(v_q, 8u);
|
||||
wuffs_base__poke_u64le__no_bounds_check(v_q.ptr, v_lo);
|
||||
if (((uint64_t)(v_r.len)) >= 8u) {
|
||||
wuffs_base__poke_u64le__no_bounds_check(v_r.ptr, v_hi);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.cmd_insert_len
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -86231,15 +86684,18 @@ wuffs_brotli__bit_cost__bits_entropy(
|
||||
v_q = wuffs_base__slice_u8__subslice_i(v_q, 4u);
|
||||
}
|
||||
v_sum += v_p;
|
||||
if (v_p <= 1u) {
|
||||
continue;
|
||||
}
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, v_p);
|
||||
v_retval = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_p), v_lp));
|
||||
v_retval = wuffs_brotli__bit_cost__f64_sub(self, v_retval, wuffs_brotli__bit_cost__f64_mul(self, wuffs_brotli__bit_cost__f64_from_u64(self, v_p), v_lp));
|
||||
}
|
||||
if (v_sum != 0u) {
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, v_sum);
|
||||
v_retval = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum), v_lp));
|
||||
v_retval = wuffs_brotli__bit_cost__f64_add(self, v_retval, wuffs_brotli__bit_cost__f64_mul(self, wuffs_brotli__bit_cost__f64_from_u64(self, v_sum), v_lp));
|
||||
}
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum)) != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum);
|
||||
if (wuffs_brotli__bit_cost__f64_lt(self, v_retval, wuffs_brotli__bit_cost__f64_from_u64(self, v_sum)) != 0u) {
|
||||
v_retval = wuffs_brotli__bit_cost__f64_from_u64(self, v_sum);
|
||||
}
|
||||
return v_retval;
|
||||
}
|
||||
@@ -86276,6 +86732,7 @@ wuffs_brotli__bit_cost__population_cost(
|
||||
uint64_t v_sum = 0;
|
||||
uint64_t v_retval = 0;
|
||||
uint64_t v_p = 0;
|
||||
uint64_t v_term = 0;
|
||||
|
||||
v_total = wuffs_brotli__bit_cost__ld64(self, a_histogram, (((uint64_t)(a_data_size)) * 4u));
|
||||
if (v_total == 0u) {
|
||||
@@ -86298,14 +86755,14 @@ wuffs_brotli__bit_cost__population_cost(
|
||||
return 4622945017495814144u;
|
||||
}
|
||||
if (v_count == 2u) {
|
||||
return wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, 4626322717216342016u, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_total));
|
||||
return wuffs_brotli__bit_cost__f64_add(self, 4626322717216342016u, wuffs_brotli__bit_cost__f64_from_u64(self, v_total));
|
||||
}
|
||||
if (v_count == 3u) {
|
||||
v_h0 = wuffs_brotli__bit_cost__ld32(self, a_histogram, (((uint64_t)(v_s[0u])) * 4u));
|
||||
v_h1 = wuffs_brotli__bit_cost__ld32(self, a_histogram, (((uint64_t)(v_s[1u])) * 4u));
|
||||
v_h2 = wuffs_brotli__bit_cost__ld32(self, a_histogram, (((uint64_t)(v_s[2u])) * 4u));
|
||||
v_histomax = wuffs_base__u32__max(v_h0, wuffs_base__u32__max(v_h1, v_h2));
|
||||
return wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, 4628574517030027264u, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, ((uint32_t)(2u * ((uint32_t)(((uint32_t)(v_h0 + v_h1)) + v_h2)))))), wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, v_histomax));
|
||||
return wuffs_brotli__bit_cost__f64_sub(self, wuffs_brotli__bit_cost__f64_add(self, 4628574517030027264u, wuffs_brotli__bit_cost__f64_from_u32(self, ((uint32_t)(2u * ((uint32_t)(((uint32_t)(v_h0 + v_h1)) + v_h2)))))), wuffs_brotli__bit_cost__f64_from_u32(self, v_histomax));
|
||||
}
|
||||
if (v_count == 4u) {
|
||||
v_i = 0u;
|
||||
@@ -86328,18 +86785,37 @@ wuffs_brotli__bit_cost__population_cost(
|
||||
}
|
||||
v_h23 = ((uint32_t)(v_histo[2u] + v_histo[3u]));
|
||||
v_histomax = wuffs_base__u32__max(v_h23, v_histo[0u]);
|
||||
return wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, 4630404104378646528u, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, ((uint32_t)(3u * v_h23)))), wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, ((uint32_t)(2u * ((uint32_t)(v_histo[0u] + v_histo[1u])))))), wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, v_histomax));
|
||||
return wuffs_brotli__bit_cost__f64_sub(self, wuffs_brotli__bit_cost__f64_add(self, wuffs_brotli__bit_cost__f64_add(self, 4630404104378646528u, wuffs_brotli__bit_cost__f64_from_u32(self, ((uint32_t)(3u * v_h23)))), wuffs_brotli__bit_cost__f64_from_u32(self, ((uint32_t)(2u * ((uint32_t)(v_histo[0u] + v_histo[1u])))))), wuffs_brotli__bit_cost__f64_from_u32(self, v_histomax));
|
||||
}
|
||||
v_max_depth = 1u;
|
||||
v_log2total = wuffs_brotli__bit_cost__fast_log2_memo(self, v_total);
|
||||
self->private_impl.f_pc_call += 1u;
|
||||
if (self->private_impl.f_pc_call == 0u) {
|
||||
wuffs_private_impl__bulk_memset(&self->private_impl.f_pc_valid[0], 256u * (size_t)4u, 0u);
|
||||
self->private_impl.f_pc_call = 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < a_data_size) {
|
||||
v_c = wuffs_brotli__bit_cost__ld32(self, a_histogram, (((uint64_t)(v_i)) * 4u));
|
||||
if (v_c > 0u) {
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, ((uint64_t)(v_c)));
|
||||
v_log2p = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_log2total, v_lp);
|
||||
v_depth = wuffs_softfloat__arith__f64_to_u64_trunc(&self->private_data.f_sf, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_log2p, 4602678819172646912u));
|
||||
v_bits = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_bits, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, v_c), v_log2p));
|
||||
if ((v_c < 256u) && (self->private_impl.f_pc_valid[(v_c & 255u)] == self->private_impl.f_pc_call)) {
|
||||
v_term = self->private_data.f_pc_term[(v_c & 255u)];
|
||||
v_depth = ((uint64_t)(self->private_data.f_pc_depth[(v_c & 255u)]));
|
||||
} else {
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, ((uint64_t)(v_c)));
|
||||
v_log2p = wuffs_brotli__bit_cost__f64_sub(self, v_log2total, v_lp);
|
||||
v_depth = wuffs_brotli__bit_cost__f64_to_u64_trunc(self, wuffs_brotli__bit_cost__f64_add(self, v_log2p, 4602678819172646912u));
|
||||
v_term = wuffs_brotli__bit_cost__f64_mul(self, wuffs_brotli__bit_cost__f64_from_u32(self, v_c), v_log2p);
|
||||
if (v_depth > 15u) {
|
||||
v_depth = 15u;
|
||||
}
|
||||
if (v_c < 256u) {
|
||||
self->private_impl.f_pc_valid[(v_c & 255u)] = self->private_impl.f_pc_call;
|
||||
self->private_data.f_pc_term[(v_c & 255u)] = v_term;
|
||||
self->private_data.f_pc_depth[(v_c & 255u)] = ((uint8_t)((v_depth & 15u)));
|
||||
}
|
||||
}
|
||||
v_bits = wuffs_brotli__bit_cost__f64_add(self, v_bits, v_term);
|
||||
if (v_depth > 15u) {
|
||||
v_depth = 15u;
|
||||
}
|
||||
@@ -86368,29 +86844,32 @@ wuffs_brotli__bit_cost__population_cost(
|
||||
v_reps -= 2u;
|
||||
while (v_reps > 0u) {
|
||||
v_depth_histo[17u] += 1u;
|
||||
v_bits = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_bits, 4613937818241073152u);
|
||||
v_bits = wuffs_brotli__bit_cost__f64_add(self, v_bits, 4613937818241073152u);
|
||||
v_reps >>= 3u;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
v_bits = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_bits, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, (18u + (2u * v_max_depth))));
|
||||
v_bits = wuffs_brotli__bit_cost__f64_add(self, v_bits, wuffs_brotli__bit_cost__f64_from_u64(self, (18u + (2u * v_max_depth))));
|
||||
v_i = 0u;
|
||||
while (v_i < 18u) {
|
||||
v_p = ((uint64_t)(v_depth_histo[v_i]));
|
||||
v_sum += v_p;
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, v_p);
|
||||
v_retval = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_p), v_lp));
|
||||
v_i += 1u;
|
||||
if (v_p <= 1u) {
|
||||
continue;
|
||||
}
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, v_p);
|
||||
v_retval = wuffs_brotli__bit_cost__f64_sub(self, v_retval, wuffs_brotli__bit_cost__f64_mul(self, wuffs_brotli__bit_cost__f64_from_u64(self, v_p), v_lp));
|
||||
}
|
||||
if (v_sum != 0u) {
|
||||
v_lp = wuffs_brotli__bit_cost__fast_log2_memo(self, v_sum);
|
||||
v_retval = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum), v_lp));
|
||||
v_retval = wuffs_brotli__bit_cost__f64_add(self, v_retval, wuffs_brotli__bit_cost__f64_mul(self, wuffs_brotli__bit_cost__f64_from_u64(self, v_sum), v_lp));
|
||||
}
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum)) != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum);
|
||||
if (wuffs_brotli__bit_cost__f64_lt(self, v_retval, wuffs_brotli__bit_cost__f64_from_u64(self, v_sum)) != 0u) {
|
||||
v_retval = wuffs_brotli__bit_cost__f64_from_u64(self, v_sum);
|
||||
}
|
||||
return wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_bits, v_retval);
|
||||
return wuffs_brotli__bit_cost__f64_add(self, v_bits, v_retval);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.rb_byte
|
||||
@@ -87834,29 +88313,14 @@ wuffs_brotli__encoder__log2_floor_non_zero(
|
||||
const wuffs_brotli__encoder* self,
|
||||
uint64_t a_n) {
|
||||
uint32_t v_x = 0;
|
||||
uint32_t v_r = 0;
|
||||
|
||||
v_x = ((uint32_t)(a_n));
|
||||
if (v_x >= 65536u) {
|
||||
v_x >>= 16u;
|
||||
v_r = 16u;
|
||||
}
|
||||
if (v_x >= 256u) {
|
||||
v_x >>= 8u;
|
||||
v_r += 8u;
|
||||
}
|
||||
if (v_x >= 16u) {
|
||||
v_x >>= 4u;
|
||||
v_r += 4u;
|
||||
}
|
||||
if (v_x >= 4u) {
|
||||
v_x >>= 2u;
|
||||
v_r += 2u;
|
||||
}
|
||||
if (v_x >= 2u) {
|
||||
v_r += 1u;
|
||||
}
|
||||
return (v_r & 31u);
|
||||
v_x |= (v_x >> 1u);
|
||||
v_x |= (v_x >> 2u);
|
||||
v_x |= (v_x >> 4u);
|
||||
v_x |= (v_x >> 8u);
|
||||
v_x |= (v_x >> 16u);
|
||||
return WUFFS_BROTLI__LOG2_DEBRUIJN32[(((uint32_t)(v_x * 130329821u)) >> 27u)];
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.fast_log2
|
||||
@@ -87887,14 +88351,14 @@ wuffs_brotli__encoder__bits_entropy_of_histogram(
|
||||
while (v_i < 256u) {
|
||||
v_p = ((uint64_t)(self->private_data.f_histogram[v_i]));
|
||||
v_sum += v_p;
|
||||
v_retval = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_p), wuffs_brotli__encoder__fast_log2(self, v_p)));
|
||||
v_retval = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_retval, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_p), wuffs_brotli__encoder__fast_log2(self, v_p)));
|
||||
v_i += 1u;
|
||||
}
|
||||
if (v_sum != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum), wuffs_brotli__encoder__fast_log2(self, v_sum)));
|
||||
v_retval = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_retval, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_sum), wuffs_brotli__encoder__fast_log2(self, v_sum)));
|
||||
}
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum)) != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum);
|
||||
if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, v_retval, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_sum)) != 0u) {
|
||||
v_retval = wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_sum);
|
||||
}
|
||||
return v_retval;
|
||||
}
|
||||
@@ -87907,12 +88371,11 @@ wuffs_brotli__encoder__load64le(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_s,
|
||||
uint64_t a_i) {
|
||||
wuffs_base__slice_u8 v_q = {0};
|
||||
|
||||
if (a_i < ((uint64_t)(a_s.len))) {
|
||||
v_q = wuffs_base__slice_u8__subslice_i(a_s, a_i);
|
||||
if (((uint64_t)(v_q.len)) >= 8u) {
|
||||
return wuffs_base__peek_u64le__no_bounds_check(v_q.ptr);
|
||||
if (a_i < 18446744073709486080u) {
|
||||
if ((a_i + 8u) <= ((uint64_t)(a_s.len))) {
|
||||
return wuffs_base__peek_u64le__no_bounds_check(wuffs_base__slice_u8__subslice_ij(a_s,
|
||||
a_i,
|
||||
(a_i + 8u)).ptr);
|
||||
}
|
||||
}
|
||||
return 0u;
|
||||
@@ -87926,12 +88389,11 @@ wuffs_brotli__encoder__load32le(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_s,
|
||||
uint64_t a_i) {
|
||||
wuffs_base__slice_u8 v_q = {0};
|
||||
|
||||
if (a_i < ((uint64_t)(a_s.len))) {
|
||||
v_q = wuffs_base__slice_u8__subslice_i(a_s, a_i);
|
||||
if (((uint64_t)(v_q.len)) >= 4u) {
|
||||
return wuffs_base__peek_u32le__no_bounds_check(v_q.ptr);
|
||||
if (a_i < 18446744073709486080u) {
|
||||
if ((a_i + 4u) <= ((uint64_t)(a_s.len))) {
|
||||
return wuffs_base__peek_u32le__no_bounds_check(wuffs_base__slice_u8__subslice_ij(a_s,
|
||||
a_i,
|
||||
(a_i + 4u)).ptr);
|
||||
}
|
||||
}
|
||||
return 0u;
|
||||
@@ -87962,11 +88424,7 @@ wuffs_brotli__encoder__find_match_length_with_limit(
|
||||
while ((v_n >= 8u) && (((uint64_t)(v_a.len)) >= 8u) && (((uint64_t)(v_b.len)) >= 8u)) {
|
||||
v_x = (wuffs_base__peek_u64le__no_bounds_check(v_a.ptr) ^ wuffs_base__peek_u64le__no_bounds_check(v_b.ptr));
|
||||
if (v_x != 0u) {
|
||||
while ((v_x & 255u) == 0u) {
|
||||
v_matched += 1u;
|
||||
v_x >>= 8u;
|
||||
}
|
||||
return v_matched;
|
||||
return ((uint64_t)(v_matched + (wuffs_brotli__encoder__ctz64(self, v_x) >> 3u)));
|
||||
}
|
||||
v_matched += 8u;
|
||||
v_n -= 8u;
|
||||
@@ -88369,6 +88827,13 @@ wuffs_brotli__metablock_builder__find_blocks(
|
||||
uint64_t v_c = 0;
|
||||
uint8_t v_cur_id = 0;
|
||||
uint8_t v_b = 0;
|
||||
uint64_t v_nh = 0;
|
||||
uint64_t v_icn = 0;
|
||||
uint64_t v_best_k = 0;
|
||||
bool v_use_capped = false;
|
||||
uint64_t v_skip_t = 0;
|
||||
uint64_t v_t = 0;
|
||||
uint32_t v_n = 0;
|
||||
|
||||
v_alphabet_size = ((uint64_t)(a_ds));
|
||||
v_bitmap_len = (((uint64_t)(a_num_histograms + 7u)) >> 3u);
|
||||
@@ -88396,13 +88861,14 @@ wuffs_brotli__metablock_builder__find_blocks(
|
||||
v_k = ((uint64_t)(((uint64_t)(v_i * a_num_histograms)) + v_j));
|
||||
if (v_k < 35200u) {
|
||||
v_c = wuffs_brotli__metablock_builder__bit_cost(self, ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(((uint64_t)(a_histograms + ((uint64_t)(v_j * v_hs)))) + ((uint64_t)(v_i * 4u))))))));
|
||||
self->private_data.f_insert_cost[v_k] = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, self->private_data.f_insert_cost[(v_j & 255u)], v_c);
|
||||
self->private_data.f_insert_cost[v_k] = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, self->private_data.f_insert_cost[(v_j & 255u)], v_c);
|
||||
}
|
||||
v_j += 1u;
|
||||
}
|
||||
}
|
||||
wuffs_private_impl__bulk_memset(&self->private_data.f_cost[0], 256u * (size_t)8u, 0u);
|
||||
wuffs_brotli__metablock_builder__zero(self, a_wb, a_switch_signal, ((uint64_t)(a_length * v_bitmap_len)));
|
||||
v_nh = wuffs_base__u64__min(a_num_histograms, 256u);
|
||||
v_byte_ix = 0u;
|
||||
while (v_byte_ix < a_length) {
|
||||
v_ix = ((uint64_t)(v_byte_ix * v_bitmap_len));
|
||||
@@ -88410,31 +88876,79 @@ wuffs_brotli__metablock_builder__find_blocks(
|
||||
a_wb,
|
||||
a_data,
|
||||
v_byte_ix,
|
||||
a_width) * a_num_histograms));
|
||||
a_width) * v_nh));
|
||||
v_min_cost = 6088095589093318446u;
|
||||
v_block_switch_cost = a_block_switch_bitcost;
|
||||
v_icn = 0u;
|
||||
if (v_insert_cost_ix <= 35200u) {
|
||||
v_icn = wuffs_base__u64__min(v_nh, (35200u - v_insert_cost_ix));
|
||||
}
|
||||
if ((v_byte_ix == 2000u) && (((uint64_t)(a_length - 2000u)) > v_alphabet_size) && (v_nh >= 4u)) {
|
||||
v_t = ((uint64_t)(v_alphabet_size * v_nh));
|
||||
v_t = wuffs_base__u64__min(v_t, 35200u);
|
||||
v_i = 0u;
|
||||
while (v_i < v_t) {
|
||||
self->private_data.f_capped_plus[wuffs_base__u64__min(v_i, 35199u)] = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, a_block_switch_bitcost, self->private_data.f_insert_cost[wuffs_base__u64__min(v_i, 35199u)]);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_use_capped = true;
|
||||
}
|
||||
v_best_k = 18446744073709551615u;
|
||||
v_k = 0u;
|
||||
while (v_k < a_num_histograms) {
|
||||
v_c = 0u;
|
||||
if (((uint64_t)(v_insert_cost_ix + v_k)) < 35200u) {
|
||||
v_c = self->private_data.f_insert_cost[(((uint64_t)(v_insert_cost_ix + v_k)) % 35200u)];
|
||||
while (v_k < v_nh) {
|
||||
if (v_k < v_icn) {
|
||||
v_j = ((uint64_t)(v_insert_cost_ix + v_k));
|
||||
v_j = wuffs_base__u64__min(v_j, 35199u);
|
||||
if (v_use_capped && (self->private_data.f_cost[(v_k & 255u)] == a_block_switch_bitcost)) {
|
||||
v_c = self->private_data.f_capped_plus[v_j];
|
||||
} else {
|
||||
v_c = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, self->private_data.f_cost[(v_k & 255u)], self->private_data.f_insert_cost[v_j]);
|
||||
}
|
||||
} else {
|
||||
v_c = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, self->private_data.f_cost[(v_k & 255u)], 0u);
|
||||
}
|
||||
v_c = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, self->private_data.f_cost[(v_k & 255u)], v_c);
|
||||
self->private_data.f_cost[(v_k & 255u)] = v_c;
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_c, v_min_cost) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, v_c, v_min_cost) != 0u) {
|
||||
v_min_cost = v_c;
|
||||
wuffs_brotli__metablock_builder__st8(self, a_wb, ((uint64_t)(a_block_id + v_byte_ix)), ((uint8_t)(v_k)));
|
||||
v_best_k = v_k;
|
||||
}
|
||||
v_k += 1u;
|
||||
}
|
||||
if (v_best_k <= 255u) {
|
||||
wuffs_brotli__metablock_builder__st8(self, a_wb, ((uint64_t)(a_block_id + v_byte_ix)), ((uint8_t)(v_best_k)));
|
||||
}
|
||||
if (v_byte_ix < 2000u) {
|
||||
v_block_switch_cost = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_block_switch_cost, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, 4605110762971426980u, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, 4540289913102728147u, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_byte_ix))));
|
||||
v_block_switch_cost = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, v_block_switch_cost, wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, 4605110762971426980u, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, 4540289913102728147u, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_byte_ix))));
|
||||
}
|
||||
v_skip_t = 18446744073709551615u;
|
||||
if ((v_nh >= 8u) && (v_min_cost < 9218868437227405312u) && (((uint64_t)(v_block_switch_cost - 1u)) < 9218868437227405311u)) {
|
||||
v_t = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_block_switch_cost, v_min_cost);
|
||||
v_n = 0u;
|
||||
while ((v_n < 3u) && (v_t < 9218868437227405312u) && (wuffs_brotli__bit_cost__f64_ge(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_t, v_min_cost), v_block_switch_cost) == 0u)) {
|
||||
v_t += 1u;
|
||||
v_n += 1u;
|
||||
}
|
||||
if ((v_t < 9218868437227405312u) && (wuffs_brotli__bit_cost__f64_ge(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_t, v_min_cost), v_block_switch_cost) != 0u)) {
|
||||
v_n = 0u;
|
||||
while ((v_n < 3u) && (v_t > 0u) && (wuffs_brotli__bit_cost__f64_ge(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, ((uint64_t)(v_t - 1u)), v_min_cost), v_block_switch_cost) != 0u)) {
|
||||
v_t -= 1u;
|
||||
v_n += 1u;
|
||||
}
|
||||
if ((v_t == 0u) || (wuffs_brotli__bit_cost__f64_ge(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, ((uint64_t)(v_t - 1u)), v_min_cost), v_block_switch_cost) == 0u)) {
|
||||
v_skip_t = v_t;
|
||||
}
|
||||
}
|
||||
}
|
||||
v_k = 0u;
|
||||
while (v_k < a_num_histograms) {
|
||||
v_c = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, self->private_data.f_cost[(v_k & 255u)], v_min_cost);
|
||||
while (v_k < v_nh) {
|
||||
v_c = self->private_data.f_cost[(v_k & 255u)];
|
||||
if ((v_c <= 9218868437227405312u) && (v_c >= v_skip_t)) {
|
||||
v_c = v_block_switch_cost;
|
||||
} else {
|
||||
v_c = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_c, v_min_cost);
|
||||
}
|
||||
self->private_data.f_cost[(v_k & 255u)] = v_c;
|
||||
if (wuffs_softfloat__arith__f64_ge(&self->private_data.f_sf, v_c, v_block_switch_cost) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f64_ge(&self->private_data.f_bc, v_c, v_block_switch_cost) != 0u) {
|
||||
self->private_data.f_cost[(v_k & 255u)] = v_block_switch_cost;
|
||||
v_j = ((uint64_t)(v_ix + (v_k >> 3u)));
|
||||
v_b = wuffs_brotli__metablock_builder__ld8(self, a_wb, ((uint64_t)(a_switch_signal + v_j)));
|
||||
@@ -88779,7 +89293,7 @@ wuffs_brotli__metablock_builder__cluster_blocks(
|
||||
v_tmp,
|
||||
((uint64_t)(v_all_histograms + ((uint64_t)(((uint64_t)(v_s)) * v_hs)))),
|
||||
((uint64_t)(v_tmp + v_hs)));
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_cur_bits, v_best_bits) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, v_cur_bits, v_best_bits) != 0u) {
|
||||
v_best_bits = v_cur_bits;
|
||||
v_best_out = v_s;
|
||||
}
|
||||
@@ -90008,7 +90522,7 @@ wuffs_brotli__metablock_builder__histogram_pair_is_less(
|
||||
uint32_t a_p2_idx1,
|
||||
uint32_t a_p2_idx2) {
|
||||
if (wuffs_softfloat__arith__f64_ne(&self->private_data.f_sf, a_p1_cost_diff, a_p2_cost_diff) != 0u) {
|
||||
return (wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, a_p1_cost_diff, a_p2_cost_diff) != 0u);
|
||||
return (wuffs_brotli__bit_cost__f64_gt(&self->private_data.f_bc, a_p1_cost_diff, a_p2_cost_diff) != 0u);
|
||||
}
|
||||
return (((uint32_t)(a_p1_idx2 - a_p1_idx1)) > ((uint32_t)(a_p2_idx2 - a_p2_idx1)));
|
||||
}
|
||||
@@ -90024,7 +90538,7 @@ wuffs_brotli__metablock_builder__cluster_cost_diff(
|
||||
uint64_t v_size_c = 0;
|
||||
|
||||
v_size_c = ((uint64_t)(a_size_a + a_size_b));
|
||||
return wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_size_a), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, a_size_a)), wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_size_b), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, a_size_b))), wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_size_c), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_size_c)));
|
||||
return wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, a_size_a), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, a_size_a)), wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, a_size_b), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, a_size_b))), wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_size_c), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_size_c)));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.pair_copy
|
||||
@@ -90101,9 +90615,9 @@ wuffs_brotli__metablock_builder__compare_and_push_to_queue(
|
||||
}
|
||||
v_h1 = ((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_idx1)) * v_hs))));
|
||||
v_h2 = ((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_idx2)) * v_hs))));
|
||||
v_cost_diff = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, 4602678819172646912u, wuffs_brotli__metablock_builder__cluster_cost_diff(self, ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_cluster_size + ((uint64_t)(((uint64_t)(v_idx1)) * 4u))))))), ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_cluster_size + ((uint64_t)(((uint64_t)(v_idx2)) * 4u)))))))));
|
||||
v_cost_diff = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_cost_diff, wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, v_h1, a_ds));
|
||||
v_cost_diff = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_cost_diff, wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, v_h2, a_ds));
|
||||
v_cost_diff = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, 4602678819172646912u, wuffs_brotli__metablock_builder__cluster_cost_diff(self, ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_cluster_size + ((uint64_t)(((uint64_t)(v_idx1)) * 4u))))))), ((uint64_t)(wuffs_brotli__metablock_builder__ld32(self, a_wb, ((uint64_t)(a_cluster_size + ((uint64_t)(((uint64_t)(v_idx2)) * 4u)))))))));
|
||||
v_cost_diff = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_cost_diff, wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, v_h1, a_ds));
|
||||
v_cost_diff = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_cost_diff, wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, v_h2, a_ds));
|
||||
if (wuffs_brotli__metablock_builder__h_total(self, a_wb, v_h1, a_ds) == 0u) {
|
||||
v_cost_combo = wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, v_h2, a_ds);
|
||||
v_is_good_pair = true;
|
||||
@@ -90127,13 +90641,13 @@ wuffs_brotli__metablock_builder__compare_and_push_to_queue(
|
||||
v_h2,
|
||||
a_ds);
|
||||
v_c = wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, a_tmp, a_ds);
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_c, wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_threshold, v_cost_diff)) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, v_c, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_threshold, v_cost_diff)) != 0u) {
|
||||
v_cost_combo = v_c;
|
||||
v_is_good_pair = true;
|
||||
}
|
||||
}
|
||||
if (v_is_good_pair) {
|
||||
v_cost_diff = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_cost_diff, v_cost_combo);
|
||||
v_cost_diff = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_cost_diff, v_cost_combo);
|
||||
if ((self->private_impl.f_num_pairs > 0u) && wuffs_brotli__metablock_builder__histogram_pair_is_less(self,
|
||||
wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_pairs + 16u))),
|
||||
wuffs_brotli__metablock_builder__ld32(self, a_wb, a_pairs),
|
||||
@@ -90225,7 +90739,7 @@ wuffs_brotli__metablock_builder__histogram_combine(
|
||||
v_idx1 += 1u;
|
||||
}
|
||||
while (v_num_clusters > v_min_cluster_size) {
|
||||
if (wuffs_softfloat__arith__f64_ge(&self->private_data.f_sf, wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_pairs + 16u))), v_cost_diff_threshold) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f64_ge(&self->private_data.f_bc, wuffs_brotli__metablock_builder__ld64(self, a_wb, ((uint64_t)(a_pairs + 16u))), v_cost_diff_threshold) != 0u) {
|
||||
v_cost_diff_threshold = 6088095589093318446u;
|
||||
v_min_cluster_size = a_max_clusters;
|
||||
continue;
|
||||
@@ -90351,7 +90865,7 @@ wuffs_brotli__metablock_builder__histogram_bit_cost_distance(
|
||||
a_candidate,
|
||||
a_ds);
|
||||
v_c = wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, a_tmp, a_ds);
|
||||
return wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_c, wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, a_candidate, a_ds));
|
||||
return wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_c, wuffs_brotli__metablock_builder__h_bit_cost(self, a_wb, a_candidate, a_ds));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.histogram_remap
|
||||
@@ -90400,7 +90914,7 @@ wuffs_brotli__metablock_builder__histogram_remap(
|
||||
((uint64_t)(a_in_histos + ((uint64_t)(v_i * v_hs)))),
|
||||
((uint64_t)(a_out + ((uint64_t)(((uint64_t)(v_c)) * v_hs)))),
|
||||
a_tmp);
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_cur_bits, v_best_bits) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, v_cur_bits, v_best_bits) != 0u) {
|
||||
v_best_bits = v_cur_bits;
|
||||
v_best_out = v_c;
|
||||
}
|
||||
@@ -91254,10 +91768,10 @@ wuffs_brotli__encoder__estimate_entropy(
|
||||
while (v_i < a_size) {
|
||||
v_p = self->private_data.f_ctx_histo[(((uint32_t)(a_off + v_i)) & 511u)];
|
||||
v_total += ((uint64_t)(v_p));
|
||||
v_result = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_result, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, v_p), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, ((uint64_t)(v_p)))));
|
||||
v_result = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_result, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u32(&self->private_data.f_bc, v_p), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, ((uint64_t)(v_p)))));
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_total), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_total)), v_result);
|
||||
return wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_total), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_total)), v_result);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.choose_context_map
|
||||
@@ -91285,24 +91799,24 @@ wuffs_brotli__encoder__choose_context_map(
|
||||
v_i += 1u;
|
||||
}
|
||||
v_e1 = wuffs_brotli__encoder__estimate_entropy(self, 16u, 3u);
|
||||
v_e2 = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, wuffs_brotli__encoder__estimate_entropy(self, 24u, 3u), wuffs_brotli__encoder__estimate_entropy(self, 27u, 3u));
|
||||
v_e2 = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, wuffs_brotli__encoder__estimate_entropy(self, 24u, 3u), wuffs_brotli__encoder__estimate_entropy(self, 27u, 3u));
|
||||
v_e3 = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < 3u) {
|
||||
v_e3 = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_e3, wuffs_brotli__encoder__estimate_entropy(self, (3u * v_i), 3u));
|
||||
v_e3 = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_e3, wuffs_brotli__encoder__estimate_entropy(self, (3u * v_i), 3u));
|
||||
v_i += 1u;
|
||||
}
|
||||
v_total = ((uint32_t)(((uint32_t)(self->private_data.f_ctx_histo[16u] + self->private_data.f_ctx_histo[17u])) + self->private_data.f_ctx_histo[18u]));
|
||||
v_e0 = wuffs_softfloat__arith__f64_div(&self->private_data.f_sf, 4607182418800017408u, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, v_total));
|
||||
v_e1 = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_e1, v_e0);
|
||||
v_e2 = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_e2, v_e0);
|
||||
v_e3 = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_e3, v_e0);
|
||||
v_e0 = wuffs_softfloat__arith__f64_div(&self->private_data.f_sf, 4607182418800017408u, wuffs_brotli__bit_cost__f64_from_u32(&self->private_data.f_bc, v_total));
|
||||
v_e1 = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, v_e1, v_e0);
|
||||
v_e2 = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, v_e2, v_e0);
|
||||
v_e3 = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, v_e3, v_e0);
|
||||
if (self->private_impl.f_quality < 7u) {
|
||||
v_e3 = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_e1, 4621819117588971520u);
|
||||
v_e3 = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, v_e1, 4621819117588971520u);
|
||||
}
|
||||
if ((wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_e1, v_e2), 4596373779694328218u) != 0u) && (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_e1, v_e3), 4596373779694328218u) != 0u)) {
|
||||
if ((wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_e1, v_e2), 4596373779694328218u) != 0u) && (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_e1, v_e3), 4596373779694328218u) != 0u)) {
|
||||
return 1u;
|
||||
} else if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_e2, v_e3), 4581421828931458171u) != 0u) {
|
||||
} else if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_e2, v_e3), 4581421828931458171u) != 0u) {
|
||||
return 2u;
|
||||
}
|
||||
return 3u;
|
||||
@@ -91362,13 +91876,13 @@ wuffs_brotli__encoder__should_use_complex_static_context_map(
|
||||
v_e2 = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < 13u) {
|
||||
v_e2 = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_e2, wuffs_brotli__encoder__estimate_entropy(self, (32u + (v_i << 5u)), 32u));
|
||||
v_e2 = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_e2, wuffs_brotli__encoder__estimate_entropy(self, (32u + (v_i << 5u)), 32u));
|
||||
v_i += 1u;
|
||||
}
|
||||
v_e0 = wuffs_softfloat__arith__f64_div(&self->private_data.f_sf, 4607182418800017408u, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, v_total));
|
||||
v_e1 = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_e1, v_e0);
|
||||
v_e2 = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_e2, v_e0);
|
||||
if ((wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, v_e2, 4613937818241073152u) != 0u) || (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_e1, v_e2), 4596373779694328218u) != 0u)) {
|
||||
v_e0 = wuffs_softfloat__arith__f64_div(&self->private_data.f_sf, 4607182418800017408u, wuffs_brotli__bit_cost__f64_from_u32(&self->private_data.f_bc, v_total));
|
||||
v_e1 = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, v_e1, v_e0);
|
||||
v_e2 = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, v_e2, v_e0);
|
||||
if ((wuffs_brotli__bit_cost__f64_gt(&self->private_data.f_bc, v_e2, 4613937818241073152u) != 0u) || (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_e1, v_e2), 4596373779694328218u) != 0u)) {
|
||||
return 0u;
|
||||
}
|
||||
return 13u;
|
||||
@@ -91456,13 +91970,13 @@ wuffs_brotli__encoder__should_compress(
|
||||
return false;
|
||||
}
|
||||
if (a_num_commands < ((a_bytes >> 8u) + 2u)) {
|
||||
if (wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_num_literals), wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, 4607092346807469998u, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_bytes))) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f64_gt(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, a_num_literals), wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, 4607092346807469998u, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, a_bytes))) != 0u) {
|
||||
v_i = 0u;
|
||||
while (v_i < 256u) {
|
||||
self->private_data.f_ctx_histo[v_i] = 0u;
|
||||
v_i += 1u;
|
||||
}
|
||||
v_bit_cost_threshold = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_bytes), 4620603145689581486u), 4590207312512236308u);
|
||||
v_bit_cost_threshold = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, a_bytes), 4620603145689581486u), 4590207312512236308u);
|
||||
v_t = (((uint64_t)(a_bytes + 12u)) / 13u);
|
||||
v_pos = ((uint32_t)(a_last_flush_pos));
|
||||
v_i = 0u;
|
||||
@@ -91471,7 +91985,7 @@ wuffs_brotli__encoder__should_compress(
|
||||
v_pos += 13u;
|
||||
v_i += 1u;
|
||||
}
|
||||
if (wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, wuffs_brotli__encoder__ctx_histo_bits_entropy(self), v_bit_cost_threshold) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f64_gt(&self->private_data.f_bc, wuffs_brotli__encoder__ctx_histo_bits_entropy(self), v_bit_cost_threshold) != 0u) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -91494,14 +92008,14 @@ wuffs_brotli__encoder__ctx_histo_bits_entropy(
|
||||
while (v_i < 256u) {
|
||||
v_p = ((uint64_t)(self->private_data.f_ctx_histo[v_i]));
|
||||
v_sum += v_p;
|
||||
v_retval = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_p), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_p)));
|
||||
v_retval = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_retval, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_p), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_p)));
|
||||
v_i += 1u;
|
||||
}
|
||||
if (v_sum != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_sum)));
|
||||
v_retval = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_retval, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_sum), wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_sum)));
|
||||
}
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_retval, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum)) != 0u) {
|
||||
v_retval = wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_sum);
|
||||
if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, v_retval, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_sum)) != 0u) {
|
||||
v_retval = wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_sum);
|
||||
}
|
||||
return v_retval;
|
||||
}
|
||||
@@ -93380,6 +93894,723 @@ wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.clz64
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__clz64(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a) {
|
||||
uint64_t v_x = 0;
|
||||
uint32_t v_n = 0;
|
||||
|
||||
v_x = a_a;
|
||||
if ((v_x >> 32u) == 0u) {
|
||||
v_x = ((uint64_t)(v_x << 32u));
|
||||
v_n = 32u;
|
||||
}
|
||||
if ((v_x >> 48u) == 0u) {
|
||||
v_x = ((uint64_t)(v_x << 16u));
|
||||
v_n = ((v_n + 16u) & 63u);
|
||||
}
|
||||
if ((v_x >> 56u) == 0u) {
|
||||
v_x = ((uint64_t)(v_x << 8u));
|
||||
v_n = ((v_n + 8u) & 63u);
|
||||
}
|
||||
if ((v_x >> 60u) == 0u) {
|
||||
v_x = ((uint64_t)(v_x << 4u));
|
||||
v_n = ((v_n + 4u) & 63u);
|
||||
}
|
||||
if ((v_x >> 62u) == 0u) {
|
||||
v_x = ((uint64_t)(v_x << 2u));
|
||||
v_n = ((v_n + 2u) & 63u);
|
||||
}
|
||||
if ((v_x >> 63u) == 0u) {
|
||||
v_n = ((v_n + 1u) & 63u);
|
||||
}
|
||||
return v_n;
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_add
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_add(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
uint32_t v_x = 0;
|
||||
uint32_t v_y = 0;
|
||||
uint32_t v_ex = 0;
|
||||
uint32_t v_d = 0;
|
||||
uint64_t v_s = 0;
|
||||
uint32_t v_z = 0;
|
||||
|
||||
if ((((uint32_t)(a_a - 8388608u)) < 2130706432u) && (((uint32_t)(a_b - 8388608u)) < 2130706432u)) {
|
||||
v_x = a_a;
|
||||
v_y = a_b;
|
||||
if (v_y > v_x) {
|
||||
v_x = a_b;
|
||||
v_y = a_a;
|
||||
}
|
||||
v_ex = ((v_x >> 23u) & 255u);
|
||||
v_d = ((uint32_t)(v_ex - ((v_y >> 23u) & 255u)));
|
||||
if (v_d >= 25u) {
|
||||
return v_x;
|
||||
}
|
||||
v_s = ((uint64_t)((((uint64_t)(((v_x & 8388607u) | 8388608u))) << 39u) + ((((uint64_t)(((v_y & 8388607u) | 8388608u))) << 39u) >> (v_d & 31u))));
|
||||
if (v_s >= 9223372036854775808u) {
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(549755813887u + ((v_s >> 40u) & 1u))))) >> 40u);
|
||||
v_z = ((uint32_t)(((uint32_t)(v_ex << 23u)) + ((uint32_t)((v_s & 33554431u)))));
|
||||
if (v_z >= 2139095040u) {
|
||||
return 2139095040u;
|
||||
}
|
||||
return v_z;
|
||||
}
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(274877906943u + ((v_s >> 39u) & 1u))))) >> 39u);
|
||||
return ((uint32_t)(((uint32_t)(((uint32_t)(v_ex - 1u)) << 23u)) + ((uint32_t)((v_s & 33554431u)))));
|
||||
} else if ((a_a == 0u) && ((a_b & 2147483647u) <= 2139095040u) && (a_b != 2147483648u)) {
|
||||
return a_b;
|
||||
} else if ((a_b == 0u) && ((a_a & 2147483647u) <= 2139095040u) && (a_a != 2147483648u)) {
|
||||
return a_a;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f32_add_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_sub
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_sub(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
uint32_t v_ex = 0;
|
||||
uint32_t v_d = 0;
|
||||
uint64_t v_s = 0;
|
||||
|
||||
if ((((uint32_t)(a_b - 8388608u)) < 2130706432u) && (a_a > a_b) && (a_a < 2139095040u)) {
|
||||
v_ex = ((a_a >> 23u) & 255u);
|
||||
v_d = ((uint32_t)(v_ex - ((a_b >> 23u) & 255u)));
|
||||
if (v_d >= 26u) {
|
||||
return a_a;
|
||||
} else if (v_d >= 2u) {
|
||||
v_s = ((uint64_t)((((uint64_t)(((a_a & 8388607u) | 8388608u))) << 39u) - ((((uint64_t)(((a_b & 8388607u) | 8388608u))) << 39u) >> (v_d & 31u))));
|
||||
if (v_s < 4611686018427387904u) {
|
||||
v_s <<= 1u;
|
||||
v_ex = (((uint32_t)(v_ex - 1u)) & 255u);
|
||||
}
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(274877906943u + ((v_s >> 39u) & 1u))))) >> 39u);
|
||||
return ((uint32_t)(((uint32_t)(((uint32_t)(v_ex - 1u)) << 23u)) + ((uint32_t)((v_s & 33554431u)))));
|
||||
}
|
||||
} else if ((a_b == 0u) && ((a_a & 2147483647u) <= 2139095040u)) {
|
||||
return a_a;
|
||||
} else if ((a_a == a_b) && (a_a < 2139095040u)) {
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f32_sub_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_add_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_add_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
uint32_t v_x = 0;
|
||||
uint32_t v_y = 0;
|
||||
uint32_t v_ex = 0;
|
||||
uint32_t v_d = 0;
|
||||
uint64_t v_mx = 0;
|
||||
uint64_t v_my = 0;
|
||||
uint64_t v_s = 0;
|
||||
uint32_t v_lz = 0;
|
||||
uint32_t v_z = 0;
|
||||
|
||||
if ((((uint32_t)(((a_a >> 23u) & 255u) - 1u)) < 254u) && (((uint32_t)(((a_b >> 23u) & 255u) - 1u)) < 254u)) {
|
||||
v_x = a_a;
|
||||
v_y = a_b;
|
||||
if ((v_y & 2147483647u) > (v_x & 2147483647u)) {
|
||||
v_x = a_b;
|
||||
v_y = a_a;
|
||||
}
|
||||
v_ex = ((v_x >> 23u) & 255u);
|
||||
v_d = ((uint32_t)(v_ex - ((v_y >> 23u) & 255u)));
|
||||
if (v_d >= 26u) {
|
||||
return v_x;
|
||||
}
|
||||
v_mx = (((uint64_t)(((v_x & 8388607u) | 8388608u))) << 39u);
|
||||
v_my = ((((uint64_t)(((v_y & 8388607u) | 8388608u))) << 39u) >> (v_d & 31u));
|
||||
if (((v_x ^ v_y) >> 31u) == 0u) {
|
||||
v_s = ((uint64_t)(v_mx + v_my));
|
||||
if (v_s >= 9223372036854775808u) {
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(549755813887u + ((v_s >> 40u) & 1u))))) >> 40u);
|
||||
v_z = ((uint32_t)(((uint32_t)(v_ex << 23u)) + ((uint32_t)((v_s & 33554431u)))));
|
||||
if (v_z >= 2139095040u) {
|
||||
v_z = 2139095040u;
|
||||
}
|
||||
return ((v_x & 2147483648u) | v_z);
|
||||
}
|
||||
} else {
|
||||
v_s = ((uint64_t)(v_mx - v_my));
|
||||
if (v_s == 0u) {
|
||||
return 0u;
|
||||
} else if (v_s < 4611686018427387904u) {
|
||||
v_lz = (((uint32_t)(wuffs_brotli__bit_cost__clz64(self, v_s) - 1u)) & 63u);
|
||||
if (v_lz >= v_ex) {
|
||||
return wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
v_ex = (((uint32_t)(v_ex - v_lz)) & 255u);
|
||||
v_s = ((uint64_t)(v_s << v_lz));
|
||||
}
|
||||
}
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(274877906943u + ((v_s >> 39u) & 1u))))) >> 39u);
|
||||
return ((v_x & 2147483648u) | ((uint32_t)(((uint32_t)(((uint32_t)(v_ex - 1u)) << 23u)) + ((uint32_t)((v_s & 33554431u))))));
|
||||
}
|
||||
return wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_sub_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_sub_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
if (((uint32_t)(((a_b >> 23u) & 255u) - 1u)) < 254u) {
|
||||
return wuffs_brotli__bit_cost__f32_add_any(self, a_a, (a_b ^ 2147483648u));
|
||||
}
|
||||
return wuffs_softfloat__arith__f32_sub(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_key
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_key(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a) {
|
||||
if ((a_a >> 31u) != 0u) {
|
||||
return (4294967295u ^ a_a);
|
||||
}
|
||||
return (a_a | 2147483648u);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_lt
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_lt(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
if ((a_a | a_b) <= 2139095040u) {
|
||||
if (a_a < a_b) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f32_lt_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_lt_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_lt_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
if (((a_a & 2147483647u) <= 2139095040u) && ((a_b & 2147483647u) <= 2139095040u)) {
|
||||
if ((wuffs_brotli__bit_cost__f32_key(self, a_a) < wuffs_brotli__bit_cost__f32_key(self, a_b)) && (((a_a | a_b) & 2147483647u) != 0u)) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_softfloat__arith__f32_lt(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_le
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_le(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
if ((a_a | a_b) <= 2139095040u) {
|
||||
if (a_a <= a_b) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f32_le_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_le_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_le_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
if (((a_a & 2147483647u) <= 2139095040u) && ((a_b & 2147483647u) <= 2139095040u)) {
|
||||
if ((wuffs_brotli__bit_cost__f32_key(self, a_a) <= wuffs_brotli__bit_cost__f32_key(self, a_b)) || (((a_a | a_b) & 2147483647u) == 0u)) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_softfloat__arith__f32_le(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_gt
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_gt(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
if ((a_a | a_b) <= 2139095040u) {
|
||||
if (a_a > a_b) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f32_gt_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_gt_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_gt_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a,
|
||||
uint32_t a_b) {
|
||||
return wuffs_brotli__bit_cost__f32_lt_any(self, a_b, a_a);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_add
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_add(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
uint64_t v_x = 0;
|
||||
uint64_t v_y = 0;
|
||||
uint64_t v_ex = 0;
|
||||
uint64_t v_d = 0;
|
||||
uint64_t v_my = 0;
|
||||
uint64_t v_s = 0;
|
||||
|
||||
if ((((uint64_t)(a_a - 4503599627370496u)) < 9214364837600034816u) && (((uint64_t)(a_b - 4503599627370496u)) < 9214364837600034816u)) {
|
||||
v_x = a_a;
|
||||
v_y = a_b;
|
||||
if (v_y > v_x) {
|
||||
v_x = a_b;
|
||||
v_y = a_a;
|
||||
}
|
||||
v_ex = ((v_x >> 52u) & 2047u);
|
||||
v_d = ((uint64_t)(v_ex - ((v_y >> 52u) & 2047u)));
|
||||
if (v_d >= 54u) {
|
||||
return v_x;
|
||||
}
|
||||
v_my = (((v_y & 4503599627370495u) | 4503599627370496u) << 10u);
|
||||
if ((v_my & ((uint64_t)((((uint64_t)(1u)) << (v_d & 63u)) - 1u))) != 0u) {
|
||||
v_my = ((v_my >> (v_d & 63u)) | 1u);
|
||||
} else {
|
||||
v_my = (v_my >> (v_d & 63u));
|
||||
}
|
||||
v_s = ((uint64_t)((((v_x & 4503599627370495u) | 4503599627370496u) << 10u) + v_my));
|
||||
if (v_s >= 9223372036854775808u) {
|
||||
v_ex = (((uint64_t)(v_ex + 1u)) & 2047u);
|
||||
v_s = ((v_s >> 1u) | (v_s & 1u));
|
||||
}
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(511u + ((v_s >> 10u) & 1u))))) >> 10u);
|
||||
v_s = ((uint64_t)(((uint64_t)(((uint64_t)(v_ex - 1u)) << 52u)) + v_s));
|
||||
if (v_s >= 9218868437227405312u) {
|
||||
return 9218868437227405312u;
|
||||
}
|
||||
return v_s;
|
||||
} else if ((a_a == 0u) && ((a_b & 9223372036854775807u) <= 9218868437227405312u) && (a_b != 9223372036854775808u)) {
|
||||
return a_b;
|
||||
} else if ((a_b == 0u) && ((a_a & 9223372036854775807u) <= 9218868437227405312u) && (a_a != 9223372036854775808u)) {
|
||||
return a_a;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f64_add_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_sub
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_sub(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
uint64_t v_ex = 0;
|
||||
uint64_t v_d = 0;
|
||||
uint64_t v_my = 0;
|
||||
uint64_t v_s = 0;
|
||||
uint64_t v_lz = 0;
|
||||
|
||||
if ((((uint64_t)(a_b - 4503599627370496u)) < 9214364837600034816u) && (a_a > a_b) && (a_a < 9218868437227405312u)) {
|
||||
v_ex = ((a_a >> 52u) & 2047u);
|
||||
v_d = ((uint64_t)(v_ex - ((a_b >> 52u) & 2047u)));
|
||||
if (v_d >= 55u) {
|
||||
return a_a;
|
||||
} else if (v_d >= 2u) {
|
||||
v_my = (((a_b & 4503599627370495u) | 4503599627370496u) << 10u);
|
||||
if ((v_my & ((uint64_t)((((uint64_t)(1u)) << (v_d & 63u)) - 1u))) != 0u) {
|
||||
v_my = ((v_my >> (v_d & 63u)) | 1u);
|
||||
} else {
|
||||
v_my = (v_my >> (v_d & 63u));
|
||||
}
|
||||
v_s = ((uint64_t)((((a_a & 4503599627370495u) | 4503599627370496u) << 10u) - v_my));
|
||||
if (v_s < 4611686018427387904u) {
|
||||
v_s <<= 1u;
|
||||
v_ex = (((uint64_t)(v_ex - 1u)) & 2047u);
|
||||
}
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(511u + ((v_s >> 10u) & 1u))))) >> 10u);
|
||||
return ((uint64_t)(((uint64_t)(((uint64_t)(v_ex - 1u)) << 52u)) + v_s));
|
||||
}
|
||||
v_s = ((uint64_t)((((a_a & 4503599627370495u) | 4503599627370496u) << 10u) - ((((a_b & 4503599627370495u) | 4503599627370496u) << 10u) >> (v_d & 1u))));
|
||||
v_lz = ((uint64_t)(((uint64_t)(wuffs_brotli__bit_cost__clz64(self, v_s))) - 1u));
|
||||
if (v_lz < v_ex) {
|
||||
v_ex = (((uint64_t)(v_ex - v_lz)) & 2047u);
|
||||
v_s = ((uint64_t)(v_s << (v_lz & 63u)));
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(511u + ((v_s >> 10u) & 1u))))) >> 10u);
|
||||
return ((uint64_t)(((uint64_t)(((uint64_t)(v_ex - 1u)) << 52u)) + v_s));
|
||||
}
|
||||
} else if ((a_b == 0u) && ((a_a & 9223372036854775807u) <= 9218868437227405312u)) {
|
||||
return a_a;
|
||||
} else if ((a_a == a_b) && (a_a < 9218868437227405312u)) {
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f64_sub_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_add_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_add_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
uint64_t v_x = 0;
|
||||
uint64_t v_y = 0;
|
||||
uint64_t v_ex = 0;
|
||||
uint64_t v_d = 0;
|
||||
uint64_t v_mx = 0;
|
||||
uint64_t v_my = 0;
|
||||
uint64_t v_s = 0;
|
||||
uint64_t v_lz = 0;
|
||||
|
||||
if ((((uint64_t)(((a_a >> 52u) & 2047u) - 1u)) < 2046u) && (((uint64_t)(((a_b >> 52u) & 2047u) - 1u)) < 2046u)) {
|
||||
v_x = a_a;
|
||||
v_y = a_b;
|
||||
if ((v_y & 9223372036854775807u) > (v_x & 9223372036854775807u)) {
|
||||
v_x = a_b;
|
||||
v_y = a_a;
|
||||
}
|
||||
v_ex = ((v_x >> 52u) & 2047u);
|
||||
v_d = ((uint64_t)(v_ex - ((v_y >> 52u) & 2047u)));
|
||||
if (v_d >= 55u) {
|
||||
return v_x;
|
||||
}
|
||||
v_mx = (((v_x & 4503599627370495u) | 4503599627370496u) << 10u);
|
||||
v_my = (((v_y & 4503599627370495u) | 4503599627370496u) << 10u);
|
||||
if ((v_my & ((uint64_t)((((uint64_t)(1u)) << (v_d & 63u)) - 1u))) != 0u) {
|
||||
v_my = ((v_my >> (v_d & 63u)) | 1u);
|
||||
} else {
|
||||
v_my = (v_my >> (v_d & 63u));
|
||||
}
|
||||
if (((v_x ^ v_y) >> 63u) == 0u) {
|
||||
v_s = ((uint64_t)(v_mx + v_my));
|
||||
if (v_s >= 9223372036854775808u) {
|
||||
v_ex = (((uint64_t)(v_ex + 1u)) & 2047u);
|
||||
v_s = ((v_s >> 1u) | (v_s & 1u));
|
||||
}
|
||||
} else {
|
||||
v_s = ((uint64_t)(v_mx - v_my));
|
||||
if (v_s == 0u) {
|
||||
return 0u;
|
||||
} else if (v_s < 4611686018427387904u) {
|
||||
v_lz = (((uint64_t)(((uint64_t)(wuffs_brotli__bit_cost__clz64(self, v_s))) - 1u)) & 63u);
|
||||
if (v_lz >= v_ex) {
|
||||
return wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
v_ex = (((uint64_t)(v_ex - v_lz)) & 2047u);
|
||||
v_s = ((uint64_t)(v_s << v_lz));
|
||||
}
|
||||
}
|
||||
v_s = (((uint64_t)(v_s + ((uint64_t)(511u + ((v_s >> 10u) & 1u))))) >> 10u);
|
||||
v_s = ((uint64_t)(((uint64_t)(((uint64_t)(v_ex - 1u)) << 52u)) + v_s));
|
||||
if (v_s >= 9218868437227405312u) {
|
||||
v_s = 9218868437227405312u;
|
||||
}
|
||||
return ((v_x & 9223372036854775808u) | v_s);
|
||||
}
|
||||
return wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_sub_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_sub_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
if (((uint64_t)(((a_b >> 52u) & 2047u) - 1u)) < 2046u) {
|
||||
return wuffs_brotli__bit_cost__f64_add_any(self, a_a, (a_b ^ 9223372036854775808u));
|
||||
}
|
||||
return wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_key
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_key(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a) {
|
||||
if ((a_a >> 63u) != 0u) {
|
||||
return (18446744073709551615u ^ a_a);
|
||||
}
|
||||
return (a_a | 9223372036854775808u);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_lt
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_lt(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
if ((a_a | a_b) <= 9218868437227405312u) {
|
||||
if (a_a < a_b) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f64_lt_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_lt_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_lt_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
if (((a_a & 9223372036854775807u) <= 9218868437227405312u) && ((a_b & 9223372036854775807u) <= 9218868437227405312u)) {
|
||||
if ((wuffs_brotli__bit_cost__f64_key(self, a_a) < wuffs_brotli__bit_cost__f64_key(self, a_b)) && (((a_a | a_b) & 9223372036854775807u) != 0u)) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_gt
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_gt(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
if ((a_a | a_b) <= 9218868437227405312u) {
|
||||
if (a_a > a_b) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f64_gt_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_gt_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_gt_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
return wuffs_brotli__bit_cost__f64_lt_any(self, a_b, a_a);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_ge
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_ge(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
if ((a_a | a_b) <= 9218868437227405312u) {
|
||||
if (a_a >= a_b) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_brotli__bit_cost__f64_ge_any(self, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_ge_any
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f64_ge_any(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
if (((a_a & 9223372036854775807u) <= 9218868437227405312u) && ((a_b & 9223372036854775807u) <= 9218868437227405312u)) {
|
||||
if ((wuffs_brotli__bit_cost__f64_key(self, a_a) >= wuffs_brotli__bit_cost__f64_key(self, a_b)) || (((a_a | a_b) & 9223372036854775807u) == 0u)) {
|
||||
return 1u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
return wuffs_softfloat__arith__f64_ge(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_mul
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_mul(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a,
|
||||
uint64_t a_b) {
|
||||
uint64_t v_ea = 0;
|
||||
uint64_t v_eb = 0;
|
||||
uint64_t v_e = 0;
|
||||
uint64_t v_ma = 0;
|
||||
uint64_t v_mb = 0;
|
||||
uint64_t v_a0 = 0;
|
||||
uint64_t v_a1 = 0;
|
||||
uint64_t v_b0 = 0;
|
||||
uint64_t v_b1 = 0;
|
||||
uint64_t v_p00 = 0;
|
||||
uint64_t v_p01 = 0;
|
||||
uint64_t v_p10 = 0;
|
||||
uint64_t v_mid = 0;
|
||||
uint64_t v_s = 0;
|
||||
uint64_t v_rb = 0;
|
||||
|
||||
v_ea = ((a_a >> 52u) & 2047u);
|
||||
v_eb = ((a_b >> 52u) & 2047u);
|
||||
if ((((uint64_t)(v_ea - 1u)) < 2046u) && (((uint64_t)(v_eb - 1u)) < 2046u)) {
|
||||
v_e = ((uint64_t)(((uint64_t)(v_ea + v_eb)) - 1023u));
|
||||
v_ma = (((a_a & 4503599627370495u) | 4503599627370496u) << 10u);
|
||||
v_mb = (((a_b & 4503599627370495u) | 4503599627370496u) << 11u);
|
||||
v_a0 = (v_ma & 4294967295u);
|
||||
v_a1 = (v_ma >> 32u);
|
||||
v_b0 = (v_mb & 4294967295u);
|
||||
v_b1 = (v_mb >> 32u);
|
||||
v_p00 = (v_a0 * v_b0);
|
||||
v_p01 = (v_a0 * v_b1);
|
||||
v_p10 = (v_a1 * v_b0);
|
||||
v_mid = ((v_p00 >> 32u) + (v_p01 & 4294967295u) + (v_p10 & 4294967295u));
|
||||
v_s = ((uint64_t)(((uint64_t)(((uint64_t)((v_a1 * v_b1) + (v_p01 >> 32u))) + (v_p10 >> 32u))) + (v_mid >> 32u)));
|
||||
if (((uint64_t)(v_ma * v_mb)) != 0u) {
|
||||
v_s |= 1u;
|
||||
}
|
||||
if (v_s < 4611686018427387904u) {
|
||||
v_e -= 1u;
|
||||
v_s <<= 1u;
|
||||
}
|
||||
if (v_e < 2045u) {
|
||||
v_rb = (v_s & 1023u);
|
||||
v_s = (((uint64_t)(v_s + 512u)) >> 10u);
|
||||
if (v_rb == 512u) {
|
||||
v_s &= 18446744073709551614u;
|
||||
}
|
||||
return (((a_a ^ a_b) & 9223372036854775808u) | ((uint64_t)(((uint64_t)(v_e << 52u)) + v_s)));
|
||||
}
|
||||
}
|
||||
return wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, a_a, a_b);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_from_u64
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_from_u64(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a) {
|
||||
uint32_t v_lz = 0;
|
||||
|
||||
if (a_a == 0u) {
|
||||
return 0u;
|
||||
} else if (a_a < 9007199254740992u) {
|
||||
v_lz = wuffs_brotli__bit_cost__clz64(self, a_a);
|
||||
if (v_lz >= 11u) {
|
||||
return ((uint64_t)(((uint64_t)((((uint64_t)(1085u - ((uint64_t)(v_lz)))) & 2047u) << 52u)) + ((uint64_t)(a_a << (v_lz - 11u)))));
|
||||
}
|
||||
}
|
||||
return wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_a);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_from_u32
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_from_u32(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a) {
|
||||
return wuffs_brotli__bit_cost__f64_from_u64(self, ((uint64_t)(a_a)));
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f64_to_u64_trunc
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__bit_cost__f64_to_u64_trunc(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint64_t a_a) {
|
||||
uint64_t v_exp = 0;
|
||||
|
||||
v_exp = (a_a >> 52u);
|
||||
if (v_exp < 1023u) {
|
||||
return 0u;
|
||||
} else if (v_exp <= 1075u) {
|
||||
return (((a_a & 4503599627370495u) | 4503599627370496u) >> ((1075u - v_exp) & 63u));
|
||||
}
|
||||
return wuffs_softfloat__arith__f64_to_u64_trunc(&self->private_data.f_sf, a_a);
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.f32_from_u32
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__bit_cost__f32_from_u32(
|
||||
const wuffs_brotli__bit_cost* self,
|
||||
uint32_t a_a) {
|
||||
uint32_t v_lz = 0;
|
||||
|
||||
if (a_a == 0u) {
|
||||
return 0u;
|
||||
} else if (a_a < 16777216u) {
|
||||
v_lz = wuffs_brotli__bit_cost__clz64(self, ((uint64_t)(a_a)));
|
||||
if (v_lz >= 40u) {
|
||||
return ((uint32_t)(((uint32_t)(((uint32_t)(189u - v_lz)) << 23u)) + ((uint32_t)(a_a << (v_lz - 40u)))));
|
||||
}
|
||||
}
|
||||
return wuffs_softfloat__arith__f32_from_u32(&self->private_data.f_sf, a_a);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hash_fast
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -93764,14 +94995,14 @@ wuffs_brotli__encoder__should_merge_block(
|
||||
v_i += 43u;
|
||||
}
|
||||
v_total = (((uint64_t)(a_len + 42u)) / 43u);
|
||||
v_r = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, wuffs_brotli__encoder__fast_log2(self, v_total), 4602678819172646912u), wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_total)), 4641240890982006784u);
|
||||
v_r = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, wuffs_brotli__encoder__fast_log2(self, v_total), 4602678819172646912u), wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_total)), 4641240890982006784u);
|
||||
v_j = 0u;
|
||||
while (v_j < 256u) {
|
||||
v_h = ((uint64_t)(self->private_data.f_histogram[v_j]));
|
||||
v_r = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_r, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_h), wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, ((uint32_t)(self->private_data.f_lit_depth[v_j]))), wuffs_brotli__encoder__fast_log2(self, v_h))));
|
||||
v_r = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_r, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_h), wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u32(&self->private_data.f_bc, ((uint32_t)(self->private_data.f_lit_depth[v_j]))), wuffs_brotli__encoder__fast_log2(self, v_h))));
|
||||
v_j += 1u;
|
||||
}
|
||||
return (wuffs_softfloat__arith__f64_ge(&self->private_data.f_sf, v_r, 0u) != 0u);
|
||||
return (wuffs_brotli__bit_cost__f64_ge(&self->private_data.f_bc, v_r, 0u) != 0u);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.should_use_uncompressed_mode
|
||||
@@ -94767,11 +95998,11 @@ wuffs_brotli__encoder__should_compress_two_pass(
|
||||
uint64_t v_max_total_bit_cost = 0;
|
||||
uint64_t v_i = 0;
|
||||
|
||||
v_corpus_size = wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_input_size);
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_num_literals), wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, 4607002274814922588u, v_corpus_size)) != 0u) {
|
||||
v_corpus_size = wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, a_input_size);
|
||||
if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, a_num_literals), wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, 4607002274814922588u, v_corpus_size)) != 0u) {
|
||||
return true;
|
||||
}
|
||||
v_max_total_bit_cost = wuffs_softfloat__arith__f64_div(&self->private_data.f_sf, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_corpus_size, 4620693217682128896u), 4607002274814922588u), 4631248529308778496u);
|
||||
v_max_total_bit_cost = wuffs_softfloat__arith__f64_div(&self->private_data.f_sf, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, v_corpus_size, 4620693217682128896u), 4607002274814922588u), 4631248529308778496u);
|
||||
wuffs_private_impl__bulk_memset(&self->private_data.f_histogram[0], 256u * (size_t)4u, 0u);
|
||||
v_i = 0u;
|
||||
while (v_i < a_input_size) {
|
||||
@@ -94780,7 +96011,7 @@ wuffs_brotli__encoder__should_compress_two_pass(
|
||||
}
|
||||
v_i += 43u;
|
||||
}
|
||||
return (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, wuffs_brotli__encoder__bits_entropy_of_histogram(self), v_max_total_bit_cost) != 0u);
|
||||
return (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, wuffs_brotli__encoder__bits_entropy_of_histogram(self), v_max_total_bit_cost) != 0u);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.emit_uncompressed_meta_block_two_pass
|
||||
@@ -95697,7 +96928,6 @@ wuffs_brotli__encoder__hasher_store(
|
||||
uint64_t v_bsize = 0;
|
||||
uint64_t v_off = 0;
|
||||
uint16_t v_num = 0;
|
||||
uint32_t v_hash = 0;
|
||||
uint64_t v_bank = 0;
|
||||
uint64_t v_idx = 0;
|
||||
uint64_t v_delta = 0;
|
||||
@@ -95705,6 +96935,10 @@ wuffs_brotli__encoder__hasher_store(
|
||||
uint32_t v_fc_bits = 0;
|
||||
|
||||
v_htype = self->private_impl.f_hasher_type;
|
||||
if ((v_htype == 58u) || (v_htype == 68u)) {
|
||||
wuffs_brotli__encoder__hasher_store_h58(self, a_wb, a_ix);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
if (self->private_impl.f_rb_buf_off > ((uint64_t)(a_wb.len))) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
@@ -95723,14 +96957,6 @@ wuffs_brotli__encoder__hasher_store(
|
||||
v_off = ((uint64_t)((((uint64_t)(v_num)) & ((((uint64_t)(1u)) << self->private_impl.f_h_block_bits) - 1u)) + (((uint64_t)(v_key)) << self->private_impl.f_h_block_bits)));
|
||||
wuffs_brotli__encoder__poke_u16(self, a_wb, ((uint64_t)(self->private_impl.f_h_off0 + ((uint64_t)(2u * ((uint64_t)(v_key)))))), ((uint16_t)(v_num + 1u)));
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(self->private_impl.f_h_off1 + ((uint64_t)(4u * v_off)))), ((uint32_t)(a_ix)));
|
||||
} else if ((v_htype == 58u) || (v_htype == 68u)) {
|
||||
v_hash = wuffs_brotli__encoder__hash_longest(self, v_data, (a_ix & ((uint64_t)(self->private_impl.f_rb_mask))));
|
||||
v_key = (v_hash >> 8u);
|
||||
v_num = wuffs_brotli__encoder__peek_u16(self, a_wb, ((uint64_t)(self->private_impl.f_h_off0 + ((uint64_t)(2u * ((uint64_t)(v_key)))))));
|
||||
v_off = ((uint64_t)((((uint64_t)(v_num)) & ((((uint64_t)(1u)) << self->private_impl.f_h_block_bits) - 1u)) + (((uint64_t)(v_key)) << self->private_impl.f_h_block_bits)));
|
||||
wuffs_brotli__encoder__poke_u16(self, a_wb, ((uint64_t)(self->private_impl.f_h_off0 + ((uint64_t)(2u * ((uint64_t)(v_key)))))), ((uint16_t)(v_num - 1u)));
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(self->private_impl.f_h_off2 + ((uint64_t)(4u * v_off)))), ((uint32_t)(a_ix)));
|
||||
wuffs_brotli__encoder__set_byte(self, a_wb, ((uint64_t)(self->private_impl.f_h_off1 + v_off)), ((uint8_t)(v_hash)));
|
||||
} else if ((v_htype == 40u) || (v_htype == 41u) || (v_htype == 42u)) {
|
||||
v_key = wuffs_brotli__encoder__hash_forgetful(self, v_data, (a_ix & ((uint64_t)(self->private_impl.f_rb_mask))));
|
||||
v_fc_bits = wuffs_brotli__encoder__fc_bank_bits(self);
|
||||
@@ -95759,6 +96985,48 @@ wuffs_brotli__encoder__hasher_store(
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hasher_store_h58
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_store_h58(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_ix) {
|
||||
uint32_t v_hash = 0;
|
||||
uint64_t v_key = 0;
|
||||
uint64_t v_koff = 0;
|
||||
uint16_t v_num = 0;
|
||||
uint64_t v_off = 0;
|
||||
|
||||
v_hash = wuffs_brotli__encoder__hash_h58(self, a_wb, a_ix);
|
||||
v_key = ((uint64_t)((v_hash >> 8u)));
|
||||
v_koff = ((uint64_t)(self->private_impl.f_h_off0 + ((uint64_t)(2u * v_key))));
|
||||
v_num = wuffs_brotli__encoder__peek_u16(self, a_wb, v_koff);
|
||||
v_off = ((uint64_t)((((uint64_t)(v_num)) & ((((uint64_t)(1u)) << self->private_impl.f_h_block_bits) - 1u)) + (v_key << self->private_impl.f_h_block_bits)));
|
||||
wuffs_brotli__encoder__poke_u16(self, a_wb, v_koff, ((uint16_t)(v_num - 1u)));
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(self->private_impl.f_h_off2 + ((uint64_t)(4u * v_off)))), ((uint32_t)(a_ix)));
|
||||
wuffs_brotli__encoder__set_byte(self, a_wb, ((uint64_t)(self->private_impl.f_h_off1 + v_off)), ((uint8_t)(v_hash)));
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hash_h58
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__encoder__hash_h58(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_ix) {
|
||||
uint64_t v_i = 0;
|
||||
|
||||
v_i = ((uint64_t)(self->private_impl.f_rb_buf_off + (a_ix & ((uint64_t)(self->private_impl.f_rb_mask)))));
|
||||
if (self->private_impl.f_hasher_type == 58u) {
|
||||
return (((uint32_t)(wuffs_brotli__encoder__load32le(self, a_wb, v_i) * 506832829u)) >> ((24u - self->private_impl.f_h_bucket_bits) & 31u));
|
||||
}
|
||||
return ((uint32_t)((((uint64_t)(wuffs_brotli__encoder__load64le(self, a_wb, v_i) * 8922571613522624512u)) >> 41u)));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hasher_store_range
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -96239,6 +97507,8 @@ wuffs_brotli__encoder__find_last_distances(
|
||||
uint64_t v_prev_ix = 0;
|
||||
uint64_t v_len = 0;
|
||||
uint64_t v_score = 0;
|
||||
uint8_t v_cur_byte = 0;
|
||||
uint32_t v_n = 0;
|
||||
|
||||
v_mask = ((uint64_t)(self->private_impl.f_rb_mask));
|
||||
v_cur_ix_masked = (a_cur_ix & v_mask);
|
||||
@@ -96246,15 +97516,13 @@ wuffs_brotli__encoder__find_last_distances(
|
||||
v_best_len = self->private_impl.f_sr_len;
|
||||
self->private_impl.f_sr_len = 0u;
|
||||
self->private_impl.f_sr_len_code_delta = 0u;
|
||||
v_cur_byte = wuffs_brotli__encoder__byte_at(self, a_data, ((uint64_t)(v_cur_ix_masked + v_best_len)));
|
||||
v_n = self->private_impl.f_h_num_last_distances;
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_impl.f_h_num_last_distances) {
|
||||
while (v_i < v_n) {
|
||||
v_backward = wuffs_brotli__encoder__dist_cache_u64(self, v_i);
|
||||
v_prev_ix = ((uint64_t)(a_cur_ix - v_backward));
|
||||
if (v_prev_ix >= a_cur_ix) {
|
||||
v_i += 1u;
|
||||
continue;
|
||||
}
|
||||
if (v_backward > a_max_backward) {
|
||||
if ((v_prev_ix >= a_cur_ix) || (v_backward > a_max_backward)) {
|
||||
v_i += 1u;
|
||||
continue;
|
||||
}
|
||||
@@ -96262,7 +97530,7 @@ wuffs_brotli__encoder__find_last_distances(
|
||||
if (((uint64_t)(v_cur_ix_masked + v_best_len)) > v_mask) {
|
||||
break;
|
||||
}
|
||||
if ((((uint64_t)(v_prev_ix + v_best_len)) > v_mask) || (wuffs_brotli__encoder__byte_at(self, a_data, ((uint64_t)(v_cur_ix_masked + v_best_len))) != wuffs_brotli__encoder__byte_at(self, a_data, ((uint64_t)(v_prev_ix + v_best_len))))) {
|
||||
if ((((uint64_t)(v_prev_ix + v_best_len)) > v_mask) || (v_cur_byte != wuffs_brotli__encoder__byte_at(self, a_data, ((uint64_t)(v_prev_ix + v_best_len))))) {
|
||||
v_i += 1u;
|
||||
continue;
|
||||
}
|
||||
@@ -96283,6 +97551,7 @@ wuffs_brotli__encoder__find_last_distances(
|
||||
self->private_impl.f_sr_len = v_best_len;
|
||||
self->private_impl.f_sr_distance = v_backward;
|
||||
self->private_impl.f_sr_score = v_best_score;
|
||||
v_cur_byte = wuffs_brotli__encoder__byte_at(self, a_data, ((uint64_t)(v_cur_ix_masked + v_best_len)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96291,67 +97560,6 @@ wuffs_brotli__encoder__find_last_distances(
|
||||
return v_best_len;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.try_bucket_entry
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__try_bucket_entry(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
uint64_t a_cur_ix,
|
||||
uint64_t a_prev_ix_in,
|
||||
uint64_t a_best_len,
|
||||
uint64_t a_max_length,
|
||||
uint64_t a_max_backward) {
|
||||
uint64_t v_mask = 0;
|
||||
uint64_t v_cur_ix_masked = 0;
|
||||
uint64_t v_prev_ix = 0;
|
||||
uint64_t v_backward = 0;
|
||||
uint64_t v_len = 0;
|
||||
uint64_t v_score = 0;
|
||||
|
||||
v_mask = ((uint64_t)(self->private_impl.f_rb_mask));
|
||||
v_cur_ix_masked = (a_cur_ix & v_mask);
|
||||
v_backward = ((uint64_t)(a_cur_ix - a_prev_ix_in));
|
||||
if (v_backward > a_max_backward) {
|
||||
return 1u;
|
||||
}
|
||||
v_prev_ix = (a_prev_ix_in & v_mask);
|
||||
if (((uint64_t)(v_cur_ix_masked + a_best_len)) > v_mask) {
|
||||
return 1u;
|
||||
}
|
||||
if ((((uint64_t)(v_prev_ix + a_best_len)) > v_mask) || (wuffs_brotli__encoder__load32le(self, a_data, ((uint64_t)(((uint64_t)(v_cur_ix_masked + a_best_len)) - 3u))) != wuffs_brotli__encoder__load32le(self, a_data, ((uint64_t)(((uint64_t)(v_prev_ix + a_best_len)) - 3u))))) {
|
||||
return 0u;
|
||||
}
|
||||
if ((self->private_impl.f_hasher_type == 6u) || (self->private_impl.f_hasher_type == 68u)) {
|
||||
if (wuffs_brotli__encoder__load32le(self, a_data, v_cur_ix_masked) != wuffs_brotli__encoder__load32le(self, a_data, v_prev_ix)) {
|
||||
return 0u;
|
||||
}
|
||||
v_len = ((uint64_t)(wuffs_brotli__encoder__find_match_length_with_limit(self,
|
||||
a_data,
|
||||
((uint64_t)(v_prev_ix + 4u)),
|
||||
((uint64_t)(v_cur_ix_masked + 4u)),
|
||||
((uint64_t)(a_max_length - 4u))) + 4u));
|
||||
} else {
|
||||
v_len = wuffs_brotli__encoder__find_match_length_with_limit(self,
|
||||
a_data,
|
||||
v_prev_ix,
|
||||
v_cur_ix_masked,
|
||||
a_max_length);
|
||||
if (v_len < 4u) {
|
||||
return 0u;
|
||||
}
|
||||
}
|
||||
v_score = wuffs_brotli__encoder__backward_reference_score(self, v_len, v_backward);
|
||||
if (self->private_impl.f_sr_score < v_score) {
|
||||
self->private_impl.f_sr_len = v_len;
|
||||
self->private_impl.f_sr_distance = v_backward;
|
||||
self->private_impl.f_sr_score = v_score;
|
||||
return ((uint64_t)(2u + v_len));
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.find_longest_match_h5
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -96375,7 +97583,13 @@ wuffs_brotli__encoder__find_longest_match_h5(
|
||||
uint64_t v_bucket = 0;
|
||||
uint64_t v_down = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_r = 0;
|
||||
uint64_t v_mask = 0;
|
||||
bool v_is64 = false;
|
||||
uint32_t v_cur4 = 0;
|
||||
uint64_t v_prev_ix = 0;
|
||||
uint64_t v_backward = 0;
|
||||
uint64_t v_len = 0;
|
||||
uint64_t v_score = 0;
|
||||
|
||||
if (self->private_impl.f_rb_buf_off > ((uint64_t)(a_wb.len))) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
@@ -96400,20 +97614,50 @@ wuffs_brotli__encoder__find_longest_match_h5(
|
||||
if (((uint64_t)(v_num)) > v_block_size) {
|
||||
v_down = ((uint64_t)(((uint64_t)(v_num)) - v_block_size));
|
||||
}
|
||||
v_mask = ((uint64_t)(self->private_impl.f_rb_mask));
|
||||
v_is64 = (self->private_impl.f_hasher_type == 6u);
|
||||
v_cur4 = wuffs_brotli__encoder__load32le(self, v_data, ((uint64_t)(((uint64_t)(v_cur_ix_masked + v_best_len)) - 3u)));
|
||||
v_i = ((uint64_t)(v_num));
|
||||
while (v_i > v_down) {
|
||||
v_i -= 1u;
|
||||
v_r = wuffs_brotli__encoder__try_bucket_entry(self,
|
||||
v_data,
|
||||
a_cur_ix,
|
||||
((uint64_t)(wuffs_brotli__encoder__load32le(self, a_wb, ((uint64_t)(v_bucket + ((uint64_t)(4u * (v_i & v_block_mask)))))))),
|
||||
v_best_len,
|
||||
a_max_length,
|
||||
a_max_backward);
|
||||
if (v_r == 1u) {
|
||||
v_prev_ix = ((uint64_t)(wuffs_brotli__encoder__load32le(self, a_wb, ((uint64_t)(v_bucket + ((uint64_t)(4u * (v_i & v_block_mask))))))));
|
||||
v_backward = ((uint64_t)(a_cur_ix - v_prev_ix));
|
||||
if (v_backward > a_max_backward) {
|
||||
break;
|
||||
} else if (v_r >= 2u) {
|
||||
v_best_len = ((uint64_t)(v_r - 2u));
|
||||
}
|
||||
v_prev_ix &= v_mask;
|
||||
if (((uint64_t)(v_cur_ix_masked + v_best_len)) > v_mask) {
|
||||
break;
|
||||
}
|
||||
if ((((uint64_t)(v_prev_ix + v_best_len)) > v_mask) || (v_cur4 != wuffs_brotli__encoder__load32le(self, v_data, ((uint64_t)(((uint64_t)(v_prev_ix + v_best_len)) - 3u))))) {
|
||||
continue;
|
||||
}
|
||||
if (v_is64) {
|
||||
if (wuffs_brotli__encoder__load32le(self, v_data, v_cur_ix_masked) != wuffs_brotli__encoder__load32le(self, v_data, v_prev_ix)) {
|
||||
continue;
|
||||
}
|
||||
v_len = ((uint64_t)(wuffs_brotli__encoder__find_match_length_with_limit(self,
|
||||
v_data,
|
||||
((uint64_t)(v_prev_ix + 4u)),
|
||||
((uint64_t)(v_cur_ix_masked + 4u)),
|
||||
((uint64_t)(a_max_length - 4u))) + 4u));
|
||||
} else {
|
||||
v_len = wuffs_brotli__encoder__find_match_length_with_limit(self,
|
||||
v_data,
|
||||
v_prev_ix,
|
||||
v_cur_ix_masked,
|
||||
a_max_length);
|
||||
if (v_len < 4u) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
v_score = wuffs_brotli__encoder__backward_reference_score(self, v_len, v_backward);
|
||||
if (self->private_impl.f_sr_score < v_score) {
|
||||
self->private_impl.f_sr_len = v_len;
|
||||
self->private_impl.f_sr_distance = v_backward;
|
||||
self->private_impl.f_sr_score = v_score;
|
||||
v_best_len = v_len;
|
||||
v_cur4 = wuffs_brotli__encoder__load32le(self, v_data, ((uint64_t)(((uint64_t)(v_cur_ix_masked + v_best_len)) - 3u)));
|
||||
}
|
||||
}
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_bucket + ((uint64_t)(4u * (((uint64_t)(v_num)) & v_block_mask))))), ((uint32_t)(a_cur_ix)));
|
||||
@@ -96459,7 +97703,17 @@ wuffs_brotli__encoder__find_longest_match_h58(
|
||||
uint64_t v_limit = 0;
|
||||
uint64_t v_k = 0;
|
||||
uint64_t v_rb_index = 0;
|
||||
uint64_t v_r = 0;
|
||||
uint64_t v_splat = 0;
|
||||
uint64_t v_matches = 0;
|
||||
uint64_t v_chunk = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint64_t v_mask = 0;
|
||||
bool v_is64 = false;
|
||||
uint32_t v_cur4 = 0;
|
||||
uint64_t v_prev_ix = 0;
|
||||
uint64_t v_backward = 0;
|
||||
uint64_t v_len = 0;
|
||||
uint64_t v_score = 0;
|
||||
|
||||
if (self->private_impl.f_rb_buf_off > ((uint64_t)(a_wb.len))) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
@@ -96467,7 +97721,7 @@ wuffs_brotli__encoder__find_longest_match_h58(
|
||||
v_data = wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_rb_buf_off);
|
||||
v_cur_ix_masked = (a_cur_ix & ((uint64_t)(self->private_impl.f_rb_mask)));
|
||||
v_min_score = self->private_impl.f_sr_score;
|
||||
v_hash = wuffs_brotli__encoder__hash_longest(self, v_data, v_cur_ix_masked);
|
||||
v_hash = wuffs_brotli__encoder__hash_h58(self, a_wb, a_cur_ix);
|
||||
v_key = ((uint64_t)((v_hash >> 8u)));
|
||||
v_block_size = (((uint64_t)(1u)) << self->private_impl.f_h_block_bits);
|
||||
v_block_mask = ((uint64_t)(v_block_size - 1u));
|
||||
@@ -96489,24 +97743,68 @@ wuffs_brotli__encoder__find_longest_match_h58(
|
||||
if (v_block_size > ((uint64_t)(v_n))) {
|
||||
v_limit = ((uint64_t)(v_n));
|
||||
}
|
||||
v_k = 0u;
|
||||
while (v_k < v_limit) {
|
||||
v_splat = ((uint64_t)(((uint64_t)(v_tag)) * 72340172838076673u));
|
||||
v_matches = 0u;
|
||||
v_j = 0u;
|
||||
while (v_j < v_block_size) {
|
||||
v_chunk = (wuffs_brotli__encoder__load64le(self, a_wb, ((uint64_t)(v_tag_bucket + v_j))) ^ v_splat);
|
||||
v_chunk = ((((uint64_t)((v_chunk | 9259542123273814144u) - 72340172838076673u)) | v_chunk) & 9259542123273814144u);
|
||||
v_chunk = ((v_chunk ^ 9259542123273814144u) >> 7u);
|
||||
v_matches |= ((uint64_t)((((uint64_t)(v_chunk * 72624976668147840u)) >> 56u) << (v_j & 63u)));
|
||||
v_j += 8u;
|
||||
}
|
||||
if (v_head > 0u) {
|
||||
v_matches = (((v_matches >> (v_head & 63u)) | ((uint64_t)(v_matches << (((uint64_t)(v_block_size - v_head)) & 63u)))) & (((uint64_t)(18446744073709551615u)) >> (((uint64_t)(64u - v_block_size)) & 63u)));
|
||||
}
|
||||
if (v_limit < 64u) {
|
||||
v_matches &= ((uint64_t)((((uint64_t)(1u)) << (v_limit & 63u)) - 1u));
|
||||
}
|
||||
v_mask = ((uint64_t)(self->private_impl.f_rb_mask));
|
||||
v_is64 = (self->private_impl.f_hasher_type == 68u);
|
||||
v_cur4 = wuffs_brotli__encoder__load32le(self, v_data, ((uint64_t)(((uint64_t)(v_cur_ix_masked + v_best_len)) - 3u)));
|
||||
while (v_matches != 0u) {
|
||||
v_k = wuffs_brotli__encoder__ctz64(self, v_matches);
|
||||
v_matches &= ((uint64_t)(v_matches - 1u));
|
||||
v_rb_index = (((uint64_t)(v_head + v_k)) & v_block_mask);
|
||||
if (wuffs_brotli__encoder__byte_at(self, a_wb, ((uint64_t)(v_tag_bucket + v_rb_index))) == v_tag) {
|
||||
v_r = wuffs_brotli__encoder__try_bucket_entry(self,
|
||||
v_prev_ix = ((uint64_t)(wuffs_brotli__encoder__load32le(self, a_wb, ((uint64_t)(v_bucket + ((uint64_t)(4u * v_rb_index)))))));
|
||||
v_backward = ((uint64_t)(a_cur_ix - v_prev_ix));
|
||||
if (v_backward > a_max_backward) {
|
||||
break;
|
||||
}
|
||||
v_prev_ix &= v_mask;
|
||||
if (((uint64_t)(v_cur_ix_masked + v_best_len)) > v_mask) {
|
||||
break;
|
||||
}
|
||||
if ((((uint64_t)(v_prev_ix + v_best_len)) > v_mask) || (v_cur4 != wuffs_brotli__encoder__load32le(self, v_data, ((uint64_t)(((uint64_t)(v_prev_ix + v_best_len)) - 3u))))) {
|
||||
continue;
|
||||
}
|
||||
if (v_is64) {
|
||||
if (wuffs_brotli__encoder__load32le(self, v_data, v_cur_ix_masked) != wuffs_brotli__encoder__load32le(self, v_data, v_prev_ix)) {
|
||||
continue;
|
||||
}
|
||||
v_len = ((uint64_t)(wuffs_brotli__encoder__find_match_length_with_limit(self,
|
||||
v_data,
|
||||
a_cur_ix,
|
||||
((uint64_t)(wuffs_brotli__encoder__load32le(self, a_wb, ((uint64_t)(v_bucket + ((uint64_t)(4u * v_rb_index))))))),
|
||||
v_best_len,
|
||||
a_max_length,
|
||||
a_max_backward);
|
||||
if (v_r == 1u) {
|
||||
break;
|
||||
} else if (v_r >= 2u) {
|
||||
v_best_len = ((uint64_t)(v_r - 2u));
|
||||
((uint64_t)(v_prev_ix + 4u)),
|
||||
((uint64_t)(v_cur_ix_masked + 4u)),
|
||||
((uint64_t)(a_max_length - 4u))) + 4u));
|
||||
} else {
|
||||
v_len = wuffs_brotli__encoder__find_match_length_with_limit(self,
|
||||
v_data,
|
||||
v_prev_ix,
|
||||
v_cur_ix_masked,
|
||||
a_max_length);
|
||||
if (v_len < 4u) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
v_k += 1u;
|
||||
v_score = wuffs_brotli__encoder__backward_reference_score(self, v_len, v_backward);
|
||||
if (self->private_impl.f_sr_score < v_score) {
|
||||
self->private_impl.f_sr_len = v_len;
|
||||
self->private_impl.f_sr_distance = v_backward;
|
||||
self->private_impl.f_sr_score = v_score;
|
||||
v_best_len = v_len;
|
||||
v_cur4 = wuffs_brotli__encoder__load32le(self, v_data, ((uint64_t)(((uint64_t)(v_cur_ix_masked + v_best_len)) - 3u)));
|
||||
}
|
||||
}
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_bucket + ((uint64_t)(4u * (((uint64_t)(v_num)) & v_block_mask))))), ((uint32_t)(a_cur_ix)));
|
||||
wuffs_brotli__encoder__set_byte(self, a_wb, ((uint64_t)(v_tag_bucket + (((uint64_t)(v_num)) & v_block_mask))), v_tag);
|
||||
@@ -97214,7 +98512,7 @@ wuffs_brotli__encoder__is_mostly_utf8(
|
||||
v_size_utf8 += v_bytes_read;
|
||||
}
|
||||
}
|
||||
return (wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, v_size_utf8), wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, 4604930618986332160u, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_length))) != 0u);
|
||||
return (wuffs_brotli__bit_cost__f64_gt(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, v_size_utf8), wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, 4604930618986332160u, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, a_length))) != 0u);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.utf8_position
|
||||
@@ -97288,12 +98586,12 @@ wuffs_brotli__encoder__lc_finish_cost(
|
||||
uint64_t v_lit_cost = 0;
|
||||
|
||||
v_lit_cost = a_lit_cost;
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_lit_cost, 4607182418800017408u) != 0u) {
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, v_lit_cost, 4602678819172646912u);
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_lit_cost, 4602678819172646912u);
|
||||
if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, v_lit_cost, 4607182418800017408u) != 0u) {
|
||||
v_lit_cost = wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, v_lit_cost, 4602678819172646912u);
|
||||
v_lit_cost = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_lit_cost, 4602678819172646912u);
|
||||
}
|
||||
if (a_utf8 && (a_i < 2000u)) {
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_lit_cost, wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, 4599976659396224614u, wuffs_softfloat__arith__f64_mul(&self->private_data.f_sf, 4550588384442628807u, wuffs_softfloat__arith__f64_from_u64(&self->private_data.f_sf, a_i))));
|
||||
v_lit_cost = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_lit_cost, wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, 4599976659396224614u, wuffs_brotli__bit_cost__f64_mul(&self->private_data.f_bc, 4550588384442628807u, wuffs_brotli__bit_cost__f64_from_u64(&self->private_data.f_bc, a_i))));
|
||||
}
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(a_cost_off + ((uint64_t)(a_i * 4u)))), wuffs_softfloat__arith__f64_to_f32(&self->private_data.f_sf, v_lit_cost));
|
||||
return wuffs_base__make_empty_struct();
|
||||
@@ -97408,8 +98706,8 @@ wuffs_brotli__encoder__estimate_bit_costs_for_literals_utf8(
|
||||
v_iw = v_iwu2;
|
||||
}
|
||||
v_lit_cost = wuffs_brotli__bit_cost__fast_log2_memo(&self->private_data.f_bc, v_iw);
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_lit_cost, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_histo));
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_lit_cost, 4584030313835631162u);
|
||||
v_lit_cost = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_lit_cost, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_histo));
|
||||
v_lit_cost = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_lit_cost, 4584030313835631162u);
|
||||
wuffs_brotli__encoder__lc_finish_cost(self,
|
||||
a_wb,
|
||||
a_cost_off,
|
||||
@@ -97480,8 +98778,8 @@ wuffs_brotli__encoder__estimate_bit_costs_for_literals(
|
||||
v_histo = 1u;
|
||||
}
|
||||
v_lit_cost = wuffs_brotli__bit_cost__fast_log2_memo(&self->private_data.f_bc, v_in_window);
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_lit_cost, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_histo));
|
||||
v_lit_cost = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_lit_cost, 4584015902316823577u);
|
||||
v_lit_cost = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_lit_cost, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_histo));
|
||||
v_lit_cost = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_lit_cost, 4584015902316823577u);
|
||||
wuffs_brotli__encoder__lc_finish_cost(self,
|
||||
a_wb,
|
||||
a_cost_off,
|
||||
@@ -97831,16 +99129,14 @@ wuffs_brotli__metablock_builder__log2_floor_non_zero(
|
||||
const wuffs_brotli__metablock_builder* self,
|
||||
uint64_t a_n) {
|
||||
uint32_t v_x = 0;
|
||||
uint32_t v_r = 0;
|
||||
|
||||
v_x = ((uint32_t)(a_n));
|
||||
while (v_x > 1u) {
|
||||
v_x >>= 1u;
|
||||
if (v_r < 31u) {
|
||||
v_r += 1u;
|
||||
}
|
||||
}
|
||||
return v_r;
|
||||
v_x |= (v_x >> 1u);
|
||||
v_x |= (v_x >> 2u);
|
||||
v_x |= (v_x >> 4u);
|
||||
v_x |= (v_x >> 8u);
|
||||
v_x |= (v_x >> 16u);
|
||||
return WUFFS_BROTLI__LOG2_DEBRUIJN32[(((uint32_t)(v_x * 130329821u)) >> 27u)];
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_builder.prefix_encode_copy_distance
|
||||
@@ -98070,12 +99366,12 @@ wuffs_brotli__metablock_builder__compute_distance_cost(
|
||||
a_tmp,
|
||||
544u,
|
||||
((uint64_t)((v_dist_prefix & 1023u))));
|
||||
v_extra_bits = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_extra_bits, wuffs_softfloat__arith__f64_from_u32(&self->private_data.f_sf, (v_dist_prefix >> 10u)));
|
||||
v_extra_bits = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_extra_bits, wuffs_brotli__bit_cost__f64_from_u32(&self->private_data.f_bc, (v_dist_prefix >> 10u)));
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
self->private_impl.f_dist_cost = wuffs_brotli__metablock_builder__h_population_cost(self, a_wb, a_tmp, 544u);
|
||||
self->private_impl.f_dist_cost = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, self->private_impl.f_dist_cost, v_extra_bits);
|
||||
self->private_impl.f_dist_cost = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, self->private_impl.f_dist_cost, v_extra_bits);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -98229,7 +99525,7 @@ wuffs_brotli__metablock_builder__build_meta_block(
|
||||
v_ndirect,
|
||||
self->private_impl.f_dist_max_distance,
|
||||
v_tmp);
|
||||
if ( ! v_success || (wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, self->private_impl.f_dist_cost, v_best_dist_cost) != 0u)) {
|
||||
if ( ! v_success || (wuffs_brotli__bit_cost__f64_gt(&self->private_data.f_bc, self->private_impl.f_dist_cost, v_best_dist_cost) != 0u)) {
|
||||
break;
|
||||
}
|
||||
v_best_dist_cost = self->private_impl.f_dist_cost;
|
||||
@@ -98257,7 +99553,7 @@ wuffs_brotli__metablock_builder__build_meta_block(
|
||||
v_orig_ndirect,
|
||||
v_orig_md,
|
||||
v_tmp);
|
||||
if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, self->private_impl.f_dist_cost, v_best_dist_cost) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, self->private_impl.f_dist_cost, v_best_dist_cost) != 0u) {
|
||||
v_best_npostfix = v_orig_npostfix;
|
||||
v_best_ndirect = v_orig_ndirect;
|
||||
v_best_asm = v_orig_asm;
|
||||
@@ -98943,25 +100239,25 @@ wuffs_brotli__encoder__gbs_finish_block(
|
||||
v_data_size);
|
||||
}
|
||||
self->private_data.f_gbs_comb_entropy[(v_jx & 31u)] = wuffs_brotli__encoder__gbs_entropy(self, a_wb, v_tmp, v_alphabet_size);
|
||||
v_d = wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, self->private_data.f_gbs_comb_entropy[(v_jx & 31u)], v_e), self->private_data.f_gbs_last_entropy[(((uint64_t)(((uint64_t)(v_le_base)) + v_jx)) & 127u)]);
|
||||
v_d = wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, self->private_data.f_gbs_comb_entropy[(v_jx & 31u)], v_e), self->private_data.f_gbs_last_entropy[(((uint64_t)(((uint64_t)(v_le_base)) + v_jx)) & 127u)]);
|
||||
if (v_j == 0u) {
|
||||
if (v_nc == 1u) {
|
||||
v_diff0 = v_d;
|
||||
} else {
|
||||
v_diff0 = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_diff0, v_d);
|
||||
v_diff0 = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_diff0, v_d);
|
||||
}
|
||||
} else {
|
||||
if (v_nc == 1u) {
|
||||
v_diff1 = v_d;
|
||||
} else {
|
||||
v_diff1 = wuffs_softfloat__arith__f64_add(&self->private_data.f_sf, v_diff1, v_d);
|
||||
v_diff1 = wuffs_brotli__bit_cost__f64_add(&self->private_data.f_bc, v_diff1, v_d);
|
||||
}
|
||||
}
|
||||
v_j += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
if ((self->private_data.f_gbs_num_types[a_c] < self->private_data.f_gbs_max_block_types[a_c]) && (wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, v_diff0, v_threshold) != 0u) && (wuffs_softfloat__arith__f64_gt(&self->private_data.f_sf, v_diff1, v_threshold) != 0u)) {
|
||||
if ((self->private_data.f_gbs_num_types[a_c] < self->private_data.f_gbs_max_block_types[a_c]) && (wuffs_brotli__bit_cost__f64_gt(&self->private_data.f_bc, v_diff0, v_threshold) != 0u) && (wuffs_brotli__bit_cost__f64_gt(&self->private_data.f_bc, v_diff1, v_threshold) != 0u)) {
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_lengths_off + ((uint64_t)(v_num_blocks * 4u)))), ((uint32_t)(self->private_data.f_gbs_block_size[a_c])));
|
||||
wuffs_brotli__encoder__set_byte(self, a_wb, ((uint64_t)(v_types_off + v_num_blocks)), ((uint8_t)(self->private_data.f_gbs_num_types[a_c])));
|
||||
self->private_data.f_gbs_last_histogram_ix[((a_c * 2u) + 1u)] = v_last0;
|
||||
@@ -98982,7 +100278,7 @@ wuffs_brotli__encoder__gbs_finish_block(
|
||||
self->private_data.f_gbs_block_size[a_c] = 0u;
|
||||
self->private_data.f_gbs_merge_last_count[a_c] = 0u;
|
||||
self->private_data.f_gbs_target_block_size[a_c] = ((uint64_t)(self->private_data.f_gbs_min_block_size[a_c]));
|
||||
} else if (wuffs_softfloat__arith__f64_lt(&self->private_data.f_sf, v_diff1, wuffs_softfloat__arith__f64_sub(&self->private_data.f_sf, v_diff0, 4626322717216342016u)) != 0u) {
|
||||
} else if (wuffs_brotli__bit_cost__f64_lt(&self->private_data.f_bc, v_diff1, wuffs_brotli__bit_cost__f64_sub(&self->private_data.f_bc, v_diff0, 4626322717216342016u)) != 0u) {
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_lengths_off + ((uint64_t)(v_num_blocks * 4u)))), ((uint32_t)(self->private_data.f_gbs_block_size[a_c])));
|
||||
wuffs_brotli__encoder__set_byte(self, a_wb, ((uint64_t)(v_types_off + v_num_blocks)), wuffs_brotli__encoder__byte_at(self, a_wb, ((uint64_t)(v_types_off + ((uint64_t)(v_num_blocks - 2u))))));
|
||||
self->private_data.f_gbs_last_histogram_ix[(a_c * 2u)] = v_last1;
|
||||
@@ -100694,6 +101990,16 @@ wuffs_brotli__metablock_tester__get(
|
||||
return 0u;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.ctz64
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__ctz64(
|
||||
const wuffs_brotli__encoder* self,
|
||||
uint64_t a_x) {
|
||||
return ((uint64_t)(WUFFS_BROTLI__CTZ64_DEBRUIJN[(((uint64_t)((a_x & ((uint64_t)(0u - a_x))) * 285870213051386505u)) >> 58u)]));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.byte_at
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -100716,12 +102022,11 @@ wuffs_brotli__encoder__peek_u16(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_s,
|
||||
uint64_t a_i) {
|
||||
wuffs_base__slice_u8 v_q = {0};
|
||||
|
||||
if (a_i < ((uint64_t)(a_s.len))) {
|
||||
v_q = wuffs_base__slice_u8__subslice_i(a_s, a_i);
|
||||
if (((uint64_t)(v_q.len)) >= 2u) {
|
||||
return wuffs_base__peek_u16le__no_bounds_check(v_q.ptr);
|
||||
if (a_i < 18446744073709486080u) {
|
||||
if ((a_i + 2u) <= ((uint64_t)(a_s.len))) {
|
||||
return wuffs_base__peek_u16le__no_bounds_check(wuffs_base__slice_u8__subslice_ij(a_s,
|
||||
a_i,
|
||||
(a_i + 2u)).ptr);
|
||||
}
|
||||
}
|
||||
return 0u;
|
||||
@@ -100736,12 +102041,11 @@ wuffs_brotli__encoder__poke_u16(
|
||||
wuffs_base__slice_u8 a_s,
|
||||
uint64_t a_i,
|
||||
uint16_t a_v) {
|
||||
wuffs_base__slice_u8 v_q = {0};
|
||||
|
||||
if (a_i < ((uint64_t)(a_s.len))) {
|
||||
v_q = wuffs_base__slice_u8__subslice_i(a_s, a_i);
|
||||
if (((uint64_t)(v_q.len)) >= 2u) {
|
||||
wuffs_base__poke_u16le__no_bounds_check(v_q.ptr, a_v);
|
||||
if (a_i < 18446744073709486080u) {
|
||||
if ((a_i + 2u) <= ((uint64_t)(a_s.len))) {
|
||||
wuffs_base__poke_u16le__no_bounds_check(wuffs_base__slice_u8__subslice_ij(a_s,
|
||||
a_i,
|
||||
(a_i + 2u)).ptr, a_v);
|
||||
}
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
@@ -100756,12 +102060,11 @@ wuffs_brotli__encoder__poke_u32(
|
||||
wuffs_base__slice_u8 a_s,
|
||||
uint64_t a_i,
|
||||
uint32_t a_v) {
|
||||
wuffs_base__slice_u8 v_q = {0};
|
||||
|
||||
if (a_i < ((uint64_t)(a_s.len))) {
|
||||
v_q = wuffs_base__slice_u8__subslice_i(a_s, a_i);
|
||||
if (((uint64_t)(v_q.len)) >= 4u) {
|
||||
wuffs_base__poke_u32le__no_bounds_check(v_q.ptr, a_v);
|
||||
if (a_i < 18446744073709486080u) {
|
||||
if ((a_i + 4u) <= ((uint64_t)(a_s.len))) {
|
||||
wuffs_base__poke_u32le__no_bounds_check(wuffs_base__slice_u8__subslice_ij(a_s,
|
||||
a_i,
|
||||
(a_i + 4u)).ptr, a_v);
|
||||
}
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
@@ -101148,15 +102451,15 @@ wuffs_brotli__encoder__z_set_cost_from_histogram(
|
||||
v_i += 1u;
|
||||
}
|
||||
}
|
||||
v_missing_symbol_cost = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, wuffs_softfloat__arith__f64_to_f32(&self->private_data.f_sf, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_missing_symbol_sum)), 1073741824u);
|
||||
v_missing_symbol_cost = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, wuffs_softfloat__arith__f64_to_f32(&self->private_data.f_sf, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, v_missing_symbol_sum)), 1073741824u);
|
||||
v_i = 0u;
|
||||
while (v_i < a_histogram_size) {
|
||||
v_h = wuffs_brotli__encoder__z_hist(self, a_which, v_i);
|
||||
if (v_h == 0u) {
|
||||
wuffs_brotli__encoder__z_set_cost(self, a_which, v_i, v_missing_symbol_cost);
|
||||
} else {
|
||||
v_cost = wuffs_softfloat__arith__f32_sub(&self->private_data.f_sf, v_log2sum, wuffs_softfloat__arith__f64_to_f32(&self->private_data.f_sf, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, ((uint64_t)(v_h)))));
|
||||
if (wuffs_softfloat__arith__f32_lt(&self->private_data.f_sf, v_cost, 1065353216u) != 0u) {
|
||||
v_cost = wuffs_brotli__bit_cost__f32_sub(&self->private_data.f_bc, v_log2sum, wuffs_softfloat__arith__f64_to_f32(&self->private_data.f_sf, wuffs_brotli__bit_cost__fast_log2(&self->private_data.f_bc, ((uint64_t)(v_h)))));
|
||||
if (wuffs_brotli__bit_cost__f32_lt(&self->private_data.f_bc, v_cost, 1065353216u) != 0u) {
|
||||
v_cost = 1065353216u;
|
||||
}
|
||||
wuffs_brotli__encoder__z_set_cost(self, a_which, v_i, v_cost);
|
||||
@@ -101239,10 +102542,10 @@ wuffs_brotli__encoder__zopfli_cost_model_set_from_commands(
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_impl.f_z_num_bytes) {
|
||||
v_b = wuffs_brotli__encoder__rb_byte(self, a_wb, ((uint64_t)(a_position + v_i)));
|
||||
v_literal_carry = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_literal_carry, self->private_data.f_z_cost_literal[v_b]);
|
||||
v_lc_i1 = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_lc_i, v_literal_carry);
|
||||
v_literal_carry = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_literal_carry, self->private_data.f_z_cost_literal[v_b]);
|
||||
v_lc_i1 = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_lc_i, v_literal_carry);
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(self->private_impl.f_z_lit_off + ((uint64_t)(((uint64_t)(v_i + 1u)) * 4u)))), v_lc_i1);
|
||||
v_literal_carry = wuffs_softfloat__arith__f32_sub(&self->private_data.f_sf, v_literal_carry, wuffs_softfloat__arith__f32_sub(&self->private_data.f_sf, v_lc_i1, v_lc_i));
|
||||
v_literal_carry = wuffs_brotli__bit_cost__f32_sub(&self->private_data.f_bc, v_literal_carry, wuffs_brotli__bit_cost__f32_sub(&self->private_data.f_bc, v_lc_i1, v_lc_i));
|
||||
v_lc_i = v_lc_i1;
|
||||
v_i += 1u;
|
||||
}
|
||||
@@ -101273,10 +102576,10 @@ wuffs_brotli__encoder__zopfli_cost_model_set_from_literal_costs(
|
||||
v_lc_i = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_impl.f_z_num_bytes) {
|
||||
v_literal_carry = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_literal_carry, wuffs_brotli__encoder__lit_cost_at(self, a_wb, ((uint64_t)(v_i + 1u))));
|
||||
v_lc_i1 = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_lc_i, v_literal_carry);
|
||||
v_literal_carry = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_literal_carry, wuffs_brotli__encoder__lit_cost_at(self, a_wb, ((uint64_t)(v_i + 1u))));
|
||||
v_lc_i1 = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_lc_i, v_literal_carry);
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(self->private_impl.f_z_lit_off + ((uint64_t)(((uint64_t)(v_i + 1u)) * 4u)))), v_lc_i1);
|
||||
v_literal_carry = wuffs_softfloat__arith__f32_sub(&self->private_data.f_sf, v_literal_carry, wuffs_softfloat__arith__f32_sub(&self->private_data.f_sf, v_lc_i1, v_lc_i));
|
||||
v_literal_carry = wuffs_brotli__bit_cost__f32_sub(&self->private_data.f_bc, v_literal_carry, wuffs_brotli__bit_cost__f32_sub(&self->private_data.f_bc, v_lc_i1, v_lc_i));
|
||||
v_lc_i = v_lc_i1;
|
||||
v_i += 1u;
|
||||
}
|
||||
@@ -101303,7 +102606,7 @@ wuffs_brotli__encoder__z_literal_costs(
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_from,
|
||||
uint64_t a_to) {
|
||||
return wuffs_softfloat__arith__f32_sub(&self->private_data.f_sf, wuffs_brotli__encoder__lit_cost_at(self, a_wb, a_to), wuffs_brotli__encoder__lit_cost_at(self, a_wb, a_from));
|
||||
return wuffs_brotli__bit_cost__f32_sub(&self->private_data.f_bc, wuffs_brotli__encoder__lit_cost_at(self, a_wb, a_to), wuffs_brotli__encoder__lit_cost_at(self, a_wb, a_from));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.update_zopfli_node
|
||||
@@ -101368,7 +102671,7 @@ wuffs_brotli__encoder__start_pos_queue_push(
|
||||
while (v_i < v_len) {
|
||||
v_a = ((uint32_t)((v_offset & 7u)));
|
||||
v_b = ((uint32_t)((((uint64_t)(v_offset + 1u)) & 7u)));
|
||||
if (wuffs_softfloat__arith__f32_gt(&self->private_data.f_sf, self->private_data.f_zq_costdiff[v_a], self->private_data.f_zq_costdiff[v_b]) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f32_gt(&self->private_data.f_bc, self->private_data.f_zq_costdiff[v_a], self->private_data.f_zq_costdiff[v_b]) != 0u) {
|
||||
wuffs_brotli__encoder__start_pos_queue_swap(self, v_a, v_b);
|
||||
}
|
||||
v_offset += 1u;
|
||||
@@ -101437,10 +102740,10 @@ wuffs_brotli__encoder__compute_minimum_copy_length(
|
||||
v_len = 2u;
|
||||
v_next_len_bucket = 4u;
|
||||
v_next_len_offset = 10u;
|
||||
while ((((uint64_t)(a_pos + v_len)) <= a_num_bytes) && (wuffs_softfloat__arith__f32_le(&self->private_data.f_sf, wuffs_brotli__encoder__zn_u(self, a_wb, ((uint64_t)(a_pos + v_len))), v_min_cost) != 0u)) {
|
||||
while ((((uint64_t)(a_pos + v_len)) <= a_num_bytes) && (wuffs_brotli__bit_cost__f32_le(&self->private_data.f_bc, wuffs_brotli__encoder__zn_u(self, a_wb, ((uint64_t)(a_pos + v_len))), v_min_cost) != 0u)) {
|
||||
v_len += 1u;
|
||||
if (v_len == v_next_len_offset) {
|
||||
v_min_cost = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_min_cost, 1065353216u);
|
||||
v_min_cost = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_min_cost, 1065353216u);
|
||||
v_next_len_offset += v_next_len_bucket;
|
||||
v_next_len_bucket *= 2u;
|
||||
}
|
||||
@@ -101525,10 +102828,10 @@ wuffs_brotli__encoder__evaluate_node(
|
||||
a_pos,
|
||||
a_max_backward_limit));
|
||||
v_lc = wuffs_brotli__encoder__z_literal_costs(self, a_wb, 0u, a_pos);
|
||||
if (wuffs_softfloat__arith__f32_le(&self->private_data.f_sf, v_node_cost, v_lc) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f32_le(&self->private_data.f_bc, v_node_cost, v_lc) != 0u) {
|
||||
self->private_impl.f_zp_pos = a_pos;
|
||||
self->private_impl.f_zp_cost = v_node_cost;
|
||||
self->private_impl.f_zp_costdiff = wuffs_softfloat__arith__f32_sub(&self->private_data.f_sf, v_node_cost, v_lc);
|
||||
self->private_impl.f_zp_costdiff = wuffs_brotli__bit_cost__f32_sub(&self->private_data.f_bc, v_node_cost, v_lc);
|
||||
wuffs_brotli__encoder__compute_distance_cache(self, a_wb, a_pos);
|
||||
wuffs_brotli__encoder__start_pos_queue_push(self);
|
||||
}
|
||||
@@ -101568,7 +102871,6 @@ wuffs_brotli__encoder__update_nodes(
|
||||
uint32_t v_base_cost = 0;
|
||||
uint64_t v_best_len = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint32_t v_idx = 0;
|
||||
uint64_t v_backward = 0;
|
||||
uint32_t v_bw32 = 0;
|
||||
uint64_t v_prev_ix = 0;
|
||||
@@ -101589,6 +102891,8 @@ wuffs_brotli__encoder__update_nodes(
|
||||
uint64_t v_max_match_len = 0;
|
||||
uint64_t v_len_code = 0;
|
||||
uint64_t v_gap = 0;
|
||||
uint32_t v_dcs[4] = {0};
|
||||
uint32_t v_prev_copycode = 0;
|
||||
|
||||
v_mask = ((uint64_t)(self->private_impl.f_rb_mask));
|
||||
v_gap = self->private_impl.f_dict_total_size;
|
||||
@@ -101609,7 +102913,7 @@ wuffs_brotli__encoder__update_nodes(
|
||||
a_pos,
|
||||
a_max_backward_limit);
|
||||
v_q = wuffs_brotli__encoder__start_pos_queue_at(self, 0u);
|
||||
v_min_cost = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, self->private_data.f_zq_cost[v_q], self->private_impl.f_z_min_cost_cmd), wuffs_brotli__encoder__z_literal_costs(self, a_wb, self->private_data.f_zq_pos[v_q], a_pos));
|
||||
v_min_cost = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, self->private_data.f_zq_cost[v_q], self->private_impl.f_z_min_cost_cmd), wuffs_brotli__encoder__z_literal_costs(self, a_wb, self->private_data.f_zq_pos[v_q], a_pos));
|
||||
v_min_len = wuffs_brotli__encoder__compute_minimum_copy_length(self,
|
||||
a_wb,
|
||||
v_min_cost,
|
||||
@@ -101621,22 +102925,25 @@ wuffs_brotli__encoder__update_nodes(
|
||||
v_start = self->private_data.f_zq_pos[v_q];
|
||||
v_inscode = wuffs_brotli__encoder__get_insert_length_code(self, ((uint64_t)(a_pos - v_start)));
|
||||
v_start_costdiff = self->private_data.f_zq_costdiff[v_q];
|
||||
v_base_cost = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_start_costdiff, wuffs_softfloat__arith__f32_from_u32(&self->private_data.f_sf, WUFFS_BROTLI__INS_EXTRA[wuffs_base__u32__min(v_inscode, 23u)])), wuffs_brotli__encoder__z_literal_costs(self, a_wb, 0u, a_pos));
|
||||
v_base_cost = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_start_costdiff, WUFFS_BROTLI__F32_OF_SMALL_INT[WUFFS_BROTLI__INS_EXTRA[wuffs_base__u32__min(v_inscode, 23u)]]), wuffs_brotli__encoder__z_literal_costs(self, a_wb, 0u, a_pos));
|
||||
v_best_len = ((uint64_t)(v_min_len - 1u));
|
||||
v_dcs[0u] = self->private_data.f_zq_dc[(((uint32_t)(v_q)) << 2u)];
|
||||
v_dcs[1u] = self->private_data.f_zq_dc[((((uint32_t)(v_q)) << 2u) | 1u)];
|
||||
v_dcs[2u] = self->private_data.f_zq_dc[((((uint32_t)(v_q)) << 2u) | 2u)];
|
||||
v_dcs[3u] = self->private_data.f_zq_dc[((((uint32_t)(v_q)) << 2u) | 3u)];
|
||||
v_continuation = wuffs_brotli__encoder__rb_raw(self, a_wb, ((uint64_t)(v_cur_ix_masked + v_best_len)), 0u);
|
||||
v_j = 0u;
|
||||
while ((v_j < 16u) && (v_best_len < v_max_len)) {
|
||||
v_idx = WUFFS_BROTLI__Z_DISTANCE_CACHE_INDEX[v_j];
|
||||
v_bw32 = ((uint32_t)(self->private_data.f_zq_dc[((((uint32_t)(v_q)) << 2u) | v_idx)] + WUFFS_BROTLI__Z_DISTANCE_CACHE_OFFSET[v_j]));
|
||||
if (((uint64_t)(v_cur_ix_masked + v_best_len)) > v_mask) {
|
||||
break;
|
||||
}
|
||||
v_bw32 = ((uint32_t)(v_dcs[WUFFS_BROTLI__Z_DISTANCE_CACHE_INDEX[v_j]] + WUFFS_BROTLI__Z_DISTANCE_CACHE_OFFSET[v_j]));
|
||||
v_backward = ((uint64_t)(v_bw32));
|
||||
if (v_bw32 >= 2147483648u) {
|
||||
v_backward |= 18446744069414584320u;
|
||||
}
|
||||
v_prev_ix = ((uint64_t)(v_cur_ix - v_backward));
|
||||
v_len = 0u;
|
||||
v_continuation = wuffs_brotli__encoder__rb_raw(self, a_wb, ((uint64_t)(v_cur_ix_masked + v_best_len)), 0u);
|
||||
if (((uint64_t)(v_cur_ix_masked + v_best_len)) > v_mask) {
|
||||
break;
|
||||
}
|
||||
if (v_backward > ((uint64_t)(v_dictionary_start + v_gap))) {
|
||||
v_j += 1u;
|
||||
continue;
|
||||
@@ -101672,18 +102979,22 @@ wuffs_brotli__encoder__update_nodes(
|
||||
v_j += 1u;
|
||||
continue;
|
||||
}
|
||||
v_dist_cost = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_base_cost, self->private_data.f_z_cost_dist[v_j]);
|
||||
v_dist_cost = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_base_cost, self->private_data.f_z_cost_dist[v_j]);
|
||||
v_l = ((uint64_t)(v_best_len + 1u));
|
||||
v_prev_copycode = 4294967295u;
|
||||
while (v_l <= v_len) {
|
||||
v_copycode = wuffs_brotli__encoder__get_copy_length_code(self, v_l);
|
||||
v_cmdcode = wuffs_brotli__encoder__combine_length_codes(self, v_inscode, v_copycode, (v_j == 0u));
|
||||
if (v_cmdcode < 128u) {
|
||||
v_cost = v_base_cost;
|
||||
} else {
|
||||
v_cost = v_dist_cost;
|
||||
if (v_copycode != v_prev_copycode) {
|
||||
v_prev_copycode = v_copycode;
|
||||
v_cmdcode = wuffs_brotli__encoder__combine_length_codes(self, v_inscode, v_copycode, (v_j == 0u));
|
||||
if (v_cmdcode < 128u) {
|
||||
v_cost = v_base_cost;
|
||||
} else {
|
||||
v_cost = v_dist_cost;
|
||||
}
|
||||
v_cost = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_cost, WUFFS_BROTLI__F32_OF_SMALL_INT[WUFFS_BROTLI__COPY_EXTRA[wuffs_base__u32__min(v_copycode, 23u)]]), self->private_data.f_z_cost_cmd[wuffs_base__u16__min(v_cmdcode, 703u)]);
|
||||
}
|
||||
v_cost = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_cost, wuffs_softfloat__arith__f32_from_u32(&self->private_data.f_sf, WUFFS_BROTLI__COPY_EXTRA[wuffs_base__u32__min(v_copycode, 23u)])), self->private_data.f_z_cost_cmd[wuffs_base__u16__min(v_cmdcode, 703u)]);
|
||||
if (wuffs_softfloat__arith__f32_lt(&self->private_data.f_sf, v_cost, wuffs_brotli__encoder__zn_u(self, a_wb, ((uint64_t)(a_pos + v_l)))) != 0u) {
|
||||
if (wuffs_brotli__bit_cost__f32_lt(&self->private_data.f_bc, v_cost, wuffs_brotli__encoder__zn_u(self, a_wb, ((uint64_t)(a_pos + v_l)))) != 0u) {
|
||||
wuffs_brotli__encoder__update_zopfli_node(self,
|
||||
a_wb,
|
||||
a_pos,
|
||||
@@ -101698,6 +103009,7 @@ wuffs_brotli__encoder__update_nodes(
|
||||
v_best_len = v_l;
|
||||
v_l += 1u;
|
||||
}
|
||||
v_continuation = wuffs_brotli__encoder__rb_raw(self, a_wb, ((uint64_t)(v_cur_ix_masked + v_best_len)), 0u);
|
||||
v_j += 1u;
|
||||
}
|
||||
if (v_k >= 2u) {
|
||||
@@ -101714,20 +103026,24 @@ wuffs_brotli__encoder__update_nodes(
|
||||
v_pe = wuffs_brotli__metablock_builder__prefix_encode_copy_distance(&self->private_data.f_mb, v_dist_code, ((uint64_t)(self->private_impl.f_num_direct_distance_codes)), self->private_impl.f_distance_postfix_bits);
|
||||
v_dist_symbol = ((uint32_t)((v_pe & 65535u)));
|
||||
v_distnumextra = (v_dist_symbol >> 10u);
|
||||
v_dist_cost = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_base_cost, wuffs_softfloat__arith__f32_from_u32(&self->private_data.f_sf, v_distnumextra)), self->private_data.f_z_cost_dist[wuffs_brotli__encoder__min32_543(self, (v_dist_symbol & 1023u))]);
|
||||
v_dist_cost = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_base_cost, wuffs_brotli__bit_cost__f32_from_u32(&self->private_data.f_bc, v_distnumextra)), self->private_data.f_z_cost_dist[wuffs_brotli__encoder__min32_543(self, (v_dist_symbol & 1023u))]);
|
||||
v_max_match_len = wuffs_brotli__encoder__bm_length(self, a_wb, v_moff);
|
||||
if ((v_len < v_max_match_len) && (v_is_dictionary_match || (v_max_match_len > v_max_zopfli_len))) {
|
||||
v_len = v_max_match_len;
|
||||
}
|
||||
v_prev_copycode = 4294967295u;
|
||||
while (v_len <= v_max_match_len) {
|
||||
v_len_code = v_len;
|
||||
if (v_is_dictionary_match) {
|
||||
v_len_code = wuffs_brotli__encoder__bm_length_code(self, a_wb, v_moff);
|
||||
}
|
||||
v_copycode = wuffs_brotli__encoder__get_copy_length_code(self, v_len_code);
|
||||
v_cmdcode = wuffs_brotli__encoder__combine_length_codes(self, v_inscode, v_copycode, false);
|
||||
v_cost = wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, wuffs_softfloat__arith__f32_add(&self->private_data.f_sf, v_dist_cost, wuffs_softfloat__arith__f32_from_u32(&self->private_data.f_sf, WUFFS_BROTLI__COPY_EXTRA[wuffs_base__u32__min(v_copycode, 23u)])), self->private_data.f_z_cost_cmd[wuffs_base__u16__min(v_cmdcode, 703u)]);
|
||||
if (wuffs_softfloat__arith__f32_lt(&self->private_data.f_sf, v_cost, wuffs_brotli__encoder__zn_u(self, a_wb, ((uint64_t)(a_pos + v_len)))) != 0u) {
|
||||
if (v_copycode != v_prev_copycode) {
|
||||
v_prev_copycode = v_copycode;
|
||||
v_cmdcode = wuffs_brotli__encoder__combine_length_codes(self, v_inscode, v_copycode, false);
|
||||
v_cost = wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, wuffs_brotli__bit_cost__f32_add(&self->private_data.f_bc, v_dist_cost, WUFFS_BROTLI__F32_OF_SMALL_INT[WUFFS_BROTLI__COPY_EXTRA[wuffs_base__u32__min(v_copycode, 23u)]]), self->private_data.f_z_cost_cmd[wuffs_base__u16__min(v_cmdcode, 703u)]);
|
||||
}
|
||||
if (wuffs_brotli__bit_cost__f32_lt(&self->private_data.f_bc, v_cost, wuffs_brotli__encoder__zn_u(self, a_wb, ((uint64_t)(a_pos + v_len)))) != 0u) {
|
||||
wuffs_brotli__encoder__update_zopfli_node(self,
|
||||
a_wb,
|
||||
a_pos,
|
||||
|
||||
@@ -0,0 +1,411 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, 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
|
||||
|
||||
// ----------------
|
||||
|
||||
// manual-test-brotli-float.c compares std/brotli's package-private softfloat
|
||||
// fast paths (the bit_cost methods in std/brotli/encode_float.wuffs) against
|
||||
// the C compiler's native (hardware) float and double arithmetic, for
|
||||
// pseudo-random operands that are biased towards the fast paths' edge cases:
|
||||
// every exponent difference, near-ties, cancellation, results near the
|
||||
// subnormal and overflow thresholds, zeroes, subnormals, infinities and NaNs.
|
||||
// Being static functions, they are called directly (this program #include's
|
||||
// the Wuffs C file with WUFFS_CONFIG__STATIC_FUNCTIONS).
|
||||
//
|
||||
// Build it WITHOUT -ffast-math and WITHOUT -mfma / -march=native:
|
||||
// gcc -O2 -std=c99 manual-test-brotli-float.c -o /tmp/brfloat
|
||||
// /tmp/brfloat # Default: 100 million operations per op.
|
||||
// /tmp/brfloat 1000000 # Or pass the number of operations per op.
|
||||
// It also checks the integer bit helpers (log2_floor_non_zero, for every u32,
|
||||
// and ctz64) against the compiler's __builtin_clz and __builtin_ctzll.
|
||||
//
|
||||
// It prints one line per op and the first few mismatches (if any), and exits
|
||||
// with a non-zero status if there were any mismatches. NaN results only need
|
||||
// to be NaN (on both sides).
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WUFFS_IMPLEMENTATION
|
||||
#define WUFFS_CONFIG__STATIC_FUNCTIONS
|
||||
#define WUFFS_CONFIG__MODULES
|
||||
#define WUFFS_CONFIG__MODULE__BASE
|
||||
#define WUFFS_CONFIG__MODULE__BROTLI
|
||||
#define WUFFS_CONFIG__MODULE__SOFTFLOAT
|
||||
#include "../release/c/wuffs-unsupported-snapshot.c"
|
||||
|
||||
#if defined(__FAST_MATH__) || defined(__FMA__)
|
||||
#error "Build without -ffast-math and without -mfma / -march=native"
|
||||
#endif
|
||||
|
||||
static wuffs_brotli__encoder* g_enc;
|
||||
static wuffs_brotli__bit_cost* g_bc;
|
||||
|
||||
static uint64_t g_rng = 0x123456789ABCDEF1ull;
|
||||
|
||||
static uint64_t //
|
||||
rnd(void) {
|
||||
// xorshift64*.
|
||||
g_rng ^= g_rng >> 12;
|
||||
g_rng ^= g_rng << 25;
|
||||
g_rng ^= g_rng >> 27;
|
||||
return g_rng * 0x2545F4914F6CDD1Dull;
|
||||
}
|
||||
|
||||
static double //
|
||||
d_of(uint64_t u) {
|
||||
double d;
|
||||
memcpy(&d, &u, 8);
|
||||
return d;
|
||||
}
|
||||
|
||||
static uint64_t //
|
||||
u_of_d(double d) {
|
||||
uint64_t u;
|
||||
memcpy(&u, &d, 8);
|
||||
return u;
|
||||
}
|
||||
|
||||
static float //
|
||||
f_of(uint32_t u) {
|
||||
float f;
|
||||
memcpy(&f, &u, 4);
|
||||
return f;
|
||||
}
|
||||
|
||||
static uint32_t //
|
||||
u_of_f(float f) {
|
||||
uint32_t u;
|
||||
memcpy(&u, &f, 4);
|
||||
return u;
|
||||
}
|
||||
|
||||
// rnd_f32 returns a random f32 bit pattern. Usually it is a normal number
|
||||
// whose exponent is near ref's (so that every exponent difference and lots
|
||||
// of cancellation is exercised) and whose low fraction bits are often all
|
||||
// zeroes or all ones (giving near-ties).
|
||||
static uint32_t //
|
||||
rnd_f32(uint32_t ref) {
|
||||
uint64_t r = rnd();
|
||||
uint32_t sign = (uint32_t)(r & 1) << 31;
|
||||
uint32_t frac = (uint32_t)(r >> 8) & 0x7FFFFF;
|
||||
switch ((r >> 1) & 7) {
|
||||
case 0:
|
||||
frac &= ~0u << ((r >> 32) % 24);
|
||||
break;
|
||||
case 1:
|
||||
frac |= ~(~0u << ((r >> 32) % 24)) & 0x7FFFFF;
|
||||
break;
|
||||
}
|
||||
uint32_t exp;
|
||||
switch ((r >> 4) & 15) {
|
||||
case 0:
|
||||
return (uint32_t)rnd(); // Anything, including NaNs.
|
||||
case 1:
|
||||
return sign | (frac & ((r >> 40) & 1 ? 0x7FFFFF : 0)); // Subnormal.
|
||||
case 2:
|
||||
return sign | 0x7F800000 | ((r >> 41) & 1 ? frac : 0); // Inf or NaN.
|
||||
case 3:
|
||||
exp = 1 + ((r >> 42) % 4); // Near the subnormal threshold.
|
||||
break;
|
||||
case 4:
|
||||
exp = 250 + ((r >> 42) % 5); // Near the overflow threshold.
|
||||
break;
|
||||
case 5:
|
||||
exp = 1 + ((r >> 42) % 254);
|
||||
break;
|
||||
default: {
|
||||
int32_t e = (int32_t)((ref >> 23) & 0xFF) - 30 + (int32_t)((r >> 42) % 61);
|
||||
exp = (e < 1) ? 1 : (e > 254) ? 254 : (uint32_t)e;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sign | (exp << 23) | frac;
|
||||
}
|
||||
|
||||
// rnd_f64 is like rnd_f32 but for f64.
|
||||
static uint64_t //
|
||||
rnd_f64(uint64_t ref) {
|
||||
uint64_t r = rnd();
|
||||
uint64_t r2 = rnd();
|
||||
uint64_t sign = (r & 1) << 63;
|
||||
uint64_t frac = r2 & 0xFFFFFFFFFFFFFull;
|
||||
switch ((r >> 1) & 7) {
|
||||
case 0:
|
||||
frac &= ~0ull << ((r >> 32) % 53);
|
||||
break;
|
||||
case 1:
|
||||
frac |= ~(~0ull << ((r >> 32) % 53)) & 0xFFFFFFFFFFFFFull;
|
||||
break;
|
||||
}
|
||||
uint64_t exp;
|
||||
switch ((r >> 4) & 15) {
|
||||
case 0:
|
||||
return rnd(); // Anything, including NaNs.
|
||||
case 1:
|
||||
return sign | (frac & ((r >> 40) & 1 ? 0xFFFFFFFFFFFFFull : 0));
|
||||
case 2:
|
||||
return sign | 0x7FF0000000000000ull | ((r >> 41) & 1 ? frac : 0);
|
||||
case 3:
|
||||
exp = 1 + ((r >> 42) % 4);
|
||||
break;
|
||||
case 4:
|
||||
exp = 2042 + ((r >> 42) % 5);
|
||||
break;
|
||||
case 5:
|
||||
exp = 1 + ((r >> 42) % 2046);
|
||||
break;
|
||||
default: {
|
||||
int64_t e = (int64_t)((ref >> 52) & 0x7FF) - 60 + (int64_t)((r >> 42) % 121);
|
||||
exp = (e < 1) ? 1 : (e > 2046) ? 2046 : (uint64_t)e;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sign | (exp << 52) | frac;
|
||||
}
|
||||
|
||||
static uint64_t g_total_mismatches;
|
||||
|
||||
#define CHECK32(name, have_expr, want_expr, is_cmp) \
|
||||
static void check_##name(uint64_t n) { \
|
||||
uint64_t mismatches = 0; \
|
||||
for (uint64_t i = 0; i < n; i++) { \
|
||||
uint32_t a = rnd_f32((uint32_t)rnd()); \
|
||||
uint32_t b = (rnd() & 3) ? rnd_f32(a) : a ^ ((rnd() & 1) << 31); \
|
||||
if (rnd() & 1) { \
|
||||
uint32_t t = a; \
|
||||
a = b; \
|
||||
b = t; \
|
||||
} \
|
||||
uint32_t have = (have_expr); \
|
||||
uint32_t want = (want_expr); \
|
||||
bool ok = (have == want) || \
|
||||
(!is_cmp && ((have & 0x7FFFFFFF) > 0x7F800000) && \
|
||||
((want & 0x7FFFFFFF) > 0x7F800000)); \
|
||||
if (!ok && (mismatches++ < 5)) { \
|
||||
printf("%s(0x%08" PRIX32 ", 0x%08" PRIX32 "): have 0x%08" PRIX32 \
|
||||
", want 0x%08" PRIX32 "\n", \
|
||||
#name, a, b, have, want); \
|
||||
} \
|
||||
} \
|
||||
printf("%-8s %12" PRIu64 " iterations, %8" PRIu64 " mismatches\n", \
|
||||
#name, n, mismatches); \
|
||||
g_total_mismatches += mismatches; \
|
||||
}
|
||||
|
||||
#define CHECK64(name, have_expr, want_expr, is_cmp) \
|
||||
static void check_##name(uint64_t n) { \
|
||||
uint64_t mismatches = 0; \
|
||||
for (uint64_t i = 0; i < n; i++) { \
|
||||
uint64_t a = rnd_f64(rnd()); \
|
||||
uint64_t b = (rnd() & 3) ? rnd_f64(a) : a ^ ((rnd() & 1) << 63); \
|
||||
if (rnd() & 1) { \
|
||||
uint64_t t = a; \
|
||||
a = b; \
|
||||
b = t; \
|
||||
} \
|
||||
uint64_t have = (have_expr); \
|
||||
uint64_t want = (want_expr); \
|
||||
bool ok = (have == want) || \
|
||||
(!is_cmp && \
|
||||
((have & 0x7FFFFFFFFFFFFFFFull) > 0x7FF0000000000000ull) && \
|
||||
((want & 0x7FFFFFFFFFFFFFFFull) > 0x7FF0000000000000ull)); \
|
||||
if (!ok && (mismatches++ < 5)) { \
|
||||
printf("%s(0x%016" PRIX64 ", 0x%016" PRIX64 "): have 0x%016" PRIX64 \
|
||||
", want 0x%016" PRIX64 "\n", \
|
||||
#name, a, b, have, want); \
|
||||
} \
|
||||
} \
|
||||
printf("%-8s %12" PRIu64 " iterations, %8" PRIu64 " mismatches\n", \
|
||||
#name, n, mismatches); \
|
||||
g_total_mismatches += mismatches; \
|
||||
}
|
||||
|
||||
CHECK32(f32_add,
|
||||
wuffs_brotli__bit_cost__f32_add(g_bc, a, b),
|
||||
u_of_f(f_of(a) + f_of(b)),
|
||||
false)
|
||||
CHECK32(f32_sub,
|
||||
wuffs_brotli__bit_cost__f32_sub(g_bc, a, b),
|
||||
u_of_f(f_of(a) - f_of(b)),
|
||||
false)
|
||||
CHECK32(f32_lt,
|
||||
wuffs_brotli__bit_cost__f32_lt(g_bc, a, b),
|
||||
f_of(a) < f_of(b),
|
||||
true)
|
||||
CHECK32(f32_le,
|
||||
wuffs_brotli__bit_cost__f32_le(g_bc, a, b),
|
||||
f_of(a) <= f_of(b),
|
||||
true)
|
||||
CHECK32(f32_gt,
|
||||
wuffs_brotli__bit_cost__f32_gt(g_bc, a, b),
|
||||
f_of(a) > f_of(b),
|
||||
true)
|
||||
CHECK64(f64_add,
|
||||
wuffs_brotli__bit_cost__f64_add(g_bc, a, b),
|
||||
u_of_d(d_of(a) + d_of(b)),
|
||||
false)
|
||||
CHECK64(f64_sub,
|
||||
wuffs_brotli__bit_cost__f64_sub(g_bc, a, b),
|
||||
u_of_d(d_of(a) - d_of(b)),
|
||||
false)
|
||||
CHECK64(f64_mul,
|
||||
wuffs_brotli__bit_cost__f64_mul(g_bc, a, b),
|
||||
u_of_d(d_of(a) * d_of(b)),
|
||||
false)
|
||||
CHECK64(f64_lt,
|
||||
wuffs_brotli__bit_cost__f64_lt(g_bc, a, b),
|
||||
d_of(a) < d_of(b),
|
||||
true)
|
||||
CHECK64(f64_gt,
|
||||
wuffs_brotli__bit_cost__f64_gt(g_bc, a, b),
|
||||
d_of(a) > d_of(b),
|
||||
true)
|
||||
CHECK64(f64_ge,
|
||||
wuffs_brotli__bit_cost__f64_ge(g_bc, a, b),
|
||||
d_of(a) >= d_of(b),
|
||||
true)
|
||||
|
||||
// rnd_int returns a random integer, with a random number of significant
|
||||
// bits (and often with many trailing zeroes).
|
||||
static uint64_t //
|
||||
rnd_int(void) {
|
||||
uint64_t r = rnd();
|
||||
uint64_t x = rnd() >> (r % 64);
|
||||
if (r & 0x100) {
|
||||
x &= ~0ull << ((r >> 9) % 64);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
static void //
|
||||
check_conversions(uint64_t n) {
|
||||
uint64_t mismatches = 0;
|
||||
for (uint64_t i = 0; i < n; i++) {
|
||||
uint64_t x = rnd_int();
|
||||
uint32_t x32 = (uint32_t)x;
|
||||
uint64_t have = wuffs_brotli__bit_cost__f64_from_u64(g_bc, x);
|
||||
uint64_t want = u_of_d((double)x);
|
||||
if ((have != want) && (mismatches++ < 5)) {
|
||||
printf("f64_from_u64(0x%016" PRIX64 "): have 0x%016" PRIX64
|
||||
", want 0x%016" PRIX64 "\n", x, have, want);
|
||||
}
|
||||
have = wuffs_brotli__bit_cost__f64_from_u32(g_bc, x32);
|
||||
want = u_of_d((double)x32);
|
||||
if ((have != want) && (mismatches++ < 5)) {
|
||||
printf("f64_from_u32(0x%08" PRIX32 "): have 0x%016" PRIX64
|
||||
", want 0x%016" PRIX64 "\n", x32, have, want);
|
||||
}
|
||||
have = wuffs_brotli__bit_cost__f32_from_u32(g_bc, x32);
|
||||
want = u_of_f((float)x32);
|
||||
if ((have != want) && (mismatches++ < 5)) {
|
||||
printf("f32_from_u32(0x%08" PRIX32 "): have 0x%08" PRIX32
|
||||
", want 0x%08" PRIX32 "\n", x32, (uint32_t)have, (uint32_t)want);
|
||||
}
|
||||
// f64_to_u64_trunc, for (non-negative) values that fit in a u64.
|
||||
uint64_t a = rnd_f64(rnd()) & 0x7FFFFFFFFFFFFFFFull;
|
||||
if ((rnd() & 3) == 0) {
|
||||
a = u_of_d((double)x) + (rnd() % 3) - 1;
|
||||
}
|
||||
if (d_of(a) < 18446744073709551616.0) {
|
||||
have = wuffs_brotli__bit_cost__f64_to_u64_trunc(g_bc, a);
|
||||
want = (uint64_t)d_of(a);
|
||||
if ((have != want) && (mismatches++ < 5)) {
|
||||
printf("f64_to_u64_trunc(0x%016" PRIX64 "): have 0x%016" PRIX64
|
||||
", want 0x%016" PRIX64 "\n", a, have, want);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (uint32_t v = 0; v < 32; v++) {
|
||||
if ((WUFFS_BROTLI__F32_OF_SMALL_INT[v] != u_of_f((float)v)) &&
|
||||
(mismatches++ < 5)) {
|
||||
printf("F32_OF_SMALL_INT[%" PRIu32 "] is wrong\n", v);
|
||||
}
|
||||
}
|
||||
printf("%-8s %12" PRIu64 " iterations, %8" PRIu64 " mismatches\n", "convert",
|
||||
n, mismatches);
|
||||
g_total_mismatches += mismatches;
|
||||
}
|
||||
|
||||
static void //
|
||||
check_bit_helpers(uint64_t n) {
|
||||
uint64_t mismatches = 0;
|
||||
wuffs_brotli__encoder* enc = g_enc;
|
||||
// log2_floor_non_zero truncates its argument to 32 bits (and returns 0 for
|
||||
// 0), so check every u32 value, with random high bits.
|
||||
uint32_t v = 0;
|
||||
do {
|
||||
uint64_t x = ((uint64_t)(rnd() & 0xFFFFFFFF00000000ull)) | v;
|
||||
uint32_t want = v ? (31 - (uint32_t)__builtin_clz(v)) : 0;
|
||||
uint32_t have0 = wuffs_brotli__encoder__log2_floor_non_zero(enc, x);
|
||||
uint32_t have1 = wuffs_brotli__metablock_builder__log2_floor_non_zero(
|
||||
&enc->private_data.f_mb, x);
|
||||
if (((have0 != want) || (have1 != want)) && (mismatches++ < 5)) {
|
||||
printf("log2_floor_non_zero(0x%016" PRIX64 "): have %" PRIu32
|
||||
" and %" PRIu32 ", want %" PRIu32 "\n", x, have0, have1, want);
|
||||
}
|
||||
} while (++v != 0);
|
||||
for (uint64_t i = 0; i < n; i++) {
|
||||
uint64_t x = (i < 64) ? (1ull << i) : rnd_int();
|
||||
if (x == 0) {
|
||||
continue;
|
||||
}
|
||||
uint64_t want = (uint64_t)__builtin_ctzll(x);
|
||||
uint64_t have = wuffs_brotli__encoder__ctz64(enc, x);
|
||||
if ((have != want) && (mismatches++ < 5)) {
|
||||
printf("ctz64(0x%016" PRIX64 "): have %" PRIu64 ", want %" PRIu64 "\n",
|
||||
x, have, want);
|
||||
}
|
||||
}
|
||||
printf("%-8s %12" PRIu64 " iterations, %8" PRIu64 " mismatches\n", "bits",
|
||||
n + (uint64_t)0x100000000ull, mismatches);
|
||||
g_total_mismatches += mismatches;
|
||||
}
|
||||
|
||||
int //
|
||||
main(int argc, char** argv) {
|
||||
uint64_t n = 100000000;
|
||||
if (argc > 1) {
|
||||
n = strtoull(argv[1], NULL, 10);
|
||||
}
|
||||
wuffs_brotli__encoder* enc = wuffs_brotli__encoder__alloc();
|
||||
if (!enc) {
|
||||
printf("alloc failed\n");
|
||||
return 1;
|
||||
}
|
||||
g_enc = enc;
|
||||
g_bc = &enc->private_data.f_bc;
|
||||
|
||||
check_f32_add(n);
|
||||
check_f32_sub(n);
|
||||
check_f32_lt(n);
|
||||
check_f32_le(n);
|
||||
check_f32_gt(n);
|
||||
check_f64_add(n);
|
||||
check_f64_sub(n);
|
||||
check_f64_mul(n);
|
||||
check_f64_lt(n);
|
||||
check_f64_gt(n);
|
||||
check_f64_ge(n);
|
||||
check_conversions(n);
|
||||
check_bit_helpers(n);
|
||||
|
||||
free(enc);
|
||||
if (g_total_mismatches) {
|
||||
printf("FAIL: %" PRIu64 " mismatches in total\n", g_total_mismatches);
|
||||
return 1;
|
||||
}
|
||||
printf("PASS: 0 mismatches in total\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -31,7 +31,9 @@
|
||||
// mostly 1 to 7 bytes), with a random large-window quirk, random
|
||||
// (raw and serialized) dictionaries and a random workbuf length (at
|
||||
// or above workbuf_len, or exactly the stream's minimum, or one byte
|
||||
// less than that, which must fail).
|
||||
// less than that, which must fail). Some decodes (with a large dst)
|
||||
// also enable QUIRK_REPORT_METADATA and libbrotlidec's metadata
|
||||
// callbacks and check that both report the same metadata blocks.
|
||||
//
|
||||
// -enc encodes random inputs with random parameters (quality, lgwin,
|
||||
// large window, lgblock, mode, size hint, npostfix, ndirect,
|
||||
@@ -1068,8 +1070,41 @@ typedef struct {
|
||||
size_t in_used;
|
||||
const char* wuffs_status;
|
||||
uint64_t calls;
|
||||
size_t md_n; // With report_md: the metadata bytes (see g_wmd and g_cmd).
|
||||
} dec_result;
|
||||
|
||||
// With dec_config.report_md, the Wuffs decoder (QUIRK_REPORT_METADATA) and
|
||||
// libbrotlidec (BrotliDecoderSetMetadataCallbacks) collect each metadata
|
||||
// block (its length as 4 little-endian bytes, then its contents) in g_wmd and
|
||||
// g_cmd. A block's contents can be up to 16 MiB long.
|
||||
#define MD_CAP ((size_t)40 << 20)
|
||||
#define MAX_OUT (((size_t)2 << 20) + 4096)
|
||||
static uint8_t* g_wmd;
|
||||
static uint8_t* g_cmd;
|
||||
static size_t g_cmd_n;
|
||||
static bool g_cmd_overflow;
|
||||
|
||||
static void //
|
||||
c_md_start(void* opaque, size_t size) {
|
||||
if (MD_CAP - g_cmd_n < 4) {
|
||||
g_cmd_overflow = true;
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < 4; i++) {
|
||||
g_cmd[g_cmd_n++] = (uint8_t)(size >> (8 * i));
|
||||
}
|
||||
}
|
||||
|
||||
static void //
|
||||
c_md_chunk(void* opaque, const uint8_t* data, size_t size) {
|
||||
if (MD_CAP - g_cmd_n < size) {
|
||||
g_cmd_overflow = true;
|
||||
return;
|
||||
}
|
||||
memcpy(g_cmd + g_cmd_n, data, size);
|
||||
g_cmd_n += size;
|
||||
}
|
||||
|
||||
static int //
|
||||
wuffs_status_class(const char* s) {
|
||||
static const struct {
|
||||
@@ -1143,6 +1178,7 @@ typedef struct {
|
||||
uint64_t wb_len; // The Wuffs workbuf length.
|
||||
bool expect_bad_wb; // wb_len is one byte too short for the stream.
|
||||
bool uninit; // WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED.
|
||||
bool report_md; // QUIRK_REPORT_METADATA and metadata callbacks.
|
||||
size_t out_cap;
|
||||
} dec_config;
|
||||
|
||||
@@ -1166,6 +1202,13 @@ wuffs_attach(wuffs_brotli__decoder* dec, const dec_config* dc, size_t* n_ok) {
|
||||
return status.repr;
|
||||
}
|
||||
}
|
||||
if (dc->report_md) {
|
||||
status = wuffs_brotli__decoder__set_quirk(
|
||||
dec, WUFFS_BROTLI__QUIRK_REPORT_METADATA, 1);
|
||||
if (status.repr) {
|
||||
return status.repr;
|
||||
}
|
||||
}
|
||||
*n_ok = 0;
|
||||
for (size_t i = 0; i < dc->dicts.n; i++) {
|
||||
status = wuffs_brotli__decoder__add_dictionary(
|
||||
@@ -1206,6 +1249,12 @@ wuffs_decode(const dec_config* dc,
|
||||
size_t ri = 0;
|
||||
size_t wi = 0;
|
||||
uint64_t max_calls = 3 * ((uint64_t)n + dc->out_cap) + 100;
|
||||
// With report_md, metadata bytes are written to dst (out) and then moved to
|
||||
// g_wmd. While md_remaining > 0, dst gets up to md_remaining bytes of slack
|
||||
// past lout (within the physical buffer's MAX_OUT bytes), which the
|
||||
// metadata bytes fill before any decoded bytes, so that the decoded bytes
|
||||
// still fit in lout (like libbrotlidec's, which uses no dst for metadata).
|
||||
size_t md_remaining = 0;
|
||||
while (true) {
|
||||
if (++res.calls > max_calls) {
|
||||
res.cls = CLS_BUG;
|
||||
@@ -1215,7 +1264,12 @@ wuffs_decode(const dec_config* dc,
|
||||
wuffs_base__io_buffer src =
|
||||
wuffs_base__ptr_u8__reader((uint8_t*)in, lin, lin == n);
|
||||
src.meta.ri = ri;
|
||||
wuffs_base__io_buffer dst = wuffs_base__ptr_u8__writer(out, lout);
|
||||
size_t slack = 0;
|
||||
if ((md_remaining > 0) && (lout < MAX_OUT)) {
|
||||
slack = (md_remaining < (MAX_OUT - lout)) ? md_remaining
|
||||
: (MAX_OUT - lout);
|
||||
}
|
||||
wuffs_base__io_buffer dst = wuffs_base__ptr_u8__writer(out, lout + slack);
|
||||
dst.meta.wi = wi;
|
||||
wuffs_base__status status =
|
||||
wuffs_brotli__decoder__transform_io(g_dec, &dst, &src, wb);
|
||||
@@ -1224,9 +1278,37 @@ wuffs_decode(const dec_config* dc,
|
||||
res.wuffs_status = "went backwards";
|
||||
break;
|
||||
}
|
||||
if (md_remaining > 0) {
|
||||
size_t k = dst.meta.wi - wi;
|
||||
k = (k < md_remaining) ? k : md_remaining;
|
||||
memcpy(g_wmd + res.md_n, out + wi, k);
|
||||
res.md_n += k;
|
||||
md_remaining -= k;
|
||||
memmove(out + wi, out + wi + k, dst.meta.wi - wi - k);
|
||||
dst.meta.wi -= k;
|
||||
}
|
||||
if (dst.meta.wi > lout) {
|
||||
res.cls = CLS_BUG;
|
||||
res.wuffs_status = "decoded bytes in the metadata slack";
|
||||
break;
|
||||
}
|
||||
ri = src.meta.ri;
|
||||
wi = dst.meta.wi;
|
||||
res.wuffs_status = status.repr;
|
||||
if (status.repr == wuffs_base__note__metadata_reported) {
|
||||
uint64_t len = wuffs_brotli__decoder__metadata_length(g_dec);
|
||||
if (!dc->report_md || (md_remaining > 0) || (len > (1 << 24)) ||
|
||||
(MD_CAP - res.md_n < 4 + len)) {
|
||||
res.cls = CLS_BUG;
|
||||
res.wuffs_status = "unexpected metadata report";
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < 4; i++) {
|
||||
g_wmd[res.md_n++] = (uint8_t)(len >> (8 * i));
|
||||
}
|
||||
md_remaining = (size_t)len;
|
||||
continue;
|
||||
}
|
||||
if (g_trace) {
|
||||
printf("wuffs call %" PRIu64 ": lin=%zu lout=%zu -> ri=%zu wi=%zu %s\n",
|
||||
res.calls, lin, lout, ri, wi, status.repr ? status.repr : "ok");
|
||||
@@ -1276,6 +1358,11 @@ c_decode(const dec_config* dc,
|
||||
if (dc->large_window) {
|
||||
BrotliDecoderSetParameter(s, BROTLI_DECODER_PARAM_LARGE_WINDOW, 1);
|
||||
}
|
||||
g_cmd_n = 0;
|
||||
g_cmd_overflow = false;
|
||||
if (dc->report_md) {
|
||||
BrotliDecoderSetMetadataCallbacks(s, c_md_start, c_md_chunk, NULL);
|
||||
}
|
||||
*c_attached = 0;
|
||||
for (size_t i = 0; i < dc->dicts.n; i++) {
|
||||
if (!BrotliDecoderAttachDictionary(
|
||||
@@ -1336,6 +1423,7 @@ c_decode(const dec_config* dc,
|
||||
BrotliDecoderDestroyInstance(s);
|
||||
res.out_n = wi;
|
||||
res.in_used = ri;
|
||||
res.md_n = g_cmd_overflow ? SIZE_MAX : g_cmd_n;
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -1429,6 +1517,10 @@ static uint64_t g_dec_gen_wuffs;
|
||||
static uint64_t g_dec_wuffs_shorter_output;
|
||||
static uint64_t g_dec_known_block_length;
|
||||
static uint64_t g_dec_known_saved;
|
||||
static uint64_t g_dec_report_md;
|
||||
static uint64_t g_dec_md_compared;
|
||||
static uint64_t g_dec_md_blocks;
|
||||
static uint64_t g_dec_md_inconclusive;
|
||||
|
||||
static int //
|
||||
cls_index(int cls) {
|
||||
@@ -1759,9 +1851,10 @@ run_dec(uint64_t iters, double secs) {
|
||||
const size_t tmp_cap = (8 << 20);
|
||||
uint8_t* tmp_in = xmalloc(tmp_cap);
|
||||
uint8_t* tmp_out = xmalloc(tmp_cap);
|
||||
const size_t max_out = (2 << 20) + 4096;
|
||||
uint8_t* wout = xmalloc(max_out);
|
||||
uint8_t* cout = xmalloc(max_out);
|
||||
uint8_t* wout = xmalloc(MAX_OUT);
|
||||
uint8_t* cout = xmalloc(MAX_OUT);
|
||||
g_wmd = xmalloc(MD_CAP);
|
||||
g_cmd = xmalloc(MD_CAP);
|
||||
const size_t mcap = (1 << 20);
|
||||
uint8_t* m = xmalloc(mcap);
|
||||
|
||||
@@ -1821,6 +1914,10 @@ run_dec(uint64_t iters, double secs) {
|
||||
g_dec_with_dicts += (dc.dicts.n > 0);
|
||||
dc.uninit = chance(500);
|
||||
dc.out_cap = chance(900) ? ((size_t)1 << 20) + rn(1 << 20) : rn(4096);
|
||||
// Only with a large dst: libbrotlidec reports metadata (via callbacks)
|
||||
// even when its dst is full, but Wuffs needs dst space for it.
|
||||
dc.report_md = (dc.out_cap >= ((size_t)1 << 20)) && chance(300);
|
||||
g_dec_report_md += dc.report_md;
|
||||
|
||||
// Attach the dictionaries to both decoders, checking that they agree on
|
||||
// which ones are accepted. libbrotlidec can be left in a modified state
|
||||
@@ -1899,6 +1996,44 @@ run_dec(uint64_t iters, double secs) {
|
||||
size_t c_attached = 0;
|
||||
dec_result w = wuffs_decode(&dc, &ch, m, n, wout);
|
||||
dec_result c = c_decode(&dc, &ch, m, n, cout, &c_attached);
|
||||
if (dc.report_md && (w.cls == CLS_NEEDS_MORE_OUTPUT)) {
|
||||
// A full dst stops Wuffs at a metadata block, but not libbrotlidec,
|
||||
// so this is inconclusive. Compare without reporting instead.
|
||||
g_dec_md_inconclusive++;
|
||||
dc.report_md = false;
|
||||
w = wuffs_decode(&dc, &ch, m, n, wout);
|
||||
c = c_decode(&dc, &ch, m, n, cout, &c_attached);
|
||||
dc.report_md = true;
|
||||
} else if (dc.report_md) {
|
||||
// Before an error, both have reported the same metadata, except that
|
||||
// one can have more of the last block's contents (or also the next
|
||||
// block's start). After success, they agree exactly.
|
||||
size_t k = (w.md_n < c.md_n) ? w.md_n : c.md_n;
|
||||
const char* mwhy = NULL;
|
||||
if (c.md_n == SIZE_MAX) {
|
||||
mwhy = "libbrotlidec metadata overflow";
|
||||
} else if (k && memcmp(g_wmd, g_cmd, k)) {
|
||||
mwhy = "different metadata";
|
||||
} else if ((w.cls == CLS_OK) && (c.cls == CLS_OK) &&
|
||||
(w.md_n != c.md_n)) {
|
||||
mwhy = "different metadata lengths";
|
||||
}
|
||||
if ((w.cls == CLS_OK) && (c.cls == CLS_OK)) {
|
||||
g_dec_md_compared++;
|
||||
for (size_t j = 0; j + 4 <= w.md_n;) {
|
||||
size_t len = (size_t)g_wmd[j] | ((size_t)g_wmd[j + 1] << 8) |
|
||||
((size_t)g_wmd[j + 2] << 16) |
|
||||
((size_t)g_wmd[j + 3] << 24);
|
||||
g_dec_md_blocks++;
|
||||
j += 4 + len;
|
||||
}
|
||||
}
|
||||
if (mwhy) {
|
||||
char desc[4096];
|
||||
describe_dec(desc, sizeof desc, mwhy, s, &dc, &ch, &w, &c, n);
|
||||
save_failure("md", desc, m, n, &dc.dicts);
|
||||
}
|
||||
}
|
||||
g_dec_decodes += 2;
|
||||
g_dec_cls_counts[pass][cls_index(c.cls)]++;
|
||||
const char* why = NULL;
|
||||
@@ -1948,6 +2083,10 @@ run_dec(uint64_t iters, double secs) {
|
||||
g_dec_wuffs_shorter_output);
|
||||
printf(" known BLOCK_LENGTH_1/2 deviations (chunked)=%" PRIu64 "\n",
|
||||
g_dec_known_block_length);
|
||||
printf(" report metadata=%" PRIu64 " (compared after success=%" PRIu64
|
||||
", blocks=%" PRIu64 ", inconclusive=%" PRIu64 ")\n",
|
||||
g_dec_report_md, g_dec_md_compared, g_dec_md_blocks,
|
||||
g_dec_md_inconclusive);
|
||||
static const char* names[23] = {
|
||||
"other", "ok", "needs-more-input", "needs-more-output",
|
||||
"-1", "-2", "-3", "-4",
|
||||
|
||||
+46
-12
@@ -6,8 +6,9 @@ and a built-in static dictionary. It is specified by [RFC
|
||||
7932](https://www.rfc-editor.org/rfc/rfc7932). The reference implementation is
|
||||
at [github.com/google/brotli](https://github.com/google/brotli).
|
||||
|
||||
This package (`std/brotli`) is a decoder. It aims for exact parity with the
|
||||
reference implementation's decoder (libbrotlidec), version v1.2.1, and in
|
||||
This package (`std/brotli`) has a decoder and an encoder (see the "Encoder"
|
||||
section below). The decoder aims for exact parity with the reference
|
||||
implementation's decoder (libbrotlidec), version v1.2.1, and in
|
||||
particular its `c/dec/decode.c` file: the same output for valid input and the
|
||||
same verdict (and the same error code, mapped as below) for invalid input. The
|
||||
Wuffs code mirrors the C code's logic and validation checks, function by
|
||||
@@ -185,8 +186,6 @@ invariants for memory safety but Wuffs requires explicit bounds checks.
|
||||
|
||||
## TODO
|
||||
|
||||
- Metadata callbacks (metadata blocks are currently skipped). See the
|
||||
"Metadata Blocks" section below.
|
||||
- More fast paths, for example for dictionary words, for copies that wrap
|
||||
around the ring buffer and for block switches.
|
||||
|
||||
@@ -881,6 +880,12 @@ round trips through the decoder. Its large-window tests cover qualities 3 to
|
||||
9 with lgwin 18 to 30 (and the H35, H55 and H65 hashers) on small files and
|
||||
on a 46 MiB input with repeats at distances of about 20 and 40 MiB, decoded
|
||||
by both this package's decoder and libbrotlidec.
|
||||
`script/manual-test-brotli-float.c` checks the encoder's package-private
|
||||
float fast paths (`encode_float.wuffs`, which spare the hot loops the
|
||||
cross-package `std/softfloat` calls) against the C compiler's hardware float
|
||||
and double arithmetic, for operands biased towards their edge cases, and
|
||||
checks the integer bit helpers (`log2_floor_non_zero` for every 32-bit value,
|
||||
and `ctz64`).
|
||||
`script/manual-test-brotli-encoder-parity.c` runs a larger grid over every
|
||||
file in `test/data/brotli`, some larger files in `test/data` (up to about
|
||||
400 KiB) and some synthetic inputs (up to 1 MiB). Its `-q=LO-HI` and
|
||||
@@ -927,9 +932,19 @@ than 12 GiB (large-window brotli with lgwin 30 and lgblock 24). `-oneshot=W`
|
||||
checks one configuration per input and quality (lgwin W, the whole input
|
||||
in one `FINISH` step), e.g. for the 3 MiB `test/data/harvesters.bmp` at
|
||||
qualities 10 and 11 with lgwin 22 and 24, also byte-exact. At qualities 10
|
||||
and 11 the Wuffs encoder is roughly 7 times slower than libbrotlienc,
|
||||
and 11 the Wuffs encoder is roughly 6 times slower than libbrotlienc,
|
||||
so the full `-q=10-11` grid takes a few CPU hours.
|
||||
|
||||
`script/manual-test-brotli-fuzz.c` is a differential fuzzer (see its header
|
||||
comment) that feeds the same random inputs, parameters and operation
|
||||
sequences to both libraries. On the final code it ran 1,014,752 random
|
||||
encoder configurations (5,936 of them at qualities 10 and 11), 3.76 million
|
||||
mutated decoder inputs (each decoded four ways, about 180 thousand of them
|
||||
also with `QUIRK_REPORT_METADATA`, comparing 24.9 million metadata blocks
|
||||
with libbrotlidec's callbacks) and 289,484 API edge case checks, all without
|
||||
an unexplained difference. The differences that it found are documented
|
||||
above (and in "Status Mapping"), or were fixed.
|
||||
|
||||
|
||||
### Encoder Differences from the C Code
|
||||
|
||||
@@ -1118,9 +1133,19 @@ metablock) for as long as it needs the results.
|
||||
- `workbuf_len` is a worst case (or, at qualities 10 and 11, a practical
|
||||
bound). A future option could take the input length (like the C code's
|
||||
size hint) to give a tighter bound.
|
||||
- Speed: at qualities 10 and 11 the Wuffs encoder is several times slower
|
||||
than libbrotlienc, mostly because of the soft float arithmetic in the
|
||||
Zopfli cost model, the block splitter and histogram clustering.
|
||||
- Speed. On a mix of text, JSON, map data, an image and an ISO image (on
|
||||
x86-64, lgwin 22, one call each, reusing the workbuf), the Wuffs encoder
|
||||
takes about 1.2 to 1.8 times libbrotlienc's time at qualities 0 to 8 (about
|
||||
1.8 times at the qualities 4 to 6 that web servers use for on-the-fly
|
||||
compression) and about the same time at quality 9. At qualities 10 and 11
|
||||
it takes about 2 times libbrotlienc's time on text and 3.5 to 4 times on
|
||||
images (such as the 3 MiB `test/data/harvesters.bmp`), mostly because of
|
||||
the soft float arithmetic in the Zopfli cost model, the block splitter and
|
||||
histogram clustering. Decoding takes about 1.2 to 1.6 times libbrotlidec's
|
||||
time. The workbuf is sized for the window (about 48 to 62 MiB at lgwin 22
|
||||
and qualities 5 to 9), so callers that compress many small inputs should
|
||||
reuse it: allocating and first touching a fresh one for each call costs
|
||||
more than the compression of a small input.
|
||||
- Serialized (shared) dictionaries, which only an experimental
|
||||
(`BROTLI_EXPERIMENTAL`) libbrotlienc build supports.
|
||||
- Changing parameters mid-stream.
|
||||
@@ -1264,7 +1289,16 @@ Differences from the C code:
|
||||
|
||||
libbrotlidec can report metadata blocks' contents through callbacks
|
||||
(`BrotliDecoderSetMetadataCallbacks`). Wuffs code cannot call back into its
|
||||
caller, so there is no equivalent: metadata blocks are skipped (validated and
|
||||
consumed but not reported), exactly as libbrotlidec does without callbacks,
|
||||
so the decoded output is the same. A future quirk could expose metadata
|
||||
blocks, e.g. by suspending with a status when one starts.
|
||||
caller. By default, metadata blocks are skipped (validated and consumed but
|
||||
not reported), exactly as libbrotlidec does without callbacks.
|
||||
|
||||
With the `QUIRK_REPORT_METADATA` quirk, the decoder reports them instead. At
|
||||
each metadata block (after every previously decoded byte has been written to
|
||||
`dst`), `transform_io` returns `base."@metadata reported"`, and
|
||||
`metadata_length()` returns the block's length, which can be zero (the C
|
||||
code's start callback's `size`). The next `metadata_length()` bytes that
|
||||
`transform_io` writes to `dst` (over one or more subsequent calls) are the
|
||||
block's contents (the C code's chunk callbacks' data). Decoded bytes follow as
|
||||
usual. The decoded output (the bytes outside of metadata blocks) is the same
|
||||
with or without the quirk. `test_mimic_brotli_decode_metadata` checks that the
|
||||
lengths and contents match libbrotlidec's callbacks.
|
||||
@@ -15,12 +15,12 @@
|
||||
// particular its c/dec/decode.c file. Comments like "Mirrors Foo in decode.c"
|
||||
// name the C function whose logic (and validation checks) a Wuffs function
|
||||
// mirrors. The C code's explicit BROTLI_STATE_ETC state machine is replaced by
|
||||
// Wuffs coroutines. The C code's "safe" and "unsafe" (fast path) variants are
|
||||
// implemented once, as the "safe" (read as few bits as possible) variant.
|
||||
// Wuffs coroutines. The C code's "safe" variants (read as few bits as
|
||||
// possible, so that decoding can suspend anywhere) are coroutines and its
|
||||
// "unsafe" fast path (ProcessCommands) is process_commands_fast, which reads
|
||||
// ahead and gives back the unused bytes (see README.md).
|
||||
//
|
||||
// See README.md for how C error codes map to Wuffs statuses.
|
||||
//
|
||||
// TODO: support metadata callbacks and fast paths.
|
||||
|
||||
pub status "#bad block length"
|
||||
pub status "#bad block switch"
|
||||
@@ -245,6 +245,17 @@ pub struct decoder? implements base.io_transformer(
|
||||
quirk_large_window : base.bool,
|
||||
large_window : base.bool,
|
||||
|
||||
// quirk_report_metadata is the QUIRK_REPORT_METADATA quirk.
|
||||
// md_length is the length of the last reported metadata block.
|
||||
// md_note_pending is whether do_transform_io has suspended (with
|
||||
// "$short write") so that transform_io returns "@metadata reported".
|
||||
// md_copying is whether do_transform_io is writing a metadata block's
|
||||
// contents to args.dst.
|
||||
quirk_report_metadata : base.bool,
|
||||
md_length : base.u32[..= 0x100_0000],
|
||||
md_note_pending : base.bool,
|
||||
md_copying : base.bool,
|
||||
|
||||
// started is whether the C code's s->state would no longer be
|
||||
// BROTLI_STATE_UNINITED. Quirks and dictionaries can only be set or
|
||||
// added before then.
|
||||
@@ -355,6 +366,8 @@ pub struct decoder? implements base.io_transformer(
|
||||
pub func decoder.get_quirk(key: base.u32) base.u64 {
|
||||
if (args.key == QUIRK_LARGE_WINDOW) and this.quirk_large_window {
|
||||
return 1
|
||||
} else if (args.key == QUIRK_REPORT_METADATA) and this.quirk_report_metadata {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -368,10 +381,22 @@ pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
|
||||
}
|
||||
this.quirk_large_window = args.value > 0
|
||||
return ok
|
||||
} else if args.key == QUIRK_REPORT_METADATA {
|
||||
if this.started {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
this.quirk_report_metadata = args.value > 0
|
||||
return ok
|
||||
}
|
||||
return base."#unsupported option"
|
||||
}
|
||||
|
||||
// metadata_length returns the length of the metadata block that was last
|
||||
// reported (see QUIRK_REPORT_METADATA).
|
||||
pub func decoder.metadata_length() base.u64 {
|
||||
return this.md_length as base.u64
|
||||
}
|
||||
|
||||
pub func decoder.dst_history_retain_length() base.optional_u63 {
|
||||
return this.util.make_optional_u63(has_value: true, value: 0)
|
||||
}
|
||||
@@ -409,7 +434,7 @@ pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
if this.dict_total > args.workbuf.length() {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
status =? this.do_transform_io?(src: args.src,
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src,
|
||||
workbuf: args.workbuf[this.dict_total ..],
|
||||
dictbuf: args.workbuf[.. this.dict_total])
|
||||
if this.dict_total > args.workbuf.length() {
|
||||
@@ -428,7 +453,10 @@ pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
return ok
|
||||
|
||||
} else if status == base."$short write" {
|
||||
if this.is_fully_flushed() {
|
||||
if this.md_note_pending {
|
||||
this.md_note_pending = false
|
||||
return base."@metadata reported"
|
||||
} else if this.is_fully_flushed() and (not this.md_copying) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -471,7 +499,7 @@ pri func decoder.flush!(dst: base.io_writer, workbuf: slice base.u8) {
|
||||
|
||||
// Mirrors the BROTLI_STATE_ETC state machine in BrotliDecoderDecompressStream
|
||||
// in decode.c.
|
||||
pri func decoder.do_transform_io?(src: base.io_reader, workbuf: slice base.u8, dictbuf: roslice base.u8) {
|
||||
pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8, dictbuf: roslice base.u8) {
|
||||
var w : base.u64
|
||||
var aligned : base.bool
|
||||
|
||||
@@ -508,9 +536,11 @@ pri func decoder.do_transform_io?(src: base.io_reader, workbuf: slice base.u8, d
|
||||
|
||||
if this.is_metadata {
|
||||
// BROTLI_STATE_METADATA. Mirrors SkipMetadataBlock.
|
||||
//
|
||||
// TODO: support metadata callbacks.
|
||||
args.src.skip?(n: this.mb_remaining as base.u64)
|
||||
if this.quirk_report_metadata {
|
||||
this.report_metadata?(dst: args.dst, src: args.src)
|
||||
} else {
|
||||
args.src.skip?(n: this.mb_remaining as base.u64)
|
||||
}
|
||||
this.mb_remaining = 0
|
||||
|
||||
} else if this.mb_remaining > 0 {
|
||||
@@ -538,6 +568,36 @@ pri func decoder.do_transform_io?(src: base.io_reader, workbuf: slice base.u8, d
|
||||
}
|
||||
}
|
||||
|
||||
// report_metadata mirrors the C code's metadata callbacks (the start callback
|
||||
// in BROTLI_STATE_METABLOCK_HEADER and the chunk callbacks in
|
||||
// SkipMetadataBlock). It waits until every decoded byte has been written to
|
||||
// args.dst, then has transform_io return "@metadata reported" and then copies
|
||||
// the metadata block's contents from args.src to args.dst.
|
||||
pri func decoder.report_metadata?(dst: base.io_writer, src: base.io_reader) {
|
||||
var n : base.u32
|
||||
|
||||
while not this.is_fully_flushed() {
|
||||
yield? base."$short write"
|
||||
}
|
||||
this.md_length = this.mb_remaining.min(no_more_than: 0x100_0000)
|
||||
this.md_note_pending = true
|
||||
yield? base."$short write"
|
||||
|
||||
this.md_copying = true
|
||||
while this.mb_remaining > 0 {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
} else if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
n = args.dst.limited_copy_u32_from_reader!(up_to: this.mb_remaining as base.u32, r: args.src)
|
||||
this.mb_remaining ~mod-= n
|
||||
}
|
||||
this.md_copying = false
|
||||
}
|
||||
|
||||
// initialize_codes sets up the decoder state that the C code sets in
|
||||
// BrotliDecoderStateInit (in state.c). It also builds the fixed table for
|
||||
// decoding the code length code lengths (kCodeLengthPrefixLength and
|
||||
|
||||
@@ -738,6 +738,13 @@ pri func decoder.process_commands_fast!(src: base.io_reader, workbuf: slice base
|
||||
var dhi : base.u64
|
||||
var slo : base.u64
|
||||
var shi : base.u64
|
||||
var cm_slice : base.u32[..= 16320]
|
||||
var wmask : base.u32
|
||||
var pos : base.u32
|
||||
var lit_n : base.u32
|
||||
var lit_i : base.u32
|
||||
var wi : base.u64
|
||||
var copied16 : base.bool
|
||||
|
||||
if this.n_bits >= 8 {
|
||||
return ok
|
||||
@@ -829,26 +836,38 @@ pri func decoder.process_commands_fast!(src: base.io_reader, workbuf: slice base
|
||||
}
|
||||
p1 = this.get_byte(workbuf: args.workbuf, distance: 1) as base.u32
|
||||
p2 = this.get_byte(workbuf: args.workbuf, distance: 2) as base.u32
|
||||
while this.insert_length > 0 {
|
||||
if (this.block_length[0] == 0) or ((this.pos ~mod+ 1) >= this.rb_size) {
|
||||
break.outer
|
||||
}
|
||||
mode_offset = this.context_mode << 9
|
||||
cm_slice = this.context_map_slice
|
||||
wmask = this.window_size ~mod- 1
|
||||
pos = this.pos
|
||||
// lit_n is how many literals the loop below decodes before this
|
||||
// command's literals, this block's literals or the ring buffer
|
||||
// (before the next ring_event) run out. The loop keeps pos, the
|
||||
// counts and the symbol in local variables, and writes them back
|
||||
// to this.etc fields before every break.
|
||||
lit_n = this.insert_length.min(no_more_than: this.block_length[0])
|
||||
if (pos ~mod+ 1) >= this.rb_size {
|
||||
lit_n = 0
|
||||
} else {
|
||||
lit_n = lit_n.min(no_more_than: (this.rb_size ~mod- pos) ~mod- 1)
|
||||
}
|
||||
lit_i = 0
|
||||
while lit_i < lit_n {
|
||||
if n_bits < 24 {
|
||||
if args.src.length() < 8 {
|
||||
break.outer
|
||||
break
|
||||
}
|
||||
bits |= args.src.peek_u64le() ~mod<< n_bits
|
||||
args.src.skip_u32_fast!(actual: (63 - n_bits) >> 3, worst_case: 8)
|
||||
n_bits |= 56
|
||||
}
|
||||
mode_offset = this.context_mode << 9
|
||||
ctx = (CONTEXT_LOOKUP[mode_offset | p1] |
|
||||
CONTEXT_LOOKUP[mode_offset | 0x100 | p2]) as base.u32
|
||||
tree = this.context_maps[this.context_map_slice + ctx] as base.u32
|
||||
tree = this.context_maps[cm_slice + ctx] as base.u32
|
||||
i = (LIT_HUFFS_BASE + (tree * LIT_HUFFS_STRIDE)) ~mod+ ((bits & 0xFF) as base.u32)
|
||||
if i >= HUFFS_LEN {
|
||||
ret = "#internal error: inconsistent Huffman table"
|
||||
break.outer
|
||||
break
|
||||
}
|
||||
e = this.huffs[i] as base.u32
|
||||
e_bits = e & 15
|
||||
@@ -856,23 +875,34 @@ pri func decoder.process_commands_fast!(src: base.io_reader, workbuf: slice base
|
||||
i = ((LIT_HUFFS_BASE + (tree * LIT_HUFFS_STRIDE)) ~mod+ (e >> 4)) ~mod+ ((((bits >> 8) & 0x7FFF) as base.u32) & (((1 as base.u32) << (e_bits - 8)) - 1))
|
||||
if i >= HUFFS_LEN {
|
||||
ret = "#internal error: inconsistent Huffman table"
|
||||
break.outer
|
||||
break
|
||||
}
|
||||
e = this.huffs[i] as base.u32
|
||||
e_bits = (e & 15) + 8
|
||||
}
|
||||
if n_bits < e_bits {
|
||||
ret = "#internal error: inconsistent bit reader"
|
||||
break.outer
|
||||
break
|
||||
}
|
||||
bits >>= e_bits
|
||||
n_bits -= e_bits
|
||||
this.symbol = e >> 4
|
||||
this.put_byte!(workbuf: args.workbuf, b: (this.symbol & 0xFF) as base.u8)
|
||||
p2 = p1
|
||||
p1 = this.symbol & 0xFF
|
||||
this.block_length[0] ~mod-= 1
|
||||
this.insert_length ~mod-= 1
|
||||
p1 = (e >> 4) & 0xFF
|
||||
wi = (pos & wmask) as base.u64
|
||||
if wi < args.workbuf.length() {
|
||||
args.workbuf[wi] = p1 as base.u8
|
||||
}
|
||||
pos ~mod+= 1
|
||||
lit_i ~mod+= 1
|
||||
}
|
||||
if lit_i > 0 {
|
||||
this.symbol = p1
|
||||
this.pos = pos
|
||||
this.block_length[0] ~mod-= lit_i
|
||||
this.insert_length ~mod-= lit_i
|
||||
}
|
||||
if (ret.is_error()) or (this.insert_length > 0) {
|
||||
break.outer
|
||||
}
|
||||
this.cmd_state = 2
|
||||
if (this.mb_remaining == 0) or (this.mb_remaining >= 0x8000_0000) {
|
||||
@@ -971,6 +1001,18 @@ pri func decoder.process_commands_fast!(src: base.io_reader, workbuf: slice base
|
||||
this.mb_remaining ~mod-= n
|
||||
// Copy in chunks of at most distance bytes, like process_commands.
|
||||
k64 = 0
|
||||
if (n <= 16) and (distance >= 16) and
|
||||
((dst_i ~mod+ 16) <= (this.rb_size as base.u64)) and
|
||||
((src_i ~mod+ 16) <= (this.window_size as base.u64)) {
|
||||
// Like the C code, copy 16 bytes, overwriting up to 16 - n bytes
|
||||
// after the copy's end. Those bytes are more than the maximum
|
||||
// backward distance (BROTLI_WINDOW_GAP is 16) away from any later
|
||||
// position, so they are never read before being rewritten.
|
||||
copied16 = this.copy16!(workbuf: args.workbuf, d: dst_i, s: src_i)
|
||||
if copied16 {
|
||||
k64 = n as base.u64
|
||||
}
|
||||
}
|
||||
while k64 < (n as base.u64) {
|
||||
n64 = (n as base.u64) ~mod- k64
|
||||
n64 = n64.min(no_more_than: distance as base.u64)
|
||||
@@ -1098,3 +1140,34 @@ pri func decoder.take_distance_from_ring_buffer!(code: base.u32[..= 15]) {
|
||||
this.distance_code = v - 3
|
||||
}
|
||||
}
|
||||
|
||||
// copy16 copies the 16 bytes at workbuf[s ..] to workbuf[d ..], if both are
|
||||
// in bounds (and returns whether it did). The two ranges must not overlap.
|
||||
pri func decoder.copy16!(workbuf: slice base.u8, d: base.u64, s: base.u64) base.bool {
|
||||
var lo : base.u64
|
||||
var hi : base.u64
|
||||
var q : slice base.u8
|
||||
var r : slice base.u8
|
||||
|
||||
if (args.s > args.workbuf.length()) or (args.d > args.workbuf.length()) {
|
||||
return false
|
||||
}
|
||||
q = args.workbuf[args.s ..]
|
||||
if q.length() >= 16 {
|
||||
lo = q.peek_u64le()
|
||||
q = q[8 ..]
|
||||
if q.length() >= 8 {
|
||||
hi = q.peek_u64le()
|
||||
q = args.workbuf[args.d ..]
|
||||
if q.length() >= 16 {
|
||||
r = q[8 ..]
|
||||
q.poke_u64le!(a: lo)
|
||||
if r.length() >= 8 {
|
||||
r.poke_u64le!(a: hi)
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -30,3 +30,19 @@ pri const QUIRKS_BASE : base.u32 = 0x2AB7_9000
|
||||
// is rejected with "#unsupported large window". With this quirk, a stream that
|
||||
// does not start with that escape is decoded as standard brotli.
|
||||
pub const QUIRK_LARGE_WINDOW : base.u32 = 0x2AB7_9000 | 0x00
|
||||
|
||||
// When this quirk is set, a positive value makes the decoder report metadata
|
||||
// blocks, like the C code's BrotliDecoderSetMetadataCallbacks. Zero (the
|
||||
// default) means that metadata blocks are skipped, like the C code without
|
||||
// callbacks.
|
||||
//
|
||||
// When reporting, at each metadata block, after all of the previously decoded
|
||||
// bytes have been written to dst, transform_io returns
|
||||
// base."@metadata reported" and metadata_length returns the block's length
|
||||
// (the C code's start callback's size argument), which can be zero. The next
|
||||
// metadata_length bytes that transform_io writes to dst (in subsequent calls)
|
||||
// are the metadata block's contents (the C code's chunk callbacks' data),
|
||||
// after which decoded bytes follow as usual.
|
||||
//
|
||||
// It must be set before decoding starts. This quirk is decoder-only.
|
||||
pub const QUIRK_REPORT_METADATA : base.u32 = 0x2AB7_9000 | 0x0A
|
||||
@@ -54,8 +54,18 @@ pri struct bit_cost?(
|
||||
// (but log2_cache need not be), so the cache starts empty.
|
||||
log2_valid : array[1024] base.u64,
|
||||
|
||||
// population_cost's per-call memo of its per-symbol values, for
|
||||
// symbol counts less than 256: pc_term[c] and pc_depth[c] are valid
|
||||
// during the call numbered pc_call if pc_valid[c] == pc_call. Call
|
||||
// numbers start at 1, so the zeroed pc_valid starts empty.
|
||||
pc_call : base.u32,
|
||||
pc_valid : array[256] base.u32,
|
||||
|
||||
sf : softfloat.arith,
|
||||
) + (
|
||||
pc_term : array[256] base.u64,
|
||||
pc_depth : array[256] base.u8,
|
||||
|
||||
// log2_cache[v] is FastLog2(v), for 256 <= v < 65536, filled lazily
|
||||
// (see fast_log2_memo). It is only a speed optimization (glibc's log2
|
||||
// is expensive to emulate): the values are computed by the same
|
||||
@@ -166,17 +176,22 @@ pri func bit_cost.bits_entropy!(population: roslice base.u8, size: base.u64) bas
|
||||
q = q[4 ..]
|
||||
}
|
||||
sum ~mod+= p
|
||||
if p <= 1 {
|
||||
// FastLog2(p) is +0, so (p * FastLog2(p)) is +0 and subtracting
|
||||
// it leaves retval (which is never a NaN) unchanged.
|
||||
continue
|
||||
}
|
||||
lp = this.fast_log2_memo!(v: p)
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: lp))
|
||||
retval = this.f64_sub(a: retval, b: this.f64_mul(
|
||||
a: this.f64_from_u64(a: p), b: lp))
|
||||
}
|
||||
if sum <> 0 {
|
||||
lp = this.fast_log2_memo!(v: sum)
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: lp))
|
||||
retval = this.f64_add(a: retval, b: this.f64_mul(
|
||||
a: this.f64_from_u64(a: sum), b: lp))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
if this.f64_lt(a: retval, b: this.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.f64_from_u64(a: sum)
|
||||
}
|
||||
return retval
|
||||
}
|
||||
@@ -184,6 +199,7 @@ pri func bit_cost.bits_entropy!(population: roslice base.u8, size: base.u64) bas
|
||||
// Mirrors BrotliPopulationCostLiteral, BrotliPopulationCostCommand and
|
||||
// BrotliPopulationCostDistance (FN(BrotliPopulationCost) in bit_cost_inc.h).
|
||||
// histogram is a histogram of data_size symbols (see the layout above).
|
||||
|
||||
pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u32[..= 704]) base.u64 {
|
||||
var total : base.u64
|
||||
var count : base.u32
|
||||
@@ -209,6 +225,7 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
var sum : base.u64
|
||||
var retval : base.u64
|
||||
var p : base.u64
|
||||
var term : base.u64
|
||||
|
||||
total = this.ld64(s: args.histogram, i: (args.data_size as base.u64) * 4)
|
||||
if total == 0 {
|
||||
@@ -234,7 +251,7 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
}
|
||||
if count == 2 {
|
||||
// kTwoSymbolHistogramCost + (double)total_count_.
|
||||
return this.sf.f64_add(a: 0x4034_0000_0000_0000, b: this.sf.f64_from_u64(a: total))
|
||||
return this.f64_add(a: 0x4034_0000_0000_0000, b: this.f64_from_u64(a: total))
|
||||
}
|
||||
if count == 3 {
|
||||
h0 = this.ld32(s: args.histogram, i: (s[0] as base.u64) * 4)
|
||||
@@ -243,11 +260,11 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
histomax = h0.max(no_less_than: h1.max(no_less_than: h2))
|
||||
// kThreeSymbolHistogramCost + 2 * (histo0 + histo1 + histo2) -
|
||||
// histomax, where the integer expression is uint32_t arithmetic.
|
||||
return this.sf.f64_sub(
|
||||
a: this.sf.f64_add(
|
||||
return this.f64_sub(
|
||||
a: this.f64_add(
|
||||
a: 0x403C_0000_0000_0000,
|
||||
b: this.sf.f64_from_u32(a: 2 ~mod* ((h0 ~mod+ h1) ~mod+ h2))),
|
||||
b: this.sf.f64_from_u32(a: histomax))
|
||||
b: this.f64_from_u32(a: 2 ~mod* ((h0 ~mod+ h1) ~mod+ h2))),
|
||||
b: this.f64_from_u32(a: histomax))
|
||||
}
|
||||
if count == 4 {
|
||||
i = 0
|
||||
@@ -273,28 +290,48 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
histomax = h23.max(no_less_than: histo[0])
|
||||
// kFourSymbolHistogramCost + 3 * h23 + 2 * (histo[0] + histo[1]) -
|
||||
// histomax, evaluated left to right.
|
||||
return this.sf.f64_sub(
|
||||
a: this.sf.f64_add(
|
||||
a: this.sf.f64_add(
|
||||
return this.f64_sub(
|
||||
a: this.f64_add(
|
||||
a: this.f64_add(
|
||||
a: 0x4042_8000_0000_0000,
|
||||
b: this.sf.f64_from_u32(a: 3 ~mod* h23)),
|
||||
b: this.sf.f64_from_u32(a: 2 ~mod* (histo[0] ~mod+ histo[1]))),
|
||||
b: this.sf.f64_from_u32(a: histomax))
|
||||
b: this.f64_from_u32(a: 3 ~mod* h23)),
|
||||
b: this.f64_from_u32(a: 2 ~mod* (histo[0] ~mod+ histo[1]))),
|
||||
b: this.f64_from_u32(a: histomax))
|
||||
}
|
||||
|
||||
// Compute the entropy of the histogram and simultaneously build a
|
||||
// simplified histogram of the code length codes.
|
||||
max_depth = 1
|
||||
log2total = this.fast_log2_memo!(v: total)
|
||||
this.pc_call ~mod+= 1
|
||||
if this.pc_call == 0 {
|
||||
this.pc_valid[.. 256].bulk_memset!(byte_value: 0)
|
||||
this.pc_call = 1
|
||||
}
|
||||
i = 0
|
||||
while i < args.data_size {
|
||||
c = this.ld32(s: args.histogram, i: (i as base.u64) * 4)
|
||||
if c > 0 {
|
||||
lp = this.fast_log2_memo!(v: c as base.u64)
|
||||
log2p = this.sf.f64_sub(a: log2total, b: lp)
|
||||
depth = this.sf.f64_to_u64_trunc(a: this.sf.f64_add(a: log2p, b: 0x3FE0_0000_0000_0000))
|
||||
bits = this.sf.f64_add(a: bits, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u32(a: c), b: log2p))
|
||||
// term is (c * log2p) and depth is (size_t)(log2p + 0.5) (capped
|
||||
// at 15), which only depend on c (and the fixed total).
|
||||
if (c < 256) and (this.pc_valid[c & 255] == this.pc_call) {
|
||||
term = this.pc_term[c & 255]
|
||||
depth = this.pc_depth[c & 255] as base.u64
|
||||
} else {
|
||||
lp = this.fast_log2_memo!(v: c as base.u64)
|
||||
log2p = this.f64_sub(a: log2total, b: lp)
|
||||
depth = this.f64_to_u64_trunc(a: this.f64_add(a: log2p, b: 0x3FE0_0000_0000_0000))
|
||||
term = this.f64_mul(a: this.f64_from_u32(a: c), b: log2p)
|
||||
if depth > 15 {
|
||||
depth = 15
|
||||
}
|
||||
if c < 256 {
|
||||
this.pc_valid[c & 255] = this.pc_call
|
||||
this.pc_term[c & 255] = term
|
||||
this.pc_depth[c & 255] = (depth & 15) as base.u8
|
||||
}
|
||||
}
|
||||
bits = this.f64_add(a: bits, b: term)
|
||||
if depth > 15 {
|
||||
depth = 15
|
||||
}
|
||||
@@ -326,14 +363,14 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
while reps > 0 {
|
||||
depth_histo[17] ~mod+= 1
|
||||
// Add the 3 extra bits for the 17 code length code.
|
||||
bits = this.sf.f64_add(a: bits, b: 0x4008_0000_0000_0000)
|
||||
bits = this.f64_add(a: bits, b: 0x4008_0000_0000_0000)
|
||||
reps >>= 3
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add the estimated encoding cost of the code length code histogram.
|
||||
bits = this.sf.f64_add(a: bits, b: this.sf.f64_from_u64(a: 18 + (2 * max_depth)))
|
||||
bits = this.f64_add(a: bits, b: this.f64_from_u64(a: 18 + (2 * max_depth)))
|
||||
|
||||
// Add the entropy of the code length code histogram. This is
|
||||
// BrotliBitsEntropy(depth_histo, BROTLI_CODE_LENGTH_CODES), inlined
|
||||
@@ -342,18 +379,22 @@ pri func bit_cost.population_cost!(histogram: roslice base.u8, data_size: base.u
|
||||
while i < 18 {
|
||||
p = depth_histo[i] as base.u64
|
||||
sum ~mod+= p
|
||||
lp = this.fast_log2_memo!(v: p)
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: lp))
|
||||
i ~mod+= 1
|
||||
if p <= 1 {
|
||||
// As in bits_entropy, this would leave retval unchanged.
|
||||
continue
|
||||
}
|
||||
lp = this.fast_log2_memo!(v: p)
|
||||
retval = this.f64_sub(a: retval, b: this.f64_mul(
|
||||
a: this.f64_from_u64(a: p), b: lp))
|
||||
}
|
||||
if sum <> 0 {
|
||||
lp = this.fast_log2_memo!(v: sum)
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: lp))
|
||||
retval = this.f64_add(a: retval, b: this.f64_mul(
|
||||
a: this.f64_from_u64(a: sum), b: lp))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
if this.f64_lt(a: retval, b: this.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.f64_from_u64(a: sum)
|
||||
}
|
||||
return this.sf.f64_add(a: bits, b: retval)
|
||||
return this.f64_add(a: bits, b: retval)
|
||||
}
|
||||
@@ -125,33 +125,27 @@ pri func encoder.store_meta_block_header!(len: base.u64, is_uncompressed: base.b
|
||||
}
|
||||
}
|
||||
|
||||
// LOG2_DEBRUIJN32 maps (v * 0x07C4_ACDD) >> 27, for v = (1 << (k + 1)) - 1,
|
||||
// to k.
|
||||
pri const LOG2_DEBRUIJN32 : roarray[32] base.u32[..= 31] = [
|
||||
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
||||
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31,
|
||||
]
|
||||
|
||||
// Mirrors Log2FloorNonZero in fast_log.h, which (with BROTLI_BSR32) truncates
|
||||
// its argument to 32 bits. It returns 0 for an argument of 0.
|
||||
pri func encoder.log2_floor_non_zero(n: base.u64) base.u32[..= 31] {
|
||||
var x : base.u32
|
||||
var r : base.u32
|
||||
|
||||
// Smear the highest set bit of the low 32 bits of n rightwards, then
|
||||
// look up its position.
|
||||
x = (args.n & 0xFFFF_FFFF) as base.u32
|
||||
if x >= 0x1_0000 {
|
||||
x >>= 16
|
||||
r = 16
|
||||
}
|
||||
if x >= 0x100 {
|
||||
x >>= 8
|
||||
r ~mod+= 8
|
||||
}
|
||||
if x >= 0x10 {
|
||||
x >>= 4
|
||||
r ~mod+= 4
|
||||
}
|
||||
if x >= 4 {
|
||||
x >>= 2
|
||||
r ~mod+= 2
|
||||
}
|
||||
if x >= 2 {
|
||||
r ~mod+= 1
|
||||
}
|
||||
return r & 31
|
||||
x |= x >> 1
|
||||
x |= x >> 2
|
||||
x |= x >> 4
|
||||
x |= x >> 8
|
||||
x |= x >> 16
|
||||
return LOG2_DEBRUIJN32[(x ~mod* 0x07C4_ACDD) >> 27]
|
||||
}
|
||||
|
||||
// Mirrors FastLog2 in fast_log.h. The result is the bit pattern of an IEEE
|
||||
@@ -175,16 +169,16 @@ pri func encoder.bits_entropy_of_histogram() base.u64 {
|
||||
while i < 256 {
|
||||
p = this.histogram[i] as base.u64
|
||||
sum ~mod+= p
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: this.fast_log2(v: p)))
|
||||
retval = this.bc.f64_sub(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: p), b: this.fast_log2(v: p)))
|
||||
i += 1
|
||||
}
|
||||
if sum <> 0 {
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: this.fast_log2(v: sum)))
|
||||
retval = this.bc.f64_add(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: sum), b: this.fast_log2(v: sum)))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
if this.bc.f64_lt(a: retval, b: this.bc.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.bc.f64_from_u64(a: sum)
|
||||
}
|
||||
return retval
|
||||
}
|
||||
@@ -193,12 +187,10 @@ pri func encoder.bits_entropy_of_histogram() base.u64 {
|
||||
// code's BROTLI_UNALIGNED_LOAD64LE. The C code never reads out of bounds, so
|
||||
// the zero result for an out of bounds read should never be used.
|
||||
pri func encoder.load64le(s: roslice base.u8, i: base.u64) base.u64 {
|
||||
var q : roslice base.u8
|
||||
|
||||
if args.i < args.s.length() {
|
||||
q = args.s[args.i ..]
|
||||
if q.length() >= 8 {
|
||||
return q.peek_u64le()
|
||||
if args.i < 0xFFFF_FFFF_FFFF_0000 {
|
||||
if (args.i + 8) <= args.s.length() {
|
||||
assert args.i <= (args.i + 8) via "a <= (a + b): 0 <= b"(b: 8)
|
||||
return args.s[args.i .. args.i + 8].peek_u64le()
|
||||
}
|
||||
}
|
||||
return 0
|
||||
@@ -206,12 +198,10 @@ pri func encoder.load64le(s: roslice base.u8, i: base.u64) base.u64 {
|
||||
|
||||
// load32le is like load64le but for 4 bytes.
|
||||
pri func encoder.load32le(s: roslice base.u8, i: base.u64) base.u32 {
|
||||
var q : roslice base.u8
|
||||
|
||||
if args.i < args.s.length() {
|
||||
q = args.s[args.i ..]
|
||||
if q.length() >= 4 {
|
||||
return q.peek_u32le()
|
||||
if args.i < 0xFFFF_FFFF_FFFF_0000 {
|
||||
if (args.i + 4) <= args.s.length() {
|
||||
assert args.i <= (args.i + 4) via "a <= (a + b): 0 <= b"(b: 4)
|
||||
return args.s[args.i .. args.i + 4].peek_u32le()
|
||||
}
|
||||
}
|
||||
return 0
|
||||
@@ -235,11 +225,7 @@ pri func encoder.find_match_length_with_limit(s: roslice base.u8, i1: base.u64,
|
||||
while (n >= 8) and (a.length() >= 8) and (b.length() >= 8) {
|
||||
x = a.peek_u64le() ^ b.peek_u64le()
|
||||
if x <> 0 {
|
||||
while (x & 0xFF) == 0 {
|
||||
matched ~mod+= 1
|
||||
x >>= 8
|
||||
}
|
||||
return matched
|
||||
return matched ~mod+ (this.ctz64(x: x) >> 3)
|
||||
}
|
||||
matched ~mod+= 8
|
||||
n -= 8
|
||||
|
||||
@@ -295,6 +295,13 @@ pri func metablock_builder.find_blocks!(wb: slice base.u8, data: base.u64, lengt
|
||||
var c : base.u64
|
||||
var cur_id : base.u8
|
||||
var b : base.u8
|
||||
var nh : base.u64[..= 256]
|
||||
var icn : base.u64
|
||||
var best_k : base.u64
|
||||
var use_capped : base.bool
|
||||
var skip_t : base.u64
|
||||
var t : base.u64
|
||||
var n : base.u32
|
||||
|
||||
alphabet_size = args.ds as base.u64
|
||||
bitmap_len = (args.num_histograms ~mod+ 7) >> 3
|
||||
@@ -330,7 +337,7 @@ pri func metablock_builder.find_blocks!(wb: slice base.u8, data: base.u64, lengt
|
||||
k = (i ~mod* args.num_histograms) ~mod+ j
|
||||
if k < 35200 {
|
||||
c = this.bit_cost!(count: this.ld32(wb: args.wb, i: (args.histograms ~mod+ (j ~mod* hs)) ~mod+ (i ~mod* 4)) as base.u64)
|
||||
this.insert_cost[k] = this.sf.f64_sub(a: this.insert_cost[j & 255], b: c)
|
||||
this.insert_cost[k] = this.bc.f64_sub(a: this.insert_cost[j & 255], b: c)
|
||||
}
|
||||
j ~mod+= 1
|
||||
}
|
||||
@@ -344,48 +351,121 @@ pri func metablock_builder.find_blocks!(wb: slice base.u8, data: base.u64, lengt
|
||||
// last position, we need to switch here.
|
||||
this.cost[.. 256].bulk_memset!(byte_value: 0)
|
||||
this.zero!(wb: args.wb, i: args.switch_signal, n: args.length ~mod* bitmap_len)
|
||||
nh = args.num_histograms.min(no_more_than: 256)
|
||||
byte_ix = 0
|
||||
while byte_ix < args.length {
|
||||
ix = byte_ix ~mod* bitmap_len
|
||||
insert_cost_ix = this.sym(wb: args.wb, base: args.data, i: byte_ix, width: args.width) ~mod* args.num_histograms
|
||||
insert_cost_ix = this.sym(wb: args.wb, base: args.data, i: byte_ix, width: args.width) ~mod* nh
|
||||
min_cost = F64_1E99
|
||||
block_switch_cost = args.block_switch_bitcost
|
||||
// insert_cost_ix is (symbol * nh) for a symbol less than
|
||||
// alphabet_size, so (insert_cost_ix + nh) is at most 35200.
|
||||
// Otherwise (which never happens), like the C code, the symbol's
|
||||
// insert costs would be out of bounds (and here, they are zero).
|
||||
icn = 0
|
||||
if insert_cost_ix <= 35200 {
|
||||
icn = nh.min(no_more_than: 35200 - insert_cost_ix)
|
||||
}
|
||||
// From byte 2000 on, block_switch_cost is block_switch_bitcost, so
|
||||
// the many cost[k] values that the previous byte capped at it all add
|
||||
// the same (block_switch_bitcost + insert_cost[i]) values. Memoize
|
||||
// those sums (the same f64_add, on the same operands) in
|
||||
// capped_plus, if there are enough remaining bytes to pay for it.
|
||||
if (byte_ix == 2000) and ((args.length ~mod- 2000) > alphabet_size) and (nh >= 4) {
|
||||
t = alphabet_size ~mod* nh
|
||||
t = t.min(no_more_than: 35200)
|
||||
i = 0
|
||||
while i < t {
|
||||
this.capped_plus[i.min(no_more_than: 35199)] = this.bc.f64_add(
|
||||
a: args.block_switch_bitcost, b: this.insert_cost[i.min(no_more_than: 35199)])
|
||||
i ~mod+= 1
|
||||
}
|
||||
use_capped = true
|
||||
}
|
||||
best_k = 0xFFFF_FFFF_FFFF_FFFF
|
||||
k = 0
|
||||
while k < args.num_histograms {
|
||||
while k < nh {
|
||||
// We are coding the symbol with entropy code k.
|
||||
c = 0
|
||||
if (insert_cost_ix ~mod+ k) < 35200 {
|
||||
c = this.insert_cost[(insert_cost_ix ~mod+ k) % 35200]
|
||||
if k < icn {
|
||||
j = insert_cost_ix ~mod+ k
|
||||
j = j.min(no_more_than: 35199)
|
||||
if use_capped and (this.cost[k & 255] == args.block_switch_bitcost) {
|
||||
c = this.capped_plus[j]
|
||||
} else {
|
||||
c = this.bc.f64_add(a: this.cost[k & 255], b: this.insert_cost[j])
|
||||
}
|
||||
} else {
|
||||
c = this.bc.f64_add(a: this.cost[k & 255], b: 0)
|
||||
}
|
||||
c = this.sf.f64_add(a: this.cost[k & 255], b: c)
|
||||
this.cost[k & 255] = c
|
||||
if this.sf.f64_lt(a: c, b: min_cost) <> 0 {
|
||||
if this.bc.f64_lt(a: c, b: min_cost) <> 0 {
|
||||
min_cost = c
|
||||
this.st8!(wb: args.wb, i: args.block_id ~mod+ byte_ix, v: (k & 0xFF) as base.u8)
|
||||
best_k = k
|
||||
}
|
||||
k ~mod+= 1
|
||||
}
|
||||
if best_k <= 0xFF {
|
||||
this.st8!(wb: args.wb, i: args.block_id ~mod+ byte_ix, v: (best_k & 0xFF) as base.u8)
|
||||
}
|
||||
// More blocks for the beginning.
|
||||
if byte_ix < 2000 {
|
||||
// block_switch_cost *= 0.77 + multiplier * (double)byte_ix, with
|
||||
// multiplier = 0.07 / 2000.
|
||||
block_switch_cost = this.sf.f64_mul(
|
||||
block_switch_cost = this.bc.f64_mul(
|
||||
a: block_switch_cost,
|
||||
b: this.sf.f64_add(
|
||||
b: this.bc.f64_add(
|
||||
a: 0x3FE8_A3D7_0A3D_70A4,
|
||||
b: this.sf.f64_mul(a: 0x3F02_599E_D7C6_FBD3, b: this.sf.f64_from_u64(a: byte_ix))))
|
||||
b: this.bc.f64_mul(a: 0x3F02_599E_D7C6_FBD3, b: this.bc.f64_from_u64(a: byte_ix))))
|
||||
}
|
||||
// S(c) = (c - min_cost), rounded, is monotone non-decreasing in c, so
|
||||
// the c values whose S(c) is at least block_switch_cost (the cost[k]
|
||||
// values that the loop below caps) are those at least some threshold
|
||||
// T. Find T by stepping (by one ULP) from (block_switch_cost +
|
||||
// min_cost) and, if that takes only a few steps, skip the
|
||||
// subtraction for every c >= T. skip_t holds T, or 0xFFFF_FFFF_FFFF_FFFF
|
||||
// (which no non-negative, non-NaN c is at least) to skip nothing.
|
||||
skip_t = 0xFFFF_FFFF_FFFF_FFFF
|
||||
if (nh >= 8) and (min_cost < 0x7FF0_0000_0000_0000) and
|
||||
((block_switch_cost ~mod- 1) < 0x7FEF_FFFF_FFFF_FFFF) {
|
||||
// min_cost is non-negative and finite, and block_switch_cost is
|
||||
// positive and finite.
|
||||
t = this.bc.f64_add(a: block_switch_cost, b: min_cost)
|
||||
n = 0
|
||||
while (n < 3) and (t < 0x7FF0_0000_0000_0000) and
|
||||
(this.bc.f64_ge(a: this.bc.f64_sub(a: t, b: min_cost), b: block_switch_cost) == 0) {
|
||||
t ~mod+= 1
|
||||
n += 1
|
||||
}
|
||||
if (t < 0x7FF0_0000_0000_0000) and
|
||||
(this.bc.f64_ge(a: this.bc.f64_sub(a: t, b: min_cost), b: block_switch_cost) <> 0) {
|
||||
// S(t) >= block_switch_cost. Step down while S(t - 1) is too.
|
||||
n = 0
|
||||
while (n < 3) and (t > 0) and
|
||||
(this.bc.f64_ge(a: this.bc.f64_sub(a: t ~mod- 1, b: min_cost), b: block_switch_cost) <> 0) {
|
||||
t ~mod-= 1
|
||||
n += 1
|
||||
}
|
||||
if (t == 0) or
|
||||
(this.bc.f64_ge(a: this.bc.f64_sub(a: t ~mod- 1, b: min_cost), b: block_switch_cost) == 0) {
|
||||
skip_t = t
|
||||
}
|
||||
}
|
||||
}
|
||||
k = 0
|
||||
while k < args.num_histograms {
|
||||
c = this.sf.f64_sub(a: this.cost[k & 255], b: min_cost)
|
||||
while k < nh {
|
||||
c = this.cost[k & 255]
|
||||
if (c <= 0x7FF0_0000_0000_0000) and (c >= skip_t) {
|
||||
// S(c) >= S(T) >= block_switch_cost.
|
||||
c = block_switch_cost
|
||||
} else {
|
||||
c = this.bc.f64_sub(a: c, b: min_cost)
|
||||
}
|
||||
this.cost[k & 255] = c
|
||||
if this.sf.f64_ge(a: c, b: block_switch_cost) <> 0 {
|
||||
if this.bc.f64_ge(a: c, b: block_switch_cost) <> 0 {
|
||||
this.cost[k & 255] = block_switch_cost
|
||||
j = ix ~mod+ (k >> 3)
|
||||
if true {
|
||||
b = this.ld8(wb: args.wb, i: args.switch_signal ~mod+ j)
|
||||
this.st8!(wb: args.wb, i: args.switch_signal ~mod+ j, v: b | ((1 as base.u8) << (k & 7)))
|
||||
}
|
||||
b = this.ld8(wb: args.wb, i: args.switch_signal ~mod+ j)
|
||||
this.st8!(wb: args.wb, i: args.switch_signal ~mod+ j, v: b | ((1 as base.u8) << (k & 7)))
|
||||
}
|
||||
k ~mod+= 1
|
||||
}
|
||||
@@ -648,7 +728,7 @@ pri func metablock_builder.cluster_blocks!(wb: slice base.u8, cat: base.u32[..=
|
||||
s = this.ld32(wb: args.wb, i: clusters ~mod+ (j ~mod* 4))
|
||||
cur_bits = this.histogram_bit_cost_distance!(wb: args.wb, ds: args.ds, histogram: tmp,
|
||||
candidate: all_histograms ~mod+ ((s as base.u64) ~mod* hs), tmp: tmp ~mod+ hs)
|
||||
if this.sf.f64_lt(a: cur_bits, b: best_bits) <> 0 {
|
||||
if this.bc.f64_lt(a: cur_bits, b: best_bits) <> 0 {
|
||||
best_bits = cur_bits
|
||||
best_out = s
|
||||
}
|
||||
|
||||
@@ -24,10 +24,9 @@
|
||||
// EncodeData, see encode_data.wuffs, with Zopfli and BrotliBuildMetaBlock at
|
||||
// qualities 10 and 11) are implemented. See README.md for the API, including
|
||||
// how the C code's FLUSH and EMIT_METADATA operations map to the
|
||||
// request_flush and request_emit_metadata methods.
|
||||
//
|
||||
// TODO: large-window brotli at qualities 3 and above and custom (prepared or
|
||||
// shared) dictionaries.
|
||||
// request_flush and request_emit_metadata methods. Large-window brotli and raw
|
||||
// (prepared) dictionaries are also implemented. Serialized (shared)
|
||||
// dictionaries are rejected, like the default (non-experimental) C build.
|
||||
|
||||
use "std/softfloat"
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ pri func metablock_builder.histogram_pair_is_less(
|
||||
p1_cost_diff: base.u64, p1_idx1: base.u32, p1_idx2: base.u32,
|
||||
p2_cost_diff: base.u64, p2_idx1: base.u32, p2_idx2: base.u32) base.bool {
|
||||
if this.sf.f64_ne(a: args.p1_cost_diff, b: args.p2_cost_diff) <> 0 {
|
||||
return this.sf.f64_gt(a: args.p1_cost_diff, b: args.p2_cost_diff) <> 0
|
||||
return this.bc.f64_gt(a: args.p1_cost_diff, b: args.p2_cost_diff) <> 0
|
||||
}
|
||||
return (args.p1_idx2 ~mod- args.p1_idx1) > (args.p2_idx2 ~mod- args.p2_idx1)
|
||||
}
|
||||
@@ -36,11 +36,11 @@ pri func metablock_builder.cluster_cost_diff(size_a: base.u64, size_b: base.u64)
|
||||
var size_c : base.u64
|
||||
|
||||
size_c = args.size_a ~mod+ args.size_b
|
||||
return this.sf.f64_sub(
|
||||
a: this.sf.f64_add(
|
||||
a: this.sf.f64_mul(a: this.sf.f64_from_u64(a: args.size_a), b: this.bc.fast_log2(v: args.size_a)),
|
||||
b: this.sf.f64_mul(a: this.sf.f64_from_u64(a: args.size_b), b: this.bc.fast_log2(v: args.size_b))),
|
||||
b: this.sf.f64_mul(a: this.sf.f64_from_u64(a: size_c), b: this.bc.fast_log2(v: size_c)))
|
||||
return this.bc.f64_sub(
|
||||
a: this.bc.f64_add(
|
||||
a: this.bc.f64_mul(a: this.bc.f64_from_u64(a: args.size_a), b: this.bc.fast_log2(v: args.size_a)),
|
||||
b: this.bc.f64_mul(a: this.bc.f64_from_u64(a: args.size_b), b: this.bc.fast_log2(v: args.size_b))),
|
||||
b: this.bc.f64_mul(a: this.bc.f64_from_u64(a: size_c), b: this.bc.fast_log2(v: size_c)))
|
||||
}
|
||||
|
||||
// pair_copy is the struct assignment pairs[dst] = pairs[src], where dst and
|
||||
@@ -88,11 +88,11 @@ pri func metablock_builder.compare_and_push_to_queue!(wb: slice base.u8, ds: bas
|
||||
}
|
||||
h1 = args.out ~mod+ ((idx1 as base.u64) ~mod* hs)
|
||||
h2 = args.out ~mod+ ((idx2 as base.u64) ~mod* hs)
|
||||
cost_diff = this.sf.f64_mul(a: 0x3FE0_0000_0000_0000, b: this.cluster_cost_diff(
|
||||
cost_diff = this.bc.f64_mul(a: 0x3FE0_0000_0000_0000, b: this.cluster_cost_diff(
|
||||
size_a: this.ld32(wb: args.wb, i: args.cluster_size ~mod+ ((idx1 as base.u64) ~mod* 4)) as base.u64,
|
||||
size_b: this.ld32(wb: args.wb, i: args.cluster_size ~mod+ ((idx2 as base.u64) ~mod* 4)) as base.u64))
|
||||
cost_diff = this.sf.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h1, ds: args.ds))
|
||||
cost_diff = this.sf.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h2, ds: args.ds))
|
||||
cost_diff = this.bc.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h1, ds: args.ds))
|
||||
cost_diff = this.bc.f64_sub(a: cost_diff, b: this.h_bit_cost(wb: args.wb, h: h2, ds: args.ds))
|
||||
|
||||
if this.h_total(wb: args.wb, h: h1, ds: args.ds) == 0 {
|
||||
cost_combo = this.h_bit_cost(wb: args.wb, h: h2, ds: args.ds)
|
||||
@@ -109,13 +109,13 @@ pri func metablock_builder.compare_and_push_to_queue!(wb: slice base.u8, ds: bas
|
||||
this.h_copy!(wb: args.wb, h: args.tmp, v: h1, ds: args.ds)
|
||||
this.h_add_histogram!(wb: args.wb, h: args.tmp, v: h2, ds: args.ds)
|
||||
c = this.h_population_cost!(wb: args.wb, h: args.tmp, ds: args.ds)
|
||||
if this.sf.f64_lt(a: c, b: this.sf.f64_sub(a: threshold, b: cost_diff)) <> 0 {
|
||||
if this.bc.f64_lt(a: c, b: this.bc.f64_sub(a: threshold, b: cost_diff)) <> 0 {
|
||||
cost_combo = c
|
||||
is_good_pair = true
|
||||
}
|
||||
}
|
||||
if is_good_pair {
|
||||
cost_diff = this.sf.f64_add(a: cost_diff, b: cost_combo)
|
||||
cost_diff = this.bc.f64_add(a: cost_diff, b: cost_combo)
|
||||
if (this.num_pairs > 0) and this.histogram_pair_is_less(
|
||||
p1_cost_diff: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16),
|
||||
p1_idx1: this.ld32(wb: args.wb, i: args.pairs),
|
||||
@@ -182,7 +182,7 @@ pri func metablock_builder.histogram_combine!(wb: slice base.u8, ds: base.u32[..
|
||||
}
|
||||
|
||||
while num_clusters > min_cluster_size {
|
||||
if this.sf.f64_ge(a: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16), b: cost_diff_threshold) <> 0 {
|
||||
if this.bc.f64_ge(a: this.ld64(wb: args.wb, i: args.pairs ~mod+ 16), b: cost_diff_threshold) <> 0 {
|
||||
cost_diff_threshold = F64_1E99
|
||||
min_cluster_size = args.max_clusters
|
||||
continue
|
||||
@@ -279,7 +279,7 @@ pri func metablock_builder.histogram_bit_cost_distance!(wb: slice base.u8, ds: b
|
||||
this.h_copy!(wb: args.wb, h: args.tmp, v: args.histogram, ds: args.ds)
|
||||
this.h_add_histogram!(wb: args.wb, h: args.tmp, v: args.candidate, ds: args.ds)
|
||||
c = this.h_population_cost!(wb: args.wb, h: args.tmp, ds: args.ds)
|
||||
return this.sf.f64_sub(a: c, b: this.h_bit_cost(wb: args.wb, h: args.candidate, ds: args.ds))
|
||||
return this.bc.f64_sub(a: c, b: this.h_bit_cost(wb: args.wb, h: args.candidate, ds: args.ds))
|
||||
}
|
||||
|
||||
// Mirrors FN(BrotliHistogramRemap) in cluster_inc.h.
|
||||
@@ -310,7 +310,7 @@ pri func metablock_builder.histogram_remap!(wb: slice base.u8, ds: base.u32[..=
|
||||
cur_bits = this.histogram_bit_cost_distance!(wb: args.wb, ds: args.ds,
|
||||
histogram: args.in_histos ~mod+ (i ~mod* hs),
|
||||
candidate: args.out ~mod+ ((c as base.u64) ~mod* hs), tmp: args.tmp)
|
||||
if this.sf.f64_lt(a: cur_bits, b: best_bits) <> 0 {
|
||||
if this.bc.f64_lt(a: cur_bits, b: best_bits) <> 0 {
|
||||
best_bits = cur_bits
|
||||
best_out = c
|
||||
}
|
||||
|
||||
@@ -553,12 +553,12 @@ pri func encoder.estimate_entropy(off: base.u32, size: base.u32) base.u64 {
|
||||
while i < args.size {
|
||||
p = this.ctx_histo[(args.off ~mod+ i) & 511]
|
||||
total ~mod+= p as base.u64
|
||||
result = this.sf.f64_add(a: result, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u32(a: p), b: this.bc.fast_log2(v: p as base.u64)))
|
||||
result = this.bc.f64_add(a: result, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u32(a: p), b: this.bc.fast_log2(v: p as base.u64)))
|
||||
i ~mod+= 1
|
||||
}
|
||||
return this.sf.f64_sub(a: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: total), b: this.bc.fast_log2(v: total)), b: result)
|
||||
return this.bc.f64_sub(a: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: total), b: this.bc.fast_log2(v: total)), b: result)
|
||||
}
|
||||
|
||||
// Mirrors ChooseContextMap in encode.c. The bigram_histo is
|
||||
@@ -585,30 +585,30 @@ pri func encoder.choose_context_map!() base.u32 {
|
||||
i ~mod+= 1
|
||||
}
|
||||
e1 = this.estimate_entropy(off: 16, size: 3)
|
||||
e2 = this.sf.f64_add(a: this.estimate_entropy(off: 24, size: 3), b: this.estimate_entropy(off: 27, size: 3))
|
||||
e2 = this.bc.f64_add(a: this.estimate_entropy(off: 24, size: 3), b: this.estimate_entropy(off: 27, size: 3))
|
||||
e3 = 0
|
||||
i = 0
|
||||
while i < 3 {
|
||||
e3 = this.sf.f64_add(a: e3, b: this.estimate_entropy(off: 3 * i, size: 3))
|
||||
e3 = this.bc.f64_add(a: e3, b: this.estimate_entropy(off: 3 * i, size: 3))
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
total = (this.ctx_histo[16] ~mod+ this.ctx_histo[17]) ~mod+ this.ctx_histo[18]
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.sf.f64_from_u32(a: total))
|
||||
e1 = this.sf.f64_mul(a: e1, b: e0)
|
||||
e2 = this.sf.f64_mul(a: e2, b: e0)
|
||||
e3 = this.sf.f64_mul(a: e3, b: e0)
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.bc.f64_from_u32(a: total))
|
||||
e1 = this.bc.f64_mul(a: e1, b: e0)
|
||||
e2 = this.bc.f64_mul(a: e2, b: e0)
|
||||
e3 = this.bc.f64_mul(a: e3, b: e0)
|
||||
|
||||
if this.quality < 7 {
|
||||
// 3 context models is a bit slower, don't use it at lower qualities.
|
||||
e3 = this.sf.f64_mul(a: e1, b: F64_10)
|
||||
e3 = this.bc.f64_mul(a: e1, b: F64_10)
|
||||
}
|
||||
// If expected savings by symbol are less than 0.2 bits, skip the context
|
||||
// modeling -- in exchange for faster decoding speed.
|
||||
if (this.sf.f64_lt(a: this.sf.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) and
|
||||
(this.sf.f64_lt(a: this.sf.f64_sub(a: e1, b: e3), b: F64_0_2) <> 0) {
|
||||
if (this.bc.f64_lt(a: this.bc.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) and
|
||||
(this.bc.f64_lt(a: this.bc.f64_sub(a: e1, b: e3), b: F64_0_2) <> 0) {
|
||||
return 1
|
||||
} else if this.sf.f64_lt(a: this.sf.f64_sub(a: e2, b: e3), b: F64_0_02) <> 0 {
|
||||
} else if this.bc.f64_lt(a: this.bc.f64_sub(a: e2, b: e3), b: F64_0_02) <> 0 {
|
||||
return 2
|
||||
}
|
||||
return 3
|
||||
@@ -668,16 +668,16 @@ pri func encoder.should_use_complex_static_context_map!(wb: roslice base.u8, sta
|
||||
e2 = 0
|
||||
i = 0
|
||||
while i < 13 {
|
||||
e2 = this.sf.f64_add(a: e2, b: this.estimate_entropy(off: 32 + (i << 5), size: 32))
|
||||
e2 = this.bc.f64_add(a: e2, b: this.estimate_entropy(off: 32 + (i << 5), size: 32))
|
||||
i ~mod+= 1
|
||||
}
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.sf.f64_from_u32(a: total))
|
||||
e1 = this.sf.f64_mul(a: e1, b: e0)
|
||||
e2 = this.sf.f64_mul(a: e2, b: e0)
|
||||
e0 = this.sf.f64_div(a: F64_1, b: this.bc.f64_from_u32(a: total))
|
||||
e1 = this.bc.f64_mul(a: e1, b: e0)
|
||||
e2 = this.bc.f64_mul(a: e2, b: e0)
|
||||
// The triggering heuristics below were tuned by compressing the
|
||||
// individual files of the silesia corpus.
|
||||
if (this.sf.f64_gt(a: e2, b: F64_3) <> 0) or
|
||||
(this.sf.f64_lt(a: this.sf.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) {
|
||||
if (this.bc.f64_gt(a: e2, b: F64_3) <> 0) or
|
||||
(this.bc.f64_lt(a: this.bc.f64_sub(a: e1, b: e2), b: F64_0_2) <> 0) {
|
||||
return 0
|
||||
}
|
||||
return 13
|
||||
@@ -748,16 +748,16 @@ pri func encoder.should_compress!(wb: roslice base.u8, last_flush_pos: base.u64,
|
||||
return false
|
||||
}
|
||||
if args.num_commands < ((args.bytes >> 8) + 2) {
|
||||
if this.sf.f64_gt(
|
||||
a: this.sf.f64_from_u64(a: args.num_literals),
|
||||
b: this.sf.f64_mul(a: F64_0_99, b: this.sf.f64_from_u64(a: args.bytes))) <> 0 {
|
||||
if this.bc.f64_gt(
|
||||
a: this.bc.f64_from_u64(a: args.num_literals),
|
||||
b: this.bc.f64_mul(a: F64_0_99, b: this.bc.f64_from_u64(a: args.bytes))) <> 0 {
|
||||
i = 0
|
||||
while i < 256 {
|
||||
this.ctx_histo[i] = 0
|
||||
i ~mod+= 1
|
||||
}
|
||||
bit_cost_threshold = this.sf.f64_mul(
|
||||
a: this.sf.f64_mul(a: this.sf.f64_from_u64(a: args.bytes), b: F64_7_92),
|
||||
bit_cost_threshold = this.bc.f64_mul(
|
||||
a: this.bc.f64_mul(a: this.bc.f64_from_u64(a: args.bytes), b: F64_7_92),
|
||||
b: F64_INV_13)
|
||||
t = (args.bytes ~mod+ 12) / 13
|
||||
pos = (args.last_flush_pos & 0xFFFF_FFFF) as base.u32
|
||||
@@ -767,7 +767,7 @@ pri func encoder.should_compress!(wb: roslice base.u8, last_flush_pos: base.u64,
|
||||
pos ~mod+= 13
|
||||
i ~mod+= 1
|
||||
}
|
||||
if this.sf.f64_gt(a: this.ctx_histo_bits_entropy(), b: bit_cost_threshold) <> 0 {
|
||||
if this.bc.f64_gt(a: this.ctx_histo_bits_entropy(), b: bit_cost_threshold) <> 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -787,16 +787,16 @@ pri func encoder.ctx_histo_bits_entropy() base.u64 {
|
||||
while i < 256 {
|
||||
p = this.ctx_histo[i] as base.u64
|
||||
sum ~mod+= p
|
||||
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: p), b: this.bc.fast_log2(v: p)))
|
||||
retval = this.bc.f64_sub(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: p), b: this.bc.fast_log2(v: p)))
|
||||
i ~mod+= 1
|
||||
}
|
||||
if sum <> 0 {
|
||||
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: sum), b: this.bc.fast_log2(v: sum)))
|
||||
retval = this.bc.f64_add(a: retval, b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: sum), b: this.bc.fast_log2(v: sum)))
|
||||
}
|
||||
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.sf.f64_from_u64(a: sum)
|
||||
if this.bc.f64_lt(a: retval, b: this.bc.f64_from_u64(a: sum)) <> 0 {
|
||||
retval = this.bc.f64_from_u64(a: sum)
|
||||
}
|
||||
return retval
|
||||
}
|
||||
|
||||
@@ -0,0 +1,742 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, 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 holds package-private fast paths for the std/softfloat
|
||||
// operations that the encoder's hot loops use (the Zopfli cost model, the
|
||||
// block splitter and histogram clustering).
|
||||
//
|
||||
// Each softfloat.arith method is another package's public function: every
|
||||
// call checks its receiver and, being an exported function, is not inlined
|
||||
// by the C compiler. These bit_cost methods instead handle the common cases
|
||||
// (finite, normal and, for addition and subtraction, mostly non-negative
|
||||
// operands with normal results) directly, so that they can be inlined, and
|
||||
// otherwise call the softfloat.arith method of the same name. Either way,
|
||||
// they return exactly the same bits as that method (the IEEE 754 result,
|
||||
// rounding to nearest, ties to even), so they are only a speed optimization.
|
||||
//
|
||||
// Operands are IEEE 754 binary32 (base.u32) or binary64 (base.u64) bit
|
||||
// patterns. The fast paths' conditions on positive normal values use that
|
||||
// such bit patterns are in [0x0080_0000, 0x7F80_0000) (or, for f64, in
|
||||
// [0x0010_0000_0000_0000, 0x7FF0_0000_0000_0000)) and that, there, the bit
|
||||
// patterns sort like the values.
|
||||
|
||||
// -------- Helpers.
|
||||
|
||||
// clz64 returns the number of leading zero bits of a, which must be non-zero
|
||||
// (if a is zero, it returns 63).
|
||||
pri func bit_cost.clz64(a: base.u64) base.u32[..= 63] {
|
||||
var x : base.u64
|
||||
var n : base.u32[..= 63]
|
||||
|
||||
x = args.a
|
||||
if (x >> 32) == 0 {
|
||||
x = x ~mod<< 32
|
||||
n = 32
|
||||
}
|
||||
if (x >> 48) == 0 {
|
||||
x = x ~mod<< 16
|
||||
n = (n + 16) & 63
|
||||
}
|
||||
if (x >> 56) == 0 {
|
||||
x = x ~mod<< 8
|
||||
n = (n + 8) & 63
|
||||
}
|
||||
if (x >> 60) == 0 {
|
||||
x = x ~mod<< 4
|
||||
n = (n + 4) & 63
|
||||
}
|
||||
if (x >> 62) == 0 {
|
||||
x = x ~mod<< 2
|
||||
n = (n + 2) & 63
|
||||
}
|
||||
if (x >> 63) == 0 {
|
||||
n = (n + 1) & 63
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// -------- f32.
|
||||
|
||||
// f32_add returns (a + b), like softfloat.arith.f32_add. Its fast path is
|
||||
// for the most common case (both are positive and normal) and the others
|
||||
// take f32_add_any.
|
||||
pri func bit_cost.f32_add(a: base.u32, b: base.u32) base.u32 {
|
||||
var x : base.u32
|
||||
var y : base.u32
|
||||
var ex : base.u32[..= 0xFF]
|
||||
var d : base.u32
|
||||
var s : base.u64
|
||||
var z : base.u32
|
||||
|
||||
if ((args.a ~mod- 0x80_0000) < 0x7F00_0000) and ((args.b ~mod- 0x80_0000) < 0x7F00_0000) {
|
||||
// Both are positive and normal. Order them so that x >= y.
|
||||
x = args.a
|
||||
y = args.b
|
||||
if y > x {
|
||||
x = args.b
|
||||
y = args.a
|
||||
}
|
||||
ex = (x >> 23) & 0xFF
|
||||
d = ex ~mod- ((y >> 23) & 0xFF)
|
||||
if d >= 25 {
|
||||
// y is less than half an ULP of x, so the sum rounds to x.
|
||||
return x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62. Shifting y's
|
||||
// right by at most 24 is exact, and the sum is less than (1 << 64).
|
||||
s = ((((x & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) ~mod+
|
||||
(((((y & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) >> (d & 31))
|
||||
// Round to nearest, ties to even: add just under half an ULP, plus
|
||||
// one if the truncated result is odd. The rounded significand is in
|
||||
// [1 << 23 ..= 1 << 24] and adding it (whose bit 23 is the implicit
|
||||
// bit) to the shifted exponent carries any rounding overflow into the
|
||||
// exponent field.
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
s = (s ~mod+ (0x7F_FFFF_FFFF ~mod+ ((s >> 40) & 1))) >> 40
|
||||
z = (ex ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
if z >= 0x7F80_0000 {
|
||||
return 0x7F80_0000
|
||||
}
|
||||
return z
|
||||
}
|
||||
s = (s ~mod+ (0x3F_FFFF_FFFF ~mod+ ((s >> 39) & 1))) >> 39
|
||||
return ((ex ~mod- 1) ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
} else if (args.a == 0) and ((args.b & 0x7FFF_FFFF) <= 0x7F80_0000) and (args.b <> 0x8000_0000) {
|
||||
// (+0 + b) is b, for any b other than a NaN or -0.
|
||||
return args.b
|
||||
} else if (args.b == 0) and ((args.a & 0x7FFF_FFFF) <= 0x7F80_0000) and (args.a <> 0x8000_0000) {
|
||||
return args.a
|
||||
}
|
||||
return this.f32_add_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_sub returns (a - b), like softfloat.arith.f32_sub. Its fast path is
|
||||
// for the most common case (both are positive and normal, a > b and there is
|
||||
// little cancellation) and the others take f32_sub_any.
|
||||
pri func bit_cost.f32_sub(a: base.u32, b: base.u32) base.u32 {
|
||||
var ex : base.u32[..= 0xFF]
|
||||
var d : base.u32
|
||||
var s : base.u64
|
||||
|
||||
if ((args.b ~mod- 0x80_0000) < 0x7F00_0000) and (args.a > args.b) and (args.a < 0x7F80_0000) {
|
||||
// Both are positive and normal, and a > b.
|
||||
ex = (args.a >> 23) & 0xFF
|
||||
d = ex ~mod- ((args.b >> 23) & 0xFF)
|
||||
if d >= 26 {
|
||||
// b is less than half of the spacing of the f32 values just
|
||||
// below a, so the difference rounds to a.
|
||||
return args.a
|
||||
} else if d >= 2 {
|
||||
// The significands, with their implicit bit at bit 62. Shifting
|
||||
// b's right by at most 25 is exact. As (d >= 2), the difference
|
||||
// is at least (1 << 61), so it needs at most a 1 bit shift to
|
||||
// normalize (which is exact too).
|
||||
s = ((((args.a & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) ~mod-
|
||||
(((((args.b & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) >> (d & 31))
|
||||
if s < 0x4000_0000_0000_0000 {
|
||||
s ~mod<<= 1
|
||||
ex = (ex ~mod- 1) & 0xFF
|
||||
}
|
||||
// Round to nearest, ties to even (see f32_add). The result is
|
||||
// normal, as ex is at least 2 after the shift (a's biased
|
||||
// exponent is at least (d + 1)).
|
||||
s = (s ~mod+ (0x3F_FFFF_FFFF ~mod+ ((s >> 39) & 1))) >> 39
|
||||
return ((ex ~mod- 1) ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
}
|
||||
} else if (args.b == 0) and ((args.a & 0x7FFF_FFFF) <= 0x7F80_0000) {
|
||||
// (a - +0) is a, for any non-NaN a.
|
||||
return args.a
|
||||
} else if (args.a == args.b) and (args.a < 0x7F80_0000) {
|
||||
// (a - a) is +0, for finite non-negative a.
|
||||
return 0
|
||||
}
|
||||
return this.f32_sub_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_add_any returns (a + b), like softfloat.arith.f32_add. It is the
|
||||
// general (but slower) fast path, for operands of any sign.
|
||||
pri func bit_cost.f32_add_any(a: base.u32, b: base.u32) base.u32 {
|
||||
var x : base.u32
|
||||
var y : base.u32
|
||||
var ex : base.u32[..= 0xFF]
|
||||
var d : base.u32
|
||||
var mx : base.u64
|
||||
var my : base.u64
|
||||
var s : base.u64
|
||||
var lz : base.u32[..= 63]
|
||||
var z : base.u32
|
||||
|
||||
if ((((args.a >> 23) & 0xFF) ~mod- 1) < 0xFE) and ((((args.b >> 23) & 0xFF) ~mod- 1) < 0xFE) {
|
||||
// Both are normal. Order them so that |x| >= |y|. The result has x's
|
||||
// sign (or is +0).
|
||||
x = args.a
|
||||
y = args.b
|
||||
if (y & 0x7FFF_FFFF) > (x & 0x7FFF_FFFF) {
|
||||
x = args.b
|
||||
y = args.a
|
||||
}
|
||||
ex = (x >> 23) & 0xFF
|
||||
d = ex ~mod- ((y >> 23) & 0xFF)
|
||||
if d >= 26 {
|
||||
// |y| is less than half an ULP of |x| (and less than half of the
|
||||
// spacing of the f32 values just below |x|), so the result
|
||||
// rounds to x.
|
||||
return x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62. Shifting y's
|
||||
// right by at most 25 is exact, so the sum or difference is exact
|
||||
// (and less than (1 << 64)).
|
||||
mx = (((x & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39
|
||||
my = ((((y & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) >> (d & 31)
|
||||
if ((x ^ y) >> 31) == 0 {
|
||||
s = mx ~mod+ my
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
// Round to nearest, ties to even: add just under half an ULP,
|
||||
// plus one if the truncated result is odd. The rounded
|
||||
// significand is in [1 << 23 ..= 1 << 24] and adding it
|
||||
// (whose bit 23 is the implicit bit) to the shifted exponent
|
||||
// carries any rounding overflow into the exponent field.
|
||||
s = (s ~mod+ (0x7F_FFFF_FFFF ~mod+ ((s >> 40) & 1))) >> 40
|
||||
z = (ex ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
if z >= 0x7F80_0000 {
|
||||
z = 0x7F80_0000
|
||||
}
|
||||
return (x & 0x8000_0000) | z
|
||||
}
|
||||
} else {
|
||||
s = mx ~mod- my
|
||||
if s == 0 {
|
||||
// Exact cancellation gives +0 when rounding to nearest.
|
||||
return 0
|
||||
} else if s < 0x4000_0000_0000_0000 {
|
||||
lz = (this.clz64(a: s) ~mod- 1) & 63
|
||||
if lz >= ex {
|
||||
// The result is subnormal.
|
||||
return this.sf.f32_add(a: args.a, b: args.b)
|
||||
}
|
||||
ex = (ex ~mod- lz) & 0xFF
|
||||
s = s ~mod<< lz
|
||||
}
|
||||
}
|
||||
// s is in [1 << 62, 1 << 63) and ex in [1 ..= 254].
|
||||
s = (s ~mod+ (0x3F_FFFF_FFFF ~mod+ ((s >> 39) & 1))) >> 39
|
||||
return (x & 0x8000_0000) | (((ex ~mod- 1) ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32))
|
||||
}
|
||||
return this.sf.f32_add(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_sub_any returns (a - b), like softfloat.arith.f32_sub.
|
||||
pri func bit_cost.f32_sub_any(a: base.u32, b: base.u32) base.u32 {
|
||||
if (((args.b >> 23) & 0xFF) ~mod- 1) < 0xFE {
|
||||
// b is normal (not a NaN), so (a - b) is exactly (a + -b), including
|
||||
// the sign of a zero result.
|
||||
return this.f32_add_any(a: args.a, b: args.b ^ 0x8000_0000)
|
||||
}
|
||||
return this.sf.f32_sub(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_key maps a non-NaN f32 to a u32 that sorts in the same order as the
|
||||
// f32 values (except that -0 sorts just below +0).
|
||||
pri func bit_cost.f32_key(a: base.u32) base.u32 {
|
||||
if (args.a >> 31) <> 0 {
|
||||
return 0xFFFF_FFFF ^ args.a
|
||||
}
|
||||
return args.a | 0x8000_0000
|
||||
}
|
||||
|
||||
// f32_lt returns (a < b), like softfloat.arith.f32_lt.
|
||||
pri func bit_cost.f32_lt(a: base.u32, b: base.u32) base.u32 {
|
||||
if (args.a | args.b) <= 0x7F80_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a < args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f32_lt_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_lt_any returns (a < b), like softfloat.arith.f32_lt.
|
||||
pri func bit_cost.f32_lt_any(a: base.u32, b: base.u32) base.u32 {
|
||||
if ((args.a & 0x7FFF_FFFF) <= 0x7F80_0000) and ((args.b & 0x7FFF_FFFF) <= 0x7F80_0000) {
|
||||
// Neither is a NaN.
|
||||
if (this.f32_key(a: args.a) < this.f32_key(a: args.b)) and
|
||||
(((args.a | args.b) & 0x7FFF_FFFF) <> 0) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.sf.f32_lt(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_le returns (a <= b), like softfloat.arith.f32_le.
|
||||
pri func bit_cost.f32_le(a: base.u32, b: base.u32) base.u32 {
|
||||
if (args.a | args.b) <= 0x7F80_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a <= args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f32_le_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_le_any returns (a <= b), like softfloat.arith.f32_le.
|
||||
pri func bit_cost.f32_le_any(a: base.u32, b: base.u32) base.u32 {
|
||||
if ((args.a & 0x7FFF_FFFF) <= 0x7F80_0000) and ((args.b & 0x7FFF_FFFF) <= 0x7F80_0000) {
|
||||
if (this.f32_key(a: args.a) <= this.f32_key(a: args.b)) or
|
||||
(((args.a | args.b) & 0x7FFF_FFFF) == 0) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.sf.f32_le(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_gt returns (a > b), like softfloat.arith.f32_gt.
|
||||
pri func bit_cost.f32_gt(a: base.u32, b: base.u32) base.u32 {
|
||||
if (args.a | args.b) <= 0x7F80_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a > args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f32_gt_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f32_gt_any returns (a > b), like softfloat.arith.f32_gt.
|
||||
pri func bit_cost.f32_gt_any(a: base.u32, b: base.u32) base.u32 {
|
||||
return this.f32_lt_any(a: args.b, b: args.a)
|
||||
}
|
||||
|
||||
// -------- f64.
|
||||
|
||||
// f64_add returns (a + b), like softfloat.arith.f64_add. Its fast path is
|
||||
// for the most common case (both are positive and normal) and the others
|
||||
// take f64_add_any.
|
||||
pri func bit_cost.f64_add(a: base.u64, b: base.u64) base.u64 {
|
||||
var x : base.u64
|
||||
var y : base.u64
|
||||
var ex : base.u64[..= 0x7FF]
|
||||
var d : base.u64
|
||||
var my : base.u64
|
||||
var s : base.u64
|
||||
|
||||
if ((args.a ~mod- 0x0010_0000_0000_0000) < 0x7FE0_0000_0000_0000) and
|
||||
((args.b ~mod- 0x0010_0000_0000_0000) < 0x7FE0_0000_0000_0000) {
|
||||
// Both are positive and normal. Order them so that x >= y.
|
||||
x = args.a
|
||||
y = args.b
|
||||
if y > x {
|
||||
x = args.b
|
||||
y = args.a
|
||||
}
|
||||
ex = (x >> 52) & 0x7FF
|
||||
d = ex ~mod- ((y >> 52) & 0x7FF)
|
||||
if d >= 54 {
|
||||
// y is less than half an ULP of x, so the sum rounds to x.
|
||||
return x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62 and 10 extra
|
||||
// bits of precision at the bottom. Align y's with x's, keeping a
|
||||
// sticky bit.
|
||||
my = ((y & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
if (my & (((1 as base.u64) << (d & 63)) ~mod- 1)) <> 0 {
|
||||
my = (my >> (d & 63)) | 1
|
||||
} else {
|
||||
my = my >> (d & 63)
|
||||
}
|
||||
s = (((x & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10) ~mod+ my
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
ex = (ex ~mod+ 1) & 0x7FF
|
||||
s = (s >> 1) | (s & 1)
|
||||
}
|
||||
// Round to nearest, ties to even (see f32_add). The rounded
|
||||
// significand is in [1 << 52 ..= 1 << 53].
|
||||
s = (s ~mod+ (0x1FF ~mod+ ((s >> 10) & 1))) >> 10
|
||||
s = ((ex ~mod- 1) ~mod<< 52) ~mod+ s
|
||||
if s >= 0x7FF0_0000_0000_0000 {
|
||||
return 0x7FF0_0000_0000_0000
|
||||
}
|
||||
return s
|
||||
} else if (args.a == 0) and ((args.b & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) and
|
||||
(args.b <> 0x8000_0000_0000_0000) {
|
||||
// (+0 + b) is b, for any b other than a NaN or -0.
|
||||
return args.b
|
||||
} else if (args.b == 0) and ((args.a & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) and
|
||||
(args.a <> 0x8000_0000_0000_0000) {
|
||||
return args.a
|
||||
}
|
||||
return this.f64_add_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_sub returns (a - b), like softfloat.arith.f64_sub. Its fast path is
|
||||
// for the most common case (both are positive and normal, a > b and there is
|
||||
// little cancellation) and the others take f64_sub_any.
|
||||
pri func bit_cost.f64_sub(a: base.u64, b: base.u64) base.u64 {
|
||||
var ex : base.u64[..= 0x7FF]
|
||||
var d : base.u64
|
||||
var my : base.u64
|
||||
var s : base.u64
|
||||
var lz : base.u64
|
||||
|
||||
if ((args.b ~mod- 0x0010_0000_0000_0000) < 0x7FE0_0000_0000_0000) and
|
||||
(args.a > args.b) and (args.a < 0x7FF0_0000_0000_0000) {
|
||||
// Both are positive and normal, and a > b.
|
||||
ex = (args.a >> 52) & 0x7FF
|
||||
d = ex ~mod- ((args.b >> 52) & 0x7FF)
|
||||
if d >= 55 {
|
||||
// b is less than half of the spacing of the f64 values just
|
||||
// below a, so the difference rounds to a.
|
||||
return args.a
|
||||
} else if d >= 2 {
|
||||
// The significands, with their implicit bit at bit 62 and 10
|
||||
// extra bits of precision at the bottom. Align b's with a's,
|
||||
// keeping a sticky bit. As (d >= 2), the difference is at least
|
||||
// (1 << 61), so it needs at most a 1 bit shift to normalize.
|
||||
my = ((args.b & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
if (my & (((1 as base.u64) << (d & 63)) ~mod- 1)) <> 0 {
|
||||
my = (my >> (d & 63)) | 1
|
||||
} else {
|
||||
my = my >> (d & 63)
|
||||
}
|
||||
s = (((args.a & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10) ~mod- my
|
||||
if s < 0x4000_0000_0000_0000 {
|
||||
s ~mod<<= 1
|
||||
ex = (ex ~mod- 1) & 0x7FF
|
||||
}
|
||||
// Round to nearest, ties to even (see f32_add). The result is
|
||||
// normal, as ex is at least 2 after the shift.
|
||||
s = (s ~mod+ (0x1FF ~mod+ ((s >> 10) & 1))) >> 10
|
||||
return ((ex ~mod- 1) ~mod<< 52) ~mod+ s
|
||||
}
|
||||
// d is 0 or 1, so shifting b's significand right by d (with 10
|
||||
// extra bits) is exact, and so is the difference (which is non-zero,
|
||||
// as a > b) and normalizing it.
|
||||
s = (((args.a & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10) ~mod-
|
||||
((((args.b & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10) >> (d & 1))
|
||||
lz = (this.clz64(a: s) as base.u64) ~mod- 1
|
||||
if lz < ex {
|
||||
ex = (ex ~mod- lz) & 0x7FF
|
||||
s = s ~mod<< (lz & 63)
|
||||
s = (s ~mod+ (0x1FF ~mod+ ((s >> 10) & 1))) >> 10
|
||||
return ((ex ~mod- 1) ~mod<< 52) ~mod+ s
|
||||
}
|
||||
} else if (args.b == 0) and ((args.a & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) {
|
||||
// (a - +0) is a, for any non-NaN a.
|
||||
return args.a
|
||||
} else if (args.a == args.b) and (args.a < 0x7FF0_0000_0000_0000) {
|
||||
// (a - a) is +0, for finite non-negative a.
|
||||
return 0
|
||||
}
|
||||
return this.f64_sub_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_add_any returns (a + b), like softfloat.arith.f64_add. It is the
|
||||
// general (but slower) fast path, for operands of any sign.
|
||||
pri func bit_cost.f64_add_any(a: base.u64, b: base.u64) base.u64 {
|
||||
var x : base.u64
|
||||
var y : base.u64
|
||||
var ex : base.u64[..= 0x7FF]
|
||||
var d : base.u64
|
||||
var mx : base.u64
|
||||
var my : base.u64
|
||||
var s : base.u64
|
||||
var lz : base.u64[..= 63]
|
||||
|
||||
if ((((args.a >> 52) & 0x7FF) ~mod- 1) < 0x7FE) and ((((args.b >> 52) & 0x7FF) ~mod- 1) < 0x7FE) {
|
||||
// Both are normal. Order them so that |x| >= |y|. The result has x's
|
||||
// sign (or is +0).
|
||||
x = args.a
|
||||
y = args.b
|
||||
if (y & 0x7FFF_FFFF_FFFF_FFFF) > (x & 0x7FFF_FFFF_FFFF_FFFF) {
|
||||
x = args.b
|
||||
y = args.a
|
||||
}
|
||||
ex = (x >> 52) & 0x7FF
|
||||
d = ex ~mod- ((y >> 52) & 0x7FF)
|
||||
if d >= 55 {
|
||||
// |y| is less than half an ULP of |x| (and less than half of the
|
||||
// spacing of the f64 values just below |x|), so the result
|
||||
// rounds to x.
|
||||
return x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62 and 10 extra
|
||||
// bits of precision at the bottom. Align my with mx, keeping a
|
||||
// sticky bit (which only happens if (d > 10)).
|
||||
mx = ((x & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
my = ((y & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
if (my & (((1 as base.u64) << (d & 63)) ~mod- 1)) <> 0 {
|
||||
my = (my >> (d & 63)) | 1
|
||||
} else {
|
||||
my = my >> (d & 63)
|
||||
}
|
||||
if ((x ^ y) >> 63) == 0 {
|
||||
s = mx ~mod+ my
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
ex = (ex ~mod+ 1) & 0x7FF
|
||||
s = (s >> 1) | (s & 1)
|
||||
}
|
||||
} else {
|
||||
s = mx ~mod- my
|
||||
if s == 0 {
|
||||
// Exact cancellation gives +0 when rounding to nearest.
|
||||
return 0
|
||||
} else if s < 0x4000_0000_0000_0000 {
|
||||
// If (d >= 2) then s >= (1 << 61) and the normalization shift
|
||||
// is 1. Otherwise, no bits were shifted out of my.
|
||||
lz = ((this.clz64(a: s) as base.u64) ~mod- 1) & 63
|
||||
if lz >= ex {
|
||||
// The result is subnormal.
|
||||
return this.sf.f64_add(a: args.a, b: args.b)
|
||||
}
|
||||
ex = (ex ~mod- lz) & 0x7FF
|
||||
s = s ~mod<< lz
|
||||
}
|
||||
}
|
||||
// Round to nearest, ties to even (see f32_add). s is in
|
||||
// [1 << 62, 1 << 63) and ex in [1 ..= 0x7FF]. The rounded
|
||||
// significand is in [1 << 52 ..= 1 << 53].
|
||||
s = (s ~mod+ (0x1FF ~mod+ ((s >> 10) & 1))) >> 10
|
||||
s = ((ex ~mod- 1) ~mod<< 52) ~mod+ s
|
||||
if s >= 0x7FF0_0000_0000_0000 {
|
||||
s = 0x7FF0_0000_0000_0000
|
||||
}
|
||||
return (x & 0x8000_0000_0000_0000) | s
|
||||
}
|
||||
return this.sf.f64_add(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_sub_any returns (a - b), like softfloat.arith.f64_sub.
|
||||
pri func bit_cost.f64_sub_any(a: base.u64, b: base.u64) base.u64 {
|
||||
if (((args.b >> 52) & 0x7FF) ~mod- 1) < 0x7FE {
|
||||
// b is normal (not a NaN), so (a - b) is exactly (a + -b), including
|
||||
// the sign of a zero result.
|
||||
return this.f64_add_any(a: args.a, b: args.b ^ 0x8000_0000_0000_0000)
|
||||
}
|
||||
return this.sf.f64_sub(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_key maps a non-NaN f64 to a u64 that sorts in the same order as the
|
||||
// f64 values (except that -0 sorts just below +0).
|
||||
pri func bit_cost.f64_key(a: base.u64) base.u64 {
|
||||
if (args.a >> 63) <> 0 {
|
||||
return 0xFFFF_FFFF_FFFF_FFFF ^ args.a
|
||||
}
|
||||
return args.a | 0x8000_0000_0000_0000
|
||||
}
|
||||
|
||||
// f64_lt returns (a < b), like softfloat.arith.f64_lt.
|
||||
pri func bit_cost.f64_lt(a: base.u64, b: base.u64) base.u32 {
|
||||
if (args.a | args.b) <= 0x7FF0_0000_0000_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a < args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f64_lt_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_lt_any returns (a < b), like softfloat.arith.f64_lt.
|
||||
pri func bit_cost.f64_lt_any(a: base.u64, b: base.u64) base.u32 {
|
||||
if ((args.a & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) and
|
||||
((args.b & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) {
|
||||
// Neither is a NaN.
|
||||
if (this.f64_key(a: args.a) < this.f64_key(a: args.b)) and
|
||||
(((args.a | args.b) & 0x7FFF_FFFF_FFFF_FFFF) <> 0) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.sf.f64_lt(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_gt returns (a > b), like softfloat.arith.f64_gt.
|
||||
pri func bit_cost.f64_gt(a: base.u64, b: base.u64) base.u32 {
|
||||
if (args.a | args.b) <= 0x7FF0_0000_0000_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a > args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f64_gt_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_gt_any returns (a > b), like softfloat.arith.f64_gt.
|
||||
pri func bit_cost.f64_gt_any(a: base.u64, b: base.u64) base.u32 {
|
||||
return this.f64_lt_any(a: args.b, b: args.a)
|
||||
}
|
||||
|
||||
// f64_ge returns (a >= b), like softfloat.arith.f64_ge.
|
||||
pri func bit_cost.f64_ge(a: base.u64, b: base.u64) base.u32 {
|
||||
if (args.a | args.b) <= 0x7FF0_0000_0000_0000 {
|
||||
// Neither is negative or a NaN, so the bit patterns sort like the
|
||||
// values.
|
||||
if args.a >= args.b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.f64_ge_any(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_ge_any returns (a >= b), like softfloat.arith.f64_ge.
|
||||
pri func bit_cost.f64_ge_any(a: base.u64, b: base.u64) base.u32 {
|
||||
if ((args.a & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) and
|
||||
((args.b & 0x7FFF_FFFF_FFFF_FFFF) <= 0x7FF0_0000_0000_0000) {
|
||||
if (this.f64_key(a: args.a) >= this.f64_key(a: args.b)) or
|
||||
(((args.a | args.b) & 0x7FFF_FFFF_FFFF_FFFF) == 0) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return this.sf.f64_ge(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_mul returns (a * b), like softfloat.arith.f64_mul. Its fast path is
|
||||
// for normal operands with a normal (and finite) result.
|
||||
pri func bit_cost.f64_mul(a: base.u64, b: base.u64) base.u64 {
|
||||
var ea : base.u64
|
||||
var eb : base.u64
|
||||
var e : base.u64
|
||||
var ma : base.u64
|
||||
var mb : base.u64
|
||||
var a0 : base.u64[..= 0xFFFF_FFFF]
|
||||
var a1 : base.u64[..= 0xFFFF_FFFF]
|
||||
var b0 : base.u64[..= 0xFFFF_FFFF]
|
||||
var b1 : base.u64[..= 0xFFFF_FFFF]
|
||||
var p00 : base.u64
|
||||
var p01 : base.u64
|
||||
var p10 : base.u64
|
||||
var mid : base.u64
|
||||
var s : base.u64
|
||||
var rb : base.u64
|
||||
|
||||
ea = (args.a >> 52) & 0x7FF
|
||||
eb = (args.b >> 52) & 0x7FF
|
||||
if ((ea ~mod- 1) < 0x7FE) and ((eb ~mod- 1) < 0x7FE) {
|
||||
// Both are normal. e is one less than the result's biased exponent
|
||||
// (before normalization), as a two's complement integer. The
|
||||
// significands have their implicit bit at bit 62 and bit 63, so that
|
||||
// the high 64 bits of their 128-bit product have the implicit bit at
|
||||
// bit 61 or 62.
|
||||
e = (ea ~mod+ eb) ~mod- 0x3FF
|
||||
ma = ((args.a & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 10
|
||||
mb = ((args.b & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) << 11
|
||||
a0 = ma & 0xFFFF_FFFF
|
||||
a1 = ma >> 32
|
||||
b0 = mb & 0xFFFF_FFFF
|
||||
b1 = mb >> 32
|
||||
p00 = a0 * b0
|
||||
p01 = a0 * b1
|
||||
p10 = a1 * b0
|
||||
mid = (p00 >> 32) + (p01 & 0xFFFF_FFFF) + (p10 & 0xFFFF_FFFF)
|
||||
s = (((a1 * b1) ~mod+ (p01 >> 32)) ~mod+ (p10 >> 32)) ~mod+ (mid >> 32)
|
||||
// The low 64 bits of the product, (ma * mb), give the sticky bit.
|
||||
if (ma ~mod* mb) <> 0 {
|
||||
s |= 1
|
||||
}
|
||||
if s < 0x4000_0000_0000_0000 {
|
||||
e ~mod-= 1
|
||||
s ~mod<<= 1
|
||||
}
|
||||
if e < 0x7FD {
|
||||
// The result is normal and finite (if e is 0x7FC and the
|
||||
// significand rounds up to (1 << 53), the exponent field is 0x7FE).
|
||||
// Round to nearest, ties to even.
|
||||
rb = s & 0x3FF
|
||||
s = (s ~mod+ 0x200) >> 10
|
||||
if rb == 0x200 {
|
||||
s &= 0xFFFF_FFFF_FFFF_FFFE
|
||||
}
|
||||
return ((args.a ^ args.b) & 0x8000_0000_0000_0000) | ((e ~mod<< 52) ~mod+ s)
|
||||
}
|
||||
}
|
||||
return this.sf.f64_mul(a: args.a, b: args.b)
|
||||
}
|
||||
|
||||
// f64_from_u64 returns the f64 nearest to a (C's (double)a), like
|
||||
// softfloat.arith.f64_from_u64. Its fast path is for a < (1 << 53), whose
|
||||
// conversion is exact.
|
||||
pri func bit_cost.f64_from_u64(a: base.u64) base.u64 {
|
||||
var lz : base.u32[..= 63]
|
||||
|
||||
if args.a == 0 {
|
||||
return 0
|
||||
} else if args.a < 0x0020_0000_0000_0000 {
|
||||
// Shift a's top bit to bit 52 (the implicit bit, which is then added
|
||||
// to the exponent field).
|
||||
lz = this.clz64(a: args.a)
|
||||
if lz >= 11 {
|
||||
return (((0x43D ~mod- (lz as base.u64)) & 0x7FF) ~mod<< 52) ~mod+ (args.a ~mod<< (lz - 11))
|
||||
}
|
||||
}
|
||||
return this.sf.f64_from_u64(a: args.a)
|
||||
}
|
||||
|
||||
// f64_from_u32 returns the f64 equal to a (C's (double)a), like
|
||||
// softfloat.arith.f64_from_u32.
|
||||
pri func bit_cost.f64_from_u32(a: base.u32) base.u64 {
|
||||
return this.f64_from_u64(a: args.a as base.u64)
|
||||
}
|
||||
|
||||
// f64_to_u64_trunc returns a, truncated towards zero, like
|
||||
// softfloat.arith.f64_to_u64_trunc. Its fast path is for 0 <= a < 2**63.
|
||||
pri func bit_cost.f64_to_u64_trunc(a: base.u64) base.u64 {
|
||||
var exp : base.u64
|
||||
|
||||
exp = args.a >> 52
|
||||
if exp < 0x3FF {
|
||||
// a is in [+0 .. 1), so the result is 0.
|
||||
return 0
|
||||
} else if exp <= 0x433 {
|
||||
return ((args.a & 0x000F_FFFF_FFFF_FFFF) | 0x0010_0000_0000_0000) >> ((0x433 - exp) & 63)
|
||||
}
|
||||
return this.sf.f64_to_u64_trunc(a: args.a)
|
||||
}
|
||||
|
||||
// f32_from_u32 returns the f32 nearest to a (C's (float)a), like
|
||||
// softfloat.arith.f32_from_u32. Its fast path is for a < (1 << 24), whose
|
||||
// conversion is exact.
|
||||
pri func bit_cost.f32_from_u32(a: base.u32) base.u32 {
|
||||
var lz : base.u32[..= 63]
|
||||
|
||||
if args.a == 0 {
|
||||
return 0
|
||||
} else if args.a < 0x100_0000 {
|
||||
// Shift a's top bit to bit 23 (the implicit bit, which is then added
|
||||
// to the exponent field).
|
||||
lz = this.clz64(a: args.a as base.u64)
|
||||
if lz >= 40 {
|
||||
return ((189 ~mod- lz) ~mod<< 23) ~mod+ (args.a ~mod<< (lz - 40))
|
||||
}
|
||||
}
|
||||
return this.sf.f32_from_u32(a: args.a)
|
||||
}
|
||||
|
||||
// F32_OF_SMALL_INT[i] is the f32 equal to i (C's (float)i).
|
||||
pri const F32_OF_SMALL_INT : roarray[32] base.u32 = [
|
||||
0x0000_0000, 0x3F80_0000, 0x4000_0000, 0x4040_0000, 0x4080_0000, 0x40A0_0000,
|
||||
0x40C0_0000, 0x40E0_0000, 0x4100_0000, 0x4110_0000, 0x4120_0000, 0x4130_0000,
|
||||
0x4140_0000, 0x4150_0000, 0x4160_0000, 0x4170_0000, 0x4180_0000, 0x4188_0000,
|
||||
0x4190_0000, 0x4198_0000, 0x41A0_0000, 0x41A8_0000, 0x41B0_0000, 0x41B8_0000,
|
||||
0x41C0_0000, 0x41C8_0000, 0x41D0_0000, 0x41D8_0000, 0x41E0_0000, 0x41E8_0000,
|
||||
0x41F0_0000, 0x41F8_0000,
|
||||
]
|
||||
@@ -332,25 +332,25 @@ pri func encoder.should_merge_block!(input: roslice base.u8, from: base.u64, len
|
||||
|
||||
total = (args.len ~mod+ 42) / 43
|
||||
// r = (FastLog2(total) + 0.5) * (double)total + 200.
|
||||
r = this.sf.f64_add(
|
||||
a: this.sf.f64_mul(
|
||||
a: this.sf.f64_add(a: this.fast_log2(v: total), b: 0x3FE0_0000_0000_0000),
|
||||
b: this.sf.f64_from_u64(a: total)),
|
||||
r = this.bc.f64_add(
|
||||
a: this.bc.f64_mul(
|
||||
a: this.bc.f64_add(a: this.fast_log2(v: total), b: 0x3FE0_0000_0000_0000),
|
||||
b: this.bc.f64_from_u64(a: total)),
|
||||
b: 0x4069_0000_0000_0000)
|
||||
j = 0
|
||||
while j < 256 {
|
||||
// r -= (double)histo[i] * (depths[i] + FastLog2(histo[i])).
|
||||
h = this.histogram[j] as base.u64
|
||||
r = this.sf.f64_sub(
|
||||
r = this.bc.f64_sub(
|
||||
a: r,
|
||||
b: this.sf.f64_mul(
|
||||
a: this.sf.f64_from_u64(a: h),
|
||||
b: this.sf.f64_add(
|
||||
a: this.sf.f64_from_u32(a: this.lit_depth[j] as base.u32),
|
||||
b: this.bc.f64_mul(
|
||||
a: this.bc.f64_from_u64(a: h),
|
||||
b: this.bc.f64_add(
|
||||
a: this.bc.f64_from_u32(a: this.lit_depth[j] as base.u32),
|
||||
b: this.fast_log2(v: h))))
|
||||
j ~mod+= 1
|
||||
}
|
||||
return this.sf.f64_ge(a: r, b: 0) <> 0
|
||||
return this.bc.f64_ge(a: r, b: 0) <> 0
|
||||
}
|
||||
|
||||
// Mirrors ShouldUseUncompressedMode in compress_fragment.c.
|
||||
|
||||
@@ -472,18 +472,18 @@ pri func encoder.should_compress_two_pass!(input: roslice base.u8, from: base.u6
|
||||
var max_total_bit_cost : base.u64
|
||||
var i : base.u64
|
||||
|
||||
corpus_size = this.sf.f64_from_u64(a: args.input_size)
|
||||
corpus_size = this.bc.f64_from_u64(a: args.input_size)
|
||||
// MIN_RATIO is 0.98 (0x3FEF_5C28_F5C2_8F5C).
|
||||
if this.sf.f64_lt(
|
||||
a: this.sf.f64_from_u64(a: args.num_literals),
|
||||
b: this.sf.f64_mul(a: 0x3FEF_5C28_F5C2_8F5C, b: corpus_size)) <> 0 {
|
||||
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.sf.f64_mul(
|
||||
a: this.sf.f64_mul(a: corpus_size, b: 0x4020_0000_0000_0000),
|
||||
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)
|
||||
@@ -494,7 +494,7 @@ pri func encoder.should_compress_two_pass!(input: roslice base.u8, from: base.u6
|
||||
}
|
||||
i ~mod+= 43
|
||||
}
|
||||
return this.sf.f64_lt(a: this.bits_entropy_of_histogram(), b: max_total_bit_cost) <> 0
|
||||
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,
|
||||
|
||||
+159
-95
@@ -400,7 +400,6 @@ pri func encoder.hasher_store!(wb: slice base.u8, ix: base.u64) {
|
||||
var bsize : base.u64
|
||||
var off : base.u64
|
||||
var num : base.u16
|
||||
var hash : base.u32
|
||||
var bank : base.u64
|
||||
var idx : base.u64
|
||||
var delta : base.u64
|
||||
@@ -408,6 +407,10 @@ pri func encoder.hasher_store!(wb: slice base.u8, ix: base.u64) {
|
||||
var fc_bits : base.u32[..= 16]
|
||||
|
||||
htype = this.hasher_type
|
||||
if (htype == 58) or (htype == 68) {
|
||||
this.hasher_store_h58!(wb: args.wb, ix: args.ix)
|
||||
return nothing
|
||||
}
|
||||
if this.rb_buf_off > args.wb.length() {
|
||||
return nothing
|
||||
}
|
||||
@@ -427,16 +430,6 @@ pri func encoder.hasher_store!(wb: slice base.u8, ix: base.u64) {
|
||||
this.poke_u16!(s: args.wb, i: this.h_off0 ~mod+ (2 ~mod* (key as base.u64)), v: num ~mod+ 1)
|
||||
this.poke_u32!(s: args.wb, i: this.h_off1 ~mod+ (4 ~mod* off), v: (args.ix & 0xFFFF_FFFF) as base.u32)
|
||||
|
||||
} else if (htype == 58) or (htype == 68) {
|
||||
hash = this.hash_longest(data: data, i: args.ix & (this.rb_mask as base.u64))
|
||||
key = hash >> 8
|
||||
num = this.peek_u16(s: args.wb, i: this.h_off0 ~mod+ (2 ~mod* (key as base.u64)))
|
||||
off = ((num as base.u64) & (((1 as base.u64) << this.h_block_bits) - 1)) ~mod+
|
||||
((key as base.u64) << this.h_block_bits)
|
||||
this.poke_u16!(s: args.wb, i: this.h_off0 ~mod+ (2 ~mod* (key as base.u64)), v: num ~mod- 1)
|
||||
this.poke_u32!(s: args.wb, i: this.h_off2 ~mod+ (4 ~mod* off), v: (args.ix & 0xFFFF_FFFF) as base.u32)
|
||||
this.set_byte!(s: args.wb, i: this.h_off1 ~mod+ off, v: (hash & 0xFF) as base.u8)
|
||||
|
||||
} else if (htype == 40) or (htype == 41) or (htype == 42) {
|
||||
key = this.hash_forgetful(data: data, i: args.ix & (this.rb_mask as base.u64))
|
||||
fc_bits = this.fc_bank_bits()
|
||||
@@ -457,6 +450,36 @@ pri func encoder.hasher_store!(wb: slice base.u8, ix: base.u64) {
|
||||
}
|
||||
}
|
||||
|
||||
// hasher_store_h58 is FN(Store) for H58 and H68.
|
||||
pri func encoder.hasher_store_h58!(wb: slice base.u8, ix: base.u64) {
|
||||
var hash : base.u32
|
||||
var key : base.u64
|
||||
var koff : base.u64
|
||||
var num : base.u16
|
||||
var off : base.u64
|
||||
|
||||
hash = this.hash_h58(wb: args.wb, ix: args.ix)
|
||||
key = (hash >> 8) as base.u64
|
||||
koff = this.h_off0 ~mod+ (2 ~mod* key)
|
||||
num = this.peek_u16(s: args.wb, i: koff)
|
||||
off = ((num as base.u64) & (((1 as base.u64) << this.h_block_bits) - 1)) ~mod+ (key << this.h_block_bits)
|
||||
this.poke_u16!(s: args.wb, i: koff, v: num ~mod- 1)
|
||||
this.poke_u32!(s: args.wb, i: this.h_off2 ~mod+ (4 ~mod* off), v: (args.ix & 0xFFFF_FFFF) as base.u32)
|
||||
this.set_byte!(s: args.wb, i: this.h_off1 ~mod+ off, v: (hash & 0xFF) as base.u8)
|
||||
}
|
||||
|
||||
// hash_h58 is hash_longest for H58 and H68, of the ring buffer bytes at
|
||||
// position ix (masked).
|
||||
pri func encoder.hash_h58(wb: roslice base.u8, ix: base.u64) base.u32 {
|
||||
var i : base.u64
|
||||
|
||||
i = this.rb_buf_off ~mod+ (args.ix & (this.rb_mask as base.u64))
|
||||
if this.hasher_type == 58 {
|
||||
return (this.load32le(s: args.wb, i: i) ~mod* HASH_MUL_32) >> ((24 - this.h_bucket_bits) & 31)
|
||||
}
|
||||
return ((this.load64le(s: args.wb, i: i) ~mod* ((HASH_MUL_64 << 24) & 0xFFFF_FFFF_FFFF_FFFF)) >> 41) as base.u32
|
||||
}
|
||||
|
||||
// Mirrors FN(StoreRange) of every hasher.
|
||||
pri func encoder.hasher_store_range!(wb: slice base.u8, ix_start: base.u64, ix_end: base.u64) {
|
||||
var i : base.u64
|
||||
@@ -803,6 +826,8 @@ pri func encoder.find_last_distances!(data: roslice base.u8, cur_ix: base.u64, m
|
||||
var prev_ix : base.u64
|
||||
var len : base.u64
|
||||
var score : base.u64
|
||||
var cur_byte : base.u8
|
||||
var n : base.u32
|
||||
|
||||
mask = this.rb_mask as base.u64
|
||||
cur_ix_masked = args.cur_ix & mask
|
||||
@@ -811,15 +836,14 @@ pri func encoder.find_last_distances!(data: roslice base.u8, cur_ix: base.u64, m
|
||||
this.sr_len = 0
|
||||
this.sr_len_code_delta = 0
|
||||
|
||||
// cur_byte is data[cur_ix_masked + best_len], updated with best_len.
|
||||
cur_byte = this.byte_at(s: args.data, i: cur_ix_masked ~mod+ best_len)
|
||||
n = this.h_num_last_distances
|
||||
i = 0
|
||||
while i < this.h_num_last_distances {
|
||||
while i < n {
|
||||
backward = this.dist_cache_u64(i: i)
|
||||
prev_ix = args.cur_ix ~mod- backward
|
||||
if prev_ix >= args.cur_ix {
|
||||
i ~mod+= 1
|
||||
continue
|
||||
}
|
||||
if backward > args.max_backward {
|
||||
if (prev_ix >= args.cur_ix) or (backward > args.max_backward) {
|
||||
i ~mod+= 1
|
||||
continue
|
||||
}
|
||||
@@ -828,7 +852,7 @@ pri func encoder.find_last_distances!(data: roslice base.u8, cur_ix: base.u64, m
|
||||
break
|
||||
}
|
||||
if ((prev_ix ~mod+ best_len) > mask) or
|
||||
(this.byte_at(s: args.data, i: cur_ix_masked ~mod+ best_len) <> this.byte_at(s: args.data, i: prev_ix ~mod+ best_len)) {
|
||||
(cur_byte <> this.byte_at(s: args.data, i: prev_ix ~mod+ best_len)) {
|
||||
i ~mod+= 1
|
||||
continue
|
||||
}
|
||||
@@ -845,6 +869,7 @@ pri func encoder.find_last_distances!(data: roslice base.u8, cur_ix: base.u64, m
|
||||
this.sr_len = best_len
|
||||
this.sr_distance = backward
|
||||
this.sr_score = best_score
|
||||
cur_byte = this.byte_at(s: args.data, i: cur_ix_masked ~mod+ best_len)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -853,55 +878,6 @@ pri func encoder.find_last_distances!(data: roslice base.u8, cur_ix: base.u64, m
|
||||
return best_len
|
||||
}
|
||||
|
||||
// try_bucket_entry is the body of the bucket loop shared by the H5, H6, H58
|
||||
// and H68 FN(FindLongestMatch) functions, for one prev_ix candidate. It
|
||||
// returns 0 to continue, 1 to break, or 2 + the new best_len.
|
||||
pri func encoder.try_bucket_entry!(data: roslice base.u8, cur_ix: base.u64, prev_ix_in: base.u64, best_len: base.u64, max_length: base.u64, max_backward: base.u64) base.u64 {
|
||||
var mask : base.u64
|
||||
var cur_ix_masked : base.u64
|
||||
var prev_ix : base.u64
|
||||
var backward : base.u64
|
||||
var len : base.u64
|
||||
var score : base.u64
|
||||
|
||||
mask = this.rb_mask as base.u64
|
||||
cur_ix_masked = args.cur_ix & mask
|
||||
backward = args.cur_ix ~mod- args.prev_ix_in
|
||||
if backward > args.max_backward {
|
||||
return 1
|
||||
}
|
||||
prev_ix = args.prev_ix_in & mask
|
||||
if (cur_ix_masked ~mod+ args.best_len) > mask {
|
||||
return 1
|
||||
}
|
||||
if ((prev_ix ~mod+ args.best_len) > mask) or
|
||||
(this.load32le(s: args.data, i: (cur_ix_masked ~mod+ args.best_len) ~mod- 3) <>
|
||||
this.load32le(s: args.data, i: (prev_ix ~mod+ args.best_len) ~mod- 3)) {
|
||||
return 0
|
||||
}
|
||||
if (this.hasher_type == 6) or (this.hasher_type == 68) {
|
||||
if this.load32le(s: args.data, i: cur_ix_masked) <> this.load32le(s: args.data, i: prev_ix) {
|
||||
return 0
|
||||
}
|
||||
len = this.find_match_length_with_limit(s: args.data, i1: prev_ix ~mod+ 4, i2: cur_ix_masked ~mod+ 4,
|
||||
limit: args.max_length ~mod- 4) ~mod+ 4
|
||||
} else {
|
||||
len = this.find_match_length_with_limit(s: args.data, i1: prev_ix, i2: cur_ix_masked, limit: args.max_length)
|
||||
if len < 4 {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
score = this.backward_reference_score(copy_length: len, backward_reference_offset: backward)
|
||||
if this.sr_score < score {
|
||||
// The C code's best_score is always equal to out->score here.
|
||||
this.sr_len = len
|
||||
this.sr_distance = backward
|
||||
this.sr_score = score
|
||||
return 2 ~mod+ len
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Mirrors FN(FindLongestMatch) in hash_longest_match_inc.h (H5) and
|
||||
// hash_longest_match64_inc.h (H6).
|
||||
pri func encoder.find_longest_match_h5!(wb: slice base.u8, cur_ix: base.u64, max_length: base.u64, max_backward: base.u64, dictionary_distance: base.u64, max_distance: base.u64) {
|
||||
@@ -916,7 +892,13 @@ pri func encoder.find_longest_match_h5!(wb: slice base.u8, cur_ix: base.u64, max
|
||||
var bucket : base.u64
|
||||
var down : base.u64
|
||||
var i : base.u64
|
||||
var r : base.u64
|
||||
var mask : base.u64
|
||||
var is64 : base.bool
|
||||
var cur4 : base.u32
|
||||
var prev_ix : base.u64
|
||||
var backward : base.u64
|
||||
var len : base.u64
|
||||
var score : base.u64
|
||||
|
||||
if this.rb_buf_off > args.wb.length() {
|
||||
return nothing
|
||||
@@ -938,20 +920,46 @@ pri func encoder.find_longest_match_h5!(wb: slice base.u8, cur_ix: base.u64, max
|
||||
if (num as base.u64) > block_size {
|
||||
down = (num as base.u64) ~mod- block_size
|
||||
}
|
||||
// Try the bucket's entries, newest first. Like the C code, this
|
||||
// compares 4 bytes ending at best_len + 1 before the full match.
|
||||
mask = this.rb_mask as base.u64
|
||||
is64 = this.hasher_type == 6
|
||||
cur4 = this.load32le(s: data, i: (cur_ix_masked ~mod+ best_len) ~mod- 3)
|
||||
i = num as base.u64
|
||||
while i > down {
|
||||
i ~mod-= 1
|
||||
r = this.try_bucket_entry!(
|
||||
data: data,
|
||||
cur_ix: args.cur_ix,
|
||||
prev_ix_in: this.load32le(s: args.wb, i: bucket ~mod+ (4 ~mod* (i & block_mask))) as base.u64,
|
||||
best_len: best_len,
|
||||
max_length: args.max_length,
|
||||
max_backward: args.max_backward)
|
||||
if r == 1 {
|
||||
prev_ix = this.load32le(s: args.wb, i: bucket ~mod+ (4 ~mod* (i & block_mask))) as base.u64
|
||||
backward = args.cur_ix ~mod- prev_ix
|
||||
if backward > args.max_backward {
|
||||
break
|
||||
} else if r >= 2 {
|
||||
best_len = r ~mod- 2
|
||||
}
|
||||
prev_ix &= mask
|
||||
if (cur_ix_masked ~mod+ best_len) > mask {
|
||||
break
|
||||
}
|
||||
if ((prev_ix ~mod+ best_len) > mask) or
|
||||
(cur4 <> this.load32le(s: data, i: (prev_ix ~mod+ best_len) ~mod- 3)) {
|
||||
continue
|
||||
}
|
||||
if is64 {
|
||||
if this.load32le(s: data, i: cur_ix_masked) <> this.load32le(s: data, i: prev_ix) {
|
||||
continue
|
||||
}
|
||||
len = this.find_match_length_with_limit(s: data, i1: prev_ix ~mod+ 4, i2: cur_ix_masked ~mod+ 4,
|
||||
limit: args.max_length ~mod- 4) ~mod+ 4
|
||||
} else {
|
||||
len = this.find_match_length_with_limit(s: data, i1: prev_ix, i2: cur_ix_masked, limit: args.max_length)
|
||||
if len < 4 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
score = this.backward_reference_score(copy_length: len, backward_reference_offset: backward)
|
||||
if this.sr_score < score {
|
||||
this.sr_len = len
|
||||
this.sr_distance = backward
|
||||
this.sr_score = score
|
||||
best_len = len
|
||||
cur4 = this.load32le(s: data, i: (cur_ix_masked ~mod+ best_len) ~mod- 3)
|
||||
}
|
||||
}
|
||||
this.poke_u32!(s: args.wb, i: bucket ~mod+ (4 ~mod* ((num as base.u64) & block_mask)), v: (args.cur_ix & 0xFFFF_FFFF) as base.u32)
|
||||
@@ -984,7 +992,17 @@ pri func encoder.find_longest_match_h58!(wb: slice base.u8, cur_ix: base.u64, ma
|
||||
var limit : base.u64
|
||||
var k : base.u64
|
||||
var rb_index : base.u64
|
||||
var r : base.u64
|
||||
var splat : base.u64
|
||||
var matches : base.u64
|
||||
var chunk : base.u64
|
||||
var j : base.u64
|
||||
var mask : base.u64
|
||||
var is64 : base.bool
|
||||
var cur4 : base.u32
|
||||
var prev_ix : base.u64
|
||||
var backward : base.u64
|
||||
var len : base.u64
|
||||
var score : base.u64
|
||||
|
||||
if this.rb_buf_off > args.wb.length() {
|
||||
return nothing
|
||||
@@ -992,7 +1010,7 @@ pri func encoder.find_longest_match_h58!(wb: slice base.u8, cur_ix: base.u64, ma
|
||||
data = args.wb[this.rb_buf_off ..]
|
||||
cur_ix_masked = args.cur_ix & (this.rb_mask as base.u64)
|
||||
min_score = this.sr_score
|
||||
hash = this.hash_longest(data: data, i: cur_ix_masked)
|
||||
hash = this.hash_h58(wb: args.wb, ix: args.cur_ix)
|
||||
key = (hash >> 8) as base.u64
|
||||
block_size = (1 as base.u64) << this.h_block_bits
|
||||
block_mask = block_size ~mod- 1
|
||||
@@ -1014,24 +1032,70 @@ pri func encoder.find_longest_match_h58!(wb: slice base.u8, cur_ix: base.u64, ma
|
||||
if block_size > (n as base.u64) {
|
||||
limit = n as base.u64
|
||||
}
|
||||
k = 0
|
||||
while k < limit {
|
||||
// Like GetMatchingTagMask's non-SSE2 code: bit j of matches is whether
|
||||
// the tag of entry j matches, 8 entries at a time.
|
||||
splat = (tag as base.u64) ~mod* 0x0101_0101_0101_0101
|
||||
matches = 0
|
||||
j = 0
|
||||
while j < block_size {
|
||||
chunk = this.load64le(s: args.wb, i: tag_bucket ~mod+ j) ^ splat
|
||||
// The 0x80 bit of each byte is set when that byte is non-zero.
|
||||
chunk = (((chunk | 0x8080_8080_8080_8080) ~mod- 0x0101_0101_0101_0101) | chunk) & 0x8080_8080_8080_8080
|
||||
chunk = (chunk ^ 0x8080_8080_8080_8080) >> 7
|
||||
matches |= ((chunk ~mod* 0x0102_0408_1020_4080) >> 56) ~mod<< (j & 63)
|
||||
j ~mod+= 8
|
||||
}
|
||||
// Rotate so that bit k is entry (head + k) & block_mask.
|
||||
if head > 0 {
|
||||
matches = ((matches >> (head & 63)) | (matches ~mod<< ((block_size ~mod- head) & 63))) &
|
||||
((0xFFFF_FFFF_FFFF_FFFF as base.u64) >> ((64 ~mod- block_size) & 63))
|
||||
}
|
||||
if limit < 64 {
|
||||
matches &= ((1 as base.u64) << (limit & 63)) ~mod- 1
|
||||
}
|
||||
// Try the entries whose tags match, in the C code's order (from head).
|
||||
// Like the C code, this compares 4 bytes ending at best_len + 1 before
|
||||
// the full match.
|
||||
mask = this.rb_mask as base.u64
|
||||
is64 = this.hasher_type == 68
|
||||
cur4 = this.load32le(s: data, i: (cur_ix_masked ~mod+ best_len) ~mod- 3)
|
||||
while matches <> 0 {
|
||||
k = this.ctz64(x: matches)
|
||||
matches &= matches ~mod- 1
|
||||
rb_index = (head ~mod+ k) & block_mask
|
||||
if this.byte_at(s: args.wb, i: tag_bucket ~mod+ rb_index) == tag {
|
||||
r = this.try_bucket_entry!(
|
||||
data: data,
|
||||
cur_ix: args.cur_ix,
|
||||
prev_ix_in: this.load32le(s: args.wb, i: bucket ~mod+ (4 ~mod* rb_index)) as base.u64,
|
||||
best_len: best_len,
|
||||
max_length: args.max_length,
|
||||
max_backward: args.max_backward)
|
||||
if r == 1 {
|
||||
break
|
||||
} else if r >= 2 {
|
||||
best_len = r ~mod- 2
|
||||
prev_ix = this.load32le(s: args.wb, i: bucket ~mod+ (4 ~mod* rb_index)) as base.u64
|
||||
backward = args.cur_ix ~mod- prev_ix
|
||||
if backward > args.max_backward {
|
||||
break
|
||||
}
|
||||
prev_ix &= mask
|
||||
if (cur_ix_masked ~mod+ best_len) > mask {
|
||||
break
|
||||
}
|
||||
if ((prev_ix ~mod+ best_len) > mask) or
|
||||
(cur4 <> this.load32le(s: data, i: (prev_ix ~mod+ best_len) ~mod- 3)) {
|
||||
continue
|
||||
}
|
||||
if is64 {
|
||||
if this.load32le(s: data, i: cur_ix_masked) <> this.load32le(s: data, i: prev_ix) {
|
||||
continue
|
||||
}
|
||||
len = this.find_match_length_with_limit(s: data, i1: prev_ix ~mod+ 4, i2: cur_ix_masked ~mod+ 4,
|
||||
limit: args.max_length ~mod- 4) ~mod+ 4
|
||||
} else {
|
||||
len = this.find_match_length_with_limit(s: data, i1: prev_ix, i2: cur_ix_masked, limit: args.max_length)
|
||||
if len < 4 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
k ~mod+= 1
|
||||
score = this.backward_reference_score(copy_length: len, backward_reference_offset: backward)
|
||||
if this.sr_score < score {
|
||||
this.sr_len = len
|
||||
this.sr_distance = backward
|
||||
this.sr_score = score
|
||||
best_len = len
|
||||
cur4 = this.load32le(s: data, i: (cur_ix_masked ~mod+ best_len) ~mod- 3)
|
||||
}
|
||||
}
|
||||
this.poke_u32!(s: args.wb, i: bucket ~mod+ (4 ~mod* ((num as base.u64) & block_mask)), v: (args.cur_ix & 0xFFFF_FFFF) as base.u32)
|
||||
this.set_byte!(s: args.wb, i: tag_bucket ~mod+ ((num as base.u64) & block_mask), v: tag)
|
||||
|
||||
@@ -93,9 +93,9 @@ pri func encoder.is_mostly_utf8!(wb: roslice base.u8, pos: base.u64, length: bas
|
||||
size_utf8 ~mod+= bytes_read
|
||||
}
|
||||
}
|
||||
return this.sf.f64_gt(
|
||||
a: this.sf.f64_from_u64(a: size_utf8),
|
||||
b: this.sf.f64_mul(a: LC_F64_UTF8_FRAC, b: this.sf.f64_from_u64(a: args.length))) <> 0
|
||||
return this.bc.f64_gt(
|
||||
a: this.bc.f64_from_u64(a: size_utf8),
|
||||
b: this.bc.f64_mul(a: LC_F64_UTF8_FRAC, b: this.bc.f64_from_u64(a: args.length))) <> 0
|
||||
}
|
||||
|
||||
// Mirrors UTF8Position in literal_cost.c.
|
||||
@@ -154,17 +154,17 @@ pri func encoder.lc_finish_cost!(wb: slice base.u8, cost_off: base.u64, i: base.
|
||||
var lit_cost : base.u64
|
||||
|
||||
lit_cost = args.lit_cost
|
||||
if this.sf.f64_lt(a: lit_cost, b: LC_F64_1) <> 0 {
|
||||
lit_cost = this.sf.f64_mul(a: lit_cost, b: LC_F64_0_5)
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: LC_F64_0_5)
|
||||
if this.bc.f64_lt(a: lit_cost, b: LC_F64_1) <> 0 {
|
||||
lit_cost = this.bc.f64_mul(a: lit_cost, b: LC_F64_0_5)
|
||||
lit_cost = this.bc.f64_add(a: lit_cost, b: LC_F64_0_5)
|
||||
}
|
||||
// Make the first bytes more expensive -- seems to help, not sure why.
|
||||
// Perhaps because the entropy source is changing its properties rapidly
|
||||
// in the beginning of the file, perhaps because the beginning of the data
|
||||
// is a statistical "anomaly".
|
||||
if args.utf8 and (args.i < 2000) {
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: this.sf.f64_add(a: LC_F64_0_35,
|
||||
b: this.sf.f64_mul(a: LC_F64_MULT, b: this.sf.f64_from_u64(a: args.i))))
|
||||
lit_cost = this.bc.f64_add(a: lit_cost, b: this.bc.f64_add(a: LC_F64_0_35,
|
||||
b: this.bc.f64_mul(a: LC_F64_MULT, b: this.bc.f64_from_u64(a: args.i))))
|
||||
}
|
||||
this.poke_u32!(s: args.wb, i: args.cost_off ~mod+ (args.i ~mod* 4), v: this.sf.f64_to_f32(a: lit_cost))
|
||||
}
|
||||
@@ -278,8 +278,8 @@ pri func encoder.estimate_bit_costs_for_literals_utf8!(wb: slice base.u8, pos: b
|
||||
iw = iwu2
|
||||
}
|
||||
lit_cost = this.bc.fast_log2_memo!(v: iw)
|
||||
lit_cost = this.sf.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: LC_F64_0_02905)
|
||||
lit_cost = this.bc.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.bc.f64_add(a: lit_cost, b: LC_F64_0_02905)
|
||||
this.lc_finish_cost!(wb: args.wb, cost_off: args.cost_off, i: i, lit_cost: lit_cost, utf8: true)
|
||||
i ~mod+= 1
|
||||
}
|
||||
@@ -339,8 +339,8 @@ pri func encoder.estimate_bit_costs_for_literals!(wb: slice base.u8, pos: base.u
|
||||
histo = 1
|
||||
}
|
||||
lit_cost = this.bc.fast_log2_memo!(v: in_window)
|
||||
lit_cost = this.sf.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.sf.f64_add(a: lit_cost, b: LC_F64_0_029)
|
||||
lit_cost = this.bc.f64_sub(a: lit_cost, b: this.bc.fast_log2(v: histo))
|
||||
lit_cost = this.bc.f64_add(a: lit_cost, b: LC_F64_0_029)
|
||||
this.lc_finish_cost!(wb: args.wb, cost_off: args.cost_off, i: i, lit_cost: lit_cost, utf8: false)
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
@@ -103,6 +103,10 @@ pri struct metablock_builder?(
|
||||
cost : array[256] base.u64,
|
||||
insert_cost : array[35200] base.u64,
|
||||
|
||||
// FindBlocks' memoized (block_switch_bitcost + insert_cost[i]) sums
|
||||
// (see find_blocks).
|
||||
capped_plus : array[35200] base.u64,
|
||||
|
||||
// RemapBlockIds' new_id array.
|
||||
new_id : array[256] base.u16,
|
||||
|
||||
@@ -279,16 +283,16 @@ pri func metablock_builder.h_population_cost!(wb: roslice base.u8, h: base.u64,
|
||||
// Mirrors Log2FloorNonZero in fast_log.h (which truncates n to 32 bits).
|
||||
pri func metablock_builder.log2_floor_non_zero(n: base.u64) base.u32[..= 31] {
|
||||
var x : base.u32
|
||||
var r : base.u32[..= 31]
|
||||
|
||||
// Smear the highest set bit of the low 32 bits of n rightwards, then
|
||||
// look up its position.
|
||||
x = (args.n & 0xFFFF_FFFF) as base.u32
|
||||
while x > 1 {
|
||||
x >>= 1
|
||||
if r < 31 {
|
||||
r += 1
|
||||
}
|
||||
}
|
||||
return r
|
||||
x |= x >> 1
|
||||
x |= x >> 2
|
||||
x |= x >> 4
|
||||
x |= x >> 8
|
||||
x |= x >> 16
|
||||
return LOG2_DEBRUIJN32[(x ~mod* 0x07C4_ACDD) >> 27]
|
||||
}
|
||||
|
||||
// Mirrors PrefixEncodeCopyDistance in prefix.h. It returns the code (the
|
||||
@@ -481,12 +485,12 @@ pri func metablock_builder.compute_distance_cost!(wb: slice base.u8, cmds: rosli
|
||||
postfix_bits: args.new_npostfix) & 0xFFFF) as base.u32
|
||||
}
|
||||
this.h_add!(wb: args.wb, h: args.tmp, ds: HISTOGRAM_DISTANCE_DATA_SIZE, val: (dist_prefix & 0x3FF) as base.u64)
|
||||
extra_bits = this.sf.f64_add(a: extra_bits, b: this.sf.f64_from_u32(a: dist_prefix >> 10))
|
||||
extra_bits = this.bc.f64_add(a: extra_bits, b: this.bc.f64_from_u32(a: dist_prefix >> 10))
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
this.dist_cost = this.h_population_cost!(wb: args.wb, h: args.tmp, ds: HISTOGRAM_DISTANCE_DATA_SIZE)
|
||||
this.dist_cost = this.sf.f64_add(a: this.dist_cost, b: extra_bits)
|
||||
this.dist_cost = this.bc.f64_add(a: this.dist_cost, b: extra_bits)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -624,7 +628,7 @@ pri func metablock_builder.build_meta_block!(wb: slice base.u8, ringbuffer: rosl
|
||||
success = this.compute_distance_cost!(wb: args.wb, cmds: args.cmds, num_commands: args.num_commands,
|
||||
orig_npostfix: orig_npostfix, orig_ndirect: orig_ndirect,
|
||||
new_npostfix: npostfix, new_ndirect: ndirect, new_max_distance: this.dist_max_distance, tmp: tmp)
|
||||
if (not success) or (this.sf.f64_gt(a: this.dist_cost, b: best_dist_cost) <> 0) {
|
||||
if (not success) or (this.bc.f64_gt(a: this.dist_cost, b: best_dist_cost) <> 0) {
|
||||
break
|
||||
}
|
||||
best_dist_cost = this.dist_cost
|
||||
@@ -645,7 +649,7 @@ pri func metablock_builder.build_meta_block!(wb: slice base.u8, ringbuffer: rosl
|
||||
success = this.compute_distance_cost!(wb: args.wb, cmds: args.cmds, num_commands: args.num_commands,
|
||||
orig_npostfix: orig_npostfix, orig_ndirect: orig_ndirect,
|
||||
new_npostfix: orig_npostfix, new_ndirect: orig_ndirect, new_max_distance: orig_md, tmp: tmp)
|
||||
if this.sf.f64_lt(a: this.dist_cost, b: best_dist_cost) <> 0 {
|
||||
if this.bc.f64_lt(a: this.dist_cost, b: best_dist_cost) <> 0 {
|
||||
best_npostfix = orig_npostfix
|
||||
best_ndirect = orig_ndirect
|
||||
best_asm = orig_asm
|
||||
|
||||
@@ -201,20 +201,20 @@ pri func encoder.gbs_finish_block!(wb: slice base.u8, c: base.u32[..= 3], is_fin
|
||||
this.gbs_add_histogram!(wb: args.wb, dst: tmp, src: this.gbs_histo_off(c: args.c, ix: last1 ~mod+ i), n: data_size)
|
||||
}
|
||||
this.gbs_comb_entropy[jx & 31] = this.gbs_entropy!(wb: args.wb, off: tmp, size: alphabet_size)
|
||||
d = this.sf.f64_sub(
|
||||
a: this.sf.f64_sub(a: this.gbs_comb_entropy[jx & 31], b: e),
|
||||
d = this.bc.f64_sub(
|
||||
a: this.bc.f64_sub(a: this.gbs_comb_entropy[jx & 31], b: e),
|
||||
b: this.gbs_last_entropy[((le_base as base.u64) ~mod+ jx) & 127])
|
||||
if j == 0 {
|
||||
if nc == 1 {
|
||||
diff0 = d
|
||||
} else {
|
||||
diff0 = this.sf.f64_add(a: diff0, b: d)
|
||||
diff0 = this.bc.f64_add(a: diff0, b: d)
|
||||
}
|
||||
} else {
|
||||
if nc == 1 {
|
||||
diff1 = d
|
||||
} else {
|
||||
diff1 = this.sf.f64_add(a: diff1, b: d)
|
||||
diff1 = this.bc.f64_add(a: diff1, b: d)
|
||||
}
|
||||
}
|
||||
j ~mod+= 1
|
||||
@@ -223,8 +223,8 @@ pri func encoder.gbs_finish_block!(wb: slice base.u8, c: base.u32[..= 3], is_fin
|
||||
}
|
||||
|
||||
if (this.gbs_num_types[args.c] < this.gbs_max_block_types[args.c]) and
|
||||
(this.sf.f64_gt(a: diff0, b: threshold) <> 0) and
|
||||
(this.sf.f64_gt(a: diff1, b: threshold) <> 0) {
|
||||
(this.bc.f64_gt(a: diff0, b: threshold) <> 0) and
|
||||
(this.bc.f64_gt(a: diff1, b: threshold) <> 0) {
|
||||
// Create new block.
|
||||
this.poke_u32!(s: args.wb, i: lengths_off ~mod+ (num_blocks ~mod* 4),
|
||||
v: (this.gbs_block_size[args.c] & 0xFFFF_FFFF) as base.u32)
|
||||
@@ -250,7 +250,7 @@ pri func encoder.gbs_finish_block!(wb: slice base.u8, c: base.u32[..= 3], is_fin
|
||||
this.gbs_merge_last_count[args.c] = 0
|
||||
this.gbs_target_block_size[args.c] = this.gbs_min_block_size[args.c] as base.u64
|
||||
|
||||
} else if this.sf.f64_lt(a: diff1, b: this.sf.f64_sub(a: diff0, b: GBS_F64_20)) <> 0 {
|
||||
} else if this.bc.f64_lt(a: diff1, b: this.bc.f64_sub(a: diff0, b: GBS_F64_20)) <> 0 {
|
||||
// Combine this block with second last block.
|
||||
this.poke_u32!(s: args.wb, i: lengths_off ~mod+ (num_blocks ~mod* 4),
|
||||
v: (this.gbs_block_size[args.c] & 0xFFFF_FFFF) as base.u32)
|
||||
|
||||
@@ -18,6 +18,21 @@
|
||||
// out of bounds branches here (which do nothing, or return 0) should never be
|
||||
// taken.
|
||||
|
||||
// CTZ64_DEBRUIJN maps ((x & -x) * 0x03F7_9D71_B4CB_0A89) >> 58 to the
|
||||
// number of trailing zero bits of the non-zero x.
|
||||
pri const CTZ64_DEBRUIJN : roarray[64] base.u8[..= 63] = [
|
||||
0, 1, 48, 2, 57, 49, 28, 3, 61, 58, 50, 42, 38, 29, 17, 4,
|
||||
62, 55, 59, 36, 53, 51, 43, 22, 45, 39, 33, 30, 24, 18, 12, 5,
|
||||
63, 47, 56, 27, 60, 41, 37, 16, 54, 35, 52, 21, 44, 32, 23, 11,
|
||||
46, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
|
||||
]
|
||||
|
||||
// ctz64 returns the number of trailing zero bits of x, which must be
|
||||
// non-zero (for x == 0 it returns 0).
|
||||
pri func encoder.ctz64(x: base.u64) base.u64[..= 63] {
|
||||
return CTZ64_DEBRUIJN[((args.x & (0 ~mod- args.x)) ~mod* 0x03F7_9D71_B4CB_0A89) >> 58] as base.u64
|
||||
}
|
||||
|
||||
// byte_at returns s[i], or 0 if i is out of bounds.
|
||||
pri func encoder.byte_at(s: roslice base.u8, i: base.u64) base.u8 {
|
||||
if args.i < args.s.length() {
|
||||
@@ -28,12 +43,10 @@ pri func encoder.byte_at(s: roslice base.u8, i: base.u64) base.u8 {
|
||||
|
||||
// peek_u16 returns the 2 bytes (little-endian) of s starting at i.
|
||||
pri func encoder.peek_u16(s: roslice base.u8, i: base.u64) base.u16 {
|
||||
var q : roslice base.u8
|
||||
|
||||
if args.i < args.s.length() {
|
||||
q = args.s[args.i ..]
|
||||
if q.length() >= 2 {
|
||||
return q.peek_u16le()
|
||||
if args.i < 0xFFFF_FFFF_FFFF_0000 {
|
||||
if (args.i + 2) <= args.s.length() {
|
||||
assert args.i <= (args.i + 2) via "a <= (a + b): 0 <= b"(b: 2)
|
||||
return args.s[args.i .. args.i + 2].peek_u16le()
|
||||
}
|
||||
}
|
||||
return 0
|
||||
@@ -41,24 +54,20 @@ pri func encoder.peek_u16(s: roslice base.u8, i: base.u64) base.u16 {
|
||||
|
||||
// poke_u16 sets the 2 bytes (little-endian) of s starting at i.
|
||||
pri func encoder.poke_u16!(s: slice base.u8, i: base.u64, v: base.u16) {
|
||||
var q : slice base.u8
|
||||
|
||||
if args.i < args.s.length() {
|
||||
q = args.s[args.i ..]
|
||||
if q.length() >= 2 {
|
||||
q.poke_u16le!(a: args.v)
|
||||
if args.i < 0xFFFF_FFFF_FFFF_0000 {
|
||||
if (args.i + 2) <= args.s.length() {
|
||||
assert args.i <= (args.i + 2) via "a <= (a + b): 0 <= b"(b: 2)
|
||||
args.s[args.i .. args.i + 2].poke_u16le!(a: args.v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// poke_u32 sets the 4 bytes (little-endian) of s starting at i.
|
||||
pri func encoder.poke_u32!(s: slice base.u8, i: base.u64, v: base.u32) {
|
||||
var q : slice base.u8
|
||||
|
||||
if args.i < args.s.length() {
|
||||
q = args.s[args.i ..]
|
||||
if q.length() >= 4 {
|
||||
q.poke_u32le!(a: args.v)
|
||||
if args.i < 0xFFFF_FFFF_FFFF_0000 {
|
||||
if (args.i + 4) <= args.s.length() {
|
||||
assert args.i <= (args.i + 4) via "a <= (a + b): 0 <= b"(b: 4)
|
||||
args.s[args.i .. args.i + 4].poke_u32le!(a: args.v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ pri func encoder.z_set_cost_from_histogram!(which: base.u32, histogram_size: bas
|
||||
i ~mod+= 1
|
||||
}
|
||||
}
|
||||
missing_symbol_cost = this.sf.f32_add(a: this.sf.f64_to_f32(a: this.bc.fast_log2(v: missing_symbol_sum)), b: Z_F32_2)
|
||||
missing_symbol_cost = this.bc.f32_add(a: this.sf.f64_to_f32(a: this.bc.fast_log2(v: missing_symbol_sum)), b: Z_F32_2)
|
||||
i = 0
|
||||
while i < args.histogram_size {
|
||||
h = this.z_hist(which: args.which, i: i)
|
||||
@@ -240,9 +240,9 @@ pri func encoder.z_set_cost_from_histogram!(which: base.u32, histogram_size: bas
|
||||
this.z_set_cost!(which: args.which, i: i, v: missing_symbol_cost)
|
||||
} else {
|
||||
// Shannon bits for this symbol.
|
||||
cost = this.sf.f32_sub(a: log2sum, b: this.sf.f64_to_f32(a: this.bc.fast_log2(v: h as base.u64)))
|
||||
cost = this.bc.f32_sub(a: log2sum, b: this.sf.f64_to_f32(a: this.bc.fast_log2(v: h as base.u64)))
|
||||
// Cannot be coded with less than 1 bit.
|
||||
if this.sf.f32_lt(a: cost, b: Z_F32_1) <> 0 {
|
||||
if this.bc.f32_lt(a: cost, b: Z_F32_1) <> 0 {
|
||||
cost = Z_F32_1
|
||||
}
|
||||
this.z_set_cost!(which: args.which, i: i, v: cost)
|
||||
@@ -322,10 +322,10 @@ pri func encoder.zopfli_cost_model_set_from_commands!(wb: slice base.u8, positio
|
||||
i = 0
|
||||
while i < this.z_num_bytes {
|
||||
b = this.rb_byte(wb: args.wb, pos: args.position ~mod+ i)
|
||||
literal_carry = this.sf.f32_add(a: literal_carry, b: this.z_cost_literal[b])
|
||||
lc_i1 = this.sf.f32_add(a: lc_i, b: literal_carry)
|
||||
literal_carry = this.bc.f32_add(a: literal_carry, b: this.z_cost_literal[b])
|
||||
lc_i1 = this.bc.f32_add(a: lc_i, b: literal_carry)
|
||||
this.poke_u32!(s: args.wb, i: this.z_lit_off ~mod+ ((i ~mod+ 1) ~mod* 4), v: lc_i1)
|
||||
literal_carry = this.sf.f32_sub(a: literal_carry, b: this.sf.f32_sub(a: lc_i1, b: lc_i))
|
||||
literal_carry = this.bc.f32_sub(a: literal_carry, b: this.bc.f32_sub(a: lc_i1, b: lc_i))
|
||||
lc_i = lc_i1
|
||||
i ~mod+= 1
|
||||
}
|
||||
@@ -346,10 +346,10 @@ pri func encoder.zopfli_cost_model_set_from_literal_costs!(wb: slice base.u8, po
|
||||
lc_i = 0
|
||||
i = 0
|
||||
while i < this.z_num_bytes {
|
||||
literal_carry = this.sf.f32_add(a: literal_carry, b: this.lit_cost_at(wb: args.wb, i: i ~mod+ 1))
|
||||
lc_i1 = this.sf.f32_add(a: lc_i, b: literal_carry)
|
||||
literal_carry = this.bc.f32_add(a: literal_carry, b: this.lit_cost_at(wb: args.wb, i: i ~mod+ 1))
|
||||
lc_i1 = this.bc.f32_add(a: lc_i, b: literal_carry)
|
||||
this.poke_u32!(s: args.wb, i: this.z_lit_off ~mod+ ((i ~mod+ 1) ~mod* 4), v: lc_i1)
|
||||
literal_carry = this.sf.f32_sub(a: literal_carry, b: this.sf.f32_sub(a: lc_i1, b: lc_i))
|
||||
literal_carry = this.bc.f32_sub(a: literal_carry, b: this.bc.f32_sub(a: lc_i1, b: lc_i))
|
||||
lc_i = lc_i1
|
||||
i ~mod+= 1
|
||||
}
|
||||
@@ -368,7 +368,7 @@ pri func encoder.zopfli_cost_model_set_from_literal_costs!(wb: slice base.u8, po
|
||||
|
||||
// Mirrors ZopfliCostModelGetLiteralCosts in backward_references_hq.c.
|
||||
pri func encoder.z_literal_costs(wb: roslice base.u8, from: base.u64, to: base.u64) base.u32 {
|
||||
return this.sf.f32_sub(a: this.lit_cost_at(wb: args.wb, i: args.to), b: this.lit_cost_at(wb: args.wb, i: args.from))
|
||||
return this.bc.f32_sub(a: this.lit_cost_at(wb: args.wb, i: args.to), b: this.lit_cost_at(wb: args.wb, i: args.from))
|
||||
}
|
||||
|
||||
// Mirrors UpdateZopfliNode in backward_references_hq.c.
|
||||
@@ -420,7 +420,7 @@ pri func encoder.start_pos_queue_push!() {
|
||||
while i < len {
|
||||
a = (offset & 7) as base.u32
|
||||
b = ((offset ~mod+ 1) & 7) as base.u32
|
||||
if this.sf.f32_gt(a: this.zq_costdiff[a], b: this.zq_costdiff[b]) <> 0 {
|
||||
if this.bc.f32_gt(a: this.zq_costdiff[a], b: this.zq_costdiff[b]) <> 0 {
|
||||
this.start_pos_queue_swap!(a: a, b: b)
|
||||
}
|
||||
offset ~mod+= 1
|
||||
@@ -473,7 +473,7 @@ pri func encoder.compute_minimum_copy_length(wb: roslice base.u8, start_cost: ba
|
||||
next_len_bucket = 4
|
||||
next_len_offset = 10
|
||||
while ((args.pos ~mod+ len) <= args.num_bytes) and
|
||||
(this.sf.f32_le(a: this.zn_u(wb: args.wb, i: args.pos ~mod+ len), b: min_cost) <> 0) {
|
||||
(this.bc.f32_le(a: this.zn_u(wb: args.wb, i: args.pos ~mod+ len), b: min_cost) <> 0) {
|
||||
// We already reached (pos + len) with no more cost than the minimum
|
||||
// possible cost of reaching anything from this pos, so there is no
|
||||
// point in looking for lengths <= len.
|
||||
@@ -481,7 +481,7 @@ pri func encoder.compute_minimum_copy_length(wb: roslice base.u8, start_cost: ba
|
||||
if len == next_len_offset {
|
||||
// We reached the next copy length code bucket, so we add one
|
||||
// more extra bit to the minimum cost.
|
||||
min_cost = this.sf.f32_add(a: min_cost, b: Z_F32_1)
|
||||
min_cost = this.bc.f32_add(a: min_cost, b: Z_F32_1)
|
||||
next_len_offset ~mod+= next_len_bucket
|
||||
next_len_bucket ~mod*= 2
|
||||
}
|
||||
@@ -553,10 +553,10 @@ pri func encoder.evaluate_node!(wb: slice base.u8, block_start: base.u64, pos: b
|
||||
this.zn_set_u!(wb: args.wb, i: args.pos, v: this.compute_distance_shortcut(
|
||||
wb: args.wb, block_start: args.block_start, pos: args.pos, max_backward_limit: args.max_backward_limit))
|
||||
lc = this.z_literal_costs(wb: args.wb, from: 0, to: args.pos)
|
||||
if this.sf.f32_le(a: node_cost, b: lc) <> 0 {
|
||||
if this.bc.f32_le(a: node_cost, b: lc) <> 0 {
|
||||
this.zp_pos = args.pos
|
||||
this.zp_cost = node_cost
|
||||
this.zp_costdiff = this.sf.f32_sub(a: node_cost, b: lc)
|
||||
this.zp_costdiff = this.bc.f32_sub(a: node_cost, b: lc)
|
||||
this.compute_distance_cache!(wb: args.wb, pos: args.pos)
|
||||
this.start_pos_queue_push!()
|
||||
}
|
||||
@@ -586,7 +586,6 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
var base_cost : base.u32
|
||||
var best_len : base.u64
|
||||
var j : base.u64
|
||||
var idx : base.u32[..= 3]
|
||||
var backward : base.u64
|
||||
var bw32 : base.u32
|
||||
var prev_ix : base.u64
|
||||
@@ -607,6 +606,8 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
var max_match_len : base.u64
|
||||
var len_code : base.u64
|
||||
var gap : base.u64
|
||||
var dcs : array[4] base.u32
|
||||
var prev_copycode : base.u32
|
||||
|
||||
mask = this.rb_mask as base.u64
|
||||
gap = this.dict_total_size
|
||||
@@ -626,8 +627,8 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
pos: args.pos, max_backward_limit: args.max_backward_limit)
|
||||
|
||||
q = this.start_pos_queue_at(k: 0)
|
||||
min_cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: this.zq_cost[q], b: this.z_min_cost_cmd),
|
||||
min_cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: this.zq_cost[q], b: this.z_min_cost_cmd),
|
||||
b: this.z_literal_costs(wb: args.wb, from: this.zq_pos[q], to: args.pos))
|
||||
min_len = this.compute_minimum_copy_length(wb: args.wb, start_cost: min_cost, num_bytes: args.num_bytes, pos: args.pos)
|
||||
|
||||
@@ -639,27 +640,32 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
start = this.zq_pos[q]
|
||||
inscode = this.get_insert_length_code(insertlen: args.pos ~mod- start)
|
||||
start_costdiff = this.zq_costdiff[q]
|
||||
base_cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: start_costdiff, b: this.sf.f32_from_u32(a: INS_EXTRA[inscode.min(no_more_than: 23)])),
|
||||
base_cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: start_costdiff, b: F32_OF_SMALL_INT[INS_EXTRA[inscode.min(no_more_than: 23)]]),
|
||||
b: this.z_literal_costs(wb: args.wb, from: 0, to: args.pos))
|
||||
|
||||
// Look for last distance matches using the distance cache from this
|
||||
// starting position.
|
||||
best_len = min_len ~mod- 1
|
||||
dcs[0] = this.zq_dc[((q as base.u32) << 2) | 0]
|
||||
dcs[1] = this.zq_dc[((q as base.u32) << 2) | 1]
|
||||
dcs[2] = this.zq_dc[((q as base.u32) << 2) | 2]
|
||||
dcs[3] = this.zq_dc[((q as base.u32) << 2) | 3]
|
||||
// continuation and the break condition only depend on best_len, so
|
||||
// they are computed again only when best_len changes.
|
||||
continuation = this.rb_raw(wb: args.wb, mpos: cur_ix_masked ~mod+ best_len, k: 0)
|
||||
j = 0
|
||||
while (j < 16) and (best_len < max_len) {
|
||||
idx = Z_DISTANCE_CACHE_INDEX[j]
|
||||
bw32 = this.zq_dc[((q as base.u32) << 2) | idx] ~mod+ Z_DISTANCE_CACHE_OFFSET[j]
|
||||
if (cur_ix_masked ~mod+ best_len) > mask {
|
||||
break
|
||||
}
|
||||
bw32 = dcs[Z_DISTANCE_CACHE_INDEX[j]] ~mod+ Z_DISTANCE_CACHE_OFFSET[j]
|
||||
backward = bw32 as base.u64
|
||||
if bw32 >= 0x8000_0000 {
|
||||
backward |= 0xFFFF_FFFF_0000_0000
|
||||
}
|
||||
prev_ix = cur_ix ~mod- backward
|
||||
len = 0
|
||||
continuation = this.rb_raw(wb: args.wb, mpos: cur_ix_masked ~mod+ best_len, k: 0)
|
||||
if (cur_ix_masked ~mod+ best_len) > mask {
|
||||
break
|
||||
}
|
||||
if backward > (dictionary_start ~mod+ gap) {
|
||||
// Word dictionary -> ignore.
|
||||
j ~mod+= 1
|
||||
@@ -691,20 +697,26 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
j ~mod+= 1
|
||||
continue
|
||||
}
|
||||
dist_cost = this.sf.f32_add(a: base_cost, b: this.z_cost_dist[j])
|
||||
dist_cost = this.bc.f32_add(a: base_cost, b: this.z_cost_dist[j])
|
||||
l = best_len ~mod+ 1
|
||||
// The cost only depends on copycode (inscode and j are fixed), so
|
||||
// it is computed again only when copycode changes.
|
||||
prev_copycode = 0xFFFF_FFFF
|
||||
while l <= len {
|
||||
copycode = this.get_copy_length_code(copylen: l)
|
||||
cmdcode = this.combine_length_codes(inscode: inscode, copycode: copycode, use_last_distance: j == 0)
|
||||
if cmdcode < 128 {
|
||||
cost = base_cost
|
||||
} else {
|
||||
cost = dist_cost
|
||||
if copycode <> prev_copycode {
|
||||
prev_copycode = copycode
|
||||
cmdcode = this.combine_length_codes(inscode: inscode, copycode: copycode, use_last_distance: j == 0)
|
||||
if cmdcode < 128 {
|
||||
cost = base_cost
|
||||
} else {
|
||||
cost = dist_cost
|
||||
}
|
||||
cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: cost, b: F32_OF_SMALL_INT[COPY_EXTRA[copycode.min(no_more_than: 23)]]),
|
||||
b: this.z_cost_cmd[cmdcode.min(no_more_than: 703)])
|
||||
}
|
||||
cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: cost, b: this.sf.f32_from_u32(a: COPY_EXTRA[copycode.min(no_more_than: 23)])),
|
||||
b: this.z_cost_cmd[cmdcode.min(no_more_than: 703)])
|
||||
if this.sf.f32_lt(a: cost, b: this.zn_u(wb: args.wb, i: args.pos ~mod+ l)) <> 0 {
|
||||
if this.bc.f32_lt(a: cost, b: this.zn_u(wb: args.wb, i: args.pos ~mod+ l)) <> 0 {
|
||||
this.update_zopfli_node!(wb: args.wb, pos: args.pos, start_pos: start, len: l,
|
||||
len_code: l, dist: backward, short_code: j ~mod+ 1, cost: cost)
|
||||
result = result.max(no_less_than: l)
|
||||
@@ -712,6 +724,7 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
best_len = l
|
||||
l ~mod+= 1
|
||||
}
|
||||
continuation = this.rb_raw(wb: args.wb, mpos: cur_ix_masked ~mod+ best_len, k: 0)
|
||||
j ~mod+= 1
|
||||
}
|
||||
|
||||
@@ -739,8 +752,8 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
postfix_bits: this.distance_postfix_bits)
|
||||
dist_symbol = (pe & 0xFFFF) as base.u32
|
||||
distnumextra = dist_symbol >> 10
|
||||
dist_cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: base_cost, b: this.sf.f32_from_u32(a: distnumextra)),
|
||||
dist_cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: base_cost, b: this.bc.f32_from_u32(a: distnumextra)),
|
||||
b: this.z_cost_dist[this.min32_543(a: dist_symbol & 0x3FF)])
|
||||
|
||||
// Try all copy lengths up until the maximum copy length
|
||||
@@ -751,17 +764,22 @@ pri func encoder.update_nodes!(wb: slice base.u8, dwb: roslice base.u8, num_byte
|
||||
if (len < max_match_len) and (is_dictionary_match or (max_match_len > max_zopfli_len)) {
|
||||
len = max_match_len
|
||||
}
|
||||
// As above, the cost only depends on copycode.
|
||||
prev_copycode = 0xFFFF_FFFF
|
||||
while len <= max_match_len {
|
||||
len_code = len
|
||||
if is_dictionary_match {
|
||||
len_code = this.bm_length_code(wb: args.wb, off: moff)
|
||||
}
|
||||
copycode = this.get_copy_length_code(copylen: len_code)
|
||||
cmdcode = this.combine_length_codes(inscode: inscode, copycode: copycode, use_last_distance: false)
|
||||
cost = this.sf.f32_add(
|
||||
a: this.sf.f32_add(a: dist_cost, b: this.sf.f32_from_u32(a: COPY_EXTRA[copycode.min(no_more_than: 23)])),
|
||||
b: this.z_cost_cmd[cmdcode.min(no_more_than: 703)])
|
||||
if this.sf.f32_lt(a: cost, b: this.zn_u(wb: args.wb, i: args.pos ~mod+ len)) <> 0 {
|
||||
if copycode <> prev_copycode {
|
||||
prev_copycode = copycode
|
||||
cmdcode = this.combine_length_codes(inscode: inscode, copycode: copycode, use_last_distance: false)
|
||||
cost = this.bc.f32_add(
|
||||
a: this.bc.f32_add(a: dist_cost, b: F32_OF_SMALL_INT[COPY_EXTRA[copycode.min(no_more_than: 23)]]),
|
||||
b: this.z_cost_cmd[cmdcode.min(no_more_than: 703)])
|
||||
}
|
||||
if this.bc.f32_lt(a: cost, b: this.zn_u(wb: args.wb, i: args.pos ~mod+ len)) <> 0 {
|
||||
this.update_zopfli_node!(wb: args.wb, pos: args.pos, start_pos: start, len: len,
|
||||
len_code: len_code, dist: dist, short_code: 0, cost: cost)
|
||||
result = result.max(no_less_than: len)
|
||||
|
||||
@@ -172,50 +172,41 @@ pub func arith.f32_mul(a: base.u32, b: base.u32) base.u32 {
|
||||
|
||||
// f32_add_mags returns (sign(a) * (|a| + |b|)), for normal a and b.
|
||||
pri func arith.f32_add_mags(a: base.u32, b: base.u32) base.u32 {
|
||||
var ea : base.u32[..= 0x100]
|
||||
var eb : base.u32[..= 0xFF]
|
||||
var ma : base.u32[..= 0x3FFF_FFC0]
|
||||
var mb : base.u32[..= 0x3FFF_FFC0]
|
||||
var d : base.u32[..= 0xFF]
|
||||
var x : base.u32
|
||||
var y : base.u32
|
||||
var ex : base.u32[..= 0xFF]
|
||||
var d : base.u32
|
||||
var s : base.u64
|
||||
var z : base.u32
|
||||
var rb : base.u32
|
||||
|
||||
ea = (args.a >> 23) & 0xFF
|
||||
eb = (args.b >> 23) & 0xFF
|
||||
// The significands, with their implicit bit at bit 29 and 6 extra bits
|
||||
// of precision at the bottom.
|
||||
ma = ((args.a & 0x7F_FFFF) | 0x80_0000) << 6
|
||||
mb = ((args.b & 0x7F_FFFF) | 0x80_0000) << 6
|
||||
if ea < eb {
|
||||
d = (eb ~mod- ea) & 0xFF
|
||||
ea = eb
|
||||
z = ma
|
||||
ma = mb
|
||||
mb = z
|
||||
// Order the magnitudes so that |x| >= |y|.
|
||||
x = args.a & F32_ABS
|
||||
y = args.b & F32_ABS
|
||||
if y > x {
|
||||
x = args.b & F32_ABS
|
||||
y = args.a & F32_ABS
|
||||
}
|
||||
ex = (x >> 23) & 0xFF
|
||||
d = ex ~mod- ((y >> 23) & 0xFF)
|
||||
if d >= 25 {
|
||||
// |y| is less than half an ULP of |x|, so the sum rounds to x.
|
||||
return (args.a & F32_SIGN) | x
|
||||
}
|
||||
// The significands, with their implicit bit at bit 62. Shifting y's
|
||||
// right by at most 24 is exact, and the sum is less than (1 << 64).
|
||||
s = ((((x & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) ~mod+
|
||||
(((((y & 0x7F_FFFF) | 0x80_0000) as base.u64) << 39) >> (d & 31))
|
||||
// Round to nearest, ties to even: add just under half an ULP, plus one
|
||||
// if the truncated result is odd. The result is in [1 << 23 ..= 1 << 24]
|
||||
// and adding it (whose bit 23 is the implicit bit) to the shifted
|
||||
// exponent carries any rounding overflow into the exponent field.
|
||||
if s >= 0x8000_0000_0000_0000 {
|
||||
s = (s ~mod+ (0x7F_FFFF_FFFF ~mod+ ((s >> 40) & 1))) >> 40
|
||||
z = (ex ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
} else {
|
||||
d = (ea ~mod- eb) & 0xFF
|
||||
s = (s ~mod+ (0x3F_FFFF_FFFF ~mod+ ((s >> 39) & 1))) >> 39
|
||||
z = ((ex ~mod- 1) ~mod<< 23) ~mod+ ((s & 0x1FF_FFFF) as base.u32)
|
||||
}
|
||||
if d >= 31 {
|
||||
mb = 1
|
||||
} else if d > 0 {
|
||||
if (mb ~mod<< (32 - d)) <> 0 {
|
||||
mb = (mb >> d) | 1
|
||||
} else {
|
||||
mb = mb >> d
|
||||
}
|
||||
}
|
||||
z = (ma & 0x3FFF_FFFF) + (mb & 0x3FFF_FFFF)
|
||||
if z >= 0x4000_0000 {
|
||||
ea += 1
|
||||
z = (z >> 1) | (z & 1)
|
||||
}
|
||||
rb = z & 0x3F
|
||||
z = (z ~mod+ 0x20) >> 6
|
||||
if rb == 0x20 {
|
||||
z &= 0xFFFF_FFFE
|
||||
}
|
||||
// ea is in [1 ..= 255] and z is in [1 << 23 ..= 1 << 24].
|
||||
z = ((ea ~mod- 1) ~mod<< 23) ~mod+ z
|
||||
if z >= F32_INF {
|
||||
z = F32_INF
|
||||
}
|
||||
|
||||
@@ -1740,6 +1740,377 @@ test_wuffs_brotli_encode_interface() {
|
||||
return status;
|
||||
}
|
||||
|
||||
// ---------------- Decoder Metadata Tests
|
||||
|
||||
// The metadata tests encode romeo.txt with metadata blocks (whose contents
|
||||
// are also taken from romeo.txt) and then decode it with
|
||||
// QUIRK_REPORT_METADATA. The decoded output ("out") and the metadata ("md",
|
||||
// each block prefixed by its length as 4 little-endian bytes) are collected
|
||||
// separately.
|
||||
|
||||
#define BROTLI_MD_NUM_STEPS 8
|
||||
#define BROTLI_MD_MAX_BLOCKS 8
|
||||
|
||||
static const uint32_t g_brotli_md_step_ops[BROTLI_MD_NUM_STEPS] = {
|
||||
BROTLI_ENC_OP_EMIT_METADATA, BROTLI_ENC_OP_PROCESS,
|
||||
BROTLI_ENC_OP_EMIT_METADATA, BROTLI_ENC_OP_EMIT_METADATA,
|
||||
BROTLI_ENC_OP_FLUSH, BROTLI_ENC_OP_EMIT_METADATA,
|
||||
BROTLI_ENC_OP_PROCESS, BROTLI_ENC_OP_FINISH,
|
||||
};
|
||||
|
||||
static const size_t g_brotli_md_step_lens[BROTLI_MD_NUM_STEPS] = {
|
||||
3, 500, 10, 0, 0, 70000, 1000, 50000,
|
||||
};
|
||||
|
||||
// brotli_md_want fills out and md with the expected results for the
|
||||
// g_brotli_md_step_etc plan applied to src. It sets md_pos[i] to the length of
|
||||
// out before the i'th metadata block.
|
||||
static void //
|
||||
brotli_md_want(wuffs_base__io_buffer* out,
|
||||
wuffs_base__io_buffer* md,
|
||||
size_t* md_pos,
|
||||
const uint8_t* src) {
|
||||
size_t n_md = 0;
|
||||
for (size_t i = 0; i < BROTLI_MD_NUM_STEPS; i++) {
|
||||
size_t n = g_brotli_md_step_lens[i];
|
||||
if (g_brotli_md_step_ops[i] == BROTLI_ENC_OP_EMIT_METADATA) {
|
||||
md_pos[n_md++] = out->meta.wi;
|
||||
wuffs_base__poke_u32le__no_bounds_check(md->data.ptr + md->meta.wi,
|
||||
(uint32_t)n);
|
||||
md->meta.wi += 4;
|
||||
memcpy(md->data.ptr + md->meta.wi, src, n);
|
||||
md->meta.wi += n;
|
||||
} else {
|
||||
memcpy(out->data.ptr + out->meta.wi, src, n);
|
||||
out->meta.wi += n;
|
||||
}
|
||||
src += n;
|
||||
}
|
||||
}
|
||||
|
||||
// wuffs_brotli_decode_metadata decodes src_ptr[.. src_len] with
|
||||
// QUIRK_REPORT_METADATA, at most dst_chunk bytes of dst and src_chunk more
|
||||
// bytes of src per transform_io call.
|
||||
static const char* //
|
||||
wuffs_brotli_decode_metadata(wuffs_base__io_buffer* out,
|
||||
wuffs_base__io_buffer* md,
|
||||
size_t* md_pos,
|
||||
size_t* num_md,
|
||||
const uint8_t* src_ptr,
|
||||
size_t src_len,
|
||||
size_t dst_chunk,
|
||||
size_t src_chunk) {
|
||||
wuffs_brotli__decoder dec;
|
||||
CHECK_STATUS("initialize",
|
||||
wuffs_brotli__decoder__initialize(
|
||||
&dec, sizeof dec, WUFFS_VERSION,
|
||||
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED));
|
||||
CHECK_STATUS("set_quirk",
|
||||
wuffs_brotli__decoder__set_quirk(
|
||||
&dec, WUFFS_BROTLI__QUIRK_REPORT_METADATA, 1));
|
||||
if (wuffs_brotli__decoder__get_quirk(
|
||||
&dec, WUFFS_BROTLI__QUIRK_REPORT_METADATA) != 1) {
|
||||
RETURN_FAIL("get_quirk: have 0, want 1");
|
||||
}
|
||||
|
||||
uint8_t tmp[4096];
|
||||
if (dst_chunk > sizeof tmp) {
|
||||
RETURN_FAIL("dst_chunk is too large");
|
||||
}
|
||||
size_t md_remaining = 0;
|
||||
*num_md = 0;
|
||||
wuffs_base__io_buffer src = wuffs_base__ptr_u8__reader(
|
||||
(uint8_t*)src_ptr, src_chunk < src_len ? src_chunk : src_len,
|
||||
src_chunk >= src_len);
|
||||
while (true) {
|
||||
wuffs_base__io_buffer dst = wuffs_base__ptr_u8__writer(tmp, dst_chunk);
|
||||
wuffs_base__status status = wuffs_brotli__decoder__transform_io(
|
||||
&dec, &dst, &src, g_work_slice_u8);
|
||||
|
||||
uint8_t* p = tmp;
|
||||
size_t n = dst.meta.wi;
|
||||
if (md_remaining > 0) {
|
||||
size_t m = (n < md_remaining) ? n : md_remaining;
|
||||
if (md->data.len - md->meta.wi < m) {
|
||||
RETURN_FAIL("md is too short");
|
||||
}
|
||||
memcpy(md->data.ptr + md->meta.wi, p, m);
|
||||
md->meta.wi += m;
|
||||
md_remaining -= m;
|
||||
p += m;
|
||||
n -= m;
|
||||
}
|
||||
if (n > 0) {
|
||||
if (md_remaining > 0) {
|
||||
RETURN_FAIL("inconsistent md_remaining");
|
||||
} else if (out->data.len - out->meta.wi < n) {
|
||||
RETURN_FAIL("out is too short");
|
||||
}
|
||||
memcpy(out->data.ptr + out->meta.wi, p, n);
|
||||
out->meta.wi += n;
|
||||
}
|
||||
|
||||
if (status.repr == NULL) {
|
||||
break;
|
||||
} else if (status.repr == wuffs_base__note__metadata_reported) {
|
||||
if (md_remaining > 0) {
|
||||
RETURN_FAIL("metadata reported while copying metadata");
|
||||
} else if (*num_md >= BROTLI_MD_MAX_BLOCKS) {
|
||||
RETURN_FAIL("too many metadata blocks");
|
||||
}
|
||||
uint64_t len = wuffs_brotli__decoder__metadata_length(&dec);
|
||||
md_pos[(*num_md)++] = out->meta.wi;
|
||||
if (md->data.len - md->meta.wi < 4) {
|
||||
RETURN_FAIL("md is too short");
|
||||
}
|
||||
wuffs_base__poke_u32le__no_bounds_check(md->data.ptr + md->meta.wi,
|
||||
(uint32_t)len);
|
||||
md->meta.wi += 4;
|
||||
md_remaining = (size_t)len;
|
||||
} else if (status.repr == wuffs_base__suspension__short_read) {
|
||||
if (src.meta.closed) {
|
||||
RETURN_FAIL("short read on closed src");
|
||||
}
|
||||
src.meta.wi = (src_len - src.meta.wi > src_chunk) ? src.meta.wi + src_chunk
|
||||
: src_len;
|
||||
src.meta.closed = src.meta.wi == src_len;
|
||||
} else if (status.repr != wuffs_base__suspension__short_write) {
|
||||
RETURN_FAIL("transform_io: \"%s\"", status.repr);
|
||||
}
|
||||
}
|
||||
if (md_remaining > 0) {
|
||||
RETURN_FAIL("incomplete metadata");
|
||||
}
|
||||
|
||||
// Setting the quirk after decoding has started fails.
|
||||
wuffs_base__status status = wuffs_brotli__decoder__set_quirk(
|
||||
&dec, WUFFS_BROTLI__QUIRK_REPORT_METADATA, 0);
|
||||
if (status.repr != wuffs_base__error__bad_call_sequence) {
|
||||
RETURN_FAIL("set_quirk after start: have \"%s\", want \"%s\"", status.repr,
|
||||
wuffs_base__error__bad_call_sequence);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char* //
|
||||
do_test_brotli_decode_metadata(const char* (*encode)(wuffs_base__io_buffer*,
|
||||
wuffs_base__io_buffer*,
|
||||
const uint32_t*,
|
||||
const uint32_t*,
|
||||
size_t,
|
||||
const uint32_t*,
|
||||
const size_t*,
|
||||
size_t,
|
||||
size_t)) {
|
||||
wuffs_base__io_buffer data = ((wuffs_base__io_buffer){
|
||||
.data = g_mimiclib_scratch_slice_u8,
|
||||
});
|
||||
CHECK_STRING(read_file(&data, "test/data/romeo.txt"));
|
||||
// The plan needs 121513 bytes, so repeat romeo.txt (which is shorter).
|
||||
size_t total = 0;
|
||||
for (size_t i = 0; i < BROTLI_MD_NUM_STEPS; i++) {
|
||||
total += g_brotli_md_step_lens[i];
|
||||
}
|
||||
if (data.meta.wi == 0) {
|
||||
RETURN_FAIL("empty romeo.txt");
|
||||
}
|
||||
while (data.meta.wi < total) {
|
||||
size_t n = data.meta.wi;
|
||||
if (n > total - data.meta.wi) {
|
||||
n = total - data.meta.wi;
|
||||
}
|
||||
memmove(data.data.ptr + data.meta.wi, data.data.ptr, n);
|
||||
data.meta.wi += n;
|
||||
}
|
||||
|
||||
// The want and have halves of g_want_slice_u8 and g_have_slice_u8.
|
||||
size_t half_w = g_want_slice_u8.len / 2;
|
||||
size_t half_h = g_have_slice_u8.len / 2;
|
||||
wuffs_base__io_buffer want_out = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_want_slice_u8.ptr, half_w),
|
||||
});
|
||||
wuffs_base__io_buffer want_md = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_want_slice_u8.ptr + half_w, half_w),
|
||||
});
|
||||
size_t want_md_pos[BROTLI_MD_MAX_BLOCKS] = {0};
|
||||
brotli_md_want(&want_out, &want_md, want_md_pos, data.data.ptr);
|
||||
|
||||
static const uint32_t qualities[3] = {0, 5, 11};
|
||||
static const size_t chunks[4][2] = {{4096, 1 << 30}, {1, 1}, {7, 3}, {3, 64}};
|
||||
for (int qi = 0; qi < 3; qi++) {
|
||||
// The decoder uses g_work_slice_u8 as its workbuf, so encode into the
|
||||
// second half of g_have_slice_u8.
|
||||
wuffs_base__io_buffer enc = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_have_slice_u8.ptr + half_h, half_h),
|
||||
});
|
||||
wuffs_base__io_buffer src = data;
|
||||
src.meta.ri = 0;
|
||||
uint32_t keys[2] = {BROTLI_ENC_PARAM_QUALITY, BROTLI_ENC_PARAM_LGWIN};
|
||||
uint32_t values[2] = {qualities[qi], 18};
|
||||
CHECK_STRING((*encode)(&enc, &src, keys, values, 2, g_brotli_md_step_ops,
|
||||
g_brotli_md_step_lens, BROTLI_MD_NUM_STEPS, 0));
|
||||
|
||||
for (int ci = 0; ci < 4; ci++) {
|
||||
// Decoded output and metadata share the first half of g_have_slice_u8.
|
||||
wuffs_base__io_buffer have_out = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_have_slice_u8.ptr, half_h / 2),
|
||||
});
|
||||
wuffs_base__io_buffer have_md = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_have_slice_u8.ptr + half_h / 2,
|
||||
half_h / 2),
|
||||
});
|
||||
size_t have_md_pos[BROTLI_MD_MAX_BLOCKS] = {0};
|
||||
size_t num_md = 0;
|
||||
const char* z = wuffs_brotli_decode_metadata(
|
||||
&have_out, &have_md, have_md_pos, &num_md, enc.data.ptr, enc.meta.wi,
|
||||
chunks[ci][0], chunks[ci][1]);
|
||||
if (z) {
|
||||
RETURN_FAIL("q=%u, chunks=%d: %s", qualities[qi], ci, z);
|
||||
}
|
||||
char s[64];
|
||||
snprintf(s, sizeof s, "q=%u, chunks=%d: out", qualities[qi], ci);
|
||||
CHECK_STRING(check_io_buffers_equal(s, &have_out, &want_out));
|
||||
snprintf(s, sizeof s, "q=%u, chunks=%d: md", qualities[qi], ci);
|
||||
CHECK_STRING(check_io_buffers_equal(s, &have_md, &want_md));
|
||||
if (num_md != 4) {
|
||||
RETURN_FAIL("q=%u, chunks=%d: num_md: have %zu, want 4",
|
||||
qualities[qi], ci, num_md);
|
||||
}
|
||||
for (size_t i = 0; i < num_md; i++) {
|
||||
if (have_md_pos[i] != want_md_pos[i]) {
|
||||
RETURN_FAIL("q=%u, chunks=%d: md_pos[%zu]: have %zu, want %zu",
|
||||
qualities[qi], ci, i, have_md_pos[i], want_md_pos[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_brotli_decode_metadata() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_brotli_decode_metadata(wuffs_brotli_encode_plan);
|
||||
}
|
||||
|
||||
#ifdef WUFFS_MIMIC
|
||||
|
||||
// mimic_brotli_md_start_func and mimic_brotli_md_chunk_func are libbrotlidec
|
||||
// metadata callbacks that append to the wuffs_base__io_buffer opaque.
|
||||
static void //
|
||||
mimic_brotli_md_start_func(void* opaque, size_t size) {
|
||||
wuffs_base__io_buffer* md = (wuffs_base__io_buffer*)opaque;
|
||||
if (md->data.len - md->meta.wi >= 4) {
|
||||
wuffs_base__poke_u32le__no_bounds_check(md->data.ptr + md->meta.wi,
|
||||
(uint32_t)size);
|
||||
md->meta.wi += 4;
|
||||
} else {
|
||||
md->meta.closed = true; // Overflow.
|
||||
}
|
||||
}
|
||||
|
||||
static void //
|
||||
mimic_brotli_md_chunk_func(void* opaque, const uint8_t* data, size_t size) {
|
||||
wuffs_base__io_buffer* md = (wuffs_base__io_buffer*)opaque;
|
||||
if (md->data.len - md->meta.wi >= size) {
|
||||
memcpy(md->data.ptr + md->meta.wi, data, size);
|
||||
md->meta.wi += size;
|
||||
} else {
|
||||
md->meta.closed = true; // Overflow.
|
||||
}
|
||||
}
|
||||
|
||||
// test_mimic_brotli_decode_metadata checks that QUIRK_REPORT_METADATA reports
|
||||
// the same metadata as libbrotlidec's metadata callbacks, for streams that
|
||||
// libbrotlienc produces (and for the streams of the brotli test data, which
|
||||
// have no metadata blocks), with a variety of input chunk sizes.
|
||||
const char* //
|
||||
test_mimic_brotli_decode_metadata() {
|
||||
CHECK_FOCUS(__func__);
|
||||
CHECK_STRING(do_test_brotli_decode_metadata(mimic_brotli_encode));
|
||||
|
||||
// Also compare libbrotlidec's callbacks with Wuffs on a libbrotlienc
|
||||
// stream, fed to libbrotlidec in 5-byte pieces.
|
||||
wuffs_base__io_buffer data = ((wuffs_base__io_buffer){
|
||||
.data = g_mimiclib_scratch_slice_u8,
|
||||
});
|
||||
CHECK_STRING(read_file(&data, "test/data/romeo.txt"));
|
||||
size_t half_h = g_have_slice_u8.len / 2;
|
||||
size_t half_w = g_want_slice_u8.len / 2;
|
||||
wuffs_base__io_buffer enc = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_have_slice_u8.ptr + half_h, half_h),
|
||||
});
|
||||
static const uint32_t ops[5] = {
|
||||
BROTLI_ENC_OP_PROCESS, BROTLI_ENC_OP_EMIT_METADATA,
|
||||
BROTLI_ENC_OP_EMIT_METADATA, BROTLI_ENC_OP_PROCESS,
|
||||
BROTLI_ENC_OP_FINISH,
|
||||
};
|
||||
size_t lens[5] = {100, 200, 0, 300, 0};
|
||||
lens[4] = data.meta.wi - 600;
|
||||
uint32_t keys[1] = {BROTLI_ENC_PARAM_QUALITY};
|
||||
uint32_t values[1] = {9};
|
||||
CHECK_STRING(mimic_brotli_encode(&enc, &data, keys, values, 1, ops, lens, 5,
|
||||
0));
|
||||
|
||||
wuffs_base__io_buffer c_out = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_want_slice_u8.ptr, half_w),
|
||||
});
|
||||
wuffs_base__io_buffer c_md = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_want_slice_u8.ptr + half_w, half_w),
|
||||
});
|
||||
BrotliDecoderState* state = BrotliDecoderCreateInstance(NULL, NULL, NULL);
|
||||
if (!state) {
|
||||
RETURN_FAIL("BrotliDecoderCreateInstance failed");
|
||||
}
|
||||
BrotliDecoderSetMetadataCallbacks(state, mimic_brotli_md_start_func,
|
||||
mimic_brotli_md_chunk_func, &c_md);
|
||||
size_t pos = 0;
|
||||
BrotliDecoderResult r = BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
|
||||
while (r != BROTLI_DECODER_RESULT_SUCCESS) {
|
||||
size_t end = (enc.meta.wi - pos > 5) ? pos + 5 : enc.meta.wi;
|
||||
size_t avail_in = end - pos;
|
||||
const uint8_t* next_in = enc.data.ptr + pos;
|
||||
size_t avail_out = c_out.data.len - c_out.meta.wi;
|
||||
uint8_t* next_out = c_out.data.ptr + c_out.meta.wi;
|
||||
r = BrotliDecoderDecompressStream(state, &avail_in, &next_in, &avail_out,
|
||||
&next_out, NULL);
|
||||
pos = end - avail_in;
|
||||
c_out.meta.wi = c_out.data.len - avail_out;
|
||||
if ((r == BROTLI_DECODER_RESULT_ERROR) ||
|
||||
((r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) &&
|
||||
(pos == enc.meta.wi))) {
|
||||
BrotliDecoderDestroyInstance(state);
|
||||
RETURN_FAIL("libbrotlidec failed");
|
||||
}
|
||||
}
|
||||
BrotliDecoderDestroyInstance(state);
|
||||
if (c_md.meta.closed) {
|
||||
RETURN_FAIL("libbrotlidec metadata overflow");
|
||||
}
|
||||
|
||||
wuffs_base__io_buffer have_out = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_have_slice_u8.ptr, half_h / 2),
|
||||
});
|
||||
wuffs_base__io_buffer have_md = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(g_have_slice_u8.ptr + half_h / 2,
|
||||
half_h / 2),
|
||||
});
|
||||
size_t have_md_pos[BROTLI_MD_MAX_BLOCKS] = {0};
|
||||
size_t num_md = 0;
|
||||
CHECK_STRING(wuffs_brotli_decode_metadata(&have_out, &have_md, have_md_pos,
|
||||
&num_md, enc.data.ptr, enc.meta.wi,
|
||||
5, 5));
|
||||
CHECK_STRING(check_io_buffers_equal("out", &have_out, &c_out));
|
||||
CHECK_STRING(check_io_buffers_equal("md", &have_md, &c_md));
|
||||
if ((num_md != 2) || (have_md_pos[0] != 100) || (have_md_pos[1] != 100)) {
|
||||
RETURN_FAIL("md_pos: have %zu blocks at %zu, %zu; want 2 at 100, 100",
|
||||
num_md, have_md_pos[0], have_md_pos[1]);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif // WUFFS_MIMIC
|
||||
|
||||
// ---------------- Encoder Mimic Tests
|
||||
|
||||
#ifdef WUFFS_MIMIC
|
||||
@@ -4618,6 +4989,7 @@ proc g_tests[] = {
|
||||
test_wuffs_brotli_decode_all,
|
||||
test_wuffs_brotli_decode_all_with_io_limits,
|
||||
test_wuffs_brotli_decode_interface,
|
||||
test_wuffs_brotli_decode_metadata,
|
||||
test_wuffs_brotli_decode_malformed,
|
||||
test_wuffs_brotli_decode_romeo,
|
||||
test_wuffs_brotli_decode_trailing_data,
|
||||
@@ -4642,6 +5014,7 @@ proc g_tests[] = {
|
||||
test_mimic_brotli_decode_all,
|
||||
test_mimic_brotli_decode_differential,
|
||||
test_mimic_brotli_decode_malformed,
|
||||
test_mimic_brotli_decode_metadata,
|
||||
test_mimic_brotli_decode_romeo,
|
||||
test_mimic_brotli_decode_split_block_length,
|
||||
|
||||
|
||||
Reference in new issue
Block a user