MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
pow5.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 pow5.h
9 * @brief Powers of five as 128-bit significands, for the scale of a decimal mantissa into a binary one.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-08-29
12 *
13 * @note transformo walks the bits of the decimal exponent and multiplies in one entry per set bit, and so nine
14 * entries reach 511.
15 * @note Declares no function. Both tables are static const data that outlive every call. A pointer into
16 * one stays good for the whole program [BORROWS].
17 */
18#ifndef MMGR_POW5_H
19#define MMGR_POW5_H
20
21/** @addtogroup mod_pow5
22 * @{ */
23
24#include "mmgr.h"
25
26EMBED_BEGIN_DECLS
27
28/**
29 * @brief Expands to 9, the number of entries in each table.
30 *
31 * @note Entry i holds five raised to two to the i, and so the nine entries run from 5^1 to 5^256.
32 * @note Carries no suffix: it sizes both tables and bounds the walk, where it converts to the signed
33 * embed_iword the loop counts in.
34 */
35#define MMGR_POW5_STEPS 9
36
37/**
38 * @brief Expands to ((1 << 9) - 1), which is 511, the largest decimal exponent the tables reach.
39 *
40 * @note Every one of the nine entries multiplied together gives 5^511.
41 * @note The expansion is a plain int constant expression, which widens to the embed_iword that carries
42 * an exponent wherever the two compare.
43 * @warning muto_scale bounds against this and returns infinity above it and zero below its negative;
44 * muto_scale_to_u64 does not. What retains either off the end of the tables is the walk itself,
45 * which takes only MMGR_POW5_STEPS steps. An exponent past this loses its higher bits.
46 */
47#define MMGR_POW5_MAX ((1 << MMGR_POW5_STEPS) - 1)
48
49/**
50 * @brief One power of five, as a 128-bit significand with a binary exponent.
51 *
52 * @note The value is (hi * 2^64 + lo) * 2^e2, and hi always has its top bit set.
53 */
54typedef struct
55{
56 embed_u64 hi; /**< High 64 bits of the significand, with its top bit set. */
57 embed_u64 lo; /**< Low 64 bits of the significand. */
58 embed_iword e2; /**< Binary exponent that scales the significand; -722 at the widest fits a 16-bit embed_iword. */
59} MmgrPow5;
60
61/**
62 * @brief Five raised to each power of two, from 5^1 at index 0 to 5^256 at index 8.
63 *
64 * @note Entry i is the multiplier for bit i of the exponent magnitude. The walk in muto_apply_pow10
65 * takes this table over mmgr_pow5_down whenever the decimal exponent is not negative.
66 * @note Index 0 through 5 are exact. 5^64 and up need more than 128 bits, and so the last three entries
67 * come truncated toward zero and read a little low.
68 * @note Every significand literal carries ULL to match the embed_u64 that holds it. Each e2 is a bare int
69 * that converts to embed_iword.
70 * @note static const at header scope, and each translation unit gets its own copy. EMBED_UNUSED retains a unit
71 * that never reads it quiet, and an entry's address stays good for the whole program [BORROWS].
72 * @note EMBED_PASS_MEMORY gives each pass of nvcc its own copy, and so a kernel reads the table from device memory.
73 * @warning transformo settles a positive exponent from its exact powers of ten and returns before the walk.
74 * The only exponent that reaches this table there is zero, which sets no bit.
75 */
76static EMBED_PASS_MEMORY const MmgrPow5 mmgr_pow5_up[MMGR_POW5_STEPS] EMBED_UNUSED = {
77 {0xA000000000000000ULL, 0x0000000000000000ULL, -125}, {0xC800000000000000ULL, 0x0000000000000000ULL, -123},
78 {0x9C40000000000000ULL, 0x0000000000000000ULL, -118}, {0xBEBC200000000000ULL, 0x0000000000000000ULL, -109},
79 {0x8E1BC9BF04000000ULL, 0x0000000000000000ULL, -90}, {0x9DC5ADA82B70B59DULL, 0xF020000000000000ULL, -53},
80 {0xC2781F49FFCFA6D5ULL, 0x3CBF6B71C76B25FBULL, 21}, {0x93BA47C980E98CDFULL, 0xC66F336C36B10137ULL, 170},
81 {0xAA7EEBFB9DF9DE8DULL, 0xDDBB901B98FEEAB7ULL, 467},
82};
83
84/**
85 * @brief The reciprocal of each mmgr_pow5_up entry, from 5^-1 at index 0 to 5^-256 at index 8.
86 *
87 * @note Entry i is the multiplier for bit i of the exponent magnitude, and the walk in muto_apply_pow10
88 * takes this table when the decimal exponent is negative.
89 * @note No negative power of five ends in binary. All nine are the exact value truncated toward zero.
90 * Every entry reads a little low and none of them round up. The entry for 5^-1 is 0xCCCC... with
91 * the C repeated, not the 0xCCCD... that a round to nearest would give.
92 * @note Every significand literal carries ULL to match the embed_u64 that holds it. Each e2 is a bare int
93 * that converts to embed_iword.
94 * @note static const at header scope, and each translation unit gets its own copy. EMBED_UNUSED retains a unit
95 * that never reads it quiet, and an entry's address stays good for the whole program [BORROWS].
96 * @note EMBED_PASS_MEMORY gives each pass of nvcc its own copy, as it does mmgr_pow5_up.
97 */
98static EMBED_PASS_MEMORY const MmgrPow5 mmgr_pow5_down[MMGR_POW5_STEPS] EMBED_UNUSED = {
99 {0xCCCCCCCCCCCCCCCCULL, 0xCCCCCCCCCCCCCCCCULL, -130}, {0xA3D70A3D70A3D70AULL, 0x3D70A3D70A3D70A3ULL, -132},
100 {0xD1B71758E219652BULL, 0xD3C36113404EA4A8ULL, -137}, {0xABCC77118461CEFCULL, 0xFDC20D2B36BA7C3DULL, -146},
101 {0xE69594BEC44DE15BULL, 0x4C2EBE687989A9B3ULL, -165}, {0xCFB11EAD453994BAULL, 0x67DE18EDA5814AF2ULL, -202},
102 {0xA87FEA27A539E9A5ULL, 0x3F2398D747B36224ULL, -276}, {0xDDD0467C64BCE4A0ULL, 0xAC7CB3F6D05DDBDEULL, -425},
103 {0xC0314325637A1939ULL, 0xFA911155FEFB5308ULL, -722},
104};
105
106/**
107 * @brief Asserts MMGR_POW5_MAX is at least 511.
108 *
109 * @note A double's decimal exponent stays well inside 511, and so nine steps cover every value one can hold.
110 */
111EMBED_STATIC_ASSERT(MMGR_POW5_MAX >= 511, "the tables do not reach the exponents a double can carry");
112
113EMBED_END_DECLS
114
115/** @} */
116
117#endif
static EMBED_PASS_MEMORY const MmgrPow5 mmgr_pow5_up[MMGR_POW5_STEPS]
Five raised to each power of two, from 5^1 at index 0 to 5^256 at index 8.
Definition pow5.h:76
static EMBED_PASS_MEMORY const MmgrPow5 mmgr_pow5_down[MMGR_POW5_STEPS]
The reciprocal of each mmgr_pow5_up entry, from 5^-1 at index 0 to 5^-256 at index 8.
Definition pow5.h:98
#define MMGR_POW5_STEPS
Expands to 9, the number of entries in each table.
Definition pow5.h:35
#define MMGR_POW5_MAX
Expands to ((1 << 9) - 1), which is 511, the largest decimal exponent the tables reach.
Definition pow5.h:47
The embedded_types headers every module builds on.
One power of five, as a 128-bit significand with a binary exponent.
Definition pow5.h:55
embed_u64 lo
Definition pow5.h:57
embed_u64 hi
Definition pow5.h:56
embed_iword e2
Definition pow5.h:58