MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
clz.h
Go to the documentation of this file.
1/* MMgr - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Commercial OR LicenseRef-Educational
3 *
4 * Every use falls under AGPL-3.0-or-later unless you hold explicit permission, which is either a
5 * negotiated commercial licensing contract or an educator's license issued to you personally.
6 */
7/**
8 * @file clz.h
9 * @brief Leading and trailing zero counts: the argument type, the two calls, and the clz dispatch
10 * table.
11 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
12 * @date 2026-08-29
13 *
14 * @note A zero count is how a scan turns a lane mask into a lane index. So these sit under the SWAR
15 * walks and not among the general tools.
16 * @note Both run branchless and in a fixed number of steps. A caller pays the same whatever the
17 * value is. That is the ground on which they appear in plain code and not through a builtin. A
18 * builtin is absent on some targets and a call on others.
19 * @warning Neither distinguishes a value of 0 from a value with one bit set at the end it counts
20 * from. A caller that can receive 0 tests for it first.
21 */
22#ifndef MMGR_CLZ_H
23#define MMGR_CLZ_H
24
25/** @addtogroup mod_clz
26 * @{ */
27
28#include "mmgr.h"
29
30EMBED_BEGIN_DECLS
31
32/**
33 * @brief Argument for both clz calls: the value to count zeros in.
34 */
35typedef struct
36{
37 const embed_u64 val; /**< Value whose leading or trailing zeros the count covers. */
38} ClzCfg;
39
40/**
41 * @brief Type of the clz dispatch table.
42 *
43 * @note EMBED_TABLE_LAYOUT asserts the two members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
44 * nothing else.
45 */
46typedef struct
47{
48 embed_iword (*lead)(const ClzCfg *args); /**< Leading zero count of val. */
49 embed_iword (*trail)(const ClzCfg *args); /**< Trailing zero count of val. */
50} ClzNs;
51EMBED_TABLE_LAYOUT(ClzNs, lead, trail);
52
53/**
54 * @brief Counts the zero bits above the highest set bit of args->val.
55 *
56 * @param[in] args Value to measure [BORROWS].
57 * @return Leading zero count, 0 through 63.
58 * @note Runs in a fixed number of steps, none of which branches on the value.
59 * @warning An args->val of 0 returns 63, the same answer as an args->val of 1.
60 */
61EMBED_HOST_DEVICE embed_iword mmgr_clz_lead(const ClzCfg *args);
62
63/**
64 * @brief Counts the zero bits below the lowest set bit of args->val.
65 *
66 * @param[in] args Value to measure [BORROWS].
67 * @return Trailing zero count, 0 through 63.
68 * @note Runs in a fixed number of steps, none of which branches on the value.
69 * @warning An args->val of 0 returns 63, the same answer as an args->val of 2^63.
70 */
71EMBED_HOST_DEVICE embed_iword mmgr_clz_trail(const ClzCfg *args);
72
73/**
74 * @brief Dispatch table instance named clz, with each member set to its mmgr_clz_ function.
75 */
76EMBED_TABLE_STORAGE ClzNs clz EMBED_UNUSED = {
77 .lead = mmgr_clz_lead,
78 .trail = mmgr_clz_trail,
79};
80
81EMBED_END_DECLS
82
83/** @} */
84
85#endif
EMBED_HOST_DEVICE embed_iword mmgr_clz_trail(const ClzCfg *args)
Counts the zero bits below the lowest set bit of args->val.
static const ClzNs clz
Dispatch table instance named clz, with each member set to its mmgr_clz_ function.
Definition clz.h:76
EMBED_HOST_DEVICE embed_iword mmgr_clz_lead(const ClzCfg *args)
Counts the zero bits above the highest set bit of args->val.
The embedded_types headers every module builds on.
Argument for both clz calls: the value to count zeros in.
Definition clz.h:36
const embed_u64 val
Definition clz.h:37
Type of the clz dispatch table.
Definition clz.h:47
embed_iword(*) lead(const ClzCfg *args)
Definition clz.h:48
embed_iword(*) trail(const ClzCfg *args)
Definition clz.h:49