/** * @file src/basic_punned.cuh * @author Ash Vardanian * @date October 25, 2026 * @brief What the basic dispatch units and the class units agree on: the verb table one variant * compiles, the class tables each class unit instantiates, and the traits each modality's * verbs differ in, with the composites they lower each C argument structure onto once. * * It includes no tower: a modality's dispatch unit reaches its variant only through its * residency's verb table, and @c basic_specializations.cuh with each * @c _specializations.cuh fills those tables from the towers. What a verb does to one * variant, as adopting a geometry or writing its values, is a @c variant_bodies specialization * beside the variants. */ #pragma once #include // `std::isnan` - an observation without a position #include // `std::memcpy` - a key read through its stride #include // `std::array` - the devices a GPU executor spans #include // `std::to_chars`, `std::from_chars` - the metadata a checkpoint records as text #include // `std::numeric_limits` - the widths of a tape's offsets #include // `std::span` #include // `std::string_view` - entries, tokens and paths #include // `std::type_identity_t` #include // `std::monostate` - a verb a modality takes no input for #include "usearch/utilities/safegraphs_format.hpp" // `safegraphs_read_index_metadata` - a checkpoint's fields #include "lib.hpp" #pragma region Crossings namespace util = unum::usearch::utilities; /** Key @p offset of the caller's keys, through their stride; zero repeats the first. */ inline usearch_key_t key_at(usearch_keys_view_t keys, std::size_t offset) noexcept { std::size_t const stride = keys.stride; usearch_key_t key; std::memcpy(&key, reinterpret_cast(keys.data) + offset * stride, sizeof(key)); return key; } /** Whether @p count + 1 offsets never fall; they may start anywhere, as an Arrow slice's do. */ template bool monotone_offsets(offset_type_ const* offsets, std::size_t count) noexcept { if (!offsets) return false; for (std::size_t entry = 0; entry != count; ++entry) if (offsets[entry + 1] < offsets[entry]) return false; return true; } /** Whether a tape's @p count + 1 offsets never fall, in the width they arrive in. */ inline bool monotone_offsets(usearch_tape_view_t const& tape, std::size_t count) noexcept { return tape.layout == usearch_tape_u64_k ? monotone_offsets(tape.offsets.u64, count) : monotone_offsets(tape.offsets.u32, count); } /** Whether @p layout names a width of offsets. */ inline bool known_layout(usearch_tape_layout_t layout) noexcept { return layout == usearch_tape_u32_k || layout == usearch_tape_u64_k; } /** Copies the offsets at @p source into @p widened, refusing one past what @c std::size_t holds. */ template util::status_t widen_offsets(offset_type_ const* source, std::span widened) noexcept { for (std::size_t& offset : widened) { if (*source > (std::numeric_limits::max)()) return util::status_code_t::overflow_k; offset = static_cast(*source++); } return util::status_code_t::success_k; } /** The @p count + 1 offsets of the caller's @p tape as the engines read them: a u64 tape's in place * where @c std::size_t matches it, and any other widened onto @p pages. */ inline util::expected> offsets_of( usearch_tape_view_t const& tape, std::size_t count, util::growing_arena& pages) noexcept { if constexpr (sizeof(std::size_t) == sizeof(util::u64_t)) if (tape.layout == usearch_tape_u64_k) return {{reinterpret_cast(tape.offsets.u64), count + 1}, util::status_code_t::success_k}; std::size_t* const widened = carve(pages, count + 1); if (!widened) return {{}, util::status_code_t::out_of_memory_k}; std::span const offsets {widened, count + 1}; util::status_t const status = tape.layout == usearch_tape_u64_k ? widen_offsets(tape.offsets.u64, offsets) : widen_offsets(tape.offsets.u32, offsets); return {offsets, status}; } /** Lays the @p offsets a get wrote out as @p tape 's over @p data of @p scalar, in place, as wide * as @c std::size_t. */ inline void write_output_tape(void const* data, std::size_t const* offsets, usearch_scalar_t scalar, usearch_tape_view_t& tape) noexcept { tape = {data, usearch_tape_u64_k, {}, scalar}; if constexpr (sizeof(std::size_t) == sizeof(util::u64_t)) tape.offsets.u64 = reinterpret_cast(offsets); else tape.layout = usearch_tape_u32_k, tape.offsets.u32 = reinterpret_cast(offsets); } /** Raises @p word to @p value, if it is larger, from any thread, through the CPU class's reference; * a relaxed peek first, so a word already that large is never written. */ template