diff --git a/doc/changelog.md b/doc/changelog.md index 69308bab..eb423e7b 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -10,6 +10,7 @@ - Added `slice_var as nptr array[etc] etc` conversion. - Added `std/jpeg`. - Added `std/netpbm`. +- Added `std/xxhash32`. - Added `wuffs_base__status__is_truncated_input_error`. - Changed `lzw.set_literal_width` to `lzw.set_quirk`. - Changed `set_quirk_enabled!(quirk: u32, enabled: bool)` to `set_quirk!(key: diff --git a/doc/std/hashers.md b/doc/std/hashers.md index a1eada8d..412f0e52 100644 --- a/doc/std/hashers.md +++ b/doc/std/hashers.md @@ -31,6 +31,7 @@ In Wuffs syntax, the `base.hasher_u32` methods are: - [std/adler32](/std/adler32) - [std/crc32](/std/crc32) +- [std/xxhash32](/std/xxhash32) ## Examples diff --git a/go.mod b/go.mod index 763bb378..089abb79 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/google/wuffs go 1.16 require ( + github.com/pierrec/xxHash v0.1.5 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect golang.org/x/image v0.5.0 // indirect ) diff --git a/go.sum b/go.sum index 8eaf0113..b1a2730f 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo= +github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 56b94156..3fa5e073 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -12267,6 +12267,210 @@ struct wuffs_wbmp__decoder__struct { #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__WBMP) || defined(WUFFS_NONMONOLITHIC) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__XXHASH32) || defined(WUFFS_NONMONOLITHIC) + +// ---------------- Status Codes + +// ---------------- Public Consts + +// ---------------- Struct Declarations + +typedef struct wuffs_xxhash32__hasher__struct wuffs_xxhash32__hasher; + +#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_xxhash32__hasher__initialize( + wuffs_xxhash32__hasher* self, + size_t sizeof_star_self, + uint64_t wuffs_version, + uint32_t options); + +size_t +sizeof__wuffs_xxhash32__hasher(); + +// ---------------- 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. + +wuffs_xxhash32__hasher* +wuffs_xxhash32__hasher__alloc(); + +static inline wuffs_base__hasher_u32* +wuffs_xxhash32__hasher__alloc_as__wuffs_base__hasher_u32() { + return (wuffs_base__hasher_u32*)(wuffs_xxhash32__hasher__alloc()); +} + +// ---------------- Upcasts + +static inline wuffs_base__hasher_u32* +wuffs_xxhash32__hasher__upcast_as__wuffs_base__hasher_u32( + wuffs_xxhash32__hasher* p) { + return (wuffs_base__hasher_u32*)p; +} + +// ---------------- Public Function Prototypes + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_xxhash32__hasher__get_quirk( + const wuffs_xxhash32__hasher* self, + uint32_t a_key); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_xxhash32__hasher__set_quirk( + wuffs_xxhash32__hasher* self, + uint32_t a_key, + uint64_t a_value); + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC uint32_t +wuffs_xxhash32__hasher__update_u32( + wuffs_xxhash32__hasher* self, + wuffs_base__slice_u8 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_xxhash32__hasher__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__hasher_u32; + wuffs_base__vtable null_vtable; + + uint32_t f_length_modulo_u32; + bool f_length_overflows_u32; + uint8_t f_padding0; + uint8_t f_padding1; + uint8_t f_buf_len; + uint8_t f_buf_data[16]; + uint32_t f_v0; + uint32_t f_v1; + uint32_t f_v2; + uint32_t f_v3; + } private_impl; + +#ifdef __cplusplus +#if defined(WUFFS_BASE__HAVE_UNIQUE_PTR) + using unique_ptr = std::unique_ptr; + + // On failure, the alloc_etc functions return nullptr. They don't throw. + + static inline unique_ptr + alloc() { + return unique_ptr(wuffs_xxhash32__hasher__alloc()); + } + + static inline wuffs_base__hasher_u32::unique_ptr + alloc_as__wuffs_base__hasher_u32() { + return wuffs_base__hasher_u32::unique_ptr( + wuffs_xxhash32__hasher__alloc_as__wuffs_base__hasher_u32()); + } +#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_xxhash32__hasher__struct() = delete; + wuffs_xxhash32__hasher__struct(const wuffs_xxhash32__hasher__struct&) = delete; + wuffs_xxhash32__hasher__struct& operator=( + const wuffs_xxhash32__hasher__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_xxhash32__hasher__initialize( + this, sizeof_star_self, wuffs_version, options); + } + + inline wuffs_base__hasher_u32* + upcast_as__wuffs_base__hasher_u32() { + return (wuffs_base__hasher_u32*)this; + } + + inline uint64_t + get_quirk( + uint32_t a_key) const { + return wuffs_xxhash32__hasher__get_quirk(this, a_key); + } + + inline wuffs_base__status + set_quirk( + uint32_t a_key, + uint64_t a_value) { + return wuffs_xxhash32__hasher__set_quirk(this, a_key, a_value); + } + + inline uint32_t + update_u32( + wuffs_base__slice_u8 a_x) { + return wuffs_xxhash32__hasher__update_u32(this, a_x); + } + +#endif // __cplusplus +}; // struct wuffs_xxhash32__hasher__struct + +#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) + +#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__XXHASH32) || defined(WUFFS_NONMONOLITHIC) + #if defined(__cplusplus) && defined(WUFFS_BASE__HAVE_UNIQUE_PTR) // ---------------- Auxiliary - Base @@ -60488,6 +60692,394 @@ wuffs_wbmp__decoder__workbuf_len( #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__WBMP) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__XXHASH32) + +// ---------------- Status Codes Implementations + +// ---------------- Private Consts + +#define WUFFS_XXHASH32__XXH_PRIME32_1 2654435761 + +#define WUFFS_XXHASH32__XXH_PRIME32_2 2246822519 + +#define WUFFS_XXHASH32__XXH_PRIME32_3 3266489917 + +#define WUFFS_XXHASH32__XXH_PRIME32_4 668265263 + +#define WUFFS_XXHASH32__XXH_PRIME32_5 374761393 + +#define WUFFS_XXHASH32__INITIAL_V0 606290984 + +#define WUFFS_XXHASH32__INITIAL_V1 2246822519 + +#define WUFFS_XXHASH32__INITIAL_V2 0 + +#define WUFFS_XXHASH32__INITIAL_V3 1640531535 + +// ---------------- Private Initializer Prototypes + +// ---------------- Private Function Prototypes + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_xxhash32__hasher__up( + wuffs_xxhash32__hasher* self, + wuffs_base__slice_u8 a_x); + +WUFFS_BASE__GENERATED_C_CODE +static uint32_t +wuffs_xxhash32__hasher__checksum_u32( + const wuffs_xxhash32__hasher* self); + +// ---------------- VTables + +const wuffs_base__hasher_u32__func_ptrs +wuffs_xxhash32__hasher__func_ptrs_for__wuffs_base__hasher_u32 = { + (uint64_t(*)(const void*, + uint32_t))(&wuffs_xxhash32__hasher__get_quirk), + (wuffs_base__status(*)(void*, + uint32_t, + uint64_t))(&wuffs_xxhash32__hasher__set_quirk), + (uint32_t(*)(void*, + wuffs_base__slice_u8))(&wuffs_xxhash32__hasher__update_u32), +}; + +// ---------------- Initializer Implementations + +wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT +wuffs_xxhash32__hasher__initialize( + wuffs_xxhash32__hasher* 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; + self->private_impl.vtable_for__wuffs_base__hasher_u32.vtable_name = + wuffs_base__hasher_u32__vtable_name; + self->private_impl.vtable_for__wuffs_base__hasher_u32.function_pointers = + (const void*)(&wuffs_xxhash32__hasher__func_ptrs_for__wuffs_base__hasher_u32); + return wuffs_base__make_status(NULL); +} + +wuffs_xxhash32__hasher* +wuffs_xxhash32__hasher__alloc() { + wuffs_xxhash32__hasher* x = + (wuffs_xxhash32__hasher*)(calloc(sizeof(wuffs_xxhash32__hasher), 1)); + if (!x) { + return NULL; + } + if (wuffs_xxhash32__hasher__initialize( + x, sizeof(wuffs_xxhash32__hasher), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) { + free(x); + return NULL; + } + return x; +} + +size_t +sizeof__wuffs_xxhash32__hasher() { + return sizeof(wuffs_xxhash32__hasher); +} + +// ---------------- Function Implementations + +// -------- func xxhash32.hasher.get_quirk + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_xxhash32__hasher__get_quirk( + const wuffs_xxhash32__hasher* 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; + } + + return 0u; +} + +// -------- func xxhash32.hasher.set_quirk + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_xxhash32__hasher__set_quirk( + wuffs_xxhash32__hasher* 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); + } + + return wuffs_base__make_status(wuffs_base__error__unsupported_option); +} + +// -------- func xxhash32.hasher.update_u32 + +WUFFS_BASE__GENERATED_C_CODE +WUFFS_BASE__MAYBE_STATIC uint32_t +wuffs_xxhash32__hasher__update_u32( + wuffs_xxhash32__hasher* self, + wuffs_base__slice_u8 a_x) { + if (!self) { + return 0; + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return 0; + } + + wuffs_base__slice_u8 v_remaining = {0}; + + if ((self->private_impl.f_length_modulo_u32 == 0u) && ! self->private_impl.f_length_overflows_u32) { + self->private_impl.f_v0 = 606290984u; + self->private_impl.f_v1 = 2246822519u; + self->private_impl.f_v2 = 0u; + self->private_impl.f_v3 = 1640531535u; + } + while (((uint64_t)(a_x.len)) > 0u) { + v_remaining = wuffs_base__slice_u8__subslice_j(a_x, 0u); + if (((uint64_t)(a_x.len)) > 16777216u) { + v_remaining = wuffs_base__slice_u8__subslice_i(a_x, 16777216u); + a_x = wuffs_base__slice_u8__subslice_j(a_x, 16777216u); + } + wuffs_xxhash32__hasher__up(self, a_x); + a_x = v_remaining; + } + return wuffs_xxhash32__hasher__checksum_u32(self); +} + +// -------- func xxhash32.hasher.up + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_xxhash32__hasher__up( + wuffs_xxhash32__hasher* self, + wuffs_base__slice_u8 a_x) { + uint32_t v_new_lmu = 0; + uint32_t v_buf_u32 = 0; + uint32_t v_buf_len = 0; + uint32_t v_v0 = 0; + uint32_t v_v1 = 0; + uint32_t v_v2 = 0; + uint32_t v_v3 = 0; + wuffs_base__slice_u8 v_p = {0}; + + v_new_lmu = ((uint32_t)(self->private_impl.f_length_modulo_u32 + ((uint32_t)(((uint64_t)(a_x.len)))))); + self->private_impl.f_length_overflows_u32 = ((v_new_lmu < self->private_impl.f_length_modulo_u32) || self->private_impl.f_length_overflows_u32); + self->private_impl.f_length_modulo_u32 = v_new_lmu; + while (true) { + if (self->private_impl.f_buf_len >= 16u) { + v_buf_u32 = (((uint32_t)(self->private_impl.f_buf_data[0u])) | + (((uint32_t)(self->private_impl.f_buf_data[1u])) << 8u) | + (((uint32_t)(self->private_impl.f_buf_data[2u])) << 16u) | + (((uint32_t)(self->private_impl.f_buf_data[3u])) << 24u)); + v_v0 = ((uint32_t)(self->private_impl.f_v0 + ((uint32_t)(v_buf_u32 * 2246822519u)))); + v_v0 = (((uint32_t)(v_v0 << 13u)) | (v_v0 >> 19u)); + self->private_impl.f_v0 = ((uint32_t)(v_v0 * 2654435761u)); + v_buf_u32 = (((uint32_t)(self->private_impl.f_buf_data[4u])) | + (((uint32_t)(self->private_impl.f_buf_data[5u])) << 8u) | + (((uint32_t)(self->private_impl.f_buf_data[6u])) << 16u) | + (((uint32_t)(self->private_impl.f_buf_data[7u])) << 24u)); + v_v1 = ((uint32_t)(self->private_impl.f_v1 + ((uint32_t)(v_buf_u32 * 2246822519u)))); + v_v1 = (((uint32_t)(v_v1 << 13u)) | (v_v1 >> 19u)); + self->private_impl.f_v1 = ((uint32_t)(v_v1 * 2654435761u)); + v_buf_u32 = (((uint32_t)(self->private_impl.f_buf_data[8u])) | + (((uint32_t)(self->private_impl.f_buf_data[9u])) << 8u) | + (((uint32_t)(self->private_impl.f_buf_data[10u])) << 16u) | + (((uint32_t)(self->private_impl.f_buf_data[11u])) << 24u)); + v_v2 = ((uint32_t)(self->private_impl.f_v2 + ((uint32_t)(v_buf_u32 * 2246822519u)))); + v_v2 = (((uint32_t)(v_v2 << 13u)) | (v_v2 >> 19u)); + self->private_impl.f_v2 = ((uint32_t)(v_v2 * 2654435761u)); + v_buf_u32 = (((uint32_t)(self->private_impl.f_buf_data[12u])) | + (((uint32_t)(self->private_impl.f_buf_data[13u])) << 8u) | + (((uint32_t)(self->private_impl.f_buf_data[14u])) << 16u) | + (((uint32_t)(self->private_impl.f_buf_data[15u])) << 24u)); + v_v3 = ((uint32_t)(self->private_impl.f_v3 + ((uint32_t)(v_buf_u32 * 2246822519u)))); + v_v3 = (((uint32_t)(v_v3 << 13u)) | (v_v3 >> 19u)); + self->private_impl.f_v3 = ((uint32_t)(v_v3 * 2654435761u)); + self->private_impl.f_buf_len = 0u; + break; + } + if (((uint64_t)(a_x.len)) <= 0u) { + return wuffs_base__make_empty_struct(); + } + self->private_impl.f_buf_data[self->private_impl.f_buf_len] = a_x.ptr[0u]; +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + self->private_impl.f_buf_len += 1u; +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + a_x = wuffs_base__slice_u8__subslice_i(a_x, 1u); + } + v_buf_len = ((uint32_t)((self->private_impl.f_buf_len & 15u))); + v_v0 = self->private_impl.f_v0; + v_v1 = self->private_impl.f_v1; + v_v2 = self->private_impl.f_v2; + v_v3 = self->private_impl.f_v3; + { + wuffs_base__slice_u8 i_slice_p = a_x; + v_p.ptr = i_slice_p.ptr; + v_p.len = 16; + uint8_t* i_end0_p = v_p.ptr + (((i_slice_p.len - (size_t)(v_p.ptr - i_slice_p.ptr)) / 16) * 16); + while (v_p.ptr < i_end0_p) { + v_buf_u32 = (((uint32_t)(v_p.ptr[0u])) | + (((uint32_t)(v_p.ptr[1u])) << 8u) | + (((uint32_t)(v_p.ptr[2u])) << 16u) | + (((uint32_t)(v_p.ptr[3u])) << 24u)); + v_v0 = ((uint32_t)(v_v0 + ((uint32_t)(v_buf_u32 * 2246822519u)))); + v_v0 = (((uint32_t)(v_v0 << 13u)) | (v_v0 >> 19u)); + v_v0 = ((uint32_t)(v_v0 * 2654435761u)); + v_buf_u32 = (((uint32_t)(v_p.ptr[4u])) | + (((uint32_t)(v_p.ptr[5u])) << 8u) | + (((uint32_t)(v_p.ptr[6u])) << 16u) | + (((uint32_t)(v_p.ptr[7u])) << 24u)); + v_v1 = ((uint32_t)(v_v1 + ((uint32_t)(v_buf_u32 * 2246822519u)))); + v_v1 = (((uint32_t)(v_v1 << 13u)) | (v_v1 >> 19u)); + v_v1 = ((uint32_t)(v_v1 * 2654435761u)); + v_buf_u32 = (((uint32_t)(v_p.ptr[8u])) | + (((uint32_t)(v_p.ptr[9u])) << 8u) | + (((uint32_t)(v_p.ptr[10u])) << 16u) | + (((uint32_t)(v_p.ptr[11u])) << 24u)); + v_v2 = ((uint32_t)(v_v2 + ((uint32_t)(v_buf_u32 * 2246822519u)))); + v_v2 = (((uint32_t)(v_v2 << 13u)) | (v_v2 >> 19u)); + v_v2 = ((uint32_t)(v_v2 * 2654435761u)); + v_buf_u32 = (((uint32_t)(v_p.ptr[12u])) | + (((uint32_t)(v_p.ptr[13u])) << 8u) | + (((uint32_t)(v_p.ptr[14u])) << 16u) | + (((uint32_t)(v_p.ptr[15u])) << 24u)); + v_v3 = ((uint32_t)(v_v3 + ((uint32_t)(v_buf_u32 * 2246822519u)))); + v_v3 = (((uint32_t)(v_v3 << 13u)) | (v_v3 >> 19u)); + v_v3 = ((uint32_t)(v_v3 * 2654435761u)); + v_p.ptr += 16; + } + v_p.len = 1; + uint8_t* i_end1_p = i_slice_p.ptr + i_slice_p.len; + while (v_p.ptr < i_end1_p) { + self->private_impl.f_buf_data[v_buf_len] = v_p.ptr[0u]; + v_buf_len = ((v_buf_len + 1u) & 15u); + v_p.ptr += 1; + } + v_p.len = 0; + } + self->private_impl.f_buf_len = ((uint8_t)(v_buf_len)); + self->private_impl.f_v0 = v_v0; + self->private_impl.f_v1 = v_v1; + self->private_impl.f_v2 = v_v2; + self->private_impl.f_v3 = v_v3; + return wuffs_base__make_empty_struct(); +} + +// -------- func xxhash32.hasher.checksum_u32 + +WUFFS_BASE__GENERATED_C_CODE +static uint32_t +wuffs_xxhash32__hasher__checksum_u32( + const wuffs_xxhash32__hasher* self) { + uint32_t v_ret = 0; + uint32_t v_i = 0; + uint32_t v_n = 0; + uint32_t v_buf_u32 = 0; + + v_ret = self->private_impl.f_length_modulo_u32; + if ((self->private_impl.f_length_modulo_u32 >= 16u) || self->private_impl.f_length_overflows_u32) { + v_ret += (((uint32_t)(self->private_impl.f_v0 << 1u)) | (self->private_impl.f_v0 >> 31u)); + v_ret += (((uint32_t)(self->private_impl.f_v1 << 7u)) | (self->private_impl.f_v1 >> 25u)); + v_ret += (((uint32_t)(self->private_impl.f_v2 << 12u)) | (self->private_impl.f_v2 >> 20u)); + v_ret += (((uint32_t)(self->private_impl.f_v3 << 18u)) | (self->private_impl.f_v3 >> 14u)); + } else { + v_ret += 374761393u; + } + v_n = 16u; + v_n = wuffs_base__u32__min(v_n, ((uint32_t)(self->private_impl.f_buf_len))); + if (4u <= v_n) { + v_buf_u32 = (((uint32_t)(self->private_impl.f_buf_data[0u])) | + (((uint32_t)(self->private_impl.f_buf_data[1u])) << 8u) | + (((uint32_t)(self->private_impl.f_buf_data[2u])) << 16u) | + (((uint32_t)(self->private_impl.f_buf_data[3u])) << 24u)); + v_ret += ((uint32_t)(v_buf_u32 * 3266489917u)); + v_ret = (((uint32_t)(v_ret << 17u)) | (v_ret >> 15u)); + v_ret *= 668265263u; + v_i = 4u; + } + if (8u <= v_n) { + v_buf_u32 = (((uint32_t)(self->private_impl.f_buf_data[4u])) | + (((uint32_t)(self->private_impl.f_buf_data[5u])) << 8u) | + (((uint32_t)(self->private_impl.f_buf_data[6u])) << 16u) | + (((uint32_t)(self->private_impl.f_buf_data[7u])) << 24u)); + v_ret += ((uint32_t)(v_buf_u32 * 3266489917u)); + v_ret = (((uint32_t)(v_ret << 17u)) | (v_ret >> 15u)); + v_ret *= 668265263u; + v_i = 8u; + } + if (12u <= v_n) { + v_buf_u32 = (((uint32_t)(self->private_impl.f_buf_data[8u])) | + (((uint32_t)(self->private_impl.f_buf_data[9u])) << 8u) | + (((uint32_t)(self->private_impl.f_buf_data[10u])) << 16u) | + (((uint32_t)(self->private_impl.f_buf_data[11u])) << 24u)); + v_ret += ((uint32_t)(v_buf_u32 * 3266489917u)); + v_ret = (((uint32_t)(v_ret << 17u)) | (v_ret >> 15u)); + v_ret *= 668265263u; + v_i = 12u; + } + while (v_i < v_n) { + v_ret += ((uint32_t)(((uint32_t)(self->private_impl.f_buf_data[v_i])) * 374761393u)); + v_ret = (((uint32_t)(v_ret << 11u)) | (v_ret >> 21u)); + v_ret *= 2654435761u; + v_i += 1u; + } + v_ret ^= (v_ret >> 15u); + v_ret *= 2246822519u; + v_ret ^= (v_ret >> 13u); + v_ret *= 3266489917u; + v_ret ^= (v_ret >> 16u); + return v_ret; +} + +#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__XXHASH32) + #if defined(__cplusplus) && defined(WUFFS_BASE__HAVE_UNIQUE_PTR) // ---------------- Auxiliary - Base diff --git a/script/checksum.go b/script/checksum.go index c58b44a2..68958fb7 100644 --- a/script/checksum.go +++ b/script/checksum.go @@ -18,7 +18,7 @@ package main // checksum.go prints a checksum of stdin's bytes, or of the opening digits of -// π. Checksum algorithms include "adler32" and "crc32/ieee". +// π. Checksum algorithms include "adler32", "crc32/ieee" and "xxhash32". // // Usage: go run checksum.go -algorithm=crc32/ieee < foo.bar @@ -31,6 +31,8 @@ import ( "io" "os" "strings" + + "github.com/pierrec/xxHash/xxHash32" ) var ( @@ -78,6 +80,8 @@ func do(r io.Reader) error { h = adler32.New() case "crc32/ieee": h = crc32.NewIEEE() + case "xxhash32": + h = xxHash32.New(0) default: return fmt.Errorf("unknown algorithm %q", *algorithm) } diff --git a/std/xxhash32/common_xxhash32.wuffs b/std/xxhash32/common_xxhash32.wuffs new file mode 100644 index 00000000..e79418a2 --- /dev/null +++ b/std/xxhash32/common_xxhash32.wuffs @@ -0,0 +1,255 @@ +// Copyright 2023 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pri const XXH_PRIME32_1 : base.u32 = 0x9E37_79B1 +pri const XXH_PRIME32_2 : base.u32 = 0x85EB_CA77 +pri const XXH_PRIME32_3 : base.u32 = 0xC2B2_AE3D +pri const XXH_PRIME32_4 : base.u32 = 0x27D4_EB2F +pri const XXH_PRIME32_5 : base.u32 = 0x1656_67B1 + +pri const INITIAL_V0 : base.u32 = 0x2423_4428 // 0 + XXH_PRIME32_1 + XXH_PRIME32_2 +pri const INITIAL_V1 : base.u32 = 0x85EB_CA77 // 0 + XXH_PRIME32_2 +pri const INITIAL_V2 : base.u32 = 0x0000_0000 // 0 +pri const INITIAL_V3 : base.u32 = 0x61C8_864F // 0 - XXH_PRIME32_1 + +pub struct hasher? implements base.hasher_u32( + length_modulo_u32 : base.u32, + length_overflows_u32 : base.bool, + + padding0 : base.u8, + padding1 : base.u8, + + buf_len : base.u8[..= 16], + buf_data : array[16] base.u8, + + v0 : base.u32, + v1 : base.u32, + v2 : base.u32, + v3 : base.u32, +) + +pub func hasher.get_quirk(key: base.u32) base.u64 { + return 0 +} + +pub func hasher.set_quirk!(key: base.u32, value: base.u64) base.status { + return base."#unsupported option" +} + +pub func hasher.update_u32!(x: roslice base.u8) base.u32 { + var remaining : roslice base.u8 + + if (this.length_modulo_u32 == 0) and not this.length_overflows_u32 { + this.v0 = INITIAL_V0 + this.v1 = INITIAL_V1 + this.v2 = INITIAL_V2 + this.v3 = INITIAL_V3 + } + + while args.x.length() > 0 { + remaining = args.x[.. 0] + if args.x.length() > 0x100_0000 { + remaining = args.x[0x100_0000 ..] + args.x = args.x[.. 0x100_0000] + } + + this.up!(x: args.x) + + args.x = remaining + } endwhile + + return this.checksum_u32() +} + +pri func hasher.up!(x: roslice base.u8) { + var new_lmu : base.u32 + var buf_u32 : base.u32 + var buf_len : base.u32[..= 15] + var v0 : base.u32 + var v1 : base.u32 + var v2 : base.u32 + var v3 : base.u32 + var p : roslice base.u8 + + new_lmu = this.length_modulo_u32 ~mod+ ((args.x.length() & 0xFFFF_FFFF) as base.u32) + this.length_overflows_u32 = (new_lmu < this.length_modulo_u32) or this.length_overflows_u32 + this.length_modulo_u32 = new_lmu + + while true { + if this.buf_len >= 16 { + buf_u32 = (this.buf_data[0x00] as base.u32) | + ((this.buf_data[0x01] as base.u32) << 8) | + ((this.buf_data[0x02] as base.u32) << 16) | + ((this.buf_data[0x03] as base.u32) << 24) + v0 = this.v0 ~mod+ (buf_u32 ~mod* XXH_PRIME32_2) + v0 = (v0 ~mod<< 13) | (v0 >> 19) + this.v0 = v0 ~mod* XXH_PRIME32_1 + + buf_u32 = (this.buf_data[0x04] as base.u32) | + ((this.buf_data[0x05] as base.u32) << 8) | + ((this.buf_data[0x06] as base.u32) << 16) | + ((this.buf_data[0x07] as base.u32) << 24) + v1 = this.v1 ~mod+ (buf_u32 ~mod* XXH_PRIME32_2) + v1 = (v1 ~mod<< 13) | (v1 >> 19) + this.v1 = v1 ~mod* XXH_PRIME32_1 + + buf_u32 = (this.buf_data[0x08] as base.u32) | + ((this.buf_data[0x09] as base.u32) << 8) | + ((this.buf_data[0x0A] as base.u32) << 16) | + ((this.buf_data[0x0B] as base.u32) << 24) + v2 = this.v2 ~mod+ (buf_u32 ~mod* XXH_PRIME32_2) + v2 = (v2 ~mod<< 13) | (v2 >> 19) + this.v2 = v2 ~mod* XXH_PRIME32_1 + + buf_u32 = (this.buf_data[0x0C] as base.u32) | + ((this.buf_data[0x0D] as base.u32) << 8) | + ((this.buf_data[0x0E] as base.u32) << 16) | + ((this.buf_data[0x0F] as base.u32) << 24) + v3 = this.v3 ~mod+ (buf_u32 ~mod* XXH_PRIME32_2) + v3 = (v3 ~mod<< 13) | (v3 >> 19) + this.v3 = v3 ~mod* XXH_PRIME32_1 + + this.buf_len = 0 + break + } + + if args.x.length() <= 0 { + return nothing + } + this.buf_data[this.buf_len] = args.x[0] + this.buf_len += 1 + args.x = args.x[1 ..] + } endwhile + + buf_len = (this.buf_len & 15) as base.u32 + v0 = this.v0 + v1 = this.v1 + v2 = this.v2 + v3 = this.v3 + + iterate (p = args.x)(length: 16, advance: 16, unroll: 1) { + buf_u32 = (p[0x00] as base.u32) | + ((p[0x01] as base.u32) << 8) | + ((p[0x02] as base.u32) << 16) | + ((p[0x03] as base.u32) << 24) + v0 = v0 ~mod+ (buf_u32 ~mod* XXH_PRIME32_2) + v0 = (v0 ~mod<< 13) | (v0 >> 19) + v0 = v0 ~mod* XXH_PRIME32_1 + + buf_u32 = (p[0x04] as base.u32) | + ((p[0x05] as base.u32) << 8) | + ((p[0x06] as base.u32) << 16) | + ((p[0x07] as base.u32) << 24) + v1 = v1 ~mod+ (buf_u32 ~mod* XXH_PRIME32_2) + v1 = (v1 ~mod<< 13) | (v1 >> 19) + v1 = v1 ~mod* XXH_PRIME32_1 + + buf_u32 = (p[0x08] as base.u32) | + ((p[0x09] as base.u32) << 8) | + ((p[0x0A] as base.u32) << 16) | + ((p[0x0B] as base.u32) << 24) + v2 = v2 ~mod+ (buf_u32 ~mod* XXH_PRIME32_2) + v2 = (v2 ~mod<< 13) | (v2 >> 19) + v2 = v2 ~mod* XXH_PRIME32_1 + + buf_u32 = (p[0x0C] as base.u32) | + ((p[0x0D] as base.u32) << 8) | + ((p[0x0E] as base.u32) << 16) | + ((p[0x0F] as base.u32) << 24) + v3 = v3 ~mod+ (buf_u32 ~mod* XXH_PRIME32_2) + v3 = (v3 ~mod<< 13) | (v3 >> 19) + v3 = v3 ~mod* XXH_PRIME32_1 + + } else (length: 1, advance: 1, unroll: 1) { + this.buf_data[buf_len] = p[0] + buf_len = (buf_len + 1) & 15 + } + + this.buf_len = buf_len as base.u8 + this.v0 = v0 + this.v1 = v1 + this.v2 = v2 + this.v3 = v3 +} + +pri func hasher.checksum_u32() base.u32 { + var ret : base.u32 + var i : base.u32[..= 16] + var n : base.u32[..= 16] + var buf_u32 : base.u32 + + ret = this.length_modulo_u32 + if (this.length_modulo_u32 >= 16) or this.length_overflows_u32 { + ret ~mod+= (this.v0 ~mod<< 1) | (this.v0 >> 31) + ret ~mod+= (this.v1 ~mod<< 7) | (this.v1 >> 25) + ret ~mod+= (this.v2 ~mod<< 12) | (this.v2 >> 20) + ret ~mod+= (this.v3 ~mod<< 18) | (this.v3 >> 14) + } else { + ret ~mod+= XXH_PRIME32_5 + } + + n = 16 + n = n.min(no_more_than: this.buf_len as base.u32) + + if 0x04 <= n { + buf_u32 = (this.buf_data[0x00] as base.u32) | + ((this.buf_data[0x01] as base.u32) << 8) | + ((this.buf_data[0x02] as base.u32) << 16) | + ((this.buf_data[0x03] as base.u32) << 24) + ret ~mod+= buf_u32 ~mod* XXH_PRIME32_3 + ret = (ret ~mod<< 17) | (ret >> 15) + ret ~mod*= XXH_PRIME32_4 + i = 0x04 + } + + if 0x08 <= n { + buf_u32 = (this.buf_data[0x04] as base.u32) | + ((this.buf_data[0x05] as base.u32) << 8) | + ((this.buf_data[0x06] as base.u32) << 16) | + ((this.buf_data[0x07] as base.u32) << 24) + ret ~mod+= buf_u32 ~mod* XXH_PRIME32_3 + ret = (ret ~mod<< 17) | (ret >> 15) + ret ~mod*= XXH_PRIME32_4 + i = 0x08 + } + + if 0x0C <= n { + buf_u32 = (this.buf_data[0x08] as base.u32) | + ((this.buf_data[0x09] as base.u32) << 8) | + ((this.buf_data[0x0A] as base.u32) << 16) | + ((this.buf_data[0x0B] as base.u32) << 24) + ret ~mod+= buf_u32 ~mod* XXH_PRIME32_3 + ret = (ret ~mod<< 17) | (ret >> 15) + ret ~mod*= XXH_PRIME32_4 + i = 0x0C + } + + while i < n { + assert i < 16 via "a < b: a < c; c <= b"(c: n) + + ret ~mod+= (this.buf_data[i] as base.u32) ~mod* XXH_PRIME32_5 + ret = (ret ~mod<< 11) | (ret >> 21) + ret ~mod*= XXH_PRIME32_1 + + i += 1 + } endwhile + + ret ^= ret >> 15 + ret ~mod*= XXH_PRIME32_2 + ret ^= ret >> 13 + ret ~mod*= XXH_PRIME32_3 + ret ^= ret >> 16 + + return ret +} diff --git a/test/c/mimiclib/xxhash32.c b/test/c/mimiclib/xxhash32.c new file mode 100644 index 00000000..1315102f --- /dev/null +++ b/test/c/mimiclib/xxhash32.c @@ -0,0 +1,57 @@ +// Copyright 2023 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "xxhash.h" + +uint32_t global_mimiclib_xxhash32_unused_u32; + +const char* // +mimic_bench_xxhash32(wuffs_base__io_buffer* dst, + wuffs_base__io_buffer* src, + uint32_t wuffs_initialize_flags, + uint64_t wlimit, + uint64_t rlimit) { + XXH32_state_t* hasher = XXH32_createState(); + if (!hasher) { + return "libxxhash: XXH32_createState failed"; + } else if (XXH_OK != XXH32_reset(hasher, 0)) { + return "libxxhash: XXH32_reset failed"; + } + + global_mimiclib_xxhash32_unused_u32 = 0; + while (src->meta.ri < src->meta.wi) { + uint8_t* ptr = src->data.ptr + src->meta.ri; + size_t len = src->meta.wi - src->meta.ri; + if (len > 0x7FFFFFFF) { + return "src length is too large"; + } else if (len > rlimit) { + len = rlimit; + } + if (XXH_OK != XXH32_update(hasher, ptr, len)) { + return "libxxhash: XXH32_update failed"; + } + src->meta.ri += len; + } + global_mimiclib_xxhash32_unused_u32 = XXH32_digest(hasher); + + if (XXH_OK != XXH32_freeState(hasher)) { + return "libxxhash: XXH32_freeState failed"; + } + return NULL; +} + +uint32_t // +mimic_xxhash32_one_shot_checksum_u32(wuffs_base__slice_u8 data) { + return XXH32(data.ptr, data.len, 0); +} diff --git a/test/c/std/xxhash32.c b/test/c/std/xxhash32.c new file mode 100644 index 00000000..c3093311 --- /dev/null +++ b/test/c/std/xxhash32.c @@ -0,0 +1,356 @@ +// Copyright 2023 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ---------------- + +/* +This test program is typically run indirectly, by the "wuffs test" or "wuffs +bench" commands. These commands take an optional "-mimic" flag to check that +Wuffs' output mimics (i.e. exactly matches) other libraries' output, such as +giflib for GIF, libpng for PNG, etc. + +To manually run this test: + +for CC in clang gcc; do + $CC -std=c99 -Wall -Werror xxhash32.c && ./a.out + rm -f a.out +done + +Each edition should print "PASS", amongst other information, and exit(0). + +Add the "wuffs mimic cflags" (everything after the colon below) to the C +compiler flags (after the .c file) to run the mimic tests. + +To manually run the benchmarks, replace "-Wall -Werror" with "-O3" and replace +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 -lxxhash + +// Wuffs ships as a "single file C library" or "header file library" as per +// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt +// +// To use that single file as a "foo.c"-like implementation, instead of a +// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or +// compiling it. +#define WUFFS_IMPLEMENTATION + +// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of +// release/c/etc.c choose which parts of Wuffs to build. That file contains the +// entire Wuffs standard library, implementing a variety of codecs and file +// formats. Without this macro definition, an optimizing compiler or linker may +// very well discard Wuffs code for unused codecs, but listing the Wuffs +// modules we use makes that process explicit. Preprocessing means that such +// code simply isn't compiled. +#define WUFFS_CONFIG__MODULES +#define WUFFS_CONFIG__MODULE__BASE +#define WUFFS_CONFIG__MODULE__XXHASH32 + +// If building this program in an environment that doesn't easily accommodate +// relative includes, you can use the script/inline-c-relative-includes.go +// program to generate a stand-alone C file. +#include "../../../release/c/wuffs-unsupported-snapshot.c" +#include "../testlib/testlib.c" +#ifdef WUFFS_MIMIC +#include "../mimiclib/xxhash32.c" +#endif + +// ---------------- Golden Tests + +golden_test g_xxhash32_midsummer_gt = { + .src_filename = "test/data/midsummer.txt", +}; + +golden_test g_xxhash32_pi_gt = { + .src_filename = "test/data/pi.txt", +}; + +// ---------------- XXHash32 Tests + +const char* // +test_wuffs_xxhash32_interface() { + CHECK_FOCUS(__func__); + wuffs_xxhash32__hasher h; + CHECK_STATUS("initialize", + wuffs_xxhash32__hasher__initialize( + &h, sizeof h, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + return do_test__wuffs_base__hasher_u32( + wuffs_xxhash32__hasher__upcast_as__wuffs_base__hasher_u32(&h), + "test/data/hat.lossy.webp", 0, SIZE_MAX, 0x1A54B53D); +} + +const char* // +test_wuffs_xxhash32_golden() { + CHECK_FOCUS(__func__); + + struct { + const char* filename; + // The want values are determined by script/checksum.go. + uint32_t want; + } test_cases[] = { + { + .filename = "test/data/hat.bmp", + .want = 0xCAD975D7, + }, + { + .filename = "test/data/hat.gif", + .want = 0x27633229, + }, + { + .filename = "test/data/hat.jpeg", + .want = 0xEEF96C12, + }, + { + .filename = "test/data/hat.lossless.webp", + .want = 0xA731CF6A, + }, + { + .filename = "test/data/hat.lossy.webp", + .want = 0x1A54B53D, + }, + { + .filename = "test/data/hat.png", + .want = 0x2EF9D842, + }, + { + .filename = "test/data/hat.tiff", + .want = 0x244C2A7F, + }, + }; + + for (size_t tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) { + wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ + .data = g_src_slice_u8, + }); + CHECK_STRING(read_file(&src, test_cases[tc].filename)); + + for (int j = 0; j < 2; j++) { + wuffs_xxhash32__hasher checksum; + CHECK_STATUS("initialize", + wuffs_xxhash32__hasher__initialize( + &checksum, sizeof checksum, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + + uint32_t have = 0; + size_t num_fragments = 0; + size_t num_bytes = 0; + do { + wuffs_base__slice_u8 data = ((wuffs_base__slice_u8){ + .ptr = src.data.ptr + num_bytes, + .len = src.meta.wi - num_bytes, + }); + size_t limit = 101 + 103 * num_fragments; + if ((j > 0) && (data.len > limit)) { + data.len = limit; + } + have = wuffs_xxhash32__hasher__update_u32(&checksum, data); + num_fragments++; + num_bytes += data.len; + } while (num_bytes < src.meta.wi); + + if (have != test_cases[tc].want) { + RETURN_FAIL("tc=%zu, j=%d, filename=\"%s\": have 0x%08" PRIX32 + ", want 0x%08" PRIX32 "\n", + tc, j, test_cases[tc].filename, have, test_cases[tc].want); + } + } + } + return NULL; +} + +const char* // +do_test_xxxxx_xxhash32_pi(bool mimic) { + const char* digits = + "3." + "141592653589793238462643383279502884197169399375105820974944592307816406" + "2862089986280348253421170"; + if (strlen(digits) != 99) { + RETURN_FAIL("strlen(digits): have %d, want 99", (int)(strlen(digits))); + } + + // The want values are determined by script/checksum.go. + // + // wants[i] is the checksum of the first i bytes of the digits string. + uint32_t wants[100] = { + 0x02CC5D05, 0x9CEC73C4, 0x8882F465, 0x76EB9891, 0x65EE94C3, 0x1D582EB0, + 0x3F23315C, 0xF7876132, 0x5C7905AB, 0xB13CFCB0, 0x249A3696, 0x8DFDDDDE, + 0x074C32E3, 0x01832398, 0x342CC9FD, 0x27DAF5DF, 0xA724DADF, 0x82C243CD, + 0x058657E3, 0x7702E9E9, 0x0BB1F08C, 0x8663CF29, 0x9EE80972, 0x8072A394, + 0x896E216F, 0x2BA14621, 0xC8F505C1, 0xA1E25C52, 0x3775542D, 0x7A89E5C6, + 0xACD02748, 0x6C4406C8, 0x260382A6, 0x6AD6D4BD, 0xB3CC8788, 0xF8DCB125, + 0xA5BBCDFB, 0x82CC4E8C, 0xCDF34B78, 0xD8D22CCE, 0x64C57168, 0xA8DE94FF, + 0x9DD2BAA1, 0x9D44B437, 0x5A136E82, 0x1907E88D, 0x80F7AA44, 0x1DC870E6, + 0xD300C657, 0xC6F80CA0, 0xECA7845A, 0xEA33A5CA, 0x6113E405, 0x8D952878, + 0x08853159, 0x83AD2241, 0x0B776C22, 0x17B74D73, 0x0A5503C1, 0x4BB9F48F, + 0xA044A6F2, 0xC7BD90E6, 0x23B9D53F, 0x512A214F, 0xDA5BF238, 0xCE112793, + 0xD6833E33, 0x28911D30, 0x588E359B, 0xC161984D, 0xD87050E1, 0xDBF9126A, + 0x676E7A0D, 0xEA6AAC3D, 0x5392F46E, 0xC3851030, 0x3714254B, 0x7136006D, + 0xD7683690, 0xDA681B6E, 0x6AE5712A, 0x30CB24D5, 0x9D760EA6, 0x5B0020E6, + 0xDC118CC1, 0xFC764944, 0x27163F53, 0x91DFA8D9, 0x2D3B63BA, 0x3790770D, + 0x9012C9F0, 0xF0F5377B, 0x624B4744, 0xF376E821, 0x8900258A, 0x5E01F292, + 0xE77B80AE, 0x335F4A44, 0x40374C75, 0x7E7BD839, + }; + + for (int i = 0; i < 100; i++) { + uint32_t have = 0; + wuffs_base__slice_u8 data = ((wuffs_base__slice_u8){ + .ptr = (uint8_t*)(digits), + .len = (size_t)(i), + }); + + if (mimic) { +#ifdef WUFFS_MIMIC + have = mimic_xxhash32_one_shot_checksum_u32(data); +#endif // WUFFS_MIMIC + + } else { + wuffs_xxhash32__hasher checksum; + CHECK_STATUS("initialize", + wuffs_xxhash32__hasher__initialize( + &checksum, sizeof checksum, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + have = wuffs_xxhash32__hasher__update_u32(&checksum, data); + } + + if (have != wants[i]) { + RETURN_FAIL("i=%d: have 0x%08" PRIX32 ", want 0x%08" PRIX32, i, have, + wants[i]); + } + } + return NULL; +} + +const char* // +test_wuffs_xxhash32_pi() { + CHECK_FOCUS(__func__); + return do_test_xxxxx_xxhash32_pi(false); +} + +// ---------------- Mimic Tests + +#ifdef WUFFS_MIMIC + +const char* // +test_mimic_xxhash32_pi() { + CHECK_FOCUS(__func__); + return do_test_xxxxx_xxhash32_pi(true); +} + +#endif // WUFFS_MIMIC + +// ---------------- XXHash32 Benches + +uint32_t g_wuffs_xxhash32_unused_u32; + +const char* // +wuffs_bench_xxhash32(wuffs_base__io_buffer* dst, + wuffs_base__io_buffer* src, + uint32_t wuffs_initialize_flags, + uint64_t wlimit, + uint64_t rlimit) { + uint64_t len = src->meta.wi - src->meta.ri; + if (rlimit) { + len = wuffs_base__u64__min(len, rlimit); + } + wuffs_xxhash32__hasher checksum = {0}; + CHECK_STATUS("initialize", wuffs_xxhash32__hasher__initialize( + &checksum, sizeof checksum, WUFFS_VERSION, + wuffs_initialize_flags)); + g_wuffs_xxhash32_unused_u32 = wuffs_xxhash32__hasher__update_u32( + &checksum, ((wuffs_base__slice_u8){ + .ptr = src->data.ptr + src->meta.ri, + .len = len, + })); + src->meta.ri += len; + return NULL; +} + +const char* // +bench_wuffs_xxhash32_10k() { + CHECK_FOCUS(__func__); + return do_bench_io_buffers( + wuffs_bench_xxhash32, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED, tcounter_src, + &g_xxhash32_midsummer_gt, UINT64_MAX, UINT64_MAX, 5000); +} + +const char* // +bench_wuffs_xxhash32_100k() { + CHECK_FOCUS(__func__); + return do_bench_io_buffers( + wuffs_bench_xxhash32, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED, tcounter_src, + &g_xxhash32_pi_gt, UINT64_MAX, UINT64_MAX, 500); +} + +// ---------------- Mimic Benches + +#ifdef WUFFS_MIMIC + +const char* // +bench_mimic_xxhash32_10k() { + CHECK_FOCUS(__func__); + return do_bench_io_buffers(mimic_bench_xxhash32, 0, tcounter_src, + &g_xxhash32_midsummer_gt, UINT64_MAX, UINT64_MAX, + 5000); +} + +const char* // +bench_mimic_xxhash32_100k() { + CHECK_FOCUS(__func__); + return do_bench_io_buffers(mimic_bench_xxhash32, 0, tcounter_src, + &g_xxhash32_pi_gt, UINT64_MAX, UINT64_MAX, 500); +} + +#endif // WUFFS_MIMIC + +// ---------------- Manifest + +proc g_tests[] = { + + test_wuffs_xxhash32_golden, + test_wuffs_xxhash32_interface, + test_wuffs_xxhash32_pi, + +#ifdef WUFFS_MIMIC + + test_mimic_xxhash32_pi, + +#endif // WUFFS_MIMIC + + NULL, +}; + +proc g_benches[] = { + + bench_wuffs_xxhash32_10k, + bench_wuffs_xxhash32_100k, + +#ifdef WUFFS_MIMIC + + bench_mimic_xxhash32_10k, + bench_mimic_xxhash32_100k, + +#endif // WUFFS_MIMIC + + NULL, +}; + +int // +main(int argc, char** argv) { + g_proc_package_name = "std/xxhash32"; + return test_main(argc, argv, g_tests, g_benches); +}