Bytes — fast unaligned reads, writes, and bulk ops on ByteArray #
The functions in this module are declared @[extern] and route to
small C functions in c/bytes_ffi.c at runtime. The Lean bodies remain
the spec for kernel reduction and proofs: any in-bounds offset must
produce the same result either way.
Out-of-bounds semantics #
The whole module avoids panics; OOB is handled as a documented default:
- Loads are partial: each byte beyond
data.sizereads as0, inheriting theByteArray.get!runtime behaviour. So aloadU64LEwhose last few bytes lie past the end returns the in-bounds prefix with the missing high bytes zeroed. - Stores are atomic: if any byte of the requested write would
fall outside
data.size, the call is a no-op anddatais returned unchanged. No partial writes. - Bulk ops (
copy,fill) are atomic in the same sense;equalreturnsfalsewhen ranges don't fit,comparereturns.eq(a safe but information-losing default — callers that care must bounds-check).
Reads #
Each loadU{N}{LE,BE} returns a UInt{N} of the natural width. To
zero-extend to a wider type, use .toUInt64 etc. at the call site.
Read one byte from data at byte off. Returns 0 if OOB.
Equations
- Bytes.loadU8 data off = data.get! off
Instances For
Read a little-endian 32-bit word at byte off.
Equations
Instances For
Read a big-endian 32-bit word at byte off.
Equations
Instances For
Read a little-endian 64-bit word at byte off.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Read a big-endian 64-bit word at byte off.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Writes #
Stores take data by value, return the modified ByteArray, and
preserve linearity at runtime: when the C function is given an exclusive
reference it mutates in place; otherwise it copies once and mutates
the copy. Atomic OOB semantics — any out-of-bounds byte makes the
whole call a no-op.
Write a little-endian 16-bit word at off. No-op if off+2 > data.size.
Equations
Instances For
Write a big-endian 16-bit word at off. No-op if off+2 > data.size.
Equations
Instances For
Bulk operations #
Backed by memmove, memset, memcmp on the C side. All four are
atomic w.r.t. OOB: if the requested range exceeds either array's
size, copy/fill no-op, equal returns false, compare returns
.eq.
Copy len bytes from src starting at srcOff into dst
starting at dstOff. Aliasing is handled (uses memmove semantics
on the C side). No-op if either range is OOB.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Fill len bytes of data starting at off with val. No-op if off+len > data.size.
Equations
Instances For
Byte-equal: true iff a[aOff..aOff+len] = b[bOff..bOff+len].
Returns false if either range is OOB. The Lean spec walks the full
range with an && accumulator (no early exit); the C runtime uses
memcmp. Same observable result.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Lexicographic byte compare of two ranges. Returns .eq as the safe
default if either range is OOB — callers that care must bounds-check.
The spec walks the full range, keeping the first non-.eq decision; C
side uses memcmp and exits early. Same observable result.
Equations
- One or more equations did not get rendered due to their size.