|  | 
|  | 1 | +/* | 
|  | 2 | +
 | 
|  | 3 | +Copyright (c) 2024,2025 Enrico Thierbach - https://github.com/radiospiel | 
|  | 4 | +
 | 
|  | 5 | +This file is released under the terms of the MIT License. It is based on the | 
|  | 6 | +work of James Edward Anhalt III, with the original license listed below. | 
|  | 7 | +
 | 
|  | 8 | +MIT License | 
|  | 9 | +
 | 
|  | 10 | +Permission is hereby granted, free of charge, to any person obtaining a copy | 
|  | 11 | +of this software and associated documentation files (the "Software"), to deal | 
|  | 12 | +in the Software without restriction, including without limitation the rights | 
|  | 13 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|  | 14 | +copies of the Software, and to permit persons to whom the Software is | 
|  | 15 | +furnished to do so, subject to the following conditions: | 
|  | 16 | +
 | 
|  | 17 | +The above copyright notice and this permission notice shall be included in all | 
|  | 18 | +copies or substantial portions of the Software. | 
|  | 19 | +
 | 
|  | 20 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
|  | 21 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|  | 22 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
|  | 23 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
|  | 24 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
|  | 25 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 
|  | 26 | +SOFTWARE. | 
|  | 27 | +*/ | 
|  | 28 | + | 
|  | 29 | +/* | 
|  | 30 | +MIT License | 
|  | 31 | +
 | 
|  | 32 | +Copyright (c) 2022 James Edward Anhalt III - https://github.com/jeaiii/itoa | 
|  | 33 | +
 | 
|  | 34 | +Permission is hereby granted, free of charge, to any person obtaining a copy | 
|  | 35 | +of this software and associated documentation files (the "Software"), to deal | 
|  | 36 | +in the Software without restriction, including without limitation the rights | 
|  | 37 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|  | 38 | +copies of the Software, and to permit persons to whom the Software is | 
|  | 39 | +furnished to do so, subject to the following conditions: | 
|  | 40 | +
 | 
|  | 41 | +The above copyright notice and this permission notice shall be included in all | 
|  | 42 | +copies or substantial portions of the Software. | 
|  | 43 | +
 | 
|  | 44 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
|  | 45 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|  | 46 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
|  | 47 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
|  | 48 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
|  | 49 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 
|  | 50 | +SOFTWARE. | 
|  | 51 | +*/ | 
|  | 52 | + | 
|  | 53 | +#ifndef JEAIII_TO_TEXT_H_ | 
|  | 54 | +#define JEAIII_TO_TEXT_H_ | 
|  | 55 | + | 
|  | 56 | +#include <stdint.h> | 
|  | 57 | + | 
|  | 58 | +typedef uint_fast32_t u32_t; | 
|  | 59 | +typedef uint_fast64_t u64_t; | 
|  | 60 | + | 
|  | 61 | +#pragma clang diagnostic push | 
|  | 62 | +#pragma clang diagnostic ignored "-Wshorten-64-to-32" | 
|  | 63 | + | 
|  | 64 | +#pragma GCC diagnostic push | 
|  | 65 | +#pragma GCC diagnostic ignored "-Wmissing-braces" | 
|  | 66 | + | 
|  | 67 | +#define u32(x) ((u32_t)(x)) | 
|  | 68 | +#define u64(x) ((u64_t)(x)) | 
|  | 69 | + | 
|  | 70 | +struct pair | 
|  | 71 | +{ | 
|  | 72 | +    char dd[2]; | 
|  | 73 | +}; | 
|  | 74 | + | 
|  | 75 | +#define cast_to_pair_ptr(b) ((struct pair*)(void*)(b)) | 
|  | 76 | + | 
|  | 77 | +static struct pair digits_dd[100] = | 
|  | 78 | +{ | 
|  | 79 | +    { '0', '0' }, { '0', '1' }, { '0', '2' }, { '0', '3' }, { '0', '4' }, { '0', '5' }, { '0', '6' }, { '0', '7' }, { '0', '8' }, { '0', '9' }, | 
|  | 80 | +    { '1', '0' }, { '1', '1' }, { '1', '2' }, { '1', '3' }, { '1', '4' }, { '1', '5' }, { '1', '6' }, { '1', '7' }, { '1', '8' }, { '1', '9' }, | 
|  | 81 | +    { '2', '0' }, { '2', '1' }, { '2', '2' }, { '2', '3' }, { '2', '4' }, { '2', '5' }, { '2', '6' }, { '2', '7' }, { '2', '8' }, { '2', '9' }, | 
|  | 82 | +    { '3', '0' }, { '3', '1' }, { '3', '2' }, { '3', '3' }, { '3', '4' }, { '3', '5' }, { '3', '6' }, { '3', '7' }, { '3', '8' }, { '3', '9' }, | 
|  | 83 | +    { '4', '0' }, { '4', '1' }, { '4', '2' }, { '4', '3' }, { '4', '4' }, { '4', '5' }, { '4', '6' }, { '4', '7' }, { '4', '8' }, { '4', '9' }, | 
|  | 84 | +    { '5', '0' }, { '5', '1' }, { '5', '2' }, { '5', '3' }, { '5', '4' }, { '5', '5' }, { '5', '6' }, { '5', '7' }, { '5', '8' }, { '5', '9' }, | 
|  | 85 | +    { '6', '0' }, { '6', '1' }, { '6', '2' }, { '6', '3' }, { '6', '4' }, { '6', '5' }, { '6', '6' }, { '6', '7' }, { '6', '8' }, { '6', '9' }, | 
|  | 86 | +    { '7', '0' }, { '7', '1' }, { '7', '2' }, { '7', '3' }, { '7', '4' }, { '7', '5' }, { '7', '6' }, { '7', '7' }, { '7', '8' }, { '7', '9' }, | 
|  | 87 | +    { '8', '0' }, { '8', '1' }, { '8', '2' }, { '8', '3' }, { '8', '4' }, { '8', '5' }, { '8', '6' }, { '8', '7' }, { '8', '8' }, { '8', '9' }, | 
|  | 88 | +    { '9', '0' }, { '9', '1' }, { '9', '2' }, { '9', '3' }, { '9', '4' }, { '9', '5' }, { '9', '6' }, { '9', '7' }, { '9', '8' }, { '9', '9' }, | 
|  | 89 | +}; | 
|  | 90 | + | 
|  | 91 | +#define NUL 'x' | 
|  | 92 | + | 
|  | 93 | +static struct pair digits_fd[100] = | 
|  | 94 | +{ | 
|  | 95 | +    { '0', NUL }, { '1', NUL }, { '2', NUL }, { '3', NUL }, { '4', NUL }, { '5', NUL }, { '6', NUL }, { '7', NUL }, { '8', NUL }, { '9', NUL }, | 
|  | 96 | +    { '1', '0' }, { '1', '1' }, { '1', '2' }, { '1', '3' }, { '1', '4' }, { '1', '5' }, { '1', '6' }, { '1', '7' }, { '1', '8' }, { '1', '9' }, | 
|  | 97 | +    { '2', '0' }, { '2', '1' }, { '2', '2' }, { '2', '3' }, { '2', '4' }, { '2', '5' }, { '2', '6' }, { '2', '7' }, { '2', '8' }, { '2', '9' }, | 
|  | 98 | +    { '3', '0' }, { '3', '1' }, { '3', '2' }, { '3', '3' }, { '3', '4' }, { '3', '5' }, { '3', '6' }, { '3', '7' }, { '3', '8' }, { '3', '9' }, | 
|  | 99 | +    { '4', '0' }, { '4', '1' }, { '4', '2' }, { '4', '3' }, { '4', '4' }, { '4', '5' }, { '4', '6' }, { '4', '7' }, { '4', '8' }, { '4', '9' }, | 
|  | 100 | +    { '5', '0' }, { '5', '1' }, { '5', '2' }, { '5', '3' }, { '5', '4' }, { '5', '5' }, { '5', '6' }, { '5', '7' }, { '5', '8' }, { '5', '9' }, | 
|  | 101 | +    { '6', '0' }, { '6', '1' }, { '6', '2' }, { '6', '3' }, { '6', '4' }, { '6', '5' }, { '6', '6' }, { '6', '7' }, { '6', '8' }, { '6', '9' }, | 
|  | 102 | +    { '7', '0' }, { '7', '1' }, { '7', '2' }, { '7', '3' }, { '7', '4' }, { '7', '5' }, { '7', '6' }, { '7', '7' }, { '7', '8' }, { '7', '9' }, | 
|  | 103 | +    { '8', '0' }, { '8', '1' }, { '8', '2' }, { '8', '3' }, { '8', '4' }, { '8', '5' }, { '8', '6' }, { '8', '7' }, { '8', '8' }, { '8', '9' }, | 
|  | 104 | +    { '9', '0' }, { '9', '1' }, { '9', '2' }, { '9', '3' }, { '9', '4' }, { '9', '5' }, { '9', '6' }, { '9', '7' }, { '9', '8' }, { '9', '9' }, | 
|  | 105 | +}; | 
|  | 106 | + | 
|  | 107 | +#undef NUL | 
|  | 108 | + | 
|  | 109 | +static u64_t mask24 = (u64(1) << 24) - 1; | 
|  | 110 | +static u64_t mask32 = (u64(1) << 32) - 1; | 
|  | 111 | +static u64_t mask57 = (u64(1) << 57) - 1; | 
|  | 112 | + | 
|  | 113 | +static  | 
|  | 114 | +char* to_text_from_ulong(char* b, u64_t n) { | 
|  | 115 | +    if (n < u32(1e2)) | 
|  | 116 | +    { | 
|  | 117 | +        *cast_to_pair_ptr(b) = digits_fd[n]; | 
|  | 118 | +        return n < 10 ? b + 1 : b + 2; | 
|  | 119 | +    } | 
|  | 120 | +    if (n < u32(1e6)) | 
|  | 121 | +    { | 
|  | 122 | +        if (n < u32(1e4)) | 
|  | 123 | +        { | 
|  | 124 | +            u32_t f0 = u32(10 * (1 << 24) / 1e3 + 1) * n; | 
|  | 125 | +            *cast_to_pair_ptr(b) = digits_fd[f0 >> 24]; | 
|  | 126 | +            b -= n < u32(1e3); | 
|  | 127 | +            u32_t f2 = (f0 & mask24) * 100; | 
|  | 128 | +            *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 24]; | 
|  | 129 | +            return b + 4; | 
|  | 130 | +        } | 
|  | 131 | +        u64_t f0 = u64(10 * (1ull << 32ull)/ 1e5 + 1) * n; | 
|  | 132 | +        *cast_to_pair_ptr(b) = digits_fd[f0 >> 32]; | 
|  | 133 | +        b -= n < u32(1e5); | 
|  | 134 | +        u64_t f2 = (f0 & mask32) * 100; | 
|  | 135 | +        *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32]; | 
|  | 136 | +        u64_t f4 = (f2 & mask32) * 100; | 
|  | 137 | +        *cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32]; | 
|  | 138 | +        return b + 6; | 
|  | 139 | +    } | 
|  | 140 | +    if (n < u64(1ull << 32ull)) | 
|  | 141 | +    { | 
|  | 142 | +        if (n < u32(1e8)) | 
|  | 143 | +        { | 
|  | 144 | +            u64_t f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * n >> 16; | 
|  | 145 | +            *cast_to_pair_ptr(b) = digits_fd[f0 >> 32]; | 
|  | 146 | +            b -= n < u32(1e7); | 
|  | 147 | +            u64_t f2 = (f0 & mask32) * 100; | 
|  | 148 | +            *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32]; | 
|  | 149 | +            u64_t f4 = (f2 & mask32) * 100; | 
|  | 150 | +            *cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32]; | 
|  | 151 | +            u64_t f6 = (f4 & mask32) * 100; | 
|  | 152 | +            *cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32]; | 
|  | 153 | +            return b + 8; | 
|  | 154 | +        } | 
|  | 155 | +        u64_t f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * n; | 
|  | 156 | +        *cast_to_pair_ptr(b) = digits_fd[f0 >> 57]; | 
|  | 157 | +        b -= n < u32(1e9); | 
|  | 158 | +        u64_t f2 = (f0 & mask57) * 100; | 
|  | 159 | +        *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 57]; | 
|  | 160 | +        u64_t f4 = (f2 & mask57) * 100; | 
|  | 161 | +        *cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 57]; | 
|  | 162 | +        u64_t f6 = (f4 & mask57) * 100; | 
|  | 163 | +        *cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 57]; | 
|  | 164 | +        u64_t f8 = (f6 & mask57) * 100; | 
|  | 165 | +        *cast_to_pair_ptr(b + 8) = digits_dd[f8 >> 57]; | 
|  | 166 | +        return b + 10; | 
|  | 167 | +    } | 
|  | 168 | + | 
|  | 169 | +    // if we get here U must be u64 but some compilers don't know that, so reassign n to a u64 to avoid warnings | 
|  | 170 | +    u32_t z = n % u32(1e8); | 
|  | 171 | +    u64_t u = n / u32(1e8); | 
|  | 172 | + | 
|  | 173 | +    if (u < u32(1e2)) | 
|  | 174 | +    { | 
|  | 175 | +        // u can't be 1 digit (if u < 10 it would have been handled above as a 9 digit 32bit number) | 
|  | 176 | +        *cast_to_pair_ptr(b) = digits_dd[u]; | 
|  | 177 | +        b += 2; | 
|  | 178 | +    } | 
|  | 179 | +    else if (u < u32(1e6)) | 
|  | 180 | +    { | 
|  | 181 | +        if (u < u32(1e4)) | 
|  | 182 | +        { | 
|  | 183 | +            u32_t f0 = u32(10 * (1 << 24) / 1e3 + 1) * u; | 
|  | 184 | +            *cast_to_pair_ptr(b) = digits_fd[f0 >> 24]; | 
|  | 185 | +            b -= u < u32(1e3); | 
|  | 186 | +            u32_t f2 = (f0 & mask24) * 100; | 
|  | 187 | +            *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 24]; | 
|  | 188 | +            b += 4; | 
|  | 189 | +        } | 
|  | 190 | +        else | 
|  | 191 | +        { | 
|  | 192 | +            u64_t f0 = u64(10 * (1ull << 32ull) / 1e5 + 1) * u; | 
|  | 193 | +            *cast_to_pair_ptr(b) = digits_fd[f0 >> 32]; | 
|  | 194 | +            b -= u < u32(1e5); | 
|  | 195 | +            u64_t f2 = (f0 & mask32) * 100; | 
|  | 196 | +            *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32]; | 
|  | 197 | +            u64_t f4 = (f2 & mask32) * 100; | 
|  | 198 | +            *cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32]; | 
|  | 199 | +            b += 6; | 
|  | 200 | +        } | 
|  | 201 | +    } | 
|  | 202 | +    else if (u < u32(1e8)) | 
|  | 203 | +    { | 
|  | 204 | +        u64_t f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * u >> 16; | 
|  | 205 | +        *cast_to_pair_ptr(b) = digits_fd[f0 >> 32]; | 
|  | 206 | +        b -= u < u32(1e7); | 
|  | 207 | +        u64_t f2 = (f0 & mask32) * 100; | 
|  | 208 | +        *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32]; | 
|  | 209 | +        u64_t f4 = (f2 & mask32) * 100; | 
|  | 210 | +        *cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32]; | 
|  | 211 | +        u64_t f6 = (f4 & mask32) * 100; | 
|  | 212 | +        *cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32]; | 
|  | 213 | +        b += 8; | 
|  | 214 | +    } | 
|  | 215 | +    else if (u < u64(1ull << 32ull)) | 
|  | 216 | +    { | 
|  | 217 | +        u64_t f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * u; | 
|  | 218 | +        *cast_to_pair_ptr(b) = digits_fd[f0 >> 57]; | 
|  | 219 | +        b -= u < u32(1e9); | 
|  | 220 | +        u64_t f2 = (f0 & mask57) * 100; | 
|  | 221 | +        *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 57]; | 
|  | 222 | +        u64_t f4 = (f2 & mask57) * 100; | 
|  | 223 | +        *cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 57]; | 
|  | 224 | +        u64_t f6 = (f4 & mask57) * 100; | 
|  | 225 | +        *cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 57]; | 
|  | 226 | +        u64_t f8 = (f6 & mask57) * 100; | 
|  | 227 | +        *cast_to_pair_ptr(b + 8) = digits_dd[f8 >> 57]; | 
|  | 228 | +        b += 10; | 
|  | 229 | +    } | 
|  | 230 | +    else | 
|  | 231 | +    { | 
|  | 232 | +        u32_t y = u % u32(1e8); | 
|  | 233 | +        u /= u32(1e8); | 
|  | 234 | + | 
|  | 235 | +        // u is 2, 3, or 4 digits (if u < 10 it would have been handled above) | 
|  | 236 | +        if (u < u32(1e2)) | 
|  | 237 | +        { | 
|  | 238 | +            *cast_to_pair_ptr(b) = digits_dd[u]; | 
|  | 239 | +            b += 2; | 
|  | 240 | +        } | 
|  | 241 | +        else | 
|  | 242 | +        { | 
|  | 243 | +            u32_t f0 = u32(10 * (1 << 24) / 1e3 + 1) * u; | 
|  | 244 | +            *cast_to_pair_ptr(b) = digits_fd[f0 >> 24]; | 
|  | 245 | +            b -= u < u32(1e3); | 
|  | 246 | +            u32_t f2 = (f0 & mask24) * 100; | 
|  | 247 | +            *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 24]; | 
|  | 248 | +            b += 4; | 
|  | 249 | +        } | 
|  | 250 | +        // do 8 digits | 
|  | 251 | +        u64_t f0 = (u64((1ull << 48ull) / 1e6 + 1) * y >> 16) + 1; | 
|  | 252 | +        *cast_to_pair_ptr(b) = digits_dd[f0 >> 32]; | 
|  | 253 | +        u64_t f2 = (f0 & mask32) * 100; | 
|  | 254 | +        *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32]; | 
|  | 255 | +        u64_t f4 = (f2 & mask32) * 100; | 
|  | 256 | +        *cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32]; | 
|  | 257 | +        u64_t f6 = (f4 & mask32) * 100; | 
|  | 258 | +        *cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32]; | 
|  | 259 | +        b += 8; | 
|  | 260 | +    } | 
|  | 261 | +    // do 8 digits | 
|  | 262 | +    u64_t f0 = (u64((1ull << 48ull) / 1e6 + 1) * z >> 16) + 1; | 
|  | 263 | +    *cast_to_pair_ptr(b) = digits_dd[f0 >> 32]; | 
|  | 264 | +    u64_t f2 = (f0 & mask32) * 100; | 
|  | 265 | +    *cast_to_pair_ptr(b + 2) = digits_dd[f2 >> 32]; | 
|  | 266 | +    u64_t f4 = (f2 & mask32) * 100; | 
|  | 267 | +    *cast_to_pair_ptr(b + 4) = digits_dd[f4 >> 32]; | 
|  | 268 | +    u64_t f6 = (f4 & mask32) * 100; | 
|  | 269 | +    *cast_to_pair_ptr(b + 6) = digits_dd[f6 >> 32]; | 
|  | 270 | +    return b + 8; | 
|  | 271 | +} | 
|  | 272 | + | 
|  | 273 | +#undef u32 | 
|  | 274 | +#undef u64 | 
|  | 275 | + | 
|  | 276 | +#pragma clang diagnostic pop | 
|  | 277 | +#pragma GCC diagnostic pop | 
|  | 278 | + | 
|  | 279 | +#endif // JEAIII_TO_TEXT_H_ | 
|  | 280 | + | 
0 commit comments