<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/89085>89085</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Suggestion for implementing runtime defined _BitInt
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          awsdert
      </td>
    </tr>
</table>

<pre>
    I'm writing a custom library and I'd like to support all bit widths supported by _BitInt. As you can imagine doing a custom va_arg for each bit width is just unreasonable. I understand that the reason it's not implemented yet is lack of implementation ideas. Here's my suggestion:

The C should look like:
```
typedef struct { size_t leng; char text[BITINT_MAX+2]; } stdbitext_t;
extern __thread stdbitext_t *stdbitext;
#ifdef BUILD_LIBC
typedef _BitInt(BITINT_MAXWIDTH) bimax_t;
typedef unsigned _BitInt(BITINT_MAXWIDTH) ubimax_t;
typedef struct { ubimax quo, rem, ...; };
extern __thread stdbitmps_t *stdbitmps;
#endif
...
        printf( "%wu %wd", (unsigned _BitInt(N))1, (_BitInt(N))-1 );
...
int snprintf(...)
{
        ...
        size_t len = 0;
        ...
        unsigned _BitInt(...) biu = va_arg( va, _BitInt(...) );
        stdbiutostr( stdbitext, biu );
        ...
        strncat(dst,stdbitext->text,stdbitext->leng);
        ...
        _BitInt(...) bid = va_arg( va, _BitInt(...) );
        stdbidtostr( stdbitext, bid );
        ...
        strncat(dst,stdbitext->text,stdbitext->leng);
        ...
}
```
The `_BitInt(...)` should resolve to something like `struct { size_t width; void *biptr; } binum;`. The stdbitmps is an optimization thing to avoid constantly allocating large amounts of stack memory (I suggest doing likewise for the _BitInt(N)s if their big enough) just to copy values that should not be modified. 

Likewise with stdbitext, however since `stdbiutostr` and `stdbidtostr` are to be exposed to the user of clang I expect there'll be cases where peops want to keep the output for a little longer so I suggest making using stdbitext optional. It should be treated as an error to give NULL to those functions and the act of doing so should segfault during runtime.

To keep the size of binaries down when any non-native width is used I suggest using functions instead of inline asm when the values are to big for any kind of register on the CPU. Likewise if there are many non-std widths in use.

In all cases when _BitInt(N) != the width of whatever _BitInt(...) segments will be treated as they should be copied into stack elements of suitable size prior to being passed into the va_list. This will simplify the math functions greatly since they only need to be optimised for one segment size. So for instance _BitInt(1) could always upgrade to `qword` when passed into a va_list but once the function exits it gets copied back to it's source if the code was something like:

```
_BitInt(1) mybool = 0;
snscanf( "%wu", &mybool );
printf( "mybool = %s\n", mybool ? "true" : "false" );
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V0Fv4zoO_jXKhZjAUZqkOeTQNFu8AN3BAm8Gu7dAthhbr7bkJ1FN_X79grKdOJ2ZXWAPCwycsSWS30dKH1kVgikt4k6s9mJ1mKlIlfM7dQkaPc1yp7vdUchNAxdvyNgSFBQxkGugNrlXvgNlNfAWDbV5QyAHIbat8wSqriE3BBejqQrjZ9SQd3DaGzpamsNTgM5FKJQF06jSWATt7gK9q5PyJZydB1RFdXMJJsAfMRBE61EFZ1Ve4xyOEK1GH4iRUaUIqELod4AhITcBrCMwTVtjg5YRdUjsrVbFG7jzbUmRYSONKszhN_SYrJsOQixLDLwqlk8iO4hseH6rEJ4hVC7WGmrn3lJabpvW2fAvvVLXosYzBPKxIBCbPQTzF54IarSlWO6hqJQHwg8Sq_3--O349dvp70__EnIvxerAG8TmAIF0bnjTicRy37vGD0Jv4XSiyqPS0z0g5NP19Wog5NKcGcz--_H1cHo97p_vQQ41E_LxBuSfx8O334TcQm4a9TEJPxpFm06Y_s_W8Vfmk8T0e-DP6IR8Bo8N_8zn8yEJ_4V404Yp8aYNU-JotTn3b-xxqOe29cbSWchHEFIKubpE4KdOb88g5ONP6H0VcivkdjHs-GHhywL4P2P0azxjCYK9huTvcjtA2eyvmKb4bmcFxPIA2Y3S_b6foOzdQ25iMu0vGTN9Vwz8h41TyByZsxjJBfJsdDtN8rn3eb_9DjR5Wyh2rQPvv9p-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