std/brotli: add an encoder for quality levels 0 and 1

Adds a brotli encoder (std/brotli's encoder struct, a base.io_transformer)
whose output is byte-identical to libbrotlienc v1.2.1 for quality levels 0
and 1, mirroring encode.c's BrotliEncoderCompressStream(Fast),
compress_fragment.c and compress_fragment_two_pass.c, plus the entropy
coding and bit stream helpers they need. Every double operation goes
through std/softfloat, following the C evaluation order exactly.

Parameters are quirks (encode_quirks.wuffs) that mirror
BrotliEncoderSetParameter, sanitized at the first transform_io call.
Each transform_io resumption corresponds to one C call with available_in
equal to the readable src length; a closed src is FINISH; request_flush!
and request_emit_metadata! make the next resumption a FLUSH or
EMIT_METADATA call. Qualities 2 to 11 return "#unsupported encoder
quality" for now. Encoder tables are generated by
script/print-brotli-tables.go -encoder.

Tests: round trips through the Wuffs decoder over the corpus and synthetic
inputs; with -mimic, byte-exact comparisons against libbrotlienc over
quality × window × mode × input chunking × flush schedule × metadata ×
size hint. script/manual-test-brotli-encoder-parity.c runs the full
grid: 86,268 configurations, zero mismatches.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011NoosQNe7DKWnqDCBeqw1p
This commit is contained in:
Claude committed 2026-10-26 23:55:56 +00:00
1 parent 4978207531
commit ca1ba9d134
17 files changed
+13990 -2772

No files matched your search

+1
View File
@@ -18,6 +18,7 @@ The LICENSE has changed from a single license (Apache 2) to a dual license
- Added `example/mzcat`.
- Added `get_quirk(key: u32) u64`.
- Added `std/brotli`.
- Added a `std/brotli` encoder (qualities 0 and 1).
- Added `std/crc64`.
- Added `std/etc2`.
- Added `std/handsum`.
+1
View File
@@ -5,6 +5,7 @@ particular file format or algorithm. Those packages can be grouped into several
categories:
- [Compression Decoders](/doc/std/compression-decoders.md).
- [Compression Encoders](/doc/std/compression-encoders.md).
- [Hashers](/doc/std/hashers.md).
- [Image Decoders](/doc/std/image-decoders.md).
+35
View File
@@ -0,0 +1,35 @@
# Compression Encoders
Compression encoders read from one input stream (an `io_reader` called `src`)
and write to an output stream (an `io_writer` called `dst`). Like
[compression decoders](/doc/std/compression-decoders.md), they implement the
`base.io_transformer` interface, whose key method is `transform_io`. It
incrementally compresses the source data.
This method is a [coroutine](/doc/note/coroutines.md), and does not require
either all of the input or all of the output to fit in a single contiguous
buffer. It suspends with the `$short_read` status when it has consumed all of
`src` (and `src` is not closed) and with `$short_write` when `dst` is full. It
returns ok once `src` is closed and all of the compressed data has been
written. Encoder parameters (e.g. the compression quality) are set with
`set_quirk` before the first `transform_io` call.
## API Listing
In Wuffs syntax, the `base.io_transformer` methods are:
- `dst_history_retain_length() u64`
- `get_quirk(key: u32) u64`
- `set_quirk!(key: u32, value: u64) status`
- `transform_io?(dst: io_writer, src: io_reader, workbuf: slice u8)`
- `workbuf_len() range_ii_u64`
Encoders may have further, format-specific methods. For example, the brotli
encoder has `request_flush`, `request_emit_metadata` and
`max_compressed_size`.
## Implementations
- [std/brotli](/std/brotli/README.md#encoder) (qualities 0 and 1 only).
+7829 -2769
View File
@@ -8490,6 +8490,723 @@ struct wuffs_bmp__decoder__struct {
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BMP) || defined(WUFFS_NONMONOLITHIC)
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SOFTFLOAT) || defined(WUFFS_NONMONOLITHIC)
// ---------------- Status Codes
// ---------------- Public Consts
// ---------------- Struct Declarations
typedef struct wuffs_softfloat__arith__struct wuffs_softfloat__arith;
#ifdef __cplusplus
extern "C" {
#endif
// ---------------- Public Initializer Prototypes
// For any given "wuffs_foo__bar* self", "wuffs_foo__bar__initialize(self,
// etc)" should be called before any other "wuffs_foo__bar__xxx(self, etc)".
//
// Pass sizeof(*self) and WUFFS_VERSION for sizeof_star_self and wuffs_version.
// Pass 0 (or some combination of WUFFS_INITIALIZE__XXX) for options.
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
wuffs_softfloat__arith__initialize(
wuffs_softfloat__arith* self,
size_t sizeof_star_self,
uint64_t wuffs_version,
uint32_t options);
size_t
sizeof__wuffs_softfloat__arith(void);
// ---------------- Allocs
// These functions allocate and initialize Wuffs structs. They return NULL if
// memory allocation fails. If they return non-NULL, there is no need to call
// wuffs_foo__bar__initialize, but the caller is responsible for eventually
// calling free on the returned pointer. That pointer is effectively a C++
// std::unique_ptr<T, wuffs_unique_ptr_deleter>.
wuffs_softfloat__arith*
wuffs_softfloat__arith__alloc(void);
// ---------------- Upcasts
// ---------------- Public Function Prototypes
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_is_nan(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_neg(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_abs(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_cmp(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_eq(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_ne(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_lt(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_le(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_gt(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_ge(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_min(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_max(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_add(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_sub(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_mul(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_div(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_fma(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_c);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_u64(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_u32(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_i64(
const wuffs_softfloat__arith* self,
int64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_to_u64_trunc(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_to_f32(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f32_to_f64(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_is_nan(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_neg(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_abs(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_add(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_sub(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_mul(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_div(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_cmp(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_eq(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_ne(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_lt(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_le(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_gt(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_ge(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_min(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_max(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_u32(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_i32(
const wuffs_softfloat__arith* self,
int32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_u64(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__log2_u64(
const wuffs_softfloat__arith* self,
uint64_t a_v);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__log2_f64(
const wuffs_softfloat__arith* self,
uint64_t a_x);
#ifdef __cplusplus
} // extern "C"
#endif
// ---------------- Struct Definitions
// These structs' fields, and the sizeof them, are private implementation
// details that aren't guaranteed to be stable across Wuffs versions.
//
// See https://en.wikipedia.org/wiki/Opaque_pointer#C
#if defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION)
struct wuffs_softfloat__arith__struct {
// Do not access the private_impl's or private_data's fields directly. There
// is no API/ABI compatibility or safety guarantee if you do so. Instead, use
// the wuffs_foo__bar__baz functions.
//
// It is a struct, not a struct*, so that the outermost wuffs_foo__bar struct
// can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
wuffs_base__vtable null_vtable;
} private_impl;
#ifdef __cplusplus
#if defined(WUFFS_BASE__HAVE_UNIQUE_PTR)
using unique_ptr = std::unique_ptr<wuffs_softfloat__arith, wuffs_unique_ptr_deleter>;
// On failure, the alloc_etc functions return nullptr. They don't throw.
static inline unique_ptr
alloc() {
return unique_ptr(wuffs_softfloat__arith__alloc());
}
#endif // defined(WUFFS_BASE__HAVE_UNIQUE_PTR)
#if defined(WUFFS_BASE__HAVE_EQ_DELETE) && !defined(WUFFS_IMPLEMENTATION)
// Disallow constructing or copying an object via standard C++ mechanisms,
// e.g. the "new" operator, as this struct is intentionally opaque. Its total
// size and field layout is not part of the public, stable, memory-safe API.
// Use malloc or memcpy and the sizeof__wuffs_foo__bar function instead, and
// call wuffs_foo__bar__baz methods (which all take a "this"-like pointer as
// their first argument) rather than tweaking bar.private_impl.qux fields.
//
// In C, we can just leave wuffs_foo__bar as an incomplete type (unless
// WUFFS_IMPLEMENTATION is #define'd). In C++, we define a complete type in
// order to provide convenience methods. These forward on "this", so that you
// can write "bar->baz(etc)" instead of "wuffs_foo__bar__baz(bar, etc)".
wuffs_softfloat__arith__struct() = delete;
wuffs_softfloat__arith__struct(const wuffs_softfloat__arith__struct&) = delete;
wuffs_softfloat__arith__struct& operator=(
const wuffs_softfloat__arith__struct&) = delete;
#endif // defined(WUFFS_BASE__HAVE_EQ_DELETE) && !defined(WUFFS_IMPLEMENTATION)
#if !defined(WUFFS_IMPLEMENTATION)
// As above, the size of the struct is not part of the public API, and unless
// WUFFS_IMPLEMENTATION is #define'd, this struct type T should be heap
// allocated, not stack allocated. Its size is not intended to be known at
// compile time, but it is unfortunately divulged as a side effect of
// defining C++ convenience methods. Use "sizeof__T()", calling the function,
// instead of "sizeof T", invoking the operator. To make the two values
// different, so that passing the latter will be rejected by the initialize
// function, we add an arbitrary amount of dead weight.
uint8_t dead_weight[123000000]; // 123 MB.
#endif // !defined(WUFFS_IMPLEMENTATION)
inline wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
initialize(
size_t sizeof_star_self,
uint64_t wuffs_version,
uint32_t options) {
return wuffs_softfloat__arith__initialize(
this, sizeof_star_self, wuffs_version, options);
}
inline uint32_t
f64_is_nan(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_is_nan(this, a_a);
}
inline uint64_t
f64_neg(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_neg(this, a_a);
}
inline uint64_t
f64_abs(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_abs(this, a_a);
}
inline uint32_t
f64_cmp(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_cmp(this, a_a, a_b);
}
inline uint32_t
f64_eq(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_eq(this, a_a, a_b);
}
inline uint32_t
f64_ne(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_ne(this, a_a, a_b);
}
inline uint32_t
f64_lt(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_lt(this, a_a, a_b);
}
inline uint32_t
f64_le(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_le(this, a_a, a_b);
}
inline uint32_t
f64_gt(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_gt(this, a_a, a_b);
}
inline uint32_t
f64_ge(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_ge(this, a_a, a_b);
}
inline uint64_t
f64_min(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_min(this, a_a, a_b);
}
inline uint64_t
f64_max(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_max(this, a_a, a_b);
}
inline uint64_t
f64_add(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_add(this, a_a, a_b);
}
inline uint64_t
f64_sub(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_sub(this, a_a, a_b);
}
inline uint64_t
f64_mul(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_mul(this, a_a, a_b);
}
inline uint64_t
f64_div(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_div(this, a_a, a_b);
}
inline uint64_t
f64_fma(
uint64_t a_a,
uint64_t a_b,
uint64_t a_c) const {
return wuffs_softfloat__arith__f64_fma(this, a_a, a_b, a_c);
}
inline uint64_t
f64_from_u64(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_from_u64(this, a_a);
}
inline uint64_t
f64_from_u32(
uint32_t a_a) const {
return wuffs_softfloat__arith__f64_from_u32(this, a_a);
}
inline uint64_t
f64_from_i64(
int64_t a_a) const {
return wuffs_softfloat__arith__f64_from_i64(this, a_a);
}
inline uint64_t
f64_to_u64_trunc(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_to_u64_trunc(this, a_a);
}
inline uint32_t
f64_to_f32(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_to_f32(this, a_a);
}
inline uint64_t
f32_to_f64(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_to_f64(this, a_a);
}
inline uint32_t
f32_is_nan(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_is_nan(this, a_a);
}
inline uint32_t
f32_neg(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_neg(this, a_a);
}
inline uint32_t
f32_abs(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_abs(this, a_a);
}
inline uint32_t
f32_add(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_add(this, a_a, a_b);
}
inline uint32_t
f32_sub(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_sub(this, a_a, a_b);
}
inline uint32_t
f32_mul(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_mul(this, a_a, a_b);
}
inline uint32_t
f32_div(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_div(this, a_a, a_b);
}
inline uint32_t
f32_cmp(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_cmp(this, a_a, a_b);
}
inline uint32_t
f32_eq(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_eq(this, a_a, a_b);
}
inline uint32_t
f32_ne(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_ne(this, a_a, a_b);
}
inline uint32_t
f32_lt(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_lt(this, a_a, a_b);
}
inline uint32_t
f32_le(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_le(this, a_a, a_b);
}
inline uint32_t
f32_gt(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_gt(this, a_a, a_b);
}
inline uint32_t
f32_ge(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_ge(this, a_a, a_b);
}
inline uint32_t
f32_min(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_min(this, a_a, a_b);
}
inline uint32_t
f32_max(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_max(this, a_a, a_b);
}
inline uint32_t
f32_from_u32(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_from_u32(this, a_a);
}
inline uint32_t
f32_from_i32(
int32_t a_a) const {
return wuffs_softfloat__arith__f32_from_i32(this, a_a);
}
inline uint32_t
f32_from_u64(
uint64_t a_a) const {
return wuffs_softfloat__arith__f32_from_u64(this, a_a);
}
inline uint64_t
log2_u64(
uint64_t a_v) const {
return wuffs_softfloat__arith__log2_u64(this, a_v);
}
inline uint64_t
log2_f64(
uint64_t a_x) const {
return wuffs_softfloat__arith__log2_f64(this, a_x);
}
#endif // __cplusplus
}; // struct wuffs_softfloat__arith__struct
#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION)
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SOFTFLOAT) || defined(WUFFS_NONMONOLITHIC)
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BROTLI) || defined(WUFFS_NONMONOLITHIC)
// ---------------- Status Codes
@@ -8513,6 +9230,7 @@ extern const char wuffs_brotli__error__bad_transform[];
extern const char wuffs_brotli__error__bad_window_bits[];
extern const char wuffs_brotli__error__truncated_input[];
extern const char wuffs_brotli__error__unsupported_large_window[];
extern const char wuffs_brotli__error__unsupported_encoder_quality[];
// ---------------- Public Consts
@@ -8520,10 +9238,34 @@ extern const char wuffs_brotli__error__unsupported_large_window[];
#define WUFFS_BROTLI__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 16777758u
#define WUFFS_BROTLI__QUIRK_LARGE_WINDOW 716673024u
#define WUFFS_BROTLI__ENCODER_DST_HISTORY_RETAIN_LENGTH_MAX_INCL_WORST_CASE 0u
#define WUFFS_BROTLI__QUIRK_ENCODER_QUALITY 716673025u
#define WUFFS_BROTLI__QUIRK_LGWIN 716673026u
#define WUFFS_BROTLI__QUIRK_LGBLOCK 716673027u
#define WUFFS_BROTLI__QUIRK_MODE 716673028u
#define WUFFS_BROTLI__QUIRK_DISABLE_LITERAL_CONTEXT_MODELING 716673029u
#define WUFFS_BROTLI__QUIRK_SIZE_HINT 716673030u
#define WUFFS_BROTLI__QUIRK_NPOSTFIX 716673031u
#define WUFFS_BROTLI__QUIRK_NDIRECT 716673032u
#define WUFFS_BROTLI__QUIRK_STREAM_OFFSET 716673033u
// ---------------- Struct Declarations
typedef struct wuffs_brotli__decoder__struct wuffs_brotli__decoder;
typedef struct wuffs_brotli__encoder__struct wuffs_brotli__encoder;
#ifdef __cplusplus
extern "C" {
#endif
@@ -8546,6 +9288,16 @@ wuffs_brotli__decoder__initialize(
size_t
sizeof__wuffs_brotli__decoder(void);
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
wuffs_brotli__encoder__initialize(
wuffs_brotli__encoder* self,
size_t sizeof_star_self,
uint64_t wuffs_version,
uint32_t options);
size_t
sizeof__wuffs_brotli__encoder(void);
// ---------------- Allocs
// These functions allocate and initialize Wuffs structs. They return NULL if
@@ -8562,6 +9314,14 @@ wuffs_brotli__decoder__alloc_as__wuffs_base__io_transformer(void) {
return (wuffs_base__io_transformer*)(wuffs_brotli__decoder__alloc());
}
wuffs_brotli__encoder*
wuffs_brotli__encoder__alloc(void);
static inline wuffs_base__io_transformer*
wuffs_brotli__encoder__alloc_as__wuffs_base__io_transformer(void) {
return (wuffs_base__io_transformer*)(wuffs_brotli__encoder__alloc());
}
// ---------------- Upcasts
static inline wuffs_base__io_transformer*
@@ -8570,6 +9330,12 @@ wuffs_brotli__decoder__upcast_as__wuffs_base__io_transformer(
return (wuffs_base__io_transformer*)p;
}
static inline wuffs_base__io_transformer*
wuffs_brotli__encoder__upcast_as__wuffs_base__io_transformer(
wuffs_brotli__encoder* p) {
return (wuffs_base__io_transformer*)p;
}
// ---------------- Public Function Prototypes
WUFFS_BASE__GENERATED_C_CODE
@@ -8603,6 +9369,54 @@ wuffs_brotli__decoder__transform_io(
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_brotli__encoder__get_quirk(
const wuffs_brotli__encoder* self,
uint32_t a_key);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_brotli__encoder__set_quirk(
wuffs_brotli__encoder* self,
uint32_t a_key,
uint64_t a_value);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__optional_u63
wuffs_brotli__encoder__dst_history_retain_length(
const wuffs_brotli__encoder* self);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64
wuffs_brotli__encoder__workbuf_len(
const wuffs_brotli__encoder* self);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_brotli__encoder__max_compressed_size(
const wuffs_brotli__encoder* self,
uint64_t a_input_size);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_brotli__encoder__request_flush(
wuffs_brotli__encoder* self);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_brotli__encoder__request_emit_metadata(
wuffs_brotli__encoder* self,
uint64_t a_length);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_brotli__encoder__transform_io(
wuffs_brotli__encoder* self,
wuffs_base__io_buffer* a_dst,
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf);
#ifdef __cplusplus
} // extern "C"
#endif
@@ -8888,6 +9702,211 @@ struct wuffs_brotli__decoder__struct {
#endif // __cplusplus
}; // struct wuffs_brotli__decoder__struct
struct wuffs_brotli__encoder__struct {
// Do not access the private_impl's or private_data's fields directly. There
// is no API/ABI compatibility or safety guarantee if you do so. Instead, use
// the wuffs_foo__bar__baz functions.
//
// It is a struct, not a struct*, so that the outermost wuffs_foo__bar struct
// can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
wuffs_base__vtable vtable_for__wuffs_base__io_transformer;
wuffs_base__vtable null_vtable;
uint32_t f_p_mode;
uint32_t f_p_quality;
bool f_p_quality_set;
uint32_t f_p_lgwin;
bool f_p_lgwin_set;
uint32_t f_p_lgblock;
uint32_t f_p_disable_lcm;
uint32_t f_p_size_hint;
uint32_t f_p_large_window;
uint32_t f_p_npostfix;
uint32_t f_p_ndirect;
uint32_t f_p_stream_offset;
bool f_is_initialized;
uint32_t f_quality;
uint32_t f_lgwin;
uint32_t f_lgblock;
bool f_large_window;
uint32_t f_size_hint;
uint32_t f_stream_offset;
uint32_t f_distance_postfix_bits;
uint32_t f_num_direct_distance_codes;
uint32_t f_dist_cache[4];
uint32_t f_saved_dist_cache[4];
uint32_t f_flint;
uint32_t f_last_bytes;
uint32_t f_last_bytes_bits;
uint32_t f_stream_state;
uint32_t f_remaining_metadata_bytes;
bool f_out_in_tiny_buf;
uint64_t f_out_pos;
uint64_t f_out_len;
bool f_flush_requested;
bool f_metadata_requested;
uint32_t f_metadata_length;
uint64_t f_bw_ix;
uint64_t f_cmd_code_numbits;
uint32_t f_huffman_tree_size;
uint32_t f_num_commands;
uint32_t f_num_literals;
uint32_t p_transform_io;
} private_impl;
struct {
wuffs_softfloat__arith f_sf;
uint8_t f_tiny_buf[32];
uint8_t f_lit_depth[256];
uint16_t f_lit_bits[256];
uint8_t f_cmd_depth[128];
uint16_t f_cmd_bits[128];
uint32_t f_cmd_histo[128];
uint8_t f_cmd_code[520];
uint32_t f_histogram[256];
uint8_t f_tmp_depth[704];
uint16_t f_tmp_bits[64];
uint32_t f_tree_count[1024];
uint16_t f_tree_left[1024];
uint16_t f_tree_right[1024];
uint8_t f_huffman_tree[1024];
uint8_t f_huffman_tree_extra_bits[1024];
uint32_t f_cl_histo[32];
uint8_t f_cl_depth[32];
uint16_t f_cl_bits[32];
uint32_t f_hash_table[131072];
uint32_t f_command_buf[131072];
uint8_t f_literal_buf[131072];
struct {
uint32_t v_op;
bool v_is_last;
bool v_force_flush;
uint64_t v_storage_len;
uint32_t v_table_bits;
uint64_t v_mark;
uint64_t scratch;
} s_transform_io;
} private_data;
#ifdef __cplusplus
#if defined(WUFFS_BASE__HAVE_UNIQUE_PTR)
using unique_ptr = std::unique_ptr<wuffs_brotli__encoder, wuffs_unique_ptr_deleter>;
// On failure, the alloc_etc functions return nullptr. They don't throw.
static inline unique_ptr
alloc() {
return unique_ptr(wuffs_brotli__encoder__alloc());
}
static inline wuffs_base__io_transformer::unique_ptr
alloc_as__wuffs_base__io_transformer() {
return wuffs_base__io_transformer::unique_ptr(
wuffs_brotli__encoder__alloc_as__wuffs_base__io_transformer());
}
#endif // defined(WUFFS_BASE__HAVE_UNIQUE_PTR)
#if defined(WUFFS_BASE__HAVE_EQ_DELETE) && !defined(WUFFS_IMPLEMENTATION)
// Disallow constructing or copying an object via standard C++ mechanisms,
// e.g. the "new" operator, as this struct is intentionally opaque. Its total
// size and field layout is not part of the public, stable, memory-safe API.
// Use malloc or memcpy and the sizeof__wuffs_foo__bar function instead, and
// call wuffs_foo__bar__baz methods (which all take a "this"-like pointer as
// their first argument) rather than tweaking bar.private_impl.qux fields.
//
// In C, we can just leave wuffs_foo__bar as an incomplete type (unless
// WUFFS_IMPLEMENTATION is #define'd). In C++, we define a complete type in
// order to provide convenience methods. These forward on "this", so that you
// can write "bar->baz(etc)" instead of "wuffs_foo__bar__baz(bar, etc)".
wuffs_brotli__encoder__struct() = delete;
wuffs_brotli__encoder__struct(const wuffs_brotli__encoder__struct&) = delete;
wuffs_brotli__encoder__struct& operator=(
const wuffs_brotli__encoder__struct&) = delete;
#endif // defined(WUFFS_BASE__HAVE_EQ_DELETE) && !defined(WUFFS_IMPLEMENTATION)
#if !defined(WUFFS_IMPLEMENTATION)
// As above, the size of the struct is not part of the public API, and unless
// WUFFS_IMPLEMENTATION is #define'd, this struct type T should be heap
// allocated, not stack allocated. Its size is not intended to be known at
// compile time, but it is unfortunately divulged as a side effect of
// defining C++ convenience methods. Use "sizeof__T()", calling the function,
// instead of "sizeof T", invoking the operator. To make the two values
// different, so that passing the latter will be rejected by the initialize
// function, we add an arbitrary amount of dead weight.
uint8_t dead_weight[123000000]; // 123 MB.
#endif // !defined(WUFFS_IMPLEMENTATION)
inline wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
initialize(
size_t sizeof_star_self,
uint64_t wuffs_version,
uint32_t options) {
return wuffs_brotli__encoder__initialize(
this, sizeof_star_self, wuffs_version, options);
}
inline wuffs_base__io_transformer*
upcast_as__wuffs_base__io_transformer() {
return (wuffs_base__io_transformer*)this;
}
inline uint64_t
get_quirk(
uint32_t a_key) const {
return wuffs_brotli__encoder__get_quirk(this, a_key);
}
inline wuffs_base__status
set_quirk(
uint32_t a_key,
uint64_t a_value) {
return wuffs_brotli__encoder__set_quirk(this, a_key, a_value);
}
inline wuffs_base__optional_u63
dst_history_retain_length() const {
return wuffs_brotli__encoder__dst_history_retain_length(this);
}
inline wuffs_base__range_ii_u64
workbuf_len() const {
return wuffs_brotli__encoder__workbuf_len(this);
}
inline uint64_t
max_compressed_size(
uint64_t a_input_size) const {
return wuffs_brotli__encoder__max_compressed_size(this, a_input_size);
}
inline wuffs_base__status
request_flush() {
return wuffs_brotli__encoder__request_flush(this);
}
inline wuffs_base__status
request_emit_metadata(
uint64_t a_length) {
return wuffs_brotli__encoder__request_emit_metadata(this, a_length);
}
inline wuffs_base__status
transform_io(
wuffs_base__io_buffer* a_dst,
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf) {
return wuffs_brotli__encoder__transform_io(this, a_dst, a_src, a_workbuf);
}
#endif // __cplusplus
}; // struct wuffs_brotli__encoder__struct
#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION)
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BROTLI) || defined(WUFFS_NONMONOLITHIC)
@@ -15335,723 +16354,6 @@ struct wuffs_sha256__hasher__struct {
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SHA256) || defined(WUFFS_NONMONOLITHIC)
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SOFTFLOAT) || defined(WUFFS_NONMONOLITHIC)
// ---------------- Status Codes
// ---------------- Public Consts
// ---------------- Struct Declarations
typedef struct wuffs_softfloat__arith__struct wuffs_softfloat__arith;
#ifdef __cplusplus
extern "C" {
#endif
// ---------------- Public Initializer Prototypes
// For any given "wuffs_foo__bar* self", "wuffs_foo__bar__initialize(self,
// etc)" should be called before any other "wuffs_foo__bar__xxx(self, etc)".
//
// Pass sizeof(*self) and WUFFS_VERSION for sizeof_star_self and wuffs_version.
// Pass 0 (or some combination of WUFFS_INITIALIZE__XXX) for options.
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
wuffs_softfloat__arith__initialize(
wuffs_softfloat__arith* self,
size_t sizeof_star_self,
uint64_t wuffs_version,
uint32_t options);
size_t
sizeof__wuffs_softfloat__arith(void);
// ---------------- Allocs
// These functions allocate and initialize Wuffs structs. They return NULL if
// memory allocation fails. If they return non-NULL, there is no need to call
// wuffs_foo__bar__initialize, but the caller is responsible for eventually
// calling free on the returned pointer. That pointer is effectively a C++
// std::unique_ptr<T, wuffs_unique_ptr_deleter>.
wuffs_softfloat__arith*
wuffs_softfloat__arith__alloc(void);
// ---------------- Upcasts
// ---------------- Public Function Prototypes
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_is_nan(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_neg(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_abs(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_cmp(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_eq(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_ne(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_lt(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_le(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_gt(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_ge(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_min(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_max(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_add(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_sub(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_mul(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_div(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_fma(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_c);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_u64(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_u32(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_i64(
const wuffs_softfloat__arith* self,
int64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_to_u64_trunc(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_to_f32(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f32_to_f64(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_is_nan(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_neg(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_abs(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_add(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_sub(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_mul(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_div(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_cmp(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_eq(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_ne(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_lt(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_le(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_gt(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_ge(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_min(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_max(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_u32(
const wuffs_softfloat__arith* self,
uint32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_i32(
const wuffs_softfloat__arith* self,
int32_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_u64(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__log2_u64(
const wuffs_softfloat__arith* self,
uint64_t a_v);
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__log2_f64(
const wuffs_softfloat__arith* self,
uint64_t a_x);
#ifdef __cplusplus
} // extern "C"
#endif
// ---------------- Struct Definitions
// These structs' fields, and the sizeof them, are private implementation
// details that aren't guaranteed to be stable across Wuffs versions.
//
// See https://en.wikipedia.org/wiki/Opaque_pointer#C
#if defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION)
struct wuffs_softfloat__arith__struct {
// Do not access the private_impl's or private_data's fields directly. There
// is no API/ABI compatibility or safety guarantee if you do so. Instead, use
// the wuffs_foo__bar__baz functions.
//
// It is a struct, not a struct*, so that the outermost wuffs_foo__bar struct
// can be stack allocated when WUFFS_IMPLEMENTATION is defined.
struct {
uint32_t magic;
uint32_t active_coroutine;
wuffs_base__vtable null_vtable;
} private_impl;
#ifdef __cplusplus
#if defined(WUFFS_BASE__HAVE_UNIQUE_PTR)
using unique_ptr = std::unique_ptr<wuffs_softfloat__arith, wuffs_unique_ptr_deleter>;
// On failure, the alloc_etc functions return nullptr. They don't throw.
static inline unique_ptr
alloc() {
return unique_ptr(wuffs_softfloat__arith__alloc());
}
#endif // defined(WUFFS_BASE__HAVE_UNIQUE_PTR)
#if defined(WUFFS_BASE__HAVE_EQ_DELETE) && !defined(WUFFS_IMPLEMENTATION)
// Disallow constructing or copying an object via standard C++ mechanisms,
// e.g. the "new" operator, as this struct is intentionally opaque. Its total
// size and field layout is not part of the public, stable, memory-safe API.
// Use malloc or memcpy and the sizeof__wuffs_foo__bar function instead, and
// call wuffs_foo__bar__baz methods (which all take a "this"-like pointer as
// their first argument) rather than tweaking bar.private_impl.qux fields.
//
// In C, we can just leave wuffs_foo__bar as an incomplete type (unless
// WUFFS_IMPLEMENTATION is #define'd). In C++, we define a complete type in
// order to provide convenience methods. These forward on "this", so that you
// can write "bar->baz(etc)" instead of "wuffs_foo__bar__baz(bar, etc)".
wuffs_softfloat__arith__struct() = delete;
wuffs_softfloat__arith__struct(const wuffs_softfloat__arith__struct&) = delete;
wuffs_softfloat__arith__struct& operator=(
const wuffs_softfloat__arith__struct&) = delete;
#endif // defined(WUFFS_BASE__HAVE_EQ_DELETE) && !defined(WUFFS_IMPLEMENTATION)
#if !defined(WUFFS_IMPLEMENTATION)
// As above, the size of the struct is not part of the public API, and unless
// WUFFS_IMPLEMENTATION is #define'd, this struct type T should be heap
// allocated, not stack allocated. Its size is not intended to be known at
// compile time, but it is unfortunately divulged as a side effect of
// defining C++ convenience methods. Use "sizeof__T()", calling the function,
// instead of "sizeof T", invoking the operator. To make the two values
// different, so that passing the latter will be rejected by the initialize
// function, we add an arbitrary amount of dead weight.
uint8_t dead_weight[123000000]; // 123 MB.
#endif // !defined(WUFFS_IMPLEMENTATION)
inline wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
initialize(
size_t sizeof_star_self,
uint64_t wuffs_version,
uint32_t options) {
return wuffs_softfloat__arith__initialize(
this, sizeof_star_self, wuffs_version, options);
}
inline uint32_t
f64_is_nan(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_is_nan(this, a_a);
}
inline uint64_t
f64_neg(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_neg(this, a_a);
}
inline uint64_t
f64_abs(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_abs(this, a_a);
}
inline uint32_t
f64_cmp(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_cmp(this, a_a, a_b);
}
inline uint32_t
f64_eq(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_eq(this, a_a, a_b);
}
inline uint32_t
f64_ne(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_ne(this, a_a, a_b);
}
inline uint32_t
f64_lt(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_lt(this, a_a, a_b);
}
inline uint32_t
f64_le(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_le(this, a_a, a_b);
}
inline uint32_t
f64_gt(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_gt(this, a_a, a_b);
}
inline uint32_t
f64_ge(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_ge(this, a_a, a_b);
}
inline uint64_t
f64_min(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_min(this, a_a, a_b);
}
inline uint64_t
f64_max(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_max(this, a_a, a_b);
}
inline uint64_t
f64_add(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_add(this, a_a, a_b);
}
inline uint64_t
f64_sub(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_sub(this, a_a, a_b);
}
inline uint64_t
f64_mul(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_mul(this, a_a, a_b);
}
inline uint64_t
f64_div(
uint64_t a_a,
uint64_t a_b) const {
return wuffs_softfloat__arith__f64_div(this, a_a, a_b);
}
inline uint64_t
f64_fma(
uint64_t a_a,
uint64_t a_b,
uint64_t a_c) const {
return wuffs_softfloat__arith__f64_fma(this, a_a, a_b, a_c);
}
inline uint64_t
f64_from_u64(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_from_u64(this, a_a);
}
inline uint64_t
f64_from_u32(
uint32_t a_a) const {
return wuffs_softfloat__arith__f64_from_u32(this, a_a);
}
inline uint64_t
f64_from_i64(
int64_t a_a) const {
return wuffs_softfloat__arith__f64_from_i64(this, a_a);
}
inline uint64_t
f64_to_u64_trunc(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_to_u64_trunc(this, a_a);
}
inline uint32_t
f64_to_f32(
uint64_t a_a) const {
return wuffs_softfloat__arith__f64_to_f32(this, a_a);
}
inline uint64_t
f32_to_f64(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_to_f64(this, a_a);
}
inline uint32_t
f32_is_nan(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_is_nan(this, a_a);
}
inline uint32_t
f32_neg(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_neg(this, a_a);
}
inline uint32_t
f32_abs(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_abs(this, a_a);
}
inline uint32_t
f32_add(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_add(this, a_a, a_b);
}
inline uint32_t
f32_sub(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_sub(this, a_a, a_b);
}
inline uint32_t
f32_mul(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_mul(this, a_a, a_b);
}
inline uint32_t
f32_div(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_div(this, a_a, a_b);
}
inline uint32_t
f32_cmp(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_cmp(this, a_a, a_b);
}
inline uint32_t
f32_eq(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_eq(this, a_a, a_b);
}
inline uint32_t
f32_ne(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_ne(this, a_a, a_b);
}
inline uint32_t
f32_lt(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_lt(this, a_a, a_b);
}
inline uint32_t
f32_le(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_le(this, a_a, a_b);
}
inline uint32_t
f32_gt(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_gt(this, a_a, a_b);
}
inline uint32_t
f32_ge(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_ge(this, a_a, a_b);
}
inline uint32_t
f32_min(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_min(this, a_a, a_b);
}
inline uint32_t
f32_max(
uint32_t a_a,
uint32_t a_b) const {
return wuffs_softfloat__arith__f32_max(this, a_a, a_b);
}
inline uint32_t
f32_from_u32(
uint32_t a_a) const {
return wuffs_softfloat__arith__f32_from_u32(this, a_a);
}
inline uint32_t
f32_from_i32(
int32_t a_a) const {
return wuffs_softfloat__arith__f32_from_i32(this, a_a);
}
inline uint32_t
f32_from_u64(
uint64_t a_a) const {
return wuffs_softfloat__arith__f32_from_u64(this, a_a);
}
inline uint64_t
log2_u64(
uint64_t a_v) const {
return wuffs_softfloat__arith__log2_u64(this, a_v);
}
inline uint64_t
log2_f64(
uint64_t a_x) const {
return wuffs_softfloat__arith__log2_f64(this, a_x);
}
#endif // __cplusplus
}; // struct wuffs_softfloat__arith__struct
#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION)
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SOFTFLOAT) || defined(WUFFS_NONMONOLITHIC)
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__TARGA) || defined(WUFFS_NONMONOLITHIC)
// ---------------- Status Codes
@@ -40126,6 +40428,2058 @@ wuffs_bmp__decoder__process_masks(
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BMP)
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SOFTFLOAT)
// ---------------- Status Codes Implementations
// ---------------- Private Consts
#define WUFFS_SOFTFLOAT__F64_SIGN 9223372036854775808u
#define WUFFS_SOFTFLOAT__F64_ABS 9223372036854775807u
#define WUFFS_SOFTFLOAT__F64_INF 9218868437227405312u
#define WUFFS_SOFTFLOAT__F64_FRAC 4503599627370495u
#define WUFFS_SOFTFLOAT__F64_HIDDEN 4503599627370496u
#define WUFFS_SOFTFLOAT__F64_QUIET 2251799813685248u
#define WUFFS_SOFTFLOAT__F64_DEFAULT_NAN 18444492273895866368u
static const uint8_t
WUFFS_SOFTFLOAT__CLZ8[256] WUFFS_BASE__POTENTIALLY_UNUSED = {
8u, 7u, 6u, 6u, 5u, 5u, 5u, 5u,
4u, 4u, 4u, 4u, 4u, 4u, 4u, 4u,
3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u,
3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u,
2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u,
2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u,
2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u,
2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
};
#define WUFFS_SOFTFLOAT__F32_SIGN 2147483648u
#define WUFFS_SOFTFLOAT__F32_ABS 2147483647u
#define WUFFS_SOFTFLOAT__F32_INF 2139095040u
#define WUFFS_SOFTFLOAT__LOG2_INVLN2_HI 4609176140020449280u
#define WUFFS_SOFTFLOAT__LOG2_INVLN2_LO 4460540536611119616u
#define WUFFS_SOFTFLOAT__LOG2_A0 13828044577248609081u
#define WUFFS_SOFTFLOAT__LOG2_A1 4602334714382648510u
#define WUFFS_SOFTFLOAT__LOG2_A2 13823540977608962043u
#define WUFFS_SOFTFLOAT__LOG2_A3 4598869476581264456u
#define WUFFS_SOFTFLOAT__LOG2_A4 13821203878746165948u
#define WUFFS_SOFTFLOAT__LOG2_A5 4596594284514769198u
#define WUFFS_SOFTFLOAT__LOG2_B0 13828044577248609022u
#define WUFFS_SOFTFLOAT__LOG2_B1 4602334714382648311u
#define WUFFS_SOFTFLOAT__LOG2_B2 13823540977621236799u
#define WUFFS_SOFTFLOAT__LOG2_B3 4598869476596800484u
#define WUFFS_SOFTFLOAT__LOG2_B4 13821203151630798821u
#define WUFFS_SOFTFLOAT__LOG2_B5 4596593529635268406u
#define WUFFS_SOFTFLOAT__LOG2_B6 13819037345097492379u
#define WUFFS_SOFTFLOAT__LOG2_B7 4594943554432801610u
#define WUFFS_SOFTFLOAT__LOG2_B8 13817758221876344892u
#define WUFFS_SOFTFLOAT__LOG2_B9 4593868635039788646u
#define WUFFS_SOFTFLOAT__F64_ONE 4607182418800017408u
#define WUFFS_SOFTFLOAT__F64_NEG_ONE 13830554455654793216u
static const uint64_t
WUFFS_SOFTFLOAT__LOG2_TAB_INVC[64] WUFFS_BASE__POTENTIALLY_UNUSED = {
4609192499661090040u, 4609119721358544033u, 4609048552263439498u, 4608978935752977541u, 4608910827810035960u, 4608844174476640876u, 4608778932670517176u, 4608715056657886750u,
4608652505995016245u, 4608591236480216541u, 4608531212716768208u, 4608472393775796206u, 4608414747454330217u, 4608358236140386643u, 4608302825730857126u, 4608248488588498998u,
4608195190521789770u, 4608142902552467893u, 4608091596839446450u, 4608041245183688113u, 4607991821672542679u, 4607943301500489090u, 4607895658995436368u, 4607848870546001381u,
4607802913757468081u, 4607757767841591096u, 4607713409864934365u, 4607669821755832624u, 4607626980202848768u, 4607584869060826940u, 4607543467839055042u, 4607502757543314897u,
4607462725802730023u, 4607423351295589007u, 4607384620866568409u, 4607346516590018701u, 4607309024952925059u, 4607272131607993180u, 4607235821368544930u, 4607200080338058785u,
4607112595412526955u, 4606976147961868676u, 4606843801959665791u, 4606715378043149032u, 4606590705194581478u, 4606469619618625715u, 4606351967316207939u, 4606237606996243811u,
4606126401917471569u, 4606018221995330636u, 4605912946732701130u, 4605810462065308559u, 4605710654407004217u, 4605613423158833629u, 4605518667591971211u, 4605426298098548397u,
4605336222619703554u, 4605248357336792306u, 4605162622515894769u, 4605078941046797933u, 4604997241283328404u, 4604917450612805666u, 4604839505954084298u, 4604763342788034444u,
};
static const uint64_t
WUFFS_SOFTFLOAT__LOG2_TAB_LOGC[64] WUFFS_BASE__POTENTIALLY_UNUSED = {
13826342638637133824u, 13826196630396129280u, 13826052246236362752u, 13825768029042720768u, 13825485539995279360u, 13825206079124377600u, 13824929594987094016u, 13824656018996125696u,
13824385299287748608u, 13824117362061238272u, 13823852166444584960u, 13823589642092011520u, 13823329752470622208u, 13823072432079704064u, 13822817626379755520u, 13822565306322034688u,
13822315408407748608u, 13822067889127702528u, 13821822706514976768u, 13821579813106167808u, 13821131081098354688u, 13820654214105104384u, 13820181677119676416u, 13819713392057769984u,
13819249286753288192u, 13818789299422093312u, 13818333340582215680u, 13817881364992663552u, 13817433267667582976u, 13816934369010221056u, 13816053372001304576u, 13815179735723180032u,
13814313441755004928u, 13813454269690494976u, 13812602167107960832u, 13810973880287035392u, 13809297139126697984u, 13807231240355184640u, 13803931045004312576u, 13796532236635865088u,
4577625706679238656u, 4584977561914671104u, 4588127867468742656u, 4590199577454788608u, 4591728374053568512u, 4593235018675142656u, 4594195890746384384u, 4594927996701155328u,
4595649931110391808u, 4596361981185310720u, 4597064405355782144u, 4597757451025965056u, 4598308306428592128u, 4598645833741746176u, 4598979039133097984u, 4599308018491162624u,
4599632888650723328u, 4599953748864847872u, 4600270694594048000u, 4600583822264602624u, 4600893218024939520u, 4601198981460250624u, 4601501185019535360u, 4601799915281006592u,
};
// ---------------- Private Initializer Prototypes
// ---------------- Private Function Prototypes
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__clz64(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__shift_right_jam64(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint32_t a_dist);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__jam128_hi(
const wuffs_softfloat__arith* self,
uint64_t a_hi,
uint64_t a_lo,
uint32_t a_dist);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__jam128_lo(
const wuffs_softfloat__arith* self,
uint64_t a_hi,
uint64_t a_lo,
uint32_t a_dist);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__mul_hi64(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__round_pack_f64(
const wuffs_softfloat__arith* self,
uint64_t a_sign,
uint32_t a_exp,
uint64_t a_sig);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__norm_round_pack_f64(
const wuffs_softfloat__arith* self,
uint64_t a_sign,
uint32_t a_exp,
uint64_t a_sig);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__norm_sub_exp(
const wuffs_softfloat__arith* self,
uint64_t a_frac);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__norm_sub_sig(
const wuffs_softfloat__arith* self,
uint64_t a_frac);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_propagate_nan(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_add_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_sign);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_sub_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_sign);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__round_pack_f32(
const wuffs_softfloat__arith* self,
uint32_t a_sign,
uint32_t a_exp,
uint32_t a_sig);
// ---------------- VTables
// ---------------- Initializer Implementations
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
wuffs_softfloat__arith__initialize(
wuffs_softfloat__arith* self,
size_t sizeof_star_self,
uint64_t wuffs_version,
uint32_t options){
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (sizeof(*self) != sizeof_star_self) {
return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver);
}
if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) ||
(((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) {
return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version);
}
if ((options & WUFFS_INITIALIZE__ALREADY_ZEROED) != 0) {
// The whole point of this if-check is to detect an uninitialized *self.
// We disable the warning on GCC. Clang-5.0 does not have this warning.
#if !defined(__clang__) && defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
if (self->private_impl.magic != 0) {
return wuffs_base__make_status(wuffs_base__error__initialize_falsely_claimed_already_zeroed);
}
#if !defined(__clang__) && defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
} else {
if ((options & WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED) == 0) {
memset(self, 0, sizeof(*self));
options |= WUFFS_INITIALIZE__ALREADY_ZEROED;
} else {
memset(&(self->private_impl), 0, sizeof(self->private_impl));
}
}
self->private_impl.magic = WUFFS_BASE__MAGIC;
return wuffs_base__make_status(NULL);
}
wuffs_softfloat__arith*
wuffs_softfloat__arith__alloc(void) {
wuffs_softfloat__arith* x =
(wuffs_softfloat__arith*)(calloc(1, sizeof(wuffs_softfloat__arith)));
if (!x) {
return NULL;
}
if (wuffs_softfloat__arith__initialize(
x, sizeof(wuffs_softfloat__arith), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) {
free(x);
return NULL;
}
return x;
}
size_t
sizeof__wuffs_softfloat__arith(void) {
return sizeof(wuffs_softfloat__arith);
}
// ---------------- Function Implementations
// -------- func softfloat.arith.clz64
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__clz64(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
uint64_t v_x = 0;
uint32_t v_n32 = 0;
uint32_t v_n16 = 0;
uint32_t v_n8 = 0;
v_x = a_a;
if ((v_x >> 32u) == 0u) {
v_x = ((uint64_t)(v_x << 32u));
v_n32 = 32u;
}
if ((v_x >> 48u) == 0u) {
v_x = ((uint64_t)(v_x << 16u));
v_n16 = 16u;
}
if ((v_x >> 56u) == 0u) {
v_x = ((uint64_t)(v_x << 8u));
v_n8 = 8u;
}
return ((v_n32 +
v_n16 +
v_n8 +
(((uint32_t)(WUFFS_SOFTFLOAT__CLZ8[(v_x >> 56u)])) & 7u)) & 63u);
}
// -------- func softfloat.arith.shift_right_jam64
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__shift_right_jam64(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint32_t a_dist) {
uint32_t v_d = 0;
if (a_dist == 0u) {
return a_a;
} else if (a_dist < 64u) {
v_d = a_dist;
if (((uint64_t)(a_a << (64u - v_d))) != 0u) {
return ((a_a >> v_d) | 1u);
}
return (a_a >> v_d);
} else if (a_a != 0u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.jam128_hi
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__jam128_hi(
const wuffs_softfloat__arith* self,
uint64_t a_hi,
uint64_t a_lo,
uint32_t a_dist) {
if (a_dist < 64u) {
return (a_hi >> a_dist);
}
return 0u;
}
// -------- func softfloat.arith.jam128_lo
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__jam128_lo(
const wuffs_softfloat__arith* self,
uint64_t a_hi,
uint64_t a_lo,
uint32_t a_dist) {
uint32_t v_d = 0;
uint64_t v_sticky = 0;
if (a_dist == 0u) {
return a_lo;
} else if (a_dist < 64u) {
v_d = a_dist;
if (((uint64_t)(a_lo << (64u - v_d))) != 0u) {
v_sticky = 1u;
}
return (((uint64_t)(a_hi << (64u - v_d))) | (a_lo >> v_d) | v_sticky);
} else if (a_dist < 128u) {
v_d = (a_dist - 64u);
if (v_d > 0u) {
if ((((uint64_t)(a_hi << (64u - v_d))) | a_lo) != 0u) {
v_sticky = 1u;
}
} else if (a_lo != 0u) {
v_sticky = 1u;
}
return ((a_hi >> v_d) | v_sticky);
} else if ((a_hi | a_lo) != 0u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.mul_hi64
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__mul_hi64(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
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_p11 = 0;
uint64_t v_mid = 0;
v_a0 = (a_a & 4294967295u);
v_a1 = (a_a >> 32u);
v_b0 = (a_b & 4294967295u);
v_b1 = (a_b >> 32u);
v_p00 = (v_a0 * v_b0);
v_p01 = (v_a0 * v_b1);
v_p10 = (v_a1 * v_b0);
v_p11 = (v_a1 * v_b1);
v_mid = ((v_p00 >> 32u) + (v_p01 & 4294967295u) + (v_p10 & 4294967295u));
return ((uint64_t)(((uint64_t)(((uint64_t)(v_p11 + (v_p01 >> 32u))) + (v_p10 >> 32u))) + (v_mid >> 32u)));
}
// -------- func softfloat.arith.round_pack_f64
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__round_pack_f64(
const wuffs_softfloat__arith* self,
uint64_t a_sign,
uint32_t a_exp,
uint64_t a_sig) {
uint32_t v_e = 0;
uint64_t v_s = 0;
uint64_t v_round_bits = 0;
v_e = a_exp;
v_s = a_sig;
v_round_bits = (v_s & 1023u);
if (v_e >= 2147483648u) {
v_s = wuffs_softfloat__arith__shift_right_jam64(self, v_s, ((uint32_t)(0u - v_e)));
v_e = 0u;
v_round_bits = (v_s & 1023u);
} else if (v_e >= 2045u) {
if ((v_e > 2045u) || (v_s >= 9223372036854775296u)) {
return (a_sign | 9218868437227405312u);
}
}
v_s = (((uint64_t)(v_s + 512u)) >> 10u);
if (v_round_bits == 512u) {
v_s = (v_s & 18446744073709551614u);
}
if (v_s == 0u) {
v_e = 0u;
}
return (a_sign | ((uint64_t)(((uint64_t)(((uint64_t)(v_e)) << 52u)) + v_s)));
}
// -------- func softfloat.arith.norm_round_pack_f64
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__norm_round_pack_f64(
const wuffs_softfloat__arith* self,
uint64_t a_sign,
uint32_t a_exp,
uint64_t a_sig) {
uint32_t v_c = 0;
uint32_t v_shift = 0;
uint32_t v_e = 0;
if (a_sig == 0u) {
return a_sign;
}
v_c = wuffs_softfloat__arith__clz64(self, a_sig);
if (v_c == 0u) {
return wuffs_softfloat__arith__round_pack_f64(self, a_sign, ((uint32_t)(a_exp + 1u)), ((a_sig >> 1u) | (a_sig & 1u)));
}
v_shift = (v_c - 1u);
v_e = ((uint32_t)(a_exp - v_shift));
if ((v_shift >= 10u) && (v_e < 2045u)) {
return (a_sign | ((uint64_t)(((uint64_t)(((uint64_t)(v_e)) << 52u)) + ((uint64_t)(a_sig << (v_shift - 10u))))));
}
return wuffs_softfloat__arith__round_pack_f64(self, a_sign, v_e, ((uint64_t)(a_sig << v_shift)));
}
// -------- func softfloat.arith.norm_sub_exp
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__norm_sub_exp(
const wuffs_softfloat__arith* self,
uint64_t a_frac) {
return ((uint32_t)(12u - wuffs_softfloat__arith__clz64(self, a_frac)));
}
// -------- func softfloat.arith.norm_sub_sig
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__norm_sub_sig(
const wuffs_softfloat__arith* self,
uint64_t a_frac) {
return ((uint64_t)(a_frac << (((uint32_t)(wuffs_softfloat__arith__clz64(self, a_frac) - 11u)) & 63u)));
}
// -------- func softfloat.arith.f64_propagate_nan
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_propagate_nan(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if ((a_a & 9223372036854775807u) > 9218868437227405312u) {
return (a_a | 2251799813685248u);
}
return (a_b | 2251799813685248u);
}
// -------- func softfloat.arith.f64_is_nan
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_is_nan(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if ((a_a & 9223372036854775807u) > 9218868437227405312u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_neg
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_neg(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return (a_a ^ 9223372036854775808u);
}
// -------- func softfloat.arith.f64_abs
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_abs(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return (a_a & 9223372036854775807u);
}
// -------- func softfloat.arith.f64_cmp
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_cmp(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_ka = 0;
uint64_t v_kb = 0;
if (((a_a & 9223372036854775807u) > 9218868437227405312u) || ((a_b & 9223372036854775807u) > 9218868437227405312u)) {
return 3u;
} else if (((a_a | a_b) & 9223372036854775807u) == 0u) {
return 1u;
}
v_ka = (a_a | 9223372036854775808u);
if ((a_a >> 63u) != 0u) {
v_ka = (18446744073709551615u ^ a_a);
}
v_kb = (a_b | 9223372036854775808u);
if ((a_b >> 63u) != 0u) {
v_kb = (18446744073709551615u ^ a_b);
}
if (v_ka < v_kb) {
return 0u;
} else if (v_ka == v_kb) {
return 1u;
}
return 2u;
}
// -------- func softfloat.arith.f64_eq
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_eq(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 1u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_ne
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_ne(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 1u) {
return 0u;
}
return 1u;
}
// -------- func softfloat.arith.f64_lt
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_lt(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 0u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_le
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_le(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) <= 1u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_gt
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_gt(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 2u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_ge
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_ge(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_c = 0;
v_c = wuffs_softfloat__arith__f64_cmp(self, a_a, a_b);
if ((v_c == 1u) || (v_c == 2u)) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_min
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_min(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 0u) {
return a_a;
}
return a_b;
}
// -------- func softfloat.arith.f64_max
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_max(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 2u) {
return a_a;
}
return a_b;
}
// -------- func softfloat.arith.f64_add
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_add(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (((a_a ^ a_b) >> 63u) == 0u) {
return wuffs_softfloat__arith__f64_add_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
}
return wuffs_softfloat__arith__f64_sub_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
}
// -------- func softfloat.arith.f64_sub
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_sub(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (((a_a ^ a_b) >> 63u) == 0u) {
return wuffs_softfloat__arith__f64_sub_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
}
return wuffs_softfloat__arith__f64_add_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
}
// -------- func softfloat.arith.f64_add_mags
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_add_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_sign) {
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_sig_z = 0;
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
if (v_exp_a == v_exp_b) {
if (v_exp_a == 0u) {
return (a_sign | ((uint64_t)((a_a & 9223372036854775807u) + v_sig_b)));
} else if (v_exp_a == 2047u) {
if ((v_sig_a | v_sig_b) != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return a_a;
}
v_exp_z = v_exp_a;
v_sig_z = ((uint64_t)(((uint64_t)(((uint64_t)(9007199254740992u + v_sig_a)) + v_sig_b)) << 9u));
return wuffs_softfloat__arith__round_pack_f64(self, a_sign, v_exp_z, v_sig_z);
}
v_sig_a = ((uint64_t)(v_sig_a << 9u));
v_sig_b = ((uint64_t)(v_sig_b << 9u));
if (v_exp_a < v_exp_b) {
if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return (a_sign | 9218868437227405312u);
}
v_exp_z = v_exp_b;
if (v_exp_a != 0u) {
v_sig_a = ((uint64_t)(v_sig_a + 2305843009213693952u));
} else {
v_sig_a = ((uint64_t)(v_sig_a << 1u));
}
v_sig_a = wuffs_softfloat__arith__shift_right_jam64(self, v_sig_a, ((uint32_t)(v_exp_b - v_exp_a)));
} else {
if (v_exp_a == 2047u) {
if (v_sig_a != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return (a_sign | 9218868437227405312u);
}
v_exp_z = v_exp_a;
if (v_exp_b != 0u) {
v_sig_b = ((uint64_t)(v_sig_b + 2305843009213693952u));
} else {
v_sig_b = ((uint64_t)(v_sig_b << 1u));
}
v_sig_b = wuffs_softfloat__arith__shift_right_jam64(self, v_sig_b, ((uint32_t)(v_exp_a - v_exp_b)));
}
v_sig_z = ((uint64_t)(((uint64_t)(2305843009213693952u + v_sig_a)) + v_sig_b));
if (v_sig_z < 4611686018427387904u) {
v_exp_z = ((uint32_t)(v_exp_z - 1u));
v_sig_z = ((uint64_t)(v_sig_z << 1u));
}
return wuffs_softfloat__arith__round_pack_f64(self, a_sign, v_exp_z, v_sig_z);
}
// -------- func softfloat.arith.f64_sub_mags
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_sub_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_sign) {
uint64_t v_sign_z = 0;
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_sig_z = 0;
uint32_t v_shift = 0;
v_sign_z = a_sign;
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
if (v_exp_a == v_exp_b) {
if (v_exp_a == 2047u) {
if ((v_sig_a | v_sig_b) != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return 18444492273895866368u;
} else if (v_sig_a == v_sig_b) {
return 0u;
}
if (v_exp_a != 0u) {
v_exp_a = ((uint32_t)(v_exp_a - 1u));
}
if (v_sig_a < v_sig_b) {
v_sign_z ^= 9223372036854775808u;
v_sig_z = ((uint64_t)(v_sig_b - v_sig_a));
} else {
v_sig_z = ((uint64_t)(v_sig_a - v_sig_b));
}
v_shift = (((uint32_t)(wuffs_softfloat__arith__clz64(self, v_sig_z) - 11u)) & 63u);
if (v_shift > v_exp_a) {
v_shift = (v_exp_a & 63u);
v_exp_z = 0u;
} else {
v_exp_z = ((uint32_t)(v_exp_a - v_shift));
}
return (v_sign_z | ((uint64_t)(((uint64_t)(((uint64_t)(v_exp_z)) << 52u)) + ((uint64_t)(v_sig_z << v_shift)))));
}
v_sig_a = ((uint64_t)(v_sig_a << 10u));
v_sig_b = ((uint64_t)(v_sig_b << 10u));
if (v_exp_a < v_exp_b) {
v_sign_z ^= 9223372036854775808u;
if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return (v_sign_z | 9218868437227405312u);
}
if (v_exp_a != 0u) {
v_sig_a = ((uint64_t)(v_sig_a + 4611686018427387904u));
} else {
v_sig_a = ((uint64_t)(v_sig_a + v_sig_a));
}
v_sig_a = wuffs_softfloat__arith__shift_right_jam64(self, v_sig_a, ((uint32_t)(v_exp_b - v_exp_a)));
v_sig_b = (v_sig_b | 4611686018427387904u);
v_exp_z = v_exp_b;
v_sig_z = ((uint64_t)(v_sig_b - v_sig_a));
} else {
if (v_exp_a == 2047u) {
if (v_sig_a != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return (v_sign_z | 9218868437227405312u);
}
if (v_exp_b != 0u) {
v_sig_b = ((uint64_t)(v_sig_b + 4611686018427387904u));
} else {
v_sig_b = ((uint64_t)(v_sig_b + v_sig_b));
}
v_sig_b = wuffs_softfloat__arith__shift_right_jam64(self, v_sig_b, ((uint32_t)(v_exp_a - v_exp_b)));
v_sig_a = (v_sig_a | 4611686018427387904u);
v_exp_z = v_exp_a;
v_sig_z = ((uint64_t)(v_sig_a - v_sig_b));
}
return wuffs_softfloat__arith__norm_round_pack_f64(self, v_sign_z, ((uint32_t)(v_exp_z - 1u)), v_sig_z);
}
// -------- func softfloat.arith.f64_mul
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_mul(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_sign_z = 0;
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_sig_z = 0;
v_sign_z = ((a_a ^ a_b) & 9223372036854775808u);
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
if (v_exp_a == 2047u) {
if ((v_sig_a != 0u) || ((v_exp_b == 2047u) && (v_sig_b != 0u))) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
} else if ((a_b & 9223372036854775807u) == 0u) {
return 18444492273895866368u;
}
return (v_sign_z | 9218868437227405312u);
} else if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
} else if ((a_a & 9223372036854775807u) == 0u) {
return 18444492273895866368u;
}
return (v_sign_z | 9218868437227405312u);
}
if (v_exp_a == 0u) {
if (v_sig_a == 0u) {
return v_sign_z;
}
v_exp_a = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_a);
v_sig_a = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_a);
}
if (v_exp_b == 0u) {
if (v_sig_b == 0u) {
return v_sign_z;
}
v_exp_b = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_b);
v_sig_b = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_b);
}
v_exp_z = ((uint32_t)(((uint32_t)(v_exp_a + v_exp_b)) - 1023u));
v_sig_a = ((uint64_t)((v_sig_a | 4503599627370496u) << 10u));
v_sig_b = ((uint64_t)((v_sig_b | 4503599627370496u) << 11u));
v_sig_z = wuffs_softfloat__arith__mul_hi64(self, v_sig_a, v_sig_b);
if (((uint64_t)(v_sig_a * v_sig_b)) != 0u) {
v_sig_z |= 1u;
}
if (v_sig_z < 4611686018427387904u) {
v_exp_z = ((uint32_t)(v_exp_z - 1u));
v_sig_z = ((uint64_t)(v_sig_z << 1u));
}
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_z, v_exp_z, v_sig_z);
}
// -------- func softfloat.arith.f64_div
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_div(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_sign_z = 0;
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_q = 0;
uint64_t v_r = 0;
uint32_t v_i = 0;
v_sign_z = ((a_a ^ a_b) & 9223372036854775808u);
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
if (v_exp_a == 2047u) {
if (v_sig_a != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
} else if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return 18444492273895866368u;
}
return (v_sign_z | 9218868437227405312u);
} else if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return v_sign_z;
}
if (v_exp_b == 0u) {
if (v_sig_b == 0u) {
if ((a_a & 9223372036854775807u) == 0u) {
return 18444492273895866368u;
}
return (v_sign_z | 9218868437227405312u);
}
v_exp_b = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_b);
v_sig_b = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_b);
}
if (v_exp_a == 0u) {
if (v_sig_a == 0u) {
return v_sign_z;
}
v_exp_a = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_a);
v_sig_a = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_a);
}
v_exp_z = ((uint32_t)(((uint32_t)(v_exp_a - v_exp_b)) + 1022u));
v_sig_a = ((v_sig_a & 4503599627370495u) | 4503599627370496u);
v_sig_b = ((v_sig_b & 4503599627370495u) | 4503599627370496u);
if (v_sig_a < v_sig_b) {
v_exp_z = ((uint32_t)(v_exp_z - 1u));
v_sig_a = ((uint64_t)(v_sig_a << 1u));
}
v_q = 1u;
v_r = ((uint64_t)(v_sig_a - v_sig_b));
v_i = 0u;
while (v_i < 5u) {
v_r = ((uint64_t)(v_r << 11u));
if (v_sig_b > 0u) {
v_q = (((uint64_t)(v_q << 11u)) | (v_r / v_sig_b));
v_r = (v_r % v_sig_b);
}
v_i += 1u;
}
v_q = ((uint64_t)(v_q << 7u));
if (v_r != 0u) {
v_q |= 1u;
}
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_z, v_exp_z, v_q);
}
// -------- func softfloat.arith.f64_fma
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_fma(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_c) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_sign_p = 0;
uint64_t v_sign_c = 0;
uint64_t v_sign_z = 0;
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_c = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_sig_c = 0;
uint64_t v_p_hi = 0;
uint64_t v_p_lo = 0;
uint64_t v_c_hi = 0;
uint64_t v_c_lo = 0;
uint64_t v_z_hi = 0;
uint64_t v_z_lo = 0;
uint64_t v_tmp = 0;
uint32_t v_d = 0;
uint32_t v_top = 0;
if (((a_a & 9223372036854775807u) > 9218868437227405312u) || ((a_b & 9223372036854775807u) > 9218868437227405312u)) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
} else if ((a_c & 9223372036854775807u) > 9218868437227405312u) {
return (a_c | 2251799813685248u);
}
v_sign_p = ((a_a ^ a_b) & 9223372036854775808u);
v_sign_c = (a_c & 9223372036854775808u);
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_exp_c = ((uint32_t)(((a_c >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
v_sig_c = (a_c & 4503599627370495u);
if ((v_exp_a == 2047u) || (v_exp_b == 2047u)) {
if (((a_a & 9223372036854775807u) == 0u) || ((a_b & 9223372036854775807u) == 0u)) {
return 18444492273895866368u;
} else if ((v_exp_c == 2047u) && (v_sign_c != v_sign_p)) {
return 18444492273895866368u;
}
return (v_sign_p | 9218868437227405312u);
} else if (v_exp_c == 2047u) {
return a_c;
}
if (((a_a & 9223372036854775807u) == 0u) || ((a_b & 9223372036854775807u) == 0u)) {
if ((a_c & 9223372036854775807u) == 0u) {
if (v_sign_p == v_sign_c) {
return a_c;
}
return 0u;
}
return a_c;
}
if (v_exp_a == 0u) {
v_exp_a = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_a);
v_sig_a = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_a);
}
if (v_exp_b == 0u) {
v_exp_b = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_b);
v_sig_b = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_b);
}
v_sig_a = ((v_sig_a & 4503599627370495u) | 4503599627370496u);
v_sig_b = ((v_sig_b & 4503599627370495u) | 4503599627370496u);
v_p_hi = wuffs_softfloat__arith__mul_hi64(self, v_sig_a, v_sig_b);
v_p_lo = ((uint64_t)(v_sig_a * v_sig_b));
v_p_hi = (((uint64_t)(v_p_hi << 20u)) | (v_p_lo >> 44u));
v_p_lo = ((uint64_t)(v_p_lo << 20u));
if ((a_c & 9223372036854775807u) == 0u) {
v_top = (127u - wuffs_softfloat__arith__clz64(self, v_p_hi));
v_exp_z = ((uint32_t)(((uint32_t)(((uint32_t)(v_exp_a + v_exp_b)) + v_top)) - 1148u));
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_p, v_exp_z, wuffs_softfloat__arith__jam128_lo(self, v_p_hi, v_p_lo, ((uint32_t)(v_top - 62u))));
}
if (v_exp_c == 0u) {
v_exp_c = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_c);
v_sig_c = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_c);
}
v_sig_c = ((v_sig_c & 4503599627370495u) | 4503599627370496u);
v_c_hi = ((uint64_t)(v_sig_c << 9u));
v_c_lo = 0u;
v_d = ((uint32_t)(((uint32_t)(((uint32_t)(v_exp_a + v_exp_b)) - v_exp_c)) - 1022u));
if (v_d < 2147483648u) {
v_tmp = wuffs_softfloat__arith__jam128_hi(self, v_c_hi, v_c_lo, v_d);
v_c_lo = wuffs_softfloat__arith__jam128_lo(self, v_c_hi, v_c_lo, v_d);
v_c_hi = v_tmp;
v_exp_z = ((uint32_t)(((uint32_t)(v_exp_a + v_exp_b)) - 1148u));
} else {
v_d = ((uint32_t)(0u - v_d));
v_tmp = wuffs_softfloat__arith__jam128_hi(self, v_p_hi, v_p_lo, v_d);
v_p_lo = wuffs_softfloat__arith__jam128_lo(self, v_p_hi, v_p_lo, v_d);
v_p_hi = v_tmp;
v_exp_z = ((uint32_t)(v_exp_c - 126u));
}
if (v_sign_p == v_sign_c) {
v_sign_z = v_sign_p;
v_z_lo = ((uint64_t)(v_p_lo + v_c_lo));
v_z_hi = ((uint64_t)(v_p_hi + v_c_hi));
if (v_z_lo < v_p_lo) {
v_z_hi = ((uint64_t)(v_z_hi + 1u));
}
} else {
if ((v_p_hi > v_c_hi) || ((v_p_hi == v_c_hi) && (v_p_lo >= v_c_lo))) {
v_sign_z = v_sign_p;
v_z_lo = ((uint64_t)(v_p_lo - v_c_lo));
v_z_hi = ((uint64_t)(v_p_hi - v_c_hi));
if (v_p_lo < v_c_lo) {
v_z_hi = ((uint64_t)(v_z_hi - 1u));
}
} else {
v_sign_z = v_sign_c;
v_z_lo = ((uint64_t)(v_c_lo - v_p_lo));
v_z_hi = ((uint64_t)(v_c_hi - v_p_hi));
if (v_c_lo < v_p_lo) {
v_z_hi = ((uint64_t)(v_z_hi - 1u));
}
}
if ((v_z_hi | v_z_lo) == 0u) {
return 0u;
}
}
if (v_z_hi != 0u) {
v_top = (127u - wuffs_softfloat__arith__clz64(self, v_z_hi));
} else {
v_top = (63u - wuffs_softfloat__arith__clz64(self, v_z_lo));
}
v_exp_z = ((uint32_t)(v_exp_z + v_top));
if (v_top >= 62u) {
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_z, v_exp_z, wuffs_softfloat__arith__jam128_lo(self, v_z_hi, v_z_lo, (v_top - 62u)));
}
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_z, v_exp_z, ((uint64_t)(v_z_lo << (62u - v_top))));
}
// -------- func softfloat.arith.f64_from_u64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_u64(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__norm_round_pack_f64(self, 0u, 1084u, a_a);
}
// -------- func softfloat.arith.f64_from_u32
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_u32(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__norm_round_pack_f64(self, 0u, 1084u, ((uint64_t)(a_a)));
}
// -------- func softfloat.arith.f64_from_i64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_i64(
const wuffs_softfloat__arith* self,
int64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_m = 0;
if (a_a >= 0u) {
return wuffs_softfloat__arith__norm_round_pack_f64(self, 0u, 1084u, ((uint64_t)(a_a)));
}
v_m = (((uint64_t)((0u - (a_a + 1u)))) + 1u);
return wuffs_softfloat__arith__norm_round_pack_f64(self, 9223372036854775808u, 1084u, v_m);
}
// -------- func softfloat.arith.f64_to_u64_trunc
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_to_u64_trunc(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_exp = 0;
uint64_t v_sig = 0;
if (a_a >= 9223372036854775808u) {
return 0u;
} else if (a_a > 9218868437227405312u) {
return 0u;
}
v_exp = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_sig = ((a_a & 4503599627370495u) | 4503599627370496u);
if (v_exp < 1023u) {
return 0u;
} else if (v_exp >= 1087u) {
return 18446744073709551615u;
} else if (v_exp >= 1075u) {
return ((uint64_t)(v_sig << ((v_exp - 1075u) & 63u)));
}
return (v_sig >> ((1075u - v_exp) & 63u));
}
// -------- func softfloat.arith.f64_to_f32
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_to_f32(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_sign = 0;
uint32_t v_exp = 0;
uint64_t v_frac = 0;
uint32_t v_sig = 0;
v_sign = (((uint32_t)((a_a >> 32u))) & 2147483648u);
v_exp = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_frac = (a_a & 4503599627370495u);
if (v_exp == 2047u) {
if (v_frac != 0u) {
return (v_sign | 2143289344u | ((uint32_t)((v_frac >> 29u))));
}
return (v_sign | 2139095040u);
}
v_sig = ((uint32_t)((wuffs_softfloat__arith__shift_right_jam64(self, v_frac, 22u) & 1073741823u)));
if ((v_exp == 0u) && (v_sig == 0u)) {
return v_sign;
}
return wuffs_softfloat__arith__round_pack_f32(self, v_sign, ((uint32_t)(v_exp - 897u)), (v_sig | 1073741824u));
}
// -------- func softfloat.arith.round_pack_f32
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__round_pack_f32(
const wuffs_softfloat__arith* self,
uint32_t a_sign,
uint32_t a_exp,
uint32_t a_sig) {
uint32_t v_e = 0;
uint32_t v_s = 0;
uint32_t v_round_bits = 0;
v_e = a_exp;
v_s = a_sig;
v_round_bits = (v_s & 127u);
if (v_e >= 2147483648u) {
v_s = ((uint32_t)(wuffs_softfloat__arith__shift_right_jam64(self, ((uint64_t)(v_s)), ((uint32_t)(0u - v_e)))));
v_e = 0u;
v_round_bits = (v_s & 127u);
} else if (v_e >= 253u) {
if ((v_e > 253u) || (v_s >= 2147483584u)) {
return (a_sign | 2139095040u);
}
}
v_s = (((uint32_t)(v_s + 64u)) >> 7u);
if (v_round_bits == 64u) {
v_s = (v_s & 4294967294u);
}
if (v_s == 0u) {
v_e = 0u;
}
return (a_sign | ((uint32_t)(((uint32_t)(v_e << 23u)) + v_s)));
}
// -------- func softfloat.arith.f32_to_f64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f32_to_f64(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_sign = 0;
uint32_t v_exp = 0;
uint64_t v_frac = 0;
uint32_t v_top = 0;
v_sign = ((((uint64_t)(a_a)) >> 31u) << 63u);
v_exp = ((a_a >> 23u) & 255u);
v_frac = ((uint64_t)((a_a & 8388607u)));
if (v_exp == 255u) {
if (v_frac != 0u) {
return (v_sign | 9221120237041090560u | (v_frac << 29u));
}
return (v_sign | 9218868437227405312u);
} else if (v_exp == 0u) {
if (v_frac == 0u) {
return v_sign;
}
v_top = (63u - wuffs_softfloat__arith__clz64(self, v_frac));
if (v_top > 22u) {
return 0u;
}
return (v_sign | (((uint64_t)((v_top + 874u))) << 52u) | (((uint64_t)(v_frac << (52u - v_top))) & 4503599627370495u));
}
return (v_sign | (((uint64_t)((v_exp + 896u))) << 52u) | (v_frac << 29u));
}
// -------- func softfloat.arith.f32_is_nan
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_is_nan(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if ((a_a & 2147483647u) > 2139095040u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_neg
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_neg(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return (a_a ^ 2147483648u);
}
// -------- func softfloat.arith.f32_abs
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_abs(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return (a_a & 2147483647u);
}
// -------- func softfloat.arith.f32_add
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_add(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_add(self, wuffs_softfloat__arith__f32_to_f64(self, a_a), wuffs_softfloat__arith__f32_to_f64(self, a_b)));
}
// -------- func softfloat.arith.f32_sub
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_sub(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_sub(self, wuffs_softfloat__arith__f32_to_f64(self, a_a), wuffs_softfloat__arith__f32_to_f64(self, a_b)));
}
// -------- func softfloat.arith.f32_mul
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_mul(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_mul(self, wuffs_softfloat__arith__f32_to_f64(self, a_a), wuffs_softfloat__arith__f32_to_f64(self, a_b)));
}
// -------- func softfloat.arith.f32_div
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_div(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_div(self, wuffs_softfloat__arith__f32_to_f64(self, a_a), wuffs_softfloat__arith__f32_to_f64(self, a_b)));
}
// -------- func softfloat.arith.f32_cmp
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_cmp(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_ka = 0;
uint32_t v_kb = 0;
if (((a_a & 2147483647u) > 2139095040u) || ((a_b & 2147483647u) > 2139095040u)) {
return 3u;
} else if (((a_a | a_b) & 2147483647u) == 0u) {
return 1u;
}
v_ka = (a_a | 2147483648u);
if ((a_a >> 31u) != 0u) {
v_ka = (4294967295u ^ a_a);
}
v_kb = (a_b | 2147483648u);
if ((a_b >> 31u) != 0u) {
v_kb = (4294967295u ^ a_b);
}
if (v_ka < v_kb) {
return 0u;
} else if (v_ka == v_kb) {
return 1u;
}
return 2u;
}
// -------- func softfloat.arith.f32_eq
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_eq(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 1u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_ne
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_ne(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 1u) {
return 0u;
}
return 1u;
}
// -------- func softfloat.arith.f32_lt
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_lt(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 0u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_le
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_le(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) <= 1u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_gt
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_gt(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 2u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_ge
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_ge(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_c = 0;
v_c = wuffs_softfloat__arith__f32_cmp(self, a_a, a_b);
if ((v_c == 1u) || (v_c == 2u)) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_min
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_min(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 0u) {
return a_a;
}
return a_b;
}
// -------- func softfloat.arith.f32_max
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_max(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 2u) {
return a_a;
}
return a_b;
}
// -------- func softfloat.arith.f32_from_u32
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_u32(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_from_u32(self, a_a));
}
// -------- func softfloat.arith.f32_from_i32
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_i32(
const wuffs_softfloat__arith* self,
int32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_from_i64(self, ((int64_t)(a_a))));
}
// -------- func softfloat.arith.f32_from_u64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_u64(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_top = 0;
uint64_t v_x = 0;
v_x = a_a;
if (v_x >= 9007199254740992u) {
v_top = (63u - wuffs_softfloat__arith__clz64(self, v_x));
if (v_top >= 53u) {
v_x = ((uint64_t)(wuffs_softfloat__arith__shift_right_jam64(self, v_x, (v_top - 52u)) << (v_top - 52u)));
}
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_from_u64(self, v_x));
}
// -------- func softfloat.arith.log2_u64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__log2_u64(
const wuffs_softfloat__arith* self,
uint64_t a_v) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__log2_f64(self, wuffs_softfloat__arith__f64_from_u64(self, a_v));
}
// -------- func softfloat.arith.log2_f64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__log2_f64(
const wuffs_softfloat__arith* self,
uint64_t a_x) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_ix = 0;
uint64_t v_top = 0;
uint64_t v_tmp = 0;
uint64_t v_i = 0;
uint64_t v_k = 0;
uint64_t v_kd = 0;
uint64_t v_invc = 0;
uint64_t v_logc = 0;
uint64_t v_z = 0;
uint64_t v_r = 0;
uint64_t v_r2 = 0;
uint64_t v_r4 = 0;
uint64_t v_t1 = 0;
uint64_t v_t2 = 0;
uint64_t v_t3 = 0;
uint64_t v_hi = 0;
uint64_t v_lo = 0;
uint64_t v_p = 0;
uint64_t v_y = 0;
v_ix = a_x;
if (((uint64_t)(v_ix - 4606800540372828160u)) < 581272283906048u) {
if (v_ix == 4607182418800017408u) {
return 0u;
}
v_r = wuffs_softfloat__arith__f64_sub(self, v_ix, 4607182418800017408u);
v_hi = wuffs_softfloat__arith__f64_mul(self, 4609176140020449280u, v_r);
v_r2 = wuffs_softfloat__arith__f64_mul(self, v_r, v_r);
v_lo = wuffs_softfloat__arith__f64_fma(self, 4609176140020449280u, v_r, wuffs_softfloat__arith__f64_neg(self, v_hi));
v_r4 = wuffs_softfloat__arith__f64_mul(self, v_r2, v_r2);
v_p = wuffs_softfloat__arith__f64_fma(self, v_r, 4602334714382648311u, 13828044577248609022u);
v_lo = wuffs_softfloat__arith__f64_fma(self, v_r, 4460540536611119616u, v_lo);
v_y = wuffs_softfloat__arith__f64_fma(self, v_p, v_r2, v_hi);
v_lo = wuffs_softfloat__arith__f64_add(self, v_lo, wuffs_softfloat__arith__f64_fma(self, v_p, v_r2, wuffs_softfloat__arith__f64_sub(self, v_hi, v_y)));
v_t1 = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_r, 4596593529635268406u, 13821203151630798821u), v_r2, wuffs_softfloat__arith__f64_fma(self, v_r, 4598869476596800484u, 13823540977621236799u));
v_t2 = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_r, 4593868635039788646u, 13817758221876344892u), v_r2, wuffs_softfloat__arith__f64_fma(self, v_r, 4594943554432801610u, 13819037345097492379u));
v_t3 = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_t2, v_r4, v_t1), v_r4, v_lo);
return wuffs_softfloat__arith__f64_add(self, v_y, v_t3);
}
v_top = (v_ix >> 48u);
if (((uint64_t)(v_top - 16u)) >= 32736u) {
if ((v_ix & 9223372036854775807u) == 0u) {
return 18442240474082181120u;
} else if (v_ix == 9218868437227405312u) {
return v_ix;
} else if (((v_top & 32768u) != 0u) || ((v_top & 32752u) == 32752u)) {
if ((v_ix & 9223372036854775807u) > 9218868437227405312u) {
return (v_ix | 2251799813685248u);
}
return 18444492273895866368u;
}
v_ix = ((uint64_t)(wuffs_softfloat__arith__f64_mul(self, v_ix, 4841369599423283200u) - 234187180623265792u));
}
v_tmp = ((uint64_t)(v_ix - 4604367669032910848u));
v_i = ((v_tmp >> 46u) & 63u);
v_k = (v_tmp >> 52u);
if (v_k >= 2048u) {
v_kd = wuffs_softfloat__arith__f64_neg(self, wuffs_softfloat__arith__f64_from_u64(self, (4096u - v_k)));
} else {
v_kd = wuffs_softfloat__arith__f64_from_u64(self, v_k);
}
v_z = ((uint64_t)(v_ix - (v_tmp & 18442240474082181120u)));
v_invc = WUFFS_SOFTFLOAT__LOG2_TAB_INVC[v_i];
v_logc = WUFFS_SOFTFLOAT__LOG2_TAB_LOGC[v_i];
v_t3 = wuffs_softfloat__arith__f64_add(self, v_kd, v_logc);
v_r = wuffs_softfloat__arith__f64_fma(self, v_z, v_invc, 13830554455654793216u);
v_t1 = wuffs_softfloat__arith__f64_mul(self, 4609176140020449280u, v_r);
v_t2 = wuffs_softfloat__arith__f64_fma(self, v_r, 4460540536611119616u, wuffs_softfloat__arith__f64_fma(self, 4609176140020449280u, v_r, wuffs_softfloat__arith__f64_neg(self, v_t1)));
v_hi = wuffs_softfloat__arith__f64_add(self, v_t1, v_t3);
v_lo = wuffs_softfloat__arith__f64_add(self, wuffs_softfloat__arith__f64_add(self, wuffs_softfloat__arith__f64_sub(self, v_t3, v_hi), v_t1), v_t2);
v_r2 = wuffs_softfloat__arith__f64_mul(self, v_r, v_r);
v_r4 = wuffs_softfloat__arith__f64_mul(self, v_r2, v_r2);
v_p = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_r, 4598869476581264456u, 13823540977608962043u), v_r2, wuffs_softfloat__arith__f64_fma(self, v_r, 4602334714382648510u, 13828044577248609081u));
v_p = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_r, 4596594284514769198u, 13821203878746165948u), v_r4, v_p);
v_y = wuffs_softfloat__arith__f64_fma(self, v_r2, v_p, v_lo);
return wuffs_softfloat__arith__f64_add(self, v_y, v_hi);
}
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SOFTFLOAT)
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BROTLI)
// ---------------- Status Codes Implementations
@@ -40152,9 +42506,560 @@ const char wuffs_brotli__error__unsupported_large_window[] = "#brotli: unsupport
const char wuffs_brotli__error__internal_error_inconsistent_bit_reader[] = "#brotli: internal error: inconsistent bit reader";
const char wuffs_brotli__error__internal_error_inconsistent_huffman_table[] = "#brotli: internal error: inconsistent Huffman table";
const char wuffs_brotli__error__internal_error_inconsistent_ring_buffer[] = "#brotli: internal error: inconsistent ring buffer";
const char wuffs_brotli__error__unsupported_encoder_quality[] = "#brotli: unsupported encoder quality";
const char wuffs_brotli__error__internal_error_inconsistent_encoder_state[] = "#brotli: internal error: inconsistent encoder state";
// ---------------- Private Consts
static const uint64_t
WUFFS_BROTLI__LOG2_TABLE[256] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 0u, 4607182418800017408u, 4609816855639490560u, 4611686018427387904u, 4612410935892508672u, 4613003237115559936u, 4613504020101726208u,
4613937818241073152u, 4614320455266861056u, 4614662735706193920u, 4614972366341013504u, 4615255036929245184u, 4615515068207988736u, 4615755819915411456u, 4615979954394365952u,
4616189618054758400u, 4616288092528050176u, 4616380936836087808u, 4616468759790485504u, 4616552076787318784u, 4616631328060735488u, 4616706892104728576u, 4616779096410554368u,
4616848227130408960u, 4616914535519879168u, 4616978243306651648u, 4617039545911738368u, 4617098618891927552u, 4617155619013525504u, 4617210685862969344u, 4617263947752407040u,
4617315517961601024u, 4617365501180379136u, 4617413992434892800u, 4617461077624487936u, 4617506836742930432u, 4617551341730922496u, 4617594659697328128u, 4617636852382302208u,
4617677976694161408u, 4617718085783126016u, 4617757227967578112u, 4617795449418416128u, 4617832792011571200u, 4617869295475490816u, 4617904996317396992u, 4617939929433899008u,
4617974127037251584u, 4618007619729096704u, 4618040435426721792u, 4618072601510543360u, 4618104143213494272u, 4618135083621023744u, 4618165445818580992u, 4618195250744131584u,
4618224518798770176u, 4618253268772978688u, 4618281518920368128u, 4618309285883936768u, 4618336585769811968u, 4618363435220992000u, 4618389847659249664u, 4618415837580099584u,
4618441417868443648u, 4618466601946054656u, 4618491401087221760u, 4618515827639975936u, 4618539892341735424u, 4618563605393047552u, 4618586977531330560u, 4618610017883389952u,
4618632736649773056u, 4618655141346672640u, 4618677241637765120u, 4618699045039243264u, 4618720559604170752u, 4618741792848740352u, 4618762752289144832u, 4618783444367835136u,
4618803876601004032u, 4618824054894231552u, 4618843985689968640u, 4618863674893795328u, 4618883127874420736u, 4618902351074295808u, 4618921349325258752u, 4618940127996018688u,
4618958691918413824u, 4618977045924282368u, 4618995195382333440u, 4619013144050663424u, 4619030896224239616u, 4619048456734900224u, 4619065829340741632u, 4619083018336731136u,
4619100026944094208u, 4619116859457798144u, 4619133519635939328u, 4619150010162872320u, 4619166335333564416u, 4619182498369241088u, 4619198501417385984u, 4619214348772966400u,
4619230043120336896u, 4619245586606981120u, 4619260983527866368u, 4619276235493605376u, 4619291345725423616u, 4619306316370804736u, 4619321150650974208u, 4619335850713415680u,
4619350418705612800u, 4619364857311920128u, 4619379168679821312u, 4619393354956800000u, 4619407418827210752u, 4619421361364795392u, 4619435185790779392u, 4619448893178904576u,
4619462485676654592u, 4619475965968384000u, 4619489335127834624u, 4619502594765619200u, 4619515747566092288u, 4619528794066124800u, 4619541737486942208u, 4619554577828544512u,
4619567317775286272u, 4619579958400909312u, 4619592501852897280u, 4619604948668121088u, 4619617300994064384u, 4619629560441339904u, 4619641727546818560u, 4619653804457984000u,
4619665792248578048u, 4619677691992342528u, 4619689505299890176u, 4619701233781833728u, 4619712877438173184u, 4619724438416392192u, 4619735917790232576u, 4619747316633436160u,
4619758636556615680u, 4619769877559771136u, 4619781041253515264u, 4619792128711589888u, 4619803141544607744u, 4619814079752568832u, 4619824944946085888u, 4619835737662029824u,
4619846459511013376u, 4619857110493036544u, 4619867692755582976u, 4619878206298652672u, 4619888652195987456u, 4619899030984458240u, 4619909344274677760u, 4619919592603516928u,
4619929776507846656u, 4619939897061408768u, 4619949954801074176u, 4619959950800584704u, 4619969885596811264u, 4619979759726624768u, 4619989574800637952u, 4619999330281979904u,
4620009027781263360u, 4620018667835359232u, 4620028250981138432u, 4620037777755471872u, 4620047249232101376u, 4620056665411026944u, 4620066027902861312u, 4620075336170733568u,
4620084591825256448u, 4620093794866429952u, 4620102945831124992u, 4620112045793083392u, 4620121095289176064u, 4620130094319403008u, 4620139043957506048u, 4620147944203485184u,
4620156796131082240u, 4620165600277168128u, 4620174356641742848u, 4620183066298548224u, 4620191729247584256u, 4620200346562592768u, 4620208918243573760u, 4620217444827398144u,
4620225926850936832u, 4620234364851060736u, 4620242759364640768u, 4620251110928547840u, 4620259419542781952u, 4620267685744214016u, 4620275910069714944u, 4620284093593026560u,
4620292235240407040u, 4620300336622469120u, 4620308398276083712u, 4620316419664379904u, 4620324401324228608u, 4620332344329371648u, 4620340248679809024u, 4620348114912411648u,
4620355943027179520u, 4620363733560983552u, 4620371486513823744u, 4620379203496312832u, 4620386883434708992u, 4620394527402754048u, 4620402135400448000u, 4620409707964661760u,
4620417245632266240u, 4620424748403261440u, 4620432216277647360u, 4620439650329165824u, 4620447050557816832u, 4620454416963600384u, 4620461750620258304u, 4620469050990919680u,
4620476318612455424u, 4620483554021736448u, 4620490757218762752u, 4620497928740405248u, 4620505068586663936u, 4620512177294409728u, 4620519254863642624u, 4620526301831233536u,
4620533318734053376u, 4620540305035231232u, 4620547261271638016u, 4620554187980144640u, 4620561085697622016u, 4620567953887199232u, 4620574793085747200u, 4620581603830136832u,
4620588385583497216u, 4620595139956441088u, 4620601865875226624u, 4620608563876724736u, 4620615235034677248u, 4620621878275342336u, 4620628494672461824u, 4620635084226035712u,
4620641647472934912u, 4620648183876288512u, 4620654693972967424u, 4620661178836713472u, 4620667637393784832u, 4620674070181052416u, 4620680477735387136u, 4620686860056788992u,
};
#define WUFFS_BROTLI__DEFAULT_COMMAND_CODE_NUM_BITS 448u
static const uint8_t
WUFFS_BROTLI__DEFAULT_COMMAND_DEPTHS[128] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 4u, 4u, 5u, 6u, 6u, 7u, 7u,
7u, 7u, 7u, 8u, 8u, 8u, 8u, 8u,
0u, 0u, 0u, 4u, 4u, 4u, 4u, 4u,
5u, 5u, 6u, 6u, 6u, 6u, 7u, 7u,
7u, 7u, 10u, 10u, 10u, 10u, 10u, 10u,
0u, 4u, 4u, 5u, 5u, 5u, 6u, 6u,
7u, 8u, 8u, 9u, 10u, 10u, 10u, 10u,
10u, 10u, 10u, 10u, 10u, 10u, 10u, 10u,
5u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
6u, 6u, 6u, 6u, 6u, 6u, 5u, 5u,
5u, 5u, 5u, 5u, 4u, 4u, 4u, 4u,
4u, 4u, 4u, 5u, 5u, 5u, 5u, 5u,
5u, 6u, 6u, 7u, 7u, 7u, 8u, 10u,
12u, 12u, 12u, 12u, 12u, 12u, 12u, 12u,
12u, 12u, 12u, 12u, 0u, 0u, 0u, 0u,
};
static const uint16_t
WUFFS_BROTLI__DEFAULT_COMMAND_BITS[128] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 0u, 8u, 9u, 3u, 35u, 7u, 71u,
39u, 103u, 23u, 47u, 175u, 111u, 239u, 31u,
0u, 0u, 0u, 4u, 12u, 2u, 10u, 6u,
13u, 29u, 11u, 43u, 27u, 59u, 87u, 55u,
15u, 79u, 319u, 831u, 191u, 703u, 447u, 959u,
0u, 14u, 1u, 25u, 5u, 21u, 19u, 51u,
119u, 159u, 95u, 223u, 479u, 991u, 63u, 575u,
127u, 639u, 383u, 895u, 255u, 767u, 511u, 1023u,
14u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
27u, 59u, 7u, 39u, 23u, 55u, 30u, 1u,
17u, 9u, 25u, 5u, 0u, 8u, 4u, 12u,
2u, 10u, 6u, 21u, 13u, 29u, 3u, 19u,
11u, 15u, 47u, 31u, 95u, 63u, 127u, 255u,
767u, 2815u, 1791u, 3839u, 511u, 2559u, 1535u, 3583u,
1023u, 3071u, 2047u, 4095u, 0u, 0u, 0u, 0u,
};
static const uint8_t
WUFFS_BROTLI__DEFAULT_COMMAND_CODE[57] WUFFS_BASE__POTENTIALLY_UNUSED = {
255u, 119u, 213u, 191u, 231u, 222u, 234u, 158u,
81u, 93u, 222u, 198u, 112u, 87u, 188u, 88u,
88u, 88u, 216u, 216u, 88u, 213u, 203u, 140u,
234u, 224u, 195u, 135u, 31u, 131u, 193u, 96u,
28u, 103u, 178u, 170u, 6u, 131u, 193u, 96u,
48u, 24u, 204u, 161u, 206u, 136u, 84u, 148u,
70u, 225u, 176u, 208u, 78u, 178u, 247u, 4u,
0u,
};
static const uint32_t
WUFFS_BROTLI__CMD_HISTO_SEED[128] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u,
};
static const uint32_t
WUFFS_BROTLI__TWO_PASS_NUM_EXTRA_BITS[128] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
2u, 2u, 3u, 3u, 4u, 4u, 5u, 5u,
6u, 7u, 8u, 9u, 10u, 12u, 14u, 24u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
1u, 1u, 2u, 2u, 3u, 3u, 4u, 4u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
1u, 1u, 2u, 2u, 3u, 3u, 4u, 4u,
5u, 5u, 6u, 7u, 8u, 9u, 10u, 24u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
1u, 1u, 2u, 2u, 3u, 3u, 4u, 4u,
5u, 5u, 6u, 6u, 7u, 7u, 8u, 8u,
9u, 9u, 10u, 10u, 11u, 11u, 12u, 12u,
13u, 13u, 14u, 14u, 15u, 15u, 16u, 16u,
17u, 17u, 18u, 18u, 19u, 19u, 20u, 20u,
21u, 21u, 22u, 22u, 23u, 23u, 24u, 24u,
};
static const uint32_t
WUFFS_BROTLI__TWO_PASS_INSERT_OFFSET[24] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 1u, 2u, 3u, 4u, 5u, 6u, 8u,
10u, 14u, 18u, 26u, 34u, 50u, 66u, 98u,
130u, 194u, 322u, 578u, 1090u, 2114u, 6210u, 22594u,
};
static const uint32_t
WUFFS_BROTLI__SHELL_GAPS[6] WUFFS_BASE__POTENTIALLY_UNUSED = {
132u, 57u, 23u, 10u, 4u, 1u,
};
static const uint32_t
WUFFS_BROTLI__REVERSE_BITS_LUT[16] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 8u, 4u, 12u, 2u, 10u, 6u, 14u,
1u, 9u, 5u, 13u, 3u, 11u, 7u, 15u,
};
#define WUFFS_BROTLI__STATIC_CODE_LENGTH_CODE 1096648316244u
static const uint32_t
WUFFS_BROTLI__CODE_LENGTH_DEPTH[18] WUFFS_BASE__POTENTIALLY_UNUSED = {
4u, 4u, 4u, 4u, 4u, 4u, 4u, 4u,
4u, 4u, 4u, 4u, 4u, 5u, 5u, 0u,
4u, 4u,
};
static const uint64_t
WUFFS_BROTLI__CODE_LENGTH_BITS[18] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 8u, 4u, 12u, 2u, 10u, 6u, 14u,
1u, 9u, 5u, 13u, 3u, 15u, 31u, 0u,
11u, 7u,
};
static const uint64_t
WUFFS_BROTLI__ZERO_REPS_BITS[704] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 0u, 0u, 7u, 23u, 39u, 55u, 71u,
87u, 103u, 119u, 1904u, 2951u, 4999u, 7047u, 9095u,
11143u, 13191u, 15239u, 919u, 2967u, 5015u, 7063u, 9111u,
11159u, 13207u, 15255u, 935u, 2983u, 5031u, 7079u, 9127u,
11175u, 13223u, 15271u, 951u, 2999u, 5047u, 7095u, 9143u,
11191u, 13239u, 15287u, 967u, 3015u, 5063u, 7111u, 9159u,
11207u, 13255u, 15303u, 983u, 3031u, 5079u, 7127u, 9175u,
11223u, 13271u, 15319u, 999u, 3047u, 5095u, 7143u, 9191u,
11239u, 13287u, 15335u, 1015u, 3063u, 5111u, 7159u, 9207u,
11255u, 13303u, 15351u, 115591u, 377735u, 639879u, 902023u, 1164167u,
1426311u, 1688455u, 1950599u, 117639u, 379783u, 641927u, 904071u, 1166215u,
1428359u, 1690503u, 1952647u, 119687u, 381831u, 643975u, 906119u, 1168263u,
1430407u, 1692551u, 1954695u, 121735u, 383879u, 646023u, 908167u, 1170311u,
1432455u, 1694599u, 1956743u, 123783u, 385927u, 648071u, 910215u, 1172359u,
1434503u, 1696647u, 1958791u, 125831u, 387975u, 650119u, 912263u, 1174407u,
1436551u, 1698695u, 1960839u, 127879u, 390023u, 652167u, 914311u, 1176455u,
1438599u, 1700743u, 1962887u, 129927u, 392071u, 654215u, 916359u, 1178503u,
1440647u, 1702791u, 1964935u, 115607u, 377751u, 639895u, 902039u, 1164183u,
1426327u, 1688471u, 1950615u, 117655u, 379799u, 641943u, 904087u, 1166231u,
1428375u, 1690519u, 1952663u, 119703u, 381847u, 643991u, 906135u, 1168279u,
1430423u, 1692567u, 1954711u, 121751u, 383895u, 646039u, 908183u, 1170327u,
1432471u, 1694615u, 1956759u, 123799u, 385943u, 648087u, 910231u, 1172375u,
1434519u, 1696663u, 1958807u, 125847u, 387991u, 650135u, 912279u, 1174423u,
1436567u, 1698711u, 1960855u, 127895u, 390039u, 652183u, 914327u, 1176471u,
1438615u, 1700759u, 1962903u, 129943u, 392087u, 654231u, 916375u, 1178519u,
1440663u, 1702807u, 1964951u, 115623u, 377767u, 639911u, 902055u, 1164199u,
1426343u, 1688487u, 1950631u, 117671u, 379815u, 641959u, 904103u, 1166247u,
1428391u, 1690535u, 1952679u, 119719u, 381863u, 644007u, 906151u, 1168295u,
1430439u, 1692583u, 1954727u, 121767u, 383911u, 646055u, 908199u, 1170343u,
1432487u, 1694631u, 1956775u, 123815u, 385959u, 648103u, 910247u, 1172391u,
1434535u, 1696679u, 1958823u, 125863u, 388007u, 650151u, 912295u, 1174439u,
1436583u, 1698727u, 1960871u, 127911u, 390055u, 652199u, 914343u, 1176487u,
1438631u, 1700775u, 1962919u, 129959u, 392103u, 654247u, 916391u, 1178535u,
1440679u, 1702823u, 1964967u, 115639u, 377783u, 639927u, 902071u, 1164215u,
1426359u, 1688503u, 1950647u, 117687u, 379831u, 641975u, 904119u, 1166263u,
1428407u, 1690551u, 1952695u, 119735u, 381879u, 644023u, 906167u, 1168311u,
1430455u, 1692599u, 1954743u, 121783u, 383927u, 646071u, 908215u, 1170359u,
1432503u, 1694647u, 1956791u, 123831u, 385975u, 648119u, 910263u, 1172407u,
1434551u, 1696695u, 1958839u, 125879u, 388023u, 650167u, 912311u, 1174455u,
1436599u, 1698743u, 1960887u, 127927u, 390071u, 652215u, 914359u, 1176503u,
1438647u, 1700791u, 1962935u, 129975u, 392119u, 654263u, 916407u, 1178551u,
1440695u, 1702839u, 1964983u, 115655u, 377799u, 639943u, 902087u, 1164231u,
1426375u, 1688519u, 1950663u, 117703u, 379847u, 641991u, 904135u, 1166279u,
1428423u, 1690567u, 1952711u, 119751u, 381895u, 644039u, 906183u, 1168327u,
1430471u, 1692615u, 1954759u, 121799u, 383943u, 646087u, 908231u, 1170375u,
1432519u, 1694663u, 1956807u, 123847u, 385991u, 648135u, 910279u, 1172423u,
1434567u, 1696711u, 1958855u, 125895u, 388039u, 650183u, 912327u, 1174471u,
1436615u, 1698759u, 1960903u, 127943u, 390087u, 652231u, 914375u, 1176519u,
1438663u, 1700807u, 1962951u, 129991u, 392135u, 654279u, 916423u, 1178567u,
1440711u, 1702855u, 1964999u, 115671u, 377815u, 639959u, 902103u, 1164247u,
1426391u, 1688535u, 1950679u, 117719u, 379863u, 642007u, 904151u, 1166295u,
1428439u, 1690583u, 1952727u, 119767u, 381911u, 644055u, 906199u, 1168343u,
1430487u, 1692631u, 1954775u, 121815u, 383959u, 646103u, 908247u, 1170391u,
1432535u, 1694679u, 1956823u, 123863u, 386007u, 648151u, 910295u, 1172439u,
1434583u, 1696727u, 1958871u, 125911u, 388055u, 650199u, 912343u, 1174487u,
1436631u, 1698775u, 1960919u, 127959u, 390103u, 652247u, 914391u, 1176535u,
1438679u, 1700823u, 1962967u, 130007u, 392151u, 654295u, 916439u, 1178583u,
1440727u, 1702871u, 1965015u, 115687u, 377831u, 639975u, 902119u, 1164263u,
1426407u, 1688551u, 1950695u, 117735u, 379879u, 642023u, 904167u, 1166311u,
1428455u, 1690599u, 1952743u, 119783u, 381927u, 644071u, 906215u, 1168359u,
1430503u, 1692647u, 1954791u, 121831u, 383975u, 646119u, 908263u, 1170407u,
1432551u, 1694695u, 1956839u, 123879u, 386023u, 648167u, 910311u, 1172455u,
1434599u, 1696743u, 1958887u, 125927u, 388071u, 650215u, 912359u, 1174503u,
1436647u, 1698791u, 1960935u, 127975u, 390119u, 652263u, 914407u, 1176551u,
1438695u, 1700839u, 1962983u, 130023u, 392167u, 654311u, 916455u, 1178599u,
1440743u, 1702887u, 1965031u, 115703u, 377847u, 639991u, 902135u, 1164279u,
1426423u, 1688567u, 1950711u, 117751u, 379895u, 642039u, 904183u, 1166327u,
1428471u, 1690615u, 1952759u, 119799u, 381943u, 644087u, 906231u, 1168375u,
1430519u, 1692663u, 1954807u, 121847u, 383991u, 646135u, 908279u, 1170423u,
1432567u, 1694711u, 1956855u, 123895u, 386039u, 648183u, 910327u, 1172471u,
1434615u, 1696759u, 1958903u, 125943u, 388087u, 650231u, 912375u, 1174519u,
1436663u, 1698807u, 1960951u, 127991u, 390135u, 652279u, 914423u, 1176567u,
1438711u, 1700855u, 1962999u, 130039u, 392183u, 654327u, 916471u, 1178615u,
1440759u, 1702903u, 1965047u, 14795655u, 48350087u, 81904519u, 115458951u, 149013383u,
182567815u, 216122247u, 249676679u, 15057799u, 48612231u, 82166663u, 115721095u, 149275527u,
182829959u, 216384391u, 249938823u, 15319943u, 48874375u, 82428807u, 115983239u, 149537671u,
183092103u, 216646535u, 250200967u, 15582087u, 49136519u, 82690951u, 116245383u, 149799815u,
183354247u, 216908679u, 250463111u, 15844231u, 49398663u, 82953095u, 116507527u, 150061959u,
183616391u, 217170823u, 250725255u, 16106375u, 49660807u, 83215239u, 116769671u, 150324103u,
183878535u, 217432967u, 250987399u, 16368519u, 49922951u, 83477383u, 117031815u, 150586247u,
184140679u, 217695111u, 251249543u, 16630663u, 50185095u, 83739527u, 117293959u, 150848391u,
184402823u, 217957255u, 251511687u, 14797703u, 48352135u, 81906567u, 115460999u, 149015431u,
182569863u, 216124295u, 249678727u, 15059847u, 48614279u, 82168711u, 115723143u, 149277575u,
182832007u, 216386439u, 249940871u, 15321991u, 48876423u, 82430855u, 115985287u, 149539719u,
183094151u, 216648583u, 250203015u, 15584135u, 49138567u, 82692999u, 116247431u, 149801863u,
183356295u, 216910727u, 250465159u, 15846279u, 49400711u, 82955143u, 116509575u, 150064007u,
183618439u, 217172871u, 250727303u, 16108423u, 49662855u, 83217287u, 116771719u, 150326151u,
183880583u, 217435015u, 250989447u, 16370567u, 49924999u, 83479431u, 117033863u, 150588295u,
};
static const uint32_t
WUFFS_BROTLI__ZERO_REPS_DEPTH[704] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 4u, 8u, 7u, 7u, 7u, 7u, 7u,
7u, 7u, 7u, 11u, 14u, 14u, 14u, 14u,
14u, 14u, 14u, 14u, 14u, 14u, 14u, 14u,
14u, 14u, 14u, 14u, 14u, 14u, 14u, 14u,
14u, 14u, 14u, 14u, 14u, 14u, 14u, 14u,
14u, 14u, 14u, 14u, 14u, 14u, 14u, 14u,
14u, 14u, 14u, 14u, 14u, 14u, 14u, 14u,
14u, 14u, 14u, 14u, 14u, 14u, 14u, 14u,
14u, 14u, 14u, 14u, 14u, 14u, 14u, 14u,
14u, 14u, 14u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 21u, 21u, 21u, 21u, 21u,
21u, 21u, 21u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
28u, 28u, 28u, 28u, 28u, 28u, 28u, 28u,
};
static const uint64_t
WUFFS_BROTLI__NON_ZERO_REPS_BITS[704] WUFFS_BASE__POTENTIALLY_UNUSED = {
11u, 27u, 43u, 59u, 715u, 1739u, 2763u, 3787u,
731u, 1755u, 2779u, 3803u, 747u, 1771u, 2795u, 3819u,
763u, 1787u, 2811u, 3835u, 45771u, 111307u, 176843u, 242379u,
46795u, 112331u, 177867u, 243403u, 47819u, 113355u, 178891u, 244427u,
48843u, 114379u, 179915u, 245451u, 45787u, 111323u, 176859u, 242395u,
46811u, 112347u, 177883u, 243419u, 47835u, 113371u, 178907u, 244443u,
48859u, 114395u, 179931u, 245467u, 45803u, 111339u, 176875u, 242411u,
46827u, 112363u, 177899u, 243435u, 47851u, 113387u, 178923u, 244459u,
48875u, 114411u, 179947u, 245483u, 45819u, 111355u, 176891u, 242427u,
46843u, 112379u, 177915u, 243451u, 47867u, 113403u, 178939u, 244475u,
48891u, 114427u, 179963u, 245499u, 2929355u, 7123659u, 11317963u, 15512267u,
2994891u, 7189195u, 11383499u, 15577803u, 3060427u, 7254731u, 11449035u, 15643339u,
3125963u, 7320267u, 11514571u, 15708875u, 2930379u, 7124683u, 11318987u, 15513291u,
2995915u, 7190219u, 11384523u, 15578827u, 3061451u, 7255755u, 11450059u, 15644363u,
3126987u, 7321291u, 11515595u, 15709899u, 2931403u, 7125707u, 11320011u, 15514315u,
2996939u, 7191243u, 11385547u, 15579851u, 3062475u, 7256779u, 11451083u, 15645387u,
3128011u, 7322315u, 11516619u, 15710923u, 2932427u, 7126731u, 11321035u, 15515339u,
2997963u, 7192267u, 11386571u, 15580875u, 3063499u, 7257803u, 11452107u, 15646411u,
3129035u, 7323339u, 11517643u, 15711947u, 2929371u, 7123675u, 11317979u, 15512283u,
2994907u, 7189211u, 11383515u, 15577819u, 3060443u, 7254747u, 11449051u, 15643355u,
3125979u, 7320283u, 11514587u, 15708891u, 2930395u, 7124699u, 11319003u, 15513307u,
2995931u, 7190235u, 11384539u, 15578843u, 3061467u, 7255771u, 11450075u, 15644379u,
3127003u, 7321307u, 11515611u, 15709915u, 2931419u, 7125723u, 11320027u, 15514331u,
2996955u, 7191259u, 11385563u, 15579867u, 3062491u, 7256795u, 11451099u, 15645403u,
3128027u, 7322331u, 11516635u, 15710939u, 2932443u, 7126747u, 11321051u, 15515355u,
2997979u, 7192283u, 11386587u, 15580891u, 3063515u, 7257819u, 11452123u, 15646427u,
3129051u, 7323355u, 11517659u, 15711963u, 2929387u, 7123691u, 11317995u, 15512299u,
2994923u, 7189227u, 11383531u, 15577835u, 3060459u, 7254763u, 11449067u, 15643371u,
3125995u, 7320299u, 11514603u, 15708907u, 2930411u, 7124715u, 11319019u, 15513323u,
2995947u, 7190251u, 11384555u, 15578859u, 3061483u, 7255787u, 11450091u, 15644395u,
3127019u, 7321323u, 11515627u, 15709931u, 2931435u, 7125739u, 11320043u, 15514347u,
2996971u, 7191275u, 11385579u, 15579883u, 3062507u, 7256811u, 11451115u, 15645419u,
3128043u, 7322347u, 11516651u, 15710955u, 2932459u, 7126763u, 11321067u, 15515371u,
2997995u, 7192299u, 11386603u, 15580907u, 3063531u, 7257835u, 11452139u, 15646443u,
3129067u, 7323371u, 11517675u, 15711979u, 2929403u, 7123707u, 11318011u, 15512315u,
2994939u, 7189243u, 11383547u, 15577851u, 3060475u, 7254779u, 11449083u, 15643387u,
3126011u, 7320315u, 11514619u, 15708923u, 2930427u, 7124731u, 11319035u, 15513339u,
2995963u, 7190267u, 11384571u, 15578875u, 3061499u, 7255803u, 11450107u, 15644411u,
3127035u, 7321339u, 11515643u, 15709947u, 2931451u, 7125755u, 11320059u, 15514363u,
2996987u, 7191291u, 11385595u, 15579899u, 3062523u, 7256827u, 11451131u, 15645435u,
3128059u, 7322363u, 11516667u, 15710971u, 2932475u, 7126779u, 11321083u, 15515387u,
2998011u, 7192315u, 11386619u, 15580923u, 3063547u, 7257851u, 11452155u, 15646459u,
3129083u, 7323387u, 11517691u, 15711995u, 187478731u, 455914187u, 724349643u, 992785099u,
191673035u, 460108491u, 728543947u, 996979403u, 195867339u, 464302795u, 732738251u, 1001173707u,
200061643u, 468497099u, 736932555u, 1005368011u, 187544267u, 455979723u, 724415179u, 992850635u,
191738571u, 460174027u, 728609483u, 997044939u, 195932875u, 464368331u, 732803787u, 1001239243u,
200127179u, 468562635u, 736998091u, 1005433547u, 187609803u, 456045259u, 724480715u, 992916171u,
191804107u, 460239563u, 728675019u, 997110475u, 195998411u, 464433867u, 732869323u, 1001304779u,
200192715u, 468628171u, 737063627u, 1005499083u, 187675339u, 456110795u, 724546251u, 992981707u,
191869643u, 460305099u, 728740555u, 997176011u, 196063947u, 464499403u, 732934859u, 1001370315u,
200258251u, 468693707u, 737129163u, 1005564619u, 187479755u, 455915211u, 724350667u, 992786123u,
191674059u, 460109515u, 728544971u, 996980427u, 195868363u, 464303819u, 732739275u, 1001174731u,
200062667u, 468498123u, 736933579u, 1005369035u, 187545291u, 455980747u, 724416203u, 992851659u,
191739595u, 460175051u, 728610507u, 997045963u, 195933899u, 464369355u, 732804811u, 1001240267u,
200128203u, 468563659u, 736999115u, 1005434571u, 187610827u, 456046283u, 724481739u, 992917195u,
191805131u, 460240587u, 728676043u, 997111499u, 195999435u, 464434891u, 732870347u, 1001305803u,
200193739u, 468629195u, 737064651u, 1005500107u, 187676363u, 456111819u, 724547275u, 992982731u,
191870667u, 460306123u, 728741579u, 997177035u, 196064971u, 464500427u, 732935883u, 1001371339u,
200259275u, 468694731u, 737130187u, 1005565643u, 187480779u, 455916235u, 724351691u, 992787147u,
191675083u, 460110539u, 728545995u, 996981451u, 195869387u, 464304843u, 732740299u, 1001175755u,
200063691u, 468499147u, 736934603u, 1005370059u, 187546315u, 455981771u, 724417227u, 992852683u,
191740619u, 460176075u, 728611531u, 997046987u, 195934923u, 464370379u, 732805835u, 1001241291u,
200129227u, 468564683u, 737000139u, 1005435595u, 187611851u, 456047307u, 724482763u, 992918219u,
191806155u, 460241611u, 728677067u, 997112523u, 196000459u, 464435915u, 732871371u, 1001306827u,
200194763u, 468630219u, 737065675u, 1005501131u, 187677387u, 456112843u, 724548299u, 992983755u,
191871691u, 460307147u, 728742603u, 997178059u, 196065995u, 464501451u, 732936907u, 1001372363u,
200260299u, 468695755u, 737131211u, 1005566667u, 187481803u, 455917259u, 724352715u, 992788171u,
191676107u, 460111563u, 728547019u, 996982475u, 195870411u, 464305867u, 732741323u, 1001176779u,
200064715u, 468500171u, 736935627u, 1005371083u, 187547339u, 455982795u, 724418251u, 992853707u,
191741643u, 460177099u, 728612555u, 997048011u, 195935947u, 464371403u, 732806859u, 1001242315u,
200130251u, 468565707u, 737001163u, 1005436619u, 187612875u, 456048331u, 724483787u, 992919243u,
191807179u, 460242635u, 728678091u, 997113547u, 196001483u, 464436939u, 732872395u, 1001307851u,
200195787u, 468631243u, 737066699u, 1005502155u, 187678411u, 456113867u, 724549323u, 992984779u,
191872715u, 460308171u, 728743627u, 997179083u, 196067019u, 464502475u, 732937931u, 1001373387u,
200261323u, 468696779u, 737132235u, 1005567691u, 187478747u, 455914203u, 724349659u, 992785115u,
191673051u, 460108507u, 728543963u, 996979419u, 195867355u, 464302811u, 732738267u, 1001173723u,
200061659u, 468497115u, 736932571u, 1005368027u, 187544283u, 455979739u, 724415195u, 992850651u,
191738587u, 460174043u, 728609499u, 997044955u, 195932891u, 464368347u, 732803803u, 1001239259u,
200127195u, 468562651u, 736998107u, 1005433563u, 187609819u, 456045275u, 724480731u, 992916187u,
191804123u, 460239579u, 728675035u, 997110491u, 195998427u, 464433883u, 732869339u, 1001304795u,
200192731u, 468628187u, 737063643u, 1005499099u, 187675355u, 456110811u, 724546267u, 992981723u,
191869659u, 460305115u, 728740571u, 997176027u, 196063963u, 464499419u, 732934875u, 1001370331u,
200258267u, 468693723u, 737129179u, 1005564635u, 187479771u, 455915227u, 724350683u, 992786139u,
191674075u, 460109531u, 728544987u, 996980443u, 195868379u, 464303835u, 732739291u, 1001174747u,
200062683u, 468498139u, 736933595u, 1005369051u, 187545307u, 455980763u, 724416219u, 992851675u,
191739611u, 460175067u, 728610523u, 997045979u, 195933915u, 464369371u, 732804827u, 1001240283u,
200128219u, 468563675u, 736999131u, 1005434587u, 187610843u, 456046299u, 724481755u, 992917211u,
191805147u, 460240603u, 728676059u, 997111515u, 195999451u, 464434907u, 732870363u, 1001305819u,
};
static const uint32_t
WUFFS_BROTLI__NON_ZERO_REPS_DEPTH[704] WUFFS_BASE__POTENTIALLY_UNUSED = {
6u, 6u, 6u, 6u, 12u, 12u, 12u, 12u,
12u, 12u, 12u, 12u, 12u, 12u, 12u, 12u,
12u, 12u, 12u, 12u, 18u, 18u, 18u, 18u,
18u, 18u, 18u, 18u, 18u, 18u, 18u, 18u,
18u, 18u, 18u, 18u, 18u, 18u, 18u, 18u,
18u, 18u, 18u, 18u, 18u, 18u, 18u, 18u,
18u, 18u, 18u, 18u, 18u, 18u, 18u, 18u,
18u, 18u, 18u, 18u, 18u, 18u, 18u, 18u,
18u, 18u, 18u, 18u, 18u, 18u, 18u, 18u,
18u, 18u, 18u, 18u, 18u, 18u, 18u, 18u,
18u, 18u, 18u, 18u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 24u, 24u, 24u, 24u,
24u, 24u, 24u, 24u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
30u, 30u, 30u, 30u, 30u, 30u, 30u, 30u,
};
static const uint8_t
WUFFS_BROTLI__STORAGE_ORDER[18] WUFFS_BASE__POTENTIALLY_UNUSED = {
1u, 2u, 3u, 4u, 0u, 5u, 17u, 6u,
16u, 7u, 8u, 9u, 10u, 11u, 12u, 13u,
14u, 15u,
};
static const uint64_t
WUFFS_BROTLI__HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_SYMBOLS[6] WUFFS_BASE__POTENTIALLY_UNUSED = {
0u, 7u, 3u, 2u, 1u, 15u,
};
static const uint32_t
WUFFS_BROTLI__HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_BIT_LENGTHS[6] WUFFS_BASE__POTENTIALLY_UNUSED = {
2u, 4u, 3u, 2u, 2u, 4u,
};
#define WUFFS_BROTLI__NUM_TRANSFORMS 121u
#define WUFFS_BROTLI__MAX_AFFIX_LENGTH 8u
@@ -56545,6 +59450,40 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = {
#define WUFFS_BROTLI__DIST_CONTEXT_MAP_BASE 16384u
#define WUFFS_BROTLI__QUIRKS_BASE 716673024u
#define WUFFS_BROTLI__STREAM_PROCESSING 0u
#define WUFFS_BROTLI__STREAM_FLUSH_REQUESTED 1u
#define WUFFS_BROTLI__STREAM_FINISHED 2u
#define WUFFS_BROTLI__STREAM_METADATA_HEAD 3u
#define WUFFS_BROTLI__STREAM_METADATA_BODY 4u
#define WUFFS_BROTLI__OP_PROCESS 0u
#define WUFFS_BROTLI__OP_FLUSH 1u
#define WUFFS_BROTLI__OP_FINISH 2u
#define WUFFS_BROTLI__OP_EMIT_METADATA 3u
#define WUFFS_BROTLI__FLINT_NEEDS_2_BYTES 2u
#define WUFFS_BROTLI__FLINT_DONE 4294967295u
#define WUFFS_BROTLI__STORAGE_SLACK 16u
#define WUFFS_BROTLI__TWO_PASS_BLOCK_SIZE 131072u
#define WUFFS_BROTLI__TREE_NONE 65535u
#define WUFFS_BROTLI__FRAGMENT_MAX_DISTANCE 262128u
#define WUFFS_BROTLI__HASH_MUL32 506832829u
// ---------------- Private Initializer Prototypes
// ---------------- Private Function Prototypes
@@ -56783,6 +59722,558 @@ wuffs_brotli__decoder__to_upper_case(
uint32_t a_p,
uint32_t a_end);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__write_bits(
wuffs_brotli__encoder* self,
uint32_t a_n_bits,
uint64_t a_bits,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__align_to_byte(
wuffs_brotli__encoder* self);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__rewind_bit_position(
wuffs_brotli__encoder* self,
uint64_t a_new_ix,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__update_bits(
wuffs_brotli__encoder* self,
uint32_t a_n_bits,
uint32_t a_bits,
uint64_t a_pos,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_meta_block_header(
wuffs_brotli__encoder* self,
uint64_t a_len,
bool a_is_uncompressed,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__log2_floor_non_zero(
const wuffs_brotli__encoder* self,
uint64_t a_n);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__fast_log2(
const wuffs_brotli__encoder* self,
uint64_t a_v);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__bits_entropy_of_histogram(
const wuffs_brotli__encoder* self);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__load64le(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s,
uint64_t a_i);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__load32le(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s,
uint64_t a_i);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__find_match_length_with_limit(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s,
uint64_t a_i1,
uint64_t a_i2,
uint64_t a_limit);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__sanitized_quality(
const wuffs_brotli__encoder* self);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__sanitized_lgwin(
const wuffs_brotli__encoder* self,
uint32_t a_quality);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__ensure_initialized(
wuffs_brotli__encoder* self);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__encode_window_bits(
wuffs_brotli__encoder* self,
uint32_t a_lgwin);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__init_command_prefix_codes(
wuffs_brotli__encoder* self);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__get_hash_table(
wuffs_brotli__encoder* self,
uint64_t a_input_size);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__inject_byte_padding_block(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_workbuf);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__push_output(
wuffs_brotli__encoder* self,
wuffs_base__io_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__check_flush_complete(
wuffs_brotli__encoder* self);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__write_metadata_header(
wuffs_brotli__encoder* self,
uint32_t a_block_size);
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__set_depth(
wuffs_brotli__encoder* self,
uint32_t a_p0,
wuffs_base__slice_u8 a_depth,
uint32_t a_max_depth);
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__huffman_tree_less(
const wuffs_brotli__encoder* self,
uint32_t a_c0,
uint16_t a_r0,
uint32_t a_c1,
uint16_t a_r1,
bool a_tie_break);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__sort_huffman_tree_items(
wuffs_brotli__encoder* self,
uint32_t a_n,
bool a_tie_break);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__set_sentinel(
wuffs_brotli__encoder* self,
uint32_t a_i);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__merge_huffman_nodes(
wuffs_brotli__encoder* self,
uint32_t a_n);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__create_huffman_tree(
wuffs_brotli__encoder* self,
uint32_t a_src,
uint32_t a_off,
uint32_t a_length,
uint32_t a_tree_limit,
wuffs_base__slice_u8 a_depth);
WUFFS_BASE__GENERATED_C_CODE
static uint16_t
wuffs_brotli__encoder__reverse_bits(
const wuffs_brotli__encoder* self,
uint32_t a_num_bits,
uint16_t a_bits);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__convert_bit_depths_to_symbols(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_depth,
uint32_t a_len,
uint32_t a_dst);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__reverse_huffman_tree(
wuffs_brotli__encoder* self,
uint32_t a_start,
uint32_t a_end);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__push_huffman_tree(
wuffs_brotli__encoder* self,
uint8_t a_value,
uint8_t a_extra_bits);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__write_huffman_tree_repetitions(
wuffs_brotli__encoder* self,
uint8_t a_previous_value,
uint8_t a_value,
uint32_t a_repetitions);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__write_huffman_tree_repetitions_zeros(
wuffs_brotli__encoder* self,
uint32_t a_repetitions);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__decide_over_rle_use(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_depth,
uint32_t a_length);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__write_huffman_tree(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_depth,
uint32_t a_length);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_huffman_tree_of_huffman_tree_to_bit_mask(
wuffs_brotli__encoder* self,
uint32_t a_num_codes,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_huffman_tree_to_bit_mask(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_huffman_tree(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_depths,
uint32_t a_num,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__build_and_store_huffman_tree_fast(
wuffs_brotli__encoder* self,
uint64_t a_histogram_total,
uint32_t a_max_bits,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(
wuffs_brotli__encoder* self,
uint32_t a_dst,
uint32_t a_src,
uint32_t a_n);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__hash_fast(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_i,
uint32_t a_shift);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__hash_bytes_at_offset_fast(
const wuffs_brotli__encoder* self,
uint64_t a_v,
uint32_t a_offset,
uint32_t a_shift);
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__is_match_fast(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_i1,
uint64_t a_i2);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__build_and_store_literal_prefix_code(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_input_size,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__build_and_store_command_prefix_code_fast(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_cmd_fast(
wuffs_brotli__encoder* self,
uint64_t a_code,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_insert_len_fast(
wuffs_brotli__encoder* self,
uint64_t a_insertlen,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_long_insert_len_fast(
wuffs_brotli__encoder* self,
uint64_t a_insertlen,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_copy_len_fast(
wuffs_brotli__encoder* self,
uint64_t a_copylen,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_copy_len_last_distance_fast(
wuffs_brotli__encoder* self,
uint64_t a_copylen,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_distance_fast(
wuffs_brotli__encoder* self,
uint64_t a_distance,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_literals(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_len,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__should_merge_block(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_len);
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__should_use_uncompressed_mode(
const wuffs_brotli__encoder* self,
uint64_t a_metablock_start,
uint64_t a_next_emit,
uint64_t a_insertlen,
uint64_t a_literal_ratio);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_uncompressed_meta_block_fast(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_begin,
uint64_t a_end,
uint64_t a_storage_ix_start,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__copy_bytes(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_begin,
uint64_t a_end,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__update_table_fast(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_ip,
uint32_t a_shift);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__compress_fragment_fast(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
bool a_is_last,
uint32_t a_table_bits,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__compress_fragment_fast_impl(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
bool a_is_last,
uint32_t a_table_bits,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__hash_two_pass(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_i,
uint32_t a_shift,
uint32_t a_length);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(
const wuffs_brotli__encoder* self,
uint64_t a_v,
uint32_t a_offset,
uint32_t a_shift,
uint32_t a_length);
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__is_match_two_pass(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_i1,
uint64_t a_i2,
uint32_t a_length);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__build_and_store_command_prefix_code_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__push_command(
wuffs_brotli__encoder* self,
uint32_t a_c);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_insert_len_two_pass(
wuffs_brotli__encoder* self,
uint32_t a_insertlen);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_copy_len_two_pass(
wuffs_brotli__encoder* self,
uint64_t a_copylen);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_copy_len_last_distance_two_pass(
wuffs_brotli__encoder* self,
uint64_t a_copylen);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_distance_two_pass(
wuffs_brotli__encoder* self,
uint32_t a_distance);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__push_literals(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_n);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__update_table_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_ip,
uint32_t a_shift,
uint32_t a_min_match,
uint32_t a_third_offset);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__create_commands(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_block_size,
uint64_t a_input_size,
uint32_t a_table_bits,
uint32_t a_min_match);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_commands(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__should_compress_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_input_size,
uint64_t a_num_literals);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_uncompressed_meta_block_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_input_size,
wuffs_base__slice_u8 a_s);
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__compress_fragment_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
bool a_is_last,
uint32_t a_table_bits,
wuffs_base__slice_u8 a_s);
// ---------------- VTables
const wuffs_base__io_transformer__func_ptrs
@@ -56800,6 +60291,21 @@ wuffs_brotli__decoder__func_ptrs_for__wuffs_base__io_transformer = {
(wuffs_base__range_ii_u64(*)(const void*))(&wuffs_brotli__decoder__workbuf_len),
};
const wuffs_base__io_transformer__func_ptrs
wuffs_brotli__encoder__func_ptrs_for__wuffs_base__io_transformer = {
(wuffs_base__optional_u63(*)(const void*))(&wuffs_brotli__encoder__dst_history_retain_length),
(uint64_t(*)(const void*,
uint32_t))(&wuffs_brotli__encoder__get_quirk),
(wuffs_base__status(*)(void*,
uint32_t,
uint64_t))(&wuffs_brotli__encoder__set_quirk),
(wuffs_base__status(*)(void*,
wuffs_base__io_buffer*,
wuffs_base__io_buffer*,
wuffs_base__slice_u8))(&wuffs_brotli__encoder__transform_io),
(wuffs_base__range_ii_u64(*)(const void*))(&wuffs_brotli__encoder__workbuf_len),
};
// ---------------- Initializer Implementations
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
@@ -56869,6 +60375,80 @@ sizeof__wuffs_brotli__decoder(void) {
return sizeof(wuffs_brotli__decoder);
}
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
wuffs_brotli__encoder__initialize(
wuffs_brotli__encoder* self,
size_t sizeof_star_self,
uint64_t wuffs_version,
uint32_t options){
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (sizeof(*self) != sizeof_star_self) {
return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver);
}
if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) ||
(((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) {
return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version);
}
if ((options & WUFFS_INITIALIZE__ALREADY_ZEROED) != 0) {
// The whole point of this if-check is to detect an uninitialized *self.
// We disable the warning on GCC. Clang-5.0 does not have this warning.
#if !defined(__clang__) && defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
if (self->private_impl.magic != 0) {
return wuffs_base__make_status(wuffs_base__error__initialize_falsely_claimed_already_zeroed);
}
#if !defined(__clang__) && defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
} else {
if ((options & WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED) == 0) {
memset(self, 0, sizeof(*self));
options |= WUFFS_INITIALIZE__ALREADY_ZEROED;
} else {
memset(&(self->private_impl), 0, sizeof(self->private_impl));
}
}
{
wuffs_base__status z = wuffs_softfloat__arith__initialize(
&self->private_data.f_sf, sizeof(self->private_data.f_sf), WUFFS_VERSION, options);
if (z.repr) {
return z;
}
}
self->private_impl.magic = WUFFS_BASE__MAGIC;
self->private_impl.vtable_for__wuffs_base__io_transformer.vtable_name =
wuffs_base__io_transformer__vtable_name;
self->private_impl.vtable_for__wuffs_base__io_transformer.function_pointers =
(const void*)(&wuffs_brotli__encoder__func_ptrs_for__wuffs_base__io_transformer);
return wuffs_base__make_status(NULL);
}
wuffs_brotli__encoder*
wuffs_brotli__encoder__alloc(void) {
wuffs_brotli__encoder* x =
(wuffs_brotli__encoder*)(calloc(1, sizeof(wuffs_brotli__encoder)));
if (!x) {
return NULL;
}
if (wuffs_brotli__encoder__initialize(
x, sizeof(wuffs_brotli__encoder), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) {
free(x);
return NULL;
}
return x;
}
size_t
sizeof__wuffs_brotli__encoder(void) {
return sizeof(wuffs_brotli__encoder);
}
// ---------------- Function Implementations
// -------- func brotli.decoder.get_quirk
@@ -59704,6 +63284,3538 @@ wuffs_brotli__decoder__to_upper_case(
return 3u;
}
// -------- func brotli.encoder.write_bits
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__write_bits(
wuffs_brotli__encoder* self,
uint32_t a_n_bits,
uint64_t a_bits,
wuffs_base__slice_u8 a_s) {
uint64_t v_p = 0;
uint64_t v_v = 0;
wuffs_base__slice_u8 v_q = {0};
v_p = (self->private_impl.f_bw_ix >> 3u);
if (v_p < ((uint64_t)(a_s.len))) {
v_q = wuffs_base__slice_u8__subslice_i(a_s, v_p);
v_v = (((uint64_t)(a_s.ptr[v_p])) | ((uint64_t)(a_bits << (self->private_impl.f_bw_ix & 7u))));
if (((uint64_t)(v_q.len)) >= 8u) {
wuffs_base__poke_u64le__no_bounds_check(v_q.ptr, v_v);
} else {
while (((uint64_t)(v_q.len)) > 0u) {
v_q.ptr[0u] = ((uint8_t)(v_v));
v_v >>= 8u;
v_q = wuffs_base__slice_u8__subslice_i(v_q, 1u);
}
}
}
self->private_impl.f_bw_ix += ((uint64_t)(a_n_bits));
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.align_to_byte
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__align_to_byte(
wuffs_brotli__encoder* self) {
self->private_impl.f_bw_ix = (((uint64_t)(self->private_impl.f_bw_ix + 7u)) & 18446744073709551608u);
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.rewind_bit_position
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__rewind_bit_position(
wuffs_brotli__encoder* self,
uint64_t a_new_ix,
wuffs_base__slice_u8 a_s) {
uint64_t v_p = 0;
uint8_t v_mask = 0;
v_p = (a_new_ix >> 3u);
v_mask = ((uint8_t)(((((uint32_t)(1u)) << (a_new_ix & 7u)) - 1u)));
if (v_p < ((uint64_t)(a_s.len))) {
a_s.ptr[v_p] &= v_mask;
}
self->private_impl.f_bw_ix = a_new_ix;
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.update_bits
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__update_bits(
wuffs_brotli__encoder* self,
uint32_t a_n_bits,
uint32_t a_bits,
uint64_t a_pos,
wuffs_base__slice_u8 a_s) {
uint32_t v_n_bits = 0;
uint32_t v_bits = 0;
uint64_t v_pos = 0;
uint64_t v_byte_pos = 0;
uint32_t v_n_unchanged_bits = 0;
uint32_t v_n_changed_bits = 0;
uint32_t v_total_bits = 0;
uint32_t v_mask = 0;
uint32_t v_unchanged_bits = 0;
uint32_t v_changed_bits = 0;
v_n_bits = a_n_bits;
v_bits = a_bits;
v_pos = a_pos;
while (v_n_bits > 0u) {
v_byte_pos = (v_pos >> 3u);
v_n_unchanged_bits = ((uint32_t)((v_pos & 7u)));
v_n_changed_bits = wuffs_base__u32__min(v_n_bits, (8u - v_n_unchanged_bits));
v_total_bits = (v_n_unchanged_bits + v_n_changed_bits);
v_mask = ((4294967295u ^ ((((uint32_t)(1u)) << v_total_bits) - 1u)) | ((((uint32_t)(1u)) << v_n_unchanged_bits) - 1u));
if (v_byte_pos >= ((uint64_t)(a_s.len))) {
return wuffs_base__make_empty_struct();
}
v_unchanged_bits = (((uint32_t)(a_s.ptr[v_byte_pos])) & v_mask);
v_changed_bits = (v_bits & ((((uint32_t)(1u)) << v_n_changed_bits) - 1u));
a_s.ptr[v_byte_pos] = ((uint8_t)(((v_changed_bits << v_n_unchanged_bits) | v_unchanged_bits)));
v_n_bits -= v_n_changed_bits;
v_bits = (v_bits >> v_n_changed_bits);
v_pos += ((uint64_t)(v_n_changed_bits));
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.store_meta_block_header
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_meta_block_header(
wuffs_brotli__encoder* self,
uint64_t a_len,
bool a_is_uncompressed,
wuffs_base__slice_u8 a_s) {
uint32_t v_nibbles_minus_4 = 0;
v_nibbles_minus_4 = 2u;
wuffs_brotli__encoder__write_bits(self, 1u, 0u, a_s);
if (a_len <= 65536u) {
v_nibbles_minus_4 = 0u;
} else if (a_len <= 1048576u) {
v_nibbles_minus_4 = 1u;
}
wuffs_brotli__encoder__write_bits(self, 2u, ((uint64_t)(v_nibbles_minus_4)), a_s);
wuffs_brotli__encoder__write_bits(self, ((v_nibbles_minus_4 + 4u) * 4u), ((uint64_t)(a_len - 1u)), a_s);
if (a_is_uncompressed) {
wuffs_brotli__encoder__write_bits(self, 1u, 1u, a_s);
} else {
wuffs_brotli__encoder__write_bits(self, 1u, 0u, a_s);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.log2_floor_non_zero
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
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);
}
// -------- func brotli.encoder.fast_log2
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__fast_log2(
const wuffs_brotli__encoder* self,
uint64_t a_v) {
if (a_v < 256u) {
return WUFFS_BROTLI__LOG2_TABLE[a_v];
}
return wuffs_softfloat__arith__log2_u64(&self->private_data.f_sf, a_v);
}
// -------- func brotli.encoder.bits_entropy_of_histogram
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__bits_entropy_of_histogram(
const wuffs_brotli__encoder* self) {
uint64_t v_sum = 0;
uint64_t v_retval = 0;
uint32_t v_i = 0;
uint64_t v_p = 0;
v_i = 0u;
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_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)));
}
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);
}
return v_retval;
}
// -------- func brotli.encoder.load64le
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
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);
}
}
return 0u;
}
// -------- func brotli.encoder.load32le
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
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);
}
}
return 0u;
}
// -------- func brotli.encoder.find_match_length_with_limit
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__find_match_length_with_limit(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s,
uint64_t a_i1,
uint64_t a_i2,
uint64_t a_limit) {
uint64_t v_matched = 0;
uint64_t v_n = 0;
wuffs_base__slice_u8 v_a = {0};
wuffs_base__slice_u8 v_b = {0};
uint64_t v_x = 0;
if ((a_i1 > ((uint64_t)(a_s.len))) || (a_i2 > ((uint64_t)(a_s.len)))) {
return 0u;
}
v_a = wuffs_base__slice_u8__subslice_i(a_s, a_i1);
v_b = wuffs_base__slice_u8__subslice_i(a_s, a_i2);
v_n = a_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;
}
v_matched += 8u;
v_n -= 8u;
v_a = wuffs_base__slice_u8__subslice_i(v_a, 8u);
v_b = wuffs_base__slice_u8__subslice_i(v_b, 8u);
}
while ((v_n > 0u) && (((uint64_t)(v_a.len)) > 0u) && (((uint64_t)(v_b.len)) > 0u)) {
if (v_a.ptr[0u] != v_b.ptr[0u]) {
break;
}
v_matched += 1u;
v_n -= 1u;
v_a = wuffs_base__slice_u8__subslice_i(v_a, 1u);
v_b = wuffs_base__slice_u8__subslice_i(v_b, 1u);
}
return v_matched;
}
// -------- func brotli.encoder.get_quirk
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_brotli__encoder__get_quirk(
const wuffs_brotli__encoder* self,
uint32_t a_key) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (a_key == 716673028u) {
return ((uint64_t)(self->private_impl.f_p_mode));
} else if (a_key == 716673025u) {
if ( ! self->private_impl.f_p_quality_set) {
return 11u;
}
return ((uint64_t)(self->private_impl.f_p_quality));
} else if (a_key == 716673026u) {
if ( ! self->private_impl.f_p_lgwin_set) {
return 22u;
}
return ((uint64_t)(self->private_impl.f_p_lgwin));
} else if (a_key == 716673027u) {
return ((uint64_t)(self->private_impl.f_p_lgblock));
} else if (a_key == 716673029u) {
return ((uint64_t)(self->private_impl.f_p_disable_lcm));
} else if (a_key == 716673030u) {
return ((uint64_t)(self->private_impl.f_p_size_hint));
} else if (a_key == 716673024u) {
return ((uint64_t)(self->private_impl.f_p_large_window));
} else if (a_key == 716673031u) {
return ((uint64_t)(self->private_impl.f_p_npostfix));
} else if (a_key == 716673032u) {
return ((uint64_t)(self->private_impl.f_p_ndirect));
} else if (a_key == 716673033u) {
return ((uint64_t)(self->private_impl.f_p_stream_offset));
}
return 0u;
}
// -------- func brotli.encoder.set_quirk
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_brotli__encoder__set_quirk(
wuffs_brotli__encoder* self,
uint32_t a_key,
uint64_t a_value) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
uint32_t v_v = 0;
if (self->private_impl.f_is_initialized) {
return wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
} else if (a_value > 4294967295u) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
v_v = ((uint32_t)(a_value));
if (a_key == 716673028u) {
self->private_impl.f_p_mode = v_v;
} else if (a_key == 716673025u) {
self->private_impl.f_p_quality = v_v;
self->private_impl.f_p_quality_set = true;
} else if (a_key == 716673026u) {
self->private_impl.f_p_lgwin = v_v;
self->private_impl.f_p_lgwin_set = true;
} else if (a_key == 716673027u) {
self->private_impl.f_p_lgblock = v_v;
} else if (a_key == 716673029u) {
if (v_v > 1u) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
self->private_impl.f_p_disable_lcm = v_v;
} else if (a_key == 716673030u) {
self->private_impl.f_p_size_hint = v_v;
} else if (a_key == 716673024u) {
if (v_v != 0u) {
self->private_impl.f_p_large_window = 1u;
} else {
self->private_impl.f_p_large_window = 0u;
}
} else if (a_key == 716673031u) {
self->private_impl.f_p_npostfix = v_v;
} else if (a_key == 716673032u) {
self->private_impl.f_p_ndirect = v_v;
} else if (a_key == 716673033u) {
if (v_v > 1073741824u) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
self->private_impl.f_p_stream_offset = v_v;
} else {
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
return wuffs_base__make_status(NULL);
}
// -------- func brotli.encoder.dst_history_retain_length
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__optional_u63
wuffs_brotli__encoder__dst_history_retain_length(
const wuffs_brotli__encoder* self) {
if (!self) {
return wuffs_base__utility__make_optional_u63(false, 0u);
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return wuffs_base__utility__make_optional_u63(false, 0u);
}
return wuffs_base__utility__make_optional_u63(true, 0u);
}
// -------- func brotli.encoder.workbuf_len
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64
wuffs_brotli__encoder__workbuf_len(
const wuffs_brotli__encoder* self) {
if (!self) {
return wuffs_base__utility__empty_range_ii_u64();
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return wuffs_base__utility__empty_range_ii_u64();
}
uint32_t v_q = 0;
uint64_t v_n = 0;
v_q = wuffs_brotli__encoder__sanitized_quality(self);
if (v_q <= 1u) {
v_n = ((((uint64_t)(2u)) << wuffs_brotli__encoder__sanitized_lgwin(self, v_q)) + 503u + 16u);
}
return wuffs_base__utility__make_range_ii_u64(v_n, v_n);
}
// -------- func brotli.encoder.max_compressed_size
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_brotli__encoder__max_compressed_size(
const wuffs_brotli__encoder* self,
uint64_t a_input_size) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_num_large_blocks = 0;
uint64_t v_overhead = 0;
uint64_t v_result = 0;
v_num_large_blocks = (a_input_size >> 14u);
v_overhead = (2u +
(4u * v_num_large_blocks) +
3u +
1u);
v_result = ((uint64_t)(a_input_size + v_overhead));
if (a_input_size == 0u) {
return 2u;
} else if (v_result < a_input_size) {
return 0u;
}
return v_result;
}
// -------- func brotli.encoder.request_flush
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_brotli__encoder__request_flush(
wuffs_brotli__encoder* self) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
if (self->private_impl.f_metadata_requested) {
return wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
}
self->private_impl.f_flush_requested = true;
return wuffs_base__make_status(NULL);
}
// -------- func brotli.encoder.request_emit_metadata
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_brotli__encoder__request_emit_metadata(
wuffs_brotli__encoder* self,
uint64_t a_length) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
if (a_length > 16777216u) {
return wuffs_base__make_status(wuffs_base__error__bad_argument);
} else if (self->private_impl.f_flush_requested || self->private_impl.f_metadata_requested) {
return wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
}
self->private_impl.f_metadata_requested = true;
self->private_impl.f_metadata_length = ((uint32_t)(a_length));
return wuffs_base__make_status(NULL);
}
// -------- func brotli.encoder.sanitized_quality
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__sanitized_quality(
const wuffs_brotli__encoder* self) {
if ( ! self->private_impl.f_p_quality_set) {
return 11u;
} else if (self->private_impl.f_p_quality >= 2147483648u) {
return 0u;
}
return wuffs_base__u32__min(self->private_impl.f_p_quality, 11u);
}
// -------- func brotli.encoder.sanitized_lgwin
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__sanitized_lgwin(
const wuffs_brotli__encoder* self,
uint32_t a_quality) {
uint32_t v_lgwin = 0;
v_lgwin = 22u;
if (self->private_impl.f_p_lgwin_set) {
v_lgwin = self->private_impl.f_p_lgwin;
}
if ((v_lgwin >= 2147483648u) || (v_lgwin < 10u)) {
return 10u;
} else if ((self->private_impl.f_p_large_window != 0u) && (a_quality > 2u)) {
return wuffs_base__u32__min(v_lgwin, 30u);
}
return wuffs_base__u32__min(v_lgwin, 24u);
}
// -------- func brotli.encoder.ensure_initialized
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__ensure_initialized(
wuffs_brotli__encoder* self) {
uint32_t v_lgblock = 0;
uint32_t v_lgwin = 0;
uint32_t v_limit = 0;
uint32_t v_npostfix = 0;
uint32_t v_ndirect = 0;
uint32_t v_ndirect_msb = 0;
uint32_t v_i = 0;
if (self->private_impl.f_is_initialized) {
return wuffs_base__make_empty_struct();
}
self->private_impl.f_dist_cache[0u] = 4u;
self->private_impl.f_dist_cache[1u] = 11u;
self->private_impl.f_dist_cache[2u] = 15u;
self->private_impl.f_dist_cache[3u] = 16u;
self->private_impl.f_saved_dist_cache[0u] = 4u;
self->private_impl.f_saved_dist_cache[1u] = 11u;
self->private_impl.f_saved_dist_cache[2u] = 15u;
self->private_impl.f_saved_dist_cache[3u] = 16u;
self->private_impl.f_stream_state = 0u;
self->private_impl.f_last_bytes_bits = 0u;
self->private_impl.f_last_bytes = 0u;
self->private_impl.f_flint = 4294967295u;
self->private_impl.f_remaining_metadata_bytes = 4294967295u;
self->private_impl.f_quality = wuffs_brotli__encoder__sanitized_quality(self);
self->private_impl.f_large_window = ((self->private_impl.f_p_large_window != 0u) && (self->private_impl.f_quality > 2u));
self->private_impl.f_lgwin = wuffs_brotli__encoder__sanitized_lgwin(self, self->private_impl.f_quality);
self->private_impl.f_size_hint = self->private_impl.f_p_size_hint;
self->private_impl.f_stream_offset = self->private_impl.f_p_stream_offset;
if (self->private_impl.f_quality <= 1u) {
self->private_impl.f_lgblock = self->private_impl.f_lgwin;
} else if (self->private_impl.f_quality < 4u) {
self->private_impl.f_lgblock = 14u;
} else if (self->private_impl.f_p_lgblock == 0u) {
self->private_impl.f_lgblock = 16u;
if ((self->private_impl.f_quality >= 9u) && (self->private_impl.f_lgwin > 16u)) {
self->private_impl.f_lgblock = wuffs_base__u32__min(self->private_impl.f_lgwin, 18u);
}
} else {
v_lgblock = self->private_impl.f_p_lgblock;
if (v_lgblock >= 2147483648u) {
v_lgblock = 16u;
}
self->private_impl.f_lgblock = wuffs_base__u32__min(wuffs_base__u32__max(v_lgblock, 16u), 24u);
}
if (self->private_impl.f_quality >= 4u) {
if (self->private_impl.f_p_mode == 2u) {
v_npostfix = 1u;
v_ndirect = 12u;
} else {
v_npostfix = self->private_impl.f_p_npostfix;
v_ndirect = self->private_impl.f_p_ndirect;
}
if (v_npostfix > 3u) {
v_npostfix = 0u;
v_ndirect = 0u;
} else {
v_ndirect_msb = ((v_ndirect >> v_npostfix) & 15u);
if ((v_ndirect > 120u) || ((v_ndirect_msb << v_npostfix) != v_ndirect)) {
v_npostfix = 0u;
v_ndirect = 0u;
}
}
}
self->private_impl.f_distance_postfix_bits = v_npostfix;
self->private_impl.f_num_direct_distance_codes = v_ndirect;
if (self->private_impl.f_stream_offset != 0u) {
self->private_impl.f_flint = 2u;
v_i = 0u;
while (v_i < 4u) {
self->private_impl.f_dist_cache[v_i] = 4294967280u;
self->private_impl.f_saved_dist_cache[v_i] = 4294967280u;
v_i += 1u;
}
}
v_lgwin = self->private_impl.f_lgwin;
if (self->private_impl.f_quality <= 1u) {
v_lgwin = wuffs_base__u32__max(v_lgwin, 18u);
}
if (self->private_impl.f_stream_offset == 0u) {
wuffs_brotli__encoder__encode_window_bits(self, v_lgwin);
} else {
v_limit = ((uint32_t)((((uint32_t)(1u)) << v_lgwin) - 16u));
self->private_impl.f_stream_offset = wuffs_base__u32__min(self->private_impl.f_stream_offset, v_limit);
}
if (self->private_impl.f_quality == 0u) {
wuffs_brotli__encoder__init_command_prefix_codes(self);
}
self->private_impl.f_is_initialized = true;
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.encode_window_bits
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__encode_window_bits(
wuffs_brotli__encoder* self,
uint32_t a_lgwin) {
if (self->private_impl.f_large_window) {
self->private_impl.f_last_bytes = (((a_lgwin & 63u) << 8u) | 17u);
self->private_impl.f_last_bytes_bits = 14u;
} else if (a_lgwin == 16u) {
self->private_impl.f_last_bytes = 0u;
self->private_impl.f_last_bytes_bits = 1u;
} else if (a_lgwin == 17u) {
self->private_impl.f_last_bytes = 1u;
self->private_impl.f_last_bytes_bits = 7u;
} else if (a_lgwin > 17u) {
self->private_impl.f_last_bytes = (((a_lgwin - 17u) << 1u) | 1u);
self->private_impl.f_last_bytes_bits = 4u;
} else if (a_lgwin >= 8u) {
self->private_impl.f_last_bytes = (((a_lgwin - 8u) << 4u) | 1u);
self->private_impl.f_last_bytes_bits = 7u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.init_command_prefix_codes
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__init_command_prefix_codes(
wuffs_brotli__encoder* self) {
uint32_t v_i = 0;
v_i = 0u;
while (v_i < 128u) {
self->private_data.f_cmd_depth[v_i] = WUFFS_BROTLI__DEFAULT_COMMAND_DEPTHS[v_i];
self->private_data.f_cmd_bits[v_i] = WUFFS_BROTLI__DEFAULT_COMMAND_BITS[v_i];
v_i += 1u;
}
v_i = 0u;
while (v_i < 57u) {
self->private_data.f_cmd_code[v_i] = WUFFS_BROTLI__DEFAULT_COMMAND_CODE[v_i];
v_i += 1u;
}
self->private_impl.f_cmd_code_numbits = ((uint64_t)(448u));
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.get_hash_table
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__get_hash_table(
wuffs_brotli__encoder* self,
uint64_t a_input_size) {
uint64_t v_max_table_size = 0;
uint64_t v_htsize = 0;
uint32_t v_table_bits = 0;
v_max_table_size = 131072u;
if (self->private_impl.f_quality == 0u) {
v_max_table_size = 32768u;
}
v_htsize = 256u;
v_table_bits = 8u;
while ((v_htsize < v_max_table_size) && (v_htsize < a_input_size)) {
v_htsize <<= 1u;
v_table_bits += 1u;
}
if (self->private_impl.f_quality == 0u) {
if ((v_htsize & 699050u) == 0u) {
v_htsize <<= 1u;
v_table_bits += 1u;
}
}
if (v_htsize <= 131072u) {
wuffs_private_impl__bulk_memset(&self->private_data.f_hash_table[0], v_htsize * (size_t)4u, 0u);
}
return v_table_bits;
}
// -------- func brotli.encoder.inject_byte_padding_block
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__inject_byte_padding_block(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_workbuf) {
uint32_t v_seal = 0;
uint32_t v_seal_bits = 0;
uint64_t v_p = 0;
uint32_t v_n = 0;
v_seal = self->private_impl.f_last_bytes;
v_seal_bits = self->private_impl.f_last_bytes_bits;
self->private_impl.f_last_bytes = 0u;
self->private_impl.f_last_bytes_bits = 0u;
v_seal |= (((uint32_t)(6u)) << v_seal_bits);
v_seal_bits += 6u;
v_n = ((v_seal_bits + 7u) >> 3u);
if (self->private_impl.f_out_len == 0u) {
self->private_impl.f_out_in_tiny_buf = true;
self->private_impl.f_out_pos = 0u;
}
v_p = ((uint64_t)(self->private_impl.f_out_pos + self->private_impl.f_out_len));
if (self->private_impl.f_out_in_tiny_buf) {
if (v_p <= 24u) {
self->private_data.f_tiny_buf[v_p] = ((uint8_t)(v_seal));
self->private_data.f_tiny_buf[(v_p + 1u)] = ((uint8_t)((v_seal >> 8u)));
self->private_data.f_tiny_buf[(v_p + 2u)] = ((uint8_t)((v_seal >> 16u)));
}
} else if (v_p < ((uint64_t)(a_workbuf.len))) {
a_workbuf.ptr[v_p] = ((uint8_t)(v_seal));
if (v_seal_bits > 8u) {
v_p += 1u;
if (v_p < ((uint64_t)(a_workbuf.len))) {
a_workbuf.ptr[v_p] = ((uint8_t)((v_seal >> 8u)));
}
}
if (v_seal_bits > 16u) {
v_p += 1u;
if (v_p < ((uint64_t)(a_workbuf.len))) {
a_workbuf.ptr[v_p] = ((uint8_t)((v_seal >> 16u)));
}
}
}
self->private_impl.f_out_len += ((uint64_t)(v_n));
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.push_output
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__push_output(
wuffs_brotli__encoder* self,
wuffs_base__io_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf) {
uint64_t v_n = 0;
uint64_t v_end = 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;
}
}
v_end = ((uint64_t)(self->private_impl.f_out_pos + self->private_impl.f_out_len));
if (self->private_impl.f_out_in_tiny_buf) {
if ((self->private_impl.f_out_pos <= v_end) && (v_end <= 32u)) {
v_n = wuffs_private_impl__io_writer__copy_from_slice(&iop_a_dst, io2_a_dst,wuffs_base__make_slice_u8_ij(self->private_data.f_tiny_buf, self->private_impl.f_out_pos, v_end));
}
} else if ((self->private_impl.f_out_pos <= v_end) && (v_end <= ((uint64_t)(a_workbuf.len)))) {
v_n = wuffs_private_impl__io_writer__copy_from_slice(&iop_a_dst, io2_a_dst,wuffs_base__slice_u8__subslice_ij(a_workbuf, self->private_impl.f_out_pos, v_end));
}
if (v_n == 0u) {
self->private_impl.f_out_len = 0u;
if (a_dst && a_dst->data.ptr) {
a_dst->meta.wi = ((size_t)(iop_a_dst - a_dst->data.ptr));
}
return wuffs_base__make_empty_struct();
}
v_n = wuffs_base__u64__min(v_n, self->private_impl.f_out_len);
self->private_impl.f_out_pos += v_n;
self->private_impl.f_out_len -= v_n;
if (a_dst && a_dst->data.ptr) {
a_dst->meta.wi = ((size_t)(iop_a_dst - a_dst->data.ptr));
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.check_flush_complete
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__check_flush_complete(
wuffs_brotli__encoder* self) {
if ((self->private_impl.f_stream_state == 1u) && (self->private_impl.f_out_len == 0u)) {
self->private_impl.f_stream_state = 0u;
self->private_impl.f_out_in_tiny_buf = false;
self->private_impl.f_out_pos = 0u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.write_metadata_header
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__write_metadata_header(
wuffs_brotli__encoder* self,
uint32_t a_block_size) {
uint32_t v_nbits = 0;
uint32_t v_nbytes = 0;
self->private_impl.f_bw_ix = ((uint64_t)(self->private_impl.f_last_bytes_bits));
self->private_data.f_tiny_buf[0u] = ((uint8_t)(self->private_impl.f_last_bytes));
self->private_data.f_tiny_buf[1u] = ((uint8_t)((self->private_impl.f_last_bytes >> 8u)));
self->private_impl.f_last_bytes = 0u;
self->private_impl.f_last_bytes_bits = 0u;
wuffs_brotli__encoder__write_bits(self, 1u, 0u, wuffs_base__make_slice_u8(self->private_data.f_tiny_buf, 32));
wuffs_brotli__encoder__write_bits(self, 2u, 3u, wuffs_base__make_slice_u8(self->private_data.f_tiny_buf, 32));
wuffs_brotli__encoder__write_bits(self, 1u, 0u, wuffs_base__make_slice_u8(self->private_data.f_tiny_buf, 32));
if (a_block_size == 0u) {
wuffs_brotli__encoder__write_bits(self, 2u, 0u, wuffs_base__make_slice_u8(self->private_data.f_tiny_buf, 32));
} else {
v_nbits = 1u;
if (a_block_size > 1u) {
v_nbits = (wuffs_brotli__encoder__log2_floor_non_zero(self, ((uint64_t)((a_block_size - 1u)))) + 1u);
}
v_nbytes = ((v_nbits + 7u) / 8u);
wuffs_brotli__encoder__write_bits(self, 2u, ((uint64_t)(v_nbytes)), wuffs_base__make_slice_u8(self->private_data.f_tiny_buf, 32));
wuffs_brotli__encoder__write_bits(self, (8u * v_nbytes), ((uint64_t)((a_block_size - 1u))), wuffs_base__make_slice_u8(self->private_data.f_tiny_buf, 32));
}
return (((uint64_t)(self->private_impl.f_bw_ix + 7u)) >> 3u);
}
// -------- func brotli.encoder.transform_io
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
wuffs_brotli__encoder__transform_io(
wuffs_brotli__encoder* self,
wuffs_base__io_buffer* a_dst,
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf) {
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
return wuffs_base__make_status(
(self->private_impl.magic == WUFFS_BASE__DISABLED)
? wuffs_base__error__disabled_by_previous_error
: wuffs_base__error__initialize_not_called);
}
if (!a_dst || !a_src) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
if ((self->private_impl.active_coroutine != 0) &&
(self->private_impl.active_coroutine != 1)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls);
}
self->private_impl.active_coroutine = 0;
wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_op = 0;
uint64_t v_avail = 0;
uint64_t v_block_size = 0;
bool v_is_last = false;
bool v_force_flush = false;
uint64_t v_storage_len = 0;
uint32_t v_table_bits = 0;
uint64_t v_mark = 0;
uint32_t v_n32 = 0;
uint64_t v_ix = 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_transform_io;
if (coro_susp_point) {
v_op = self->private_data.s_transform_io.v_op;
v_is_last = self->private_data.s_transform_io.v_is_last;
v_force_flush = self->private_data.s_transform_io.v_force_flush;
v_storage_len = self->private_data.s_transform_io.v_storage_len;
v_table_bits = self->private_data.s_transform_io.v_table_bits;
v_mark = self->private_data.s_transform_io.v_mark;
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
wuffs_brotli__encoder__ensure_initialized(self);
if (self->private_impl.f_quality > 1u) {
status = wuffs_base__make_status(wuffs_brotli__error__unsupported_encoder_quality);
goto exit;
}
while (true) {
if (self->private_impl.f_metadata_requested) {
if (self->private_impl.f_stream_state == 0u) {
self->private_impl.f_remaining_metadata_bytes = self->private_impl.f_metadata_length;
self->private_impl.f_stream_state = 3u;
}
if ((self->private_impl.f_stream_state != 3u) && (self->private_impl.f_stream_state != 4u)) {
status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
goto exit;
}
while (true) {
if (self->private_impl.f_out_len != 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(1);
continue;
}
if (a_dst) {
a_dst->meta.wi = ((size_t)(iop_a_dst - a_dst->data.ptr));
}
wuffs_brotli__encoder__push_output(self, a_dst, a_workbuf);
if (a_dst) {
iop_a_dst = a_dst->data.ptr + a_dst->meta.wi;
}
continue;
}
if (self->private_impl.f_stream_state == 3u) {
self->private_impl.f_out_len = wuffs_brotli__encoder__write_metadata_header(self, self->private_impl.f_remaining_metadata_bytes);
self->private_impl.f_out_in_tiny_buf = true;
self->private_impl.f_out_pos = 0u;
self->private_impl.f_stream_state = 4u;
continue;
}
if (self->private_impl.f_remaining_metadata_bytes == 0u) {
self->private_impl.f_remaining_metadata_bytes = 4294967295u;
self->private_impl.f_stream_state = 0u;
self->private_impl.f_metadata_requested = false;
break;
}
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(2);
continue;
} else if (((uint64_t)(io2_a_src - iop_a_src)) <= 0u) {
if (a_src && a_src->meta.closed) {
status = wuffs_base__make_status(wuffs_brotli__error__truncated_input);
goto exit;
}
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3);
continue;
}
v_n32 = wuffs_private_impl__io_writer__limited_copy_u32_from_reader(
&iop_a_dst, io2_a_dst,self->private_impl.f_remaining_metadata_bytes, &iop_a_src, io2_a_src);
self->private_impl.f_remaining_metadata_bytes -= v_n32;
}
continue;
}
if (self->private_impl.f_flush_requested) {
v_op = 1u;
} else if (a_src && a_src->meta.closed) {
v_op = 2u;
} else {
v_op = 0u;
}
if ((self->private_impl.f_stream_state == 3u) || (self->private_impl.f_stream_state == 4u)) {
status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
goto exit;
} else if ((self->private_impl.f_stream_state != 0u) && (((uint64_t)(io2_a_src - iop_a_src)) > 0u)) {
status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
goto exit;
}
while (true) {
if ((self->private_impl.f_stream_state == 1u) && (self->private_impl.f_last_bytes_bits != 0u)) {
wuffs_brotli__encoder__inject_byte_padding_block(self, a_workbuf);
continue;
}
if ((self->private_impl.f_out_len != 0u) && (((uint64_t)(io2_a_dst - iop_a_dst)) > 0u)) {
if (a_dst) {
a_dst->meta.wi = ((size_t)(iop_a_dst - a_dst->data.ptr));
}
wuffs_brotli__encoder__push_output(self, a_dst, a_workbuf);
if (a_dst) {
iop_a_dst = a_dst->data.ptr + a_dst->meta.wi;
}
continue;
}
if ((self->private_impl.f_out_len == 0u) && (self->private_impl.f_stream_state == 0u) && ((((uint64_t)(io2_a_src - iop_a_src)) > 0u) || (v_op != 0u))) {
v_avail = ((uint64_t)(io2_a_src - iop_a_src));
v_block_size = wuffs_base__u64__min(v_avail, (((uint64_t)(1u)) << self->private_impl.f_lgwin));
v_is_last = ((v_avail == v_block_size) && (v_op == 2u));
v_force_flush = ((v_avail == v_block_size) && (v_op == 1u));
if (v_force_flush && (v_block_size == 0u)) {
self->private_impl.f_stream_state = 1u;
continue;
}
v_storage_len = ((v_block_size * 2u) + 503u + 16u);
if ((v_storage_len > ((uint64_t)(a_workbuf.len))) || (((uint64_t)(a_workbuf.len)) < 2u)) {
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
goto exit;
}
a_workbuf.ptr[0u] = ((uint8_t)(self->private_impl.f_last_bytes));
a_workbuf.ptr[1u] = ((uint8_t)((self->private_impl.f_last_bytes >> 8u)));
self->private_impl.f_bw_ix = ((uint64_t)(self->private_impl.f_last_bytes_bits));
v_table_bits = wuffs_brotli__encoder__get_hash_table(self, v_block_size);
v_mark = ((uint64_t)(iop_a_src - io0_a_src));
self->private_data.s_transform_io.scratch = v_block_size;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
if (self->private_data.s_transform_io.scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
self->private_data.s_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_transform_io.scratch;
if (v_storage_len > ((uint64_t)(a_workbuf.len))) {
status = wuffs_base__make_status(wuffs_brotli__error__internal_error_inconsistent_encoder_state);
goto exit;
}
if (self->private_impl.f_quality == 0u) {
wuffs_brotli__encoder__compress_fragment_fast(self,
wuffs_private_impl__io__since(v_mark, ((uint64_t)(iop_a_src - io0_a_src)), io0_a_src),
v_is_last,
v_table_bits,
wuffs_base__slice_u8__subslice_j(a_workbuf, v_storage_len));
} else {
wuffs_brotli__encoder__compress_fragment_two_pass(self,
wuffs_private_impl__io__since(v_mark, ((uint64_t)(iop_a_src - io0_a_src)), io0_a_src),
v_is_last,
v_table_bits,
wuffs_base__slice_u8__subslice_j(a_workbuf, v_storage_len));
}
self->private_impl.f_out_in_tiny_buf = false;
self->private_impl.f_out_pos = 0u;
self->private_impl.f_out_len = (self->private_impl.f_bw_ix >> 3u);
v_ix = (self->private_impl.f_bw_ix >> 3u);
if (v_ix < ((uint64_t)(a_workbuf.len))) {
self->private_impl.f_last_bytes = ((uint32_t)(a_workbuf.ptr[v_ix]));
} else {
self->private_impl.f_last_bytes = 0u;
}
self->private_impl.f_last_bytes_bits = ((uint32_t)((self->private_impl.f_bw_ix & 7u)));
if (v_force_flush) {
self->private_impl.f_stream_state = 1u;
}
if (v_is_last) {
self->private_impl.f_stream_state = 2u;
}
continue;
}
break;
}
wuffs_brotli__encoder__check_flush_complete(self);
if (self->private_impl.f_out_len != 0u) {
status = wuffs_base__make_status(wuffs_base__suspension__short_write);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(5);
} else if (v_op == 1u) {
self->private_impl.f_flush_requested = false;
} else if (self->private_impl.f_stream_state == 2u) {
status = wuffs_base__make_status(NULL);
goto ok;
} else if (v_op == 0u) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(6);
} else {
status = wuffs_base__make_status(wuffs_brotli__error__internal_error_inconsistent_encoder_state);
goto exit;
}
}
ok:
self->private_impl.p_transform_io = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_transform_io = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 1 : 0;
self->private_data.s_transform_io.v_op = v_op;
self->private_data.s_transform_io.v_is_last = v_is_last;
self->private_data.s_transform_io.v_force_flush = v_force_flush;
self->private_data.s_transform_io.v_storage_len = v_storage_len;
self->private_data.s_transform_io.v_table_bits = v_table_bits;
self->private_data.s_transform_io.v_mark = v_mark;
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));
}
if (wuffs_base__status__is_error(&status)) {
self->private_impl.magic = WUFFS_BASE__DISABLED;
}
return status;
}
// -------- func brotli.encoder.set_depth
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__set_depth(
wuffs_brotli__encoder* self,
uint32_t a_p0,
wuffs_base__slice_u8 a_depth,
uint32_t a_max_depth) {
uint32_t v_stack[16] = {0};
uint32_t v_level = 0;
uint32_t v_p = 0;
uint64_t v_v = 0;
v_stack[0u] = 4294967295u;
v_p = a_p0;
while (true) {
if (self->private_data.f_tree_left[(v_p & 1023u)] != 65535u) {
if ((v_level >= a_max_depth) || (v_level >= 15u)) {
return false;
}
v_level += 1u;
v_stack[v_level] = ((uint32_t)(self->private_data.f_tree_right[(v_p & 1023u)]));
v_p = ((uint32_t)(self->private_data.f_tree_left[(v_p & 1023u)]));
continue;
}
v_v = ((uint64_t)(self->private_data.f_tree_right[(v_p & 1023u)]));
if (v_v < ((uint64_t)(a_depth.len))) {
a_depth.ptr[v_v] = ((uint8_t)(v_level));
}
while (v_stack[v_level] == 4294967295u) {
if (v_level == 0u) {
return true;
}
v_level -= 1u;
}
v_p = v_stack[v_level];
v_stack[v_level] = 4294967295u;
}
}
// -------- func brotli.encoder.huffman_tree_less
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__huffman_tree_less(
const wuffs_brotli__encoder* self,
uint32_t a_c0,
uint16_t a_r0,
uint32_t a_c1,
uint16_t a_r1,
bool a_tie_break) {
if (a_c0 != a_c1) {
return (a_c0 < a_c1);
}
return (a_tie_break && (a_r0 > a_r1));
}
// -------- func brotli.encoder.sort_huffman_tree_items
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__sort_huffman_tree_items(
wuffs_brotli__encoder* self,
uint32_t a_n,
bool a_tie_break) {
uint32_t v_i = 0;
uint32_t v_j = 0;
uint32_t v_k = 0;
uint32_t v_g = 0;
uint32_t v_gap = 0;
uint32_t v_tc = 0;
uint16_t v_tl = 0;
uint16_t v_tr = 0;
if (a_n < 13u) {
v_i = 1u;
while (v_i < a_n) {
v_tc = self->private_data.f_tree_count[(v_i & 1023u)];
v_tl = self->private_data.f_tree_left[(v_i & 1023u)];
v_tr = self->private_data.f_tree_right[(v_i & 1023u)];
v_k = v_i;
v_j = ((uint32_t)(v_i - 1u));
while (wuffs_brotli__encoder__huffman_tree_less(self,
v_tc,
v_tr,
self->private_data.f_tree_count[(v_j & 1023u)],
self->private_data.f_tree_right[(v_j & 1023u)],
a_tie_break)) {
self->private_data.f_tree_count[(v_k & 1023u)] = self->private_data.f_tree_count[(v_j & 1023u)];
self->private_data.f_tree_left[(v_k & 1023u)] = self->private_data.f_tree_left[(v_j & 1023u)];
self->private_data.f_tree_right[(v_k & 1023u)] = self->private_data.f_tree_right[(v_j & 1023u)];
v_k = v_j;
if (v_j == 0u) {
break;
}
v_j -= 1u;
}
self->private_data.f_tree_count[(v_k & 1023u)] = v_tc;
self->private_data.f_tree_left[(v_k & 1023u)] = v_tl;
self->private_data.f_tree_right[(v_k & 1023u)] = v_tr;
v_i += 1u;
}
return wuffs_base__make_empty_struct();
}
v_g = 0u;
if (a_n < 57u) {
v_g = 2u;
}
while (v_g < 6u) {
v_gap = WUFFS_BROTLI__SHELL_GAPS[v_g];
v_i = v_gap;
while (v_i < a_n) {
v_j = v_i;
v_tc = self->private_data.f_tree_count[(v_i & 1023u)];
v_tl = self->private_data.f_tree_left[(v_i & 1023u)];
v_tr = self->private_data.f_tree_right[(v_i & 1023u)];
while (v_j >= v_gap) {
if ( ! wuffs_brotli__encoder__huffman_tree_less(self,
v_tc,
v_tr,
self->private_data.f_tree_count[(((uint32_t)(v_j - v_gap)) & 1023u)],
self->private_data.f_tree_right[(((uint32_t)(v_j - v_gap)) & 1023u)],
a_tie_break)) {
break;
}
self->private_data.f_tree_count[(v_j & 1023u)] = self->private_data.f_tree_count[(((uint32_t)(v_j - v_gap)) & 1023u)];
self->private_data.f_tree_left[(v_j & 1023u)] = self->private_data.f_tree_left[(((uint32_t)(v_j - v_gap)) & 1023u)];
self->private_data.f_tree_right[(v_j & 1023u)] = self->private_data.f_tree_right[(((uint32_t)(v_j - v_gap)) & 1023u)];
v_j -= v_gap;
}
self->private_data.f_tree_count[(v_j & 1023u)] = v_tc;
self->private_data.f_tree_left[(v_j & 1023u)] = v_tl;
self->private_data.f_tree_right[(v_j & 1023u)] = v_tr;
v_i += 1u;
}
v_g += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.set_sentinel
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__set_sentinel(
wuffs_brotli__encoder* self,
uint32_t a_i) {
self->private_data.f_tree_count[(a_i & 1023u)] = 4294967295u;
self->private_data.f_tree_left[(a_i & 1023u)] = 65535u;
self->private_data.f_tree_right[(a_i & 1023u)] = 65535u;
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.merge_huffman_nodes
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__merge_huffman_nodes(
wuffs_brotli__encoder* self,
uint32_t a_n) {
uint32_t v_i = 0;
uint32_t v_j = 0;
uint32_t v_k = 0;
uint32_t v_left = 0;
uint32_t v_right = 0;
uint32_t v_j_end = 0;
wuffs_brotli__encoder__set_sentinel(self, a_n);
wuffs_brotli__encoder__set_sentinel(self, ((uint32_t)(a_n + 1u)));
v_i = 0u;
v_j = ((uint32_t)(a_n + 1u));
v_k = ((uint32_t)(a_n - 1u));
while (v_k != 0u) {
if (self->private_data.f_tree_count[(v_i & 1023u)] <= self->private_data.f_tree_count[(v_j & 1023u)]) {
v_left = v_i;
v_i += 1u;
} else {
v_left = v_j;
v_j += 1u;
}
if (self->private_data.f_tree_count[(v_i & 1023u)] <= self->private_data.f_tree_count[(v_j & 1023u)]) {
v_right = v_i;
v_i += 1u;
} else {
v_right = v_j;
v_j += 1u;
}
v_j_end = ((uint32_t)(((uint32_t)(a_n * 2u)) - v_k));
self->private_data.f_tree_count[(v_j_end & 1023u)] = ((uint32_t)(self->private_data.f_tree_count[(v_left & 1023u)] + self->private_data.f_tree_count[(v_right & 1023u)]));
self->private_data.f_tree_left[(v_j_end & 1023u)] = ((uint16_t)(v_left));
self->private_data.f_tree_right[(v_j_end & 1023u)] = ((uint16_t)(v_right));
wuffs_brotli__encoder__set_sentinel(self, ((uint32_t)(v_j_end + 1u)));
v_k -= 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.create_huffman_tree
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__create_huffman_tree(
wuffs_brotli__encoder* self,
uint32_t a_src,
uint32_t a_off,
uint32_t a_length,
uint32_t a_tree_limit,
wuffs_base__slice_u8 a_depth) {
uint32_t v_count_limit = 0;
uint32_t v_n = 0;
uint64_t v_i = 0;
uint32_t v_count = 0;
uint64_t v_v = 0;
bool v_depth_ok = false;
v_count_limit = 1u;
while (true) {
v_n = 0u;
v_i = ((uint64_t)(a_length));
while (v_i > 0u) {
v_i -= 1u;
if (a_src == 0u) {
v_count = self->private_data.f_cmd_histo[(((uint64_t)(((uint64_t)(a_off)) + v_i)) & 127u)];
} else {
v_count = self->private_data.f_cl_histo[(((uint64_t)(((uint64_t)(a_off)) + v_i)) & 31u)];
}
if (v_count != 0u) {
self->private_data.f_tree_count[(v_n & 1023u)] = wuffs_base__u32__max(v_count, v_count_limit);
self->private_data.f_tree_left[(v_n & 1023u)] = 65535u;
self->private_data.f_tree_right[(v_n & 1023u)] = ((uint16_t)(v_i));
v_n += 1u;
}
}
if (v_n == 1u) {
v_v = ((uint64_t)(self->private_data.f_tree_right[0u]));
if (v_v < ((uint64_t)(a_depth.len))) {
a_depth.ptr[v_v] = 1u;
}
break;
} else if (v_n == 0u) {
break;
}
wuffs_brotli__encoder__sort_huffman_tree_items(self, v_n, true);
wuffs_brotli__encoder__merge_huffman_nodes(self, v_n);
v_depth_ok = wuffs_brotli__encoder__set_depth(self, ((uint32_t)(((uint32_t)(v_n * 2u)) - 1u)), a_depth, a_tree_limit);
if (v_depth_ok) {
break;
}
v_count_limit *= 2u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.reverse_bits
WUFFS_BASE__GENERATED_C_CODE
static uint16_t
wuffs_brotli__encoder__reverse_bits(
const wuffs_brotli__encoder* self,
uint32_t a_num_bits,
uint16_t a_bits) {
uint32_t v_retval = 0;
uint32_t v_bits = 0;
uint32_t v_i = 0;
v_bits = ((uint32_t)(a_bits));
v_retval = WUFFS_BROTLI__REVERSE_BITS_LUT[(v_bits & 15u)];
v_i = 4u;
while (v_i < a_num_bits) {
v_retval = (((uint32_t)(v_retval << 4u)) | WUFFS_BROTLI__REVERSE_BITS_LUT[((v_bits >> 4u) & 15u)]);
v_bits >>= 4u;
v_i += 4u;
}
v_retval >>= (((uint32_t)(0u - a_num_bits)) & 3u);
return ((uint16_t)(v_retval));
}
// -------- func brotli.encoder.convert_bit_depths_to_symbols
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__convert_bit_depths_to_symbols(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_depth,
uint32_t a_len,
uint32_t a_dst) {
uint16_t v_bl_count[16] = {0};
uint16_t v_next_code[16] = {0};
uint64_t v_i = 0;
uint32_t v_code = 0;
uint32_t v_d = 0;
uint16_t v_r = 0;
v_i = 0u;
while (v_i < ((uint64_t)(a_len))) {
if (v_i < ((uint64_t)(a_depth.len))) {
v_d = ((uint32_t)(((uint8_t)(a_depth.ptr[v_i] & 15u))));
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
v_bl_count[v_d] += 1u;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
}
v_i += 1u;
}
v_bl_count[0u] = 0u;
v_next_code[0u] = 0u;
v_i = 1u;
while (v_i < 16u) {
v_code = ((uint32_t)(((uint32_t)(v_code + ((uint32_t)(v_bl_count[(((uint64_t)(v_i - 1u)) & 15u)])))) << 1u));
v_next_code[v_i] = ((uint16_t)(v_code));
v_i += 1u;
}
v_i = 0u;
while (v_i < ((uint64_t)(a_len))) {
if (v_i < ((uint64_t)(a_depth.len))) {
v_d = ((uint32_t)(((uint8_t)(a_depth.ptr[v_i] & 15u))));
if (v_d != 0u) {
v_r = wuffs_brotli__encoder__reverse_bits(self, v_d, v_next_code[v_d]);
if (a_dst == 0u) {
self->private_data.f_tmp_bits[(v_i & 63u)] = v_r;
} else if (a_dst == 1u) {
self->private_data.f_cmd_bits[(((uint64_t)(v_i + 64u)) & 127u)] = v_r;
} else if (a_dst == 2u) {
self->private_data.f_cl_bits[(v_i & 31u)] = v_r;
} else {
self->private_data.f_lit_bits[(v_i & 255u)] = v_r;
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
v_next_code[v_d] += 1u;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
}
}
v_i += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.reverse_huffman_tree
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__reverse_huffman_tree(
wuffs_brotli__encoder* self,
uint32_t a_start,
uint32_t a_end) {
uint32_t v_start = 0;
uint32_t v_end = 0;
uint8_t v_tmp = 0;
v_start = a_start;
v_end = ((uint32_t)(a_end - 1u));
while (v_start < v_end) {
v_tmp = self->private_data.f_huffman_tree[(v_start & 1023u)];
self->private_data.f_huffman_tree[(v_start & 1023u)] = self->private_data.f_huffman_tree[(v_end & 1023u)];
self->private_data.f_huffman_tree[(v_end & 1023u)] = v_tmp;
v_tmp = self->private_data.f_huffman_tree_extra_bits[(v_start & 1023u)];
self->private_data.f_huffman_tree_extra_bits[(v_start & 1023u)] = self->private_data.f_huffman_tree_extra_bits[(v_end & 1023u)];
self->private_data.f_huffman_tree_extra_bits[(v_end & 1023u)] = v_tmp;
v_start += 1u;
v_end -= 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.push_huffman_tree
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__push_huffman_tree(
wuffs_brotli__encoder* self,
uint8_t a_value,
uint8_t a_extra_bits) {
self->private_data.f_huffman_tree[(self->private_impl.f_huffman_tree_size & 1023u)] = a_value;
self->private_data.f_huffman_tree_extra_bits[(self->private_impl.f_huffman_tree_size & 1023u)] = a_extra_bits;
self->private_impl.f_huffman_tree_size += 1u;
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.write_huffman_tree_repetitions
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__write_huffman_tree_repetitions(
wuffs_brotli__encoder* self,
uint8_t a_previous_value,
uint8_t a_value,
uint32_t a_repetitions) {
uint32_t v_repetitions = 0;
uint32_t v_start = 0;
v_repetitions = a_repetitions;
if (a_previous_value != a_value) {
wuffs_brotli__encoder__push_huffman_tree(self, a_value, 0u);
v_repetitions -= 1u;
}
if (v_repetitions == 7u) {
wuffs_brotli__encoder__push_huffman_tree(self, a_value, 0u);
v_repetitions -= 1u;
}
if (v_repetitions < 3u) {
while (v_repetitions > 0u) {
wuffs_brotli__encoder__push_huffman_tree(self, a_value, 0u);
v_repetitions -= 1u;
}
} else {
v_start = self->private_impl.f_huffman_tree_size;
v_repetitions -= 3u;
while (true) {
wuffs_brotli__encoder__push_huffman_tree(self, 16u, ((uint8_t)((v_repetitions & 3u))));
v_repetitions >>= 2u;
if (v_repetitions == 0u) {
break;
}
v_repetitions -= 1u;
}
wuffs_brotli__encoder__reverse_huffman_tree(self, v_start, self->private_impl.f_huffman_tree_size);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.write_huffman_tree_repetitions_zeros
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__write_huffman_tree_repetitions_zeros(
wuffs_brotli__encoder* self,
uint32_t a_repetitions) {
uint32_t v_repetitions = 0;
uint32_t v_start = 0;
v_repetitions = a_repetitions;
if (v_repetitions == 11u) {
wuffs_brotli__encoder__push_huffman_tree(self, 0u, 0u);
v_repetitions -= 1u;
}
if (v_repetitions < 3u) {
while (v_repetitions > 0u) {
wuffs_brotli__encoder__push_huffman_tree(self, 0u, 0u);
v_repetitions -= 1u;
}
} else {
v_start = self->private_impl.f_huffman_tree_size;
v_repetitions -= 3u;
while (true) {
wuffs_brotli__encoder__push_huffman_tree(self, 17u, ((uint8_t)((v_repetitions & 7u))));
v_repetitions >>= 3u;
if (v_repetitions == 0u) {
break;
}
v_repetitions -= 1u;
}
wuffs_brotli__encoder__reverse_huffman_tree(self, v_start, self->private_impl.f_huffman_tree_size);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.decide_over_rle_use
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__decide_over_rle_use(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_depth,
uint32_t a_length) {
uint32_t v_total_reps_zero = 0;
uint32_t v_total_reps_non_zero = 0;
uint32_t v_count_reps_zero = 0;
uint32_t v_count_reps_non_zero = 0;
uint64_t v_i = 0;
uint64_t v_k = 0;
uint64_t v_length = 0;
uint32_t v_reps = 0;
uint8_t v_value = 0;
uint32_t v_ret = 0;
v_length = ((uint64_t)(a_length));
v_count_reps_zero = 1u;
v_count_reps_non_zero = 1u;
v_i = 0u;
while (v_i < v_length) {
if (v_i >= ((uint64_t)(a_depth.len))) {
break;
}
v_value = a_depth.ptr[v_i];
v_reps = 1u;
v_k = ((uint64_t)(v_i + 1u));
while ((v_k < v_length) && (v_k < ((uint64_t)(a_depth.len)))) {
if (a_depth.ptr[v_k] != v_value) {
break;
}
v_reps += 1u;
v_k += 1u;
}
if ((v_reps >= 3u) && (v_value == 0u)) {
v_total_reps_zero += v_reps;
v_count_reps_zero += 1u;
}
if ((v_reps >= 4u) && (v_value != 0u)) {
v_total_reps_non_zero += v_reps;
v_count_reps_non_zero += 1u;
}
v_i += ((uint64_t)(v_reps));
}
if (v_total_reps_non_zero > ((uint32_t)(v_count_reps_non_zero * 2u))) {
v_ret |= 1u;
}
if (v_total_reps_zero > ((uint32_t)(v_count_reps_zero * 2u))) {
v_ret |= 2u;
}
return v_ret;
}
// -------- func brotli.encoder.write_huffman_tree
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__write_huffman_tree(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_depth,
uint32_t a_length) {
uint8_t v_previous_value = 0;
uint64_t v_i = 0;
uint64_t v_k = 0;
uint64_t v_length = 0;
uint32_t v_reps = 0;
uint64_t v_new_length = 0;
uint32_t v_rle = 0;
uint8_t v_value = 0;
v_previous_value = 8u;
v_length = ((uint64_t)(a_length));
v_new_length = v_length;
v_i = 0u;
while (v_i < v_length) {
v_k = ((uint64_t)(((uint64_t)(v_length - v_i)) - 1u));
if (v_k >= ((uint64_t)(a_depth.len))) {
break;
}
if (a_depth.ptr[v_k] == 0u) {
v_new_length -= 1u;
} else {
break;
}
v_i += 1u;
}
if (v_length > 50u) {
v_rle = wuffs_brotli__encoder__decide_over_rle_use(self, a_depth, ((uint32_t)(v_new_length)));
}
v_i = 0u;
while (v_i < v_new_length) {
if (v_i >= ((uint64_t)(a_depth.len))) {
break;
}
v_value = a_depth.ptr[v_i];
v_reps = 1u;
if (((v_value != 0u) && ((v_rle & 1u) != 0u)) || ((v_value == 0u) && ((v_rle & 2u) != 0u))) {
v_k = ((uint64_t)(v_i + 1u));
while ((v_k < v_new_length) && (v_k < ((uint64_t)(a_depth.len)))) {
if (a_depth.ptr[v_k] != v_value) {
break;
}
v_reps += 1u;
v_k += 1u;
}
}
if (v_value == 0u) {
wuffs_brotli__encoder__write_huffman_tree_repetitions_zeros(self, v_reps);
} else {
wuffs_brotli__encoder__write_huffman_tree_repetitions(self, v_previous_value, v_value, v_reps);
v_previous_value = v_value;
}
v_i += ((uint64_t)(v_reps));
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.store_huffman_tree_of_huffman_tree_to_bit_mask
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_huffman_tree_of_huffman_tree_to_bit_mask(
wuffs_brotli__encoder* self,
uint32_t a_num_codes,
wuffs_base__slice_u8 a_s) {
uint32_t v_skip_some = 0;
uint32_t v_codes_to_store = 0;
uint32_t v_i = 0;
uint32_t v_l = 0;
v_codes_to_store = 18u;
if (a_num_codes > 1u) {
while (v_codes_to_store > 0u) {
if (self->private_data.f_cl_depth[WUFFS_BROTLI__STORAGE_ORDER[(v_codes_to_store - 1u)]] != 0u) {
break;
}
v_codes_to_store -= 1u;
}
}
if ((self->private_data.f_cl_depth[WUFFS_BROTLI__STORAGE_ORDER[0u]] == 0u) && (self->private_data.f_cl_depth[WUFFS_BROTLI__STORAGE_ORDER[1u]] == 0u)) {
v_skip_some = 2u;
if (self->private_data.f_cl_depth[WUFFS_BROTLI__STORAGE_ORDER[2u]] == 0u) {
v_skip_some = 3u;
}
}
wuffs_brotli__encoder__write_bits(self, 2u, ((uint64_t)(v_skip_some)), a_s);
v_i = v_skip_some;
while ((v_i < ((uint32_t)(v_codes_to_store))) && (v_i < 18u)) {
v_l = ((uint32_t)(self->private_data.f_cl_depth[WUFFS_BROTLI__STORAGE_ORDER[v_i]]));
if (v_l < 6u) {
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_BIT_LENGTHS[v_l], WUFFS_BROTLI__HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_SYMBOLS[v_l], a_s);
}
v_i += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.store_huffman_tree_to_bit_mask
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_huffman_tree_to_bit_mask(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s) {
uint32_t v_i = 0;
uint32_t v_ix = 0;
v_i = 0u;
while (v_i < self->private_impl.f_huffman_tree_size) {
v_ix = ((uint32_t)(((uint8_t)(self->private_data.f_huffman_tree[(v_i & 1023u)] & 31u))));
wuffs_brotli__encoder__write_bits(self, ((uint32_t)(self->private_data.f_cl_depth[v_ix])), ((uint64_t)(self->private_data.f_cl_bits[v_ix])), a_s);
if (v_ix == 16u) {
wuffs_brotli__encoder__write_bits(self, 2u, ((uint64_t)(self->private_data.f_huffman_tree_extra_bits[(v_i & 1023u)])), a_s);
} else if (v_ix == 17u) {
wuffs_brotli__encoder__write_bits(self, 3u, ((uint64_t)(self->private_data.f_huffman_tree_extra_bits[(v_i & 1023u)])), a_s);
}
v_i += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.store_huffman_tree
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_huffman_tree(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_depths,
uint32_t a_num,
wuffs_base__slice_u8 a_s) {
uint32_t v_i = 0;
uint32_t v_num_codes = 0;
uint32_t v_code = 0;
self->private_impl.f_huffman_tree_size = 0u;
wuffs_brotli__encoder__write_huffman_tree(self, a_depths, a_num);
v_i = 0u;
while (v_i < 32u) {
self->private_data.f_cl_histo[v_i] = 0u;
self->private_data.f_cl_depth[v_i] = 0u;
v_i += 1u;
}
v_i = 0u;
while (v_i < self->private_impl.f_huffman_tree_size) {
self->private_data.f_cl_histo[((uint8_t)(self->private_data.f_huffman_tree[(v_i & 1023u)] & 31u))] += 1u;
v_i += 1u;
}
v_i = 0u;
while (v_i < 18u) {
if (self->private_data.f_cl_histo[v_i] != 0u) {
if (v_num_codes == 0u) {
v_code = v_i;
v_num_codes = 1u;
} else if (v_num_codes == 1u) {
v_num_codes = 2u;
break;
}
}
v_i += 1u;
}
wuffs_brotli__encoder__create_huffman_tree(self,
1u,
0u,
18u,
5u,
wuffs_base__make_slice_u8(self->private_data.f_cl_depth, 32));
wuffs_brotli__encoder__convert_bit_depths_to_symbols(self, wuffs_base__make_slice_u8(self->private_data.f_cl_depth, 32), 18u, 2u);
wuffs_brotli__encoder__store_huffman_tree_of_huffman_tree_to_bit_mask(self, v_num_codes, a_s);
if (v_num_codes == 1u) {
self->private_data.f_cl_depth[(v_code & 31u)] = 0u;
}
wuffs_brotli__encoder__store_huffman_tree_to_bit_mask(self, a_s);
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.build_and_store_huffman_tree_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__build_and_store_huffman_tree_fast(
wuffs_brotli__encoder* self,
uint64_t a_histogram_total,
uint32_t a_max_bits,
wuffs_base__slice_u8 a_s) {
uint32_t v_count = 0;
uint32_t v_symbols[4] = {0};
uint32_t v_length = 0;
uint64_t v_total = 0;
uint32_t v_count_limit = 0;
uint32_t v_n = 0;
uint32_t v_l = 0;
uint32_t v_h = 0;
uint32_t v_i = 0;
uint32_t v_j = 0;
uint32_t v_tmp = 0;
bool v_depth_ok = false;
uint32_t v_value = 0;
uint32_t v_prev_value = 0;
uint32_t v_reps = 0;
uint32_t v_k = 0;
v_total = a_histogram_total;
while ((v_total != 0u) && (v_length < 256u)) {
v_h = self->private_data.f_histogram[v_length];
if (v_h != 0u) {
if (v_count < 4u) {
v_symbols[v_count] = v_length;
}
v_count += 1u;
v_total -= ((uint64_t)(v_h));
}
v_length += 1u;
}
if (v_count <= 1u) {
wuffs_brotli__encoder__write_bits(self, 4u, 1u, a_s);
wuffs_brotli__encoder__write_bits(self, a_max_bits, ((uint64_t)(v_symbols[0u])), a_s);
self->private_data.f_lit_depth[(v_symbols[0u] & 255u)] = 0u;
self->private_data.f_lit_bits[(v_symbols[0u] & 255u)] = 0u;
return wuffs_base__make_empty_struct();
}
v_i = 0u;
while (v_i < v_length) {
self->private_data.f_lit_depth[(v_i & 255u)] = 0u;
v_i += 1u;
}
v_count_limit = 1u;
while (true) {
v_n = 0u;
v_l = v_length;
while (v_l > 0u) {
v_l -= 1u;
v_h = self->private_data.f_histogram[(v_l & 255u)];
if (v_h != 0u) {
if (v_h >= v_count_limit) {
self->private_data.f_tree_count[(v_n & 1023u)] = v_h;
} else {
self->private_data.f_tree_count[(v_n & 1023u)] = v_count_limit;
}
self->private_data.f_tree_left[(v_n & 1023u)] = 65535u;
self->private_data.f_tree_right[(v_n & 1023u)] = ((uint16_t)(v_l));
v_n += 1u;
}
}
wuffs_brotli__encoder__sort_huffman_tree_items(self, v_n, false);
wuffs_brotli__encoder__merge_huffman_nodes(self, v_n);
v_depth_ok = wuffs_brotli__encoder__set_depth(self, ((uint32_t)(((uint32_t)(v_n * 2u)) - 1u)), wuffs_base__make_slice_u8(self->private_data.f_lit_depth, 256), 14u);
if (v_depth_ok) {
break;
}
v_count_limit *= 2u;
}
wuffs_brotli__encoder__convert_bit_depths_to_symbols(self, wuffs_base__make_slice_u8(self->private_data.f_lit_depth, 256), v_length, 3u);
if (v_count <= 4u) {
wuffs_brotli__encoder__write_bits(self, 2u, 1u, a_s);
wuffs_brotli__encoder__write_bits(self, 2u, ((uint64_t)(((uint32_t)(v_count - 1u)))), a_s);
v_i = 0u;
while (v_i < v_count) {
v_j = ((uint32_t)(v_i + 1u));
while (v_j < v_count) {
if (self->private_data.f_lit_depth[(v_symbols[(v_j & 3u)] & 255u)] < self->private_data.f_lit_depth[(v_symbols[(v_i & 3u)] & 255u)]) {
v_tmp = v_symbols[(v_j & 3u)];
v_symbols[(v_j & 3u)] = v_symbols[(v_i & 3u)];
v_symbols[(v_i & 3u)] = v_tmp;
}
v_j += 1u;
}
v_i += 1u;
}
wuffs_brotli__encoder__write_bits(self, a_max_bits, ((uint64_t)(v_symbols[0u])), a_s);
wuffs_brotli__encoder__write_bits(self, a_max_bits, ((uint64_t)(v_symbols[1u])), a_s);
if (v_count >= 3u) {
wuffs_brotli__encoder__write_bits(self, a_max_bits, ((uint64_t)(v_symbols[2u])), a_s);
}
if (v_count >= 4u) {
wuffs_brotli__encoder__write_bits(self, a_max_bits, ((uint64_t)(v_symbols[3u])), a_s);
if (self->private_data.f_lit_depth[(v_symbols[0u] & 255u)] == 1u) {
wuffs_brotli__encoder__write_bits(self, 1u, 1u, a_s);
} else {
wuffs_brotli__encoder__write_bits(self, 1u, 0u, a_s);
}
}
} else {
wuffs_brotli__encoder__write_bits(self, 40u, 1096648316244u, a_s);
v_prev_value = 8u;
v_i = 0u;
while (v_i < v_length) {
v_value = ((uint32_t)(self->private_data.f_lit_depth[(v_i & 255u)]));
v_reps = 1u;
v_k = ((uint32_t)(v_i + 1u));
while (v_k < v_length) {
if (((uint32_t)(self->private_data.f_lit_depth[(v_k & 255u)])) != v_value) {
break;
}
v_reps += 1u;
v_k += 1u;
}
v_i += v_reps;
if (v_value == 0u) {
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__ZERO_REPS_DEPTH[(v_reps & 511u)], WUFFS_BROTLI__ZERO_REPS_BITS[(v_reps & 511u)], a_s);
} else {
if (v_prev_value != v_value) {
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__CODE_LENGTH_DEPTH[(v_value & 15u)], WUFFS_BROTLI__CODE_LENGTH_BITS[(v_value & 15u)], a_s);
v_reps -= 1u;
}
if (v_reps < 3u) {
while (v_reps != 0u) {
v_reps -= 1u;
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__CODE_LENGTH_DEPTH[(v_value & 15u)], WUFFS_BROTLI__CODE_LENGTH_BITS[(v_value & 15u)], a_s);
}
} else {
v_reps -= 3u;
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__NON_ZERO_REPS_DEPTH[(v_reps & 511u)], WUFFS_BROTLI__NON_ZERO_REPS_BITS[(v_reps & 511u)], a_s);
}
v_prev_value = v_value;
}
}
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.copy_tmp_bits_to_cmd_bits
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(
wuffs_brotli__encoder* self,
uint32_t a_dst,
uint32_t a_src,
uint32_t a_n) {
uint32_t v_i = 0;
v_i = 0u;
while (v_i < a_n) {
self->private_data.f_cmd_bits[(((uint32_t)(a_dst + v_i)) & 127u)] = self->private_data.f_tmp_bits[(((uint32_t)(a_src + v_i)) & 63u)];
v_i += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.hash_fast
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__hash_fast(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_i,
uint32_t a_shift) {
uint64_t v_h = 0;
v_h = ((uint64_t)(((uint64_t)(wuffs_brotli__encoder__load64le(self, a_input, a_i) << 24u)) * 506832829u));
return ((uint32_t)((v_h >> (a_shift & 63u))));
}
// -------- func brotli.encoder.hash_bytes_at_offset_fast
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__hash_bytes_at_offset_fast(
const wuffs_brotli__encoder* self,
uint64_t a_v,
uint32_t a_offset,
uint32_t a_shift) {
uint64_t v_h = 0;
v_h = ((uint64_t)(((uint64_t)((a_v >> (((uint32_t)(8u * a_offset)) & 63u)) << 24u)) * 506832829u));
return ((uint32_t)((v_h >> (a_shift & 63u))));
}
// -------- func brotli.encoder.is_match_fast
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__is_match_fast(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_i1,
uint64_t a_i2) {
if ((a_i1 >= ((uint64_t)(a_input.len))) || (a_i2 >= ((uint64_t)(a_input.len)))) {
return false;
}
if (wuffs_brotli__encoder__load32le(self, a_input, a_i1) != wuffs_brotli__encoder__load32le(self, a_input, a_i2)) {
return false;
}
if ((((uint64_t)(a_i1 + 4u)) >= ((uint64_t)(a_input.len))) || (((uint64_t)(a_i2 + 4u)) >= ((uint64_t)(a_input.len)))) {
return false;
}
return (a_input.ptr[((uint64_t)(a_i1 + 4u))] == a_input.ptr[((uint64_t)(a_i2 + 4u))]);
}
// -------- func brotli.encoder.build_and_store_literal_prefix_code
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__build_and_store_literal_prefix_code(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_input_size,
wuffs_base__slice_u8 a_s) {
uint64_t v_histogram_total = 0;
uint64_t v_i = 0;
uint32_t v_j = 0;
uint32_t v_adjust = 0;
uint64_t v_literal_ratio = 0;
uint32_t v_h = 0;
v_j = 0u;
while (v_j < 256u) {
self->private_data.f_histogram[v_j] = 0u;
v_j += 1u;
}
if (a_input_size < 32768u) {
v_i = 0u;
while (v_i < a_input_size) {
if (((uint64_t)(a_from + v_i)) < ((uint64_t)(a_input.len))) {
self->private_data.f_histogram[a_input.ptr[((uint64_t)(a_from + v_i))]] += 1u;
}
v_i += 1u;
}
v_histogram_total = a_input_size;
v_j = 0u;
while (v_j < 256u) {
v_adjust = (2u * wuffs_base__u32__min(self->private_data.f_histogram[v_j], 11u));
self->private_data.f_histogram[v_j] += v_adjust;
v_histogram_total += ((uint64_t)(v_adjust));
v_j += 1u;
}
} else {
v_i = 0u;
while (v_i < a_input_size) {
if (((uint64_t)(a_from + v_i)) < ((uint64_t)(a_input.len))) {
self->private_data.f_histogram[a_input.ptr[((uint64_t)(a_from + v_i))]] += 1u;
}
v_i += 29u;
}
v_histogram_total = (((uint64_t)(a_input_size + 28u)) / 29u);
v_j = 0u;
while (v_j < 256u) {
v_adjust = (1u + (2u * wuffs_base__u32__min(self->private_data.f_histogram[v_j], 11u)));
self->private_data.f_histogram[v_j] += v_adjust;
v_histogram_total += ((uint64_t)(v_adjust));
v_j += 1u;
}
}
wuffs_brotli__encoder__build_and_store_huffman_tree_fast(self, v_histogram_total, 8u, a_s);
v_j = 0u;
while (v_j < 256u) {
v_h = self->private_data.f_histogram[v_j];
if (v_h != 0u) {
v_literal_ratio += ((uint64_t)(((uint32_t)(v_h * ((uint32_t)(self->private_data.f_lit_depth[v_j]))))));
}
v_j += 1u;
}
if (v_histogram_total == 0u) {
return 0u;
}
return (((uint64_t)(v_literal_ratio * 125u)) / v_histogram_total);
}
// -------- func brotli.encoder.build_and_store_command_prefix_code_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__build_and_store_command_prefix_code_fast(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s) {
uint32_t v_i = 0;
wuffs_private_impl__bulk_memset(&self->private_data.f_tmp_depth[0], 704u, 0u);
wuffs_brotli__encoder__create_huffman_tree(self,
0u,
0u,
64u,
15u,
wuffs_base__make_slice_u8(self->private_data.f_cmd_depth, 64));
wuffs_brotli__encoder__create_huffman_tree(self,
0u,
64u,
64u,
14u,
wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 64, 128));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 0, 24), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 0, 24));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 24, 32), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 40, 48));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 32, 40), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 24, 32));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 40, 48), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 48, 56));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 48, 56), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 32, 40));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 56, 64), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 56, 64));
wuffs_brotli__encoder__convert_bit_depths_to_symbols(self, wuffs_base__make_slice_u8(self->private_data.f_tmp_depth, 704), 64u, 0u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 0u, 0u, 24u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 24u, 32u, 8u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 32u, 48u, 8u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 40u, 24u, 8u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 48u, 40u, 8u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 56u, 56u, 8u);
wuffs_brotli__encoder__convert_bit_depths_to_symbols(self, wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 64, 128), 64u, 1u);
wuffs_private_impl__bulk_memset(&self->private_data.f_tmp_depth[0], 64u, 0u);
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 0, 8), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 0, 8));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 64, 72), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 8, 16));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 128, 136), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 16, 24));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 192, 200), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 24, 32));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 384, 392), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 32, 40));
v_i = 0u;
while (v_i < 8u) {
self->private_data.f_tmp_depth[(128u + (8u * v_i))] = self->private_data.f_cmd_depth[(40u + v_i)];
self->private_data.f_tmp_depth[(256u + (8u * v_i))] = self->private_data.f_cmd_depth[(48u + v_i)];
self->private_data.f_tmp_depth[(448u + (8u * v_i))] = self->private_data.f_cmd_depth[(56u + v_i)];
v_i += 1u;
}
wuffs_brotli__encoder__store_huffman_tree(self, wuffs_base__make_slice_u8(self->private_data.f_tmp_depth, 704), 704u, a_s);
wuffs_brotli__encoder__store_huffman_tree(self, wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 64, 128), 64u, a_s);
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_cmd_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_cmd_fast(
wuffs_brotli__encoder* self,
uint64_t a_code,
wuffs_base__slice_u8 a_s) {
wuffs_brotli__encoder__write_bits(self, ((uint32_t)(self->private_data.f_cmd_depth[(a_code & 127u)])), ((uint64_t)(self->private_data.f_cmd_bits[(a_code & 127u)])), a_s);
self->private_data.f_cmd_histo[(a_code & 127u)] += 1u;
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_insert_len_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_insert_len_fast(
wuffs_brotli__encoder* self,
uint64_t a_insertlen,
wuffs_base__slice_u8 a_s) {
uint64_t v_tail = 0;
uint32_t v_nbits = 0;
uint64_t v_prefix = 0;
if (a_insertlen < 6u) {
wuffs_brotli__encoder__emit_cmd_fast(self, (a_insertlen + 40u), a_s);
} else if (a_insertlen < 130u) {
v_tail = (a_insertlen - 2u);
v_nbits = ((uint32_t)(wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail) - 1u));
v_prefix = (v_tail >> (v_nbits & 63u));
wuffs_brotli__encoder__emit_cmd_fast(self, ((uint64_t)(((uint64_t)(((uint64_t)(((uint64_t)(v_nbits)) << 1u)) + v_prefix)) + 42u)), a_s);
wuffs_brotli__encoder__write_bits(self, v_nbits, ((uint64_t)(v_tail - ((uint64_t)(v_prefix << (v_nbits & 63u))))), a_s);
} else if (a_insertlen < 2114u) {
v_tail = (a_insertlen - 66u);
v_nbits = wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail);
wuffs_brotli__encoder__emit_cmd_fast(self, (((uint64_t)(v_nbits)) + 50u), a_s);
wuffs_brotli__encoder__write_bits(self, v_nbits, ((uint64_t)(v_tail - (((uint64_t)(1u)) << v_nbits))), a_s);
} else {
wuffs_brotli__encoder__emit_cmd_fast(self, 61u, a_s);
wuffs_brotli__encoder__write_bits(self, 12u, ((uint64_t)(a_insertlen - 2114u)), a_s);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_long_insert_len_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_long_insert_len_fast(
wuffs_brotli__encoder* self,
uint64_t a_insertlen,
wuffs_base__slice_u8 a_s) {
if (a_insertlen < 22594u) {
wuffs_brotli__encoder__emit_cmd_fast(self, 62u, a_s);
wuffs_brotli__encoder__write_bits(self, 14u, ((uint64_t)(a_insertlen - 6210u)), a_s);
} else {
wuffs_brotli__encoder__emit_cmd_fast(self, 63u, a_s);
wuffs_brotli__encoder__write_bits(self, 24u, ((uint64_t)(a_insertlen - 22594u)), a_s);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_copy_len_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_copy_len_fast(
wuffs_brotli__encoder* self,
uint64_t a_copylen,
wuffs_base__slice_u8 a_s) {
uint64_t v_tail = 0;
uint32_t v_nbits = 0;
uint64_t v_prefix = 0;
if (a_copylen < 10u) {
wuffs_brotli__encoder__emit_cmd_fast(self, (a_copylen + 14u), a_s);
} else if (a_copylen < 134u) {
v_tail = (a_copylen - 6u);
v_nbits = ((uint32_t)(wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail) - 1u));
v_prefix = (v_tail >> (v_nbits & 63u));
wuffs_brotli__encoder__emit_cmd_fast(self, ((uint64_t)(((uint64_t)(((uint64_t)(((uint64_t)(v_nbits)) << 1u)) + v_prefix)) + 20u)), a_s);
wuffs_brotli__encoder__write_bits(self, v_nbits, ((uint64_t)(v_tail - ((uint64_t)(v_prefix << (v_nbits & 63u))))), a_s);
} else if (a_copylen < 2118u) {
v_tail = (a_copylen - 70u);
v_nbits = wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail);
wuffs_brotli__encoder__emit_cmd_fast(self, (((uint64_t)(v_nbits)) + 28u), a_s);
wuffs_brotli__encoder__write_bits(self, v_nbits, ((uint64_t)(v_tail - (((uint64_t)(1u)) << v_nbits))), a_s);
} else {
wuffs_brotli__encoder__emit_cmd_fast(self, 39u, a_s);
wuffs_brotli__encoder__write_bits(self, 24u, ((uint64_t)(a_copylen - 2118u)), a_s);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_copy_len_last_distance_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_copy_len_last_distance_fast(
wuffs_brotli__encoder* self,
uint64_t a_copylen,
wuffs_base__slice_u8 a_s) {
uint64_t v_tail = 0;
uint32_t v_nbits = 0;
uint64_t v_prefix = 0;
if (a_copylen < 12u) {
wuffs_brotli__encoder__emit_cmd_fast(self, ((uint64_t)(a_copylen - 4u)), a_s);
} else if (a_copylen < 72u) {
v_tail = (a_copylen - 8u);
v_nbits = ((uint32_t)(wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail) - 1u));
v_prefix = (v_tail >> (v_nbits & 63u));
wuffs_brotli__encoder__emit_cmd_fast(self, ((uint64_t)(((uint64_t)(((uint64_t)(((uint64_t)(v_nbits)) << 1u)) + v_prefix)) + 4u)), a_s);
wuffs_brotli__encoder__write_bits(self, v_nbits, ((uint64_t)(v_tail - ((uint64_t)(v_prefix << (v_nbits & 63u))))), a_s);
} else if (a_copylen < 136u) {
v_tail = (a_copylen - 8u);
wuffs_brotli__encoder__emit_cmd_fast(self, ((v_tail >> 5u) + 30u), a_s);
wuffs_brotli__encoder__write_bits(self, 5u, (v_tail & 31u), a_s);
wuffs_brotli__encoder__emit_cmd_fast(self, 64u, a_s);
} else if (a_copylen < 2120u) {
v_tail = (a_copylen - 72u);
v_nbits = wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail);
wuffs_brotli__encoder__emit_cmd_fast(self, (((uint64_t)(v_nbits)) + 28u), a_s);
wuffs_brotli__encoder__write_bits(self, v_nbits, ((uint64_t)(v_tail - (((uint64_t)(1u)) << v_nbits))), a_s);
wuffs_brotli__encoder__emit_cmd_fast(self, 64u, a_s);
} else {
wuffs_brotli__encoder__emit_cmd_fast(self, 39u, a_s);
wuffs_brotli__encoder__write_bits(self, 24u, ((uint64_t)(a_copylen - 2120u)), a_s);
wuffs_brotli__encoder__emit_cmd_fast(self, 64u, a_s);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_distance_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_distance_fast(
wuffs_brotli__encoder* self,
uint64_t a_distance,
wuffs_base__slice_u8 a_s) {
uint64_t v_d = 0;
uint32_t v_nbits = 0;
uint64_t v_prefix = 0;
uint64_t v_offset = 0;
v_d = ((uint64_t)(a_distance + 3u));
v_nbits = ((uint32_t)(wuffs_brotli__encoder__log2_floor_non_zero(self, v_d) - 1u));
v_prefix = ((v_d >> (v_nbits & 63u)) & 1u);
v_offset = ((uint64_t)((2u + v_prefix) << (v_nbits & 63u)));
wuffs_brotli__encoder__emit_cmd_fast(self, ((uint64_t)(((uint64_t)((2u * ((uint64_t)(((uint32_t)(v_nbits - 1u))))) + v_prefix)) + 80u)), a_s);
wuffs_brotli__encoder__write_bits(self, v_nbits, ((uint64_t)(v_d - v_offset)), a_s);
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_literals
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_literals(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_len,
wuffs_base__slice_u8 a_s) {
uint64_t v_j = 0;
uint8_t v_lit = 0;
v_j = 0u;
while (v_j < a_len) {
if (((uint64_t)(a_from + v_j)) >= ((uint64_t)(a_input.len))) {
break;
}
v_lit = a_input.ptr[((uint64_t)(a_from + v_j))];
wuffs_brotli__encoder__write_bits(self, ((uint32_t)(self->private_data.f_lit_depth[v_lit])), ((uint64_t)(self->private_data.f_lit_bits[v_lit])), a_s);
v_j += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.should_merge_block
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__should_merge_block(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_len) {
uint64_t v_i = 0;
uint32_t v_j = 0;
uint64_t v_total = 0;
uint64_t v_r = 0;
uint64_t v_h = 0;
v_j = 0u;
while (v_j < 256u) {
self->private_data.f_histogram[v_j] = 0u;
v_j += 1u;
}
v_i = 0u;
while (v_i < a_len) {
if (((uint64_t)(a_from + v_i)) < ((uint64_t)(a_input.len))) {
self->private_data.f_histogram[a_input.ptr[((uint64_t)(a_from + v_i))]] += 1u;
}
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_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_j += 1u;
}
return (wuffs_softfloat__arith__f64_ge(&self->private_data.f_sf, v_r, 0u) != 0u);
}
// -------- func brotli.encoder.should_use_uncompressed_mode
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__should_use_uncompressed_mode(
const wuffs_brotli__encoder* self,
uint64_t a_metablock_start,
uint64_t a_next_emit,
uint64_t a_insertlen,
uint64_t a_literal_ratio) {
uint64_t v_compressed = 0;
v_compressed = ((uint64_t)(a_next_emit - a_metablock_start));
if (((uint64_t)(v_compressed * 50u)) > a_insertlen) {
return false;
}
return (a_literal_ratio > 980u);
}
// -------- func brotli.encoder.emit_uncompressed_meta_block_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_uncompressed_meta_block_fast(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_begin,
uint64_t a_end,
uint64_t a_storage_ix_start,
wuffs_base__slice_u8 a_s) {
uint64_t v_len = 0;
uint64_t v_p = 0;
v_len = ((uint64_t)(a_end - a_begin));
wuffs_brotli__encoder__rewind_bit_position(self, a_storage_ix_start, a_s);
wuffs_brotli__encoder__store_meta_block_header(self, v_len, true, a_s);
wuffs_brotli__encoder__align_to_byte(self);
wuffs_brotli__encoder__copy_bytes(self,
a_input,
a_begin,
a_end,
a_s);
self->private_impl.f_bw_ix += ((uint64_t)(v_len << 3u));
v_p = (self->private_impl.f_bw_ix >> 3u);
if (v_p < ((uint64_t)(a_s.len))) {
a_s.ptr[v_p] = 0u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.copy_bytes
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__copy_bytes(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_begin,
uint64_t a_end,
wuffs_base__slice_u8 a_s) {
uint64_t v_p = 0;
v_p = (self->private_impl.f_bw_ix >> 3u);
if ((a_begin <= a_end) && (a_end <= ((uint64_t)(a_input.len))) && (v_p <= ((uint64_t)(a_s.len)))) {
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__slice_u8__subslice_i(a_s, v_p), wuffs_base__slice_u8__subslice_ij(a_input,
a_begin,
a_end));
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.update_table_fast
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__update_table_fast(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_ip,
uint32_t a_shift) {
uint64_t v_input_bytes = 0;
uint32_t v_prev_hash = 0;
uint32_t v_cur_hash = 0;
uint64_t v_candidate = 0;
v_input_bytes = wuffs_brotli__encoder__load64le(self, a_input, ((uint64_t)(a_ip - 3u)));
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_fast(self, v_input_bytes, 0u, a_shift);
v_cur_hash = wuffs_brotli__encoder__hash_bytes_at_offset_fast(self, v_input_bytes, 3u, a_shift);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 3u))));
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_fast(self, v_input_bytes, 1u, a_shift);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 2u))));
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_fast(self, v_input_bytes, 2u, a_shift);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 1u))));
v_candidate = ((uint64_t)(self->private_data.f_hash_table[(v_cur_hash & 131071u)]));
self->private_data.f_hash_table[(v_cur_hash & 131071u)] = ((uint32_t)(a_ip));
return v_candidate;
}
// -------- func brotli.encoder.compress_fragment_fast
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__compress_fragment_fast(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
bool a_is_last,
uint32_t a_table_bits,
wuffs_base__slice_u8 a_s) {
uint64_t v_initial_ix = 0;
uint64_t v_input_size = 0;
v_initial_ix = self->private_impl.f_bw_ix;
v_input_size = ((uint64_t)(a_input.len));
if (v_input_size == 0u) {
wuffs_brotli__encoder__write_bits(self, 1u, 1u, a_s);
wuffs_brotli__encoder__write_bits(self, 1u, 1u, a_s);
wuffs_brotli__encoder__align_to_byte(self);
return wuffs_base__make_empty_struct();
}
wuffs_brotli__encoder__compress_fragment_fast_impl(self,
a_input,
a_is_last,
a_table_bits,
a_s);
if (((uint64_t)(self->private_impl.f_bw_ix - v_initial_ix)) > ((uint64_t)(31u + ((uint64_t)(v_input_size << 3u))))) {
wuffs_brotli__encoder__emit_uncompressed_meta_block_fast(self,
a_input,
0u,
v_input_size,
v_initial_ix,
a_s);
}
if (a_is_last) {
wuffs_brotli__encoder__write_bits(self, 1u, 1u, a_s);
wuffs_brotli__encoder__write_bits(self, 1u, 1u, a_s);
wuffs_brotli__encoder__align_to_byte(self);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.compress_fragment_fast_impl
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__compress_fragment_fast_impl(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
bool a_is_last,
uint32_t a_table_bits,
wuffs_base__slice_u8 a_s) {
uint64_t v_in_off = 0;
uint64_t v_in_size = 0;
uint64_t v_next_emit = 0;
uint64_t v_metablock_start = 0;
uint64_t v_block_size = 0;
uint64_t v_total_block_size = 0;
uint64_t v_mlen_storage_ix = 0;
uint64_t v_literal_ratio = 0;
uint32_t v_shift = 0;
uint64_t v_i = 0;
uint64_t v_ip = 0;
uint64_t v_ip_end = 0;
uint64_t v_ip_limit = 0;
uint64_t v_len_limit = 0;
uint64_t v_next_ip = 0;
uint64_t v_candidate = 0;
uint64_t v_base_ip = 0;
uint64_t v_matched = 0;
uint64_t v_distance = 0;
uint64_t v_insert = 0;
uint64_t v_last_distance = 0;
uint32_t v_skip = 0;
uint32_t v_hash = 0;
uint32_t v_next_hash = 0;
bool v_goto_next_block = false;
bool v_merge = false;
uint64_t v_saved_ix = 0;
v_shift = ((uint32_t)(64u - a_table_bits));
v_in_size = ((uint64_t)(a_input.len));
v_block_size = wuffs_base__u64__min(v_in_size, 98304u);
v_total_block_size = v_block_size;
v_mlen_storage_ix = ((uint64_t)(self->private_impl.f_bw_ix + 3u));
wuffs_brotli__encoder__store_meta_block_header(self, v_block_size, false, a_s);
wuffs_brotli__encoder__write_bits(self, 13u, 0u, a_s);
v_literal_ratio = wuffs_brotli__encoder__build_and_store_literal_prefix_code(self,
a_input,
v_in_off,
v_block_size,
a_s);
v_i = 0u;
while (((uint64_t)(v_i + 7u)) < self->private_impl.f_cmd_code_numbits) {
wuffs_brotli__encoder__write_bits(self, 8u, ((uint64_t)(self->private_data.f_cmd_code[((v_i >> 3u) & 511u)])), a_s);
v_i += 8u;
}
wuffs_brotli__encoder__write_bits(self, ((uint32_t)((self->private_impl.f_cmd_code_numbits & 7u))), ((uint64_t)(self->private_data.f_cmd_code[((self->private_impl.f_cmd_code_numbits >> 3u) & 511u)])), a_s);
while (true) {
v_i = 0u;
while (v_i < 128u) {
self->private_data.f_cmd_histo[v_i] = WUFFS_BROTLI__CMD_HISTO_SEED[v_i];
v_i += 1u;
}
v_ip = v_in_off;
v_last_distance = 0u;
v_ip_end = ((uint64_t)(v_in_off + v_block_size));
v_goto_next_block = false;
if (v_block_size >= 16u) {
v_len_limit = ((uint64_t)(v_block_size - 5u));
v_len_limit = wuffs_base__u64__min(v_len_limit, ((uint64_t)(v_in_size - 16u)));
v_ip_limit = ((uint64_t)(v_in_off + v_len_limit));
v_ip += 1u;
v_next_hash = wuffs_brotli__encoder__hash_fast(self, a_input, v_ip, v_shift);
while (true) {
v_skip = 32u;
v_next_ip = v_ip;
while (true) {
v_hash = v_next_hash;
v_ip = v_next_ip;
v_next_ip = ((uint64_t)(v_ip + ((uint64_t)((v_skip >> 5u)))));
v_skip += 1u;
if (v_next_ip > v_ip_limit) {
goto label__outer__break;
}
v_next_hash = wuffs_brotli__encoder__hash_fast(self, a_input, v_next_ip, v_shift);
if ((v_last_distance > 0u) && (v_last_distance <= v_ip)) {
v_candidate = ((uint64_t)(v_ip - v_last_distance));
if (wuffs_brotli__encoder__is_match_fast(self, a_input, v_ip, v_candidate)) {
self->private_data.f_hash_table[(v_hash & 131071u)] = ((uint32_t)(v_ip));
if (((uint64_t)(v_ip - v_candidate)) > 262128u) {
continue;
}
break;
}
}
v_candidate = ((uint64_t)(self->private_data.f_hash_table[(v_hash & 131071u)]));
self->private_data.f_hash_table[(v_hash & 131071u)] = ((uint32_t)(v_ip));
if (wuffs_brotli__encoder__is_match_fast(self, a_input, v_ip, v_candidate)) {
if (((uint64_t)(v_ip - v_candidate)) > 262128u) {
continue;
}
break;
}
}
v_base_ip = v_ip;
v_matched = ((uint64_t)(5u + wuffs_brotli__encoder__find_match_length_with_limit(self,
a_input,
((uint64_t)(v_candidate + 5u)),
((uint64_t)(v_ip + 5u)),
((uint64_t)(((uint64_t)(v_ip_end - v_ip)) - 5u)))));
v_distance = ((uint64_t)(v_base_ip - v_candidate));
v_insert = ((uint64_t)(v_base_ip - v_next_emit));
v_ip += v_matched;
if (v_insert < 6210u) {
wuffs_brotli__encoder__emit_insert_len_fast(self, v_insert, a_s);
} else if (wuffs_brotli__encoder__should_use_uncompressed_mode(self,
v_metablock_start,
v_next_emit,
v_insert,
v_literal_ratio)) {
wuffs_brotli__encoder__emit_uncompressed_meta_block_fast(self,
a_input,
v_metablock_start,
v_base_ip,
((uint64_t)(v_mlen_storage_ix - 3u)),
a_s);
v_in_size -= ((uint64_t)(v_base_ip - v_in_off));
v_in_off = v_base_ip;
v_next_emit = v_in_off;
v_goto_next_block = true;
break;
} else {
wuffs_brotli__encoder__emit_long_insert_len_fast(self, v_insert, a_s);
}
wuffs_brotli__encoder__emit_literals(self,
a_input,
v_next_emit,
v_insert,
a_s);
if (v_distance == v_last_distance) {
wuffs_brotli__encoder__emit_cmd_fast(self, 64u, a_s);
} else {
wuffs_brotli__encoder__emit_distance_fast(self, v_distance, a_s);
v_last_distance = v_distance;
}
wuffs_brotli__encoder__emit_copy_len_last_distance_fast(self, v_matched, a_s);
v_next_emit = v_ip;
if (v_ip >= v_ip_limit) {
break;
}
v_candidate = wuffs_brotli__encoder__update_table_fast(self, a_input, v_ip, v_shift);
while (wuffs_brotli__encoder__is_match_fast(self, a_input, v_ip, v_candidate)) {
v_base_ip = v_ip;
v_matched = ((uint64_t)(5u + wuffs_brotli__encoder__find_match_length_with_limit(self,
a_input,
((uint64_t)(v_candidate + 5u)),
((uint64_t)(v_ip + 5u)),
((uint64_t)(((uint64_t)(v_ip_end - v_ip)) - 5u)))));
if (((uint64_t)(v_ip - v_candidate)) > 262128u) {
break;
}
v_ip += v_matched;
v_last_distance = ((uint64_t)(v_base_ip - v_candidate));
wuffs_brotli__encoder__emit_copy_len_fast(self, v_matched, a_s);
wuffs_brotli__encoder__emit_distance_fast(self, v_last_distance, a_s);
v_next_emit = v_ip;
if (v_ip >= v_ip_limit) {
goto label__outer__break;
}
v_candidate = wuffs_brotli__encoder__update_table_fast(self, a_input, v_ip, v_shift);
}
v_ip += 1u;
v_next_hash = wuffs_brotli__encoder__hash_fast(self, a_input, v_ip, v_shift);
}
label__outer__break:;
}
if ( ! v_goto_next_block) {
v_in_off += v_block_size;
v_in_size -= v_block_size;
v_block_size = wuffs_base__u64__min(v_in_size, 65536u);
v_merge = false;
if ((v_in_size > 0u) && (((uint64_t)(v_total_block_size + v_block_size)) <= 1048576u)) {
v_merge = wuffs_brotli__encoder__should_merge_block(self, a_input, v_in_off, v_block_size);
}
if (v_merge) {
v_total_block_size += v_block_size;
wuffs_brotli__encoder__update_bits(self,
20u,
((uint32_t)(((uint64_t)(v_total_block_size - 1u)))),
v_mlen_storage_ix,
a_s);
continue;
}
if (v_next_emit < v_ip_end) {
v_insert = ((uint64_t)(v_ip_end - v_next_emit));
if (v_insert < 6210u) {
wuffs_brotli__encoder__emit_insert_len_fast(self, v_insert, a_s);
wuffs_brotli__encoder__emit_literals(self,
a_input,
v_next_emit,
v_insert,
a_s);
} else if (wuffs_brotli__encoder__should_use_uncompressed_mode(self,
v_metablock_start,
v_next_emit,
v_insert,
v_literal_ratio)) {
wuffs_brotli__encoder__emit_uncompressed_meta_block_fast(self,
a_input,
v_metablock_start,
v_ip_end,
((uint64_t)(v_mlen_storage_ix - 3u)),
a_s);
} else {
wuffs_brotli__encoder__emit_long_insert_len_fast(self, v_insert, a_s);
wuffs_brotli__encoder__emit_literals(self,
a_input,
v_next_emit,
v_insert,
a_s);
}
}
v_next_emit = v_ip_end;
}
if (v_in_size > 0u) {
v_metablock_start = v_in_off;
v_block_size = wuffs_base__u64__min(v_in_size, 98304u);
v_total_block_size = v_block_size;
v_mlen_storage_ix = ((uint64_t)(self->private_impl.f_bw_ix + 3u));
wuffs_brotli__encoder__store_meta_block_header(self, v_block_size, false, a_s);
wuffs_brotli__encoder__write_bits(self, 13u, 0u, a_s);
v_literal_ratio = wuffs_brotli__encoder__build_and_store_literal_prefix_code(self,
a_input,
v_in_off,
v_block_size,
a_s);
wuffs_brotli__encoder__build_and_store_command_prefix_code_fast(self, a_s);
continue;
}
break;
}
if ( ! a_is_last) {
self->private_data.f_cmd_code[0u] = 0u;
v_saved_ix = self->private_impl.f_bw_ix;
self->private_impl.f_bw_ix = 0u;
wuffs_brotli__encoder__build_and_store_command_prefix_code_fast(self, wuffs_base__make_slice_u8(self->private_data.f_cmd_code, 520));
self->private_impl.f_cmd_code_numbits = self->private_impl.f_bw_ix;
self->private_impl.f_bw_ix = v_saved_ix;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.hash_two_pass
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__hash_two_pass(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_i,
uint32_t a_shift,
uint32_t a_length) {
uint64_t v_h = 0;
v_h = ((uint64_t)(((uint64_t)(wuffs_brotli__encoder__load64le(self, a_input, a_i) << (((uint32_t)(64u - ((uint32_t)(8u * a_length)))) & 63u))) * 506832829u));
return ((uint32_t)((v_h >> (a_shift & 63u))));
}
// -------- func brotli.encoder.hash_bytes_at_offset_two_pass
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(
const wuffs_brotli__encoder* self,
uint64_t a_v,
uint32_t a_offset,
uint32_t a_shift,
uint32_t a_length) {
uint64_t v_h = 0;
v_h = ((uint64_t)(((uint64_t)((a_v >> (((uint32_t)(8u * a_offset)) & 63u)) << (((uint32_t)(64u - ((uint32_t)(8u * a_length)))) & 63u))) * 506832829u));
return ((uint32_t)((v_h >> (a_shift & 63u))));
}
// -------- func brotli.encoder.is_match_two_pass
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__is_match_two_pass(
const wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_i1,
uint64_t a_i2,
uint32_t a_length) {
wuffs_base__slice_u8 v_q1 = {0};
wuffs_base__slice_u8 v_q2 = {0};
if ((a_i1 >= ((uint64_t)(a_input.len))) || (a_i2 >= ((uint64_t)(a_input.len)))) {
return false;
}
if (wuffs_brotli__encoder__load32le(self, a_input, a_i1) != wuffs_brotli__encoder__load32le(self, a_input, a_i2)) {
return false;
}
if (a_length == 4u) {
return true;
}
v_q1 = wuffs_base__slice_u8__subslice_i(a_input, a_i1);
v_q2 = wuffs_base__slice_u8__subslice_i(a_input, a_i2);
if ((((uint64_t)(v_q1.len)) >= 6u) && (((uint64_t)(v_q2.len)) >= 6u)) {
return ((v_q1.ptr[4u] == v_q2.ptr[4u]) && (v_q1.ptr[5u] == v_q2.ptr[5u]));
}
return false;
}
// -------- func brotli.encoder.build_and_store_command_prefix_code_two_pass
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__build_and_store_command_prefix_code_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s) {
uint32_t v_i = 0;
wuffs_private_impl__bulk_memset(&self->private_data.f_tmp_depth[0], 704u, 0u);
wuffs_brotli__encoder__create_huffman_tree(self,
0u,
0u,
64u,
15u,
wuffs_base__make_slice_u8(self->private_data.f_cmd_depth, 64));
wuffs_brotli__encoder__create_huffman_tree(self,
0u,
64u,
64u,
14u,
wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 64, 128));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 0, 24), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 24, 48));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 24, 32), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 0, 8));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 32, 40), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 48, 56));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 40, 48), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 8, 16));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 48, 56), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 56, 64));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 56, 64), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 16, 24));
wuffs_brotli__encoder__convert_bit_depths_to_symbols(self, wuffs_base__make_slice_u8(self->private_data.f_tmp_depth, 704), 64u, 0u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 0u, 24u, 8u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 8u, 40u, 8u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 16u, 56u, 8u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 24u, 0u, 24u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 48u, 32u, 8u);
wuffs_brotli__encoder__copy_tmp_bits_to_cmd_bits(self, 56u, 48u, 8u);
wuffs_brotli__encoder__convert_bit_depths_to_symbols(self, wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 64, 128), 64u, 1u);
wuffs_private_impl__bulk_memset(&self->private_data.f_tmp_depth[0], 64u, 0u);
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 0, 8), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 24, 32));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 64, 72), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 32, 40));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 128, 136), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 40, 48));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 192, 200), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 48, 56));
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__make_slice_u8_ij(self->private_data.f_tmp_depth, 384, 392), wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 56, 64));
v_i = 0u;
while (v_i < 8u) {
self->private_data.f_tmp_depth[(128u + (8u * v_i))] = self->private_data.f_cmd_depth[v_i];
self->private_data.f_tmp_depth[(256u + (8u * v_i))] = self->private_data.f_cmd_depth[(8u + v_i)];
self->private_data.f_tmp_depth[(448u + (8u * v_i))] = self->private_data.f_cmd_depth[(16u + v_i)];
v_i += 1u;
}
wuffs_brotli__encoder__store_huffman_tree(self, wuffs_base__make_slice_u8(self->private_data.f_tmp_depth, 704), 704u, a_s);
wuffs_brotli__encoder__store_huffman_tree(self, wuffs_base__make_slice_u8_ij(self->private_data.f_cmd_depth, 64, 128), 64u, a_s);
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.push_command
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__push_command(
wuffs_brotli__encoder* self,
uint32_t a_c) {
self->private_data.f_command_buf[(self->private_impl.f_num_commands & 131071u)] = a_c;
self->private_impl.f_num_commands += 1u;
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_insert_len_two_pass
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_insert_len_two_pass(
wuffs_brotli__encoder* self,
uint32_t a_insertlen) {
uint32_t v_tail = 0;
uint32_t v_nbits = 0;
uint32_t v_prefix = 0;
uint32_t v_extra = 0;
if (a_insertlen < 6u) {
wuffs_brotli__encoder__push_command(self, a_insertlen);
} else if (a_insertlen < 130u) {
v_tail = (a_insertlen - 2u);
v_nbits = ((uint32_t)(wuffs_brotli__encoder__log2_floor_non_zero(self, ((uint64_t)(v_tail))) - 1u));
v_prefix = (v_tail >> (v_nbits & 31u));
v_extra = ((uint32_t)(v_tail - ((uint32_t)(v_prefix << (v_nbits & 31u)))));
wuffs_brotli__encoder__push_command(self, (((uint32_t)(((uint32_t)(((uint32_t)(v_nbits << 1u)) + v_prefix)) + 2u)) | ((uint32_t)(v_extra << 8u))));
} else if (a_insertlen < 2114u) {
v_tail = (a_insertlen - 66u);
v_nbits = wuffs_brotli__encoder__log2_floor_non_zero(self, ((uint64_t)(v_tail)));
v_extra = ((uint32_t)(v_tail - (((uint32_t)(1u)) << v_nbits)));
wuffs_brotli__encoder__push_command(self, ((v_nbits + 10u) | ((uint32_t)(v_extra << 8u))));
} else if (a_insertlen < 6210u) {
wuffs_brotli__encoder__push_command(self, (21u | ((uint32_t)((a_insertlen - 2114u) << 8u))));
} else if (a_insertlen < 22594u) {
wuffs_brotli__encoder__push_command(self, (22u | ((uint32_t)((a_insertlen - 6210u) << 8u))));
} else {
wuffs_brotli__encoder__push_command(self, (23u | ((uint32_t)((a_insertlen - 22594u) << 8u))));
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_copy_len_two_pass
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_copy_len_two_pass(
wuffs_brotli__encoder* self,
uint64_t a_copylen) {
uint64_t v_tail = 0;
uint32_t v_nbits = 0;
uint64_t v_prefix = 0;
uint64_t v_extra = 0;
if (a_copylen < 10u) {
wuffs_brotli__encoder__push_command(self, ((uint32_t)((a_copylen + 38u))));
} else if (a_copylen < 134u) {
v_tail = (a_copylen - 6u);
v_nbits = ((uint32_t)(wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail) - 1u));
v_prefix = (v_tail >> (v_nbits & 63u));
v_extra = ((uint64_t)(v_tail - ((uint64_t)(v_prefix << (v_nbits & 63u)))));
wuffs_brotli__encoder__push_command(self, ((uint32_t)((((uint64_t)(((uint64_t)(((uint64_t)(((uint64_t)(v_nbits)) << 1u)) + v_prefix)) + 44u)) | ((uint64_t)(v_extra << 8u))))));
} else if (a_copylen < 2118u) {
v_tail = (a_copylen - 70u);
v_nbits = wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail);
v_extra = ((uint64_t)(v_tail - (((uint64_t)(1u)) << v_nbits)));
wuffs_brotli__encoder__push_command(self, ((uint32_t)(((((uint64_t)(v_nbits)) + 52u) | ((uint64_t)(v_extra << 8u))))));
} else {
wuffs_brotli__encoder__push_command(self, ((uint32_t)((63u | ((uint64_t)(((uint64_t)(a_copylen - 2118u)) << 8u))))));
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_copy_len_last_distance_two_pass
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_copy_len_last_distance_two_pass(
wuffs_brotli__encoder* self,
uint64_t a_copylen) {
uint64_t v_tail = 0;
uint32_t v_nbits = 0;
uint64_t v_prefix = 0;
uint64_t v_extra = 0;
if (a_copylen < 12u) {
wuffs_brotli__encoder__push_command(self, ((uint32_t)((a_copylen + 20u))));
} else if (a_copylen < 72u) {
v_tail = (a_copylen - 8u);
v_nbits = ((uint32_t)(wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail) - 1u));
v_prefix = (v_tail >> (v_nbits & 63u));
v_extra = ((uint64_t)(v_tail - ((uint64_t)(v_prefix << (v_nbits & 63u)))));
wuffs_brotli__encoder__push_command(self, ((uint32_t)((((uint64_t)(((uint64_t)(((uint64_t)(((uint64_t)(v_nbits)) << 1u)) + v_prefix)) + 28u)) | ((uint64_t)(v_extra << 8u))))));
} else if (a_copylen < 136u) {
v_tail = (a_copylen - 8u);
wuffs_brotli__encoder__push_command(self, ((uint32_t)((((v_tail >> 5u) + 54u) | ((v_tail & 31u) << 8u)))));
wuffs_brotli__encoder__push_command(self, 64u);
} else if (a_copylen < 2120u) {
v_tail = (a_copylen - 72u);
v_nbits = wuffs_brotli__encoder__log2_floor_non_zero(self, v_tail);
v_extra = ((uint64_t)(v_tail - (((uint64_t)(1u)) << v_nbits)));
wuffs_brotli__encoder__push_command(self, ((uint32_t)(((((uint64_t)(v_nbits)) + 52u) | ((uint64_t)(v_extra << 8u))))));
wuffs_brotli__encoder__push_command(self, 64u);
} else {
wuffs_brotli__encoder__push_command(self, ((uint32_t)((63u | ((uint64_t)(((uint64_t)(a_copylen - 2120u)) << 8u))))));
wuffs_brotli__encoder__push_command(self, 64u);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.emit_distance_two_pass
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_distance_two_pass(
wuffs_brotli__encoder* self,
uint32_t a_distance) {
uint32_t v_d = 0;
uint32_t v_nbits = 0;
uint32_t v_prefix = 0;
uint32_t v_offset = 0;
uint32_t v_distcode = 0;
uint32_t v_extra = 0;
v_d = ((uint32_t)(a_distance + 3u));
v_nbits = ((uint32_t)(wuffs_brotli__encoder__log2_floor_non_zero(self, ((uint64_t)(v_d))) - 1u));
v_prefix = ((v_d >> (v_nbits & 31u)) & 1u);
v_offset = ((uint32_t)((2u + v_prefix) << (v_nbits & 31u)));
v_distcode = ((uint32_t)(((uint32_t)(((uint32_t)(2u * ((uint32_t)(v_nbits - 1u)))) + v_prefix)) + 80u));
v_extra = ((uint32_t)(v_d - v_offset));
wuffs_brotli__encoder__push_command(self, (v_distcode | ((uint32_t)(v_extra << 8u))));
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.push_literals
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__push_literals(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_n) {
uint64_t v_j = 0;
v_j = 0u;
while (v_j < a_n) {
if (((uint64_t)(a_from + v_j)) >= ((uint64_t)(a_input.len))) {
break;
}
self->private_data.f_literal_buf[(self->private_impl.f_num_literals & 131071u)] = a_input.ptr[((uint64_t)(a_from + v_j))];
self->private_impl.f_num_literals += 1u;
v_j += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.update_table_two_pass
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_brotli__encoder__update_table_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_ip,
uint32_t a_shift,
uint32_t a_min_match,
uint32_t a_third_offset) {
uint64_t v_input_bytes = 0;
uint32_t v_prev_hash = 0;
uint32_t v_cur_hash = 0;
uint64_t v_candidate = 0;
uint32_t v_mm = 0;
v_mm = a_min_match;
if (v_mm == 4u) {
v_input_bytes = wuffs_brotli__encoder__load64le(self, a_input, ((uint64_t)(a_ip - 3u)));
v_cur_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
3u,
a_shift,
v_mm);
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
0u,
a_shift,
v_mm);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 3u))));
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
1u,
a_shift,
v_mm);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 2u))));
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
a_third_offset,
a_shift,
v_mm);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 1u))));
} else {
v_input_bytes = wuffs_brotli__encoder__load64le(self, a_input, ((uint64_t)(a_ip - 5u)));
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
0u,
a_shift,
v_mm);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 5u))));
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
1u,
a_shift,
v_mm);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 4u))));
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
2u,
a_shift,
v_mm);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 3u))));
v_input_bytes = wuffs_brotli__encoder__load64le(self, a_input, ((uint64_t)(a_ip - 2u)));
v_cur_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
2u,
a_shift,
v_mm);
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
0u,
a_shift,
v_mm);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 2u))));
v_prev_hash = wuffs_brotli__encoder__hash_bytes_at_offset_two_pass(self,
v_input_bytes,
1u,
a_shift,
v_mm);
self->private_data.f_hash_table[(v_prev_hash & 131071u)] = ((uint32_t)(((uint64_t)(a_ip - 1u))));
}
v_candidate = ((uint64_t)(self->private_data.f_hash_table[(v_cur_hash & 131071u)]));
self->private_data.f_hash_table[(v_cur_hash & 131071u)] = ((uint32_t)(a_ip));
return v_candidate;
}
// -------- func brotli.encoder.create_commands
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__create_commands(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_block_size,
uint64_t a_input_size,
uint32_t a_table_bits,
uint32_t a_min_match) {
uint64_t v_ip = 0;
uint32_t v_shift = 0;
uint64_t v_ip_end = 0;
uint64_t v_next_emit = 0;
uint64_t v_last_distance = 0;
uint64_t v_len_limit = 0;
uint64_t v_ip_limit = 0;
uint32_t v_next_hash = 0;
uint32_t v_hash = 0;
uint32_t v_skip = 0;
uint64_t v_next_ip = 0;
uint64_t v_candidate = 0;
uint64_t v_base_ip = 0;
uint64_t v_matched = 0;
uint64_t v_distance = 0;
uint64_t v_insert = 0;
uint64_t v_mm = 0;
self->private_impl.f_num_commands = 0u;
self->private_impl.f_num_literals = 0u;
v_mm = ((uint64_t)(a_min_match));
v_ip = a_from;
v_shift = ((uint32_t)(64u - a_table_bits));
v_ip_end = ((uint64_t)(a_from + a_block_size));
v_next_emit = a_from;
if (a_block_size >= 16u) {
v_len_limit = ((uint64_t)(a_block_size - v_mm));
v_len_limit = wuffs_base__u64__min(v_len_limit, ((uint64_t)(a_input_size - 16u)));
v_ip_limit = ((uint64_t)(a_from + v_len_limit));
v_ip += 1u;
v_next_hash = wuffs_brotli__encoder__hash_two_pass(self,
a_input,
v_ip,
v_shift,
a_min_match);
while (true) {
v_skip = 32u;
v_next_ip = v_ip;
while (true) {
v_hash = v_next_hash;
v_ip = v_next_ip;
v_next_ip = ((uint64_t)(v_ip + ((uint64_t)((v_skip >> 5u)))));
v_skip += 1u;
if (v_next_ip > v_ip_limit) {
goto label__outer__break;
}
v_next_hash = wuffs_brotli__encoder__hash_two_pass(self,
a_input,
v_next_ip,
v_shift,
a_min_match);
if ((v_last_distance > 0u) && (v_last_distance <= v_ip)) {
v_candidate = ((uint64_t)(v_ip - v_last_distance));
if (wuffs_brotli__encoder__is_match_two_pass(self,
a_input,
v_ip,
v_candidate,
a_min_match)) {
self->private_data.f_hash_table[(v_hash & 131071u)] = ((uint32_t)(v_ip));
if (((uint64_t)(v_ip - v_candidate)) > 262128u) {
continue;
}
break;
}
}
v_candidate = ((uint64_t)(self->private_data.f_hash_table[(v_hash & 131071u)]));
self->private_data.f_hash_table[(v_hash & 131071u)] = ((uint32_t)(v_ip));
if (wuffs_brotli__encoder__is_match_two_pass(self,
a_input,
v_ip,
v_candidate,
a_min_match)) {
if (((uint64_t)(v_ip - v_candidate)) > 262128u) {
continue;
}
break;
}
}
v_base_ip = v_ip;
v_matched = ((uint64_t)(v_mm + wuffs_brotli__encoder__find_match_length_with_limit(self,
a_input,
((uint64_t)(v_candidate + v_mm)),
((uint64_t)(v_ip + v_mm)),
((uint64_t)(((uint64_t)(v_ip_end - v_ip)) - v_mm)))));
v_distance = ((uint64_t)(v_base_ip - v_candidate));
v_insert = ((uint64_t)(v_base_ip - v_next_emit));
v_ip += v_matched;
wuffs_brotli__encoder__emit_insert_len_two_pass(self, ((uint32_t)(v_insert)));
wuffs_brotli__encoder__push_literals(self, a_input, v_next_emit, v_insert);
if (v_distance == v_last_distance) {
wuffs_brotli__encoder__push_command(self, 64u);
} else {
wuffs_brotli__encoder__emit_distance_two_pass(self, ((uint32_t)(v_distance)));
v_last_distance = v_distance;
}
wuffs_brotli__encoder__emit_copy_len_last_distance_two_pass(self, v_matched);
v_next_emit = v_ip;
if (v_ip >= v_ip_limit) {
break;
}
v_candidate = wuffs_brotli__encoder__update_table_two_pass(self,
a_input,
v_ip,
v_shift,
a_min_match,
0u);
while (((uint64_t)(v_ip - v_candidate)) <= 262128u) {
if ( ! wuffs_brotli__encoder__is_match_two_pass(self,
a_input,
v_ip,
v_candidate,
a_min_match)) {
break;
}
v_base_ip = v_ip;
v_matched = ((uint64_t)(v_mm + wuffs_brotli__encoder__find_match_length_with_limit(self,
a_input,
((uint64_t)(v_candidate + v_mm)),
((uint64_t)(v_ip + v_mm)),
((uint64_t)(((uint64_t)(v_ip_end - v_ip)) - v_mm)))));
v_ip += v_matched;
v_last_distance = ((uint64_t)(v_base_ip - v_candidate));
wuffs_brotli__encoder__emit_copy_len_two_pass(self, v_matched);
wuffs_brotli__encoder__emit_distance_two_pass(self, ((uint32_t)(v_last_distance)));
v_next_emit = v_ip;
if (v_ip >= v_ip_limit) {
goto label__outer__break;
}
v_candidate = wuffs_brotli__encoder__update_table_two_pass(self,
a_input,
v_ip,
v_shift,
a_min_match,
2u);
}
v_ip += 1u;
v_next_hash = wuffs_brotli__encoder__hash_two_pass(self,
a_input,
v_ip,
v_shift,
a_min_match);
}
label__outer__break:;
}
if (v_next_emit < v_ip_end) {
v_insert = ((uint64_t)(v_ip_end - v_next_emit));
wuffs_brotli__encoder__emit_insert_len_two_pass(self, ((uint32_t)(v_insert)));
wuffs_brotli__encoder__push_literals(self, a_input, v_next_emit, v_insert);
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.store_commands
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__store_commands(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_s) {
uint32_t v_i = 0;
uint32_t v_j = 0;
uint32_t v_cmd = 0;
uint32_t v_code = 0;
uint32_t v_extra = 0;
uint32_t v_insert = 0;
uint32_t v_lit_i = 0;
uint8_t v_lit = 0;
wuffs_private_impl__bulk_memset(&self->private_data.f_histogram[0], 256u * (size_t)4u, 0u);
wuffs_private_impl__bulk_memset(&self->private_data.f_cmd_depth[0], 128u, 0u);
wuffs_private_impl__bulk_memset(&self->private_data.f_cmd_bits[0], 128u * (size_t)2u, 0u);
wuffs_private_impl__bulk_memset(&self->private_data.f_cmd_histo[0], 128u * (size_t)4u, 0u);
v_i = 0u;
while (v_i < self->private_impl.f_num_literals) {
self->private_data.f_histogram[self->private_data.f_literal_buf[(v_i & 131071u)]] += 1u;
v_i += 1u;
}
wuffs_brotli__encoder__build_and_store_huffman_tree_fast(self, ((uint64_t)(self->private_impl.f_num_literals)), 8u, a_s);
v_i = 0u;
while (v_i < self->private_impl.f_num_commands) {
v_code = (self->private_data.f_command_buf[(v_i & 131071u)] & 255u);
self->private_data.f_cmd_histo[(v_code & 127u)] += 1u;
v_i += 1u;
}
self->private_data.f_cmd_histo[1u] += 1u;
self->private_data.f_cmd_histo[2u] += 1u;
self->private_data.f_cmd_histo[64u] += 1u;
self->private_data.f_cmd_histo[84u] += 1u;
wuffs_brotli__encoder__build_and_store_command_prefix_code_two_pass(self, a_s);
v_i = 0u;
while (v_i < self->private_impl.f_num_commands) {
v_cmd = self->private_data.f_command_buf[(v_i & 131071u)];
v_code = (v_cmd & 127u);
v_extra = (v_cmd >> 8u);
wuffs_brotli__encoder__write_bits(self, ((uint32_t)(self->private_data.f_cmd_depth[v_code])), ((uint64_t)(self->private_data.f_cmd_bits[v_code])), a_s);
wuffs_brotli__encoder__write_bits(self, WUFFS_BROTLI__TWO_PASS_NUM_EXTRA_BITS[v_code], ((uint64_t)(v_extra)), a_s);
if (v_code < 24u) {
v_insert = ((uint32_t)(WUFFS_BROTLI__TWO_PASS_INSERT_OFFSET[v_code] + v_extra));
v_j = 0u;
while (v_j < v_insert) {
v_lit = self->private_data.f_literal_buf[(v_lit_i & 131071u)];
wuffs_brotli__encoder__write_bits(self, ((uint32_t)(self->private_data.f_lit_depth[v_lit])), ((uint64_t)(self->private_data.f_lit_bits[v_lit])), a_s);
v_lit_i += 1u;
v_j += 1u;
}
}
v_i += 1u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.should_compress_two_pass
WUFFS_BASE__GENERATED_C_CODE
static bool
wuffs_brotli__encoder__should_compress_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_input_size,
uint64_t a_num_literals) {
uint64_t v_corpus_size = 0;
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) {
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);
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) {
if (((uint64_t)(a_from + v_i)) < ((uint64_t)(a_input.len))) {
self->private_data.f_histogram[a_input.ptr[((uint64_t)(a_from + v_i))]] += 1u;
}
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);
}
// -------- func brotli.encoder.emit_uncompressed_meta_block_two_pass
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__emit_uncompressed_meta_block_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
uint64_t a_from,
uint64_t a_input_size,
wuffs_base__slice_u8 a_s) {
uint64_t v_p = 0;
wuffs_brotli__encoder__store_meta_block_header(self, a_input_size, true, a_s);
wuffs_brotli__encoder__align_to_byte(self);
wuffs_brotli__encoder__copy_bytes(self,
a_input,
a_from,
((uint64_t)(a_from + a_input_size)),
a_s);
self->private_impl.f_bw_ix += ((uint64_t)(a_input_size << 3u));
v_p = (self->private_impl.f_bw_ix >> 3u);
if (v_p < ((uint64_t)(a_s.len))) {
a_s.ptr[v_p] = 0u;
}
return wuffs_base__make_empty_struct();
}
// -------- func brotli.encoder.compress_fragment_two_pass
WUFFS_BASE__GENERATED_C_CODE
static wuffs_base__empty_struct
wuffs_brotli__encoder__compress_fragment_two_pass(
wuffs_brotli__encoder* self,
wuffs_base__slice_u8 a_input,
bool a_is_last,
uint32_t a_table_bits,
wuffs_base__slice_u8 a_s) {
uint64_t v_initial_ix = 0;
uint32_t v_min_match = 0;
uint64_t v_from = 0;
uint64_t v_input_size = 0;
uint64_t v_block_size = 0;
bool v_compress = false;
v_initial_ix = self->private_impl.f_bw_ix;
v_min_match = 6u;
if (a_table_bits <= 15u) {
v_min_match = 4u;
}
v_input_size = ((uint64_t)(a_input.len));
while (v_input_size > 0u) {
v_block_size = wuffs_base__u64__min(v_input_size, ((uint64_t)(131072u)));
wuffs_brotli__encoder__create_commands(self,
a_input,
v_from,
v_block_size,
v_input_size,
a_table_bits,
v_min_match);
v_compress = wuffs_brotli__encoder__should_compress_two_pass(self,
a_input,
v_from,
v_block_size,
((uint64_t)(self->private_impl.f_num_literals)));
if (v_compress) {
wuffs_brotli__encoder__store_meta_block_header(self, v_block_size, false, a_s);
wuffs_brotli__encoder__write_bits(self, 13u, 0u, a_s);
wuffs_brotli__encoder__store_commands(self, a_s);
} else {
wuffs_brotli__encoder__emit_uncompressed_meta_block_two_pass(self,
a_input,
v_from,
v_block_size,
a_s);
}
v_from += v_block_size;
v_input_size -= v_block_size;
}
if (((uint64_t)(self->private_impl.f_bw_ix - v_initial_ix)) > ((uint64_t)(31u + ((uint64_t)(((uint64_t)(a_input.len)) << 3u))))) {
wuffs_brotli__encoder__rewind_bit_position(self, v_initial_ix, a_s);
wuffs_brotli__encoder__emit_uncompressed_meta_block_two_pass(self,
a_input,
0u,
((uint64_t)(a_input.len)),
a_s);
}
if (a_is_last) {
wuffs_brotli__encoder__write_bits(self, 1u, 1u, a_s);
wuffs_brotli__encoder__write_bits(self, 1u, 1u, a_s);
wuffs_brotli__encoder__align_to_byte(self);
}
return wuffs_base__make_empty_struct();
}
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BROTLI)
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BZIP2)
@@ -99368,2058 +106480,6 @@ wuffs_sha256__hasher__checksum_bitvec256(
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SHA256)
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SOFTFLOAT)
// ---------------- Status Codes Implementations
// ---------------- Private Consts
#define WUFFS_SOFTFLOAT__F64_SIGN 9223372036854775808u
#define WUFFS_SOFTFLOAT__F64_ABS 9223372036854775807u
#define WUFFS_SOFTFLOAT__F64_INF 9218868437227405312u
#define WUFFS_SOFTFLOAT__F64_FRAC 4503599627370495u
#define WUFFS_SOFTFLOAT__F64_HIDDEN 4503599627370496u
#define WUFFS_SOFTFLOAT__F64_QUIET 2251799813685248u
#define WUFFS_SOFTFLOAT__F64_DEFAULT_NAN 18444492273895866368u
static const uint8_t
WUFFS_SOFTFLOAT__CLZ8[256] WUFFS_BASE__POTENTIALLY_UNUSED = {
8u, 7u, 6u, 6u, 5u, 5u, 5u, 5u,
4u, 4u, 4u, 4u, 4u, 4u, 4u, 4u,
3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u,
3u, 3u, 3u, 3u, 3u, 3u, 3u, 3u,
2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u,
2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u,
2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u,
2u, 2u, 2u, 2u, 2u, 2u, 2u, 2u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
};
#define WUFFS_SOFTFLOAT__F32_SIGN 2147483648u
#define WUFFS_SOFTFLOAT__F32_ABS 2147483647u
#define WUFFS_SOFTFLOAT__F32_INF 2139095040u
#define WUFFS_SOFTFLOAT__LOG2_INVLN2_HI 4609176140020449280u
#define WUFFS_SOFTFLOAT__LOG2_INVLN2_LO 4460540536611119616u
#define WUFFS_SOFTFLOAT__LOG2_A0 13828044577248609081u
#define WUFFS_SOFTFLOAT__LOG2_A1 4602334714382648510u
#define WUFFS_SOFTFLOAT__LOG2_A2 13823540977608962043u
#define WUFFS_SOFTFLOAT__LOG2_A3 4598869476581264456u
#define WUFFS_SOFTFLOAT__LOG2_A4 13821203878746165948u
#define WUFFS_SOFTFLOAT__LOG2_A5 4596594284514769198u
#define WUFFS_SOFTFLOAT__LOG2_B0 13828044577248609022u
#define WUFFS_SOFTFLOAT__LOG2_B1 4602334714382648311u
#define WUFFS_SOFTFLOAT__LOG2_B2 13823540977621236799u
#define WUFFS_SOFTFLOAT__LOG2_B3 4598869476596800484u
#define WUFFS_SOFTFLOAT__LOG2_B4 13821203151630798821u
#define WUFFS_SOFTFLOAT__LOG2_B5 4596593529635268406u
#define WUFFS_SOFTFLOAT__LOG2_B6 13819037345097492379u
#define WUFFS_SOFTFLOAT__LOG2_B7 4594943554432801610u
#define WUFFS_SOFTFLOAT__LOG2_B8 13817758221876344892u
#define WUFFS_SOFTFLOAT__LOG2_B9 4593868635039788646u
#define WUFFS_SOFTFLOAT__F64_ONE 4607182418800017408u
#define WUFFS_SOFTFLOAT__F64_NEG_ONE 13830554455654793216u
static const uint64_t
WUFFS_SOFTFLOAT__LOG2_TAB_INVC[64] WUFFS_BASE__POTENTIALLY_UNUSED = {
4609192499661090040u, 4609119721358544033u, 4609048552263439498u, 4608978935752977541u, 4608910827810035960u, 4608844174476640876u, 4608778932670517176u, 4608715056657886750u,
4608652505995016245u, 4608591236480216541u, 4608531212716768208u, 4608472393775796206u, 4608414747454330217u, 4608358236140386643u, 4608302825730857126u, 4608248488588498998u,
4608195190521789770u, 4608142902552467893u, 4608091596839446450u, 4608041245183688113u, 4607991821672542679u, 4607943301500489090u, 4607895658995436368u, 4607848870546001381u,
4607802913757468081u, 4607757767841591096u, 4607713409864934365u, 4607669821755832624u, 4607626980202848768u, 4607584869060826940u, 4607543467839055042u, 4607502757543314897u,
4607462725802730023u, 4607423351295589007u, 4607384620866568409u, 4607346516590018701u, 4607309024952925059u, 4607272131607993180u, 4607235821368544930u, 4607200080338058785u,
4607112595412526955u, 4606976147961868676u, 4606843801959665791u, 4606715378043149032u, 4606590705194581478u, 4606469619618625715u, 4606351967316207939u, 4606237606996243811u,
4606126401917471569u, 4606018221995330636u, 4605912946732701130u, 4605810462065308559u, 4605710654407004217u, 4605613423158833629u, 4605518667591971211u, 4605426298098548397u,
4605336222619703554u, 4605248357336792306u, 4605162622515894769u, 4605078941046797933u, 4604997241283328404u, 4604917450612805666u, 4604839505954084298u, 4604763342788034444u,
};
static const uint64_t
WUFFS_SOFTFLOAT__LOG2_TAB_LOGC[64] WUFFS_BASE__POTENTIALLY_UNUSED = {
13826342638637133824u, 13826196630396129280u, 13826052246236362752u, 13825768029042720768u, 13825485539995279360u, 13825206079124377600u, 13824929594987094016u, 13824656018996125696u,
13824385299287748608u, 13824117362061238272u, 13823852166444584960u, 13823589642092011520u, 13823329752470622208u, 13823072432079704064u, 13822817626379755520u, 13822565306322034688u,
13822315408407748608u, 13822067889127702528u, 13821822706514976768u, 13821579813106167808u, 13821131081098354688u, 13820654214105104384u, 13820181677119676416u, 13819713392057769984u,
13819249286753288192u, 13818789299422093312u, 13818333340582215680u, 13817881364992663552u, 13817433267667582976u, 13816934369010221056u, 13816053372001304576u, 13815179735723180032u,
13814313441755004928u, 13813454269690494976u, 13812602167107960832u, 13810973880287035392u, 13809297139126697984u, 13807231240355184640u, 13803931045004312576u, 13796532236635865088u,
4577625706679238656u, 4584977561914671104u, 4588127867468742656u, 4590199577454788608u, 4591728374053568512u, 4593235018675142656u, 4594195890746384384u, 4594927996701155328u,
4595649931110391808u, 4596361981185310720u, 4597064405355782144u, 4597757451025965056u, 4598308306428592128u, 4598645833741746176u, 4598979039133097984u, 4599308018491162624u,
4599632888650723328u, 4599953748864847872u, 4600270694594048000u, 4600583822264602624u, 4600893218024939520u, 4601198981460250624u, 4601501185019535360u, 4601799915281006592u,
};
// ---------------- Private Initializer Prototypes
// ---------------- Private Function Prototypes
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__clz64(
const wuffs_softfloat__arith* self,
uint64_t a_a);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__shift_right_jam64(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint32_t a_dist);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__jam128_hi(
const wuffs_softfloat__arith* self,
uint64_t a_hi,
uint64_t a_lo,
uint32_t a_dist);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__jam128_lo(
const wuffs_softfloat__arith* self,
uint64_t a_hi,
uint64_t a_lo,
uint32_t a_dist);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__mul_hi64(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__round_pack_f64(
const wuffs_softfloat__arith* self,
uint64_t a_sign,
uint32_t a_exp,
uint64_t a_sig);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__norm_round_pack_f64(
const wuffs_softfloat__arith* self,
uint64_t a_sign,
uint32_t a_exp,
uint64_t a_sig);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__norm_sub_exp(
const wuffs_softfloat__arith* self,
uint64_t a_frac);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__norm_sub_sig(
const wuffs_softfloat__arith* self,
uint64_t a_frac);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_propagate_nan(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_add_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_sign);
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_sub_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_sign);
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__round_pack_f32(
const wuffs_softfloat__arith* self,
uint32_t a_sign,
uint32_t a_exp,
uint32_t a_sig);
// ---------------- VTables
// ---------------- Initializer Implementations
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
wuffs_softfloat__arith__initialize(
wuffs_softfloat__arith* self,
size_t sizeof_star_self,
uint64_t wuffs_version,
uint32_t options){
if (!self) {
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
}
if (sizeof(*self) != sizeof_star_self) {
return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver);
}
if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) ||
(((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) {
return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version);
}
if ((options & WUFFS_INITIALIZE__ALREADY_ZEROED) != 0) {
// The whole point of this if-check is to detect an uninitialized *self.
// We disable the warning on GCC. Clang-5.0 does not have this warning.
#if !defined(__clang__) && defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
if (self->private_impl.magic != 0) {
return wuffs_base__make_status(wuffs_base__error__initialize_falsely_claimed_already_zeroed);
}
#if !defined(__clang__) && defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
} else {
if ((options & WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED) == 0) {
memset(self, 0, sizeof(*self));
options |= WUFFS_INITIALIZE__ALREADY_ZEROED;
} else {
memset(&(self->private_impl), 0, sizeof(self->private_impl));
}
}
self->private_impl.magic = WUFFS_BASE__MAGIC;
return wuffs_base__make_status(NULL);
}
wuffs_softfloat__arith*
wuffs_softfloat__arith__alloc(void) {
wuffs_softfloat__arith* x =
(wuffs_softfloat__arith*)(calloc(1, sizeof(wuffs_softfloat__arith)));
if (!x) {
return NULL;
}
if (wuffs_softfloat__arith__initialize(
x, sizeof(wuffs_softfloat__arith), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) {
free(x);
return NULL;
}
return x;
}
size_t
sizeof__wuffs_softfloat__arith(void) {
return sizeof(wuffs_softfloat__arith);
}
// ---------------- Function Implementations
// -------- func softfloat.arith.clz64
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__clz64(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
uint64_t v_x = 0;
uint32_t v_n32 = 0;
uint32_t v_n16 = 0;
uint32_t v_n8 = 0;
v_x = a_a;
if ((v_x >> 32u) == 0u) {
v_x = ((uint64_t)(v_x << 32u));
v_n32 = 32u;
}
if ((v_x >> 48u) == 0u) {
v_x = ((uint64_t)(v_x << 16u));
v_n16 = 16u;
}
if ((v_x >> 56u) == 0u) {
v_x = ((uint64_t)(v_x << 8u));
v_n8 = 8u;
}
return ((v_n32 +
v_n16 +
v_n8 +
(((uint32_t)(WUFFS_SOFTFLOAT__CLZ8[(v_x >> 56u)])) & 7u)) & 63u);
}
// -------- func softfloat.arith.shift_right_jam64
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__shift_right_jam64(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint32_t a_dist) {
uint32_t v_d = 0;
if (a_dist == 0u) {
return a_a;
} else if (a_dist < 64u) {
v_d = a_dist;
if (((uint64_t)(a_a << (64u - v_d))) != 0u) {
return ((a_a >> v_d) | 1u);
}
return (a_a >> v_d);
} else if (a_a != 0u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.jam128_hi
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__jam128_hi(
const wuffs_softfloat__arith* self,
uint64_t a_hi,
uint64_t a_lo,
uint32_t a_dist) {
if (a_dist < 64u) {
return (a_hi >> a_dist);
}
return 0u;
}
// -------- func softfloat.arith.jam128_lo
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__jam128_lo(
const wuffs_softfloat__arith* self,
uint64_t a_hi,
uint64_t a_lo,
uint32_t a_dist) {
uint32_t v_d = 0;
uint64_t v_sticky = 0;
if (a_dist == 0u) {
return a_lo;
} else if (a_dist < 64u) {
v_d = a_dist;
if (((uint64_t)(a_lo << (64u - v_d))) != 0u) {
v_sticky = 1u;
}
return (((uint64_t)(a_hi << (64u - v_d))) | (a_lo >> v_d) | v_sticky);
} else if (a_dist < 128u) {
v_d = (a_dist - 64u);
if (v_d > 0u) {
if ((((uint64_t)(a_hi << (64u - v_d))) | a_lo) != 0u) {
v_sticky = 1u;
}
} else if (a_lo != 0u) {
v_sticky = 1u;
}
return ((a_hi >> v_d) | v_sticky);
} else if ((a_hi | a_lo) != 0u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.mul_hi64
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__mul_hi64(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
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_p11 = 0;
uint64_t v_mid = 0;
v_a0 = (a_a & 4294967295u);
v_a1 = (a_a >> 32u);
v_b0 = (a_b & 4294967295u);
v_b1 = (a_b >> 32u);
v_p00 = (v_a0 * v_b0);
v_p01 = (v_a0 * v_b1);
v_p10 = (v_a1 * v_b0);
v_p11 = (v_a1 * v_b1);
v_mid = ((v_p00 >> 32u) + (v_p01 & 4294967295u) + (v_p10 & 4294967295u));
return ((uint64_t)(((uint64_t)(((uint64_t)(v_p11 + (v_p01 >> 32u))) + (v_p10 >> 32u))) + (v_mid >> 32u)));
}
// -------- func softfloat.arith.round_pack_f64
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__round_pack_f64(
const wuffs_softfloat__arith* self,
uint64_t a_sign,
uint32_t a_exp,
uint64_t a_sig) {
uint32_t v_e = 0;
uint64_t v_s = 0;
uint64_t v_round_bits = 0;
v_e = a_exp;
v_s = a_sig;
v_round_bits = (v_s & 1023u);
if (v_e >= 2147483648u) {
v_s = wuffs_softfloat__arith__shift_right_jam64(self, v_s, ((uint32_t)(0u - v_e)));
v_e = 0u;
v_round_bits = (v_s & 1023u);
} else if (v_e >= 2045u) {
if ((v_e > 2045u) || (v_s >= 9223372036854775296u)) {
return (a_sign | 9218868437227405312u);
}
}
v_s = (((uint64_t)(v_s + 512u)) >> 10u);
if (v_round_bits == 512u) {
v_s = (v_s & 18446744073709551614u);
}
if (v_s == 0u) {
v_e = 0u;
}
return (a_sign | ((uint64_t)(((uint64_t)(((uint64_t)(v_e)) << 52u)) + v_s)));
}
// -------- func softfloat.arith.norm_round_pack_f64
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__norm_round_pack_f64(
const wuffs_softfloat__arith* self,
uint64_t a_sign,
uint32_t a_exp,
uint64_t a_sig) {
uint32_t v_c = 0;
uint32_t v_shift = 0;
uint32_t v_e = 0;
if (a_sig == 0u) {
return a_sign;
}
v_c = wuffs_softfloat__arith__clz64(self, a_sig);
if (v_c == 0u) {
return wuffs_softfloat__arith__round_pack_f64(self, a_sign, ((uint32_t)(a_exp + 1u)), ((a_sig >> 1u) | (a_sig & 1u)));
}
v_shift = (v_c - 1u);
v_e = ((uint32_t)(a_exp - v_shift));
if ((v_shift >= 10u) && (v_e < 2045u)) {
return (a_sign | ((uint64_t)(((uint64_t)(((uint64_t)(v_e)) << 52u)) + ((uint64_t)(a_sig << (v_shift - 10u))))));
}
return wuffs_softfloat__arith__round_pack_f64(self, a_sign, v_e, ((uint64_t)(a_sig << v_shift)));
}
// -------- func softfloat.arith.norm_sub_exp
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__norm_sub_exp(
const wuffs_softfloat__arith* self,
uint64_t a_frac) {
return ((uint32_t)(12u - wuffs_softfloat__arith__clz64(self, a_frac)));
}
// -------- func softfloat.arith.norm_sub_sig
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__norm_sub_sig(
const wuffs_softfloat__arith* self,
uint64_t a_frac) {
return ((uint64_t)(a_frac << (((uint32_t)(wuffs_softfloat__arith__clz64(self, a_frac) - 11u)) & 63u)));
}
// -------- func softfloat.arith.f64_propagate_nan
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_propagate_nan(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if ((a_a & 9223372036854775807u) > 9218868437227405312u) {
return (a_a | 2251799813685248u);
}
return (a_b | 2251799813685248u);
}
// -------- func softfloat.arith.f64_is_nan
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_is_nan(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if ((a_a & 9223372036854775807u) > 9218868437227405312u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_neg
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_neg(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return (a_a ^ 9223372036854775808u);
}
// -------- func softfloat.arith.f64_abs
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_abs(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return (a_a & 9223372036854775807u);
}
// -------- func softfloat.arith.f64_cmp
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_cmp(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_ka = 0;
uint64_t v_kb = 0;
if (((a_a & 9223372036854775807u) > 9218868437227405312u) || ((a_b & 9223372036854775807u) > 9218868437227405312u)) {
return 3u;
} else if (((a_a | a_b) & 9223372036854775807u) == 0u) {
return 1u;
}
v_ka = (a_a | 9223372036854775808u);
if ((a_a >> 63u) != 0u) {
v_ka = (18446744073709551615u ^ a_a);
}
v_kb = (a_b | 9223372036854775808u);
if ((a_b >> 63u) != 0u) {
v_kb = (18446744073709551615u ^ a_b);
}
if (v_ka < v_kb) {
return 0u;
} else if (v_ka == v_kb) {
return 1u;
}
return 2u;
}
// -------- func softfloat.arith.f64_eq
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_eq(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 1u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_ne
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_ne(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 1u) {
return 0u;
}
return 1u;
}
// -------- func softfloat.arith.f64_lt
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_lt(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 0u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_le
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_le(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) <= 1u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_gt
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_gt(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 2u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_ge
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_ge(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_c = 0;
v_c = wuffs_softfloat__arith__f64_cmp(self, a_a, a_b);
if ((v_c == 1u) || (v_c == 2u)) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f64_min
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_min(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 0u) {
return a_a;
}
return a_b;
}
// -------- func softfloat.arith.f64_max
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_max(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f64_cmp(self, a_a, a_b) == 2u) {
return a_a;
}
return a_b;
}
// -------- func softfloat.arith.f64_add
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_add(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (((a_a ^ a_b) >> 63u) == 0u) {
return wuffs_softfloat__arith__f64_add_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
}
return wuffs_softfloat__arith__f64_sub_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
}
// -------- func softfloat.arith.f64_sub
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_sub(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (((a_a ^ a_b) >> 63u) == 0u) {
return wuffs_softfloat__arith__f64_sub_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
}
return wuffs_softfloat__arith__f64_add_mags(self, a_a, a_b, (a_a & 9223372036854775808u));
}
// -------- func softfloat.arith.f64_add_mags
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_add_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_sign) {
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_sig_z = 0;
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
if (v_exp_a == v_exp_b) {
if (v_exp_a == 0u) {
return (a_sign | ((uint64_t)((a_a & 9223372036854775807u) + v_sig_b)));
} else if (v_exp_a == 2047u) {
if ((v_sig_a | v_sig_b) != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return a_a;
}
v_exp_z = v_exp_a;
v_sig_z = ((uint64_t)(((uint64_t)(((uint64_t)(9007199254740992u + v_sig_a)) + v_sig_b)) << 9u));
return wuffs_softfloat__arith__round_pack_f64(self, a_sign, v_exp_z, v_sig_z);
}
v_sig_a = ((uint64_t)(v_sig_a << 9u));
v_sig_b = ((uint64_t)(v_sig_b << 9u));
if (v_exp_a < v_exp_b) {
if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return (a_sign | 9218868437227405312u);
}
v_exp_z = v_exp_b;
if (v_exp_a != 0u) {
v_sig_a = ((uint64_t)(v_sig_a + 2305843009213693952u));
} else {
v_sig_a = ((uint64_t)(v_sig_a << 1u));
}
v_sig_a = wuffs_softfloat__arith__shift_right_jam64(self, v_sig_a, ((uint32_t)(v_exp_b - v_exp_a)));
} else {
if (v_exp_a == 2047u) {
if (v_sig_a != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return (a_sign | 9218868437227405312u);
}
v_exp_z = v_exp_a;
if (v_exp_b != 0u) {
v_sig_b = ((uint64_t)(v_sig_b + 2305843009213693952u));
} else {
v_sig_b = ((uint64_t)(v_sig_b << 1u));
}
v_sig_b = wuffs_softfloat__arith__shift_right_jam64(self, v_sig_b, ((uint32_t)(v_exp_a - v_exp_b)));
}
v_sig_z = ((uint64_t)(((uint64_t)(2305843009213693952u + v_sig_a)) + v_sig_b));
if (v_sig_z < 4611686018427387904u) {
v_exp_z = ((uint32_t)(v_exp_z - 1u));
v_sig_z = ((uint64_t)(v_sig_z << 1u));
}
return wuffs_softfloat__arith__round_pack_f64(self, a_sign, v_exp_z, v_sig_z);
}
// -------- func softfloat.arith.f64_sub_mags
WUFFS_BASE__GENERATED_C_CODE
static uint64_t
wuffs_softfloat__arith__f64_sub_mags(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_sign) {
uint64_t v_sign_z = 0;
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_sig_z = 0;
uint32_t v_shift = 0;
v_sign_z = a_sign;
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
if (v_exp_a == v_exp_b) {
if (v_exp_a == 2047u) {
if ((v_sig_a | v_sig_b) != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return 18444492273895866368u;
} else if (v_sig_a == v_sig_b) {
return 0u;
}
if (v_exp_a != 0u) {
v_exp_a = ((uint32_t)(v_exp_a - 1u));
}
if (v_sig_a < v_sig_b) {
v_sign_z ^= 9223372036854775808u;
v_sig_z = ((uint64_t)(v_sig_b - v_sig_a));
} else {
v_sig_z = ((uint64_t)(v_sig_a - v_sig_b));
}
v_shift = (((uint32_t)(wuffs_softfloat__arith__clz64(self, v_sig_z) - 11u)) & 63u);
if (v_shift > v_exp_a) {
v_shift = (v_exp_a & 63u);
v_exp_z = 0u;
} else {
v_exp_z = ((uint32_t)(v_exp_a - v_shift));
}
return (v_sign_z | ((uint64_t)(((uint64_t)(((uint64_t)(v_exp_z)) << 52u)) + ((uint64_t)(v_sig_z << v_shift)))));
}
v_sig_a = ((uint64_t)(v_sig_a << 10u));
v_sig_b = ((uint64_t)(v_sig_b << 10u));
if (v_exp_a < v_exp_b) {
v_sign_z ^= 9223372036854775808u;
if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return (v_sign_z | 9218868437227405312u);
}
if (v_exp_a != 0u) {
v_sig_a = ((uint64_t)(v_sig_a + 4611686018427387904u));
} else {
v_sig_a = ((uint64_t)(v_sig_a + v_sig_a));
}
v_sig_a = wuffs_softfloat__arith__shift_right_jam64(self, v_sig_a, ((uint32_t)(v_exp_b - v_exp_a)));
v_sig_b = (v_sig_b | 4611686018427387904u);
v_exp_z = v_exp_b;
v_sig_z = ((uint64_t)(v_sig_b - v_sig_a));
} else {
if (v_exp_a == 2047u) {
if (v_sig_a != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return (v_sign_z | 9218868437227405312u);
}
if (v_exp_b != 0u) {
v_sig_b = ((uint64_t)(v_sig_b + 4611686018427387904u));
} else {
v_sig_b = ((uint64_t)(v_sig_b + v_sig_b));
}
v_sig_b = wuffs_softfloat__arith__shift_right_jam64(self, v_sig_b, ((uint32_t)(v_exp_a - v_exp_b)));
v_sig_a = (v_sig_a | 4611686018427387904u);
v_exp_z = v_exp_a;
v_sig_z = ((uint64_t)(v_sig_a - v_sig_b));
}
return wuffs_softfloat__arith__norm_round_pack_f64(self, v_sign_z, ((uint32_t)(v_exp_z - 1u)), v_sig_z);
}
// -------- func softfloat.arith.f64_mul
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_mul(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_sign_z = 0;
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_sig_z = 0;
v_sign_z = ((a_a ^ a_b) & 9223372036854775808u);
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
if (v_exp_a == 2047u) {
if ((v_sig_a != 0u) || ((v_exp_b == 2047u) && (v_sig_b != 0u))) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
} else if ((a_b & 9223372036854775807u) == 0u) {
return 18444492273895866368u;
}
return (v_sign_z | 9218868437227405312u);
} else if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
} else if ((a_a & 9223372036854775807u) == 0u) {
return 18444492273895866368u;
}
return (v_sign_z | 9218868437227405312u);
}
if (v_exp_a == 0u) {
if (v_sig_a == 0u) {
return v_sign_z;
}
v_exp_a = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_a);
v_sig_a = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_a);
}
if (v_exp_b == 0u) {
if (v_sig_b == 0u) {
return v_sign_z;
}
v_exp_b = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_b);
v_sig_b = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_b);
}
v_exp_z = ((uint32_t)(((uint32_t)(v_exp_a + v_exp_b)) - 1023u));
v_sig_a = ((uint64_t)((v_sig_a | 4503599627370496u) << 10u));
v_sig_b = ((uint64_t)((v_sig_b | 4503599627370496u) << 11u));
v_sig_z = wuffs_softfloat__arith__mul_hi64(self, v_sig_a, v_sig_b);
if (((uint64_t)(v_sig_a * v_sig_b)) != 0u) {
v_sig_z |= 1u;
}
if (v_sig_z < 4611686018427387904u) {
v_exp_z = ((uint32_t)(v_exp_z - 1u));
v_sig_z = ((uint64_t)(v_sig_z << 1u));
}
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_z, v_exp_z, v_sig_z);
}
// -------- func softfloat.arith.f64_div
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_div(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_sign_z = 0;
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_q = 0;
uint64_t v_r = 0;
uint32_t v_i = 0;
v_sign_z = ((a_a ^ a_b) & 9223372036854775808u);
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
if (v_exp_a == 2047u) {
if (v_sig_a != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
} else if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return 18444492273895866368u;
}
return (v_sign_z | 9218868437227405312u);
} else if (v_exp_b == 2047u) {
if (v_sig_b != 0u) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
}
return v_sign_z;
}
if (v_exp_b == 0u) {
if (v_sig_b == 0u) {
if ((a_a & 9223372036854775807u) == 0u) {
return 18444492273895866368u;
}
return (v_sign_z | 9218868437227405312u);
}
v_exp_b = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_b);
v_sig_b = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_b);
}
if (v_exp_a == 0u) {
if (v_sig_a == 0u) {
return v_sign_z;
}
v_exp_a = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_a);
v_sig_a = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_a);
}
v_exp_z = ((uint32_t)(((uint32_t)(v_exp_a - v_exp_b)) + 1022u));
v_sig_a = ((v_sig_a & 4503599627370495u) | 4503599627370496u);
v_sig_b = ((v_sig_b & 4503599627370495u) | 4503599627370496u);
if (v_sig_a < v_sig_b) {
v_exp_z = ((uint32_t)(v_exp_z - 1u));
v_sig_a = ((uint64_t)(v_sig_a << 1u));
}
v_q = 1u;
v_r = ((uint64_t)(v_sig_a - v_sig_b));
v_i = 0u;
while (v_i < 5u) {
v_r = ((uint64_t)(v_r << 11u));
if (v_sig_b > 0u) {
v_q = (((uint64_t)(v_q << 11u)) | (v_r / v_sig_b));
v_r = (v_r % v_sig_b);
}
v_i += 1u;
}
v_q = ((uint64_t)(v_q << 7u));
if (v_r != 0u) {
v_q |= 1u;
}
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_z, v_exp_z, v_q);
}
// -------- func softfloat.arith.f64_fma
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_fma(
const wuffs_softfloat__arith* self,
uint64_t a_a,
uint64_t a_b,
uint64_t a_c) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_sign_p = 0;
uint64_t v_sign_c = 0;
uint64_t v_sign_z = 0;
uint32_t v_exp_a = 0;
uint32_t v_exp_b = 0;
uint32_t v_exp_c = 0;
uint32_t v_exp_z = 0;
uint64_t v_sig_a = 0;
uint64_t v_sig_b = 0;
uint64_t v_sig_c = 0;
uint64_t v_p_hi = 0;
uint64_t v_p_lo = 0;
uint64_t v_c_hi = 0;
uint64_t v_c_lo = 0;
uint64_t v_z_hi = 0;
uint64_t v_z_lo = 0;
uint64_t v_tmp = 0;
uint32_t v_d = 0;
uint32_t v_top = 0;
if (((a_a & 9223372036854775807u) > 9218868437227405312u) || ((a_b & 9223372036854775807u) > 9218868437227405312u)) {
return wuffs_softfloat__arith__f64_propagate_nan(self, a_a, a_b);
} else if ((a_c & 9223372036854775807u) > 9218868437227405312u) {
return (a_c | 2251799813685248u);
}
v_sign_p = ((a_a ^ a_b) & 9223372036854775808u);
v_sign_c = (a_c & 9223372036854775808u);
v_exp_a = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_exp_b = ((uint32_t)(((a_b >> 52u) & 2047u)));
v_exp_c = ((uint32_t)(((a_c >> 52u) & 2047u)));
v_sig_a = (a_a & 4503599627370495u);
v_sig_b = (a_b & 4503599627370495u);
v_sig_c = (a_c & 4503599627370495u);
if ((v_exp_a == 2047u) || (v_exp_b == 2047u)) {
if (((a_a & 9223372036854775807u) == 0u) || ((a_b & 9223372036854775807u) == 0u)) {
return 18444492273895866368u;
} else if ((v_exp_c == 2047u) && (v_sign_c != v_sign_p)) {
return 18444492273895866368u;
}
return (v_sign_p | 9218868437227405312u);
} else if (v_exp_c == 2047u) {
return a_c;
}
if (((a_a & 9223372036854775807u) == 0u) || ((a_b & 9223372036854775807u) == 0u)) {
if ((a_c & 9223372036854775807u) == 0u) {
if (v_sign_p == v_sign_c) {
return a_c;
}
return 0u;
}
return a_c;
}
if (v_exp_a == 0u) {
v_exp_a = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_a);
v_sig_a = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_a);
}
if (v_exp_b == 0u) {
v_exp_b = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_b);
v_sig_b = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_b);
}
v_sig_a = ((v_sig_a & 4503599627370495u) | 4503599627370496u);
v_sig_b = ((v_sig_b & 4503599627370495u) | 4503599627370496u);
v_p_hi = wuffs_softfloat__arith__mul_hi64(self, v_sig_a, v_sig_b);
v_p_lo = ((uint64_t)(v_sig_a * v_sig_b));
v_p_hi = (((uint64_t)(v_p_hi << 20u)) | (v_p_lo >> 44u));
v_p_lo = ((uint64_t)(v_p_lo << 20u));
if ((a_c & 9223372036854775807u) == 0u) {
v_top = (127u - wuffs_softfloat__arith__clz64(self, v_p_hi));
v_exp_z = ((uint32_t)(((uint32_t)(((uint32_t)(v_exp_a + v_exp_b)) + v_top)) - 1148u));
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_p, v_exp_z, wuffs_softfloat__arith__jam128_lo(self, v_p_hi, v_p_lo, ((uint32_t)(v_top - 62u))));
}
if (v_exp_c == 0u) {
v_exp_c = wuffs_softfloat__arith__norm_sub_exp(self, v_sig_c);
v_sig_c = wuffs_softfloat__arith__norm_sub_sig(self, v_sig_c);
}
v_sig_c = ((v_sig_c & 4503599627370495u) | 4503599627370496u);
v_c_hi = ((uint64_t)(v_sig_c << 9u));
v_c_lo = 0u;
v_d = ((uint32_t)(((uint32_t)(((uint32_t)(v_exp_a + v_exp_b)) - v_exp_c)) - 1022u));
if (v_d < 2147483648u) {
v_tmp = wuffs_softfloat__arith__jam128_hi(self, v_c_hi, v_c_lo, v_d);
v_c_lo = wuffs_softfloat__arith__jam128_lo(self, v_c_hi, v_c_lo, v_d);
v_c_hi = v_tmp;
v_exp_z = ((uint32_t)(((uint32_t)(v_exp_a + v_exp_b)) - 1148u));
} else {
v_d = ((uint32_t)(0u - v_d));
v_tmp = wuffs_softfloat__arith__jam128_hi(self, v_p_hi, v_p_lo, v_d);
v_p_lo = wuffs_softfloat__arith__jam128_lo(self, v_p_hi, v_p_lo, v_d);
v_p_hi = v_tmp;
v_exp_z = ((uint32_t)(v_exp_c - 126u));
}
if (v_sign_p == v_sign_c) {
v_sign_z = v_sign_p;
v_z_lo = ((uint64_t)(v_p_lo + v_c_lo));
v_z_hi = ((uint64_t)(v_p_hi + v_c_hi));
if (v_z_lo < v_p_lo) {
v_z_hi = ((uint64_t)(v_z_hi + 1u));
}
} else {
if ((v_p_hi > v_c_hi) || ((v_p_hi == v_c_hi) && (v_p_lo >= v_c_lo))) {
v_sign_z = v_sign_p;
v_z_lo = ((uint64_t)(v_p_lo - v_c_lo));
v_z_hi = ((uint64_t)(v_p_hi - v_c_hi));
if (v_p_lo < v_c_lo) {
v_z_hi = ((uint64_t)(v_z_hi - 1u));
}
} else {
v_sign_z = v_sign_c;
v_z_lo = ((uint64_t)(v_c_lo - v_p_lo));
v_z_hi = ((uint64_t)(v_c_hi - v_p_hi));
if (v_c_lo < v_p_lo) {
v_z_hi = ((uint64_t)(v_z_hi - 1u));
}
}
if ((v_z_hi | v_z_lo) == 0u) {
return 0u;
}
}
if (v_z_hi != 0u) {
v_top = (127u - wuffs_softfloat__arith__clz64(self, v_z_hi));
} else {
v_top = (63u - wuffs_softfloat__arith__clz64(self, v_z_lo));
}
v_exp_z = ((uint32_t)(v_exp_z + v_top));
if (v_top >= 62u) {
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_z, v_exp_z, wuffs_softfloat__arith__jam128_lo(self, v_z_hi, v_z_lo, (v_top - 62u)));
}
return wuffs_softfloat__arith__round_pack_f64(self, v_sign_z, v_exp_z, ((uint64_t)(v_z_lo << (62u - v_top))));
}
// -------- func softfloat.arith.f64_from_u64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_u64(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__norm_round_pack_f64(self, 0u, 1084u, a_a);
}
// -------- func softfloat.arith.f64_from_u32
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_u32(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__norm_round_pack_f64(self, 0u, 1084u, ((uint64_t)(a_a)));
}
// -------- func softfloat.arith.f64_from_i64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_from_i64(
const wuffs_softfloat__arith* self,
int64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_m = 0;
if (a_a >= 0u) {
return wuffs_softfloat__arith__norm_round_pack_f64(self, 0u, 1084u, ((uint64_t)(a_a)));
}
v_m = (((uint64_t)((0u - (a_a + 1u)))) + 1u);
return wuffs_softfloat__arith__norm_round_pack_f64(self, 9223372036854775808u, 1084u, v_m);
}
// -------- func softfloat.arith.f64_to_u64_trunc
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f64_to_u64_trunc(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_exp = 0;
uint64_t v_sig = 0;
if (a_a >= 9223372036854775808u) {
return 0u;
} else if (a_a > 9218868437227405312u) {
return 0u;
}
v_exp = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_sig = ((a_a & 4503599627370495u) | 4503599627370496u);
if (v_exp < 1023u) {
return 0u;
} else if (v_exp >= 1087u) {
return 18446744073709551615u;
} else if (v_exp >= 1075u) {
return ((uint64_t)(v_sig << ((v_exp - 1075u) & 63u)));
}
return (v_sig >> ((1075u - v_exp) & 63u));
}
// -------- func softfloat.arith.f64_to_f32
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f64_to_f32(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_sign = 0;
uint32_t v_exp = 0;
uint64_t v_frac = 0;
uint32_t v_sig = 0;
v_sign = (((uint32_t)((a_a >> 32u))) & 2147483648u);
v_exp = ((uint32_t)(((a_a >> 52u) & 2047u)));
v_frac = (a_a & 4503599627370495u);
if (v_exp == 2047u) {
if (v_frac != 0u) {
return (v_sign | 2143289344u | ((uint32_t)((v_frac >> 29u))));
}
return (v_sign | 2139095040u);
}
v_sig = ((uint32_t)((wuffs_softfloat__arith__shift_right_jam64(self, v_frac, 22u) & 1073741823u)));
if ((v_exp == 0u) && (v_sig == 0u)) {
return v_sign;
}
return wuffs_softfloat__arith__round_pack_f32(self, v_sign, ((uint32_t)(v_exp - 897u)), (v_sig | 1073741824u));
}
// -------- func softfloat.arith.round_pack_f32
WUFFS_BASE__GENERATED_C_CODE
static uint32_t
wuffs_softfloat__arith__round_pack_f32(
const wuffs_softfloat__arith* self,
uint32_t a_sign,
uint32_t a_exp,
uint32_t a_sig) {
uint32_t v_e = 0;
uint32_t v_s = 0;
uint32_t v_round_bits = 0;
v_e = a_exp;
v_s = a_sig;
v_round_bits = (v_s & 127u);
if (v_e >= 2147483648u) {
v_s = ((uint32_t)(wuffs_softfloat__arith__shift_right_jam64(self, ((uint64_t)(v_s)), ((uint32_t)(0u - v_e)))));
v_e = 0u;
v_round_bits = (v_s & 127u);
} else if (v_e >= 253u) {
if ((v_e > 253u) || (v_s >= 2147483584u)) {
return (a_sign | 2139095040u);
}
}
v_s = (((uint32_t)(v_s + 64u)) >> 7u);
if (v_round_bits == 64u) {
v_s = (v_s & 4294967294u);
}
if (v_s == 0u) {
v_e = 0u;
}
return (a_sign | ((uint32_t)(((uint32_t)(v_e << 23u)) + v_s)));
}
// -------- func softfloat.arith.f32_to_f64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__f32_to_f64(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_sign = 0;
uint32_t v_exp = 0;
uint64_t v_frac = 0;
uint32_t v_top = 0;
v_sign = ((((uint64_t)(a_a)) >> 31u) << 63u);
v_exp = ((a_a >> 23u) & 255u);
v_frac = ((uint64_t)((a_a & 8388607u)));
if (v_exp == 255u) {
if (v_frac != 0u) {
return (v_sign | 9221120237041090560u | (v_frac << 29u));
}
return (v_sign | 9218868437227405312u);
} else if (v_exp == 0u) {
if (v_frac == 0u) {
return v_sign;
}
v_top = (63u - wuffs_softfloat__arith__clz64(self, v_frac));
if (v_top > 22u) {
return 0u;
}
return (v_sign | (((uint64_t)((v_top + 874u))) << 52u) | (((uint64_t)(v_frac << (52u - v_top))) & 4503599627370495u));
}
return (v_sign | (((uint64_t)((v_exp + 896u))) << 52u) | (v_frac << 29u));
}
// -------- func softfloat.arith.f32_is_nan
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_is_nan(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if ((a_a & 2147483647u) > 2139095040u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_neg
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_neg(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return (a_a ^ 2147483648u);
}
// -------- func softfloat.arith.f32_abs
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_abs(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return (a_a & 2147483647u);
}
// -------- func softfloat.arith.f32_add
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_add(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_add(self, wuffs_softfloat__arith__f32_to_f64(self, a_a), wuffs_softfloat__arith__f32_to_f64(self, a_b)));
}
// -------- func softfloat.arith.f32_sub
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_sub(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_sub(self, wuffs_softfloat__arith__f32_to_f64(self, a_a), wuffs_softfloat__arith__f32_to_f64(self, a_b)));
}
// -------- func softfloat.arith.f32_mul
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_mul(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_mul(self, wuffs_softfloat__arith__f32_to_f64(self, a_a), wuffs_softfloat__arith__f32_to_f64(self, a_b)));
}
// -------- func softfloat.arith.f32_div
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_div(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_div(self, wuffs_softfloat__arith__f32_to_f64(self, a_a), wuffs_softfloat__arith__f32_to_f64(self, a_b)));
}
// -------- func softfloat.arith.f32_cmp
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_cmp(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_ka = 0;
uint32_t v_kb = 0;
if (((a_a & 2147483647u) > 2139095040u) || ((a_b & 2147483647u) > 2139095040u)) {
return 3u;
} else if (((a_a | a_b) & 2147483647u) == 0u) {
return 1u;
}
v_ka = (a_a | 2147483648u);
if ((a_a >> 31u) != 0u) {
v_ka = (4294967295u ^ a_a);
}
v_kb = (a_b | 2147483648u);
if ((a_b >> 31u) != 0u) {
v_kb = (4294967295u ^ a_b);
}
if (v_ka < v_kb) {
return 0u;
} else if (v_ka == v_kb) {
return 1u;
}
return 2u;
}
// -------- func softfloat.arith.f32_eq
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_eq(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 1u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_ne
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_ne(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 1u) {
return 0u;
}
return 1u;
}
// -------- func softfloat.arith.f32_lt
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_lt(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 0u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_le
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_le(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) <= 1u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_gt
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_gt(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 2u) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_ge
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_ge(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_c = 0;
v_c = wuffs_softfloat__arith__f32_cmp(self, a_a, a_b);
if ((v_c == 1u) || (v_c == 2u)) {
return 1u;
}
return 0u;
}
// -------- func softfloat.arith.f32_min
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_min(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 0u) {
return a_a;
}
return a_b;
}
// -------- func softfloat.arith.f32_max
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_max(
const wuffs_softfloat__arith* self,
uint32_t a_a,
uint32_t a_b) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
if (wuffs_softfloat__arith__f32_cmp(self, a_a, a_b) == 2u) {
return a_a;
}
return a_b;
}
// -------- func softfloat.arith.f32_from_u32
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_u32(
const wuffs_softfloat__arith* self,
uint32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_from_u32(self, a_a));
}
// -------- func softfloat.arith.f32_from_i32
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_i32(
const wuffs_softfloat__arith* self,
int32_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_from_i64(self, ((int64_t)(a_a))));
}
// -------- func softfloat.arith.f32_from_u64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint32_t
wuffs_softfloat__arith__f32_from_u64(
const wuffs_softfloat__arith* self,
uint64_t a_a) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint32_t v_top = 0;
uint64_t v_x = 0;
v_x = a_a;
if (v_x >= 9007199254740992u) {
v_top = (63u - wuffs_softfloat__arith__clz64(self, v_x));
if (v_top >= 53u) {
v_x = ((uint64_t)(wuffs_softfloat__arith__shift_right_jam64(self, v_x, (v_top - 52u)) << (v_top - 52u)));
}
}
return wuffs_softfloat__arith__f64_to_f32(self, wuffs_softfloat__arith__f64_from_u64(self, v_x));
}
// -------- func softfloat.arith.log2_u64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__log2_u64(
const wuffs_softfloat__arith* self,
uint64_t a_v) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
return wuffs_softfloat__arith__log2_f64(self, wuffs_softfloat__arith__f64_from_u64(self, a_v));
}
// -------- func softfloat.arith.log2_f64
WUFFS_BASE__GENERATED_C_CODE
WUFFS_BASE__MAYBE_STATIC uint64_t
wuffs_softfloat__arith__log2_f64(
const wuffs_softfloat__arith* self,
uint64_t a_x) {
if (!self) {
return 0;
}
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
return 0;
}
uint64_t v_ix = 0;
uint64_t v_top = 0;
uint64_t v_tmp = 0;
uint64_t v_i = 0;
uint64_t v_k = 0;
uint64_t v_kd = 0;
uint64_t v_invc = 0;
uint64_t v_logc = 0;
uint64_t v_z = 0;
uint64_t v_r = 0;
uint64_t v_r2 = 0;
uint64_t v_r4 = 0;
uint64_t v_t1 = 0;
uint64_t v_t2 = 0;
uint64_t v_t3 = 0;
uint64_t v_hi = 0;
uint64_t v_lo = 0;
uint64_t v_p = 0;
uint64_t v_y = 0;
v_ix = a_x;
if (((uint64_t)(v_ix - 4606800540372828160u)) < 581272283906048u) {
if (v_ix == 4607182418800017408u) {
return 0u;
}
v_r = wuffs_softfloat__arith__f64_sub(self, v_ix, 4607182418800017408u);
v_hi = wuffs_softfloat__arith__f64_mul(self, 4609176140020449280u, v_r);
v_r2 = wuffs_softfloat__arith__f64_mul(self, v_r, v_r);
v_lo = wuffs_softfloat__arith__f64_fma(self, 4609176140020449280u, v_r, wuffs_softfloat__arith__f64_neg(self, v_hi));
v_r4 = wuffs_softfloat__arith__f64_mul(self, v_r2, v_r2);
v_p = wuffs_softfloat__arith__f64_fma(self, v_r, 4602334714382648311u, 13828044577248609022u);
v_lo = wuffs_softfloat__arith__f64_fma(self, v_r, 4460540536611119616u, v_lo);
v_y = wuffs_softfloat__arith__f64_fma(self, v_p, v_r2, v_hi);
v_lo = wuffs_softfloat__arith__f64_add(self, v_lo, wuffs_softfloat__arith__f64_fma(self, v_p, v_r2, wuffs_softfloat__arith__f64_sub(self, v_hi, v_y)));
v_t1 = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_r, 4596593529635268406u, 13821203151630798821u), v_r2, wuffs_softfloat__arith__f64_fma(self, v_r, 4598869476596800484u, 13823540977621236799u));
v_t2 = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_r, 4593868635039788646u, 13817758221876344892u), v_r2, wuffs_softfloat__arith__f64_fma(self, v_r, 4594943554432801610u, 13819037345097492379u));
v_t3 = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_t2, v_r4, v_t1), v_r4, v_lo);
return wuffs_softfloat__arith__f64_add(self, v_y, v_t3);
}
v_top = (v_ix >> 48u);
if (((uint64_t)(v_top - 16u)) >= 32736u) {
if ((v_ix & 9223372036854775807u) == 0u) {
return 18442240474082181120u;
} else if (v_ix == 9218868437227405312u) {
return v_ix;
} else if (((v_top & 32768u) != 0u) || ((v_top & 32752u) == 32752u)) {
if ((v_ix & 9223372036854775807u) > 9218868437227405312u) {
return (v_ix | 2251799813685248u);
}
return 18444492273895866368u;
}
v_ix = ((uint64_t)(wuffs_softfloat__arith__f64_mul(self, v_ix, 4841369599423283200u) - 234187180623265792u));
}
v_tmp = ((uint64_t)(v_ix - 4604367669032910848u));
v_i = ((v_tmp >> 46u) & 63u);
v_k = (v_tmp >> 52u);
if (v_k >= 2048u) {
v_kd = wuffs_softfloat__arith__f64_neg(self, wuffs_softfloat__arith__f64_from_u64(self, (4096u - v_k)));
} else {
v_kd = wuffs_softfloat__arith__f64_from_u64(self, v_k);
}
v_z = ((uint64_t)(v_ix - (v_tmp & 18442240474082181120u)));
v_invc = WUFFS_SOFTFLOAT__LOG2_TAB_INVC[v_i];
v_logc = WUFFS_SOFTFLOAT__LOG2_TAB_LOGC[v_i];
v_t3 = wuffs_softfloat__arith__f64_add(self, v_kd, v_logc);
v_r = wuffs_softfloat__arith__f64_fma(self, v_z, v_invc, 13830554455654793216u);
v_t1 = wuffs_softfloat__arith__f64_mul(self, 4609176140020449280u, v_r);
v_t2 = wuffs_softfloat__arith__f64_fma(self, v_r, 4460540536611119616u, wuffs_softfloat__arith__f64_fma(self, 4609176140020449280u, v_r, wuffs_softfloat__arith__f64_neg(self, v_t1)));
v_hi = wuffs_softfloat__arith__f64_add(self, v_t1, v_t3);
v_lo = wuffs_softfloat__arith__f64_add(self, wuffs_softfloat__arith__f64_add(self, wuffs_softfloat__arith__f64_sub(self, v_t3, v_hi), v_t1), v_t2);
v_r2 = wuffs_softfloat__arith__f64_mul(self, v_r, v_r);
v_r4 = wuffs_softfloat__arith__f64_mul(self, v_r2, v_r2);
v_p = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_r, 4598869476581264456u, 13823540977608962043u), v_r2, wuffs_softfloat__arith__f64_fma(self, v_r, 4602334714382648510u, 13828044577248609081u));
v_p = wuffs_softfloat__arith__f64_fma(self, wuffs_softfloat__arith__f64_fma(self, v_r, 4596594284514769198u, 13821203878746165948u), v_r4, v_p);
v_y = wuffs_softfloat__arith__f64_fma(self, v_r2, v_p, v_lo);
return wuffs_softfloat__arith__f64_add(self, v_y, v_hi);
}
#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__SOFTFLOAT)
#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__TARGA)
// ---------------- Status Codes Implementations
+646
View File
@@ -0,0 +1,646 @@
// 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-encoder-parity.c checks that Wuffs' std/brotli encoder
// produces exactly the same bytes as libbrotlienc v1.2.1 (at qualities 0 and
// 1) over the full parameter grid: every test input times
// - quality {0, 1}
// - lgwin {10, 16, 18, 22, 24}
// - mode {0, 1, 2}
// - input chunking {single, 1-byte (inputs up to 4 KiB only), 7-byte,
// 4096-byte, pseudo-random}
// - flush {none, every chunk, middle}
// plus metadata blocks (of 0, 1, 100 and 70000 bytes, at the start, middle
// and end), size_hint {0, exact, 1 << 30}, other parameters (which have no
// effect at qualities 0 and 1) and limited dst lengths.
//
// The test inputs are the files named on the command line (or, by default,
// every file in test/data/brotli plus test/data/{256.bytes,enwik5,romeo.txt})
// plus some synthetic inputs (1 MiB of zeroes, 256 KiB of pseudo-random
// bytes, a repeated 100-byte pattern and pseudo-random lengths 0 to 40).
//
// The test/c/std/brotli.c test program (run by "wuffs test -mimic
// std/brotli") checks a subset of this grid.
//
// Run it from the Wuffs root directory with:
//
// gcc -O2 -std=c99 script/manual-test-brotli-encoder-parity.c
// -lbrotlienc -lbrotlicommon -lm -o /tmp/brotli-enc-parity
//
// (as one command line).
// /tmp/brotli-enc-parity
//
// It prints each mismatching configuration (and the first differing offset)
// and exits with a non-zero status if there were any mismatches.
#define _POSIX_C_SOURCE 200809L
#include <dirent.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "brotli/encode.h"
#define WUFFS_IMPLEMENTATION
#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"
#define OP_PROCESS 0
#define OP_FLUSH 1
#define OP_FINISH 2
#define OP_EMIT_METADATA 3
#define MAX_STEPS (1 << 22)
#define MAX_PARAMS 16
#define MAX_INPUT (16 << 20)
#define MAX_OUTPUT (64 << 20)
static uint32_t g_step_ops[MAX_STEPS];
static size_t g_step_lens[MAX_STEPS];
static size_t g_num_steps;
static uint8_t* g_src; // The plan's source: input plus any metadata.
static size_t g_src_len;
static uint8_t* g_have; // Wuffs' output.
static uint8_t* g_want; // libbrotlienc's output.
static uint8_t* g_workbuf; // Wuffs' workbuf.
static size_t g_workbuf_len;
static uint64_t g_num_configs;
static uint64_t g_num_mismatches;
typedef struct {
uint32_t keys[MAX_PARAMS];
uint32_t values[MAX_PARAMS];
size_t num_params;
uint32_t chunking; // 0 single, 1 1-byte, 2 7-byte, 3 4096-byte, 4 random.
uint32_t flush; // 0 none, 1 every chunk, 2 middle.
uint32_t metadata_position; // 0 none, 1 start, 2 middle, 3 end.
size_t metadata_len;
size_t dst_chunk; // 0 means unlimited.
uint64_t seed;
} config;
static void //
add_param(config* c, uint32_t key, uint32_t value) {
if (c->num_params < MAX_PARAMS) {
c->keys[c->num_params] = key;
c->values[c->num_params] = value;
c->num_params++;
}
}
static uint64_t //
rand64(uint64_t* state) {
// SplitMix64.
uint64_t z = (*state += 0x9E3779B97F4A7C15ull);
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
return z ^ (z >> 31);
}
static void //
fill_random(uint8_t* p, size_t n, uint64_t seed) {
uint64_t state = seed;
for (size_t i = 0; i < n; i++) {
p[i] = (uint8_t)(rand64(&state) >> 56);
}
}
static bool //
add_step(uint32_t op, size_t len) {
if (g_num_steps >= MAX_STEPS) {
return false;
}
g_step_ops[g_num_steps] = op;
g_step_lens[g_num_steps] = len;
g_num_steps++;
return true;
}
// make_plan mirrors make_brotli_enc_plan in test/c/std/brotli.c.
static const char* //
make_plan(const config* c, const uint8_t* data, size_t data_len) {
g_num_steps = 0;
g_src_len = 0;
if ((data_len + c->metadata_len) > MAX_INPUT) {
return "input is too long";
}
uint64_t rng = c->seed;
size_t metadata_at = SIZE_MAX;
if (c->metadata_position == 1) {
metadata_at = 0;
} else if (c->metadata_position == 2) {
metadata_at = data_len / 2;
} else if (c->metadata_position == 3) {
metadata_at = data_len;
}
bool flushed_middle = false;
size_t i = 0;
while (true) {
if (i == metadata_at) {
fill_random(g_src + g_src_len, c->metadata_len, c->seed ^ 0x5A5A);
g_src_len += c->metadata_len;
if (!add_step(OP_EMIT_METADATA, c->metadata_len)) {
return "too many steps";
}
metadata_at = SIZE_MAX;
}
size_t n = data_len - i;
switch (c->chunking) {
case 1:
n = 1;
break;
case 2:
n = 7;
break;
case 3:
n = 4096;
break;
case 4: {
uint64_t r = rand64(&rng);
n = (size_t)(1 + ((r & 0xFF) < 0xF0 ? ((r >> 8) % 300)
: ((r >> 8) % 70000)));
break;
}
}
if ((metadata_at != SIZE_MAX) && (metadata_at > i)) {
n = (n < (metadata_at - i)) ? n : (metadata_at - i);
}
if (n > (data_len - i)) {
n = data_len - i;
}
memcpy(g_src + g_src_len, data + i, n);
g_src_len += n;
i += n;
bool last = (i == data_len) && (metadata_at == SIZE_MAX);
bool ok = true;
if (last) {
ok = add_step(OP_FINISH, n);
} else if (c->flush == 1) {
ok = add_step(OP_FLUSH, n);
} else {
ok = add_step(OP_PROCESS, n);
if ((c->flush == 2) && !flushed_middle && ((2 * i) >= data_len)) {
flushed_middle = true;
ok = ok && add_step(OP_FLUSH, 0);
}
}
if (!ok) {
return "too many steps";
}
if (last) {
break;
}
}
return NULL;
}
// brotli_enc_quirk_key maps a BrotliEncoderParameter value to the
// corresponding std/brotli quirk key (or 0 if there is none).
static uint32_t //
brotli_enc_quirk_key(uint32_t param) {
switch (param) {
case 0:
return WUFFS_BROTLI__QUIRK_MODE;
case 1:
return WUFFS_BROTLI__QUIRK_ENCODER_QUALITY;
case 2:
return WUFFS_BROTLI__QUIRK_LGWIN;
case 3:
return WUFFS_BROTLI__QUIRK_LGBLOCK;
case 4:
return WUFFS_BROTLI__QUIRK_DISABLE_LITERAL_CONTEXT_MODELING;
case 5:
return WUFFS_BROTLI__QUIRK_SIZE_HINT;
case 6:
return WUFFS_BROTLI__QUIRK_LARGE_WINDOW;
case 7:
return WUFFS_BROTLI__QUIRK_NPOSTFIX;
case 8:
return WUFFS_BROTLI__QUIRK_NDIRECT;
case 9:
return WUFFS_BROTLI__QUIRK_STREAM_OFFSET;
}
return 0;
}
// encode_wuffs mirrors wuffs_brotli_encode_plan in test/c/std/brotli.c.
static const char* //
encode_wuffs(const config* c, size_t* have_len) {
wuffs_brotli__encoder* enc = wuffs_brotli__encoder__alloc();
if (!enc) {
return "alloc failed";
}
const char* ret = NULL;
wuffs_base__io_buffer dst = wuffs_base__ptr_u8__writer(g_have, MAX_OUTPUT);
wuffs_base__io_buffer src = wuffs_base__ptr_u8__reader(g_src, g_src_len,
false);
src.meta.wi = 0;
for (size_t j = 0; j < c->num_params; j++) {
wuffs_base__status status = wuffs_brotli__encoder__set_quirk(
enc, brotli_enc_quirk_key(c->keys[j]), c->values[j]);
if (status.repr) {
ret = status.repr;
goto cleanup;
}
}
uint64_t wlen = wuffs_brotli__encoder__workbuf_len(enc).max_incl;
if (wlen > g_workbuf_len) {
ret = "workbuf_len is too large";
goto cleanup;
}
wuffs_base__slice_u8 workbuf =
wuffs_base__make_slice_u8(g_workbuf, (size_t)wlen);
size_t end = 0;
for (size_t i = 0; i < g_num_steps; i++) {
end += g_step_lens[i];
uint32_t op = g_step_ops[i];
wuffs_base__status status = wuffs_base__make_status(NULL);
if (op == OP_FLUSH) {
status = wuffs_brotli__encoder__request_flush(enc);
} else if (op == OP_EMIT_METADATA) {
status = wuffs_brotli__encoder__request_emit_metadata(enc, g_step_lens[i]);
}
if (status.repr) {
ret = status.repr;
goto cleanup;
}
while (true) {
wuffs_base__io_buffer d = dst;
if ((c->dst_chunk > 0) && ((d.data.len - d.meta.wi) > c->dst_chunk)) {
d.data.len = d.meta.wi + c->dst_chunk;
}
wuffs_base__io_buffer s = src;
s.meta.wi = end;
s.meta.closed = (op == OP_FINISH);
status = wuffs_brotli__encoder__transform_io(enc, &d, &s, workbuf);
dst.meta.wi = d.meta.wi;
src.meta.ri = s.meta.ri;
if (status.repr == NULL) {
if (op != OP_FINISH) {
ret = "unexpected ok";
goto cleanup;
}
break;
} else if (status.repr == wuffs_base__suspension__short_write) {
if (dst.meta.wi == dst.data.len) {
ret = "dst is full";
goto cleanup;
}
continue;
} else if ((status.repr == wuffs_base__suspension__short_read) &&
(op != OP_FINISH) && (src.meta.ri == end)) {
break;
}
ret = status.repr;
goto cleanup;
}
}
cleanup:
*have_len = dst.meta.wi;
free(enc);
return ret;
}
// encode_mimic mirrors mimic_brotli_encode in test/c/mimiclib/brotli.c.
static const char* //
encode_mimic(const config* c, size_t* want_len) {
*want_len = 0;
BrotliEncoderState* state = BrotliEncoderCreateInstance(NULL, NULL, NULL);
if (!state) {
return "BrotliEncoderCreateInstance failed";
}
const char* ret = NULL;
for (size_t j = 0; j < c->num_params; j++) {
if (!BrotliEncoderSetParameter(state, (BrotliEncoderParameter)(c->keys[j]),
c->values[j])) {
ret = "BrotliEncoderSetParameter failed";
goto cleanup;
}
}
const uint8_t* next_in = g_src;
for (size_t i = 0; i < g_num_steps; i++) {
size_t avail_in = g_step_lens[i];
BrotliEncoderOperation op = (BrotliEncoderOperation)(g_step_ops[i]);
while (true) {
size_t avail_out = MAX_OUTPUT - *want_len;
if ((c->dst_chunk > 0) && (avail_out > c->dst_chunk)) {
avail_out = c->dst_chunk;
}
size_t avail_out0 = avail_out;
uint8_t* next_out = g_want + *want_len;
if (!BrotliEncoderCompressStream(state, op, &avail_in, &next_in,
&avail_out, &next_out, NULL)) {
ret = "BrotliEncoderCompressStream failed";
goto cleanup;
}
*want_len += avail_out0 - avail_out;
if (op == BROTLI_OPERATION_FINISH) {
if (BrotliEncoderIsFinished(state)) {
break;
}
} else if ((avail_in == 0) && !BrotliEncoderHasMoreOutput(state)) {
break;
}
if (*want_len == MAX_OUTPUT) {
ret = "dst is full";
goto cleanup;
}
}
}
cleanup:
BrotliEncoderDestroyInstance(state);
return ret;
}
static void //
describe(char* buf, size_t n, const char* name, const config* c) {
int k = snprintf(buf, n, "%s:", name);
for (size_t j = 0; (j < c->num_params) && (k > 0) && ((size_t)k < n); j++) {
k += snprintf(buf + k, n - (size_t)k, " p%u=%u", c->keys[j], c->values[j]);
}
if ((k > 0) && ((size_t)k < n)) {
snprintf(buf + k, n - (size_t)k,
" chunking=%u flush=%u metadata=%u/%zu dst_chunk=%zu seed=0x%" PRIX64,
c->chunking, c->flush, c->metadata_position, c->metadata_len,
c->dst_chunk, c->seed);
}
}
static void //
check(const char* name, const config* c, const uint8_t* data, size_t len) {
char desc[512];
g_num_configs++;
const char* err = make_plan(c, data, len);
if (err) {
describe(desc, sizeof desc, name, c);
printf("SKIP %s: %s\n", desc, err);
return;
}
size_t have_len = 0;
size_t want_len = 0;
const char* have_err = encode_wuffs(c, &have_len);
const char* want_err = encode_mimic(c, &want_len);
if (have_err || want_err) {
g_num_mismatches++;
describe(desc, sizeof desc, name, c);
printf("MISMATCH %s: wuffs error \"%s\", mimic error \"%s\"\n", desc,
have_err ? have_err : "", want_err ? want_err : "");
fflush(stdout);
return;
}
size_t n = (have_len < want_len) ? have_len : want_len;
size_t i = 0;
while ((i < n) && (g_have[i] == g_want[i])) {
i++;
}
if ((i < n) || (have_len != want_len)) {
g_num_mismatches++;
describe(desc, sizeof desc, name, c);
printf("MISMATCH %s: lengths %zu (wuffs) vs %zu (mimic), first difference "
"at offset %zu\n",
desc, have_len, want_len, i);
fflush(stdout);
}
}
static config //
make_config(uint32_t quality, uint32_t lgwin) {
config c;
memset(&c, 0, sizeof c);
add_param(&c, 1, quality); // BROTLI_PARAM_QUALITY.
add_param(&c, 2, lgwin); // BROTLI_PARAM_LGWIN.
return c;
}
static void //
run_input(const char* name, const uint8_t* data, size_t len) {
static const uint32_t lgwins[] = {10, 16, 18, 22, 24};
static const size_t metadata_lens[] = {0, 1, 100, 70000};
uint64_t configs0 = g_num_configs;
uint64_t mismatches0 = g_num_mismatches;
time_t start = time(NULL);
// The main grid.
for (uint32_t q = 0; q <= 1; q++) {
for (size_t w = 0; w < 5; w++) {
for (uint32_t mode = 0; mode <= 2; mode++) {
for (uint32_t ch = 0; ch < 5; ch++) {
if ((ch == 1) && (len > 4096)) {
continue;
}
for (uint32_t fl = 0; fl < 3; fl++) {
config c = make_config(q, lgwins[w]);
add_param(&c, 0, mode); // BROTLI_PARAM_MODE.
c.chunking = ch;
c.flush = fl;
c.seed = (w << 8) | (mode << 4) | fl | ((uint64_t)len << 16);
check(name, &c, data, len);
}
}
}
}
}
// Metadata, size_hint, other parameters and limited dst lengths.
for (uint32_t q = 0; q <= 1; q++) {
for (size_t i = 0; i < 4; i++) {
for (uint32_t pos = 1; pos <= 3; pos++) {
for (uint32_t fl = 0; fl < 3; fl++) {
config c = make_config(q, 16);
c.chunking = 4;
c.flush = fl;
c.metadata_position = pos;
c.metadata_len = metadata_lens[i];
c.dst_chunk = (fl == 2) ? 5 : 0;
c.seed = (i << 8) | pos;
check(name, &c, data, len);
}
}
}
static const uint32_t extras[][2] = {
{5, 0}, // BROTLI_PARAM_SIZE_HINT.
{5, 0xFFFFFFFF}, // Replaced by the exact size.
{5, 1u << 30}, //
{3, 16}, // BROTLI_PARAM_LGBLOCK.
{3, 24}, //
{4, 1}, // BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING.
{6, 1}, // BROTLI_PARAM_LARGE_WINDOW.
{7, 2}, // BROTLI_PARAM_NPOSTFIX.
{8, 12}, // BROTLI_PARAM_NDIRECT.
{9, 1}, // BROTLI_PARAM_STREAM_OFFSET.
{9, 1000}, //
{9, 1u << 30}, //
{2, 0xFFFFFFFF}, // LGWIN (int)-1.
{2, 30}, //
{1, 0xFFFFFFFF}, // QUALITY (int)-1, so quality 0.
};
for (size_t i = 0; i < sizeof extras / sizeof extras[0]; i++) {
for (uint32_t ch = 0; ch < 5; ch += 4) {
config c = make_config(q, 22);
uint32_t v = extras[i][1];
if ((extras[i][0] == 5) && (v == 0xFFFFFFFF)) {
v = (uint32_t)len;
}
add_param(&c, extras[i][0], v);
c.chunking = ch;
c.flush = 2;
c.seed = i;
check(name, &c, data, len);
}
}
if (len <= (256 << 10)) {
static const size_t dst_chunks[] = {1, 2, 3, 7, 64, 4096};
for (size_t i = 0; i < 6; i++) {
for (uint32_t fl = 0; fl < 3; fl++) {
config c = make_config(q, (i & 1) ? 10 : 24);
c.chunking = (fl == 0) ? 0 : 4;
c.flush = fl;
c.dst_chunk = dst_chunks[i];
c.seed = i;
check(name, &c, data, len);
}
}
}
}
printf("%-50s %8zu bytes: %6" PRIu64 " configs, %" PRIu64
" mismatches (%lds)\n",
name, len, g_num_configs - configs0, g_num_mismatches - mismatches0,
(long)(time(NULL) - start));
fflush(stdout);
}
static uint8_t* //
read_whole_file(const char* path, size_t* len) {
FILE* f = fopen(path, "rb");
if (!f) {
return NULL;
}
uint8_t* p = malloc(MAX_INPUT);
if (p) {
*len = fread(p, 1, MAX_INPUT, f);
}
fclose(f);
return p;
}
static int //
compare_strings(const void* a, const void* b) {
return strcmp(*(const char* const*)a, *(const char* const*)b);
}
int //
main(int argc, char** argv) {
if (BrotliEncoderVersion() != 0x1002001) {
fprintf(stderr, "libbrotlienc version: have 0x%X, want 0x1002001\n",
BrotliEncoderVersion());
return 1;
}
g_src = malloc(MAX_INPUT);
g_have = malloc(MAX_OUTPUT);
g_want = malloc(MAX_OUTPUT);
g_workbuf_len = (2u << 24) + 503 + 16;
g_workbuf = malloc(g_workbuf_len);
if (!g_src || !g_have || !g_want || !g_workbuf) {
fprintf(stderr, "malloc failed\n");
return 1;
}
char* names[1024];
int num_names = 0;
if (argc > 1) {
for (int i = 1; (i < argc) && (num_names < 1024); i++) {
names[num_names++] = strdup(argv[i]);
}
} else {
names[num_names++] = strdup("test/data/256.bytes");
names[num_names++] = strdup("test/data/enwik5");
names[num_names++] = strdup("test/data/romeo.txt");
DIR* d = opendir("test/data/brotli");
if (!d) {
fprintf(stderr, "could not open test/data/brotli (run this program "
"from the Wuffs root directory)\n");
return 1;
}
struct dirent* e;
int first = num_names;
while ((e = readdir(d)) && (num_names < 1024)) {
if (e->d_name[0] == '.') {
continue;
}
char path[1024];
snprintf(path, sizeof path, "test/data/brotli/%s", e->d_name);
names[num_names++] = strdup(path);
}
closedir(d);
qsort(names + first, (size_t)(num_names - first), sizeof names[0],
compare_strings);
}
for (int i = 0; i < num_names; i++) {
size_t len = 0;
uint8_t* data = read_whole_file(names[i], &len);
if (!data) {
fprintf(stderr, "could not read %s\n", names[i]);
return 1;
}
run_input(names[i], data, len);
free(data);
}
if (argc <= 1) {
// Synthetic inputs.
uint8_t* p = malloc(1 << 20);
if (!p) {
return 1;
}
memset(p, 0, 1 << 20);
run_input("synthetic: 1 MiB of zeroes", p, 1 << 20);
fill_random(p, 256 << 10, 0x1234);
run_input("synthetic: 256 KiB of pseudo-random bytes", p, 256 << 10);
uint8_t pattern[100];
fill_random(pattern, 100, 0x5678);
for (size_t i = 0; i < (64 << 10); i++) {
p[i] = pattern[i % 100];
}
run_input("synthetic: a repeated 100-byte pattern", p, 64 << 10);
for (size_t n = 0; n <= 40; n++) {
fill_random(p, n, 0x9999 + n);
char name[64];
snprintf(name, sizeof name, "synthetic: %zu pseudo-random bytes", n);
run_input(name, p, n);
}
free(p);
}
printf("TOTAL: %" PRIu64 " configs, %" PRIu64 " mismatches\n", g_num_configs,
g_num_mismatches);
return g_num_mismatches ? 1 : 0;
}
+309 -2
View File
@@ -21,6 +21,12 @@ package main
//
// Usage: go run print-brotli-tables.go [brotli_c_dir] > common_tables.wuffs
//
// With a "-encoder" first argument, it instead prints the
// std/brotli/common_encoder_tables.wuffs file (the tables used only by the
// encoder, from the brotli source's "enc" directory):
//
// go run print-brotli-tables.go -encoder [brotli_c_dir] > common_encoder_tables.wuffs
//
// The optional brotli_c_dir argument defaults to /home/user/brotli-ref/c. It
// is the "c" directory of a brotli source tree (the one that contains the
// "common" and "dec" directories).
@@ -29,6 +35,7 @@ import (
"bytes"
"errors"
"fmt"
"math"
"os"
"path/filepath"
"regexp"
@@ -37,7 +44,12 @@ import (
)
func main() {
if err := main1(); err != nil {
f := main1
if (len(os.Args) > 1) && (os.Args[1] == "-encoder") {
os.Args = append(os.Args[:1], os.Args[2:]...)
f = mainEncoder
}
if err := f(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
@@ -351,7 +363,9 @@ func printTable(w *bytes.Buffer, comment string, name string, elemType string,
}
format := "%d,"
if hex {
if maxValue > 0xFF {
if maxValue > 0xFFFF_FFFF {
format = "0x%016X,"
} else if maxValue > 0xFF {
format = "0x%04X,"
} else {
format = "0x%02X,"
@@ -615,3 +629,296 @@ const header = `// Copyright 2026 The Wuffs Authors.
// SPDX-License-Identifier: Apache-2.0 OR MIT
`
// ---- Encoder tables.
func mainEncoder() error {
dir := "/home/user/brotli-ref/c"
if len(os.Args) > 1 {
dir = os.Args[1]
}
readFile := func(name string) (string, error) {
b, err := os.ReadFile(filepath.Join(dir, name))
if err != nil {
return "", err
}
return stripComments(string(b)), nil
}
w := &bytes.Buffer{}
w.WriteString(header)
fmt.Fprintf(w, "// The tables in this file were generated by script/print-brotli-tables.go\n")
fmt.Fprintf(w, "// (with the -encoder flag) from the brotli (C) source code. Do not edit by\n")
fmt.Fprintf(w, "// hand. These tables are only used by the encoder.\n\n")
// ---- enc/fast_log.c
fastLogC, err := readFile("enc/fast_log.c")
if err != nil {
return err
}
log2Body, err := findBraces(fastLogC, "kBrotliLog2Table")
if err != nil {
return err
}
log2Table := []int64(nil)
for _, tok := range strings.Split(log2Body, ",") {
tok = strings.TrimSpace(tok)
if tok == "" {
continue
}
// The C literals are float (not double) literals, widened to double.
if !strings.HasSuffix(tok, "f") {
return fmt.Errorf("kBrotliLog2Table: %q is not a float literal", tok)
}
v, err := strconv.ParseFloat(strings.TrimSuffix(tok, "f"), 32)
if err != nil {
return fmt.Errorf("kBrotliLog2Table: could not parse %q", tok)
}
log2Table = append(log2Table, int64(math.Float64bits(float64(float32(v)))))
}
if len(log2Table) != 256 {
return fmt.Errorf("kBrotliLog2Table: have %d elements, want 256", len(log2Table))
}
printTable(w, "From enc/fast_log.c kBrotliLog2Table, as the bit patterns of IEEE 754\n"+
"// binary64 values. The C literals are float literals widened to double.",
"LOG2_TABLE", "base.u64", log2Table, 4, true)
// ---- enc/encode.c
encodeC, err := readFile("enc/encode.c")
if err != nil {
return err
}
defaultCommandDepths, err := parseIntsAfterPadded(encodeC, "kDefaultCommandDepths", 128)
if err != nil {
return err
}
defaultCommandBits, err := parseIntsAfterPadded(encodeC, "kDefaultCommandBits", 128)
if err != nil {
return err
}
defaultCommandCode, err := parseIntsAfter(encodeC, "kDefaultCommandCode", -1)
if err != nil {
return err
}
numBits, err := parseScalarAfter(encodeC, "kDefaultCommandCodeNumBits")
if err != nil {
return err
}
if n := int64(len(defaultCommandCode)); (numBits+7)/8 > n {
return fmt.Errorf("kDefaultCommandCode: inconsistent length")
}
fmt.Fprintf(w, "// From enc/encode.c kDefaultCommandCodeNumBits.\n")
fmt.Fprintf(w, "pri const DEFAULT_COMMAND_CODE_NUM_BITS : base.u32 = %d\n\n", numBits)
printTable(w, "From enc/encode.c kDefaultCommandDepths.",
"DEFAULT_COMMAND_DEPTHS", "base.u8[..= 15]", defaultCommandDepths, 16, false)
printTable(w, "From enc/encode.c kDefaultCommandBits.",
"DEFAULT_COMMAND_BITS", "base.u16", defaultCommandBits, 16, false)
printTable(w, "From enc/encode.c kDefaultCommandCode.",
"DEFAULT_COMMAND_CODE", "base.u8", defaultCommandCode, 16, true)
// ---- enc/compress_fragment.c
compressFragmentC, err := readFile("enc/compress_fragment.c")
if err != nil {
return err
}
cmdHistoSeed, err := parseIntsAfter(compressFragmentC, "kCmdHistoSeed", 128)
if err != nil {
return err
}
printTable(w, "From enc/compress_fragment.c kCmdHistoSeed.",
"CMD_HISTO_SEED", "base.u32[..= 1]", cmdHistoSeed, 16, false)
// ---- enc/compress_fragment_two_pass.c
twoPassC, err := readFile("enc/compress_fragment_two_pass.c")
if err != nil {
return err
}
numExtraBits, err := parseIntsAfter(twoPassC, "kNumExtraBits", 128)
if err != nil {
return err
}
insertOffset, err := parseIntsAfter(twoPassC, "kInsertOffset", 24)
if err != nil {
return err
}
printTable(w, "From enc/compress_fragment_two_pass.c kNumExtraBits.",
"TWO_PASS_NUM_EXTRA_BITS", "base.u32[..= 24]", numExtraBits, 16, false)
printTable(w, "From enc/compress_fragment_two_pass.c kInsertOffset.",
"TWO_PASS_INSERT_OFFSET", "base.u32[..= 22594]", insertOffset, 12, false)
// ---- enc/entropy_encode.c
entropyEncodeC, err := readFile("enc/entropy_encode.c")
if err != nil {
return err
}
shellGaps, err := parseIntsAfter(entropyEncodeC, "kBrotliShellGaps", 6)
if err != nil {
return err
}
reverseLut, err := parseIntsAfter(entropyEncodeC, "kLut", 16)
if err != nil {
return err
}
printTable(w, "From enc/entropy_encode.c kBrotliShellGaps.",
"SHELL_GAPS", "base.u32[..= 132]", shellGaps, 16, false)
printTable(w, "From enc/entropy_encode.c kLut (in BrotliReverseBits).",
"REVERSE_BITS_LUT", "base.u32[..= 15]", reverseLut, 16, true)
// ---- enc/entropy_encode_static.h
staticH, err := readFile("enc/entropy_encode_static.h")
if err != nil {
return err
}
codeLengthDepth, err := parseIntsAfter(staticH, "kCodeLengthDepth", 18)
if err != nil {
return err
}
codeLengthBits, err := parseIntsAfter(staticH, "kCodeLengthBits", 18)
if err != nil {
return err
}
zeroRepsBits, err := parseIntsAfter(staticH, "kZeroRepsBits", 704)
if err != nil {
return err
}
zeroRepsDepth, err := parseIntsAfter(staticH, "kZeroRepsDepth", 704)
if err != nil {
return err
}
nonZeroRepsBits, err := parseIntsAfter(staticH, "kNonZeroRepsBits", 704)
if err != nil {
return err
}
nonZeroRepsDepth, err := parseIntsAfter(staticH, "kNonZeroRepsDepth", 704)
if err != nil {
return err
}
staticCodeLengthCode, err := parseMakeUint64After(staticH, "StoreStaticCodeLengthCode")
if err != nil {
return err
}
maxDepth := func(name string, vs []int64) error {
for _, v := range vs {
if (v < 0) || (v > 56) {
return fmt.Errorf("%s: depth %d out of range", name, v)
}
}
return nil
}
for _, x := range []struct {
name string
vs []int64
}{
{"kCodeLengthDepth", codeLengthDepth},
{"kZeroRepsDepth", zeroRepsDepth},
{"kNonZeroRepsDepth", nonZeroRepsDepth},
} {
if err := maxDepth(x.name, x.vs); err != nil {
return err
}
}
fmt.Fprintf(w, "// From enc/entropy_encode_static.h StoreStaticCodeLengthCode: 40 bits.\n")
fmt.Fprintf(w, "pri const STATIC_CODE_LENGTH_CODE : base.u64 = 0x%X\n\n", staticCodeLengthCode)
printTable(w, "From enc/entropy_encode_static.h kCodeLengthDepth.",
"CODE_LENGTH_DEPTH", "base.u32[..= 56]", codeLengthDepth, 18, false)
printTable(w, "From enc/entropy_encode_static.h kCodeLengthBits.",
"CODE_LENGTH_BITS", "base.u64", codeLengthBits, 18, false)
printTable(w, "From enc/entropy_encode_static.h kZeroRepsBits.",
"ZERO_REPS_BITS", "base.u64", zeroRepsBits, 8, true)
printTable(w, "From enc/entropy_encode_static.h kZeroRepsDepth.",
"ZERO_REPS_DEPTH", "base.u32[..= 56]", zeroRepsDepth, 16, false)
printTable(w, "From enc/entropy_encode_static.h kNonZeroRepsBits.",
"NON_ZERO_REPS_BITS", "base.u64", nonZeroRepsBits, 8, true)
printTable(w, "From enc/entropy_encode_static.h kNonZeroRepsDepth.",
"NON_ZERO_REPS_DEPTH", "base.u32[..= 56]", nonZeroRepsDepth, 16, false)
// ---- enc/brotli_bit_stream.c
bitStreamC, err := readFile("enc/brotli_bit_stream.c")
if err != nil {
return err
}
storageOrder, err := parseIntsAfter(bitStreamC, "kStorageOrder", 18)
if err != nil {
return err
}
hblhcSymbols, err := parseIntsAfter(bitStreamC, "kHuffmanBitLengthHuffmanCodeSymbols", 6)
if err != nil {
return err
}
hblhcBitLengths, err := parseIntsAfter(bitStreamC, "kHuffmanBitLengthHuffmanCodeBitLengths", 6)
if err != nil {
return err
}
printTable(w, "From enc/brotli_bit_stream.c kStorageOrder.",
"STORAGE_ORDER", "base.u8[..= 17]", storageOrder, 18, false)
printTable(w, "From enc/brotli_bit_stream.c kHuffmanBitLengthHuffmanCodeSymbols.",
"HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_SYMBOLS", "base.u64[..= 15]", hblhcSymbols, 16, false)
printTable(w, "From enc/brotli_bit_stream.c kHuffmanBitLengthHuffmanCodeBitLengths.",
"HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_BIT_LENGTHS", "base.u32[..= 4]", hblhcBitLengths, 16, false)
// Drop the final blank line.
_, err = os.Stdout.Write(bytes.TrimSuffix(w.Bytes(), []byte("\n")))
return err
}
var scalarRE = regexp.MustCompile(`^\s*=\s*([0-9]+)[uU]?\s*;`)
// parseScalarAfter parses the "= 123;" integer initializer after name.
func parseScalarAfter(s string, name string) (int64, error) {
i := strings.Index(s, name)
if i < 0 {
return 0, fmt.Errorf("could not find %q", name)
}
m := scalarRE.FindStringSubmatch(s[i+len(name):])
if m == nil {
return 0, fmt.Errorf("could not parse %q's value", name)
}
return strconv.ParseInt(m[1], 10, 64)
}
var makeUint64RE = regexp.MustCompile(`BROTLI_MAKE_UINT64_T\(\s*(0x[0-9A-Fa-f]+)[uU]?\s*,\s*(0x[0-9A-Fa-f]+)[uU]?\s*\)`)
// parseMakeUint64After parses the first BROTLI_MAKE_UINT64_T(high, low) after
// name.
func parseMakeUint64After(s string, name string) (int64, error) {
i := strings.Index(s, name)
if i < 0 {
return 0, fmt.Errorf("could not find %q", name)
}
m := makeUint64RE.FindStringSubmatch(s[i:])
if m == nil {
return 0, fmt.Errorf("could not find %q's BROTLI_MAKE_UINT64_T", name)
}
hi, err := strconv.ParseUint(m[1], 0, 32)
if err != nil {
return 0, err
}
lo, err := strconv.ParseUint(m[2], 0, 32)
if err != nil {
return 0, err
}
return int64((hi << 32) | lo), nil
}
// parseIntsAfterPadded is like parseIntsAfter but allows fewer than wantLen
// initializers, padding with zeroes (C's implicit zero-initialization).
func parseIntsAfterPadded(s string, name string, wantLen int) ([]int64, error) {
ret, err := parseIntsAfter(s, name, -1)
if err != nil {
return nil, err
} else if len(ret) > wantLen {
return nil, fmt.Errorf("%s: have %d elements, want at most %d", name, len(ret), wantLen)
}
for len(ret) < wantLen {
ret = append(ret, 0)
}
return ret, nil
}
+172
View File
@@ -152,3 +152,175 @@ invariants for memory safety but Wuffs requires explicit bounds checks.
- Fast paths.
- Fuzzers.
- An encoder.
## Encoder
This package also has an `encoder`, which implements the `base.io_transformer`
interface. It aims for byte-exact parity with the reference implementation's
encoder (libbrotlienc), version v1.2.1: for the same parameters and the same
sequence of operations, it produces exactly the same bytes. Like the decoder,
the Wuffs code mirrors the C code function by function, with comments like
"Mirrors BrotliCompressFragmentFast in compress_fragment.c". Its floating
point arithmetic (the C code's `double`s) uses `std/softfloat`, so that the
results are bit-exact regardless of the compiler and CPU.
Only qualities 0 and 1 (the C code's `FAST_ONE_PASS_COMPRESSION_QUALITY` and
`FAST_TWO_PASS_COMPRESSION_QUALITY`, implemented by `compress_fragment.c` and
`compress_fragment_two_pass.c`) are implemented. For qualities 2 to 11
(including the default quality, 11), `transform_io` returns
`"#unsupported encoder quality"`.
The source files are:
- `encode_brotli.wuffs`: the `encoder` struct, its public methods and the
`encode.c` streaming logic (`BrotliEncoderCompressStream`,
`BrotliEncoderCompressStreamFast`, `ProcessMetadata`, `EnsureInitialized`,
`InjectBytePaddingBlock`, etc).
- `encode_quirks.wuffs`: the encoder's quirk keys.
- `encode_bit_writer.wuffs`: `write_bits.h`, `fast_log.h`,
`find_match_length.h` and small `brotli_bit_stream.c` helpers.
- `encode_entropy.wuffs`: `entropy_encode.c` (Huffman tree construction) and
the `brotli_bit_stream.c` functions that store Huffman codes.
- `encode_fragment_fast.wuffs`: `compress_fragment.c` (quality 0).
- `encode_fragment_two_pass.wuffs`: `compress_fragment_two_pass.c` (quality
1).
- `common_encoder_tables.wuffs`: tables generated by `go run
script/print-brotli-tables.go -encoder` from the C code.
### Encoder API
In addition to the `base.io_transformer` methods, the encoder has:
- `request_flush!() status`: the next `transform_io` call acts like the C
code's `BROTLI_OPERATION_FLUSH` (instead of `PROCESS` or `FINISH`) until
the flush is complete. It returns `"#bad call sequence"` while a metadata
request is pending.
- `request_emit_metadata!(length: u64) status`: the next `transform_io` call
acts like `BROTLI_OPERATION_EMIT_METADATA`, with the next `length` bytes of
`src` as the metadata block's contents. `length` can be at most `1 << 24`
(otherwise `"#bad argument"`). It returns `"#bad call sequence"` while
another request is pending.
- `max_compressed_size(input_size: u64) u64`: mirrors
`BrotliEncoderMaxCompressedSize` (0 means overflow).
`dst_history_retain_length` is 0: the encoder never re-reads `dst`.
### Encoder Quirks
Each quirk mirrors one `BrotliEncoderParameter`, with the value semantics of
`BrotliEncoderSetParameter`. Values are stored as is (and must fit in a u32,
otherwise `set_quirk` returns `"#bad argument"`). Like the C code, they are
sanitized (`SanitizeParams`, `ComputeLgBlock`, `ChooseDistanceParams`) at the
first `transform_io` call, after which `set_quirk` returns
`"#bad call sequence"`.
| Quirk | C parameter |
| ---------------------------------------- | ---------------------------------------------- |
| `QUIRK_ENCODER_QUALITY` | `BROTLI_PARAM_QUALITY` (default 11) |
| `QUIRK_LGWIN` | `BROTLI_PARAM_LGWIN` (default 22) |
| `QUIRK_LGBLOCK` | `BROTLI_PARAM_LGBLOCK` |
| `QUIRK_MODE` | `BROTLI_PARAM_MODE` |
| `QUIRK_DISABLE_LITERAL_CONTEXT_MODELING` | `BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING` (0 or 1) |
| `QUIRK_SIZE_HINT` | `BROTLI_PARAM_SIZE_HINT` |
| `QUIRK_LARGE_WINDOW` (shared with the decoder) | `BROTLI_PARAM_LARGE_WINDOW` |
| `QUIRK_NPOSTFIX` | `BROTLI_PARAM_NPOSTFIX` |
| `QUIRK_NDIRECT` | `BROTLI_PARAM_NDIRECT` |
| `QUIRK_STREAM_OFFSET` | `BROTLI_PARAM_STREAM_OFFSET` (at most `1 << 30`) |
The quality quirk is not named `QUIRK_QUALITY` because that name would clash
with `base.QUIRK_QUALITY`. At qualities 0 and 1, only the quality, lgwin and
stream offset affect the output (lgblock, mode, size hint, large window,
npostfix, ndirect and literal context modeling are stored, sanitized and then
ignored, as in the C code).
### Encoder Workbuf Layout
At qualities 0 and 1, the workbuf is the C code's metablock storage (its
`GetBrotliStorage` buffer): `workbuf_len` is `(2 << lgwin) + 503 + 16` bytes
(the storage for a `1 << lgwin` byte block plus 16 bytes of slack for 8-byte
bit writes), where lgwin is the sanitized value. A shorter workbuf still works
if every input block is shorter: a block of `n` bytes needs `(2 * n) + 519`
bytes, otherwise `transform_io` returns `"#bad workbuf length"`. At other
qualities, `workbuf_len` is 0.
The hash table (up to `1 << 17` entries), the quality 1 command and literal
buffers (`1 << 17` entries each) and the Huffman scratch space are arrays in
the `encoder` struct (which is therefore more than 1 MiB). They do not need
zero-initializing: `WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED` is
fine. Unlike the C code, the input block is not copied: the encoder compresses
directly from `src`.
### Encoder Operation Mapping
Each `transform_io` call (initial or resumed) corresponds to one or more
`BrotliEncoderCompressStream` calls, with `src`'s readable bytes as the C
code's `available_in` and `dst`'s writable bytes as `available_out`. The
operation is:
- `EMIT_METADATA` if `request_emit_metadata` is pending (until the metadata
block is complete),
- otherwise `FLUSH` if `request_flush` is pending (until the flush is
complete),
- otherwise `FINISH` if `src` is closed,
- otherwise `PROCESS`.
`transform_io` suspends with `"$short write"` when there is pending output
(the C code's `BrotliEncoderHasMoreOutput`) but `dst` is full, suspends with
`"$short read"` when all of `src` is consumed (and, for a flush, the flush is
complete) and returns ok once the stream is finished (the C code's
`BrotliEncoderIsFinished`). So these two loops produce the same bytes:
- C: for each chunk, call `BrotliEncoderCompressStream(op)` until all of the
chunk is consumed and there is no more output (or, for `FINISH`, until
finished).
- Wuffs: for each chunk, call `request_flush` (for `FLUSH`) or
`request_emit_metadata(len)` (for `EMIT_METADATA`), make `src` hold exactly
that chunk (closed for `FINISH`) and call `transform_io` until it returns
`"$short read"` (or ok for `FINISH`), giving it more `dst` room after each
`"$short write"`.
At qualities 0 and 1 the C code compresses each call's available input
immediately (in blocks of at most `1 << lgwin` bytes), so the output depends
on how the input is split into chunks. The test programs' encode drivers
(`wuffs_brotli_encode_plan` in `test/c/std/brotli.c` and
`mimic_brotli_encode` in `test/c/mimiclib/brotli.c`) run the same "plan" of
(operation, length) steps with each library.
### Encoder Testing
`wuffs test -mimic std/brotli` checks byte-exact parity with libbrotlienc
over a grid of qualities, lgwins, modes, input chunkings, flushes, metadata
blocks, size hints and other parameters, as well as round trips through the
decoder. `script/manual-test-brotli-encoder-parity.c` runs a larger grid
(about 86 thousand configurations over every file in `test/data/brotli` and
some synthetic inputs).
### Encoder Differences from the C Code
- The input block is not copied (see the workbuf layout above), and output is
always staged in the workbuf (or a small internal buffer) and then copied to
`dst`. The C code sometimes writes directly to `next_out`. The bytes are the
same.
- Wuffs' C code generator does not support `slice base.u16` or
`slice base.u32` arguments (and mishandles `copy_from_slice` on `base.u16`
arrays), so some functions (e.g. `create_huffman_tree`,
`convert_bit_depths_to_symbols`) take a selector naming one of the
encoder's arrays instead of a slice.
- Like the C code, there is no fallback to an uncompressed stream (that is
only in the C code's one-shot `BrotliEncoderCompress`).
### Encoder TODO
- Qualities 2 to 11 (the C code's `EncodeData`, hashers, block splitting,
context modeling, Zopfli, etc).
- Large-window encoding (only used at qualities 3 and above).
- Custom and shared dictionaries (`BrotliEncoderAttachPreparedDictionary`).
- Changing parameters mid-stream.
+558
View File
@@ -0,0 +1,558 @@
// 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
// The tables in this file were generated by script/print-brotli-tables.go
// (with the -encoder flag) from the brotli (C) source code. Do not edit by
// hand. These tables are only used by the encoder.
// From enc/fast_log.c kBrotliLog2Table, as the bit patterns of IEEE 754
// binary64 values. The C literals are float literals widened to double.
pri const LOG2_TABLE : roarray[256] base.u64 = [
0x0000_0000_0000_0000, 0x0000_0000_0000_0000, 0x3FF0_0000_0000_0000, 0x3FF9_5C01_A000_0000,
0x4000_0000_0000_0000, 0x4002_934F_0000_0000, 0x4004_AE00_E000_0000, 0x4006_7576_8000_0000,
0x4008_0000_0000_0000, 0x4009_5C01_A000_0000, 0x400A_934F_0000_0000, 0x400B_ACEA_8000_0000,
0x400C_AE00_E000_0000, 0x400D_9A80_2000_0000, 0x400E_7576_8000_0000, 0x400F_414F_E000_0000,
0x4010_0000_0000_0000, 0x4010_598F_E000_0000, 0x4010_AE00_E000_0000, 0x4010_FDE0_C000_0000,
0x4011_49A7_8000_0000, 0x4011_91BB_A000_0000, 0x4011_D675_4000_0000, 0x4012_1820_A000_0000,
0x4012_5700_6000_0000, 0x4012_934F_0000_0000, 0x4012_CD40_2000_0000, 0x4013_0501_4000_0000,
0x4013_3ABB_4000_0000, 0x4013_6E92_A000_0000, 0x4013_A0A7_E000_0000, 0x4013_D118_E000_0000,
0x4014_0000_0000_0000, 0x4014_2D75_A000_0000, 0x4014_598F_E000_0000, 0x4014_8462_C000_0000,
0x4014_AE00_E000_0000, 0x4014_D67B_0000_0000, 0x4014_FDE0_C000_0000, 0x4015_2440_8000_0000,
0x4015_49A7_8000_0000, 0x4015_6E22_2000_0000, 0x4015_91BB_A000_0000, 0x4015_B47E_C000_0000,
0x4015_D675_4000_0000, 0x4015_F7A8_6000_0000, 0x4016_1820_A000_0000, 0x4016_37E6_2000_0000,
0x4016_5700_6000_0000, 0x4016_7576_8000_0000, 0x4016_934F_0000_0000, 0x4016_B090_4000_0000,
0x4016_CD40_2000_0000, 0x4016_E964_0000_0000, 0x4017_0501_4000_0000, 0x4017_201C_C000_0000,
0x4017_3ABB_4000_0000, 0x4017_54E1_2000_0000, 0x4017_6E92_A000_0000, 0x4017_87D3_A000_0000,
0x4017_A0A7_E000_0000, 0x4017_B913_4000_0000, 0x4017_D118_E000_0000, 0x4017_E8BC_2000_0000,
0x4018_0000_0000_0000, 0x4018_16E7_A000_0000, 0x4018_2D75_A000_0000, 0x4018_43AC_E000_0000,
0x4018_598F_E000_0000, 0x4018_6F21_0000_0000, 0x4018_8462_C000_0000, 0x4018_9957_4000_0000,
0x4018_AE00_E000_0000, 0x4018_C261_6000_0000, 0x4018_D67B_0000_0000, 0x4018_EA4F_8000_0000,
0x4018_FDE0_C000_0000, 0x4019_1130_8000_0000, 0x4019_2440_8000_0000, 0x4019_3712_4000_0000,
0x4019_49A7_8000_0000, 0x4019_5C01_A000_0000, 0x4019_6E22_2000_0000, 0x4019_800A_6000_0000,
0x4019_91BB_A000_0000, 0x4019_A337_6000_0000, 0x4019_B47E_C000_0000, 0x4019_C593_0000_0000,
0x4019_D675_4000_0000, 0x4019_E726_A000_0000, 0x4019_F7A8_6000_0000, 0x401A_07FB_6000_0000,
0x401A_1820_A000_0000, 0x401A_2819_4000_0000, 0x401A_37E6_2000_0000, 0x401A_4788_4000_0000,
0x401A_5700_6000_0000, 0x401A_664F_8000_0000, 0x401A_7576_8000_0000, 0x401A_8476_0000_0000,
0x401A_934F_0000_0000, 0x401A_A202_4000_0000, 0x401A_B090_4000_0000, 0x401A_BEFA_0000_0000,
0x401A_CD40_2000_0000, 0x401A_DB63_2000_0000, 0x401A_E964_0000_0000, 0x401A_F743_2000_0000,
0x401B_0501_4000_0000, 0x401B_129E_E000_0000, 0x401B_201C_C000_0000, 0x401B_2D7B_6000_0000,
0x401B_3ABB_4000_0000, 0x401B_47DD_0000_0000, 0x401B_54E1_2000_0000, 0x401B_61C8_2000_0000,
0x401B_6E92_A000_0000, 0x401B_7B40_E000_0000, 0x401B_87D3_A000_0000, 0x401B_944B_2000_0000,
0x401B_A0A7_E000_0000, 0x401B_ACEA_8000_0000, 0x401B_B913_4000_0000, 0x401B_C522_8000_0000,
0x401B_D118_E000_0000, 0x401B_DCF6_8000_0000, 0x401B_E8BC_2000_0000, 0x401B_F469_C000_0000,
0x401C_0000_0000_0000, 0x401C_0B7F_2000_0000, 0x401C_16E7_A000_0000, 0x401C_2239_A000_0000,
0x401C_2D75_A000_0000, 0x401C_389C_0000_0000, 0x401C_43AC_E000_0000, 0x401C_4EA8_C000_0000,
0x401C_598F_E000_0000, 0x401C_6462_8000_0000, 0x401C_6F21_0000_0000, 0x401C_79CB_C000_0000,
0x401C_8462_C000_0000, 0x401C_8EE6_8000_0000, 0x401C_9957_4000_0000, 0x401C_A3B5_4000_0000,
0x401C_AE00_E000_0000, 0x401C_B83A_2000_0000, 0x401C_C261_6000_0000, 0x401C_CC76_E000_0000,
0x401C_D67B_0000_0000, 0x401C_E06D_C000_0000, 0x401C_EA4F_8000_0000, 0x401C_F420_6000_0000,
0x401C_FDE0_C000_0000, 0x401D_0790_A000_0000, 0x401D_1130_8000_0000, 0x401D_1AC0_6000_0000,
0x401D_2440_8000_0000, 0x401D_2DB1_0000_0000, 0x401D_3712_4000_0000, 0x401D_4064_6000_0000,
0x401D_49A7_8000_0000, 0x401D_52DB_E000_0000, 0x401D_5C01_A000_0000, 0x401D_6519_0000_0000,
0x401D_6E22_2000_0000, 0x401D_771D_2000_0000, 0x401D_800A_6000_0000, 0x401D_88E9_C000_0000,
0x401D_91BB_A000_0000, 0x401D_9A80_2000_0000, 0x401D_A337_6000_0000, 0x401D_ABE1_8000_0000,
0x401D_B47E_C000_0000, 0x401D_BD0F_2000_0000, 0x401D_C593_0000_0000, 0x401D_CE0A_4000_0000,
0x401D_D675_4000_0000, 0x401D_DED4_0000_0000, 0x401D_E726_A000_0000, 0x401D_EF6D_6000_0000,
0x401D_F7A8_6000_0000, 0x401D_FFD7_A000_0000, 0x401E_07FB_6000_0000, 0x401E_1013_A000_0000,
0x401E_1820_A000_0000, 0x401E_2022_8000_0000, 0x401E_2819_4000_0000, 0x401E_3005_2000_0000,
0x401E_37E6_2000_0000, 0x401E_3FBC_8000_0000, 0x401E_4788_4000_0000, 0x401E_4F49_8000_0000,
0x401E_5700_6000_0000, 0x401E_5EAD_0000_0000, 0x401E_664F_8000_0000, 0x401E_6DE8_0000_0000,
0x401E_7576_8000_0000, 0x401E_7CFB_2000_0000, 0x401E_8476_0000_0000, 0x401E_8BE7_6000_0000,
0x401E_934F_0000_0000, 0x401E_9AAD_4000_0000, 0x401E_A202_4000_0000, 0x401E_A94D_E000_0000,
0x401E_B090_4000_0000, 0x401E_B7C9_A000_0000, 0x401E_BEFA_0000_0000, 0x401E_C621_8000_0000,
0x401E_CD40_2000_0000, 0x401E_D456_0000_0000, 0x401E_DB63_2000_0000, 0x401E_E267_E000_0000,
0x401E_E964_0000_0000, 0x401E_F057_C000_0000, 0x401E_F743_2000_0000, 0x401E_FE26_4000_0000,
0x401F_0501_4000_0000, 0x401F_0BD4_2000_0000, 0x401F_129E_E000_0000, 0x401F_1961_C000_0000,
0x401F_201C_C000_0000, 0x401F_26CF_E000_0000, 0x401F_2D7B_6000_0000, 0x401F_341F_2000_0000,
0x401F_3ABB_4000_0000, 0x401F_414F_E000_0000, 0x401F_47DD_0000_0000, 0x401F_4E62_C000_0000,
0x401F_54E1_2000_0000, 0x401F_5B58_4000_0000, 0x401F_61C8_2000_0000, 0x401F_6830_E000_0000,
0x401F_6E92_A000_0000, 0x401F_74ED_4000_0000, 0x401F_7B40_E000_0000, 0x401F_818D_A000_0000,
0x401F_87D3_A000_0000, 0x401F_8E12_C000_0000, 0x401F_944B_2000_0000, 0x401F_9A7C_E000_0000,
0x401F_A0A7_E000_0000, 0x401F_A6CC_8000_0000, 0x401F_ACEA_8000_0000, 0x401F_B302_0000_0000,
0x401F_B913_4000_0000, 0x401F_BF1E_0000_0000, 0x401F_C522_8000_0000, 0x401F_CB20_C000_0000,
0x401F_D118_E000_0000, 0x401F_D70A_C000_0000, 0x401F_DCF6_8000_0000, 0x401F_E2DC_6000_0000,
0x401F_E8BC_2000_0000, 0x401F_EE95_E000_0000, 0x401F_F469_C000_0000, 0x401F_FA37_C000_0000,
]
// From enc/encode.c kDefaultCommandCodeNumBits.
pri const DEFAULT_COMMAND_CODE_NUM_BITS : base.u32 = 448
// From enc/encode.c kDefaultCommandDepths.
pri const DEFAULT_COMMAND_DEPTHS : roarray[128] base.u8[..= 15] = [
0, 4, 4, 5, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
0, 0, 0, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7,
7, 7, 10, 10, 10, 10, 10, 10, 0, 4, 4, 5, 5, 5, 6, 6,
7, 8, 8, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4,
4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 10,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 0, 0, 0, 0,
]
// From enc/encode.c kDefaultCommandBits.
pri const DEFAULT_COMMAND_BITS : roarray[128] base.u16 = [
0, 0, 8, 9, 3, 35, 7, 71, 39, 103, 23, 47,
175, 111, 239, 31, 0, 0, 0, 4, 12, 2, 10, 6,
13, 29, 11, 43, 27, 59, 87, 55, 15, 79, 319, 831,
191, 703, 447, 959, 0, 14, 1, 25, 5, 21, 19, 51,
119, 159, 95, 223, 479, 991, 63, 575, 127, 639, 383, 895,
255, 767, 511, 1023, 14, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 27, 59, 7, 39,
23, 55, 30, 1, 17, 9, 25, 5, 0, 8, 4, 12,
2, 10, 6, 21, 13, 29, 3, 19, 11, 15, 47, 31,
95, 63, 127, 255, 767, 2815, 1791, 3839, 511, 2559, 1535, 3583,
1023, 3071, 2047, 4095, 0, 0, 0, 0,
]
// From enc/encode.c kDefaultCommandCode.
pri const DEFAULT_COMMAND_CODE : roarray[57] base.u8 = [
0xFF, 0x77, 0xD5, 0xBF, 0xE7, 0xDE, 0xEA, 0x9E, 0x51, 0x5D, 0xDE, 0xC6,
0x70, 0x57, 0xBC, 0x58, 0x58, 0x58, 0xD8, 0xD8, 0x58, 0xD5, 0xCB, 0x8C,
0xEA, 0xE0, 0xC3, 0x87, 0x1F, 0x83, 0xC1, 0x60, 0x1C, 0x67, 0xB2, 0xAA,
0x06, 0x83, 0xC1, 0x60, 0x30, 0x18, 0xCC, 0xA1, 0xCE, 0x88, 0x54, 0x94,
0x46, 0xE1, 0xB0, 0xD0, 0x4E, 0xB2, 0xF7, 0x04, 0x00,
]
// From enc/compress_fragment.c kCmdHistoSeed.
pri const CMD_HISTO_SEED : roarray[128] base.u32[..= 1] = [
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
]
// From enc/compress_fragment_two_pass.c kNumExtraBits.
pri const TWO_PASS_NUM_EXTRA_BITS : roarray[128] base.u32[..= 24] = [
0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
6, 7, 8, 9, 10, 12, 14, 24, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 2, 2, 3, 3, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 24,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24,
]
// From enc/compress_fragment_two_pass.c kInsertOffset.
pri const TWO_PASS_INSERT_OFFSET : roarray[24] base.u32[..= 22594] = [
0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26,
34, 50, 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594,
]
// From enc/entropy_encode.c kBrotliShellGaps.
pri const SHELL_GAPS : roarray[6] base.u32[..= 132] = [
132, 57, 23, 10, 4, 1,
]
// From enc/entropy_encode.c kLut (in BrotliReverseBits).
pri const REVERSE_BITS_LUT : roarray[16] base.u32[..= 15] = [
0x00, 0x08, 0x04, 0x0C, 0x02, 0x0A, 0x06, 0x0E, 0x01, 0x09, 0x05, 0x0D,
0x03, 0x0B, 0x07, 0x0F,
]
// From enc/entropy_encode_static.h StoreStaticCodeLengthCode: 40 bits.
pri const STATIC_CODE_LENGTH_CODE : base.u64 = 0xFF_5555_5554
// From enc/entropy_encode_static.h kCodeLengthDepth.
pri const CODE_LENGTH_DEPTH : roarray[18] base.u32[..= 56] = [
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 0, 4, 4,
]
// From enc/entropy_encode_static.h kCodeLengthBits.
pri const CODE_LENGTH_BITS : roarray[18] base.u64 = [
0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 15, 31, 0, 11, 7,
]
// From enc/entropy_encode_static.h kZeroRepsBits.
pri const ZERO_REPS_BITS : roarray[704] base.u64 = [
0x0000, 0x0000, 0x0000, 0x0007, 0x0017, 0x0027, 0x0037, 0x0047,
0x0057, 0x0067, 0x0077, 0x0770, 0x0B87, 0x1387, 0x1B87, 0x2387,
0x2B87, 0x3387, 0x3B87, 0x0397, 0x0B97, 0x1397, 0x1B97, 0x2397,
0x2B97, 0x3397, 0x3B97, 0x03A7, 0x0BA7, 0x13A7, 0x1BA7, 0x23A7,
0x2BA7, 0x33A7, 0x3BA7, 0x03B7, 0x0BB7, 0x13B7, 0x1BB7, 0x23B7,
0x2BB7, 0x33B7, 0x3BB7, 0x03C7, 0x0BC7, 0x13C7, 0x1BC7, 0x23C7,
0x2BC7, 0x33C7, 0x3BC7, 0x03D7, 0x0BD7, 0x13D7, 0x1BD7, 0x23D7,
0x2BD7, 0x33D7, 0x3BD7, 0x03E7, 0x0BE7, 0x13E7, 0x1BE7, 0x23E7,
0x2BE7, 0x33E7, 0x3BE7, 0x03F7, 0x0BF7, 0x13F7, 0x1BF7, 0x23F7,
0x2BF7, 0x33F7, 0x3BF7, 0x1_C387, 0x5_C387, 0x9_C387, 0xD_C387, 0x11_C387,
0x15_C387, 0x19_C387, 0x1D_C387, 0x1_CB87, 0x5_CB87, 0x9_CB87, 0xD_CB87, 0x11_CB87,
0x15_CB87, 0x19_CB87, 0x1D_CB87, 0x1_D387, 0x5_D387, 0x9_D387, 0xD_D387, 0x11_D387,
0x15_D387, 0x19_D387, 0x1D_D387, 0x1_DB87, 0x5_DB87, 0x9_DB87, 0xD_DB87, 0x11_DB87,
0x15_DB87, 0x19_DB87, 0x1D_DB87, 0x1_E387, 0x5_E387, 0x9_E387, 0xD_E387, 0x11_E387,
0x15_E387, 0x19_E387, 0x1D_E387, 0x1_EB87, 0x5_EB87, 0x9_EB87, 0xD_EB87, 0x11_EB87,
0x15_EB87, 0x19_EB87, 0x1D_EB87, 0x1_F387, 0x5_F387, 0x9_F387, 0xD_F387, 0x11_F387,
0x15_F387, 0x19_F387, 0x1D_F387, 0x1_FB87, 0x5_FB87, 0x9_FB87, 0xD_FB87, 0x11_FB87,
0x15_FB87, 0x19_FB87, 0x1D_FB87, 0x1_C397, 0x5_C397, 0x9_C397, 0xD_C397, 0x11_C397,
0x15_C397, 0x19_C397, 0x1D_C397, 0x1_CB97, 0x5_CB97, 0x9_CB97, 0xD_CB97, 0x11_CB97,
0x15_CB97, 0x19_CB97, 0x1D_CB97, 0x1_D397, 0x5_D397, 0x9_D397, 0xD_D397, 0x11_D397,
0x15_D397, 0x19_D397, 0x1D_D397, 0x1_DB97, 0x5_DB97, 0x9_DB97, 0xD_DB97, 0x11_DB97,
0x15_DB97, 0x19_DB97, 0x1D_DB97, 0x1_E397, 0x5_E397, 0x9_E397, 0xD_E397, 0x11_E397,
0x15_E397, 0x19_E397, 0x1D_E397, 0x1_EB97, 0x5_EB97, 0x9_EB97, 0xD_EB97, 0x11_EB97,
0x15_EB97, 0x19_EB97, 0x1D_EB97, 0x1_F397, 0x5_F397, 0x9_F397, 0xD_F397, 0x11_F397,
0x15_F397, 0x19_F397, 0x1D_F397, 0x1_FB97, 0x5_FB97, 0x9_FB97, 0xD_FB97, 0x11_FB97,
0x15_FB97, 0x19_FB97, 0x1D_FB97, 0x1_C3A7, 0x5_C3A7, 0x9_C3A7, 0xD_C3A7, 0x11_C3A7,
0x15_C3A7, 0x19_C3A7, 0x1D_C3A7, 0x1_CBA7, 0x5_CBA7, 0x9_CBA7, 0xD_CBA7, 0x11_CBA7,
0x15_CBA7, 0x19_CBA7, 0x1D_CBA7, 0x1_D3A7, 0x5_D3A7, 0x9_D3A7, 0xD_D3A7, 0x11_D3A7,
0x15_D3A7, 0x19_D3A7, 0x1D_D3A7, 0x1_DBA7, 0x5_DBA7, 0x9_DBA7, 0xD_DBA7, 0x11_DBA7,
0x15_DBA7, 0x19_DBA7, 0x1D_DBA7, 0x1_E3A7, 0x5_E3A7, 0x9_E3A7, 0xD_E3A7, 0x11_E3A7,
0x15_E3A7, 0x19_E3A7, 0x1D_E3A7, 0x1_EBA7, 0x5_EBA7, 0x9_EBA7, 0xD_EBA7, 0x11_EBA7,
0x15_EBA7, 0x19_EBA7, 0x1D_EBA7, 0x1_F3A7, 0x5_F3A7, 0x9_F3A7, 0xD_F3A7, 0x11_F3A7,
0x15_F3A7, 0x19_F3A7, 0x1D_F3A7, 0x1_FBA7, 0x5_FBA7, 0x9_FBA7, 0xD_FBA7, 0x11_FBA7,
0x15_FBA7, 0x19_FBA7, 0x1D_FBA7, 0x1_C3B7, 0x5_C3B7, 0x9_C3B7, 0xD_C3B7, 0x11_C3B7,
0x15_C3B7, 0x19_C3B7, 0x1D_C3B7, 0x1_CBB7, 0x5_CBB7, 0x9_CBB7, 0xD_CBB7, 0x11_CBB7,
0x15_CBB7, 0x19_CBB7, 0x1D_CBB7, 0x1_D3B7, 0x5_D3B7, 0x9_D3B7, 0xD_D3B7, 0x11_D3B7,
0x15_D3B7, 0x19_D3B7, 0x1D_D3B7, 0x1_DBB7, 0x5_DBB7, 0x9_DBB7, 0xD_DBB7, 0x11_DBB7,
0x15_DBB7, 0x19_DBB7, 0x1D_DBB7, 0x1_E3B7, 0x5_E3B7, 0x9_E3B7, 0xD_E3B7, 0x11_E3B7,
0x15_E3B7, 0x19_E3B7, 0x1D_E3B7, 0x1_EBB7, 0x5_EBB7, 0x9_EBB7, 0xD_EBB7, 0x11_EBB7,
0x15_EBB7, 0x19_EBB7, 0x1D_EBB7, 0x1_F3B7, 0x5_F3B7, 0x9_F3B7, 0xD_F3B7, 0x11_F3B7,
0x15_F3B7, 0x19_F3B7, 0x1D_F3B7, 0x1_FBB7, 0x5_FBB7, 0x9_FBB7, 0xD_FBB7, 0x11_FBB7,
0x15_FBB7, 0x19_FBB7, 0x1D_FBB7, 0x1_C3C7, 0x5_C3C7, 0x9_C3C7, 0xD_C3C7, 0x11_C3C7,
0x15_C3C7, 0x19_C3C7, 0x1D_C3C7, 0x1_CBC7, 0x5_CBC7, 0x9_CBC7, 0xD_CBC7, 0x11_CBC7,
0x15_CBC7, 0x19_CBC7, 0x1D_CBC7, 0x1_D3C7, 0x5_D3C7, 0x9_D3C7, 0xD_D3C7, 0x11_D3C7,
0x15_D3C7, 0x19_D3C7, 0x1D_D3C7, 0x1_DBC7, 0x5_DBC7, 0x9_DBC7, 0xD_DBC7, 0x11_DBC7,
0x15_DBC7, 0x19_DBC7, 0x1D_DBC7, 0x1_E3C7, 0x5_E3C7, 0x9_E3C7, 0xD_E3C7, 0x11_E3C7,
0x15_E3C7, 0x19_E3C7, 0x1D_E3C7, 0x1_EBC7, 0x5_EBC7, 0x9_EBC7, 0xD_EBC7, 0x11_EBC7,
0x15_EBC7, 0x19_EBC7, 0x1D_EBC7, 0x1_F3C7, 0x5_F3C7, 0x9_F3C7, 0xD_F3C7, 0x11_F3C7,
0x15_F3C7, 0x19_F3C7, 0x1D_F3C7, 0x1_FBC7, 0x5_FBC7, 0x9_FBC7, 0xD_FBC7, 0x11_FBC7,
0x15_FBC7, 0x19_FBC7, 0x1D_FBC7, 0x1_C3D7, 0x5_C3D7, 0x9_C3D7, 0xD_C3D7, 0x11_C3D7,
0x15_C3D7, 0x19_C3D7, 0x1D_C3D7, 0x1_CBD7, 0x5_CBD7, 0x9_CBD7, 0xD_CBD7, 0x11_CBD7,
0x15_CBD7, 0x19_CBD7, 0x1D_CBD7, 0x1_D3D7, 0x5_D3D7, 0x9_D3D7, 0xD_D3D7, 0x11_D3D7,
0x15_D3D7, 0x19_D3D7, 0x1D_D3D7, 0x1_DBD7, 0x5_DBD7, 0x9_DBD7, 0xD_DBD7, 0x11_DBD7,
0x15_DBD7, 0x19_DBD7, 0x1D_DBD7, 0x1_E3D7, 0x5_E3D7, 0x9_E3D7, 0xD_E3D7, 0x11_E3D7,
0x15_E3D7, 0x19_E3D7, 0x1D_E3D7, 0x1_EBD7, 0x5_EBD7, 0x9_EBD7, 0xD_EBD7, 0x11_EBD7,
0x15_EBD7, 0x19_EBD7, 0x1D_EBD7, 0x1_F3D7, 0x5_F3D7, 0x9_F3D7, 0xD_F3D7, 0x11_F3D7,
0x15_F3D7, 0x19_F3D7, 0x1D_F3D7, 0x1_FBD7, 0x5_FBD7, 0x9_FBD7, 0xD_FBD7, 0x11_FBD7,
0x15_FBD7, 0x19_FBD7, 0x1D_FBD7, 0x1_C3E7, 0x5_C3E7, 0x9_C3E7, 0xD_C3E7, 0x11_C3E7,
0x15_C3E7, 0x19_C3E7, 0x1D_C3E7, 0x1_CBE7, 0x5_CBE7, 0x9_CBE7, 0xD_CBE7, 0x11_CBE7,
0x15_CBE7, 0x19_CBE7, 0x1D_CBE7, 0x1_D3E7, 0x5_D3E7, 0x9_D3E7, 0xD_D3E7, 0x11_D3E7,
0x15_D3E7, 0x19_D3E7, 0x1D_D3E7, 0x1_DBE7, 0x5_DBE7, 0x9_DBE7, 0xD_DBE7, 0x11_DBE7,
0x15_DBE7, 0x19_DBE7, 0x1D_DBE7, 0x1_E3E7, 0x5_E3E7, 0x9_E3E7, 0xD_E3E7, 0x11_E3E7,
0x15_E3E7, 0x19_E3E7, 0x1D_E3E7, 0x1_EBE7, 0x5_EBE7, 0x9_EBE7, 0xD_EBE7, 0x11_EBE7,
0x15_EBE7, 0x19_EBE7, 0x1D_EBE7, 0x1_F3E7, 0x5_F3E7, 0x9_F3E7, 0xD_F3E7, 0x11_F3E7,
0x15_F3E7, 0x19_F3E7, 0x1D_F3E7, 0x1_FBE7, 0x5_FBE7, 0x9_FBE7, 0xD_FBE7, 0x11_FBE7,
0x15_FBE7, 0x19_FBE7, 0x1D_FBE7, 0x1_C3F7, 0x5_C3F7, 0x9_C3F7, 0xD_C3F7, 0x11_C3F7,
0x15_C3F7, 0x19_C3F7, 0x1D_C3F7, 0x1_CBF7, 0x5_CBF7, 0x9_CBF7, 0xD_CBF7, 0x11_CBF7,
0x15_CBF7, 0x19_CBF7, 0x1D_CBF7, 0x1_D3F7, 0x5_D3F7, 0x9_D3F7, 0xD_D3F7, 0x11_D3F7,
0x15_D3F7, 0x19_D3F7, 0x1D_D3F7, 0x1_DBF7, 0x5_DBF7, 0x9_DBF7, 0xD_DBF7, 0x11_DBF7,
0x15_DBF7, 0x19_DBF7, 0x1D_DBF7, 0x1_E3F7, 0x5_E3F7, 0x9_E3F7, 0xD_E3F7, 0x11_E3F7,
0x15_E3F7, 0x19_E3F7, 0x1D_E3F7, 0x1_EBF7, 0x5_EBF7, 0x9_EBF7, 0xD_EBF7, 0x11_EBF7,
0x15_EBF7, 0x19_EBF7, 0x1D_EBF7, 0x1_F3F7, 0x5_F3F7, 0x9_F3F7, 0xD_F3F7, 0x11_F3F7,
0x15_F3F7, 0x19_F3F7, 0x1D_F3F7, 0x1_FBF7, 0x5_FBF7, 0x9_FBF7, 0xD_FBF7, 0x11_FBF7,
0x15_FBF7, 0x19_FBF7, 0x1D_FBF7, 0xE1_C387, 0x2E1_C387, 0x4E1_C387, 0x6E1_C387, 0x8E1_C387,
0xAE1_C387, 0xCE1_C387, 0xEE1_C387, 0xE5_C387, 0x2E5_C387, 0x4E5_C387, 0x6E5_C387, 0x8E5_C387,
0xAE5_C387, 0xCE5_C387, 0xEE5_C387, 0xE9_C387, 0x2E9_C387, 0x4E9_C387, 0x6E9_C387, 0x8E9_C387,
0xAE9_C387, 0xCE9_C387, 0xEE9_C387, 0xED_C387, 0x2ED_C387, 0x4ED_C387, 0x6ED_C387, 0x8ED_C387,
0xAED_C387, 0xCED_C387, 0xEED_C387, 0xF1_C387, 0x2F1_C387, 0x4F1_C387, 0x6F1_C387, 0x8F1_C387,
0xAF1_C387, 0xCF1_C387, 0xEF1_C387, 0xF5_C387, 0x2F5_C387, 0x4F5_C387, 0x6F5_C387, 0x8F5_C387,
0xAF5_C387, 0xCF5_C387, 0xEF5_C387, 0xF9_C387, 0x2F9_C387, 0x4F9_C387, 0x6F9_C387, 0x8F9_C387,
0xAF9_C387, 0xCF9_C387, 0xEF9_C387, 0xFD_C387, 0x2FD_C387, 0x4FD_C387, 0x6FD_C387, 0x8FD_C387,
0xAFD_C387, 0xCFD_C387, 0xEFD_C387, 0xE1_CB87, 0x2E1_CB87, 0x4E1_CB87, 0x6E1_CB87, 0x8E1_CB87,
0xAE1_CB87, 0xCE1_CB87, 0xEE1_CB87, 0xE5_CB87, 0x2E5_CB87, 0x4E5_CB87, 0x6E5_CB87, 0x8E5_CB87,
0xAE5_CB87, 0xCE5_CB87, 0xEE5_CB87, 0xE9_CB87, 0x2E9_CB87, 0x4E9_CB87, 0x6E9_CB87, 0x8E9_CB87,
0xAE9_CB87, 0xCE9_CB87, 0xEE9_CB87, 0xED_CB87, 0x2ED_CB87, 0x4ED_CB87, 0x6ED_CB87, 0x8ED_CB87,
0xAED_CB87, 0xCED_CB87, 0xEED_CB87, 0xF1_CB87, 0x2F1_CB87, 0x4F1_CB87, 0x6F1_CB87, 0x8F1_CB87,
0xAF1_CB87, 0xCF1_CB87, 0xEF1_CB87, 0xF5_CB87, 0x2F5_CB87, 0x4F5_CB87, 0x6F5_CB87, 0x8F5_CB87,
0xAF5_CB87, 0xCF5_CB87, 0xEF5_CB87, 0xF9_CB87, 0x2F9_CB87, 0x4F9_CB87, 0x6F9_CB87, 0x8F9_CB87,
]
// From enc/entropy_encode_static.h kZeroRepsDepth.
pri const ZERO_REPS_DEPTH : roarray[704] base.u32[..= 56] = [
0, 4, 8, 7, 7, 7, 7, 7, 7, 7, 7, 11, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
]
// From enc/entropy_encode_static.h kNonZeroRepsBits.
pri const NON_ZERO_REPS_BITS : roarray[704] base.u64 = [
0x000B, 0x001B, 0x002B, 0x003B,
0x02CB, 0x06CB, 0x0ACB, 0x0ECB,
0x02DB, 0x06DB, 0x0ADB, 0x0EDB,
0x02EB, 0x06EB, 0x0AEB, 0x0EEB,
0x02FB, 0x06FB, 0x0AFB, 0x0EFB,
0xB2CB, 0x1_B2CB, 0x2_B2CB, 0x3_B2CB,
0xB6CB, 0x1_B6CB, 0x2_B6CB, 0x3_B6CB,
0xBACB, 0x1_BACB, 0x2_BACB, 0x3_BACB,
0xBECB, 0x1_BECB, 0x2_BECB, 0x3_BECB,
0xB2DB, 0x1_B2DB, 0x2_B2DB, 0x3_B2DB,
0xB6DB, 0x1_B6DB, 0x2_B6DB, 0x3_B6DB,
0xBADB, 0x1_BADB, 0x2_BADB, 0x3_BADB,
0xBEDB, 0x1_BEDB, 0x2_BEDB, 0x3_BEDB,
0xB2EB, 0x1_B2EB, 0x2_B2EB, 0x3_B2EB,
0xB6EB, 0x1_B6EB, 0x2_B6EB, 0x3_B6EB,
0xBAEB, 0x1_BAEB, 0x2_BAEB, 0x3_BAEB,
0xBEEB, 0x1_BEEB, 0x2_BEEB, 0x3_BEEB,
0xB2FB, 0x1_B2FB, 0x2_B2FB, 0x3_B2FB,
0xB6FB, 0x1_B6FB, 0x2_B6FB, 0x3_B6FB,
0xBAFB, 0x1_BAFB, 0x2_BAFB, 0x3_BAFB,
0xBEFB, 0x1_BEFB, 0x2_BEFB, 0x3_BEFB,
0x2C_B2CB, 0x6C_B2CB, 0xAC_B2CB, 0xEC_B2CB,
0x2D_B2CB, 0x6D_B2CB, 0xAD_B2CB, 0xED_B2CB,
0x2E_B2CB, 0x6E_B2CB, 0xAE_B2CB, 0xEE_B2CB,
0x2F_B2CB, 0x6F_B2CB, 0xAF_B2CB, 0xEF_B2CB,
0x2C_B6CB, 0x6C_B6CB, 0xAC_B6CB, 0xEC_B6CB,
0x2D_B6CB, 0x6D_B6CB, 0xAD_B6CB, 0xED_B6CB,
0x2E_B6CB, 0x6E_B6CB, 0xAE_B6CB, 0xEE_B6CB,
0x2F_B6CB, 0x6F_B6CB, 0xAF_B6CB, 0xEF_B6CB,
0x2C_BACB, 0x6C_BACB, 0xAC_BACB, 0xEC_BACB,
0x2D_BACB, 0x6D_BACB, 0xAD_BACB, 0xED_BACB,
0x2E_BACB, 0x6E_BACB, 0xAE_BACB, 0xEE_BACB,
0x2F_BACB, 0x6F_BACB, 0xAF_BACB, 0xEF_BACB,
0x2C_BECB, 0x6C_BECB, 0xAC_BECB, 0xEC_BECB,
0x2D_BECB, 0x6D_BECB, 0xAD_BECB, 0xED_BECB,
0x2E_BECB, 0x6E_BECB, 0xAE_BECB, 0xEE_BECB,
0x2F_BECB, 0x6F_BECB, 0xAF_BECB, 0xEF_BECB,
0x2C_B2DB, 0x6C_B2DB, 0xAC_B2DB, 0xEC_B2DB,
0x2D_B2DB, 0x6D_B2DB, 0xAD_B2DB, 0xED_B2DB,
0x2E_B2DB, 0x6E_B2DB, 0xAE_B2DB, 0xEE_B2DB,
0x2F_B2DB, 0x6F_B2DB, 0xAF_B2DB, 0xEF_B2DB,
0x2C_B6DB, 0x6C_B6DB, 0xAC_B6DB, 0xEC_B6DB,
0x2D_B6DB, 0x6D_B6DB, 0xAD_B6DB, 0xED_B6DB,
0x2E_B6DB, 0x6E_B6DB, 0xAE_B6DB, 0xEE_B6DB,
0x2F_B6DB, 0x6F_B6DB, 0xAF_B6DB, 0xEF_B6DB,
0x2C_BADB, 0x6C_BADB, 0xAC_BADB, 0xEC_BADB,
0x2D_BADB, 0x6D_BADB, 0xAD_BADB, 0xED_BADB,
0x2E_BADB, 0x6E_BADB, 0xAE_BADB, 0xEE_BADB,
0x2F_BADB, 0x6F_BADB, 0xAF_BADB, 0xEF_BADB,
0x2C_BEDB, 0x6C_BEDB, 0xAC_BEDB, 0xEC_BEDB,
0x2D_BEDB, 0x6D_BEDB, 0xAD_BEDB, 0xED_BEDB,
0x2E_BEDB, 0x6E_BEDB, 0xAE_BEDB, 0xEE_BEDB,
0x2F_BEDB, 0x6F_BEDB, 0xAF_BEDB, 0xEF_BEDB,
0x2C_B2EB, 0x6C_B2EB, 0xAC_B2EB, 0xEC_B2EB,
0x2D_B2EB, 0x6D_B2EB, 0xAD_B2EB, 0xED_B2EB,
0x2E_B2EB, 0x6E_B2EB, 0xAE_B2EB, 0xEE_B2EB,
0x2F_B2EB, 0x6F_B2EB, 0xAF_B2EB, 0xEF_B2EB,
0x2C_B6EB, 0x6C_B6EB, 0xAC_B6EB, 0xEC_B6EB,
0x2D_B6EB, 0x6D_B6EB, 0xAD_B6EB, 0xED_B6EB,
0x2E_B6EB, 0x6E_B6EB, 0xAE_B6EB, 0xEE_B6EB,
0x2F_B6EB, 0x6F_B6EB, 0xAF_B6EB, 0xEF_B6EB,
0x2C_BAEB, 0x6C_BAEB, 0xAC_BAEB, 0xEC_BAEB,
0x2D_BAEB, 0x6D_BAEB, 0xAD_BAEB, 0xED_BAEB,
0x2E_BAEB, 0x6E_BAEB, 0xAE_BAEB, 0xEE_BAEB,
0x2F_BAEB, 0x6F_BAEB, 0xAF_BAEB, 0xEF_BAEB,
0x2C_BEEB, 0x6C_BEEB, 0xAC_BEEB, 0xEC_BEEB,
0x2D_BEEB, 0x6D_BEEB, 0xAD_BEEB, 0xED_BEEB,
0x2E_BEEB, 0x6E_BEEB, 0xAE_BEEB, 0xEE_BEEB,
0x2F_BEEB, 0x6F_BEEB, 0xAF_BEEB, 0xEF_BEEB,
0x2C_B2FB, 0x6C_B2FB, 0xAC_B2FB, 0xEC_B2FB,
0x2D_B2FB, 0x6D_B2FB, 0xAD_B2FB, 0xED_B2FB,
0x2E_B2FB, 0x6E_B2FB, 0xAE_B2FB, 0xEE_B2FB,
0x2F_B2FB, 0x6F_B2FB, 0xAF_B2FB, 0xEF_B2FB,
0x2C_B6FB, 0x6C_B6FB, 0xAC_B6FB, 0xEC_B6FB,
0x2D_B6FB, 0x6D_B6FB, 0xAD_B6FB, 0xED_B6FB,
0x2E_B6FB, 0x6E_B6FB, 0xAE_B6FB, 0xEE_B6FB,
0x2F_B6FB, 0x6F_B6FB, 0xAF_B6FB, 0xEF_B6FB,
0x2C_BAFB, 0x6C_BAFB, 0xAC_BAFB, 0xEC_BAFB,
0x2D_BAFB, 0x6D_BAFB, 0xAD_BAFB, 0xED_BAFB,
0x2E_BAFB, 0x6E_BAFB, 0xAE_BAFB, 0xEE_BAFB,
0x2F_BAFB, 0x6F_BAFB, 0xAF_BAFB, 0xEF_BAFB,
0x2C_BEFB, 0x6C_BEFB, 0xAC_BEFB, 0xEC_BEFB,
0x2D_BEFB, 0x6D_BEFB, 0xAD_BEFB, 0xED_BEFB,
0x2E_BEFB, 0x6E_BEFB, 0xAE_BEFB, 0xEE_BEFB,
0x2F_BEFB, 0x6F_BEFB, 0xAF_BEFB, 0xEF_BEFB,
0xB2C_B2CB, 0x1B2C_B2CB, 0x2B2C_B2CB, 0x3B2C_B2CB,
0xB6C_B2CB, 0x1B6C_B2CB, 0x2B6C_B2CB, 0x3B6C_B2CB,
0xBAC_B2CB, 0x1BAC_B2CB, 0x2BAC_B2CB, 0x3BAC_B2CB,
0xBEC_B2CB, 0x1BEC_B2CB, 0x2BEC_B2CB, 0x3BEC_B2CB,
0xB2D_B2CB, 0x1B2D_B2CB, 0x2B2D_B2CB, 0x3B2D_B2CB,
0xB6D_B2CB, 0x1B6D_B2CB, 0x2B6D_B2CB, 0x3B6D_B2CB,
0xBAD_B2CB, 0x1BAD_B2CB, 0x2BAD_B2CB, 0x3BAD_B2CB,
0xBED_B2CB, 0x1BED_B2CB, 0x2BED_B2CB, 0x3BED_B2CB,
0xB2E_B2CB, 0x1B2E_B2CB, 0x2B2E_B2CB, 0x3B2E_B2CB,
0xB6E_B2CB, 0x1B6E_B2CB, 0x2B6E_B2CB, 0x3B6E_B2CB,
0xBAE_B2CB, 0x1BAE_B2CB, 0x2BAE_B2CB, 0x3BAE_B2CB,
0xBEE_B2CB, 0x1BEE_B2CB, 0x2BEE_B2CB, 0x3BEE_B2CB,
0xB2F_B2CB, 0x1B2F_B2CB, 0x2B2F_B2CB, 0x3B2F_B2CB,
0xB6F_B2CB, 0x1B6F_B2CB, 0x2B6F_B2CB, 0x3B6F_B2CB,
0xBAF_B2CB, 0x1BAF_B2CB, 0x2BAF_B2CB, 0x3BAF_B2CB,
0xBEF_B2CB, 0x1BEF_B2CB, 0x2BEF_B2CB, 0x3BEF_B2CB,
0xB2C_B6CB, 0x1B2C_B6CB, 0x2B2C_B6CB, 0x3B2C_B6CB,
0xB6C_B6CB, 0x1B6C_B6CB, 0x2B6C_B6CB, 0x3B6C_B6CB,
0xBAC_B6CB, 0x1BAC_B6CB, 0x2BAC_B6CB, 0x3BAC_B6CB,
0xBEC_B6CB, 0x1BEC_B6CB, 0x2BEC_B6CB, 0x3BEC_B6CB,
0xB2D_B6CB, 0x1B2D_B6CB, 0x2B2D_B6CB, 0x3B2D_B6CB,
0xB6D_B6CB, 0x1B6D_B6CB, 0x2B6D_B6CB, 0x3B6D_B6CB,
0xBAD_B6CB, 0x1BAD_B6CB, 0x2BAD_B6CB, 0x3BAD_B6CB,
0xBED_B6CB, 0x1BED_B6CB, 0x2BED_B6CB, 0x3BED_B6CB,
0xB2E_B6CB, 0x1B2E_B6CB, 0x2B2E_B6CB, 0x3B2E_B6CB,
0xB6E_B6CB, 0x1B6E_B6CB, 0x2B6E_B6CB, 0x3B6E_B6CB,
0xBAE_B6CB, 0x1BAE_B6CB, 0x2BAE_B6CB, 0x3BAE_B6CB,
0xBEE_B6CB, 0x1BEE_B6CB, 0x2BEE_B6CB, 0x3BEE_B6CB,
0xB2F_B6CB, 0x1B2F_B6CB, 0x2B2F_B6CB, 0x3B2F_B6CB,
0xB6F_B6CB, 0x1B6F_B6CB, 0x2B6F_B6CB, 0x3B6F_B6CB,
0xBAF_B6CB, 0x1BAF_B6CB, 0x2BAF_B6CB, 0x3BAF_B6CB,
0xBEF_B6CB, 0x1BEF_B6CB, 0x2BEF_B6CB, 0x3BEF_B6CB,
0xB2C_BACB, 0x1B2C_BACB, 0x2B2C_BACB, 0x3B2C_BACB,
0xB6C_BACB, 0x1B6C_BACB, 0x2B6C_BACB, 0x3B6C_BACB,
0xBAC_BACB, 0x1BAC_BACB, 0x2BAC_BACB, 0x3BAC_BACB,
0xBEC_BACB, 0x1BEC_BACB, 0x2BEC_BACB, 0x3BEC_BACB,
0xB2D_BACB, 0x1B2D_BACB, 0x2B2D_BACB, 0x3B2D_BACB,
0xB6D_BACB, 0x1B6D_BACB, 0x2B6D_BACB, 0x3B6D_BACB,
0xBAD_BACB, 0x1BAD_BACB, 0x2BAD_BACB, 0x3BAD_BACB,
0xBED_BACB, 0x1BED_BACB, 0x2BED_BACB, 0x3BED_BACB,
0xB2E_BACB, 0x1B2E_BACB, 0x2B2E_BACB, 0x3B2E_BACB,
0xB6E_BACB, 0x1B6E_BACB, 0x2B6E_BACB, 0x3B6E_BACB,
0xBAE_BACB, 0x1BAE_BACB, 0x2BAE_BACB, 0x3BAE_BACB,
0xBEE_BACB, 0x1BEE_BACB, 0x2BEE_BACB, 0x3BEE_BACB,
0xB2F_BACB, 0x1B2F_BACB, 0x2B2F_BACB, 0x3B2F_BACB,
0xB6F_BACB, 0x1B6F_BACB, 0x2B6F_BACB, 0x3B6F_BACB,
0xBAF_BACB, 0x1BAF_BACB, 0x2BAF_BACB, 0x3BAF_BACB,
0xBEF_BACB, 0x1BEF_BACB, 0x2BEF_BACB, 0x3BEF_BACB,
0xB2C_BECB, 0x1B2C_BECB, 0x2B2C_BECB, 0x3B2C_BECB,
0xB6C_BECB, 0x1B6C_BECB, 0x2B6C_BECB, 0x3B6C_BECB,
0xBAC_BECB, 0x1BAC_BECB, 0x2BAC_BECB, 0x3BAC_BECB,
0xBEC_BECB, 0x1BEC_BECB, 0x2BEC_BECB, 0x3BEC_BECB,
0xB2D_BECB, 0x1B2D_BECB, 0x2B2D_BECB, 0x3B2D_BECB,
0xB6D_BECB, 0x1B6D_BECB, 0x2B6D_BECB, 0x3B6D_BECB,
0xBAD_BECB, 0x1BAD_BECB, 0x2BAD_BECB, 0x3BAD_BECB,
0xBED_BECB, 0x1BED_BECB, 0x2BED_BECB, 0x3BED_BECB,
0xB2E_BECB, 0x1B2E_BECB, 0x2B2E_BECB, 0x3B2E_BECB,
0xB6E_BECB, 0x1B6E_BECB, 0x2B6E_BECB, 0x3B6E_BECB,
0xBAE_BECB, 0x1BAE_BECB, 0x2BAE_BECB, 0x3BAE_BECB,
0xBEE_BECB, 0x1BEE_BECB, 0x2BEE_BECB, 0x3BEE_BECB,
0xB2F_BECB, 0x1B2F_BECB, 0x2B2F_BECB, 0x3B2F_BECB,
0xB6F_BECB, 0x1B6F_BECB, 0x2B6F_BECB, 0x3B6F_BECB,
0xBAF_BECB, 0x1BAF_BECB, 0x2BAF_BECB, 0x3BAF_BECB,
0xBEF_BECB, 0x1BEF_BECB, 0x2BEF_BECB, 0x3BEF_BECB,
0xB2C_B2DB, 0x1B2C_B2DB, 0x2B2C_B2DB, 0x3B2C_B2DB,
0xB6C_B2DB, 0x1B6C_B2DB, 0x2B6C_B2DB, 0x3B6C_B2DB,
0xBAC_B2DB, 0x1BAC_B2DB, 0x2BAC_B2DB, 0x3BAC_B2DB,
0xBEC_B2DB, 0x1BEC_B2DB, 0x2BEC_B2DB, 0x3BEC_B2DB,
0xB2D_B2DB, 0x1B2D_B2DB, 0x2B2D_B2DB, 0x3B2D_B2DB,
0xB6D_B2DB, 0x1B6D_B2DB, 0x2B6D_B2DB, 0x3B6D_B2DB,
0xBAD_B2DB, 0x1BAD_B2DB, 0x2BAD_B2DB, 0x3BAD_B2DB,
0xBED_B2DB, 0x1BED_B2DB, 0x2BED_B2DB, 0x3BED_B2DB,
0xB2E_B2DB, 0x1B2E_B2DB, 0x2B2E_B2DB, 0x3B2E_B2DB,
0xB6E_B2DB, 0x1B6E_B2DB, 0x2B6E_B2DB, 0x3B6E_B2DB,
0xBAE_B2DB, 0x1BAE_B2DB, 0x2BAE_B2DB, 0x3BAE_B2DB,
0xBEE_B2DB, 0x1BEE_B2DB, 0x2BEE_B2DB, 0x3BEE_B2DB,
0xB2F_B2DB, 0x1B2F_B2DB, 0x2B2F_B2DB, 0x3B2F_B2DB,
0xB6F_B2DB, 0x1B6F_B2DB, 0x2B6F_B2DB, 0x3B6F_B2DB,
0xBAF_B2DB, 0x1BAF_B2DB, 0x2BAF_B2DB, 0x3BAF_B2DB,
0xBEF_B2DB, 0x1BEF_B2DB, 0x2BEF_B2DB, 0x3BEF_B2DB,
0xB2C_B6DB, 0x1B2C_B6DB, 0x2B2C_B6DB, 0x3B2C_B6DB,
0xB6C_B6DB, 0x1B6C_B6DB, 0x2B6C_B6DB, 0x3B6C_B6DB,
0xBAC_B6DB, 0x1BAC_B6DB, 0x2BAC_B6DB, 0x3BAC_B6DB,
0xBEC_B6DB, 0x1BEC_B6DB, 0x2BEC_B6DB, 0x3BEC_B6DB,
0xB2D_B6DB, 0x1B2D_B6DB, 0x2B2D_B6DB, 0x3B2D_B6DB,
0xB6D_B6DB, 0x1B6D_B6DB, 0x2B6D_B6DB, 0x3B6D_B6DB,
0xBAD_B6DB, 0x1BAD_B6DB, 0x2BAD_B6DB, 0x3BAD_B6DB,
0xBED_B6DB, 0x1BED_B6DB, 0x2BED_B6DB, 0x3BED_B6DB,
0xB2E_B6DB, 0x1B2E_B6DB, 0x2B2E_B6DB, 0x3B2E_B6DB,
0xB6E_B6DB, 0x1B6E_B6DB, 0x2B6E_B6DB, 0x3B6E_B6DB,
0xBAE_B6DB, 0x1BAE_B6DB, 0x2BAE_B6DB, 0x3BAE_B6DB,
]
// From enc/entropy_encode_static.h kNonZeroRepsDepth.
pri const NON_ZERO_REPS_DEPTH : roarray[704] base.u32[..= 56] = [
6, 6, 6, 6, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
]
// From enc/brotli_bit_stream.c kStorageOrder.
pri const STORAGE_ORDER : roarray[18] base.u8[..= 17] = [
1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
]
// From enc/brotli_bit_stream.c kHuffmanBitLengthHuffmanCodeSymbols.
pri const HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_SYMBOLS : roarray[6] base.u64[..= 15] = [
0, 7, 3, 2, 1, 15,
]
// From enc/brotli_bit_stream.c kHuffmanBitLengthHuffmanCodeBitLengths.
pri const HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_BIT_LENGTHS : roarray[6] base.u32[..= 4] = [
2, 4, 3, 2, 2, 4,
]
+32
View File
@@ -0,0 +1,32 @@
// 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
// --------
// Quirks are discussed in (/doc/note/quirks.md).
//
// The base38 encoding of "brot" is 0x0A_ADE4. Left shifting by 10 gives
// 0x2AB7_9000.
pri const QUIRKS_BASE : base.u32 = 0x2AB7_9000
// --------
// When this quirk is set, a positive value enables large-window brotli
// (https://datatracker.ietf.org/doc/draft-vandevenne-shared-brotli-format/),
// with window sizes up to (1 << 30) bytes. Zero means standard (RFC 7932)
// brotli. It mirrors the C code's BROTLI_DECODER_PARAM_LARGE_WINDOW.
//
// It must be set before decoding starts. Setting it also increases the
// workbuf_len.
//
// Without this quirk, a stream that starts with the large-window WBITS escape
// 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
+259
View File
@@ -0,0 +1,259 @@
// 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 the encoder's bit writer and other small helpers shared by
// the quality 0 and quality 1 code paths. The bit position (the C code's
// *storage_ix) is the encoder.bw_ix field. The storage (the C code's uint8_t*
// storage) is a slice argument.
// Mirrors BrotliWriteBits in write_bits.h (its BROTLI_LITTLE_ENDIAN branch).
//
// Like the C code, this ORs the bits into the byte at (bw_ix >> 3) and then
// stores 8 bytes, little-endian, zeroing any bits above the newly written
// ones. The byte-exact output depends on that zeroing, as later code reads
// back (and ORs into) those bytes. The storage slice always has enough slack
// that the 8-byte store fits. If it ever did not, the bits are written byte
// by byte, as far as they fit.
pri func encoder.write_bits!(n_bits: base.u32, bits: base.u64, s: slice base.u8) {
var p : base.u64
var v : base.u64
var q : slice base.u8
p = this.bw_ix >> 3
if p < args.s.length() {
q = args.s[p ..]
v = (args.s[p] as base.u64) | (args.bits ~mod<< (this.bw_ix & 7))
if q.length() >= 8 {
q.poke_u64le!(a: v)
} else {
while q.length() > 0 {
q[0] = (v & 0xFF) as base.u8
v >>= 8
q = q[1 ..]
}
}
}
this.bw_ix ~mod+= args.n_bits as base.u64
}
// Mirrors JumpToByteBoundary in brotli_bit_stream.c, without the zeroing
// (which the C code's callers in this file rely on the 8-byte stores for).
// The "(*storage_ix + 7u) & ~7u" expressions in compress_fragment.c and
// compress_fragment_two_pass.c.
pri func encoder.align_to_byte!() {
this.bw_ix = (this.bw_ix ~mod+ 7) & 0xFFFF_FFFF_FFFF_FFF8
}
// Mirrors RewindBitPosition in compress_fragment.c and
// compress_fragment_two_pass.c.
pri func encoder.rewind_bit_position!(new_ix: base.u64, s: slice base.u8) {
var p : base.u64
var mask : base.u8
p = args.new_ix >> 3
mask = (((1 as base.u32) << (args.new_ix & 7)) - 1) as base.u8
if p < args.s.length() {
args.s[p] &= mask
}
this.bw_ix = args.new_ix
}
// Mirrors UpdateBits in compress_fragment.c.
pri func encoder.update_bits!(n_bits: base.u32, bits: base.u32, pos: base.u64, s: slice base.u8) {
var n_bits : base.u32
var bits : base.u32
var pos : base.u64
var byte_pos : base.u64
var n_unchanged_bits : base.u32[..= 7]
var n_changed_bits : base.u32[..= 8]
var total_bits : base.u32[..= 15]
var mask : base.u32
var unchanged_bits : base.u32
var changed_bits : base.u32
n_bits = args.n_bits
bits = args.bits
pos = args.pos
while n_bits > 0 {
byte_pos = pos >> 3
n_unchanged_bits = (pos & 7) as base.u32
n_changed_bits = n_bits.min(no_more_than: 8 - n_unchanged_bits)
total_bits = n_unchanged_bits + n_changed_bits
mask = (0xFFFF_FFFF ^ (((1 as base.u32) << total_bits) - 1)) |
(((1 as base.u32) << n_unchanged_bits) - 1)
if byte_pos >= args.s.length() {
return nothing
}
unchanged_bits = (args.s[byte_pos] as base.u32) & mask
changed_bits = bits & (((1 as base.u32) << n_changed_bits) - 1)
args.s[byte_pos] = (((changed_bits << n_unchanged_bits) | unchanged_bits) & 0xFF) as base.u8
n_bits ~mod-= n_changed_bits
bits = bits >> n_changed_bits
pos ~mod+= n_changed_bits as base.u64
}
}
// Mirrors BrotliStoreMetaBlockHeader in compress_fragment.c (and the identical
// function in compress_fragment_two_pass.c).
pri func encoder.store_meta_block_header!(len: base.u64, is_uncompressed: base.bool, s: slice base.u8) {
var nibbles_minus_4 : base.u32[..= 2]
nibbles_minus_4 = 2
// ISLAST.
this.write_bits!(n_bits: 1, bits: 0, s: args.s)
if args.len <= 0x1_0000 {
nibbles_minus_4 = 0
} else if args.len <= 0x10_0000 {
nibbles_minus_4 = 1
}
this.write_bits!(n_bits: 2, bits: nibbles_minus_4 as base.u64, s: args.s)
this.write_bits!(n_bits: (nibbles_minus_4 + 4) * 4, bits: args.len ~mod- 1, s: args.s)
// ISUNCOMPRESSED.
if args.is_uncompressed {
this.write_bits!(n_bits: 1, bits: 1, s: args.s)
} else {
this.write_bits!(n_bits: 1, bits: 0, s: args.s)
}
}
// 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
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
}
// Mirrors FastLog2 in fast_log.h. The result is the bit pattern of an IEEE
// 754 binary64 value.
pri func encoder.fast_log2(v: base.u64) base.u64 {
if args.v < 256 {
return LOG2_TABLE[args.v]
}
return this.sf.log2_u64(v: args.v)
}
// Mirrors BrotliBitsEntropy in bit_cost.c, over this.histogram (with size
// 256). The result is the bit pattern of an IEEE 754 binary64 value.
pri func encoder.bits_entropy_of_histogram() base.u64 {
var sum : base.u64
var retval : base.u64
var i : base.u32
var p : base.u64
i = 0
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)))
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)))
}
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
retval = this.sf.f64_from_u64(a: sum)
}
return retval
}
// load64le returns the 8 bytes (little-endian) of s starting at i, like the C
// 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()
}
}
return 0
}
// 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()
}
}
return 0
}
// Mirrors FindMatchLengthWithLimit in find_match_length.h: the length of the
// common prefix of s[i1 ..] and s[i2 ..], up to limit bytes.
pri func encoder.find_match_length_with_limit(s: roslice base.u8, i1: base.u64, i2: base.u64, limit: base.u64) base.u64 {
var matched : base.u64
var n : base.u64
var a : roslice base.u8
var b : roslice base.u8
var x : base.u64
if (args.i1 > args.s.length()) or (args.i2 > args.s.length()) {
return 0
}
a = args.s[args.i1 ..]
b = args.s[args.i2 ..]
n = args.limit
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
}
matched ~mod+= 8
n -= 8
a = a[8 ..]
b = b[8 ..]
}
while (n > 0) and (a.length() > 0) and (b.length() > 0) {
if a[0] <> b[0] {
break
}
matched ~mod+= 1
n -= 1
a = a[1 ..]
b = b[1 ..]
}
return matched
}
+833
View File
@@ -0,0 +1,833 @@
// 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
// --------
// The encoder aims for parity with the reference C implementation's encoder
// (libbrotlienc, tag v1.2.1): for the same parameters and the same sequence of
// BrotliEncoderCompressStream operations (with the same input chunking), it
// produces byte-for-byte identical output. Comments like "Mirrors Foo in
// encode.c" name the C function whose logic a Wuffs function mirrors.
//
// The C code's floating point (double) arithmetic is done with the
// std/softfloat package, which is bit-exact. FastLog2's log2 matches glibc's.
//
// Only qualities 0 and 1 (the C code's BrotliCompressFragmentFast and
// BrotliCompressFragmentTwoPass) are implemented so far. 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: qualities 2 to 11 (the C code's EncodeData, WriteMetaBlockInternal,
// hashers, block splitting, context modeling and Zopfli), large-window
// brotli at those qualities and custom (prepared or shared) dictionaries.
use "std/softfloat"
pub status "#unsupported encoder quality"
pri status "#internal error: inconsistent encoder state"
pub const ENCODER_DST_HISTORY_RETAIN_LENGTH_MAX_INCL_WORST_CASE : base.u64 = 0
// The C code's BrotliEncoderStreamState values.
pri const STREAM_PROCESSING : base.u32 = 0
pri const STREAM_FLUSH_REQUESTED : base.u32 = 1
pri const STREAM_FINISHED : base.u32 = 2
pri const STREAM_METADATA_HEAD : base.u32 = 3
pri const STREAM_METADATA_BODY : base.u32 = 4
// The C code's BrotliEncoderOperation values.
pri const OP_PROCESS : base.u32 = 0
pri const OP_FLUSH : base.u32 = 1
pri const OP_FINISH : base.u32 = 2
pri const OP_EMIT_METADATA : base.u32 = 3
// The C code's BrotliEncoderFlintState values, as two's complement u32s.
pri const FLINT_NEEDS_2_BYTES : base.u32 = 2
pri const FLINT_DONE : base.u32 = 0xFFFF_FFFF
// STORAGE_SLACK is how many bytes the storage (the workbuf) needs on top of
// the C code's (2 * block_size + 503) bytes for a metablock: the 8-byte
// stores of write_bits and up to 3 bytes for InjectBytePaddingBlock.
pri const STORAGE_SLACK : base.u64 = 16
// Mirrors kCompressFragmentTwoPassBlockSize in compress_fragment_two_pass.h.
pri const TWO_PASS_BLOCK_SIZE : base.u32 = 0x2_0000
pub struct encoder? implements base.io_transformer(
// The raw parameters, as set by set_quirk (the C code's
// BrotliEncoderSetParameter). The quality and lgwin defaults (11 and
// 22) apply unless the p_quality_set and p_lgwin_set bools are set.
p_mode : base.u32,
p_quality : base.u32,
p_quality_set : base.bool,
p_lgwin : base.u32,
p_lgwin_set : base.bool,
p_lgblock : base.u32,
p_disable_lcm : base.u32,
p_size_hint : base.u32,
p_large_window : base.u32,
p_npostfix : base.u32,
p_ndirect : base.u32,
p_stream_offset : base.u32,
// The parameters after EnsureInitialized (SanitizeParams,
// ComputeLgBlock and ChooseDistanceParams).
is_initialized : base.bool,
quality : base.u32[..= 11],
lgwin : base.u32[..= 30],
lgblock : base.u32[..= 30],
large_window : base.bool,
size_hint : base.u32,
stream_offset : base.u32,
distance_postfix_bits : base.u32,
num_direct_distance_codes : base.u32,
// The C code's BrotliEncoderState fields of the same name.
dist_cache : array[4] base.u32,
saved_dist_cache : array[4] base.u32,
flint : base.u32,
last_bytes : base.u32[..= 0xFFFF],
last_bytes_bits : base.u32[..= 15],
stream_state : base.u32,
remaining_metadata_bytes : base.u32,
// The pending output: out_len bytes starting at out_pos, in either
// the tiny_buf array (if out_in_tiny_buf) or in the storage (the
// workbuf). These are the C code's next_out_ and available_out_.
out_in_tiny_buf : base.bool,
out_pos : base.u64,
out_len : base.u64,
// The pending requests. See request_flush and request_emit_metadata.
flush_requested : base.bool,
metadata_requested : base.bool,
metadata_length : base.u32,
// bw_ix is the bit writer's position, the C code's *storage_ix.
bw_ix : base.u64,
// cmd_code_numbits is the C code's BrotliOnePassArena.cmd_code_numbits.
cmd_code_numbits : base.u64,
// huffman_tree_size is BrotliStoreHuffmanTree's huffman_tree_size.
huffman_tree_size : base.u32,
// num_commands and num_literals are the lengths of command_buf and
// literal_buf, as filled by CreateCommands.
num_commands : base.u32,
num_literals : base.u32,
sf : softfloat.arith,
util : base.utility,
) + (
// tiny_buf is the C code's tiny_buf_ (plus slack for the 8-byte
// stores of write_bits).
tiny_buf : array[32] base.u8,
// These are the fields of the C code's BrotliOnePassArena and
// BrotliTwoPassArena (an encoder uses only one of the two). The
// histogram field is the C code's BrotliOnePassArena.histogram and
// BrotliTwoPassArena.lit_histo.
lit_depth : array[256] base.u8,
lit_bits : array[256] base.u16,
cmd_depth : array[128] base.u8,
cmd_bits : array[128] base.u16,
cmd_histo : array[128] base.u32,
cmd_code : array[520] base.u8,
histogram : array[256] base.u32,
tmp_depth : array[704] base.u8,
tmp_bits : array[64] base.u16,
// The C code's HuffmanTree pool. See encode_entropy.wuffs.
tree_count : array[1024] base.u32,
tree_left : array[1024] base.u16,
tree_right : array[1024] base.u16,
// BrotliStoreHuffmanTree's local variables: huffman_tree,
// huffman_tree_extra_bits, huffman_tree_histogram,
// code_length_bitdepth and code_length_bitdepth_symbols.
huffman_tree : array[1024] base.u8,
huffman_tree_extra_bits : array[1024] base.u8,
cl_histo : array[32] base.u32,
cl_depth : array[32] base.u8,
cl_bits : array[32] base.u16,
// hash_table is the C code's small_table_ or large_table_ (the
// GetHashTable result), with up to (1 << 17) elements.
hash_table : array[131072] base.u32,
// command_buf and literal_buf are the C code's command_buf_ and
// literal_buf_ (quality 1 only).
command_buf : array[131072] base.u32,
literal_buf : array[131072] base.u8,
)
pub func encoder.get_quirk(key: base.u32) base.u64 {
if args.key == QUIRK_MODE {
return this.p_mode as base.u64
} else if args.key == QUIRK_ENCODER_QUALITY {
if not this.p_quality_set {
return 11
}
return this.p_quality as base.u64
} else if args.key == QUIRK_LGWIN {
if not this.p_lgwin_set {
return 22
}
return this.p_lgwin as base.u64
} else if args.key == QUIRK_LGBLOCK {
return this.p_lgblock as base.u64
} else if args.key == QUIRK_DISABLE_LITERAL_CONTEXT_MODELING {
return this.p_disable_lcm as base.u64
} else if args.key == QUIRK_SIZE_HINT {
return this.p_size_hint as base.u64
} else if args.key == QUIRK_LARGE_WINDOW {
return this.p_large_window as base.u64
} else if args.key == QUIRK_NPOSTFIX {
return this.p_npostfix as base.u64
} else if args.key == QUIRK_NDIRECT {
return this.p_ndirect as base.u64
} else if args.key == QUIRK_STREAM_OFFSET {
return this.p_stream_offset as base.u64
}
return 0
}
// Mirrors BrotliEncoderSetParameter in encode.c.
pub func encoder.set_quirk!(key: base.u32, value: base.u64) base.status {
var v : base.u32
// Changing parameters on the fly is not implemented (in the C code).
if this.is_initialized {
return base."#bad call sequence"
} else if args.value > 0xFFFF_FFFF {
return base."#bad argument"
}
v = (args.value & 0xFFFF_FFFF) as base.u32
if args.key == QUIRK_MODE {
this.p_mode = v
} else if args.key == QUIRK_ENCODER_QUALITY {
this.p_quality = v
this.p_quality_set = true
} else if args.key == QUIRK_LGWIN {
this.p_lgwin = v
this.p_lgwin_set = true
} else if args.key == QUIRK_LGBLOCK {
this.p_lgblock = v
} else if args.key == QUIRK_DISABLE_LITERAL_CONTEXT_MODELING {
if v > 1 {
return base."#bad argument"
}
this.p_disable_lcm = v
} else if args.key == QUIRK_SIZE_HINT {
this.p_size_hint = v
} else if args.key == QUIRK_LARGE_WINDOW {
if v <> 0 {
this.p_large_window = 1
} else {
this.p_large_window = 0
}
} else if args.key == QUIRK_NPOSTFIX {
this.p_npostfix = v
} else if args.key == QUIRK_NDIRECT {
this.p_ndirect = v
} else if args.key == QUIRK_STREAM_OFFSET {
if v > 0x4000_0000 {
return base."#bad argument"
}
this.p_stream_offset = v
} else {
return base."#unsupported option"
}
return ok
}
pub func encoder.dst_history_retain_length() base.optional_u63 {
return this.util.make_optional_u63(has_value: true, value: 0)
}
// workbuf_len returns the workbuf length needed for the current parameters
// (after the C code's SanitizeParams). At qualities 0 and 1 the workbuf is
// the storage for one metablock's output: ((2 << lgwin) + 503) bytes (the C
// code's GetBrotliStorage size for a (1 << lgwin) byte block) plus 16 bytes
// of slack. A shorter workbuf still works if every transform_io input block
// is short enough: a block of n bytes needs ((2 * n) + 519) bytes.
pub func encoder.workbuf_len() base.range_ii_u64 {
var q : base.u32[..= 11]
var n : base.u64
q = this.sanitized_quality()
if q <= 1 {
n = ((2 as base.u64) << this.sanitized_lgwin(quality: q)) + 503 + STORAGE_SLACK
}
return this.util.make_range_ii_u64(min_incl: n, max_incl: n)
}
// max_compressed_size mirrors BrotliEncoderMaxCompressedSize in encode.c:
// an upper bound on the compressed size of input_size bytes (with an
// uncompressed fallback, which this encoder does not do itself), or 0 on
// overflow.
pub func encoder.max_compressed_size(input_size: base.u64) base.u64 {
var num_large_blocks : base.u64
var overhead : base.u64
var result : base.u64
num_large_blocks = args.input_size >> 14
overhead = 2 + (4 * num_large_blocks) + 3 + 1
result = args.input_size ~mod+ overhead
if args.input_size == 0 {
return 2
} else if result < args.input_size {
return 0
}
return result
}
// request_flush makes the next transform_io call act like the C code's
// BrotliEncoderCompressStream with BROTLI_OPERATION_FLUSH (instead of
// PROCESS or FINISH), until the flush is complete. It is complete once a
// transform_io call returns "$short read" (or ok).
pub func encoder.request_flush!() base.status {
if this.metadata_requested {
return base."#bad call sequence"
}
this.flush_requested = true
return ok
}
// request_emit_metadata makes the next transform_io call act like the C
// code's BrotliEncoderCompressStream with BROTLI_OPERATION_EMIT_METADATA,
// with the next length bytes of src as the metadata. Once those length bytes
// have been read (transform_io returns "$short read" if src has fewer of
// them), and the metadata block written, transform_io continues as usual.
// Like the C code, length can be at most (1 << 24).
pub func encoder.request_emit_metadata!(length: base.u64) base.status {
if args.length > 0x100_0000 {
return base."#bad argument"
} else if this.flush_requested or this.metadata_requested {
return base."#bad call sequence"
}
this.metadata_requested = true
this.metadata_length = (args.length & 0xFFFF_FFFF) as base.u32
return ok
}
// sanitized_quality mirrors SanitizeParams in quality.h, for the quality.
pri func encoder.sanitized_quality() base.u32[..= 11] {
if not this.p_quality_set {
return 11
} else if this.p_quality >= 0x8000_0000 {
// A negative (int) value.
return 0
}
return this.p_quality.min(no_more_than: 11)
}
// sanitized_lgwin mirrors SanitizeParams in quality.h, for the lgwin.
pri func encoder.sanitized_lgwin(quality: base.u32[..= 11]) base.u32[..= 30] {
var lgwin : base.u32
lgwin = 22
if this.p_lgwin_set {
lgwin = this.p_lgwin
}
if (lgwin >= 0x8000_0000) or (lgwin < 10) {
return 10
} else if (this.p_large_window <> 0) and (args.quality > 2) {
return lgwin.min(no_more_than: 30)
}
return lgwin.min(no_more_than: 24)
}
// Mirrors EnsureInitialized (and BrotliEncoderInitState's distance cache
// initialization) in encode.c.
pri func encoder.ensure_initialized!() {
var lgblock : base.u32
var lgwin : base.u32[..= 30]
var limit : base.u32
var npostfix : base.u32
var ndirect : base.u32
var ndirect_msb : base.u32
var i : base.u32
if this.is_initialized {
return nothing
}
// BrotliEncoderInitState.
this.dist_cache[0] = 4
this.dist_cache[1] = 11
this.dist_cache[2] = 15
this.dist_cache[3] = 16
this.saved_dist_cache[0] = 4
this.saved_dist_cache[1] = 11
this.saved_dist_cache[2] = 15
this.saved_dist_cache[3] = 16
this.stream_state = STREAM_PROCESSING
this.last_bytes_bits = 0
this.last_bytes = 0
this.flint = FLINT_DONE
this.remaining_metadata_bytes = 0xFFFF_FFFF
// SanitizeParams.
this.quality = this.sanitized_quality()
this.large_window = (this.p_large_window <> 0) and (this.quality > 2)
this.lgwin = this.sanitized_lgwin(quality: this.quality)
this.size_hint = this.p_size_hint
this.stream_offset = this.p_stream_offset
// ComputeLgBlock.
if this.quality <= 1 {
this.lgblock = this.lgwin
} else if this.quality < 4 {
this.lgblock = 14
} else if this.p_lgblock == 0 {
this.lgblock = 16
if (this.quality >= 9) and (this.lgwin > 16) {
this.lgblock = this.lgwin.min(no_more_than: 18)
}
} else {
lgblock = this.p_lgblock
if lgblock >= 0x8000_0000 {
// A negative (int) value.
lgblock = 16
}
this.lgblock = lgblock.max(no_less_than: 16).min(no_more_than: 24)
}
// ChooseDistanceParams. BrotliInitDistanceParams' other fields are not
// needed at qualities 0 and 1.
if this.quality >= 4 {
if this.p_mode == 2 {
npostfix = 1
ndirect = 12
} else {
npostfix = this.p_npostfix
ndirect = this.p_ndirect
}
if npostfix > 3 {
npostfix = 0
ndirect = 0
} else {
ndirect_msb = (ndirect >> npostfix) & 0x0F
if (ndirect > 120) or ((ndirect_msb << npostfix) <> ndirect) {
npostfix = 0
ndirect = 0
}
}
}
this.distance_postfix_bits = npostfix
this.num_direct_distance_codes = ndirect
if this.stream_offset <> 0 {
this.flint = FLINT_NEEDS_2_BYTES
// Poison the distance cache. -16 +- 3 is still less than zero
// (invalid).
i = 0
while i < 4 {
this.dist_cache[i] = 0xFFFF_FFF0
this.saved_dist_cache[i] = 0xFFFF_FFF0
i += 1
}
}
// Initialize last byte with stream header.
lgwin = this.lgwin
if this.quality <= 1 {
lgwin = lgwin.max(no_less_than: 18)
}
if this.stream_offset == 0 {
this.encode_window_bits!(lgwin: lgwin)
} else {
// Bigger values have the same effect, but could cause overflows.
limit = ((1 as base.u32) << lgwin) ~mod- 16
this.stream_offset = this.stream_offset.min(no_more_than: limit)
}
if this.quality == 0 {
this.init_command_prefix_codes!()
}
this.is_initialized = true
}
// Mirrors EncodeWindowBits in encode.c.
pri func encoder.encode_window_bits!(lgwin: base.u32[..= 30]) {
if this.large_window {
this.last_bytes = ((args.lgwin & 0x3F) << 8) | 0x11
this.last_bytes_bits = 14
} else if args.lgwin == 16 {
this.last_bytes = 0
this.last_bytes_bits = 1
} else if args.lgwin == 17 {
this.last_bytes = 1
this.last_bytes_bits = 7
} else if args.lgwin > 17 {
this.last_bytes = ((args.lgwin - 17) << 1) | 0x01
this.last_bytes_bits = 4
} else if args.lgwin >= 8 {
this.last_bytes = ((args.lgwin - 8) << 4) | 0x01
this.last_bytes_bits = 7
}
}
// Mirrors InitCommandPrefixCodes in encode.c.
pri func encoder.init_command_prefix_codes!() {
var i : base.u32
i = 0
while i < 128 {
this.cmd_depth[i] = DEFAULT_COMMAND_DEPTHS[i]
this.cmd_bits[i] = DEFAULT_COMMAND_BITS[i]
i += 1
}
i = 0
while i < 57 {
this.cmd_code[i] = DEFAULT_COMMAND_CODE[i]
i += 1
}
this.cmd_code_numbits = DEFAULT_COMMAND_CODE_NUM_BITS as base.u64
}
// Mirrors HashTableSize and GetHashTable in encode.c. It zeroes the first
// htsize elements of this.hash_table and returns log2(htsize), the C code's
// table_bits (Log2FloorNonZero(table_size) in BrotliCompressFragmentFast and
// BrotliCompressFragmentTwoPass).
pri func encoder.get_hash_table!(input_size: base.u64) base.u32 {
var max_table_size : base.u64
var htsize : base.u64
var table_bits : base.u32
// MaxHashTableSize in quality.h.
max_table_size = 0x2_0000
if this.quality == 0 {
max_table_size = 0x8000
}
htsize = 256
table_bits = 8
while (htsize < max_table_size) and (htsize < args.input_size) {
htsize ~mod<<= 1
table_bits ~mod+= 1
}
if this.quality == 0 {
// Only odd shifts are supported by fast-one-pass.
if (htsize & 0xA_AAAA) == 0 {
htsize ~mod<<= 1
table_bits ~mod+= 1
}
}
if htsize <= 0x2_0000 {
this.hash_table[.. htsize].bulk_memset!(byte_value: 0)
}
return table_bits
}
// Mirrors InjectBytePaddingBlock in encode.c. Its destination is right after
// the pending output (in the storage or in tiny_buf).
pri func encoder.inject_byte_padding_block!(workbuf: slice base.u8) {
var seal : base.u32
var seal_bits : base.u32
var p : base.u64
var n : base.u32
seal = this.last_bytes
seal_bits = this.last_bytes_bits
this.last_bytes = 0
this.last_bytes_bits = 0
// is_last = 0, data_nibbles = 11, reserved = 0, meta_nibbles = 00.
seal |= (6 as base.u32) << seal_bits
seal_bits += 6
n = (seal_bits + 7) >> 3
if this.out_len == 0 {
this.out_in_tiny_buf = true
this.out_pos = 0
}
p = this.out_pos ~mod+ this.out_len
if this.out_in_tiny_buf {
if p <= 24 {
this.tiny_buf[p] = (seal & 0xFF) as base.u8
this.tiny_buf[p + 1] = ((seal >> 8) & 0xFF) as base.u8
this.tiny_buf[p + 2] = ((seal >> 16) & 0xFF) as base.u8
}
} else if p < args.workbuf.length() {
args.workbuf[p] = (seal & 0xFF) as base.u8
if seal_bits > 8 {
p ~mod+= 1
if p < args.workbuf.length() {
args.workbuf[p] = ((seal >> 8) & 0xFF) as base.u8
}
}
if seal_bits > 16 {
p ~mod+= 1
if p < args.workbuf.length() {
args.workbuf[p] = ((seal >> 16) & 0xFF) as base.u8
}
}
}
this.out_len ~mod+= n as base.u64
}
// push_output copies as much pending output to args.dst as fits, like the
// second half of InjectFlushOrPushOutput in encode.c.
pri func encoder.push_output!(dst: base.io_writer, workbuf: slice base.u8) {
var n : base.u64
var end : base.u64
end = this.out_pos ~mod+ this.out_len
if this.out_in_tiny_buf {
if (this.out_pos <= end) and (end <= 32) {
n = args.dst.copy_from_slice!(s: this.tiny_buf[this.out_pos .. end])
}
} else if (this.out_pos <= end) and (end <= args.workbuf.length()) {
n = args.dst.copy_from_slice!(s: args.workbuf[this.out_pos .. end])
}
if n == 0 {
// Should be unreachable, since the caller checks that args.dst is
// not full. Discard the output so that the caller makes progress.
this.out_len = 0
return nothing
}
n = n.min(no_more_than: this.out_len)
this.out_pos ~mod+= n
this.out_len ~mod-= n
}
// Mirrors CheckFlushComplete in encode.c.
pri func encoder.check_flush_complete!() {
if (this.stream_state == STREAM_FLUSH_REQUESTED) and (this.out_len == 0) {
this.stream_state = STREAM_PROCESSING
this.out_in_tiny_buf = false
this.out_pos = 0
}
}
// Mirrors WriteMetadataHeader in encode.c, writing to tiny_buf.
pri func encoder.write_metadata_header!(block_size: base.u32) base.u64 {
var nbits : base.u32[..= 32]
var nbytes : base.u32[..= 4]
this.bw_ix = this.last_bytes_bits as base.u64
this.tiny_buf[0] = (this.last_bytes & 0xFF) as base.u8
this.tiny_buf[1] = (this.last_bytes >> 8) as base.u8
this.last_bytes = 0
this.last_bytes_bits = 0
this.write_bits!(n_bits: 1, bits: 0, s: this.tiny_buf[..])
this.write_bits!(n_bits: 2, bits: 3, s: this.tiny_buf[..])
this.write_bits!(n_bits: 1, bits: 0, s: this.tiny_buf[..])
if args.block_size == 0 {
this.write_bits!(n_bits: 2, bits: 0, s: this.tiny_buf[..])
} else {
nbits = 1
if args.block_size > 1 {
nbits = this.log2_floor_non_zero(n: (args.block_size - 1) as base.u64) + 1
}
nbytes = (nbits + 7) / 8
this.write_bits!(n_bits: 2, bits: nbytes as base.u64, s: this.tiny_buf[..])
this.write_bits!(n_bits: 8 * nbytes, bits: (args.block_size - 1) as base.u64, s: this.tiny_buf[..])
}
return (this.bw_ix ~mod+ 7) >> 3
}
// transform_io mirrors BrotliEncoderCompressStream in encode.c (and, for
// qualities 0 and 1, BrotliEncoderCompressStreamFast). Each (initial or
// resumed) transform_io call corresponds to one or more C calls. See
// README.md for the details.
pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
var op : base.u32
var avail : base.u64
var block_size : base.u64
var is_last : base.bool
var force_flush : base.bool
var storage_len : base.u64
var table_bits : base.u32
var mark : base.u64
var n32 : base.u32
var ix : base.u64
this.ensure_initialized!()
if this.quality > 1 {
// TODO: implement the C code's EncodeData (and its callees) for
// qualities 2 to 11.
return "#unsupported encoder quality"
}
while true {
// ---- The start of a C BrotliEncoderCompressStream call.
if this.metadata_requested {
// op == BROTLI_OPERATION_EMIT_METADATA. UpdateSizeHint(s, 0) is a
// no-op at qualities 0 and 1 (and so is ProcessMetadata's
// EncodeData call, as the fast path never advances input_pos_).
//
// Mirrors ProcessMetadata in encode.c.
if this.stream_state == STREAM_PROCESSING {
this.remaining_metadata_bytes = this.metadata_length
this.stream_state = STREAM_METADATA_HEAD
}
if (this.stream_state <> STREAM_METADATA_HEAD) and
(this.stream_state <> STREAM_METADATA_BODY) {
return base."#bad call sequence"
}
while true {
if this.out_len <> 0 {
if args.dst.length() <= 0 {
yield? base."$short write"
continue
}
this.push_output!(dst: args.dst, workbuf: args.workbuf)
continue
}
if this.stream_state == STREAM_METADATA_HEAD {
this.out_len = this.write_metadata_header!(block_size: this.remaining_metadata_bytes)
this.out_in_tiny_buf = true
this.out_pos = 0
this.stream_state = STREAM_METADATA_BODY
continue
}
// Exit workflow only when there is no more input and no more
// output.
if this.remaining_metadata_bytes == 0 {
this.remaining_metadata_bytes = 0xFFFF_FFFF
this.stream_state = STREAM_PROCESSING
this.metadata_requested = false
break
}
if args.dst.length() <= 0 {
yield? base."$short write"
continue
} else if args.src.length() <= 0 {
if args.src.is_closed() {
return "#truncated input"
}
yield? base."$short read"
continue
}
// Directly copy input to output.
n32 = args.dst.limited_copy_u32_from_reader!(
up_to: this.remaining_metadata_bytes, r: args.src)
this.remaining_metadata_bytes ~mod-= n32
}
continue
}
if this.flush_requested {
op = OP_FLUSH
} else if args.src.is_closed() {
op = OP_FINISH
} else {
op = OP_PROCESS
}
if (this.stream_state == STREAM_METADATA_HEAD) or
(this.stream_state == STREAM_METADATA_BODY) {
return base."#bad call sequence"
} else if (this.stream_state <> STREAM_PROCESSING) and (args.src.length() > 0) {
return base."#bad call sequence"
}
// Mirrors BrotliEncoderCompressStreamFast in encode.c.
while true {
// Mirrors InjectFlushOrPushOutput in encode.c.
if (this.stream_state == STREAM_FLUSH_REQUESTED) and (this.last_bytes_bits <> 0) {
this.inject_byte_padding_block!(workbuf: args.workbuf)
continue
}
if (this.out_len <> 0) and (args.dst.length() > 0) {
this.push_output!(dst: args.dst, workbuf: args.workbuf)
continue
}
// Compress block only when internal output buffer is empty,
// stream is not finished, there is no pending flush request, and
// there is either additional input or pending operation.
if (this.out_len == 0) and (this.stream_state == STREAM_PROCESSING) and
((args.src.length() > 0) or (op <> OP_PROCESS)) {
avail = args.src.length()
block_size = avail.min(no_more_than: (1 as base.u64) << this.lgwin)
is_last = (avail == block_size) and (op == OP_FINISH)
force_flush = (avail == block_size) and (op == OP_FLUSH)
if force_flush and (block_size == 0) {
this.stream_state = STREAM_FLUSH_REQUESTED
continue
}
storage_len = (block_size * 2) + 503 + STORAGE_SLACK
if (storage_len > args.workbuf.length()) or (args.workbuf.length() < 2) {
return base."#bad workbuf length"
}
args.workbuf[0] = (this.last_bytes & 0xFF) as base.u8
args.workbuf[1] = (this.last_bytes >> 8) as base.u8
this.bw_ix = this.last_bytes_bits as base.u64
table_bits = this.get_hash_table!(input_size: block_size)
// The C code compresses straight from next_in. So does this
// code, from the args.src bytes that it skips over.
mark = args.src.mark()
args.src.skip?(n: block_size)
if storage_len > args.workbuf.length() {
return "#internal error: inconsistent encoder state"
}
if this.quality == 0 {
this.compress_fragment_fast!(
input: args.src.since(mark: mark),
is_last: is_last,
table_bits: table_bits,
s: args.workbuf[.. storage_len])
} else {
this.compress_fragment_two_pass!(
input: args.src.since(mark: mark),
is_last: is_last,
table_bits: table_bits,
s: args.workbuf[.. storage_len])
}
this.out_in_tiny_buf = false
this.out_pos = 0
this.out_len = this.bw_ix >> 3
ix = this.bw_ix >> 3
if ix < args.workbuf.length() {
this.last_bytes = args.workbuf[ix] as base.u32
} else {
this.last_bytes = 0
}
this.last_bytes_bits = (this.bw_ix & 7) as base.u32
if force_flush {
this.stream_state = STREAM_FLUSH_REQUESTED
}
if is_last {
this.stream_state = STREAM_FINISHED
}
continue
}
break
}
this.check_flush_complete!()
// ---- The end of a C BrotliEncoderCompressStream call.
if this.out_len <> 0 {
yield? base."$short write"
} else if op == OP_FLUSH {
this.flush_requested = false
} else if this.stream_state == STREAM_FINISHED {
return ok
} else if op == OP_PROCESS {
yield? base."$short read"
} else {
return "#internal error: inconsistent encoder state"
}
}
}
+804
View File
@@ -0,0 +1,804 @@
// 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 the Huffman (prefix code) construction and storage
// functions that the quality 0 and quality 1 encoders need, from the C code's
// entropy_encode.c and brotli_bit_stream.c.
//
// The C code's HuffmanTree array (a pool of nodes) is the encoder.tree_count,
// encoder.tree_left and encoder.tree_right arrays. A node's int16_t
// index_left_ and index_right_or_value_ fields are stored as base.u16 values,
// with 0xFFFF meaning -1. The C code's pools hold at most (2 * 256) + 1
// nodes. The arrays have 1024 elements so that indexes can be masked.
// TREE_NONE is the C code's -1 for a HuffmanTree index.
pri const TREE_NONE : base.u16 = 0xFFFF
// Mirrors BrotliSetDepth in entropy_encode.c.
pri func encoder.set_depth!(p0: base.u32, depth: slice base.u8, max_depth: base.u32[..= 15]) base.bool {
var stack : array[16] base.u32
var level : base.u32[..= 15]
var p : base.u32
var v : base.u64
stack[0] = 0xFFFF_FFFF
p = args.p0
while true {
if this.tree_left[p & 1023] <> TREE_NONE {
if (level >= args.max_depth) or (level >= 15) {
return false
}
level += 1
stack[level] = this.tree_right[p & 1023] as base.u32
p = this.tree_left[p & 1023] as base.u32
continue
}
v = this.tree_right[p & 1023] as base.u64
if v < args.depth.length() {
args.depth[v] = level as base.u8
}
while stack[level] == 0xFFFF_FFFF {
if level == 0 {
return true
}
level -= 1
}
p = stack[level]
stack[level] = 0xFFFF_FFFF
}
}
// huffman_tree_less is the C code's SortHuffmanTree comparators. With
// tie_break, it mirrors the one in entropy_encode.c (ties are broken by
// index_right_or_value_, in decreasing order). Without, it mirrors the one in
// brotli_bit_stream.c (only total_count_ is compared).
pri func encoder.huffman_tree_less(c0: base.u32, r0: base.u16, c1: base.u32, r1: base.u16, tie_break: base.bool) base.bool {
if args.c0 <> args.c1 {
return args.c0 < args.c1
}
return args.tie_break and (args.r0 > args.r1)
}
// Mirrors SortHuffmanTreeItems in entropy_encode.h, sorting the first n
// nodes of the pool.
pri func encoder.sort_huffman_tree_items!(n: base.u32, tie_break: base.bool) {
var i : base.u32
var j : base.u32
var k : base.u32
var g : base.u32
var gap : base.u32
var tc : base.u32
var tl : base.u16
var tr : base.u16
if args.n < 13 {
// Insertion sort.
i = 1
while i < args.n {
tc = this.tree_count[i & 1023]
tl = this.tree_left[i & 1023]
tr = this.tree_right[i & 1023]
k = i
j = i ~mod- 1
while this.huffman_tree_less(
c0: tc,
r0: tr,
c1: this.tree_count[j & 1023],
r1: this.tree_right[j & 1023],
tie_break: args.tie_break) {
this.tree_count[k & 1023] = this.tree_count[j & 1023]
this.tree_left[k & 1023] = this.tree_left[j & 1023]
this.tree_right[k & 1023] = this.tree_right[j & 1023]
k = j
if j == 0 {
break
}
j -= 1
}
this.tree_count[k & 1023] = tc
this.tree_left[k & 1023] = tl
this.tree_right[k & 1023] = tr
i ~mod+= 1
}
return nothing
}
// Shell sort.
g = 0
if args.n < 57 {
g = 2
}
while g < 6 {
gap = SHELL_GAPS[g]
i = gap
while i < args.n {
j = i
tc = this.tree_count[i & 1023]
tl = this.tree_left[i & 1023]
tr = this.tree_right[i & 1023]
while j >= gap {
if not this.huffman_tree_less(
c0: tc,
r0: tr,
c1: this.tree_count[(j ~mod- gap) & 1023],
r1: this.tree_right[(j ~mod- gap) & 1023],
tie_break: args.tie_break) {
break
}
this.tree_count[j & 1023] = this.tree_count[(j ~mod- gap) & 1023]
this.tree_left[j & 1023] = this.tree_left[(j ~mod- gap) & 1023]
this.tree_right[j & 1023] = this.tree_right[(j ~mod- gap) & 1023]
j ~mod-= gap
}
this.tree_count[j & 1023] = tc
this.tree_left[j & 1023] = tl
this.tree_right[j & 1023] = tr
i ~mod+= 1
}
g ~mod+= 1
}
}
// set_sentinel sets the pool's i'th node to the C code's sentinel node.
pri func encoder.set_sentinel!(i: base.u32) {
this.tree_count[args.i & 1023] = 0xFFFF_FFFF
this.tree_left[args.i & 1023] = TREE_NONE
this.tree_right[args.i & 1023] = TREE_NONE
}
// merge_huffman_nodes runs the C code's "for (k = n - 1; k != 0; --k)" loop
// (shared by BrotliCreateHuffmanTree in entropy_encode.c and
// BrotliBuildAndStoreHuffmanTreeFast in brotli_bit_stream.c) that combines
// the n sorted leaf nodes into a tree, whose root is node ((2 * n) - 1).
pri func encoder.merge_huffman_nodes!(n: base.u32) {
var i : base.u32
var j : base.u32
var k : base.u32
var left : base.u32
var right : base.u32
var j_end : base.u32
this.set_sentinel!(i: args.n)
this.set_sentinel!(i: args.n ~mod+ 1)
i = 0
j = args.n ~mod+ 1
k = args.n ~mod- 1
while k <> 0 {
if this.tree_count[i & 1023] <= this.tree_count[j & 1023] {
left = i
i ~mod+= 1
} else {
left = j
j ~mod+= 1
}
if this.tree_count[i & 1023] <= this.tree_count[j & 1023] {
right = i
i ~mod+= 1
} else {
right = j
j ~mod+= 1
}
j_end = (args.n ~mod* 2) ~mod- k
this.tree_count[j_end & 1023] = this.tree_count[left & 1023] ~mod+ this.tree_count[right & 1023]
this.tree_left[j_end & 1023] = (left & 0xFFFF) as base.u16
this.tree_right[j_end & 1023] = (right & 0xFFFF) as base.u16
this.set_sentinel!(i: j_end ~mod+ 1)
k -= 1
}
}
// Mirrors BrotliCreateHuffmanTree in entropy_encode.c. Wuffs' C code
// generator does not support "slice base.u32" arguments, so the C code's data
// argument is either this.cmd_histo[args.off ..] (when args.src is 0) or
// this.cl_histo[args.off ..] (when args.src is 1).
pri func encoder.create_huffman_tree!(src: base.u32, off: base.u32, length: base.u32, tree_limit: base.u32[..= 15], depth: slice base.u8) {
var count_limit : base.u32
var n : base.u32
var i : base.u64
var count : base.u32
var v : base.u64
var depth_ok : base.bool
count_limit = 1
while true {
n = 0
i = args.length as base.u64
while i > 0 {
i -= 1
if args.src == 0 {
count = this.cmd_histo[((args.off as base.u64) ~mod+ i) & 127]
} else {
count = this.cl_histo[((args.off as base.u64) ~mod+ i) & 31]
}
if count <> 0 {
this.tree_count[n & 1023] = count.max(no_less_than: count_limit)
this.tree_left[n & 1023] = TREE_NONE
this.tree_right[n & 1023] = (i & 0xFFFF) as base.u16
n ~mod+= 1
}
}
if n == 1 {
v = this.tree_right[0] as base.u64
if v < args.depth.length() {
args.depth[v] = 1
}
break
} else if n == 0 {
// The C code never calls this with an all-zero histogram.
break
}
this.sort_huffman_tree_items!(n: n, tie_break: true)
this.merge_huffman_nodes!(n: n)
depth_ok = this.set_depth!(p0: (n ~mod* 2) ~mod- 1, depth: args.depth, max_depth: args.tree_limit)
if depth_ok {
break
}
count_limit ~mod*= 2
}
}
// Mirrors BrotliReverseBits in entropy_encode.c.
pri func encoder.reverse_bits(num_bits: base.u32, bits: base.u16) base.u16 {
var retval : base.u32
var bits : base.u32
var i : base.u32
bits = args.bits as base.u32
retval = REVERSE_BITS_LUT[bits & 15]
i = 4
while i < args.num_bits {
retval = (retval ~mod<< 4) | REVERSE_BITS_LUT[(bits >> 4) & 15]
bits >>= 4
i ~mod+= 4
}
retval >>= (0 ~mod- args.num_bits) & 3
return (retval & 0xFFFF) as base.u16
}
// Mirrors BrotliConvertBitDepthsToSymbols in entropy_encode.c. Wuffs' C code
// generator does not support "slice base.u16" arguments, so the C code's bits
// argument is this.tmp_bits, this.cmd_bits[64 ..], this.cl_bits or
// this.lit_bits when args.dst is 0, 1, 2 or 3.
pri func encoder.convert_bit_depths_to_symbols!(depth: slice base.u8, len: base.u32, dst: base.u32) {
var bl_count : array[16] base.u16
var next_code : array[16] base.u16
var i : base.u64
var code : base.u32
var d : base.u32
var r : base.u16
i = 0
while i < (args.len as base.u64) {
if i < args.depth.length() {
d = (args.depth[i] & 15) as base.u32
bl_count[d] ~mod+= 1
}
i ~mod+= 1
}
bl_count[0] = 0
next_code[0] = 0
i = 1
while i < 16 {
code = (code ~mod+ (bl_count[(i ~mod- 1) & 15] as base.u32)) ~mod<< 1
next_code[i] = (code & 0xFFFF) as base.u16
i ~mod+= 1
}
i = 0
while i < (args.len as base.u64) {
if i < args.depth.length() {
d = (args.depth[i] & 15) as base.u32
if d <> 0 {
r = this.reverse_bits(num_bits: d, bits: next_code[d])
if args.dst == 0 {
this.tmp_bits[i & 63] = r
} else if args.dst == 1 {
this.cmd_bits[(i ~mod+ 64) & 127] = r
} else if args.dst == 2 {
this.cl_bits[i & 31] = r
} else {
this.lit_bits[i & 255] = r
}
next_code[d] ~mod+= 1
}
}
i ~mod+= 1
}
}
// Mirrors Reverse in entropy_encode.c, applied to both the huffman_tree and
// huffman_tree_extra_bits arrays.
pri func encoder.reverse_huffman_tree!(start: base.u32, end: base.u32) {
var start : base.u32
var end : base.u32
var tmp : base.u8
start = args.start
end = args.end ~mod- 1
while start < end {
tmp = this.huffman_tree[start & 1023]
this.huffman_tree[start & 1023] = this.huffman_tree[end & 1023]
this.huffman_tree[end & 1023] = tmp
tmp = this.huffman_tree_extra_bits[start & 1023]
this.huffman_tree_extra_bits[start & 1023] = this.huffman_tree_extra_bits[end & 1023]
this.huffman_tree_extra_bits[end & 1023] = tmp
start ~mod+= 1
end ~mod-= 1
}
}
// push_huffman_tree appends (value, extra_bits) to the huffman_tree and
// huffman_tree_extra_bits arrays.
pri func encoder.push_huffman_tree!(value: base.u8, extra_bits: base.u8) {
this.huffman_tree[this.huffman_tree_size & 1023] = args.value
this.huffman_tree_extra_bits[this.huffman_tree_size & 1023] = args.extra_bits
this.huffman_tree_size ~mod+= 1
}
// Mirrors BrotliWriteHuffmanTreeRepetitions in entropy_encode.c.
pri func encoder.write_huffman_tree_repetitions!(previous_value: base.u8, value: base.u8, repetitions: base.u32) {
var repetitions : base.u32
var start : base.u32
repetitions = args.repetitions
if args.previous_value <> args.value {
this.push_huffman_tree!(value: args.value, extra_bits: 0)
repetitions ~mod-= 1
}
if repetitions == 7 {
this.push_huffman_tree!(value: args.value, extra_bits: 0)
repetitions ~mod-= 1
}
if repetitions < 3 {
while repetitions > 0 {
this.push_huffman_tree!(value: args.value, extra_bits: 0)
repetitions -= 1
}
} else {
start = this.huffman_tree_size
repetitions -= 3
while true {
this.push_huffman_tree!(value: 16, extra_bits: (repetitions & 3) as base.u8)
repetitions >>= 2
if repetitions == 0 {
break
}
repetitions -= 1
}
this.reverse_huffman_tree!(start: start, end: this.huffman_tree_size)
}
}
// Mirrors BrotliWriteHuffmanTreeRepetitionsZeros in entropy_encode.c.
pri func encoder.write_huffman_tree_repetitions_zeros!(repetitions: base.u32) {
var repetitions : base.u32
var start : base.u32
repetitions = args.repetitions
if repetitions == 11 {
this.push_huffman_tree!(value: 0, extra_bits: 0)
repetitions -= 1
}
if repetitions < 3 {
while repetitions > 0 {
this.push_huffman_tree!(value: 0, extra_bits: 0)
repetitions -= 1
}
} else {
start = this.huffman_tree_size
repetitions -= 3
while true {
this.push_huffman_tree!(value: 17, extra_bits: (repetitions & 7) as base.u8)
repetitions >>= 3
if repetitions == 0 {
break
}
repetitions -= 1
}
this.reverse_huffman_tree!(start: start, end: this.huffman_tree_size)
}
}
// Mirrors DecideOverRleUse in entropy_encode.c. The result's 0x01 bit is
// use_rle_for_non_zero and its 0x02 bit is use_rle_for_zero.
pri func encoder.decide_over_rle_use(depth: slice base.u8, length: base.u32) base.u32 {
var total_reps_zero : base.u32
var total_reps_non_zero : base.u32
var count_reps_zero : base.u32
var count_reps_non_zero : base.u32
var i : base.u64
var k : base.u64
var length : base.u64
var reps : base.u32
var value : base.u8
var ret : base.u32
length = args.length as base.u64
count_reps_zero = 1
count_reps_non_zero = 1
i = 0
while i < length {
if i >= args.depth.length() {
break
}
value = args.depth[i]
reps = 1
k = i ~mod+ 1
while (k < length) and (k < args.depth.length()) {
if args.depth[k] <> value {
break
}
reps ~mod+= 1
k ~mod+= 1
}
if (reps >= 3) and (value == 0) {
total_reps_zero ~mod+= reps
count_reps_zero ~mod+= 1
}
if (reps >= 4) and (value <> 0) {
total_reps_non_zero ~mod+= reps
count_reps_non_zero ~mod+= 1
}
i ~mod+= reps as base.u64
}
if total_reps_non_zero > (count_reps_non_zero ~mod* 2) {
ret |= 1
}
if total_reps_zero > (count_reps_zero ~mod* 2) {
ret |= 2
}
return ret
}
// Mirrors BrotliWriteHuffmanTree in entropy_encode.c. The output is appended
// to the huffman_tree and huffman_tree_extra_bits arrays.
pri func encoder.write_huffman_tree!(depth: slice base.u8, length: base.u32) {
var previous_value : base.u8
var i : base.u64
var k : base.u64
var length : base.u64
var reps : base.u32
var new_length : base.u64
var rle : base.u32
var value : base.u8
previous_value = 8 // BROTLI_INITIAL_REPEATED_CODE_LENGTH.
length = args.length as base.u64
// Throw away trailing zeros.
new_length = length
i = 0
while i < length {
k = (length ~mod- i) ~mod- 1
if k >= args.depth.length() {
break
}
if args.depth[k] == 0 {
new_length ~mod-= 1
} else {
break
}
i ~mod+= 1
}
// First gather statistics on if it is a good idea to do RLE.
if length > 50 {
rle = this.decide_over_rle_use(depth: args.depth, length: (new_length & 0xFFFF_FFFF) as base.u32)
}
// Actual RLE coding.
i = 0
while i < new_length {
if i >= args.depth.length() {
break
}
value = args.depth[i]
reps = 1
if ((value <> 0) and ((rle & 1) <> 0)) or ((value == 0) and ((rle & 2) <> 0)) {
k = i ~mod+ 1
while (k < new_length) and (k < args.depth.length()) {
if args.depth[k] <> value {
break
}
reps ~mod+= 1
k ~mod+= 1
}
}
if value == 0 {
this.write_huffman_tree_repetitions_zeros!(repetitions: reps)
} else {
this.write_huffman_tree_repetitions!(previous_value: previous_value, value: value, repetitions: reps)
previous_value = value
}
i ~mod+= reps as base.u64
}
}
// Mirrors BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask in
// brotli_bit_stream.c, using this.cl_depth as code_length_bitdepth.
pri func encoder.store_huffman_tree_of_huffman_tree_to_bit_mask!(num_codes: base.u32, s: slice base.u8) {
var skip_some : base.u32
var codes_to_store : base.u32[..= 18]
var i : base.u32
var l : base.u32
// Throw away trailing zeros.
codes_to_store = 18
if args.num_codes > 1 {
while codes_to_store > 0 {
if this.cl_depth[STORAGE_ORDER[codes_to_store - 1]] <> 0 {
break
}
codes_to_store -= 1
}
}
if (this.cl_depth[STORAGE_ORDER[0]] == 0) and (this.cl_depth[STORAGE_ORDER[1]] == 0) {
skip_some = 2
if this.cl_depth[STORAGE_ORDER[2]] == 0 {
skip_some = 3
}
}
this.write_bits!(n_bits: 2, bits: skip_some as base.u64, s: args.s)
i = skip_some
while (i < (codes_to_store as base.u32)) and (i < 18) {
l = this.cl_depth[STORAGE_ORDER[i]] as base.u32
if l < 6 {
this.write_bits!(
n_bits: HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_BIT_LENGTHS[l],
bits: HUFFMAN_BIT_LENGTH_HUFFMAN_CODE_SYMBOLS[l],
s: args.s)
}
i ~mod+= 1
}
}
// Mirrors BrotliStoreHuffmanTreeToBitMask in brotli_bit_stream.c, using
// this.cl_depth and this.cl_bits as code_length_bitdepth and
// code_length_bitdepth_symbols.
pri func encoder.store_huffman_tree_to_bit_mask!(s: slice base.u8) {
var i : base.u32
var ix : base.u32
i = 0
while i < this.huffman_tree_size {
ix = (this.huffman_tree[i & 1023] & 31) as base.u32
this.write_bits!(
n_bits: this.cl_depth[ix] as base.u32,
bits: this.cl_bits[ix] as base.u64,
s: args.s)
// Extra bits.
if ix == 16 { // BROTLI_REPEAT_PREVIOUS_CODE_LENGTH.
this.write_bits!(n_bits: 2, bits: this.huffman_tree_extra_bits[i & 1023] as base.u64, s: args.s)
} else if ix == 17 { // BROTLI_REPEAT_ZERO_CODE_LENGTH.
this.write_bits!(n_bits: 3, bits: this.huffman_tree_extra_bits[i & 1023] as base.u64, s: args.s)
}
i ~mod+= 1
}
}
// Mirrors BrotliStoreHuffmanTree in brotli_bit_stream.c.
pri func encoder.store_huffman_tree!(depths: slice base.u8, num: base.u32, s: slice base.u8) {
var i : base.u32
var num_codes : base.u32
var code : base.u32
this.huffman_tree_size = 0
this.write_huffman_tree!(depth: args.depths, length: args.num)
// Calculate the statistics of the Huffman tree in brotli-representation.
i = 0
while i < 32 {
this.cl_histo[i] = 0
this.cl_depth[i] = 0
i ~mod+= 1
}
i = 0
while i < this.huffman_tree_size {
this.cl_histo[this.huffman_tree[i & 1023] & 31] ~mod+= 1
i ~mod+= 1
}
i = 0
while i < 18 {
if this.cl_histo[i] <> 0 {
if num_codes == 0 {
code = i
num_codes = 1
} else if num_codes == 1 {
num_codes = 2
break
}
}
i ~mod+= 1
}
// Calculate another Huffman tree to use for compressing both the earlier
// Huffman tree with.
this.create_huffman_tree!(src: 1, off: 0, length: 18, tree_limit: 5, depth: this.cl_depth[..])
this.convert_bit_depths_to_symbols!(depth: this.cl_depth[..], len: 18, dst: 2)
// Now, we have all the data, let's start storing it.
this.store_huffman_tree_of_huffman_tree_to_bit_mask!(num_codes: num_codes, s: args.s)
if num_codes == 1 {
this.cl_depth[code & 31] = 0
}
// Store the real Huffman tree now.
this.store_huffman_tree_to_bit_mask!(s: args.s)
}
// Mirrors BrotliBuildAndStoreHuffmanTreeFast in brotli_bit_stream.c, with
// this.histogram, this.lit_depth and this.lit_bits as the histogram, depth and
// bits arguments.
pri func encoder.build_and_store_huffman_tree_fast!(histogram_total: base.u64, max_bits: base.u32, s: slice base.u8) {
var count : base.u32
var symbols : array[4] base.u32
var length : base.u32
var total : base.u64
var count_limit : base.u32
var n : base.u32
var l : base.u32
var h : base.u32
var i : base.u32
var j : base.u32
var tmp : base.u32
var depth_ok : base.bool
var value : base.u32
var prev_value : base.u32
var reps : base.u32
var k : base.u32
total = args.histogram_total
while (total <> 0) and (length < 256) {
h = this.histogram[length]
if h <> 0 {
if count < 4 {
symbols[count] = length
}
count ~mod+= 1
total ~mod-= h as base.u64
}
length ~mod+= 1
}
if count <= 1 {
this.write_bits!(n_bits: 4, bits: 1, s: args.s)
this.write_bits!(n_bits: args.max_bits, bits: symbols[0] as base.u64, s: args.s)
this.lit_depth[symbols[0] & 255] = 0
this.lit_bits[symbols[0] & 255] = 0
return nothing
}
i = 0
while i < length {
this.lit_depth[i & 255] = 0
i ~mod+= 1
}
count_limit = 1
while true {
n = 0
l = length
while l > 0 {
l -= 1
h = this.histogram[l & 255]
if h <> 0 {
if h >= count_limit {
this.tree_count[n & 1023] = h
} else {
this.tree_count[n & 1023] = count_limit
}
this.tree_left[n & 1023] = TREE_NONE
this.tree_right[n & 1023] = (l & 0xFFFF) as base.u16
n ~mod+= 1
}
}
this.sort_huffman_tree_items!(n: n, tie_break: false)
this.merge_huffman_nodes!(n: n)
depth_ok = this.set_depth!(p0: (n ~mod* 2) ~mod- 1, depth: this.lit_depth[..], max_depth: 14)
if depth_ok {
break
}
count_limit ~mod*= 2
}
this.convert_bit_depths_to_symbols!(depth: this.lit_depth[..], len: length, dst: 3)
if count <= 4 {
// Value of 1 indicates a simple Huffman code.
this.write_bits!(n_bits: 2, bits: 1, s: args.s)
this.write_bits!(n_bits: 2, bits: (count ~mod- 1) as base.u64, s: args.s) // NSYM - 1.
// Sort.
i = 0
while i < count {
j = i ~mod+ 1
while j < count {
if this.lit_depth[symbols[j & 3] & 255] < this.lit_depth[symbols[i & 3] & 255] {
tmp = symbols[j & 3]
symbols[j & 3] = symbols[i & 3]
symbols[i & 3] = tmp
}
j ~mod+= 1
}
i ~mod+= 1
}
this.write_bits!(n_bits: args.max_bits, bits: symbols[0] as base.u64, s: args.s)
this.write_bits!(n_bits: args.max_bits, bits: symbols[1] as base.u64, s: args.s)
if count >= 3 {
this.write_bits!(n_bits: args.max_bits, bits: symbols[2] as base.u64, s: args.s)
}
if count >= 4 {
this.write_bits!(n_bits: args.max_bits, bits: symbols[3] as base.u64, s: args.s)
// Tree-select.
if this.lit_depth[symbols[0] & 255] == 1 {
this.write_bits!(n_bits: 1, bits: 1, s: args.s)
} else {
this.write_bits!(n_bits: 1, bits: 0, s: args.s)
}
}
} else {
// Complex Huffman Tree.
this.write_bits!(n_bits: 40, bits: STATIC_CODE_LENGTH_CODE, s: args.s)
// Actual RLE coding.
prev_value = 8
i = 0
while i < length {
value = this.lit_depth[i & 255] as base.u32
reps = 1
k = i ~mod+ 1
while k < length {
if (this.lit_depth[k & 255] as base.u32) <> value {
break
}
reps ~mod+= 1
k ~mod+= 1
}
i ~mod+= reps
if value == 0 {
this.write_bits!(n_bits: ZERO_REPS_DEPTH[reps & 511], bits: ZERO_REPS_BITS[reps & 511], s: args.s)
} else {
if prev_value <> value {
this.write_bits!(n_bits: CODE_LENGTH_DEPTH[value & 15], bits: CODE_LENGTH_BITS[value & 15], s: args.s)
reps ~mod-= 1
}
if reps < 3 {
while reps <> 0 {
reps -= 1
this.write_bits!(n_bits: CODE_LENGTH_DEPTH[value & 15], bits: CODE_LENGTH_BITS[value & 15], s: args.s)
}
} else {
reps -= 3
this.write_bits!(n_bits: NON_ZERO_REPS_DEPTH[reps & 511], bits: NON_ZERO_REPS_BITS[reps & 511], s: args.s)
}
prev_value = value
}
}
}
}
// copy_tmp_bits_to_cmd_bits copies this.tmp_bits[args.src .. args.src + args.n]
// to this.cmd_bits[args.dst .. args.dst + args.n]. Wuffs' C code generator
// mishandles copy_from_slice on base.u16 arrays, so this is an explicit loop.
pri func encoder.copy_tmp_bits_to_cmd_bits!(dst: base.u32, src: base.u32, n: base.u32) {
var i : base.u32
i = 0
while i < args.n {
this.cmd_bits[(args.dst ~mod+ i) & 127] = this.tmp_bits[(args.src ~mod+ i) & 63]
i ~mod+= 1
}
}
+716
View File
@@ -0,0 +1,716 @@
// 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 mirrors compress_fragment.c, the quality 0 (one-pass) encoder.
//
// The C code's input pointers (input, ip, next_emit, candidate, etc.) are
// base.u64 offsets from the start of the fragment (the C code's base_ip).
// The hash table holds the same offsets. The C code's last_distance is -1 or
// positive. Here, 0 means -1.
// FRAGMENT_MAX_DISTANCE is the C code's MAX_DISTANCE,
// BROTLI_MAX_BACKWARD_LIMIT(18).
pri const FRAGMENT_MAX_DISTANCE : base.u64 = 262128
// HASH_MUL32 is kHashMul32 in hash_base.h.
pri const HASH_MUL32 : base.u64 = 0x1E35_A7BD
// Mirrors Hash in compress_fragment.c.
pri func encoder.hash_fast(input: roslice base.u8, i: base.u64, shift: base.u32) base.u32 {
var h : base.u64
h = (this.load64le(s: args.input, i: args.i) ~mod<< 24) ~mod* HASH_MUL32
return ((h >> (args.shift & 63)) & 0xFFFF_FFFF) as base.u32
}
// Mirrors HashBytesAtOffset in compress_fragment.c.
pri func encoder.hash_bytes_at_offset_fast(v: base.u64, offset: base.u32, shift: base.u32) base.u32 {
var h : base.u64
h = ((args.v >> ((8 ~mod* args.offset) & 63)) ~mod<< 24) ~mod* HASH_MUL32
return ((h >> (args.shift & 63)) & 0xFFFF_FFFF) as base.u32
}
// Mirrors IsMatch in compress_fragment.c.
pri func encoder.is_match_fast(input: roslice base.u8, i1: base.u64, i2: base.u64) base.bool {
if (args.i1 >= args.input.length()) or (args.i2 >= args.input.length()) {
return false
}
if this.load32le(s: args.input, i: args.i1) <> this.load32le(s: args.input, i: args.i2) {
return false
}
if ((args.i1 ~mod+ 4) >= args.input.length()) or ((args.i2 ~mod+ 4) >= args.input.length()) {
return false
}
return args.input[args.i1 ~mod+ 4] == args.input[args.i2 ~mod+ 4]
}
// Mirrors BuildAndStoreLiteralPrefixCode in compress_fragment.c, for the
// input_size bytes at args.input[args.from ..].
pri func encoder.build_and_store_literal_prefix_code!(input: roslice base.u8, from: base.u64, input_size: base.u64, s: slice base.u8) base.u64 {
var histogram_total : base.u64
var i : base.u64
var j : base.u32
var adjust : base.u32
var literal_ratio : base.u64
var h : base.u32
j = 0
while j < 256 {
this.histogram[j] = 0
j ~mod+= 1
}
if args.input_size < 0x8000 {
i = 0
while i < args.input_size {
if (args.from ~mod+ i) < args.input.length() {
this.histogram[args.input[args.from ~mod+ i]] ~mod+= 1
}
i ~mod+= 1
}
histogram_total = args.input_size
j = 0
while j < 256 {
// We weigh the first 11 samples with weight 3 to account for the
// balancing effect of the LZ77 phase on the histogram.
adjust = 2 * this.histogram[j].min(no_more_than: 11)
this.histogram[j] ~mod+= adjust
histogram_total ~mod+= adjust as base.u64
j ~mod+= 1
}
} else {
i = 0
while i < args.input_size {
if (args.from ~mod+ i) < args.input.length() {
this.histogram[args.input[args.from ~mod+ i]] ~mod+= 1
}
i ~mod+= 29
}
histogram_total = (args.input_size ~mod+ 28) / 29
j = 0
while j < 256 {
// We add 1 to each population count to avoid 0 bit depths (since
// this is only a sample and we don't know if the symbol appears
// or not), and we weigh the first 11 samples with weight 3 to
// account for the balancing effect of the LZ77 phase on the
// histogram (more frequent symbols are more likely to be in
// backward references instead as literals).
adjust = 1 + (2 * this.histogram[j].min(no_more_than: 11))
this.histogram[j] ~mod+= adjust
histogram_total ~mod+= adjust as base.u64
j ~mod+= 1
}
}
this.build_and_store_huffman_tree_fast!(histogram_total: histogram_total, max_bits: 8, s: args.s)
j = 0
while j < 256 {
h = this.histogram[j]
if h <> 0 {
literal_ratio ~mod+= ((h ~mod* (this.lit_depth[j] as base.u32)) as base.u64)
}
j ~mod+= 1
}
// Estimated encoding ratio, millibytes per symbol.
if histogram_total == 0 {
return 0
}
return (literal_ratio ~mod* 125) / histogram_total
}
// Mirrors BuildAndStoreCommandPrefixCode in compress_fragment.c.
pri func encoder.build_and_store_command_prefix_code_fast!(s: slice base.u8) {
var i : base.u32
this.tmp_depth[.. 704].bulk_memset!(byte_value: 0)
this.create_huffman_tree!(src: 0, off: 0, length: 64, tree_limit: 15, depth: this.cmd_depth[.. 64])
this.create_huffman_tree!(src: 0, off: 64, length: 64, tree_limit: 14, depth: this.cmd_depth[64 ..])
// We have to jump through a few hoops here in order to compute the
// command bits because the symbols are in a different order than in the
// full alphabet. This looks complicated, but having the symbols in this
// order in the command bits saves a few branches in the Emit* functions.
this.tmp_depth[0 .. 24].copy_from_slice!(s: this.cmd_depth[0 .. 24])
this.tmp_depth[24 .. 32].copy_from_slice!(s: this.cmd_depth[40 .. 48])
this.tmp_depth[32 .. 40].copy_from_slice!(s: this.cmd_depth[24 .. 32])
this.tmp_depth[40 .. 48].copy_from_slice!(s: this.cmd_depth[48 .. 56])
this.tmp_depth[48 .. 56].copy_from_slice!(s: this.cmd_depth[32 .. 40])
this.tmp_depth[56 .. 64].copy_from_slice!(s: this.cmd_depth[56 .. 64])
this.convert_bit_depths_to_symbols!(depth: this.tmp_depth[..], len: 64, dst: 0)
this.copy_tmp_bits_to_cmd_bits!(dst: 0, src: 0, n: 24)
this.copy_tmp_bits_to_cmd_bits!(dst: 24, src: 32, n: 8)
this.copy_tmp_bits_to_cmd_bits!(dst: 32, src: 48, n: 8)
this.copy_tmp_bits_to_cmd_bits!(dst: 40, src: 24, n: 8)
this.copy_tmp_bits_to_cmd_bits!(dst: 48, src: 40, n: 8)
this.copy_tmp_bits_to_cmd_bits!(dst: 56, src: 56, n: 8)
this.convert_bit_depths_to_symbols!(depth: this.cmd_depth[64 ..], len: 64, dst: 1)
// Create the bit length array for the full command alphabet.
this.tmp_depth[.. 64].bulk_memset!(byte_value: 0)
this.tmp_depth[0 .. 8].copy_from_slice!(s: this.cmd_depth[0 .. 8])
this.tmp_depth[64 .. 72].copy_from_slice!(s: this.cmd_depth[8 .. 16])
this.tmp_depth[128 .. 136].copy_from_slice!(s: this.cmd_depth[16 .. 24])
this.tmp_depth[192 .. 200].copy_from_slice!(s: this.cmd_depth[24 .. 32])
this.tmp_depth[384 .. 392].copy_from_slice!(s: this.cmd_depth[32 .. 40])
i = 0
while i < 8 {
this.tmp_depth[128 + (8 * i)] = this.cmd_depth[40 + i]
this.tmp_depth[256 + (8 * i)] = this.cmd_depth[48 + i]
this.tmp_depth[448 + (8 * i)] = this.cmd_depth[56 + i]
i ~mod+= 1
}
this.store_huffman_tree!(depths: this.tmp_depth[..], num: 704, s: args.s)
this.store_huffman_tree!(depths: this.cmd_depth[64 ..], num: 64, s: args.s)
}
// emit_cmd_fast writes the command (or distance) prefix code symbol and
// updates its histogram count. It is the C code's "BrotliWriteBits(depth[code],
// bits[code], storage_ix, storage); ++histo[code];".
pri func encoder.emit_cmd_fast!(code: base.u64, s: slice base.u8) {
this.write_bits!(
n_bits: this.cmd_depth[args.code & 127] as base.u32,
bits: this.cmd_bits[args.code & 127] as base.u64,
s: args.s)
this.cmd_histo[args.code & 127] ~mod+= 1
}
// Mirrors EmitInsertLen in compress_fragment.c.
pri func encoder.emit_insert_len_fast!(insertlen: base.u64, s: slice base.u8) {
var tail : base.u64
var nbits : base.u32
var prefix : base.u64
if args.insertlen < 6 {
this.emit_cmd_fast!(code: args.insertlen + 40, s: args.s)
} else if args.insertlen < 130 {
tail = args.insertlen - 2
nbits = this.log2_floor_non_zero(n: tail) ~mod- 1
prefix = tail >> (nbits & 63)
this.emit_cmd_fast!(code: (((nbits as base.u64) ~mod<< 1) ~mod+ prefix) ~mod+ 42, s: args.s)
this.write_bits!(n_bits: nbits, bits: tail ~mod- (prefix ~mod<< (nbits & 63)), s: args.s)
} else if args.insertlen < 2114 {
tail = args.insertlen - 66
nbits = this.log2_floor_non_zero(n: tail)
this.emit_cmd_fast!(code: (nbits as base.u64) + 50, s: args.s)
this.write_bits!(n_bits: nbits, bits: tail ~mod- ((1 as base.u64) << nbits), s: args.s)
} else {
this.emit_cmd_fast!(code: 61, s: args.s)
this.write_bits!(n_bits: 12, bits: args.insertlen ~mod- 2114, s: args.s)
}
}
// Mirrors EmitLongInsertLen in compress_fragment.c.
pri func encoder.emit_long_insert_len_fast!(insertlen: base.u64, s: slice base.u8) {
if args.insertlen < 22594 {
this.emit_cmd_fast!(code: 62, s: args.s)
this.write_bits!(n_bits: 14, bits: args.insertlen ~mod- 6210, s: args.s)
} else {
this.emit_cmd_fast!(code: 63, s: args.s)
this.write_bits!(n_bits: 24, bits: args.insertlen ~mod- 22594, s: args.s)
}
}
// Mirrors EmitCopyLen in compress_fragment.c.
pri func encoder.emit_copy_len_fast!(copylen: base.u64, s: slice base.u8) {
var tail : base.u64
var nbits : base.u32
var prefix : base.u64
if args.copylen < 10 {
this.emit_cmd_fast!(code: args.copylen + 14, s: args.s)
} else if args.copylen < 134 {
tail = args.copylen - 6
nbits = this.log2_floor_non_zero(n: tail) ~mod- 1
prefix = tail >> (nbits & 63)
this.emit_cmd_fast!(code: (((nbits as base.u64) ~mod<< 1) ~mod+ prefix) ~mod+ 20, s: args.s)
this.write_bits!(n_bits: nbits, bits: tail ~mod- (prefix ~mod<< (nbits & 63)), s: args.s)
} else if args.copylen < 2118 {
tail = args.copylen - 70
nbits = this.log2_floor_non_zero(n: tail)
this.emit_cmd_fast!(code: (nbits as base.u64) + 28, s: args.s)
this.write_bits!(n_bits: nbits, bits: tail ~mod- ((1 as base.u64) << nbits), s: args.s)
} else {
this.emit_cmd_fast!(code: 39, s: args.s)
this.write_bits!(n_bits: 24, bits: args.copylen ~mod- 2118, s: args.s)
}
}
// Mirrors EmitCopyLenLastDistance in compress_fragment.c.
pri func encoder.emit_copy_len_last_distance_fast!(copylen: base.u64, s: slice base.u8) {
var tail : base.u64
var nbits : base.u32
var prefix : base.u64
if args.copylen < 12 {
this.emit_cmd_fast!(code: args.copylen ~mod- 4, s: args.s)
} else if args.copylen < 72 {
tail = args.copylen - 8
nbits = this.log2_floor_non_zero(n: tail) ~mod- 1
prefix = tail >> (nbits & 63)
this.emit_cmd_fast!(code: (((nbits as base.u64) ~mod<< 1) ~mod+ prefix) ~mod+ 4, s: args.s)
this.write_bits!(n_bits: nbits, bits: tail ~mod- (prefix ~mod<< (nbits & 63)), s: args.s)
} else if args.copylen < 136 {
tail = args.copylen - 8
this.emit_cmd_fast!(code: (tail >> 5) + 30, s: args.s)
this.write_bits!(n_bits: 5, bits: tail & 31, s: args.s)
this.emit_cmd_fast!(code: 64, s: args.s)
} else if args.copylen < 2120 {
tail = args.copylen - 72
nbits = this.log2_floor_non_zero(n: tail)
this.emit_cmd_fast!(code: (nbits as base.u64) + 28, s: args.s)
this.write_bits!(n_bits: nbits, bits: tail ~mod- ((1 as base.u64) << nbits), s: args.s)
this.emit_cmd_fast!(code: 64, s: args.s)
} else {
this.emit_cmd_fast!(code: 39, s: args.s)
this.write_bits!(n_bits: 24, bits: args.copylen ~mod- 2120, s: args.s)
this.emit_cmd_fast!(code: 64, s: args.s)
}
}
// Mirrors EmitDistance in compress_fragment.c.
pri func encoder.emit_distance_fast!(distance: base.u64, s: slice base.u8) {
var d : base.u64
var nbits : base.u32
var prefix : base.u64
var offset : base.u64
d = args.distance ~mod+ 3
nbits = this.log2_floor_non_zero(n: d) ~mod- 1
prefix = (d >> (nbits & 63)) & 1
offset = (2 + prefix) ~mod<< (nbits & 63)
this.emit_cmd_fast!(code: (((2 * ((nbits ~mod- 1) as base.u64)) ~mod+ prefix) ~mod+ 80), s: args.s)
this.write_bits!(n_bits: nbits, bits: d ~mod- offset, s: args.s)
}
// Mirrors EmitLiterals in compress_fragment.c.
pri func encoder.emit_literals!(input: roslice base.u8, from: base.u64, len: base.u64, s: slice base.u8) {
var j : base.u64
var lit : base.u8
j = 0
while j < args.len {
if (args.from ~mod+ j) >= args.input.length() {
break
}
lit = args.input[args.from ~mod+ j]
this.write_bits!(n_bits: this.lit_depth[lit] as base.u32, bits: this.lit_bits[lit] as base.u64, s: args.s)
j ~mod+= 1
}
}
// Mirrors ShouldMergeBlock in compress_fragment.c, for the len bytes at
// args.input[args.from ..].
pri func encoder.should_merge_block!(input: roslice base.u8, from: base.u64, len: base.u64) base.bool {
var i : base.u64
var j : base.u32
var total : base.u64
var r : base.u64
var h : base.u64
j = 0
while j < 256 {
this.histogram[j] = 0
j ~mod+= 1
}
i = 0
while i < args.len {
if (args.from ~mod+ i) < args.input.length() {
this.histogram[args.input[args.from ~mod+ i]] ~mod+= 1
}
i ~mod+= 43
}
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)),
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(
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.fast_log2(v: h))))
j ~mod+= 1
}
return this.sf.f64_ge(a: r, b: 0) <> 0
}
// Mirrors ShouldUseUncompressedMode in compress_fragment.c.
pri func encoder.should_use_uncompressed_mode(metablock_start: base.u64, next_emit: base.u64, insertlen: base.u64, literal_ratio: base.u64) base.bool {
var compressed : base.u64
compressed = args.next_emit ~mod- args.metablock_start
if (compressed ~mod* 50) > args.insertlen {
return false
}
// MIN_RATIO is 980.
return args.literal_ratio > 980
}
// Mirrors EmitUncompressedMetaBlock in compress_fragment.c.
pri func encoder.emit_uncompressed_meta_block_fast!(input: roslice base.u8, begin: base.u64, end: base.u64, storage_ix_start: base.u64, s: slice base.u8) {
var len : base.u64
var p : base.u64
len = args.end ~mod- args.begin
this.rewind_bit_position!(new_ix: args.storage_ix_start, s: args.s)
this.store_meta_block_header!(len: len, is_uncompressed: true, s: args.s)
this.align_to_byte!()
this.copy_bytes!(input: args.input, begin: args.begin, end: args.end, s: args.s)
this.bw_ix ~mod+= len ~mod<< 3
p = this.bw_ix >> 3
if p < args.s.length() {
args.s[p] = 0
}
}
// copy_bytes is the C code's "memcpy(&storage[*storage_ix >> 3], begin,
// len)", where bw_ix is byte-aligned.
pri func encoder.copy_bytes!(input: roslice base.u8, begin: base.u64, end: base.u64, s: slice base.u8) {
var p : base.u64
p = this.bw_ix >> 3
if (args.begin <= args.end) and (args.end <= args.input.length()) and (p <= args.s.length()) {
args.s[p ..].copy_from_slice!(s: args.input[args.begin .. args.end])
}
}
// update_table_fast updates the hash table with the hashes of some positions
// within the last copy (ending at ip), and returns the next candidate. It is
// the C code's "We could immediately start working at ip now, but to improve
// compression we first update table with the hashes..." block.
pri func encoder.update_table_fast!(input: roslice base.u8, ip: base.u64, shift: base.u32) base.u64 {
var input_bytes : base.u64
var prev_hash : base.u32
var cur_hash : base.u32
var candidate : base.u64
input_bytes = this.load64le(s: args.input, i: args.ip ~mod- 3)
prev_hash = this.hash_bytes_at_offset_fast(v: input_bytes, offset: 0, shift: args.shift)
cur_hash = this.hash_bytes_at_offset_fast(v: input_bytes, offset: 3, shift: args.shift)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 3) & 0xFFFF_FFFF) as base.u32
prev_hash = this.hash_bytes_at_offset_fast(v: input_bytes, offset: 1, shift: args.shift)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 2) & 0xFFFF_FFFF) as base.u32
prev_hash = this.hash_bytes_at_offset_fast(v: input_bytes, offset: 2, shift: args.shift)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 1) & 0xFFFF_FFFF) as base.u32
candidate = this.hash_table[cur_hash & 0x1_FFFF] as base.u64
this.hash_table[cur_hash & 0x1_FFFF] = (args.ip & 0xFFFF_FFFF) as base.u32
return candidate
}
// Mirrors BrotliCompressFragmentFast in compress_fragment.c.
pri func encoder.compress_fragment_fast!(input: roslice base.u8, is_last: base.bool, table_bits: base.u32, s: slice base.u8) {
var initial_ix : base.u64
var input_size : base.u64
initial_ix = this.bw_ix
input_size = args.input.length()
if input_size == 0 {
this.write_bits!(n_bits: 1, bits: 1, s: args.s) // islast
this.write_bits!(n_bits: 1, bits: 1, s: args.s) // isempty
this.align_to_byte!()
return nothing
}
this.compress_fragment_fast_impl!(input: args.input, is_last: args.is_last, table_bits: args.table_bits, s: args.s)
// If output is larger than single uncompressed block, rewrite it.
if (this.bw_ix ~mod- initial_ix) > (31 ~mod+ (input_size ~mod<< 3)) {
this.emit_uncompressed_meta_block_fast!(
input: args.input, begin: 0, end: input_size, storage_ix_start: initial_ix, s: args.s)
}
if args.is_last {
this.write_bits!(n_bits: 1, bits: 1, s: args.s) // islast
this.write_bits!(n_bits: 1, bits: 1, s: args.s) // isempty
this.align_to_byte!()
}
}
// Mirrors BrotliCompressFragmentFastImpl in compress_fragment.c. The C code's
// gotos (to emit_commands, trawl, emit_remainder and next_block) are labeled
// loops and the goto_next_block bool.
pri func encoder.compress_fragment_fast_impl!(input: roslice base.u8, is_last: base.bool, table_bits: base.u32, s: slice base.u8) {
var in_off : base.u64
var in_size : base.u64
var next_emit : base.u64
var metablock_start : base.u64
var block_size : base.u64
var total_block_size : base.u64
var mlen_storage_ix : base.u64
var literal_ratio : base.u64
var shift : base.u32
var i : base.u64
var ip : base.u64
var ip_end : base.u64
var ip_limit : base.u64
var len_limit : base.u64
var next_ip : base.u64
var candidate : base.u64
var base_ip : base.u64
var matched : base.u64
var distance : base.u64
var insert : base.u64
var last_distance : base.u64
var skip : base.u32
var hash : base.u32
var next_hash : base.u32
var goto_next_block : base.bool
var merge : base.bool
var saved_ix : base.u64
shift = 64 ~mod- args.table_bits
in_size = args.input.length()
block_size = in_size.min(no_more_than: 98304) // kFirstBlockSize is (3 << 15).
total_block_size = block_size
// Save the bit position of the MLEN field of the meta-block header, so
// that we can update it later if we decide to extend this meta-block.
mlen_storage_ix = this.bw_ix ~mod+ 3
this.store_meta_block_header!(len: block_size, is_uncompressed: false, s: args.s)
// No block splits, no contexts.
this.write_bits!(n_bits: 13, bits: 0, s: args.s)
literal_ratio = this.build_and_store_literal_prefix_code!(
input: args.input, from: in_off, input_size: block_size, s: args.s)
// Store the pre-compressed command and distance prefix codes.
i = 0
while (i ~mod+ 7) < this.cmd_code_numbits {
this.write_bits!(n_bits: 8, bits: this.cmd_code[(i >> 3) & 511] as base.u64, s: args.s)
i ~mod+= 8
}
this.write_bits!(
n_bits: (this.cmd_code_numbits & 7) as base.u32,
bits: this.cmd_code[(this.cmd_code_numbits >> 3) & 511] as base.u64,
s: args.s)
while.emit_commands true {
// Initialize the command and distance histograms. We will gather
// statistics of command and distance codes during the processing of
// this block and use it to update the command and distance prefix
// codes for the next block.
i = 0
while i < 128 {
this.cmd_histo[i] = CMD_HISTO_SEED[i]
i ~mod+= 1
}
ip = in_off
last_distance = 0
ip_end = in_off ~mod+ block_size
goto_next_block = false
if block_size >= 16 {
// For the last block, we need to keep a 16 bytes margin so that
// we can be sure that all distances are at most window size - 16.
// For all other blocks, we only need to keep a margin of 5 bytes
// so that we don't go over the block size with a copy.
len_limit = block_size ~mod- 5
len_limit = len_limit.min(no_more_than: in_size ~mod- 16)
ip_limit = in_off ~mod+ len_limit
ip ~mod+= 1
next_hash = this.hash_fast(input: args.input, i: ip, shift: shift)
while.outer true {
// Step 1: Scan forward in the input looking for a 5-byte-long
// match. If we get close to exhausting the input then goto
// emit_remainder.
skip = 32
next_ip = ip
while.trawl true {
hash = next_hash
ip = next_ip
next_ip = ip ~mod+ ((skip >> 5) as base.u64)
skip ~mod+= 1
if next_ip > ip_limit {
break.outer
}
next_hash = this.hash_fast(input: args.input, i: next_ip, shift: shift)
if (last_distance > 0) and (last_distance <= ip) {
candidate = ip ~mod- last_distance
if this.is_match_fast(input: args.input, i1: ip, i2: candidate) {
this.hash_table[hash & 0x1_FFFF] = (ip & 0xFFFF_FFFF) as base.u32
// Check copy distance. If candidate is not
// feasible, continue search.
if (ip ~mod- candidate) > FRAGMENT_MAX_DISTANCE {
continue.trawl
}
break.trawl
}
}
candidate = this.hash_table[hash & 0x1_FFFF] as base.u64
this.hash_table[hash & 0x1_FFFF] = (ip & 0xFFFF_FFFF) as base.u32
if this.is_match_fast(input: args.input, i1: ip, i2: candidate) {
if (ip ~mod- candidate) > FRAGMENT_MAX_DISTANCE {
continue.trawl
}
break.trawl
}
}.trawl
// Step 2: Emit the found match together with the literal bytes
// from "next_emit" to the bit stream, and then see if we can
// find a next match immediately afterwards. Repeat until we
// find no match for the input without emitting some literal
// bytes.
base_ip = ip
matched = 5 ~mod+ this.find_match_length_with_limit(
s: args.input, i1: candidate ~mod+ 5, i2: ip ~mod+ 5, limit: (ip_end ~mod- ip) ~mod- 5)
distance = base_ip ~mod- candidate
insert = base_ip ~mod- next_emit
ip ~mod+= matched
if insert < 6210 {
this.emit_insert_len_fast!(insertlen: insert, s: args.s)
} else if this.should_use_uncompressed_mode(
metablock_start: metablock_start, next_emit: next_emit,
insertlen: insert, literal_ratio: literal_ratio) {
this.emit_uncompressed_meta_block_fast!(
input: args.input, begin: metablock_start, end: base_ip,
storage_ix_start: mlen_storage_ix ~mod- 3, s: args.s)
in_size ~mod-= base_ip ~mod- in_off
in_off = base_ip
next_emit = in_off
goto_next_block = true
break.outer
} else {
this.emit_long_insert_len_fast!(insertlen: insert, s: args.s)
}
this.emit_literals!(input: args.input, from: next_emit, len: insert, s: args.s)
if distance == last_distance {
this.emit_cmd_fast!(code: 64, s: args.s)
} else {
this.emit_distance_fast!(distance: distance, s: args.s)
last_distance = distance
}
this.emit_copy_len_last_distance_fast!(copylen: matched, s: args.s)
next_emit = ip
if ip >= ip_limit {
break.outer
}
candidate = this.update_table_fast!(input: args.input, ip: ip, shift: shift)
while.inner this.is_match_fast(input: args.input, i1: ip, i2: candidate) {
// We have a 5-byte match at ip, and no need to emit any
// literal bytes prior to ip.
base_ip = ip
matched = 5 ~mod+ this.find_match_length_with_limit(
s: args.input, i1: candidate ~mod+ 5, i2: ip ~mod+ 5, limit: (ip_end ~mod- ip) ~mod- 5)
if (ip ~mod- candidate) > FRAGMENT_MAX_DISTANCE {
break.inner
}
ip ~mod+= matched
last_distance = base_ip ~mod- candidate
this.emit_copy_len_fast!(copylen: matched, s: args.s)
this.emit_distance_fast!(distance: last_distance, s: args.s)
next_emit = ip
if ip >= ip_limit {
break.outer
}
candidate = this.update_table_fast!(input: args.input, ip: ip, shift: shift)
}.inner
ip ~mod+= 1
next_hash = this.hash_fast(input: args.input, i: ip, shift: shift)
}.outer
}
if not goto_next_block {
// emit_remainder:
in_off ~mod+= block_size
in_size ~mod-= block_size
block_size = in_size.min(no_more_than: 0x1_0000) // kMergeBlockSize.
// Decide if we want to continue this meta-block instead of
// emitting the last insert-only command.
merge = false
if (in_size > 0) and ((total_block_size ~mod+ block_size) <= 0x10_0000) {
merge = this.should_merge_block!(input: args.input, from: in_off, len: block_size)
}
if merge {
// Update the size of the current meta-block and continue
// emitting commands. We can do this because the current size
// and the new size both have 5 nibbles.
total_block_size ~mod+= block_size
this.update_bits!(
n_bits: 20,
bits: ((total_block_size ~mod- 1) & 0xFFFF_FFFF) as base.u32,
pos: mlen_storage_ix,
s: args.s)
continue.emit_commands
}
// Emit the remaining bytes as literals.
if next_emit < ip_end {
insert = ip_end ~mod- next_emit
if insert < 6210 {
this.emit_insert_len_fast!(insertlen: insert, s: args.s)
this.emit_literals!(input: args.input, from: next_emit, len: insert, s: args.s)
} else if this.should_use_uncompressed_mode(
metablock_start: metablock_start, next_emit: next_emit,
insertlen: insert, literal_ratio: literal_ratio) {
this.emit_uncompressed_meta_block_fast!(
input: args.input, begin: metablock_start, end: ip_end,
storage_ix_start: mlen_storage_ix ~mod- 3, s: args.s)
} else {
this.emit_long_insert_len_fast!(insertlen: insert, s: args.s)
this.emit_literals!(input: args.input, from: next_emit, len: insert, s: args.s)
}
}
next_emit = ip_end
}
// next_block:
// If we have more data, write a new meta-block header and prefix
// codes and then continue emitting commands.
if in_size > 0 {
metablock_start = in_off
block_size = in_size.min(no_more_than: 98304)
total_block_size = block_size
mlen_storage_ix = this.bw_ix ~mod+ 3
this.store_meta_block_header!(len: block_size, is_uncompressed: false, s: args.s)
// No block splits, no contexts.
this.write_bits!(n_bits: 13, bits: 0, s: args.s)
literal_ratio = this.build_and_store_literal_prefix_code!(
input: args.input, from: in_off, input_size: block_size, s: args.s)
this.build_and_store_command_prefix_code_fast!(s: args.s)
continue.emit_commands
}
break.emit_commands
}.emit_commands
if not args.is_last {
// If this is not the last block, update the command and distance
// prefix codes for the next block and store the compressed forms.
this.cmd_code[0] = 0
saved_ix = this.bw_ix
this.bw_ix = 0
this.build_and_store_command_prefix_code_fast!(s: this.cmd_code[..])
this.cmd_code_numbits = this.bw_ix
this.bw_ix = saved_ix
}
}
+566
View File
@@ -0,0 +1,566 @@
// 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 mirrors compress_fragment_two_pass.c, the quality 1 (two-pass)
// encoder. Like encode_fragment_fast.wuffs, the C code's pointers are
// base.u64 offsets from the start of the fragment (the C code's base_ip) and
// a last_distance of 0 means the C code's -1.
//
// The C code's commands and literals pointers (into command_buf and
// literal_buf) are the num_commands and num_literals fields.
// Mirrors Hash in compress_fragment_two_pass.c.
pri func encoder.hash_two_pass(input: roslice base.u8, i: base.u64, shift: base.u32, length: base.u32) base.u32 {
var h : base.u64
h = (this.load64le(s: args.input, i: args.i) ~mod<< ((64 ~mod- (8 ~mod* args.length)) & 63)) ~mod* HASH_MUL32
return ((h >> (args.shift & 63)) & 0xFFFF_FFFF) as base.u32
}
// Mirrors HashBytesAtOffset in compress_fragment_two_pass.c.
pri func encoder.hash_bytes_at_offset_two_pass(v: base.u64, offset: base.u32, shift: base.u32, length: base.u32) base.u32 {
var h : base.u64
h = ((args.v >> ((8 ~mod* args.offset) & 63)) ~mod<< ((64 ~mod- (8 ~mod* args.length)) & 63)) ~mod* HASH_MUL32
return ((h >> (args.shift & 63)) & 0xFFFF_FFFF) as base.u32
}
// Mirrors IsMatch in compress_fragment_two_pass.c.
pri func encoder.is_match_two_pass(input: roslice base.u8, i1: base.u64, i2: base.u64, length: base.u32) base.bool {
var q1 : roslice base.u8
var q2 : roslice base.u8
if (args.i1 >= args.input.length()) or (args.i2 >= args.input.length()) {
return false
}
if this.load32le(s: args.input, i: args.i1) <> this.load32le(s: args.input, i: args.i2) {
return false
}
if args.length == 4 {
return true
}
q1 = args.input[args.i1 ..]
q2 = args.input[args.i2 ..]
if (q1.length() >= 6) and (q2.length() >= 6) {
return (q1[4] == q2[4]) and (q1[5] == q2[5])
}
return false
}
// Mirrors BuildAndStoreCommandPrefixCode in compress_fragment_two_pass.c.
// Note that its symbol order differs from the one in compress_fragment.c.
pri func encoder.build_and_store_command_prefix_code_two_pass!(s: slice base.u8) {
var i : base.u32
this.tmp_depth[.. 704].bulk_memset!(byte_value: 0)
this.create_huffman_tree!(src: 0, off: 0, length: 64, tree_limit: 15, depth: this.cmd_depth[.. 64])
this.create_huffman_tree!(src: 0, off: 64, length: 64, tree_limit: 14, depth: this.cmd_depth[64 ..])
// We have to jump through a few hoops here in order to compute the
// command bits because the symbols are in a different order than in the
// full alphabet. This looks complicated, but having the symbols in this
// order in the command bits saves a few branches in the Emit* functions.
this.tmp_depth[0 .. 24].copy_from_slice!(s: this.cmd_depth[24 .. 48])
this.tmp_depth[24 .. 32].copy_from_slice!(s: this.cmd_depth[0 .. 8])
this.tmp_depth[32 .. 40].copy_from_slice!(s: this.cmd_depth[48 .. 56])
this.tmp_depth[40 .. 48].copy_from_slice!(s: this.cmd_depth[8 .. 16])
this.tmp_depth[48 .. 56].copy_from_slice!(s: this.cmd_depth[56 .. 64])
this.tmp_depth[56 .. 64].copy_from_slice!(s: this.cmd_depth[16 .. 24])
this.convert_bit_depths_to_symbols!(depth: this.tmp_depth[..], len: 64, dst: 0)
this.copy_tmp_bits_to_cmd_bits!(dst: 0, src: 24, n: 8)
this.copy_tmp_bits_to_cmd_bits!(dst: 8, src: 40, n: 8)
this.copy_tmp_bits_to_cmd_bits!(dst: 16, src: 56, n: 8)
this.copy_tmp_bits_to_cmd_bits!(dst: 24, src: 0, n: 24)
this.copy_tmp_bits_to_cmd_bits!(dst: 48, src: 32, n: 8)
this.copy_tmp_bits_to_cmd_bits!(dst: 56, src: 48, n: 8)
this.convert_bit_depths_to_symbols!(depth: this.cmd_depth[64 ..], len: 64, dst: 1)
// Create the bit length array for the full command alphabet.
this.tmp_depth[.. 64].bulk_memset!(byte_value: 0)
this.tmp_depth[0 .. 8].copy_from_slice!(s: this.cmd_depth[24 .. 32])
this.tmp_depth[64 .. 72].copy_from_slice!(s: this.cmd_depth[32 .. 40])
this.tmp_depth[128 .. 136].copy_from_slice!(s: this.cmd_depth[40 .. 48])
this.tmp_depth[192 .. 200].copy_from_slice!(s: this.cmd_depth[48 .. 56])
this.tmp_depth[384 .. 392].copy_from_slice!(s: this.cmd_depth[56 .. 64])
i = 0
while i < 8 {
this.tmp_depth[128 + (8 * i)] = this.cmd_depth[i]
this.tmp_depth[256 + (8 * i)] = this.cmd_depth[8 + i]
this.tmp_depth[448 + (8 * i)] = this.cmd_depth[16 + i]
i ~mod+= 1
}
this.store_huffman_tree!(depths: this.tmp_depth[..], num: 704, s: args.s)
this.store_huffman_tree!(depths: this.cmd_depth[64 ..], num: 64, s: args.s)
}
// push_command appends to command_buf.
pri func encoder.push_command!(c: base.u32) {
this.command_buf[this.num_commands & 0x1_FFFF] = args.c
this.num_commands ~mod+= 1
}
// Mirrors EmitInsertLen in compress_fragment_two_pass.c.
pri func encoder.emit_insert_len_two_pass!(insertlen: base.u32) {
var tail : base.u32
var nbits : base.u32
var prefix : base.u32
var extra : base.u32
if args.insertlen < 6 {
this.push_command!(c: args.insertlen)
} else if args.insertlen < 130 {
tail = args.insertlen - 2
nbits = this.log2_floor_non_zero(n: tail as base.u64) ~mod- 1
prefix = tail >> (nbits & 31)
extra = tail ~mod- (prefix ~mod<< (nbits & 31))
this.push_command!(c: ((((nbits ~mod<< 1) ~mod+ prefix) ~mod+ 2) | (extra ~mod<< 8)))
} else if args.insertlen < 2114 {
tail = args.insertlen - 66
nbits = this.log2_floor_non_zero(n: tail as base.u64)
extra = tail ~mod- ((1 as base.u32) << nbits)
this.push_command!(c: ((nbits + 10) | (extra ~mod<< 8)))
} else if args.insertlen < 6210 {
this.push_command!(c: (21 | ((args.insertlen - 2114) ~mod<< 8)))
} else if args.insertlen < 22594 {
this.push_command!(c: (22 | ((args.insertlen - 6210) ~mod<< 8)))
} else {
this.push_command!(c: (23 | ((args.insertlen - 22594) ~mod<< 8)))
}
}
// Mirrors EmitCopyLen in compress_fragment_two_pass.c.
pri func encoder.emit_copy_len_two_pass!(copylen: base.u64) {
var tail : base.u64
var nbits : base.u32
var prefix : base.u64
var extra : base.u64
if args.copylen < 10 {
this.push_command!(c: ((args.copylen + 38) & 0xFFFF_FFFF) as base.u32)
} else if args.copylen < 134 {
tail = args.copylen - 6
nbits = this.log2_floor_non_zero(n: tail) ~mod- 1
prefix = tail >> (nbits & 63)
extra = tail ~mod- (prefix ~mod<< (nbits & 63))
this.push_command!(c: ((((((nbits as base.u64) ~mod<< 1) ~mod+ prefix) ~mod+ 44) | (extra ~mod<< 8)) & 0xFFFF_FFFF) as base.u32)
} else if args.copylen < 2118 {
tail = args.copylen - 70
nbits = this.log2_floor_non_zero(n: tail)
extra = tail ~mod- ((1 as base.u64) << nbits)
this.push_command!(c: ((((nbits as base.u64) + 52) | (extra ~mod<< 8)) & 0xFFFF_FFFF) as base.u32)
} else {
this.push_command!(c: ((63 | ((args.copylen ~mod- 2118) ~mod<< 8)) & 0xFFFF_FFFF) as base.u32)
}
}
// Mirrors EmitCopyLenLastDistance in compress_fragment_two_pass.c.
pri func encoder.emit_copy_len_last_distance_two_pass!(copylen: base.u64) {
var tail : base.u64
var nbits : base.u32
var prefix : base.u64
var extra : base.u64
if args.copylen < 12 {
this.push_command!(c: ((args.copylen + 20) & 0xFFFF_FFFF) as base.u32)
} else if args.copylen < 72 {
tail = args.copylen - 8
nbits = this.log2_floor_non_zero(n: tail) ~mod- 1
prefix = tail >> (nbits & 63)
extra = tail ~mod- (prefix ~mod<< (nbits & 63))
this.push_command!(c: ((((((nbits as base.u64) ~mod<< 1) ~mod+ prefix) ~mod+ 28) | (extra ~mod<< 8)) & 0xFFFF_FFFF) as base.u32)
} else if args.copylen < 136 {
tail = args.copylen - 8
this.push_command!(c: (((tail >> 5) + 54) | ((tail & 31) << 8)) as base.u32)
this.push_command!(c: 64)
} else if args.copylen < 2120 {
tail = args.copylen - 72
nbits = this.log2_floor_non_zero(n: tail)
extra = tail ~mod- ((1 as base.u64) << nbits)
this.push_command!(c: ((((nbits as base.u64) + 52) | (extra ~mod<< 8)) & 0xFFFF_FFFF) as base.u32)
this.push_command!(c: 64)
} else {
this.push_command!(c: ((63 | ((args.copylen ~mod- 2120) ~mod<< 8)) & 0xFFFF_FFFF) as base.u32)
this.push_command!(c: 64)
}
}
// Mirrors EmitDistance in compress_fragment_two_pass.c.
pri func encoder.emit_distance_two_pass!(distance: base.u32) {
var d : base.u32
var nbits : base.u32
var prefix : base.u32
var offset : base.u32
var distcode : base.u32
var extra : base.u32
d = args.distance ~mod+ 3
nbits = this.log2_floor_non_zero(n: d as base.u64) ~mod- 1
prefix = (d >> (nbits & 31)) & 1
offset = (2 + prefix) ~mod<< (nbits & 31)
distcode = ((2 ~mod* (nbits ~mod- 1)) ~mod+ prefix) ~mod+ 80
extra = d ~mod- offset
this.push_command!(c: distcode | (extra ~mod<< 8))
}
// push_literals appends args.input[args.from .. args.from + args.n] to
// literal_buf, the C code's "memcpy(*literals, next_emit, insert)".
pri func encoder.push_literals!(input: roslice base.u8, from: base.u64, n: base.u64) {
var j : base.u64
j = 0
while j < args.n {
if (args.from ~mod+ j) >= args.input.length() {
break
}
this.literal_buf[this.num_literals & 0x1_FFFF] = args.input[args.from ~mod+ j]
this.num_literals ~mod+= 1
j ~mod+= 1
}
}
// update_table_two_pass updates the hash table with the hashes of some
// positions within the last copy (ending at ip), and returns the next
// candidate. The C code has two copies of this block, which differ (for
// min_match == 4) in the third hash's offset: third_offset is 0 for the first
// copy and 2 for the second copy.
pri func encoder.update_table_two_pass!(input: roslice base.u8, ip: base.u64, shift: base.u32, min_match: base.u32, third_offset: base.u32) base.u64 {
var input_bytes : base.u64
var prev_hash : base.u32
var cur_hash : base.u32
var candidate : base.u64
var mm : base.u32
mm = args.min_match
if mm == 4 {
input_bytes = this.load64le(s: args.input, i: args.ip ~mod- 3)
cur_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 3, shift: args.shift, length: mm)
prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 0, shift: args.shift, length: mm)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 3) & 0xFFFF_FFFF) as base.u32
prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 1, shift: args.shift, length: mm)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 2) & 0xFFFF_FFFF) as base.u32
prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: args.third_offset, shift: args.shift, length: mm)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 1) & 0xFFFF_FFFF) as base.u32
} else {
input_bytes = this.load64le(s: args.input, i: args.ip ~mod- 5)
prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 0, shift: args.shift, length: mm)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 5) & 0xFFFF_FFFF) as base.u32
prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 1, shift: args.shift, length: mm)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 4) & 0xFFFF_FFFF) as base.u32
prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 2, shift: args.shift, length: mm)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 3) & 0xFFFF_FFFF) as base.u32
input_bytes = this.load64le(s: args.input, i: args.ip ~mod- 2)
cur_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 2, shift: args.shift, length: mm)
prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 0, shift: args.shift, length: mm)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 2) & 0xFFFF_FFFF) as base.u32
prev_hash = this.hash_bytes_at_offset_two_pass(v: input_bytes, offset: 1, shift: args.shift, length: mm)
this.hash_table[prev_hash & 0x1_FFFF] = ((args.ip ~mod- 1) & 0xFFFF_FFFF) as base.u32
}
candidate = this.hash_table[cur_hash & 0x1_FFFF] as base.u64
this.hash_table[cur_hash & 0x1_FFFF] = (args.ip & 0xFFFF_FFFF) as base.u32
return candidate
}
// Mirrors CreateCommands in compress_fragment_two_pass.c. The block is the
// block_size bytes at args.input[args.from ..]. input_size is the number of
// bytes remaining in the fragment, from args.from.
pri func encoder.create_commands!(input: roslice base.u8, from: base.u64, block_size: base.u64, input_size: base.u64, table_bits: base.u32, min_match: base.u32) {
var ip : base.u64
var shift : base.u32
var ip_end : base.u64
var next_emit : base.u64
var last_distance : base.u64
var len_limit : base.u64
var ip_limit : base.u64
var next_hash : base.u32
var hash : base.u32
var skip : base.u32
var next_ip : base.u64
var candidate : base.u64
var base_ip : base.u64
var matched : base.u64
var distance : base.u64
var insert : base.u64
var mm : base.u64
this.num_commands = 0
this.num_literals = 0
mm = args.min_match as base.u64
ip = args.from
shift = 64 ~mod- args.table_bits
ip_end = args.from ~mod+ args.block_size
next_emit = args.from
if args.block_size >= 16 {
// For the last block, we need to keep a 16 bytes margin so that we
// can be sure that all distances are at most window size - 16. For
// all other blocks, we only need to keep a margin of 5 bytes so that
// we don't go over the block size with a copy.
len_limit = args.block_size ~mod- mm
len_limit = len_limit.min(no_more_than: args.input_size ~mod- 16)
ip_limit = args.from ~mod+ len_limit
ip ~mod+= 1
next_hash = this.hash_two_pass(input: args.input, i: ip, shift: shift, length: args.min_match)
while.outer true {
// Step 1: Scan forward in the input looking for a 6-byte-long
// match. If we get close to exhausting the input then goto
// emit_remainder.
skip = 32
next_ip = ip
while.trawl true {
hash = next_hash
ip = next_ip
next_ip = ip ~mod+ ((skip >> 5) as base.u64)
skip ~mod+= 1
if next_ip > ip_limit {
break.outer
}
next_hash = this.hash_two_pass(input: args.input, i: next_ip, shift: shift, length: args.min_match)
if (last_distance > 0) and (last_distance <= ip) {
candidate = ip ~mod- last_distance
if this.is_match_two_pass(input: args.input, i1: ip, i2: candidate, length: args.min_match) {
this.hash_table[hash & 0x1_FFFF] = (ip & 0xFFFF_FFFF) as base.u32
// Check copy distance. If candidate is not feasible,
// continue search.
if (ip ~mod- candidate) > FRAGMENT_MAX_DISTANCE {
continue.trawl
}
break.trawl
}
}
candidate = this.hash_table[hash & 0x1_FFFF] as base.u64
this.hash_table[hash & 0x1_FFFF] = (ip & 0xFFFF_FFFF) as base.u32
if this.is_match_two_pass(input: args.input, i1: ip, i2: candidate, length: args.min_match) {
if (ip ~mod- candidate) > FRAGMENT_MAX_DISTANCE {
continue.trawl
}
break.trawl
}
}.trawl
// Step 2: Emit the found match together with the literal bytes
// from "next_emit", and then see if we can find a next match
// immediately afterwards. Repeat until we find no match for the
// input without emitting some literal bytes.
base_ip = ip
matched = mm ~mod+ this.find_match_length_with_limit(
s: args.input, i1: candidate ~mod+ mm, i2: ip ~mod+ mm, limit: (ip_end ~mod- ip) ~mod- mm)
distance = base_ip ~mod- candidate
insert = base_ip ~mod- next_emit
ip ~mod+= matched
this.emit_insert_len_two_pass!(insertlen: (insert & 0xFFFF_FFFF) as base.u32)
this.push_literals!(input: args.input, from: next_emit, n: insert)
if distance == last_distance {
this.push_command!(c: 64)
} else {
this.emit_distance_two_pass!(distance: (distance & 0xFFFF_FFFF) as base.u32)
last_distance = distance
}
this.emit_copy_len_last_distance_two_pass!(copylen: matched)
next_emit = ip
if ip >= ip_limit {
break.outer
}
candidate = this.update_table_two_pass!(
input: args.input, ip: ip, shift: shift, min_match: args.min_match, third_offset: 0)
while (ip ~mod- candidate) <= FRAGMENT_MAX_DISTANCE {
if not this.is_match_two_pass(input: args.input, i1: ip, i2: candidate, length: args.min_match) {
break
}
// We have a 6-byte match at ip, and no need to emit any
// literal bytes prior to ip.
base_ip = ip
matched = mm ~mod+ this.find_match_length_with_limit(
s: args.input, i1: candidate ~mod+ mm, i2: ip ~mod+ mm, limit: (ip_end ~mod- ip) ~mod- mm)
ip ~mod+= matched
last_distance = base_ip ~mod- candidate
this.emit_copy_len_two_pass!(copylen: matched)
this.emit_distance_two_pass!(distance: (last_distance & 0xFFFF_FFFF) as base.u32)
next_emit = ip
if ip >= ip_limit {
break.outer
}
candidate = this.update_table_two_pass!(
input: args.input, ip: ip, shift: shift, min_match: args.min_match, third_offset: 2)
}
ip ~mod+= 1
next_hash = this.hash_two_pass(input: args.input, i: ip, shift: shift, length: args.min_match)
}.outer
}
// emit_remainder:
// Emit the remaining bytes as literals.
if next_emit < ip_end {
insert = ip_end ~mod- next_emit
this.emit_insert_len_two_pass!(insertlen: (insert & 0xFFFF_FFFF) as base.u32)
this.push_literals!(input: args.input, from: next_emit, n: insert)
}
}
// Mirrors StoreCommands in compress_fragment_two_pass.c.
pri func encoder.store_commands!(s: slice base.u8) {
var i : base.u32
var j : base.u32
var cmd : base.u32
var code : base.u32
var extra : base.u32
var insert : base.u32
var lit_i : base.u32
var lit : base.u8
this.histogram[.. 256].bulk_memset!(byte_value: 0)
this.cmd_depth[.. 128].bulk_memset!(byte_value: 0)
this.cmd_bits[.. 128].bulk_memset!(byte_value: 0)
this.cmd_histo[.. 128].bulk_memset!(byte_value: 0)
i = 0
while i < this.num_literals {
this.histogram[this.literal_buf[i & 0x1_FFFF]] ~mod+= 1
i ~mod+= 1
}
this.build_and_store_huffman_tree_fast!(histogram_total: this.num_literals as base.u64, max_bits: 8, s: args.s)
i = 0
while i < this.num_commands {
code = this.command_buf[i & 0x1_FFFF] & 0xFF
this.cmd_histo[code & 127] ~mod+= 1
i ~mod+= 1
}
this.cmd_histo[1] ~mod+= 1
this.cmd_histo[2] ~mod+= 1
this.cmd_histo[64] ~mod+= 1
this.cmd_histo[84] ~mod+= 1
this.build_and_store_command_prefix_code_two_pass!(s: args.s)
i = 0
while i < this.num_commands {
cmd = this.command_buf[i & 0x1_FFFF]
code = cmd & 0x7F
extra = cmd >> 8
this.write_bits!(n_bits: this.cmd_depth[code] as base.u32, bits: this.cmd_bits[code] as base.u64, s: args.s)
this.write_bits!(n_bits: TWO_PASS_NUM_EXTRA_BITS[code], bits: extra as base.u64, s: args.s)
if code < 24 {
insert = TWO_PASS_INSERT_OFFSET[code] ~mod+ extra
j = 0
while j < insert {
lit = this.literal_buf[lit_i & 0x1_FFFF]
this.write_bits!(n_bits: this.lit_depth[lit] as base.u32, bits: this.lit_bits[lit] as base.u64, s: args.s)
lit_i ~mod+= 1
j ~mod+= 1
}
}
i ~mod+= 1
}
}
// Mirrors ShouldCompress in compress_fragment_two_pass.c, for the input_size
// bytes at args.input[args.from ..].
pri func encoder.should_compress_two_pass!(input: roslice base.u8, from: base.u64, input_size: base.u64, num_literals: base.u64) base.bool {
var corpus_size : base.u64
var max_total_bit_cost : base.u64
var i : base.u64
corpus_size = this.sf.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 {
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),
b: 0x3FEF_5C28_F5C2_8F5C),
b: 0x4045_8000_0000_0000)
this.histogram[.. 256].bulk_memset!(byte_value: 0)
i = 0
while i < args.input_size {
if (args.from ~mod+ i) < args.input.length() {
this.histogram[args.input[args.from ~mod+ i]] ~mod+= 1
}
i ~mod+= 43
}
return this.sf.f64_lt(a: this.bits_entropy_of_histogram(), b: max_total_bit_cost) <> 0
}
// Mirrors EmitUncompressedMetaBlock in compress_fragment_two_pass.c (which,
// unlike the one in compress_fragment.c, does not rewind).
pri func encoder.emit_uncompressed_meta_block_two_pass!(input: roslice base.u8, from: base.u64, input_size: base.u64, s: slice base.u8) {
var p : base.u64
this.store_meta_block_header!(len: args.input_size, is_uncompressed: true, s: args.s)
this.align_to_byte!()
this.copy_bytes!(input: args.input, begin: args.from, end: args.from ~mod+ args.input_size, s: args.s)
this.bw_ix ~mod+= args.input_size ~mod<< 3
p = this.bw_ix >> 3
if p < args.s.length() {
args.s[p] = 0
}
}
// Mirrors BrotliCompressFragmentTwoPass and BrotliCompressFragmentTwoPassImpl
// in compress_fragment_two_pass.c.
pri func encoder.compress_fragment_two_pass!(input: roslice base.u8, is_last: base.bool, table_bits: base.u32, s: slice base.u8) {
var initial_ix : base.u64
var min_match : base.u32
var from : base.u64
var input_size : base.u64
var block_size : base.u64
var compress : base.bool
initial_ix = this.bw_ix
min_match = 6
if args.table_bits <= 15 {
min_match = 4
}
input_size = args.input.length()
while input_size > 0 {
block_size = input_size.min(no_more_than: TWO_PASS_BLOCK_SIZE as base.u64)
this.create_commands!(
input: args.input, from: from, block_size: block_size, input_size: input_size,
table_bits: args.table_bits, min_match: min_match)
compress = this.should_compress_two_pass!(
input: args.input, from: from, input_size: block_size, num_literals: this.num_literals as base.u64)
if compress {
this.store_meta_block_header!(len: block_size, is_uncompressed: false, s: args.s)
// No block splits, no contexts.
this.write_bits!(n_bits: 13, bits: 0, s: args.s)
this.store_commands!(s: args.s)
} else {
// Since we did not find many backward references and the entropy
// of the data is close to 8 bits, we can simply emit an
// uncompressed block. This makes compression speed of
// uncompressible data about 3x faster.
this.emit_uncompressed_meta_block_two_pass!(input: args.input, from: from, input_size: block_size, s: args.s)
}
from ~mod+= block_size
input_size ~mod-= block_size
}
// If output is larger than single uncompressed block, rewrite it.
if (this.bw_ix ~mod- initial_ix) > (31 ~mod+ (args.input.length() ~mod<< 3)) {
this.rewind_bit_position!(new_ix: initial_ix, s: args.s)
this.emit_uncompressed_meta_block_two_pass!(input: args.input, from: 0, input_size: args.input.length(), s: args.s)
}
if args.is_last {
this.write_bits!(n_bits: 1, bits: 1, s: args.s) // islast
this.write_bits!(n_bits: 1, bits: 1, s: args.s) // isempty
this.align_to_byte!()
}
}
+81
View File
@@ -0,0 +1,81 @@
// 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
// --------
// Quirks are discussed in (/doc/note/quirks.md). QUIRKS_BASE is defined in
// decode_quirks.wuffs.
//
// The encoder's quirks mirror the C code's BrotliEncoderParameter values, with
// the value semantics of BrotliEncoderSetParameter. The encoder also accepts
// the QUIRK_LARGE_WINDOW key (defined in decode_quirks.wuffs) for the
// BROTLI_PARAM_LARGE_WINDOW parameter: zero or non-zero (large-window brotli
// is not used at qualities 0 to 2).
//
// For all of the encoder's quirks, the (u64) value must fit in a u32 (like
// the C function's uint32_t argument), otherwise set_quirk returns
// base."#bad argument". Like the C code, the parameters can only be set
// before the first transform_io call, otherwise set_quirk returns
// base."#bad call sequence".
// --------
// When this quirk is set, it sets the encoder's BROTLI_PARAM_QUALITY
// parameter: 0 to 11. The default is 11. Like the C code, out of range values
// are clamped (and a value with its high bit set is negative, so it is
// clamped to 0). Only qualities 0 and 1 are currently supported: transform_io
// returns "#unsupported encoder quality" for the others.
//
// It is not named QUIRK_QUALITY because the base package already has a
// base.QUIRK_QUALITY key (with different value semantics).
pub const QUIRK_ENCODER_QUALITY : base.u32 = 0x2AB7_9000 | 0x01
// When this quirk is set, it sets the encoder's BROTLI_PARAM_LGWIN parameter:
// the log2 of the window size, 10 to 24 (or up to 30 for large-window brotli,
// at qualities 3 and above). The default is 22. Out of range values are
// clamped.
pub const QUIRK_LGWIN : base.u32 = 0x2AB7_9000 | 0x02
// When this quirk is set, it sets the encoder's BROTLI_PARAM_LGBLOCK
// parameter: the log2 of the input block size. 0 (the default) means to
// choose it automatically. It has no effect at qualities 0 and 1.
pub const QUIRK_LGBLOCK : base.u32 = 0x2AB7_9000 | 0x03
// When this quirk is set, it sets the encoder's BROTLI_PARAM_MODE parameter:
// 0 (BROTLI_MODE_GENERIC, the default), 1 (BROTLI_MODE_TEXT) or 2
// (BROTLI_MODE_FONT). Like the C code, other values are accepted (and have
// the same effect as BROTLI_MODE_GENERIC). It has no effect at qualities 0
// and 1.
pub const QUIRK_MODE : base.u32 = 0x2AB7_9000 | 0x04
// When this quirk is set, it sets the encoder's
// BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING parameter: 0 or 1. Other
// values are rejected with base."#bad argument". It has no effect at
// qualities 0 and 1.
pub const QUIRK_DISABLE_LITERAL_CONTEXT_MODELING : base.u32 = 0x2AB7_9000 | 0x05
// When this quirk is set, it sets the encoder's BROTLI_PARAM_SIZE_HINT
// parameter: the estimated total input size. It has no effect at qualities 0
// and 1.
pub const QUIRK_SIZE_HINT : base.u32 = 0x2AB7_9000 | 0x06
// When this quirk is set, it sets the encoder's BROTLI_PARAM_NPOSTFIX
// parameter. It has no effect at qualities 0 and 1.
pub const QUIRK_NPOSTFIX : base.u32 = 0x2AB7_9000 | 0x07
// When this quirk is set, it sets the encoder's BROTLI_PARAM_NDIRECT
// parameter. It has no effect at qualities 0 and 1.
pub const QUIRK_NDIRECT : base.u32 = 0x2AB7_9000 | 0x08
// When this quirk is set, it sets the encoder's BROTLI_PARAM_STREAM_OFFSET
// parameter: 0 to (1 << 30). Larger values are rejected with
// base."#bad argument". Like the C code, a non-zero stream offset means that
// no stream header (WBITS) is written.
pub const QUIRK_STREAM_OFFSET : base.u32 = 0x2AB7_9000 | 0x09
+86
View File
@@ -13,11 +13,14 @@
// The reference implementation for std/brotli is libbrotlidec v1.2.1 (from
// github.com/google/brotli). Older versions (such as v1.1.0) differ slightly
// in which error code some invalid inputs produce.
//
// The encoder's reference implementation is libbrotlienc v1.2.1.
#include <limits.h>
#include <stdint.h>
#include "brotli/decode.h"
#include "brotli/encode.h"
// mimic_brotli_decode_error_code is like mimic_brotli_decode but also stores
// the libbrotlidec error code (a BrotliDecoderErrorCode value) in
@@ -72,3 +75,86 @@ mimic_brotli_decode(wuffs_base__io_buffer* dst,
int error_code = 0;
return mimic_brotli_decode_error_code(dst, src, &error_code);
}
// ---------------- Encoder
// mimic_brotli_encode runs libbrotlienc's BrotliEncoderCompressStream over a
// "plan" of num_steps steps. Step i is the BrotliEncoderOperation step_ops[i]
// (0 = PROCESS, 1 = FLUSH, 2 = FINISH, 3 = EMIT_METADATA) applied to the next
// step_lens[i] bytes of src. Each step repeats that operation until all of its
// input is consumed and there is no more pending output (or, for FINISH,
// until the encoder is finished).
//
// Before the first step, each of the num_params (param_keys[j],
// param_values[j]) pairs is passed to BrotliEncoderSetParameter. The keys are
// BrotliEncoderParameter values.
//
// Each BrotliEncoderCompressStream call gets at most dst_chunk bytes of dst
// (or all of dst's writable bytes, if dst_chunk is zero).
const char* //
mimic_brotli_encode(wuffs_base__io_buffer* dst,
wuffs_base__io_buffer* src,
const uint32_t* param_keys,
const uint32_t* param_values,
size_t num_params,
const uint32_t* step_ops,
const size_t* step_lens,
size_t num_steps,
size_t dst_chunk) {
if (BrotliEncoderVersion() != 0x1002001) {
return "libbrotlienc: unexpected version (want v1.2.1)";
}
BrotliEncoderState* state = BrotliEncoderCreateInstance(NULL, NULL, NULL);
if (!state) {
return "libbrotlienc: BrotliEncoderCreateInstance failed";
}
const char* ret = NULL;
for (size_t j = 0; j < num_params; j++) {
if (!BrotliEncoderSetParameter(state,
(BrotliEncoderParameter)(param_keys[j]),
param_values[j])) {
ret = "libbrotlienc: BrotliEncoderSetParameter failed";
goto cleanup;
}
}
for (size_t i = 0; i < num_steps; i++) {
size_t avail_in = step_lens[i];
if (avail_in > wuffs_base__io_buffer__reader_length(src)) {
ret = "mimic_brotli_encode: step length exceeds the input";
goto cleanup;
}
const uint8_t* next_in = wuffs_base__io_buffer__reader_pointer(src);
BrotliEncoderOperation op = (BrotliEncoderOperation)(step_ops[i]);
while (true) {
size_t avail_out = wuffs_base__io_buffer__writer_length(dst);
if ((dst_chunk > 0) && (avail_out > dst_chunk)) {
avail_out = dst_chunk;
}
size_t avail_out0 = avail_out;
uint8_t* next_out = wuffs_base__io_buffer__writer_pointer(dst);
if (!BrotliEncoderCompressStream(state, op, &avail_in, &next_in,
&avail_out, &next_out, NULL)) {
ret = "libbrotlienc: BrotliEncoderCompressStream failed";
goto cleanup;
}
dst->meta.wi += avail_out0 - avail_out;
if (op == BROTLI_OPERATION_FINISH) {
if (BrotliEncoderIsFinished(state)) {
break;
}
} else if ((avail_in == 0) && !BrotliEncoderHasMoreOutput(state)) {
break;
}
if (wuffs_base__io_buffer__writer_length(dst) == 0) {
ret = "mimic_brotli_encode: dst is full";
goto cleanup;
}
}
src->meta.ri += step_lens[i];
}
cleanup:
BrotliEncoderDestroyInstance(state);
return ret;
}
+1062 -1
View File
@@ -33,7 +33,7 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
"wuffs mimic cflags" to run the mimic benchmarks.
*/
// ¿ wuffs mimic cflags: -DWUFFS_MIMIC -lbrotlidec -lbrotlicommon
// ¿ wuffs mimic cflags: -DWUFFS_MIMIC -lbrotlienc -lbrotlidec -lbrotlicommon -lm
// 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
@@ -53,6 +53,7 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the
#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
@@ -633,6 +634,1036 @@ test_mimic_brotli_decode_malformed() {
#endif // WUFFS_MIMIC
// ---------------- Encoder Tests
// The encoder tests describe each encoding as a "plan": a sequence of steps.
// Step i applies the BrotliEncoderOperation g_brotli_enc_step_ops[i] (0 =
// PROCESS, 1 = FLUSH, 2 = FINISH, 3 = EMIT_METADATA) to the next
// g_brotli_enc_step_lens[i] bytes of the source. The wuffs_brotli_encode_plan
// and mimic_brotli_encode functions run a plan with Wuffs' std/brotli encoder
// and libbrotlienc, respectively, so that their outputs can be compared.
#define BROTLI_ENC_OP_PROCESS 0
#define BROTLI_ENC_OP_FLUSH 1
#define BROTLI_ENC_OP_FINISH 2
#define BROTLI_ENC_OP_EMIT_METADATA 3
// The BrotliEncoderParameter values.
#define BROTLI_ENC_PARAM_MODE 0
#define BROTLI_ENC_PARAM_QUALITY 1
#define BROTLI_ENC_PARAM_LGWIN 2
#define BROTLI_ENC_PARAM_LGBLOCK 3
#define BROTLI_ENC_PARAM_DISABLE_LITERAL_CONTEXT_MODELING 4
#define BROTLI_ENC_PARAM_SIZE_HINT 5
#define BROTLI_ENC_PARAM_LARGE_WINDOW 6
#define BROTLI_ENC_PARAM_NPOSTFIX 7
#define BROTLI_ENC_PARAM_NDIRECT 8
#define BROTLI_ENC_PARAM_STREAM_OFFSET 9
#define BROTLI_ENC_MAX_STEPS (1 << 19)
#define BROTLI_ENC_MAX_PARAMS 16
uint32_t g_brotli_enc_step_ops[BROTLI_ENC_MAX_STEPS];
size_t g_brotli_enc_step_lens[BROTLI_ENC_MAX_STEPS];
size_t g_brotli_enc_num_steps;
// The chunkings (how the input is split into steps).
#define BROTLI_ENC_CHUNK_SINGLE 0
#define BROTLI_ENC_CHUNK_1 1
#define BROTLI_ENC_CHUNK_7 2
#define BROTLI_ENC_CHUNK_4096 3
#define BROTLI_ENC_CHUNK_RANDOM 4
#define BROTLI_ENC_NUM_CHUNKINGS 5
// The flush modes.
#define BROTLI_ENC_FLUSH_NONE 0
#define BROTLI_ENC_FLUSH_EVERY_CHUNK 1
#define BROTLI_ENC_FLUSH_MIDDLE 2
#define BROTLI_ENC_NUM_FLUSHES 3
// The metadata positions.
#define BROTLI_ENC_METADATA_NONE 0
#define BROTLI_ENC_METADATA_START 1
#define BROTLI_ENC_METADATA_MIDDLE 2
#define BROTLI_ENC_METADATA_END 3
typedef struct {
uint32_t param_keys[BROTLI_ENC_MAX_PARAMS];
uint32_t param_values[BROTLI_ENC_MAX_PARAMS];
size_t num_params;
uint32_t chunking;
uint32_t flush;
uint32_t metadata_position;
size_t metadata_len;
size_t dst_chunk;
uint64_t seed;
} brotli_enc_config;
void //
brotli_enc_config__add_param(brotli_enc_config* c,
uint32_t key,
uint32_t value) {
if (c->num_params < BROTLI_ENC_MAX_PARAMS) {
c->param_keys[c->num_params] = key;
c->param_values[c->num_params] = value;
c->num_params++;
}
}
uint64_t //
brotli_enc_rand(uint64_t* state) {
// SplitMix64.
uint64_t z = (*state += 0x9E3779B97F4A7C15ull);
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
return z ^ (z >> 31);
}
void //
brotli_enc_fill_random(uint8_t* p, size_t n, uint64_t seed) {
uint64_t state = seed;
for (size_t i = 0; i < n; i++) {
p[i] = (uint8_t)(brotli_enc_rand(&state) >> 56);
}
}
const char* //
brotli_enc_add_step(uint32_t op, size_t len) {
if (g_brotli_enc_num_steps >= BROTLI_ENC_MAX_STEPS) {
return "too many steps";
}
g_brotli_enc_step_ops[g_brotli_enc_num_steps] = op;
g_brotli_enc_step_lens[g_brotli_enc_num_steps] = len;
g_brotli_enc_num_steps++;
return NULL;
}
// make_brotli_enc_plan sets the g_brotli_enc_step_* plan for encoding the
// data[:data_len] input with the config c. It also writes the plan's source
// (the input, with any metadata bytes interleaved) to src.
const char* //
make_brotli_enc_plan(wuffs_base__io_buffer* src,
const brotli_enc_config* c,
const uint8_t* data,
size_t data_len) {
g_brotli_enc_num_steps = 0;
src->meta.ri = 0;
src->meta.wi = 0;
src->meta.pos = 0;
src->meta.closed = true;
if ((data_len + c->metadata_len) > src->data.len) {
return "make_brotli_enc_plan: src is too short";
}
uint64_t rng = c->seed;
size_t metadata_at = SIZE_MAX;
if (c->metadata_position == BROTLI_ENC_METADATA_START) {
metadata_at = 0;
} else if (c->metadata_position == BROTLI_ENC_METADATA_MIDDLE) {
metadata_at = data_len / 2;
} else if (c->metadata_position == BROTLI_ENC_METADATA_END) {
metadata_at = data_len;
}
bool flushed_middle = false;
size_t i = 0;
while (true) {
if (i == metadata_at) {
brotli_enc_fill_random(src->data.ptr + src->meta.wi, c->metadata_len,
c->seed ^ 0x5A5A);
src->meta.wi += c->metadata_len;
CHECK_STRING(
brotli_enc_add_step(BROTLI_ENC_OP_EMIT_METADATA, c->metadata_len));
metadata_at = SIZE_MAX;
}
size_t n = data_len - i;
switch (c->chunking) {
case BROTLI_ENC_CHUNK_1:
n = 1;
break;
case BROTLI_ENC_CHUNK_7:
n = 7;
break;
case BROTLI_ENC_CHUNK_4096:
n = 4096;
break;
case BROTLI_ENC_CHUNK_RANDOM: {
uint64_t r = brotli_enc_rand(&rng);
// Mostly small chunks, with occasional large ones.
n = (size_t)(1 + ((r & 0xFF) < 0xF0 ? ((r >> 8) % 300)
: ((r >> 8) % 70000)));
break;
}
}
if ((metadata_at != SIZE_MAX) && (metadata_at > i)) {
n = (n < (metadata_at - i)) ? n : (metadata_at - i);
}
if (n > (data_len - i)) {
n = data_len - i;
}
memcpy(src->data.ptr + src->meta.wi, data + i, n);
src->meta.wi += n;
i += n;
bool last = (i == data_len) && (metadata_at == SIZE_MAX);
if (last) {
CHECK_STRING(brotli_enc_add_step(BROTLI_ENC_OP_FINISH, n));
break;
} else if (c->flush == BROTLI_ENC_FLUSH_EVERY_CHUNK) {
CHECK_STRING(brotli_enc_add_step(BROTLI_ENC_OP_FLUSH, n));
} else {
CHECK_STRING(brotli_enc_add_step(BROTLI_ENC_OP_PROCESS, n));
if ((c->flush == BROTLI_ENC_FLUSH_MIDDLE) && !flushed_middle &&
((2 * i) >= data_len)) {
flushed_middle = true;
CHECK_STRING(brotli_enc_add_step(BROTLI_ENC_OP_FLUSH, 0));
}
}
}
return NULL;
}
// brotli_enc_quirk_key maps a BrotliEncoderParameter value to the
// corresponding std/brotli quirk key (or 0 if there is none).
uint32_t //
brotli_enc_quirk_key(uint32_t param) {
switch (param) {
case 0:
return WUFFS_BROTLI__QUIRK_MODE;
case 1:
return WUFFS_BROTLI__QUIRK_ENCODER_QUALITY;
case 2:
return WUFFS_BROTLI__QUIRK_LGWIN;
case 3:
return WUFFS_BROTLI__QUIRK_LGBLOCK;
case 4:
return WUFFS_BROTLI__QUIRK_DISABLE_LITERAL_CONTEXT_MODELING;
case 5:
return WUFFS_BROTLI__QUIRK_SIZE_HINT;
case 6:
return WUFFS_BROTLI__QUIRK_LARGE_WINDOW;
case 7:
return WUFFS_BROTLI__QUIRK_NPOSTFIX;
case 8:
return WUFFS_BROTLI__QUIRK_NDIRECT;
case 9:
return WUFFS_BROTLI__QUIRK_STREAM_OFFSET;
}
return 0;
}
// wuffs_brotli_encode_plan is the Wuffs equivalent of mimic_brotli_encode
// (in test/c/mimiclib/brotli.c).
const char* //
wuffs_brotli_encode_plan(wuffs_base__io_buffer* dst,
wuffs_base__io_buffer* src,
const uint32_t* param_keys,
const uint32_t* param_values,
size_t num_params,
const uint32_t* step_ops,
const size_t* step_lens,
size_t num_steps,
size_t dst_chunk) {
// The encoder struct is large (more than 1 MiB), mostly arrays that do not
// need zero-initializing, so don't use wuffs_brotli__encoder__alloc.
wuffs_brotli__encoder* enc = malloc(sizeof__wuffs_brotli__encoder());
if (!enc) {
return "malloc failed";
}
const char* ret = wuffs_brotli__encoder__initialize(
enc, sizeof__wuffs_brotli__encoder(), WUFFS_VERSION,
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)
.repr;
if (ret) {
goto cleanup;
}
for (size_t j = 0; j < num_params; j++) {
wuffs_base__status status = wuffs_brotli__encoder__set_quirk(
enc, brotli_enc_quirk_key(param_keys[j]),
param_values[j]);
if (status.repr) {
ret = status.repr;
goto cleanup;
}
}
wuffs_base__range_ii_u64 workbuf_len =
wuffs_brotli__encoder__workbuf_len(enc);
if (workbuf_len.max_incl > g_work_slice_u8.len) {
ret = "workbuf_len is too large";
goto cleanup;
}
wuffs_base__slice_u8 workbuf = wuffs_base__make_slice_u8(
g_work_slice_u8.ptr, (size_t)workbuf_len.max_incl);
for (size_t i = 0; i < num_steps; i++) {
size_t end = src->meta.ri + step_lens[i];
if (end > src->meta.wi) {
ret = "wuffs_brotli_encode_plan: step length exceeds the input";
goto cleanup;
}
uint32_t op = step_ops[i];
wuffs_base__status status = wuffs_base__make_status(NULL);
if (op == BROTLI_ENC_OP_FLUSH) {
status = wuffs_brotli__encoder__request_flush(enc);
} else if (op == BROTLI_ENC_OP_EMIT_METADATA) {
status = wuffs_brotli__encoder__request_emit_metadata(enc, step_lens[i]);
}
if (status.repr) {
ret = status.repr;
goto cleanup;
}
while (true) {
wuffs_base__io_buffer d = *dst;
if ((dst_chunk > 0) && ((d.data.len - d.meta.wi) > dst_chunk)) {
d.data.len = d.meta.wi + dst_chunk;
}
wuffs_base__io_buffer s = *src;
s.meta.wi = end;
s.meta.closed = (op == BROTLI_ENC_OP_FINISH);
status = wuffs_brotli__encoder__transform_io(enc, &d, &s, workbuf);
dst->meta.wi = d.meta.wi;
src->meta.ri = s.meta.ri;
if (status.repr == NULL) {
if (op != BROTLI_ENC_OP_FINISH) {
ret = "wuffs_brotli_encode_plan: unexpected ok status";
goto cleanup;
}
break;
} else if (status.repr == wuffs_base__suspension__short_write) {
if (dst->meta.wi == dst->data.len) {
ret = "wuffs_brotli_encode_plan: dst is full";
goto cleanup;
}
continue;
} else if ((status.repr == wuffs_base__suspension__short_read) &&
(op != BROTLI_ENC_OP_FINISH) && (src->meta.ri == end)) {
break;
}
ret = status.repr;
goto cleanup;
}
}
cleanup:
free(enc);
return ret;
}
// wuffs_brotli_encode encodes src to dst in one go, at the given quality and
// lgwin, with a dst limit of wlimit bytes per transform_io call.
const char* //
wuffs_brotli_encode(wuffs_base__io_buffer* dst,
wuffs_base__io_buffer* src,
uint32_t quality,
uint32_t lgwin,
size_t dst_chunk) {
uint32_t keys[2] = {BROTLI_ENC_PARAM_QUALITY, BROTLI_ENC_PARAM_LGWIN};
uint32_t values[2] = {quality, lgwin};
uint32_t op = BROTLI_ENC_OP_FINISH;
size_t len = wuffs_base__io_buffer__reader_length(src);
return wuffs_brotli_encode_plan(dst, src, keys, values, 2, &op, &len, 1,
dst_chunk);
}
// do_test_brotli_encode_round_trip encodes data[:data_len] (with the plan's
// config c) and then decodes it (with Wuffs' std/brotli decoder), checking
// that the result is the original data.
const char* //
do_test_brotli_encode_round_trip(const char* name,
const brotli_enc_config* c,
const uint8_t* data,
size_t data_len) {
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
.data = g_src_slice_u8,
});
CHECK_STRING(make_brotli_enc_plan(&src, c, data, data_len));
wuffs_base__io_buffer have = ((wuffs_base__io_buffer){
.data = g_have_slice_u8,
});
const char* status = wuffs_brotli_encode_plan(
&have, &src, c->param_keys, c->param_values, c->num_params,
g_brotli_enc_step_ops, g_brotli_enc_step_lens, g_brotli_enc_num_steps,
c->dst_chunk);
if (status) {
RETURN_FAIL("%s: encode: %s", name, status);
}
have.meta.closed = true;
wuffs_base__io_buffer decoded = ((wuffs_base__io_buffer){
.data = g_want_slice_u8,
});
status = wuffs_brotli_decode(&decoded, &have, WUFFS_INITIALIZE__DEFAULT_OPTIONS,
UINT64_MAX, UINT64_MAX);
if (status) {
RETURN_FAIL("%s: decode: %s", name, status);
} else if (have.meta.ri != have.meta.wi) {
RETURN_FAIL("%s: decode: trailing data", name);
} else if (decoded.meta.wi != data_len) {
RETURN_FAIL("%s: decoded length: have %zu, want %zu", name,
decoded.meta.wi, data_len);
} else if (memcmp(decoded.data.ptr, data, data_len)) {
RETURN_FAIL("%s: decoded bytes differ", name);
}
return NULL;
}
// brotli_enc_corpus_filenames lists the round trip (and parity) test inputs:
// every file in test/data/brotli plus some others.
const char* brotli_enc_corpus_filenames[] = {
"test/data/256.bytes",
"test/data/enwik5",
"test/data/romeo.txt",
"test/data/brotli/README.md",
};
// for_each_brotli_enc_corpus_file calls f on every corpus file, loading each
// one into a malloc'ed buffer.
const char* //
for_each_brotli_enc_corpus_file(
const char* (*f)(const char* name, const uint8_t* data, size_t len)) {
const char* names[256];
size_t num_names = 0;
for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(brotli_enc_corpus_filenames);
i++) {
names[num_names++] = brotli_enc_corpus_filenames[i];
}
for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(g_brotli_vectors); i++) {
names[num_names++] = g_brotli_vectors[i].src_filename;
names[num_names++] = g_brotli_vectors[i].want_filename;
}
for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(g_brotli_malformed); i++) {
if (g_brotli_malformed[i].src_filename) {
names[num_names++] = g_brotli_malformed[i].src_filename;
}
}
uint8_t* buf = malloc(1 << 20);
if (!buf) {
return "malloc failed";
}
const char* ret = NULL;
for (size_t i = 0; i < num_names; i++) {
bool dup = false;
for (size_t j = 0; j < i; j++) {
if (!strcmp(names[i], names[j])) {
dup = true;
break;
}
}
if (dup) {
continue;
}
wuffs_base__io_buffer b = ((wuffs_base__io_buffer){
.data = wuffs_base__make_slice_u8(buf, 1 << 20),
});
ret = read_file(&b, names[i]);
if (!ret) {
ret = f(names[i], b.data.ptr, b.meta.wi);
}
if (ret) {
break;
}
}
free(buf);
return ret;
}
// brotli_enc_synthetic makes one of the synthetic test inputs: 0 is 1 MiB of
// zeroes, 1 is 256 KiB of pseudo-random bytes and 2 is a 100-byte pattern
// repeated to 64 KiB. It returns a malloc'ed buffer.
uint8_t* //
brotli_enc_synthetic(int which, size_t* len) {
size_t n = (which == 0) ? (1 << 20) : (which == 1) ? (256 << 10) : (64 << 10);
uint8_t* p = malloc(n);
if (!p) {
return NULL;
}
if (which == 0) {
memset(p, 0, n);
} else if (which == 1) {
brotli_enc_fill_random(p, n, 0x1234);
} else {
uint8_t pattern[100];
brotli_enc_fill_random(pattern, 100, 0x5678);
for (size_t i = 0; i < n; i++) {
p[i] = pattern[i % 100];
}
}
*len = n;
return p;
}
brotli_enc_config //
make_brotli_enc_config(uint32_t quality, uint32_t lgwin) {
brotli_enc_config c;
memset(&c, 0, sizeof c);
brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_QUALITY, quality);
brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_LGWIN, lgwin);
return c;
}
const char* //
do_test_brotli_encode_round_trip_file(const char* name,
const uint8_t* data,
size_t len) {
for (uint32_t q = 0; q <= 1; q++) {
for (int k = 0; k < 3; k++) {
brotli_enc_config c = make_brotli_enc_config(q, (k == 0) ? 10 : 22);
if (k == 2) {
c.chunking = BROTLI_ENC_CHUNK_RANDOM;
c.flush = BROTLI_ENC_FLUSH_MIDDLE;
c.dst_chunk = 97;
c.seed = len;
}
char s[256];
snprintf(s, sizeof s, "%s (q=%u, k=%d)", name, q, k);
CHECK_STRING(do_test_brotli_encode_round_trip(s, &c, data, len));
}
}
return NULL;
}
const char* //
test_wuffs_brotli_encode_round_trip_corpus() {
CHECK_FOCUS(__func__);
return for_each_brotli_enc_corpus_file(do_test_brotli_encode_round_trip_file);
}
const char* //
test_wuffs_brotli_encode_round_trip_lengths() {
CHECK_FOCUS(__func__);
uint8_t data[40];
brotli_enc_fill_random(data, 20, 0x9999);
memcpy(data + 20, data, 20);
for (size_t n = 0; n <= 40; n++) {
char s[64];
snprintf(s, sizeof s, "length %zu", n);
CHECK_STRING(do_test_brotli_encode_round_trip_file(s, data, n));
}
return NULL;
}
const char* //
test_wuffs_brotli_encode_round_trip_synthetic() {
CHECK_FOCUS(__func__);
for (int which = 0; which < 3; which++) {
size_t len = 0;
uint8_t* data = brotli_enc_synthetic(which, &len);
if (!data) {
RETURN_FAIL("malloc failed");
}
char s[64];
snprintf(s, sizeof s, "synthetic #%d", which);
const char* status = do_test_brotli_encode_round_trip_file(s, data, len);
free(data);
if (status) {
return status;
}
}
return NULL;
}
const char* //
test_wuffs_brotli_encode_metadata_round_trip() {
CHECK_FOCUS(__func__);
wuffs_base__io_buffer data = ((wuffs_base__io_buffer){
.data = g_mimiclib_scratch_slice_u8,
});
CHECK_STRING(read_file(&data, "test/data/romeo.txt"));
static const size_t lens[] = {0, 1, 100, 70000};
for (uint32_t q = 0; q <= 1; q++) {
for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(lens); i++) {
for (uint32_t pos = 1; pos <= 3; pos++) {
brotli_enc_config c = make_brotli_enc_config(q, 18);
c.chunking = BROTLI_ENC_CHUNK_7;
c.metadata_position = pos;
c.metadata_len = lens[i];
c.dst_chunk = (pos == 2) ? 3 : 0;
char s[64];
snprintf(s, sizeof s, "q=%u, len=%zu, pos=%u", q, lens[i], pos);
CHECK_STRING(do_test_brotli_encode_round_trip(s, &c, data.data.ptr,
data.meta.wi));
}
}
}
return NULL;
}
const char* //
test_wuffs_brotli_encode_parameters() {
CHECK_FOCUS(__func__);
wuffs_brotli__encoder* enc = wuffs_brotli__encoder__alloc();
if (!enc) {
RETURN_FAIL("alloc failed");
}
const char* ret = NULL;
wuffs_base__status status;
uint8_t buf[64];
wuffs_base__io_buffer dst =
wuffs_base__ptr_u8__writer(buf, sizeof buf);
wuffs_base__io_buffer src = make_io_buffer_from_string("", 0);
#define BROTLI_ENC_CHECK(cond, msg) \
if (!(cond)) { \
ret = msg; \
goto cleanup; \
}
// Unknown keys and out-of-range values.
status = wuffs_brotli__encoder__set_quirk(
enc, WUFFS_BROTLI__QUIRK_STREAM_OFFSET + 1, 0);
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__unsupported_option,
"unknown key");
status = wuffs_brotli__encoder__set_quirk(enc, 12345, 0);
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__unsupported_option,
"unknown key (2)");
status = wuffs_brotli__encoder__set_quirk(
enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, 0x100000000ull);
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_argument,
"value > 0xFFFFFFFF");
status = wuffs_brotli__encoder__set_quirk(
enc, WUFFS_BROTLI__QUIRK_DISABLE_LITERAL_CONTEXT_MODELING, 2);
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_argument,
"disable_lcm > 1");
status = wuffs_brotli__encoder__set_quirk(
enc, WUFFS_BROTLI__QUIRK_STREAM_OFFSET, (1 << 30) + 1);
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_argument,
"stream_offset > 1<<30");
status = wuffs_brotli__encoder__request_emit_metadata(enc, (1 << 24) + 1);
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_argument,
"metadata length > 1<<24");
// Raw values are stored as is, and sanitized later.
status = wuffs_brotli__encoder__set_quirk(
enc, WUFFS_BROTLI__QUIRK_LGWIN, 99);
BROTLI_ENC_CHECK(!status.repr, "set lgwin");
BROTLI_ENC_CHECK(wuffs_brotli__encoder__get_quirk(
enc, WUFFS_BROTLI__QUIRK_LGWIN) == 99,
"get lgwin");
// The default quality is 11, which is unsupported.
BROTLI_ENC_CHECK(wuffs_brotli__encoder__workbuf_len(enc).max_incl == 0,
"workbuf_len at q11");
status = wuffs_brotli__encoder__set_quirk(
enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, 0xFFFFFFFF); // (int)-1.
BROTLI_ENC_CHECK(!status.repr, "set quality");
// Quality -1 is sanitized to 0 and lgwin 99 to 24.
BROTLI_ENC_CHECK(wuffs_brotli__encoder__workbuf_len(enc).max_incl ==
((2u << 24) + 503 + 16),
"workbuf_len at q0w24");
status = wuffs_brotli__encoder__set_quirk(
enc, WUFFS_BROTLI__QUIRK_LGWIN, 0xFFFFFFFF); // (int)-1.
BROTLI_ENC_CHECK(!status.repr, "set lgwin (2)");
BROTLI_ENC_CHECK(wuffs_brotli__encoder__workbuf_len(enc).max_incl ==
((2u << 10) + 503 + 16),
"workbuf_len at q0w10");
// max_compressed_size mirrors BrotliEncoderMaxCompressedSize.
BROTLI_ENC_CHECK(wuffs_brotli__encoder__max_compressed_size(enc, 0) == 2,
"max_compressed_size(0)");
BROTLI_ENC_CHECK(
wuffs_brotli__encoder__max_compressed_size(enc, 1 << 20) ==
((1 << 20) + 6 + (4 * 64)),
"max_compressed_size(1<<20)");
BROTLI_ENC_CHECK(
wuffs_brotli__encoder__max_compressed_size(enc, UINT64_MAX) == 0,
"max_compressed_size(overflow)");
// A too-short workbuf.
src.meta.closed = false;
status = wuffs_brotli__encoder__transform_io(
enc, &dst, &src, wuffs_base__make_slice_u8(g_work_slice_u8.ptr, 10));
BROTLI_ENC_CHECK(status.repr == wuffs_base__suspension__short_read,
"empty input");
// Parameters can't be changed after the first transform_io call.
status = wuffs_brotli__encoder__set_quirk(
enc, WUFFS_BROTLI__QUIRK_LGWIN, 16);
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_call_sequence,
"set_quirk after transform_io");
src.data.ptr = (uint8_t*)"abc";
src.data.len = 3;
src.meta.wi = 3;
status = wuffs_brotli__encoder__transform_io(
enc, &dst, &src, wuffs_base__make_slice_u8(g_work_slice_u8.ptr, 10));
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_workbuf_length,
"short workbuf");
// Qualities 2 to 11 are unsupported.
for (uint32_t q = 2; q <= 12; q++) {
free(enc);
enc = wuffs_brotli__encoder__alloc();
if (!enc) {
RETURN_FAIL("alloc failed");
}
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY,
q);
status = wuffs_brotli__encoder__transform_io(enc, &dst, &src,
g_work_slice_u8);
BROTLI_ENC_CHECK(
status.repr == wuffs_brotli__error__unsupported_encoder_quality,
"quality 2..11");
}
// Requests.
free(enc);
enc = wuffs_brotli__encoder__alloc();
if (!enc) {
RETURN_FAIL("alloc failed");
}
status = wuffs_brotli__encoder__request_emit_metadata(enc, 1);
BROTLI_ENC_CHECK(!status.repr, "request_emit_metadata");
status = wuffs_brotli__encoder__request_flush(enc);
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_call_sequence,
"request_flush while metadata is pending");
status = wuffs_brotli__encoder__request_emit_metadata(enc, 1);
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_call_sequence,
"request_emit_metadata twice");
// A closed src with too few metadata bytes.
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, 1);
src = make_io_buffer_from_string("", 0);
status = wuffs_brotli__encoder__transform_io(enc, &dst, &src,
g_work_slice_u8);
BROTLI_ENC_CHECK(status.repr == wuffs_brotli__error__truncated_input,
"truncated metadata");
#undef BROTLI_ENC_CHECK
cleanup:
free(enc);
if (ret) {
RETURN_FAIL("%s: status \"%s\"", ret, status.repr);
}
return NULL;
}
const char* //
test_wuffs_brotli_encode_interface() {
CHECK_FOCUS(__func__);
wuffs_brotli__encoder* enc = wuffs_brotli__encoder__alloc();
if (!enc) {
RETURN_FAIL("alloc failed");
}
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, 1);
// Encoding romeo.txt at q1w22 gives 575 bytes, the last of which is 0xC3.
const char* status = do_test__wuffs_base__io_transformer(
wuffs_brotli__encoder__upcast_as__wuffs_base__io_transformer(enc),
"test/data/romeo.txt", 0, SIZE_MAX, 575, 0xC3);
free(enc);
return status;
}
// ---------------- Encoder Mimic Tests
#ifdef WUFFS_MIMIC
// do_test_mimic_brotli_encode checks that Wuffs and libbrotlienc produce
// exactly the same bytes for encoding data[:data_len] with the config c.
const char* //
do_test_mimic_brotli_encode(const char* name,
const brotli_enc_config* c,
const uint8_t* data,
size_t data_len) {
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
.data = g_src_slice_u8,
});
CHECK_STRING(make_brotli_enc_plan(&src, c, data, data_len));
wuffs_base__io_buffer have = ((wuffs_base__io_buffer){
.data = g_have_slice_u8,
});
wuffs_base__io_buffer want = ((wuffs_base__io_buffer){
.data = g_want_slice_u8,
});
wuffs_base__io_buffer src0 = src;
const char* have_status = wuffs_brotli_encode_plan(
&have, &src0, c->param_keys, c->param_values, c->num_params,
g_brotli_enc_step_ops, g_brotli_enc_step_lens, g_brotli_enc_num_steps,
c->dst_chunk);
if (have_status) {
RETURN_FAIL("%s: wuffs: %s", name, have_status);
}
wuffs_base__io_buffer src1 = src;
const char* want_status = mimic_brotli_encode(
&want, &src1, c->param_keys, c->param_values, c->num_params,
g_brotli_enc_step_ops, g_brotli_enc_step_lens, g_brotli_enc_num_steps,
c->dst_chunk);
if (want_status) {
RETURN_FAIL("%s: mimic: %s", name, want_status);
}
const char* status = check_io_buffers_equal("", &have, &want);
if (status) {
RETURN_FAIL("%s: %s", name, status);
}
return NULL;
}
const char* brotli_enc_parity_filenames[] = {
"test/data/256.bytes",
"test/data/romeo.txt",
"test/data/brotli/alice29.txt",
"test/data/brotli/random_org_10k.bin",
};
const char* //
test_mimic_brotli_encode_parity_grid() {
CHECK_FOCUS(__func__);
static const uint32_t lgwins[] = {10, 16, 18, 22, 24};
wuffs_base__io_buffer data = ((wuffs_base__io_buffer){
.data = g_mimiclib_scratch_slice_u8,
});
for (size_t f = 0; f < WUFFS_TESTLIB_ARRAY_SIZE(brotli_enc_parity_filenames);
f++) {
data.meta.ri = 0;
data.meta.wi = 0;
data.meta.closed = false;
CHECK_STRING(read_file(&data, brotli_enc_parity_filenames[f]));
for (uint32_t q = 0; q <= 1; q++) {
for (size_t w = 0; w < WUFFS_TESTLIB_ARRAY_SIZE(lgwins); w++) {
for (uint32_t mode = 0; mode <= 2; mode++) {
for (uint32_t ch = 0; ch < BROTLI_ENC_NUM_CHUNKINGS; ch++) {
if ((ch == BROTLI_ENC_CHUNK_1) && (data.meta.wi > 4096)) {
continue;
}
for (uint32_t fl = 0; fl < BROTLI_ENC_NUM_FLUSHES; fl++) {
brotli_enc_config c = make_brotli_enc_config(q, lgwins[w]);
brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_MODE, mode);
c.chunking = ch;
c.flush = fl;
c.seed = (f << 16) | (w << 8) | (mode << 4) | fl;
// To keep this test fast, the 7-byte chunking only uses the
// first 4 KiB of the input. The
// script/manual-test-brotli-encoder-parity.c program runs
// the whole input.
size_t n = data.meta.wi;
if ((ch == BROTLI_ENC_CHUNK_7) && (n > 4096)) {
n = 4096;
}
char s[256];
snprintf(s, sizeof s,
"%s: q=%u lgwin=%u mode=%u chunking=%u flush=%u",
brotli_enc_parity_filenames[f], q, lgwins[w], mode, ch,
fl);
CHECK_STRING(
do_test_mimic_brotli_encode(s, &c, data.data.ptr, n));
}
}
}
}
}
}
return NULL;
}
const char* //
test_mimic_brotli_encode_parity_metadata() {
CHECK_FOCUS(__func__);
static const size_t lens[] = {0, 1, 100, 70000};
wuffs_base__io_buffer data = ((wuffs_base__io_buffer){
.data = g_mimiclib_scratch_slice_u8,
});
CHECK_STRING(read_file(&data, "test/data/romeo.txt"));
for (uint32_t q = 0; q <= 1; q++) {
for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(lens); i++) {
for (uint32_t pos = 1; pos <= 3; pos++) {
for (uint32_t fl = 0; fl < BROTLI_ENC_NUM_FLUSHES; fl++) {
for (size_t dc = 0; dc < 2; dc++) {
brotli_enc_config c = make_brotli_enc_config(q, 16);
c.chunking = BROTLI_ENC_CHUNK_RANDOM;
c.flush = fl;
c.metadata_position = pos;
c.metadata_len = lens[i];
c.dst_chunk = dc ? 5 : 0;
c.seed = (i << 8) | pos;
char s[256];
snprintf(s, sizeof s,
"q=%u metadata_len=%zu pos=%u flush=%u dst_chunk=%zu", q,
lens[i], pos, fl, c.dst_chunk);
CHECK_STRING(do_test_mimic_brotli_encode(s, &c, data.data.ptr,
data.meta.wi));
}
}
}
}
}
return NULL;
}
const char* //
test_mimic_brotli_encode_parity_params() {
CHECK_FOCUS(__func__);
wuffs_base__io_buffer data = ((wuffs_base__io_buffer){
.data = g_mimiclib_scratch_slice_u8,
});
CHECK_STRING(read_file(&data, "test/data/brotli/alice29.txt"));
uint32_t n = (uint32_t)data.meta.wi;
static const struct {
uint32_t key;
uint32_t value;
} extras[] = {
{BROTLI_ENC_PARAM_SIZE_HINT, 0},
{BROTLI_ENC_PARAM_SIZE_HINT, 0xFFFFFFFF}, // Replaced by the exact size.
{BROTLI_ENC_PARAM_SIZE_HINT, 1u << 30},
{BROTLI_ENC_PARAM_LGBLOCK, 16},
{BROTLI_ENC_PARAM_LGBLOCK, 24},
{BROTLI_ENC_PARAM_DISABLE_LITERAL_CONTEXT_MODELING, 1},
{BROTLI_ENC_PARAM_LARGE_WINDOW, 1},
{BROTLI_ENC_PARAM_NPOSTFIX, 2},
{BROTLI_ENC_PARAM_NDIRECT, 12},
{BROTLI_ENC_PARAM_STREAM_OFFSET, 1},
{BROTLI_ENC_PARAM_STREAM_OFFSET, 1000},
{BROTLI_ENC_PARAM_STREAM_OFFSET, 1u << 30},
{BROTLI_ENC_PARAM_LGWIN, 0xFFFFFFFF}, // (int)-1.
{BROTLI_ENC_PARAM_LGWIN, 30},
{BROTLI_ENC_PARAM_QUALITY, 0xFFFFFFFF}, // (int)-1, so quality 0.
};
for (uint32_t q = 0; q <= 1; q++) {
for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(extras); i++) {
for (uint32_t ch = 0; ch < BROTLI_ENC_NUM_CHUNKINGS; ch += 4) {
brotli_enc_config c = make_brotli_enc_config(q, 22);
uint32_t v = extras[i].value;
if ((extras[i].key == BROTLI_ENC_PARAM_SIZE_HINT) &&
(v == 0xFFFFFFFF)) {
v = n;
}
brotli_enc_config__add_param(&c, extras[i].key, v);
c.chunking = ch;
c.flush = BROTLI_ENC_FLUSH_MIDDLE;
c.seed = i;
char s[256];
snprintf(s, sizeof s, "q=%u param[%u]=%u chunking=%u", q,
extras[i].key, v, ch);
CHECK_STRING(
do_test_mimic_brotli_encode(s, &c, data.data.ptr, data.meta.wi));
}
}
}
return NULL;
}
const char* //
do_test_mimic_brotli_encode_file(const char* name,
const uint8_t* data,
size_t len) {
for (uint32_t q = 0; q <= 1; q++) {
for (uint32_t w = 10; w <= 24; w += 14) {
brotli_enc_config c = make_brotli_enc_config(q, w);
c.chunking = (w == 10) ? BROTLI_ENC_CHUNK_SINGLE : BROTLI_ENC_CHUNK_RANDOM;
c.flush = (w == 10) ? BROTLI_ENC_FLUSH_NONE : BROTLI_ENC_FLUSH_MIDDLE;
c.seed = len;
char s[256];
snprintf(s, sizeof s, "%s: q=%u lgwin=%u", name, q, w);
CHECK_STRING(do_test_mimic_brotli_encode(s, &c, data, len));
}
}
return NULL;
}
const char* //
test_mimic_brotli_encode_parity_corpus() {
CHECK_FOCUS(__func__);
CHECK_STRING(
for_each_brotli_enc_corpus_file(do_test_mimic_brotli_encode_file));
uint8_t data[40];
brotli_enc_fill_random(data, 20, 0x9999);
memcpy(data + 20, data, 20);
for (size_t n = 0; n <= 40; n++) {
char s[64];
snprintf(s, sizeof s, "length %zu", n);
CHECK_STRING(do_test_mimic_brotli_encode_file(s, data, n));
}
for (int which = 0; which < 3; which++) {
size_t len = 0;
uint8_t* p = brotli_enc_synthetic(which, &len);
if (!p) {
RETURN_FAIL("malloc failed");
}
char s[64];
snprintf(s, sizeof s, "synthetic #%d", which);
const char* status = do_test_mimic_brotli_encode_file(s, p, len);
free(p);
if (status) {
return status;
}
}
return NULL;
}
#endif // WUFFS_MIMIC
// ---------------- Encoder Benches
const char* //
do_bench_brotli_encode(const char* (*codec_func)(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),
uint32_t quality,
uint64_t iters_unscaled) {
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
.data = g_src_slice_u8,
});
CHECK_STRING(read_file(&src, "test/data/brotli/alice29.txt"));
uint32_t keys[2] = {BROTLI_ENC_PARAM_QUALITY, BROTLI_ENC_PARAM_LGWIN};
uint32_t values[2] = {quality, 22};
uint32_t op = BROTLI_ENC_OP_FINISH;
size_t len = src.meta.wi;
bench_start();
uint64_t n_bytes = 0;
uint64_t i;
uint64_t iters = iters_unscaled * g_flags.iterscale;
for (i = 0; i < iters; i++) {
wuffs_base__io_buffer have = ((wuffs_base__io_buffer){
.data = g_have_slice_u8,
});
src.meta.ri = 0;
CHECK_STRING(codec_func(&have, &src, keys, values, 2, &op, &len, 1, 0));
n_bytes += len;
}
bench_finish(iters, n_bytes);
return NULL;
}
const char* //
bench_wuffs_brotli_encode_alice29_q0() {
CHECK_FOCUS(__func__);
return do_bench_brotli_encode(wuffs_brotli_encode_plan, 0, 30);
}
const char* //
bench_wuffs_brotli_encode_alice29_q1() {
CHECK_FOCUS(__func__);
return do_bench_brotli_encode(wuffs_brotli_encode_plan, 1, 30);
}
// ---------------- Encoder Mimic Benches
#ifdef WUFFS_MIMIC
const char* //
bench_mimic_brotli_encode_alice29_q0() {
CHECK_FOCUS(__func__);
return do_bench_brotli_encode(mimic_brotli_encode, 0, 30);
}
const char* //
bench_mimic_brotli_encode_alice29_q1() {
CHECK_FOCUS(__func__);
return do_bench_brotli_encode(mimic_brotli_encode, 1, 30);
}
#endif // WUFFS_MIMIC
// ---------------- Brotli Benches
const char* //
@@ -682,6 +1713,24 @@ proc g_tests[] = {
test_mimic_brotli_decode_malformed,
test_mimic_brotli_decode_romeo,
#endif // WUFFS_MIMIC
// Encoder tests.
test_wuffs_brotli_encode_interface,
test_wuffs_brotli_encode_metadata_round_trip,
test_wuffs_brotli_encode_parameters,
test_wuffs_brotli_encode_round_trip_corpus,
test_wuffs_brotli_encode_round_trip_lengths,
test_wuffs_brotli_encode_round_trip_synthetic,
#ifdef WUFFS_MIMIC
test_mimic_brotli_encode_parity_corpus,
test_mimic_brotli_encode_parity_grid,
test_mimic_brotli_encode_parity_metadata,
test_mimic_brotli_encode_parity_params,
#endif // WUFFS_MIMIC
NULL,
@@ -695,6 +1744,18 @@ proc g_benches[] = {
bench_mimic_brotli_decode_alice29,
#endif // WUFFS_MIMIC
// Encoder benches.
bench_wuffs_brotli_encode_alice29_q0,
bench_wuffs_brotli_encode_alice29_q1,
#ifdef WUFFS_MIMIC
bench_mimic_brotli_encode_alice29_q0,
bench_mimic_brotli_encode_alice29_q1,
#endif // WUFFS_MIMIC
NULL,