dragonbox_to_chars.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // The contents of this file is based on contents of:
  2. //
  3. // https://github.com/ulfjack/ryu/blob/master/ryu/common.h,
  4. // https://github.com/ulfjack/ryu/blob/master/ryu/d2s.c, and
  5. // https://github.com/ulfjack/ryu/blob/master/ryu/f2s.c,
  6. //
  7. // which are distributed under the following terms:
  8. //--------------------------------------------------------------------------------
  9. // Copyright 2018 Ulf Adams
  10. //
  11. // The contents of this file may be used under the terms of the Apache License,
  12. // Version 2.0.
  13. //
  14. // (See accompanying file LICENSE-Apache or copy at
  15. // http://www.apache.org/licenses/LICENSE-2.0)
  16. //
  17. // Alternatively, the contents of this file may be used under the terms of
  18. // the Boost Software License, Version 1.0.
  19. // (See accompanying file LICENSE-Boost or copy at
  20. // https://www.boost.org/LICENSE_1_0.txt)
  21. //
  22. // Unless required by applicable law or agreed to in writing, this software
  23. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  24. // KIND, either express or implied.
  25. //--------------------------------------------------------------------------------
  26. // Modifications Copyright 2020 Junekey Jeon
  27. //
  28. // Following modifications were made to the original contents:
  29. // - Put everything inside the namespace jkj::dragonbox::to_chars_detail
  30. // - Combined decimalLength9 (from common.h) and decimalLength17 (from d2s.c)
  31. // into a single template function decimal_length
  32. // - Combined to_chars (from f2s.c) and to_chars (from d2s.c) into a
  33. // single template function fp_to_chars_impl
  34. // - Removed index counting statements; replaced them with pointer increments
  35. // - Removed usages of DIGIT_TABLE; replaced them with radix_100_table
  36. //
  37. // These modifications, together with other contents of this file may be used
  38. // under the same terms as the original contents.
  39. #include "dragonbox/dragonbox_to_chars.h"
  40. namespace jkj::dragonbox {
  41. namespace to_chars_detail {
  42. static constexpr char radix_100_table[] = {
  43. '0', '0', '0', '1', '0', '2', '0', '3', '0', '4',
  44. '0', '5', '0', '6', '0', '7', '0', '8', '0', '9',
  45. '1', '0', '1', '1', '1', '2', '1', '3', '1', '4',
  46. '1', '5', '1', '6', '1', '7', '1', '8', '1', '9',
  47. '2', '0', '2', '1', '2', '2', '2', '3', '2', '4',
  48. '2', '5', '2', '6', '2', '7', '2', '8', '2', '9',
  49. '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
  50. '3', '5', '3', '6', '3', '7', '3', '8', '3', '9',
  51. '4', '0', '4', '1', '4', '2', '4', '3', '4', '4',
  52. '4', '5', '4', '6', '4', '7', '4', '8', '4', '9',
  53. '5', '0', '5', '1', '5', '2', '5', '3', '5', '4',
  54. '5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
  55. '6', '0', '6', '1', '6', '2', '6', '3', '6', '4',
  56. '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
  57. '7', '0', '7', '1', '7', '2', '7', '3', '7', '4',
  58. '7', '5', '7', '6', '7', '7', '7', '8', '7', '9',
  59. '8', '0', '8', '1', '8', '2', '8', '3', '8', '4',
  60. '8', '5', '8', '6', '8', '7', '8', '8', '8', '9',
  61. '9', '0', '9', '1', '9', '2', '9', '3', '9', '4',
  62. '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'
  63. };
  64. template <class UInt>
  65. static constexpr std::uint32_t decimal_length(UInt const v) {
  66. if constexpr (std::is_same_v<UInt, std::uint32_t>) {
  67. // Function precondition: v is not a 10-digit number.
  68. // (f2s: 9 digits are sufficient for round-tripping.)
  69. // (d2fixed: We print 9-digit blocks.)
  70. assert(v < 1000000000);
  71. if (v >= 100000000) { return 9; }
  72. if (v >= 10000000) { return 8; }
  73. if (v >= 1000000) { return 7; }
  74. if (v >= 100000) { return 6; }
  75. if (v >= 10000) { return 5; }
  76. if (v >= 1000) { return 4; }
  77. if (v >= 100) { return 3; }
  78. if (v >= 10) { return 2; }
  79. return 1;
  80. }
  81. else {
  82. static_assert(std::is_same_v<UInt, std::uint64_t>);
  83. // This is slightly faster than a loop.
  84. // The average output length is 16.38 digits, so we check high-to-low.
  85. // Function precondition: v is not an 18, 19, or 20-digit number.
  86. // (17 digits are sufficient for round-tripping.)
  87. assert(v < 100000000000000000L);
  88. if (v >= 10000000000000000L) { return 17; }
  89. if (v >= 1000000000000000L) { return 16; }
  90. if (v >= 100000000000000L) { return 15; }
  91. if (v >= 10000000000000L) { return 14; }
  92. if (v >= 1000000000000L) { return 13; }
  93. if (v >= 100000000000L) { return 12; }
  94. if (v >= 10000000000L) { return 11; }
  95. if (v >= 1000000000L) { return 10; }
  96. if (v >= 100000000L) { return 9; }
  97. if (v >= 10000000L) { return 8; }
  98. if (v >= 1000000L) { return 7; }
  99. if (v >= 100000L) { return 6; }
  100. if (v >= 10000L) { return 5; }
  101. if (v >= 1000L) { return 4; }
  102. if (v >= 100L) { return 3; }
  103. if (v >= 10L) { return 2; }
  104. return 1;
  105. }
  106. }
  107. template <class Float>
  108. static char* to_chars_impl(unsigned_fp_t<Float> v, char* buffer)
  109. {
  110. auto output = v.significand;
  111. auto const olength = decimal_length(output);
  112. int32_t exp = v.exponent + (int32_t)olength - 1;
  113. if (exp >= -6 && exp <= 20)
  114. {
  115. int index = 0;
  116. if (exp < 0)
  117. {
  118. buffer[index++] = '0';
  119. buffer[index++] = '.';
  120. while (++exp)
  121. buffer[index++] = '0';
  122. for (int32_t i = olength - 1; i >= 0; --i)
  123. {
  124. const uint32_t c = output % 10;
  125. output /= 10;
  126. buffer[index + i] = '0' + c;
  127. }
  128. index += olength;
  129. }
  130. else if (exp + 1 >= olength)
  131. {
  132. for (int32_t i = olength - 1; i >= 0; --i)
  133. {
  134. const uint32_t c = output % 10;
  135. output /= 10;
  136. buffer[index + i] = '0' + c;
  137. }
  138. index += olength;
  139. while (exp >= olength)
  140. {
  141. buffer[index++] = '0';
  142. --exp;
  143. }
  144. }
  145. else
  146. {
  147. for (int32_t i = olength; i > exp + 1; --i)
  148. {
  149. const uint32_t c = output % 10;
  150. output /= 10;
  151. buffer[index + i] = '0' + c;
  152. }
  153. buffer[index + exp + 1] = '.';
  154. for (int32_t i = exp; i >= 0; --i)
  155. {
  156. const uint32_t c = output % 10;
  157. output /= 10;
  158. buffer[index + i] = '0' + c;
  159. }
  160. index += olength + 1;
  161. }
  162. return buffer + index;
  163. }
  164. // Print the decimal digits.
  165. // The following code is equivalent to:
  166. // for (uint32_t i = 0; i < olength - 1; ++i) {
  167. // const uint32_t c = output % 10; output /= 10;
  168. // result[index + olength - i] = (char) ('0' + c);
  169. // }
  170. // result[index] = '0' + output % 10;
  171. uint32_t i = 0;
  172. if constexpr (sizeof(Float) == 8) {
  173. // We prefer 32-bit operations, even on 64-bit platforms.
  174. // We have at most 17 digits, and uint32_t can store 9 digits.
  175. // If output doesn't fit into uint32_t, we cut off 8 digits,
  176. // so the rest will fit into uint32_t.
  177. if ((output >> 32) != 0) {
  178. // Expensive 64-bit division.
  179. const uint64_t q = output / 100000000;
  180. uint32_t output2 = ((uint32_t)output) - 100000000 * ((uint32_t)q);
  181. output = q;
  182. const uint32_t c = output2 % 10000;
  183. output2 /= 10000;
  184. const uint32_t d = output2 % 10000;
  185. const uint32_t c0 = (c % 100) << 1;
  186. const uint32_t c1 = (c / 100) << 1;
  187. const uint32_t d0 = (d % 100) << 1;
  188. const uint32_t d1 = (d / 100) << 1;
  189. memcpy(buffer + olength - i - 1, radix_100_table + c0, 2);
  190. memcpy(buffer + olength - i - 3, radix_100_table + c1, 2);
  191. memcpy(buffer + olength - i - 5, radix_100_table + d0, 2);
  192. memcpy(buffer + olength - i - 7, radix_100_table + d1, 2);
  193. i += 8;
  194. }
  195. }
  196. auto output2 = (uint32_t)output;
  197. while (output2 >= 10000) {
  198. #ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=38217
  199. const uint32_t c = output2 - 10000 * (output2 / 10000);
  200. #else
  201. const uint32_t c = output2 % 10000;
  202. #endif
  203. output2 /= 10000;
  204. const uint32_t c0 = (c % 100) << 1;
  205. const uint32_t c1 = (c / 100) << 1;
  206. memcpy(buffer + olength - i - 1, radix_100_table + c0, 2);
  207. memcpy(buffer + olength - i - 3, radix_100_table + c1, 2);
  208. i += 4;
  209. }
  210. if (output2 >= 100) {
  211. const uint32_t c = (output2 % 100) << 1;
  212. output2 /= 100;
  213. memcpy(buffer + olength - i - 1, radix_100_table + c, 2);
  214. i += 2;
  215. }
  216. if (output2 >= 10) {
  217. const uint32_t c = output2 << 1;
  218. // We can't use memcpy here: the decimal dot goes between these two digits.
  219. buffer[olength - i] = radix_100_table[c + 1];
  220. buffer[0] = radix_100_table[c];
  221. }
  222. else {
  223. buffer[0] = (char)('0' + output2);
  224. }
  225. // Print decimal point if needed.
  226. if (olength > 1) {
  227. buffer[1] = '.';
  228. buffer += olength + 1;
  229. }
  230. else {
  231. ++buffer;
  232. }
  233. // Print the exponent.
  234. *buffer = 'e';
  235. ++buffer;
  236. if (exp < 0) {
  237. *buffer = '-';
  238. ++buffer;
  239. exp = -exp;
  240. }
  241. if constexpr (sizeof(Float) == 8) {
  242. if (exp >= 100) {
  243. const int32_t c = exp % 10;
  244. memcpy(buffer, radix_100_table + 2 * (exp / 10), 2);
  245. buffer[2] = (char)('0' + c);
  246. buffer += 3;
  247. }
  248. else if (exp >= 10) {
  249. memcpy(buffer, radix_100_table + 2 * exp, 2);
  250. buffer += 2;
  251. }
  252. else {
  253. *buffer = (char)('0' + exp);
  254. ++buffer;
  255. }
  256. }
  257. else {
  258. if (exp >= 10) {
  259. memcpy(buffer, radix_100_table + 2 * exp, 2);
  260. buffer += 2;
  261. }
  262. else {
  263. *buffer = (char)('0' + exp);
  264. ++buffer;
  265. }
  266. }
  267. return buffer;
  268. }
  269. char* to_chars(unsigned_fp_t<float> v, char* buffer) {
  270. return to_chars_impl(v, buffer);
  271. }
  272. char* to_chars(unsigned_fp_t<double> v, char* buffer) {
  273. return to_chars_impl(v, buffer);
  274. }
  275. }
  276. }