_uintmaxtostr.c 3.9 KB

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