MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
transformo.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 transformo.h
9 * @brief Decimal to binary conversion, the limits, the arguments, and the muto dispatch table.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 *
13 * @note take builds a mantissa one digit at a time, and take_in does the same in a base from 2 to 36.
14 * @note scale turns one into a double, and scale_to_u64 turns one into an integer.
15 */
16#ifndef MMGR_TRANSFORMO_H
17#define MMGR_TRANSFORMO_H
18
19/** @addtogroup mod_muto
20 * @{ */
21
22#include "fractio/fractio.h"
23#include "mmgr.h"
24#include "pow5/pow5.h"
25
26EMBED_BEGIN_DECLS
27
28/**
29 * @brief Expands to the largest mantissa mmgr_muto_take will extend, which is (~0 - 9) / 10.
30 *
31 * @note Chosen so a multiply by ten and an add of a digit of nine both stay inside an embed_u64.
32 * @note mmgr_muto_take returns EMBED_FALSE and appends nothing once the mantissa passes this.
33 * @note Explicit casts hold both the complement and the result at embed_u64, and the 9u and 10u
34 * match that width. So the subtraction and the divide run at the mantissa's own width
35 * and not at int's.
36 */
37#define MMGR_MUTO_MANT_MAX ((embed_u64)((~(embed_u64)0 - 9u) / 10u))
38
39/**
40 * @brief Expands to 400, the count at which a parsed decimal exponent stops its growth.
41 *
42 * @note cellul_expo tests its running exponent against this before each multiply and add. A long run
43 * of digits cannot run the count away with the embed_iword that holds it.
44 * @note 400 is past the 308 a double reaches. Anything the clamp holds back has already gone to a
45 * signed infinity or a signed zero. An early stop of the count cannot change the result.
46 * @note The literal is a plain number, as the embed_iword exponent it compares against.
47 */
48#define MMGR_MUTO_EXP_LIMIT 400
49
50/**
51 * @brief Arguments for the four muto calls.
52 *
53 * @note take reads mant and digit, and take_in reads mant, digit and base.
54 * @note scale reads mant, ex, rest and neg.
55 * @note scale_to_u64 reads mant, e2 and ex.
56 * @warning take and take_in write through mant, and so the caller's mantissa changes [BORROWS].
57 */
58typedef struct
59{
60 embed_u64 *const mant; /**< The mantissa, which take extends and both scale calls read [BORROWS]. */
61 const char digit; /**< ASCII digit take and take_in append. */
62 const embed_iword e2; /**< Binary exponent the mantissa already carries, read by scale_to_u64. */
63 const embed_iword ex; /**< Decimal exponent to apply. */
64 const embed_iword rest; /**< Non-zero when the caller already dropped bits, and so the round can see them. */
65 const embed_bool neg; /**< Sign scale gives its result. */
66 const unsigned int base; /**< Base take_in reads the digit in, from 2 to 36. */
68
69/**
70 * @brief Type of the muto dispatch table.
71 *
72 * @note EMBED_TABLE_LAYOUT asserts the four members sit at consecutive EMBED_FUNCTION_POINTER_BYTES offsets, with
73 * nothing else.
74 */
75typedef struct
76{
77 embed_bool (*take)(const TransformoCfg *args); /**< Appends one decimal digit to a mantissa. */
78 double (*scale)(const TransformoCfg *args); /**< Turns a mantissa and decimal exponent into a double. */
79 embed_u64 (*scale_to_u64)(const TransformoCfg *args); /**< Turns the same plus e2 into a rounded 64-bit integer. */
80 embed_bool (*take_in)(const TransformoCfg *args); /**< Appends one digit of a base to a mantissa. */
82EMBED_TABLE_LAYOUT(TransformoNs, take, scale, scale_to_u64, take_in);
83
84/**
85 * @brief The value of an ASCII digit in a base up to 36.
86 *
87 * @param[in] character The byte.
88 * @return 0 to 9 for '0' to '9', 10 to 35 for a letter of either case, and 36 for any other byte.
89 * @note A value at or past the base says the byte is no digit of that base, and 36 is past every base.
90 */
91EMBED_HOST_DEVICE EMBED_INLINE unsigned int mmgr_muto_digit_value(uint8_t character)
92{
93 const unsigned int decimal = (unsigned int)character - (unsigned int)'0';
94 // A letter of either case folds to its lower case with the bit of 0x20 set
95 const unsigned int letter = ((unsigned int)character | 0x20u) - (unsigned int)'a';
96 return (decimal < 10u) ? decimal : ((letter < 26u) ? (letter + 10u) : 36u);
97}
98
99/**
100 * @brief Appends args->digit to *args->mant as one more decimal digit.
101 *
102 * @param[in] args The mantissa to extend and the digit to append [BORROWS].
103 * @return EMBED_TRUE when the digit went on, EMBED_FALSE when *args->mant already passed
104 * MMGR_MUTO_MANT_MAX.
105 * @note On EMBED_FALSE the mantissa stays as it was. A caller can count the digits it had to drop.
106 * @warning args->digit must be an ASCII decimal digit, since the value added is args->digit minus '0'.
107 * @warning Writes through args->mant [BORROWS].
108 */
109EMBED_HOST_DEVICE embed_bool mmgr_muto_take(const TransformoCfg *args);
110
111/**
112 * @brief Turns *args->mant times ten raised to args->ex into a double, signed by args->neg.
113 *
114 * @param[in] args The mantissa, the decimal exponent, the dropped bits and the sign [BORROWS].
115 * @return The nearest double, with ties to even.
116 * @note Set args->rest when digits fell off the mantissa, and so the round still accounts for them.
117 * @note Takes a plain double path when nothing fell off, the mantissa is under 2^53 and args->ex is within 22.
118 * @note Does not read args->e2, and so the mantissa counts as a plain integer.
119 * @warning Returns a signed infinity for an args->ex above MMGR_POW5_MAX and a signed zero below its negative.
120 */
121EMBED_HOST_DEVICE double mmgr_muto_scale(const TransformoCfg *args);
122
123/**
124 * @brief Turns *args->mant times two raised to args->e2 times ten raised to args->ex into a rounded 64-bit integer.
125 *
126 * @param[in] args The mantissa, its binary exponent and the decimal exponent [BORROWS].
127 * @return The nearest integer with a half taken up, 0 when the value rounds below one, or all
128 * ones when too large.
129 * @note Reads args->e2, which mmgr_muto_scale leaves alone, and so the mantissa may carry a binary exponent here.
130 * @note A half goes up whatever lies below it, and so the result is the magnitude rounded away from
131 * zero. mmgr_muto_scale rounds its own halves to even, as a double wants and what
132 * this deliberately does not do.
133 * @note Reads neither args->rest, args->above nor args->neg, and so the result is always unsigned.
134 * @warning Does not bound args->ex against MMGR_POW5_MAX the way mmgr_muto_scale does. Only
135 * MMGR_POW5_STEPS bits of the magnitude take part in the walk. A larger exponent loses its high
136 * bits and the value scaled is not the one asked for.
137 */
138EMBED_HOST_DEVICE embed_u64 mmgr_muto_scale_to_u64(const TransformoCfg *args);
139
140/**
141 * @brief Appends args->digit to *args->mant as one more digit in base args->base.
142 *
143 * @param[in] args The mantissa to extend, the digit to append and its base [BORROWS].
144 * @return EMBED_TRUE when the digit went on. EMBED_FALSE when the base is outside 2 to 36, when the byte is
145 * no digit of the base, or when the digit would carry the mantissa past all ones.
146 * @note On EMBED_FALSE the mantissa stays as it was. A caller tells an overflow from a byte that is no digit with
147 * mmgr_muto_digit_value.
148 * @note A digit is '0' to '9', then a letter of either case for 10 to 35.
149 * @warning Writes through args->mant [BORROWS].
150 */
151EMBED_HOST_DEVICE embed_bool mmgr_muto_take_in(const TransformoCfg *args);
152
153/**
154 * @brief Dispatch table instance named muto, with each member set to its mmgr_muto_ function.
155 */
156EMBED_TABLE_STORAGE TransformoNs muto EMBED_UNUSED = {
157 .take = mmgr_muto_take,
158 .scale = mmgr_muto_scale,
159 .scale_to_u64 = mmgr_muto_scale_to_u64,
160 .take_in = mmgr_muto_take_in,
161};
162
163EMBED_END_DECLS
164
165/** @} */
166
167#endif
The binary64 field layout and scale bounds, the assertions that pin them, the six entry points and th...
EMBED_HOST_DEVICE embed_bool mmgr_muto_take_in(const TransformoCfg *args)
Appends args->digit to *args->mant as one more digit in base args->base.
static const TransformoNs muto
Dispatch table instance named muto, with each member set to its mmgr_muto_ function.
Definition transformo.h:156
static EMBED_HOST_DEVICE unsigned int mmgr_muto_digit_value(uint8_t character)
The value of an ASCII digit in a base up to 36.
Definition transformo.h:91
EMBED_HOST_DEVICE embed_u64 mmgr_muto_scale_to_u64(const TransformoCfg *args)
Turns *args->mant times two raised to args->e2 times ten raised to args->ex into a rounded 64-bit int...
EMBED_HOST_DEVICE double mmgr_muto_scale(const TransformoCfg *args)
Turns *args->mant times ten raised to args->ex into a double, signed by args->neg.
EMBED_HOST_DEVICE embed_bool mmgr_muto_take(const TransformoCfg *args)
Appends args->digit to *args->mant as one more decimal digit.
The embedded_types headers every module builds on.
Powers of five as 128-bit significands, for the scale of a decimal mantissa into a binary one.
Arguments for the four muto calls.
Definition transformo.h:59
embed_u64 *const mant
Definition transformo.h:60
const embed_iword ex
Definition transformo.h:63
const char digit
Definition transformo.h:61
const embed_iword e2
Definition transformo.h:62
const unsigned int base
Definition transformo.h:66
const embed_iword rest
Definition transformo.h:64
const embed_bool neg
Definition transformo.h:65
Type of the muto dispatch table.
Definition transformo.h:76
embed_bool(*) take_in(const TransformoCfg *args)
Definition transformo.h:80
embed_bool(*) take(const TransformoCfg *args)
Definition transformo.h:77
embed_u64(*) scale_to_u64(const TransformoCfg *args)
Definition transformo.h:79
double(*) scale(const TransformoCfg *args)
Definition transformo.h:78