utoa_invert.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (c) 2005, Dmitry Xmelkov
  2. All rights reserved.
  3. Rewritten in C by Soren Kuula
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of the copyright holders nor the names of
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE. */
  26. #include <stdint.h>
  27. #include "xtoa_fast.h"
  28. char * ultoa_invert (uint32_t val, char *s, uint8_t base) {
  29. if (base == 8) {
  30. do {
  31. *s = '0' + (val & 0x7);
  32. val >>= 3;
  33. } while(val);
  34. return s;
  35. }
  36. if (base == 16) {
  37. do {
  38. uint8_t digit = '0' + (val & 0xf);
  39. #if XTOA_UPPER == 0
  40. if (digit > '0' + 9)
  41. digit += ('a' - '0' - 10);
  42. #else
  43. if (digit > '0' + 9)
  44. digit += ('A' - '0' - 10);
  45. #endif
  46. *s++ = digit;
  47. val >>= 4;
  48. } while(val);
  49. return s;
  50. }
  51. // Every base which in not hex and not oct is considered decimal.
  52. // 33 bits would have been enough.
  53. uint64_t xval = val;
  54. do {
  55. uint8_t saved = xval;
  56. xval &= ~1;
  57. xval += 2;
  58. xval += xval >> 1; // *1.5
  59. xval += xval >> 4; // *1.0625
  60. xval += xval >> 8; // *1.00390625
  61. xval += xval >> 16; // *1.000015259
  62. xval += xval >> 32; // it all amounts to *1.6
  63. xval >>= 4; // /16 ... so *1.6/16 is /10, fraction truncated.
  64. *s++ = '0' + saved - 10 * (uint8_t)xval;
  65. } while (xval);
  66. return s;
  67. }
  68. char * ulltoa_invert (uint64_t val, char *s, uint8_t base) {
  69. if (base == 8) {
  70. do {
  71. *s = '0' + (val & 0x7);
  72. val >>= 3;
  73. } while(val);
  74. return s;
  75. }
  76. if (base == 16) {
  77. do {
  78. uint8_t digit = '0' + (val & 0xf);
  79. #if XTOA_UPPER == 0
  80. if (digit > '0' + 9)
  81. digit += ('a' - '0' - 10);
  82. #else
  83. if (digit > '0' + 9)
  84. digit += ('A' - '0' - 10);
  85. #endif
  86. *s++ = digit;
  87. val >>= 4;
  88. } while(val);
  89. return s;
  90. }
  91. // Every base which in not hex and not oct is considered decimal.
  92. // 64 bits is not actually enough, we need 65, but it should
  93. // be good enough for the log dumping we're using this for
  94. uint64_t xval = val;
  95. do {
  96. uint8_t saved = xval;
  97. xval &= ~1;
  98. xval += 2;
  99. xval += xval >> 1; // *1.5
  100. xval += xval >> 4; // *1.0625
  101. xval += xval >> 8; // *1.00390625
  102. xval += xval >> 16; // *1.000015259
  103. xval += xval >> 32; // it all amounts to *1.6
  104. xval >>= 4; // /16 ... so *1.6/16 is /10, fraction truncated.
  105. *s++ = '0' + saved - 10 * (uint8_t)xval;
  106. } while (xval);
  107. return s;
  108. }