MMgr v1.0.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
mmgr_vector.h
Go to the documentation of this file.
1/*
2 * MMgr - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
3 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Commercial OR LicenseRef-Educational
4 *
5 * Every use falls under AGPL-3.0-or-later unless you hold explicit permission, which is either a
6 * negotiated commercial licensing contract or an educator's license issued to you personally.
7 */
8/**
9 * @file mmgr_vector.h
10 * @brief The lane, embedded_types' 256-bit unit beside the word, as the walks of memoria_operor and
11 * cellularum_laboro take it.
12 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
13 * @date 2026-10-29
14 *
15 * @note A walk steps a word at a time through verbum_scrutor's lanes. The same walk first takes
16 * whole lanes, MMGR_VECTOR_BYTES at a step, and leaves the word walk the rest. The questions a
17 * walk asks of a word are the ones asked here of a lane: which bytes hold zero, which bytes two
18 * lanes differ in, and the first byte set in a mask.
19 * @note The lane is embed_lane, the target's widest vector register, derived by embedded_types as
20 * the word is: 512 bits under AVX-512, 256 under AVX2, 128 under SSE2 or NEON, the word's
21 * width where the target has no vector unit. MMGR_VECTOR_BYTES is EMBED_LANE_BYTES, and 0
22 * where MMGR_VECTOR_OFF is 1, which leaves the word walks alone. A wider register in a later
23 * build widens every walk here with no edit. Every arm of every gate is defined. A mask holds
24 * one bit a byte, the first byte lowest.
25 * @warning mmgr_vector_load and mmgr_vector_put are unaligned, for a walk over bytes it was given a
26 * count of, which keeps every read within them. A walk over text ended by a zero, whose cap
27 * may reach far past the text, loads through mmgr_vector_load_aligned alone, at an address
28 * a multiple of MMGR_VECTOR_BYTES: an aligned lane never crosses a page, and a read past
29 * the text's end stays in the page the text ends in, as the word walk's does.
30 */
31#ifndef MMGR_VECTOR_H
32#define MMGR_VECTOR_H
33
34#include <stddef.h>
35#include <stdint.h>
36
37#include "embed_lane.h"
38
39#ifndef MMGR_VECTOR_OFF
40#define MMGR_VECTOR_OFF 0
41#endif
42#if (MMGR_VECTOR_OFF != 0) && (MMGR_VECTOR_OFF != 1)
43#error "MMGR_VECTOR_OFF must be 0 or 1"
44#endif
45
46#if MMGR_VECTOR_OFF
47#define MMGR_VECTOR_BYTES 0u
48#else
49#define MMGR_VECTOR_BYTES EMBED_LANE_BYTES
50#endif
51
52/**
53 * @brief The bytes of a lane as a mask, one bit a byte, the first byte lowest.
54 */
55typedef embed_lane_mask MmgrVectorMask;
56
57/**
58 * @brief The mask of the first `bytes_` of a lane.
59 */
60#define MMGR_VECTOR_MASK_OF(bytes_) \
61 (((bytes_) >= EMBED_LANE_BYTES) ? EMBED_LANE_MASK_ALL : (((MmgrVectorMask)1 << (bytes_)) - 1u))
62
63/**
64 * @brief The index of the first byte set in a mask.
65 *
66 * @param[in] lanes A mask with at least one byte set.
67 * @return The byte, counted from the first.
68 */
69EMBED_INLINE size_t mmgr_vector_mask_first(MmgrVectorMask lanes)
70{
71 // Explicit cast widens the lane's byte index to the offset it is used as
72 return (size_t)embed_lane_mask_first(lanes);
73}
74
75/**
76 * @brief The bytes before the first byte set in a mask, every byte where none is set.
77 *
78 * @param[in] lanes Bytes that end a walk.
79 * @return The bytes a match counts in.
80 */
82{
83 return (lanes == 0u) ? EMBED_LANE_MASK_ALL : ((lanes & (0u - lanes)) - 1u);
84}
85
86#if MMGR_VECTOR_BYTES != 0u
87
88typedef embed_lane MmgrVector;
89
90EMBED_INLINE MmgrVector mmgr_vector_load(const uint8_t *at)
91{
92 return embed_lane_load(at);
93}
94
95EMBED_INLINE MmgrVector mmgr_vector_load_aligned(const uint8_t *at)
96{
97 return embed_lane_load_aligned(at);
98}
99
100EMBED_INLINE void mmgr_vector_put(uint8_t *dst, MmgrVector vector)
101{
102 embed_lane_put(dst, vector);
103}
104
105EMBED_INLINE MmgrVector mmgr_vector_fill(uint8_t val)
106{
107 return embed_lane_fill_byte(val);
108}
109
110EMBED_INLINE MmgrVectorMask mmgr_vector_equal_lanes(MmgrVector one, MmgrVector other)
111{
112 return embed_lane_bytes_equal(one, other);
113}
114
115EMBED_INLINE MmgrVectorMask mmgr_vector_zero_lanes(MmgrVector vector)
116{
117 return embed_lane_bytes_zero(vector);
118}
119
120/**
121 * @brief The bytes two lanes differ in.
122 */
123EMBED_INLINE MmgrVectorMask mmgr_vector_differ_lanes(MmgrVector one, MmgrVector other)
124{
125 return EMBED_LANE_MASK_ALL & ~mmgr_vector_equal_lanes(one, other);
126}
127
128#endif
129
130#endif
embed_lane_mask MmgrVectorMask
The bytes of a lane as a mask, one bit a byte, the first byte lowest.
Definition mmgr_vector.h:55
static MmgrVectorMask mmgr_vector_mask_before(MmgrVectorMask lanes)
The bytes before the first byte set in a mask, every byte where none is set.
Definition mmgr_vector.h:81
static size_t mmgr_vector_mask_first(MmgrVectorMask lanes)
The index of the first byte set in a mask.
Definition mmgr_vector.h:69