MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
fractio.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 fractio.h
9 * @brief The binary64 field layout and scale bounds, the assertions that pin them, the six entry points
10 * and the fract table.
11 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
12 * @date 2026-08-29
13 *
14 * @note Everything here is for binary64. The assertions below are what hold the target to it,
15 * and they are EMBED_STATIC_ASSERT. A target whose double is another shape fails the build
16 * and does not read the wrong bits.
17 * @note FractioCfg carries no pointer, and so no call here follows one into a caller's buffer. The whole
18 * module is field arithmetic on the one value it receives.
19 * @warning args is the single one pointer the six entry points take, and not one of them tests it. The bodies
20 * EMBED_ENTRY writes read its members straight through. A null args faults.
21 */
22#ifndef MMGR_FRACTIO_H
23#define MMGR_FRACTIO_H
24
25/** @addtogroup mod_fract
26 * @{ */
27
28#include "mmgr.h"
29
30EMBED_BEGIN_DECLS
31
32/**
33 * @brief The binary64 field layout: three masks, the sign shift, the two field widths, the two whole
34 * field values and the exponent bias.
35 *
36 * @note The masks, the shift and the widths give each field's position and extent. MMGR_DBL_SIGN_ONE
37 * and MMGR_DBL_EXP_ALL are values a field can hold and not positions, and MMGR_DBL_BIAS is
38 * an amount.
39 * @note The assertions below check the masks tile the word without gap or overlap.
40 * @note The masks, MMGR_DBL_SIGN_ONE and MMGR_DBL_EXP_ALL carry a ull suffix, as the embed_u64 that
41 * holds the fields. The shifts and widths carry u. MMGR_DBL_BIAS carries neither, since the
42 * signed scale arithmetic below subtracts it.
43 */
44#define MMGR_DBL_SIGN_MASK 0x8000000000000000ull /**< The sign bit. */
45#define MMGR_DBL_EXP_MASK 0x7FF0000000000000ull /**< The eleven exponent bits. */
46#define MMGR_DBL_MANT_MASK 0x000FFFFFFFFFFFFFull /**< The fifty-two stored mantissa bits. */
47#define MMGR_DBL_SIGN_SHIFT 63u /**< Bit position of the sign. */
48#define MMGR_DBL_MANT_BITS 52u /**< Stored mantissa width, and the exponent's shift. */
49#define MMGR_DBL_EXP_BITS 11u /**< Exponent width. */
50#define MMGR_DBL_SIGN_ONE 0x1ull /**< A sign of one, and merge's mask for args->sign. */
51#define MMGR_DBL_EXP_ALL 0x7FFull /**< All ones in the exponent, and merge's mask for args->exp. */
52#define MMGR_DBL_BIAS 1023 /**< Amount added to the true exponent when stored. */
53
54/**
55 * @brief The width of a double in bits, bytes and embed_word units.
56 *
57 * @note MMGR_DBL_BITS is a statement and not a measurement. The assertion below compares it against sizeof(double)
58 * and fails the build when the target disagrees.
59 * @note The 8u in MMGR_DBL_BYTES is bits per byte. That same assertion holds a target to it, since a
60 * byte of another width would leave sizeof(double) * 8u short of MMGR_DBL_BITS.
61 * @note MMGR_DBL_WORDS rounds up, and the assertion below requires it to come out exact.
62 */
63#define MMGR_DBL_BITS 64u /**< Bits in a double. */
64#define MMGR_DBL_BYTES (MMGR_DBL_BITS / 8u) /**< Bytes in a double. */
65#define MMGR_DBL_WORDS \
66 ((MMGR_DBL_BITS + (EMBED_WORD_BITS - 1u)) / EMBED_WORD_BITS) /**< embed_word units per double. \
67 */
68
69/**
70 * @brief Pins the target's double to the width and storage this file assumes.
71 *
72 * @note Checks the bit width, that a double is a whole number of embed_word units, and that embed_u64 holds it.
73 */
74EMBED_STATIC_ASSERT(sizeof(double) * 8u == MMGR_DBL_BITS,
75 "double is not 64 bits on this target, so every field position in this file is wrong "
76 "- a build where double means float cannot use it");
77EMBED_STATIC_ASSERT(MMGR_DBL_WORDS *EMBED_WORD_BITS == MMGR_DBL_BITS,
78 "a double is not a whole number of words on this target");
79EMBED_STATIC_ASSERT(sizeof(embed_u64) == sizeof(double), "the bit pattern of a double does not fit embed_u64");
80
81/**
82 * @brief Pins the field constants above against each other.
83 *
84 * @note Checks the widths sum to 64, the masks tile the word, and no two masks overlap.
85 * @note Also checks each mask against the shift or width for the same field, that MMGR_DBL_EXP_ALL fills
86 * MMGR_DBL_EXP_BITS, and that MMGR_DBL_BIAS is half the exponent range less one.
87 */
88EMBED_STATIC_ASSERT(1u + MMGR_DBL_EXP_BITS + MMGR_DBL_MANT_BITS == MMGR_DBL_BITS,
89 "the three fields do not add up to the width of the value");
90EMBED_STATIC_ASSERT((MMGR_DBL_SIGN_MASK | MMGR_DBL_EXP_MASK | MMGR_DBL_MANT_MASK) == 0xFFFFFFFFFFFFFFFFull,
91 "the three field masks leave a gap");
92EMBED_STATIC_ASSERT((MMGR_DBL_SIGN_MASK & MMGR_DBL_EXP_MASK) == 0u && (MMGR_DBL_EXP_MASK & MMGR_DBL_MANT_MASK) == 0u &&
94 "the three field masks overlap");
96 "the sign mask and the sign shift disagree about where the sign is");
97EMBED_STATIC_ASSERT(MMGR_DBL_EXP_MASK == (MMGR_DBL_EXP_ALL << MMGR_DBL_MANT_BITS),
98 "the exponent mask and the exponent width disagree");
99EMBED_STATIC_ASSERT(MMGR_DBL_EXP_ALL == ((1u << MMGR_DBL_EXP_BITS) - 1u), "the exponent does not fill its field");
100EMBED_STATIC_ASSERT(MMGR_DBL_BIAS == ((1 << (MMGR_DBL_EXP_BITS - 1u)) - 1), "the bias is not the one binary64 uses");
101
102/**
103 * @brief The range of powers of two a double can carry once the mantissa counts as an integer.
104 *
105 * @note MMGR_DBL_SCALE_MAX takes the largest finite exponent, removes the bias, and drops the mantissa width.
106 * @note MMGR_DBL_SCALE_MIN starts from an exponent field of 1, the smallest normal.
107 * @note Explicit casts hold both expressions in embed_iword, since neither MMGR_DBL_EXP_ALL nor
108 * MMGR_DBL_MANT_BITS has a sign. MMGR_DBL_SCALE_MIN is negative, and without its cast the subtraction would wrap.
109 */
110#define MMGR_DBL_SCALE_MAX ((embed_iword)(MMGR_DBL_EXP_ALL - 1u) - MMGR_DBL_BIAS - (embed_iword)MMGR_DBL_MANT_BITS)
111#define MMGR_DBL_SCALE_MIN (1 - MMGR_DBL_BIAS - (embed_iword)MMGR_DBL_MANT_BITS)
112
113/**
114 * @brief Pins the two scale bounds to the values binary64 gives.
115 */
116EMBED_STATIC_ASSERT(MMGR_DBL_SCALE_MAX == 971, "the largest scale a finite double can carry is not what it was");
117EMBED_STATIC_ASSERT(MMGR_DBL_SCALE_MIN == -1074, "the smallest scale a subnormal can carry is not what it was");
118
119/**
120 * @brief Arguments for the fract calls, where each call reads only the members it needs.
121 *
122 * @note val and bits share storage, and so a write of one and a read of the other reinterprets the same bytes.
123 * @note Only merge reads sign, exp and mant. The other five calls read the union.
124 */
125typedef struct
126{
127 union {
128 const double val; /**< The value, when the caller supplies a double. */
129 const embed_u64 bits; /**< The same storage read as a bit pattern. */
130 };
131 const embed_u64 sign; /**< Sign for merge, 0 or 1. */
132 const embed_u64 exp; /**< Biased exponent for merge. */
133 const embed_u64 mant; /**< Stored mantissa for merge. */
134} FractioCfg;
135
136/**
137 * @brief Type of the fract dispatch table.
138 *
139 * @note EMBED_TABLE_LAYOUT asserts the six members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
140 * nothing else.
141 */
142typedef struct
143{
144 embed_u64 (*sign)(const FractioCfg *args); /**< Sign bit of bits, as 0 or 1. */
145 embed_u64 (*exp)(const FractioCfg *args); /**< Biased exponent field of bits. */
146 embed_u64 (*mant)(const FractioCfg *args); /**< Stored mantissa field of bits. */
147 embed_u64 (*merge)(const FractioCfg *args); /**< Packs sign, exp and mant into one pattern. */
148 double (*from_bits)(const FractioCfg *args); /**< Reads the union as a double. */
149 embed_u64 (*to_bits)(const FractioCfg *args); /**< Reads the union as a bit pattern. */
150} FractioNs;
151EMBED_TABLE_LAYOUT(FractioNs, sign, exp, mant, merge, from_bits, to_bits);
152
153/**
154 * @brief Returns the sign bit of args->bits.
155 *
156 * @param[in] args Bit pattern in the union [BORROWS].
157 * @return 0 for a positive sign, 1 for a negative one.
158 * @note The bit reads as stored, and nothing else counts. A negative zero answers 1, and so
159 * does a NaN with the sign.
160 */
161EMBED_HOST_DEVICE embed_u64 mmgr_fract_sign(const FractioCfg *args);
162
163/**
164 * @brief Returns the exponent field of args->bits.
165 *
166 * @param[in] args Bit pattern in the union [BORROWS].
167 * @return The stored exponent, with MMGR_DBL_BIAS still in it.
168 * @note 0 marks a zero or subnormal. MMGR_DBL_EXP_ALL marks an infinity or NaN.
169 */
170EMBED_HOST_DEVICE embed_u64 mmgr_fract_exp(const FractioCfg *args);
171
172/**
173 * @brief Returns the mantissa field of args->bits.
174 *
175 * @param[in] args Bit pattern in the union [BORROWS].
176 * @return The fifty-two stored bits, without the implicit leading one.
177 * @note The leading one is implicit only where the exponent field is nonzero. A zero or a subnormal
178 * has none, and there these fifty-two bits are the whole mantissa.
179 */
180EMBED_HOST_DEVICE embed_u64 mmgr_fract_mant(const FractioCfg *args);
181
182/**
183 * @brief Packs args->sign, args->exp and args->mant into one bit pattern.
184 *
185 * @param[in] args The three fields [BORROWS].
186 * @return The assembled pattern.
187 * @note Each field has a mask of its own width. A wide input cannot reach the next field.
188 * @warning What the mask drops stays gone and nothing reports it. A sign above MMGR_DBL_SIGN_ONE, an
189 * exponent above MMGR_DBL_EXP_ALL or a mantissa above MMGR_DBL_MANT_MASK retains only its low
190 * bits.
191 */
192EMBED_HOST_DEVICE embed_u64 mmgr_fract_merge(const FractioCfg *args);
193
194/**
195 * @brief Reads the union as a double.
196 *
197 * @param[in] args Union with its bits member filled [BORROWS].
198 * @return The same storage interpreted as a double.
199 */
200EMBED_HOST_DEVICE double mmgr_fract_from_bits(const FractioCfg *args);
201
202/**
203 * @brief Reads the union as a bit pattern.
204 *
205 * @param[in] args Union with its val member filled [BORROWS].
206 * @return The same storage interpreted as embed_u64.
207 */
208EMBED_HOST_DEVICE embed_u64 mmgr_fract_to_bits(const FractioCfg *args);
209
210/**
211 * @brief Dispatch table instance named fract, whose members are the mmgr_fract_ entries.
212 *
213 * @note EMBED_TABLE_STORAGE is static const, and so every translation unit that includes this header holds a copy of
214 * its own and the addresses differ between them. The six functions it points at are the one shared definition.
215 * @note EMBED_UNUSED retains a translation unit that includes the header and never calls through the
216 * table from a warning about it. It expands to nothing where the unused attribute is unavailable.
217 */
218EMBED_TABLE_STORAGE FractioNs fract EMBED_UNUSED = {
219 .sign = mmgr_fract_sign,
220 .exp = mmgr_fract_exp,
221 .mant = mmgr_fract_mant,
222 .merge = mmgr_fract_merge,
223 .from_bits = mmgr_fract_from_bits,
224 .to_bits = mmgr_fract_to_bits,
225};
226
227EMBED_END_DECLS
228
229/** @} */
230
231#endif
#define MMGR_DBL_EXP_ALL
Definition fractio.h:51
#define MMGR_DBL_MANT_BITS
Definition fractio.h:48
#define MMGR_DBL_BITS
The width of a double in bits, bytes and embed_word units.
Definition fractio.h:63
EMBED_HOST_DEVICE embed_u64 mmgr_fract_mant(const FractioCfg *args)
Returns the mantissa field of args->bits.
#define MMGR_DBL_BIAS
Definition fractio.h:52
#define MMGR_DBL_EXP_MASK
Definition fractio.h:45
EMBED_HOST_DEVICE embed_u64 mmgr_fract_to_bits(const FractioCfg *args)
Reads the union as a bit pattern.
#define MMGR_DBL_MANT_MASK
Definition fractio.h:46
EMBED_HOST_DEVICE double mmgr_fract_from_bits(const FractioCfg *args)
Reads the union as a double.
static const FractioNs fract
Dispatch table instance named fract, whose members are the mmgr_fract_ entries.
Definition fractio.h:218
EMBED_HOST_DEVICE embed_u64 mmgr_fract_exp(const FractioCfg *args)
Returns the exponent field of args->bits.
#define MMGR_DBL_SIGN_SHIFT
Definition fractio.h:47
#define MMGR_DBL_EXP_BITS
Definition fractio.h:49
EMBED_HOST_DEVICE embed_u64 mmgr_fract_merge(const FractioCfg *args)
Packs args->sign, args->exp and args->mant into one bit pattern.
#define MMGR_DBL_SIGN_ONE
Definition fractio.h:50
EMBED_HOST_DEVICE embed_u64 mmgr_fract_sign(const FractioCfg *args)
Returns the sign bit of args->bits.
#define MMGR_DBL_SIGN_MASK
The binary64 field layout: three masks, the sign shift, the two field widths, the two whole field val...
Definition fractio.h:44
#define MMGR_DBL_WORDS
Definition fractio.h:65
#define MMGR_DBL_SCALE_MAX
Pins the target's double to the width and storage this file assumes.
Definition fractio.h:110
The embedded_types headers every module builds on.
Pins the two scale bounds to the values binary64 gives.
Definition fractio.h:126
const embed_u64 exp
Definition fractio.h:132
const embed_u64 mant
Definition fractio.h:133
const embed_u64 sign
Definition fractio.h:131
Type of the fract dispatch table.
Definition fractio.h:143
embed_u64(*) to_bits(const FractioCfg *args)
Definition fractio.h:149
embed_u64(*) mant(const FractioCfg *args)
Definition fractio.h:146
embed_u64(*) exp(const FractioCfg *args)
Definition fractio.h:145
double(*) from_bits(const FractioCfg *args)
Definition fractio.h:148
embed_u64(*) sign(const FractioCfg *args)
Definition fractio.h:144
embed_u64(*) merge(const FractioCfg *args)
Definition fractio.h:147