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