_uintmaxtostr.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  8. #include "_stdio.h"
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <bits/uClibc_uintmaxtostr.h>
  12. /* Experimentally off - libc_hidden_proto(memcpy) */
  13. /* Avoid using long long / and % operations to cut down dependencies on
  14. * libgcc.a. Definitely helps on i386 at least. */
  15. #if (INTMAX_MAX > INT_MAX) && (((INTMAX_MAX/INT_MAX)/2) - 2 <= INT_MAX)
  16. #define INTERNAL_DIV_MOD
  17. #endif
  18. char attribute_hidden *_uintmaxtostr(register char * __restrict bufend, uintmax_t uval,
  19. int base, __UIM_CASE alphacase)
  20. {
  21. int negative;
  22. unsigned int digit;
  23. #ifdef INTERNAL_DIV_MOD
  24. unsigned int H, L, high, low, rh;
  25. #endif
  26. #ifndef __LOCALE_C_ONLY
  27. int grouping, outdigit;
  28. const char *g; /* This does not need to be initialized. */
  29. #endif /* __LOCALE_C_ONLY */
  30. negative = 0;
  31. if (base < 0) { /* signed value */
  32. base = -base;
  33. if (uval > INTMAX_MAX) {
  34. uval = -uval;
  35. negative = 1;
  36. }
  37. }
  38. /* this is an internal routine -- we shouldn't need to check this */
  39. assert(!((base < 2) || (base > 36)));
  40. #ifndef __LOCALE_C_ONLY
  41. grouping = -1;
  42. outdigit = 0x80 & alphacase;
  43. alphacase ^= outdigit;
  44. if (alphacase == __UIM_GROUP) {
  45. assert(base == 10);
  46. if (*(g = __UCLIBC_CURLOCALE->grouping)) {
  47. grouping = *g;
  48. }
  49. }
  50. #endif /* __LOCALE_C_ONLY */
  51. *bufend = '\0';
  52. #ifndef INTERNAL_DIV_MOD
  53. do {
  54. #ifndef __LOCALE_C_ONLY
  55. if (!grouping) { /* Finished a group. */
  56. bufend -= __UCLIBC_CURLOCALE->thousands_sep_len;
  57. memcpy(bufend, __UCLIBC_CURLOCALE->thousands_sep,
  58. __UCLIBC_CURLOCALE->thousands_sep_len);
  59. if (g[1] != 0) { /* g[1] == 0 means repeat last grouping. */
  60. /* Note: g[1] == -1 means no further grouping. But since
  61. * we'll never wrap around, we can set grouping to -1 without
  62. * fear of */
  63. ++g;
  64. }
  65. grouping = *g;
  66. }
  67. --grouping;
  68. #endif /* __LOCALE_C_ONLY */
  69. digit = uval % base;
  70. uval /= base;
  71. #ifndef __LOCALE_C_ONLY
  72. if (unlikely(outdigit)) {
  73. bufend -= __UCLIBC_CURLOCALE->outdigit_length[digit];
  74. memcpy(bufend,
  75. (&__UCLIBC_CURLOCALE->outdigit0_mb)[digit],
  76. __UCLIBC_CURLOCALE->outdigit_length[digit]);
  77. } else
  78. #endif
  79. {
  80. *--bufend = ( (digit < 10) ? digit + '0' : digit + alphacase );
  81. }
  82. } while (uval);
  83. #else /* ************************************************** */
  84. H = (UINT_MAX / base);
  85. L = UINT_MAX % base + 1;
  86. if (L == base) {
  87. ++H;
  88. L = 0;
  89. }
  90. low = (unsigned int) uval;
  91. high = (unsigned int) (uval >> (sizeof(unsigned int) * CHAR_BIT));
  92. do {
  93. #ifndef __LOCALE_C_ONLY
  94. if (!grouping) { /* Finished a group. */
  95. bufend -= __UCLIBC_CURLOCALE->thousands_sep_len;
  96. memcpy(bufend, __UCLIBC_CURLOCALE->thousands_sep,
  97. __UCLIBC_CURLOCALE->thousands_sep_len);
  98. if (g[1] != 0) { /* g[1] == 0 means repeat last grouping. */
  99. /* Note: g[1] == -1 means no further grouping. But since
  100. * we'll never wrap around, we can set grouping to -1 without
  101. * fear of */
  102. ++g;
  103. }
  104. grouping = *g;
  105. }
  106. --grouping;
  107. #endif /* __LOCALE_C_ONLY */
  108. if (unlikely(high)) {
  109. rh = high % base;
  110. high /= base;
  111. digit = (low % base) + (L * rh);
  112. low = (low / base) + (H * rh) + (digit / base);
  113. digit %= base;
  114. } else {
  115. digit = low % base;
  116. low /= base;
  117. }
  118. #ifndef __LOCALE_C_ONLY
  119. if (unlikely(outdigit)) {
  120. bufend -= __UCLIBC_CURLOCALE->outdigit_length[digit];
  121. memcpy(bufend,
  122. (&__UCLIBC_CURLOCALE->outdigit0_mb)[digit],
  123. __UCLIBC_CURLOCALE->outdigit_length[digit]);
  124. } else
  125. #endif
  126. {
  127. *--bufend = ( (digit < 10) ? digit + '0' : digit + alphacase );
  128. }
  129. } while (low | high);
  130. #endif /******************************************************/
  131. if (negative) {
  132. *--bufend = '-';
  133. }
  134. return bufend;
  135. }