1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! One-time runtime dispatch between the hardware CRC32C backends
//! ([`super::x86`], [`super::aarch64`]) and the portable scalar fallback
//! ([`super::scalar`]).
//!
//! # Cached bool, not a cached function pointer
//!
//! Two dispatch shapes were measured before choosing one: a
//! [`std::sync::OnceLock<bool>`] read every call, branching directly to
//! whichever backend function that resolves to, versus a
//! [`std::sync::OnceLock`] holding a resolved `fn(u32, &[u8]) -> u32`,
//! called indirectly every call. A release-profile (`opt-level = 3`,
//! `lto = true`, `codegen-units = 1`) microbenchmark of both, dispatching to
//! one of two `#[inline(never)]` backends on a 10-byte input (the size where
//! per-call dispatch overhead is largest relative to the work done — this
//! crate's own frame header is exactly 10 bytes, see `benches/crc32c.rs`),
//! measured the cached-bool branch consistently faster: ~6.2–6.5 ns/iter
//! against ~6.7–6.8 ns/iter for the function-pointer form, stable across
//! repeated rounds. This matches the general expectation for a two-way,
//! almost-always-same-outcome branch: a direct conditional branch is
//! trivial for the branch predictor after warmup, while an indirect call
//! through a function pointer cannot be inlined or spoken for by the same
//! prediction machinery. [`super::x86::is_available`] and
//! [`super::aarch64::is_available`] each cache their own detection result
//! the same way, for the same reason.
use update_state_scalar;
use aarch64;
use x86;
/// Folds `data` into the running, non-finalised CRC register `state`,
/// picking the fastest backend the current CPU supports: hardware CRC32C
/// (SSE4.2 on x86_64, the ARMv8 CRC extension on aarch64) when the runtime
/// feature check passes, the portable slice-by-8 table kernel otherwise.
///
/// This is the sole entry point [`super::Crc32c`]/[`super::crc32c`]/
/// [`super::crc32c_append`] use — the public API's behaviour (including its
/// `const`-ness where already `const`) is unchanged by which backend
/// actually runs; only the wall-clock cost differs.
pub