1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
8 #define _ISOC99_SOURCE /* for ULLONG primarily... */
12 #include <bits/uClibc_uintmaxtostr.h>
15 /* Avoid using long long / and % operations to cut down dependencies on
16 * libgcc.a. Definitely helps on i386 at least. */
17 #if (INTMAX_MAX > INT_MAX) && (((INTMAX_MAX/INT_MAX)/2) - 2 <= INT_MAX)
18 #define INTERNAL_DIV_MOD
21 char attribute_hidden *_uintmaxtostr(register char * __restrict bufend, uintmax_t uval,
22 int base, __UIM_CASE alphacase)
26 #ifdef INTERNAL_DIV_MOD
27 unsigned int H, L, high, low, rh;
29 #ifndef __LOCALE_C_ONLY
30 int grouping, outdigit;
31 const char *g; /* This does not need to be initialized. */
32 #endif /* __LOCALE_C_ONLY */
35 if (base < 0) { /* signed value */
37 if (uval > INTMAX_MAX) {
43 /* this is an internal routine -- we shouldn't need to check this */
44 assert(!((base < 2) || (base > 36)));
46 #ifndef __LOCALE_C_ONLY
48 outdigit = 0x80 & alphacase;
49 alphacase ^= outdigit;
50 if (alphacase == __UIM_GROUP) {
52 if (*(g = __UCLIBC_CURLOCALE->grouping)) {
56 #endif /* __LOCALE_C_ONLY */
60 #ifndef INTERNAL_DIV_MOD
62 #ifndef __LOCALE_C_ONLY
63 if (!grouping) { /* Finished a group. */
64 bufend -= __UCLIBC_CURLOCALE->thousands_sep_len;
65 memcpy(bufend, __UCLIBC_CURLOCALE->thousands_sep,
66 __UCLIBC_CURLOCALE->thousands_sep_len);
67 if (g[1] != 0) { /* g[1] == 0 means repeat last grouping. */
68 /* Note: g[1] == -1 means no further grouping. But since
69 * we'll never wrap around, we can set grouping to -1 without
76 #endif /* __LOCALE_C_ONLY */
80 #ifndef __LOCALE_C_ONLY
81 if (unlikely(outdigit)) {
82 bufend -= __UCLIBC_CURLOCALE->outdigit_length[digit];
84 (&__UCLIBC_CURLOCALE->outdigit0_mb)[digit],
85 __UCLIBC_CURLOCALE->outdigit_length[digit]);
89 *--bufend = ( (digit < 10) ? digit + '0' : digit + alphacase );
93 #else /* ************************************************** */
95 H = (UINT_MAX / base);
96 L = UINT_MAX % base + 1;
101 low = (unsigned int) uval;
102 high = (unsigned int) (uval >> (sizeof(unsigned int) * CHAR_BIT));
105 #ifndef __LOCALE_C_ONLY
106 if (!grouping) { /* Finished a group. */
107 bufend -= __UCLIBC_CURLOCALE->thousands_sep_len;
108 memcpy(bufend, __UCLIBC_CURLOCALE->thousands_sep,
109 __UCLIBC_CURLOCALE->thousands_sep_len);
110 if (g[1] != 0) { /* g[1] == 0 means repeat last grouping. */
111 /* Note: g[1] == -1 means no further grouping. But since
112 * we'll never wrap around, we can set grouping to -1 without
119 #endif /* __LOCALE_C_ONLY */
121 if (unlikely(high)) {
124 digit = (low % base) + (L * rh);
125 low = (low / base) + (H * rh) + (digit / base);
132 #ifndef __LOCALE_C_ONLY
133 if (unlikely(outdigit)) {
134 bufend -= __UCLIBC_CURLOCALE->outdigit_length[digit];
136 (&__UCLIBC_CURLOCALE->outdigit0_mb)[digit],
137 __UCLIBC_CURLOCALE->outdigit_length[digit]);
141 *--bufend = ( (digit < 10) ? digit + '0' : digit + alphacase );
143 } while (low | high);
145 #endif /******************************************************/