pub unsafe extern "C" fn logos_rt_memmem(
haystack_ptr: i64,
haystack_len: i64,
needle_ptr: i64,
needle_buf_len: i64,
needle_len: i64,
start: i64,
_bound_unused: i64,
) -> i64Expand description
Count OVERLAPPING occurrences of an ASCII needle in an ASCII haystack,
reproducing the LOGOS naive-search nest BIT-IDENTICALLY (the string_search
benchmark idiom). The recognizer in adapt_region collapses the per-byte
nested-loop region to a single call to this helper.
Parameters mirror the nest’s live values at region entry:
haystack_ptr/haystack_len: the pinnedtextbyte buffer (char index == byte index for the ASCII pin) andlength of text.needle_ptr/needle_buf_len: the pinnedneedlebyte buffer and its actual length (the pinned len slot).needle_len: the PROGRAM’sneedleLenvalue (the inner loop bound), which may differ fromneedle_buf_len.start: the 1-based outer indexiat region entry.
Semantics, matching the nest while i <= textLen - needleLen + 1 { … }:
- the outer bound is
haystack_len - needle_len + 1(recomputed here from the SAME inputs the nest used, so it agrees exactly); - a position
i(1-based) counts iffhaystack[i-1 + j] == needle[j]for alljin[0, needle_len)(0-based byte compare); - an empty needle (
needle_len == 0) matches at EVERY outer position; - a needle longer than the haystack (bound
< start) yields zero; - if a needle index
jwould read pastneedle_buf_len(i.e.needle_len > needle_buf_len, the nest’s CHECKEDIndexonneedle), the helper returnsLOGOS_MEMMEM_DEOPTWITHOUT scanning — the caller side-exits and bytecode reproduces the exactindex out of boundserror. (Text accesses are guaranteed in-bounds by the bound, exactly as the nest’sIndexUncheckedontextrelies on.)
Counting is via a first-byte scan + window verify (memchr-style) which is
bit-identical to the nested loop: it visits the same positions and applies
the same byte equality, just without per-op bytecode dispatch.
§Safety
haystack_ptr must point to haystack_len readable bytes and needle_ptr
to needle_buf_len readable bytes, both pinned (borrowed) for the call.