_uintmaxtostr.c 3.7 KB

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