lj_strfmt_num.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. ** String formatting for floating-point numbers.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. ** Contributed by Peter Cawley.
  5. */
  6. #include <stdio.h>
  7. #define lj_strfmt_num_c
  8. #define LUA_CORE
  9. #include "lj_obj.h"
  10. #include "lj_buf.h"
  11. #include "lj_str.h"
  12. #include "lj_strfmt.h"
  13. /* -- Precomputed tables -------------------------------------------------- */
  14. /* Rescale factors to push the exponent of a number towards zero. */
  15. #define RESCALE_EXPONENTS(P, N) \
  16. P(308), P(289), P(270), P(250), P(231), P(212), P(193), P(173), P(154), \
  17. P(135), P(115), P(96), P(77), P(58), P(38), P(0), P(0), P(0), N(39), N(58), \
  18. N(77), N(96), N(116), N(135), N(154), N(174), N(193), N(212), N(231), \
  19. N(251), N(270), N(289)
  20. #define ONE_E_P(X) 1e+0 ## X
  21. #define ONE_E_N(X) 1e-0 ## X
  22. static const int16_t rescale_e[] = { RESCALE_EXPONENTS(-, +) };
  23. static const double rescale_n[] = { RESCALE_EXPONENTS(ONE_E_P, ONE_E_N) };
  24. #undef ONE_E_N
  25. #undef ONE_E_P
  26. /*
  27. ** For p in range -70 through 57, this table encodes pairs (m, e) such that
  28. ** 4*2^p <= (uint8_t)m*10^e, and is the smallest value for which this holds.
  29. */
  30. static const int8_t four_ulp_m_e[] = {
  31. 34, -21, 68, -21, 14, -20, 28, -20, 55, -20, 2, -19, 3, -19, 5, -19, 9, -19,
  32. -82, -18, 35, -18, 7, -17, -117, -17, 28, -17, 56, -17, 112, -16, -33, -16,
  33. 45, -16, 89, -16, -78, -15, 36, -15, 72, -15, -113, -14, 29, -14, 57, -14,
  34. 114, -13, -28, -13, 46, -13, 91, -12, -74, -12, 37, -12, 73, -12, 15, -11, 3,
  35. -11, 59, -11, 2, -10, 3, -10, 5, -10, 1, -9, -69, -9, 38, -9, 75, -9, 15, -7,
  36. 3, -7, 6, -7, 12, -6, -17, -7, 48, -7, 96, -7, -65, -6, 39, -6, 77, -6, -103,
  37. -5, 31, -5, 62, -5, 123, -4, -11, -4, 49, -4, 98, -4, -60, -3, 4, -2, 79, -3,
  38. 16, -2, 32, -2, 63, -2, 2, -1, 25, 0, 5, 1, 1, 2, 2, 2, 4, 2, 8, 2, 16, 2,
  39. 32, 2, 64, 2, -128, 2, 26, 2, 52, 2, 103, 3, -51, 3, 41, 4, 82, 4, -92, 4,
  40. 33, 4, 66, 4, -124, 5, 27, 5, 53, 5, 105, 6, 21, 6, 42, 6, 84, 6, 17, 7, 34,
  41. 7, 68, 7, 2, 8, 3, 8, 6, 8, 108, 9, -41, 9, 43, 10, 86, 9, -84, 10, 35, 10,
  42. 69, 10, -118, 11, 28, 11, 55, 12, 11, 13, 22, 13, 44, 13, 88, 13, -80, 13,
  43. 36, 13, 71, 13, -115, 14, 29, 14, 57, 14, 113, 15, -30, 15, 46, 15, 91, 15,
  44. 19, 16, 37, 16, 73, 16, 2, 17, 3, 17, 6, 17
  45. };
  46. /* min(2^32-1, 10^e-1) for e in range 0 through 10 */
  47. static uint32_t ndigits_dec_threshold[] = {
  48. 0, 9U, 99U, 999U, 9999U, 99999U, 999999U,
  49. 9999999U, 99999999U, 999999999U, 0xffffffffU
  50. };
  51. /* -- Helper functions ---------------------------------------------------- */
  52. /* Compute the number of digits in the decimal representation of x. */
  53. static MSize ndigits_dec(uint32_t x)
  54. {
  55. MSize t = ((lj_fls(x | 1) * 77) >> 8) + 1; /* 2^8/77 is roughly log2(10) */
  56. return t + (x > ndigits_dec_threshold[t]);
  57. }
  58. #define WINT_R(x, sh, sc) \
  59. { uint32_t d = (x*(((1<<sh)+sc-1)/sc))>>sh; x -= d*sc; *p++ = (char)('0'+d); }
  60. /* Write 9-digit unsigned integer to buffer. */
  61. static char *lj_strfmt_wuint9(char *p, uint32_t u)
  62. {
  63. uint32_t v = u / 10000, w;
  64. u -= v * 10000;
  65. w = v / 10000;
  66. v -= w * 10000;
  67. *p++ = (char)('0'+w);
  68. WINT_R(v, 23, 1000)
  69. WINT_R(v, 12, 100)
  70. WINT_R(v, 10, 10)
  71. *p++ = (char)('0'+v);
  72. WINT_R(u, 23, 1000)
  73. WINT_R(u, 12, 100)
  74. WINT_R(u, 10, 10)
  75. *p++ = (char)('0'+u);
  76. return p;
  77. }
  78. #undef WINT_R
  79. /* -- Extended precision arithmetic --------------------------------------- */
  80. /*
  81. ** The "nd" format is a fixed-precision decimal representation for numbers. It
  82. ** consists of up to 64 uint32_t values, with each uint32_t storing a value
  83. ** in the range [0, 1e9). A number in "nd" format consists of three variables:
  84. **
  85. ** uint32_t nd[64];
  86. ** uint32_t ndlo;
  87. ** uint32_t ndhi;
  88. **
  89. ** The integral part of the number is stored in nd[0 ... ndhi], the value of
  90. ** which is sum{i in [0, ndhi] | nd[i] * 10^(9*i)}. If the fractional part of
  91. ** the number is zero, ndlo is zero. Otherwise, the fractional part is stored
  92. ** in nd[ndlo ... 63], the value of which is taken to be
  93. ** sum{i in [ndlo, 63] | nd[i] * 10^(9*(i-64))}.
  94. **
  95. ** If the array part had 128 elements rather than 64, then every double would
  96. ** have an exact representation in "nd" format. With 64 elements, all integral
  97. ** doubles have an exact representation, and all non-integral doubles have
  98. ** enough digits to make both %.99e and %.99f do the right thing.
  99. */
  100. #if LJ_64
  101. #define ND_MUL2K_MAX_SHIFT 29
  102. #define ND_MUL2K_DIV1E9(val) ((uint32_t)((val) / 1000000000))
  103. #else
  104. #define ND_MUL2K_MAX_SHIFT 11
  105. #define ND_MUL2K_DIV1E9(val) ((uint32_t)((val) >> 9) / 1953125)
  106. #endif
  107. /* Multiply nd by 2^k and add carry_in (ndlo is assumed to be zero). */
  108. static uint32_t nd_mul2k(uint32_t* nd, uint32_t ndhi, uint32_t k,
  109. uint32_t carry_in, SFormat sf)
  110. {
  111. uint32_t i, ndlo = 0, start = 1;
  112. /* Performance hacks. */
  113. if (k > ND_MUL2K_MAX_SHIFT*2 && STRFMT_FP(sf) != STRFMT_FP(STRFMT_T_FP_F)) {
  114. start = ndhi - (STRFMT_PREC(sf) + 17) / 8;
  115. }
  116. /* Real logic. */
  117. while (k >= ND_MUL2K_MAX_SHIFT) {
  118. for (i = ndlo; i <= ndhi; i++) {
  119. uint64_t val = ((uint64_t)nd[i] << ND_MUL2K_MAX_SHIFT) | carry_in;
  120. carry_in = ND_MUL2K_DIV1E9(val);
  121. nd[i] = (uint32_t)val - carry_in * 1000000000;
  122. }
  123. if (carry_in) {
  124. nd[++ndhi] = carry_in; carry_in = 0;
  125. if (start++ == ndlo) ++ndlo;
  126. }
  127. k -= ND_MUL2K_MAX_SHIFT;
  128. }
  129. if (k) {
  130. for (i = ndlo; i <= ndhi; i++) {
  131. uint64_t val = ((uint64_t)nd[i] << k) | carry_in;
  132. carry_in = ND_MUL2K_DIV1E9(val);
  133. nd[i] = (uint32_t)val - carry_in * 1000000000;
  134. }
  135. if (carry_in) nd[++ndhi] = carry_in;
  136. }
  137. return ndhi;
  138. }
  139. /* Divide nd by 2^k (ndlo is assumed to be zero). */
  140. static uint32_t nd_div2k(uint32_t* nd, uint32_t ndhi, uint32_t k, SFormat sf)
  141. {
  142. uint32_t ndlo = 0, stop1 = ~0, stop2 = ~0;
  143. /* Performance hacks. */
  144. if (!ndhi) {
  145. if (!nd[0]) {
  146. return 0;
  147. } else {
  148. uint32_t s = lj_ffs(nd[0]);
  149. if (s >= k) { nd[0] >>= k; return 0; }
  150. nd[0] >>= s; k -= s;
  151. }
  152. }
  153. if (k > 18) {
  154. if (STRFMT_FP(sf) == STRFMT_FP(STRFMT_T_FP_F)) {
  155. stop1 = 63 - (int32_t)STRFMT_PREC(sf) / 9;
  156. } else {
  157. int32_t floorlog2 = ndhi * 29 + lj_fls(nd[ndhi]) - k;
  158. int32_t floorlog10 = (int32_t)(floorlog2 * 0.30102999566398114);
  159. stop1 = 62 + (floorlog10 - (int32_t)STRFMT_PREC(sf)) / 9;
  160. stop2 = 61 + ndhi - (int32_t)STRFMT_PREC(sf) / 8;
  161. }
  162. }
  163. /* Real logic. */
  164. while (k >= 9) {
  165. uint32_t i = ndhi, carry = 0;
  166. for (;;) {
  167. uint32_t val = nd[i];
  168. nd[i] = (val >> 9) + carry;
  169. carry = (val & 0x1ff) * 1953125;
  170. if (i == ndlo) break;
  171. i = (i - 1) & 0x3f;
  172. }
  173. if (ndlo != stop1 && ndlo != stop2) {
  174. if (carry) { ndlo = (ndlo - 1) & 0x3f; nd[ndlo] = carry; }
  175. if (!nd[ndhi]) { ndhi = (ndhi - 1) & 0x3f; stop2--; }
  176. } else if (!nd[ndhi]) {
  177. if (ndhi != ndlo) { ndhi = (ndhi - 1) & 0x3f; stop2--; }
  178. else return ndlo;
  179. }
  180. k -= 9;
  181. }
  182. if (k) {
  183. uint32_t mask = (1U << k) - 1, mul = 1000000000 >> k, i = ndhi, carry = 0;
  184. for (;;) {
  185. uint32_t val = nd[i];
  186. nd[i] = (val >> k) + carry;
  187. carry = (val & mask) * mul;
  188. if (i == ndlo) break;
  189. i = (i - 1) & 0x3f;
  190. }
  191. if (carry) { ndlo = (ndlo - 1) & 0x3f; nd[ndlo] = carry; }
  192. }
  193. return ndlo;
  194. }
  195. /* Add m*10^e to nd (assumes ndlo <= e/9 <= ndhi and 0 <= m <= 9). */
  196. static uint32_t nd_add_m10e(uint32_t* nd, uint32_t ndhi, uint8_t m, int32_t e)
  197. {
  198. uint32_t i, carry;
  199. if (e >= 0) {
  200. i = (uint32_t)e/9;
  201. carry = m * (ndigits_dec_threshold[e - (int32_t)i*9] + 1);
  202. } else {
  203. int32_t f = (e-8)/9;
  204. i = (uint32_t)(64 + f);
  205. carry = m * (ndigits_dec_threshold[e - f*9] + 1);
  206. }
  207. for (;;) {
  208. uint32_t val = nd[i] + carry;
  209. if (LJ_UNLIKELY(val >= 1000000000)) {
  210. val -= 1000000000;
  211. nd[i] = val;
  212. if (LJ_UNLIKELY(i == ndhi)) {
  213. ndhi = (ndhi + 1) & 0x3f;
  214. nd[ndhi] = 1;
  215. break;
  216. }
  217. carry = 1;
  218. i = (i + 1) & 0x3f;
  219. } else {
  220. nd[i] = val;
  221. break;
  222. }
  223. }
  224. return ndhi;
  225. }
  226. /* Test whether two "nd" values are equal in their most significant digits. */
  227. static int nd_similar(uint32_t* nd, uint32_t ndhi, uint32_t* ref, MSize hilen,
  228. MSize prec)
  229. {
  230. char nd9[9], ref9[9];
  231. if (hilen <= prec) {
  232. if (LJ_UNLIKELY(nd[ndhi] != *ref)) return 0;
  233. prec -= hilen; ref--; ndhi = (ndhi - 1) & 0x3f;
  234. if (prec >= 9) {
  235. if (LJ_UNLIKELY(nd[ndhi] != *ref)) return 0;
  236. prec -= 9; ref--; ndhi = (ndhi - 1) & 0x3f;
  237. }
  238. } else {
  239. prec -= hilen - 9;
  240. }
  241. lj_assertX(prec < 9, "bad precision %d", prec);
  242. lj_strfmt_wuint9(nd9, nd[ndhi]);
  243. lj_strfmt_wuint9(ref9, *ref);
  244. return !memcmp(nd9, ref9, prec) && (nd9[prec] < '5') == (ref9[prec] < '5');
  245. }
  246. /* -- Formatted conversions to buffer ------------------------------------- */
  247. /* Write formatted floating-point number to either sb or p. */
  248. static char *lj_strfmt_wfnum(SBuf *sb, SFormat sf, lua_Number n, char *p)
  249. {
  250. MSize width = STRFMT_WIDTH(sf), prec = STRFMT_PREC(sf), len;
  251. TValue t;
  252. t.n = n;
  253. if (LJ_UNLIKELY((t.u32.hi << 1) >= 0xffe00000)) {
  254. /* Handle non-finite values uniformly for %a, %e, %f, %g. */
  255. int prefix = 0, ch = (sf & STRFMT_F_UPPER) ? 0x202020 : 0;
  256. if (((t.u32.hi & 0x000fffff) | t.u32.lo) != 0) {
  257. ch ^= ('n' << 16) | ('a' << 8) | 'n';
  258. if ((sf & STRFMT_F_SPACE)) prefix = ' ';
  259. } else {
  260. ch ^= ('i' << 16) | ('n' << 8) | 'f';
  261. if ((t.u32.hi & 0x80000000)) prefix = '-';
  262. else if ((sf & STRFMT_F_PLUS)) prefix = '+';
  263. else if ((sf & STRFMT_F_SPACE)) prefix = ' ';
  264. }
  265. len = 3 + (prefix != 0);
  266. if (!p) p = lj_buf_more(sb, width > len ? width : len);
  267. if (!(sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' ';
  268. if (prefix) *p++ = prefix;
  269. *p++ = (char)(ch >> 16); *p++ = (char)(ch >> 8); *p++ = (char)ch;
  270. } else if (STRFMT_FP(sf) == STRFMT_FP(STRFMT_T_FP_A)) {
  271. /* %a */
  272. const char *hexdig = (sf & STRFMT_F_UPPER) ? "0123456789ABCDEFPX"
  273. : "0123456789abcdefpx";
  274. int32_t e = (t.u32.hi >> 20) & 0x7ff;
  275. char prefix = 0, eprefix = '+';
  276. if (t.u32.hi & 0x80000000) prefix = '-';
  277. else if ((sf & STRFMT_F_PLUS)) prefix = '+';
  278. else if ((sf & STRFMT_F_SPACE)) prefix = ' ';
  279. t.u32.hi &= 0xfffff;
  280. if (e) {
  281. t.u32.hi |= 0x100000;
  282. e -= 1023;
  283. } else if (t.u32.lo | t.u32.hi) {
  284. /* Non-zero denormal - normalise it. */
  285. uint32_t shift = t.u32.hi ? 20-lj_fls(t.u32.hi) : 52-lj_fls(t.u32.lo);
  286. e = -1022 - shift;
  287. t.u64 <<= shift;
  288. }
  289. /* abs(n) == t.u64 * 2^(e - 52) */
  290. /* If n != 0, bit 52 of t.u64 is set, and is the highest set bit. */
  291. if ((int32_t)prec < 0) {
  292. /* Default precision: use smallest precision giving exact result. */
  293. prec = t.u32.lo ? 13-lj_ffs(t.u32.lo)/4 : 5-lj_ffs(t.u32.hi|0x100000)/4;
  294. } else if (prec < 13) {
  295. /* Precision is sufficiently low as to maybe require rounding. */
  296. t.u64 += (((uint64_t)1) << (51 - prec*4));
  297. }
  298. if (e < 0) {
  299. eprefix = '-';
  300. e = -e;
  301. }
  302. len = 5 + ndigits_dec((uint32_t)e) + prec + (prefix != 0)
  303. + ((prec | (sf & STRFMT_F_ALT)) != 0);
  304. if (!p) p = lj_buf_more(sb, width > len ? width : len);
  305. if (!(sf & (STRFMT_F_LEFT | STRFMT_F_ZERO))) {
  306. while (width-- > len) *p++ = ' ';
  307. }
  308. if (prefix) *p++ = prefix;
  309. *p++ = '0';
  310. *p++ = hexdig[17]; /* x or X */
  311. if ((sf & (STRFMT_F_LEFT | STRFMT_F_ZERO)) == STRFMT_F_ZERO) {
  312. while (width-- > len) *p++ = '0';
  313. }
  314. *p++ = '0' + (t.u32.hi >> 20); /* Usually '1', sometimes '0' or '2'. */
  315. if ((prec | (sf & STRFMT_F_ALT))) {
  316. /* Emit fractional part. */
  317. char *q = p + 1 + prec;
  318. *p = '.';
  319. if (prec < 13) t.u64 >>= (52 - prec*4);
  320. else while (prec > 13) p[prec--] = '0';
  321. while (prec) { p[prec--] = hexdig[t.u64 & 15]; t.u64 >>= 4; }
  322. p = q;
  323. }
  324. *p++ = hexdig[16]; /* p or P */
  325. *p++ = eprefix; /* + or - */
  326. p = lj_strfmt_wint(p, e);
  327. } else {
  328. /* %e or %f or %g - begin by converting n to "nd" format. */
  329. uint32_t nd[64];
  330. uint32_t ndhi = 0, ndlo, i;
  331. int32_t e = (t.u32.hi >> 20) & 0x7ff, ndebias = 0;
  332. char prefix = 0, *q;
  333. if (t.u32.hi & 0x80000000) prefix = '-';
  334. else if ((sf & STRFMT_F_PLUS)) prefix = '+';
  335. else if ((sf & STRFMT_F_SPACE)) prefix = ' ';
  336. prec += ((int32_t)prec >> 31) & 7; /* Default precision is 6. */
  337. if (STRFMT_FP(sf) == STRFMT_FP(STRFMT_T_FP_G)) {
  338. /* %g - decrement precision if non-zero (to make it like %e). */
  339. prec--;
  340. prec ^= (uint32_t)((int32_t)prec >> 31);
  341. }
  342. if ((sf & STRFMT_T_FP_E) && prec < 14 && n != 0) {
  343. /* Precision is sufficiently low that rescaling will probably work. */
  344. if ((ndebias = rescale_e[e >> 6])) {
  345. t.n = n * rescale_n[e >> 6];
  346. if (LJ_UNLIKELY(!e)) t.n *= 1e10, ndebias -= 10;
  347. t.u64 -= 2; /* Convert 2ulp below (later we convert 2ulp above). */
  348. nd[0] = 0x100000 | (t.u32.hi & 0xfffff);
  349. e = ((t.u32.hi >> 20) & 0x7ff) - 1075 - (ND_MUL2K_MAX_SHIFT < 29);
  350. goto load_t_lo; rescale_failed:
  351. t.n = n;
  352. e = (t.u32.hi >> 20) & 0x7ff;
  353. ndebias = ndhi = 0;
  354. }
  355. }
  356. nd[0] = t.u32.hi & 0xfffff;
  357. if (e == 0) e++; else nd[0] |= 0x100000;
  358. e -= 1043;
  359. if (t.u32.lo) {
  360. e -= 32 + (ND_MUL2K_MAX_SHIFT < 29); load_t_lo:
  361. #if ND_MUL2K_MAX_SHIFT >= 29
  362. nd[0] = (nd[0] << 3) | (t.u32.lo >> 29);
  363. ndhi = nd_mul2k(nd, ndhi, 29, t.u32.lo & 0x1fffffff, sf);
  364. #elif ND_MUL2K_MAX_SHIFT >= 11
  365. ndhi = nd_mul2k(nd, ndhi, 11, t.u32.lo >> 21, sf);
  366. ndhi = nd_mul2k(nd, ndhi, 11, (t.u32.lo >> 10) & 0x7ff, sf);
  367. ndhi = nd_mul2k(nd, ndhi, 11, (t.u32.lo << 1) & 0x7ff, sf);
  368. #else
  369. #error "ND_MUL2K_MAX_SHIFT too small"
  370. #endif
  371. }
  372. if (e >= 0) {
  373. ndhi = nd_mul2k(nd, ndhi, (uint32_t)e, 0, sf);
  374. ndlo = 0;
  375. } else {
  376. ndlo = nd_div2k(nd, ndhi, (uint32_t)-e, sf);
  377. if (ndhi && !nd[ndhi]) ndhi--;
  378. }
  379. /* abs(n) == nd * 10^ndebias (for slightly loose interpretation of ==) */
  380. if ((sf & STRFMT_T_FP_E)) {
  381. /* %e or %g - assume %e and start by calculating nd's exponent (nde). */
  382. char eprefix = '+';
  383. int32_t nde = -1;
  384. MSize hilen;
  385. if (ndlo && !nd[ndhi]) {
  386. ndhi = 64; do {} while (!nd[--ndhi]);
  387. nde -= 64 * 9;
  388. }
  389. hilen = ndigits_dec(nd[ndhi]);
  390. nde += ndhi * 9 + hilen;
  391. if (ndebias) {
  392. /*
  393. ** Rescaling was performed, but this introduced some error, and might
  394. ** have pushed us across a rounding boundary. We check whether this
  395. ** error affected the result by introducing even more error (2ulp in
  396. ** either direction), and seeing whether a rounding boundary was
  397. ** crossed. Having already converted the -2ulp case, we save off its
  398. ** most significant digits, convert the +2ulp case, and compare them.
  399. */
  400. int32_t eidx = e + 70 + (ND_MUL2K_MAX_SHIFT < 29)
  401. + (t.u32.lo >= 0xfffffffe && !(~t.u32.hi << 12));
  402. const int8_t *m_e = four_ulp_m_e + eidx * 2;
  403. lj_assertG_(G(sbufL(sb)), 0 <= eidx && eidx < 128, "bad eidx %d", eidx);
  404. nd[33] = nd[ndhi];
  405. nd[32] = nd[(ndhi - 1) & 0x3f];
  406. nd[31] = nd[(ndhi - 2) & 0x3f];
  407. nd_add_m10e(nd, ndhi, (uint8_t)*m_e, m_e[1]);
  408. if (LJ_UNLIKELY(!nd_similar(nd, ndhi, nd + 33, hilen, prec + 1))) {
  409. goto rescale_failed;
  410. }
  411. }
  412. if ((int32_t)(prec - nde) < (0x3f & -(int32_t)ndlo) * 9) {
  413. /* Precision is sufficiently low as to maybe require rounding. */
  414. ndhi = nd_add_m10e(nd, ndhi, 5, nde - prec - 1);
  415. nde += (hilen != ndigits_dec(nd[ndhi]));
  416. }
  417. nde += ndebias;
  418. if ((sf & STRFMT_T_FP_F)) {
  419. /* %g */
  420. if ((int32_t)prec >= nde && nde >= -4) {
  421. if (nde < 0) ndhi = 0;
  422. prec -= nde;
  423. goto g_format_like_f;
  424. } else if (!(sf & STRFMT_F_ALT) && prec && width > 5) {
  425. /* Decrease precision in order to strip trailing zeroes. */
  426. char tail[9];
  427. uint32_t maxprec = hilen - 1 + ((ndhi - ndlo) & 0x3f) * 9;
  428. if (prec >= maxprec) prec = maxprec;
  429. else ndlo = (ndhi - (((int32_t)(prec - hilen) + 9) / 9)) & 0x3f;
  430. i = prec - hilen - (((ndhi - ndlo) & 0x3f) * 9) + 10;
  431. lj_strfmt_wuint9(tail, nd[ndlo]);
  432. while (prec && tail[--i] == '0') {
  433. prec--;
  434. if (!i) {
  435. if (ndlo == ndhi) { prec = 0; break; }
  436. ndlo = (ndlo + 1) & 0x3f;
  437. lj_strfmt_wuint9(tail, nd[ndlo]);
  438. i = 9;
  439. }
  440. }
  441. }
  442. }
  443. if (nde < 0) {
  444. /* Make nde non-negative. */
  445. eprefix = '-';
  446. nde = -nde;
  447. }
  448. len = 3 + prec + (prefix != 0) + ndigits_dec((uint32_t)nde) + (nde < 10)
  449. + ((prec | (sf & STRFMT_F_ALT)) != 0);
  450. if (!p) p = lj_buf_more(sb, (width > len ? width : len) + 5);
  451. if (!(sf & (STRFMT_F_LEFT | STRFMT_F_ZERO))) {
  452. while (width-- > len) *p++ = ' ';
  453. }
  454. if (prefix) *p++ = prefix;
  455. if ((sf & (STRFMT_F_LEFT | STRFMT_F_ZERO)) == STRFMT_F_ZERO) {
  456. while (width-- > len) *p++ = '0';
  457. }
  458. q = lj_strfmt_wint(p + 1, nd[ndhi]);
  459. p[0] = p[1]; /* Put leading digit in the correct place. */
  460. if ((prec | (sf & STRFMT_F_ALT))) {
  461. /* Emit fractional part. */
  462. p[1] = '.'; p += 2;
  463. prec -= (MSize)(q - p); p = q; /* Account for digits already emitted. */
  464. /* Then emit chunks of 9 digits (this may emit 8 digits too many). */
  465. for (i = ndhi; (int32_t)prec > 0 && i != ndlo; prec -= 9) {
  466. i = (i - 1) & 0x3f;
  467. p = lj_strfmt_wuint9(p, nd[i]);
  468. }
  469. if ((sf & STRFMT_T_FP_F) && !(sf & STRFMT_F_ALT)) {
  470. /* %g (and not %#g) - strip trailing zeroes. */
  471. p += (int32_t)prec & ((int32_t)prec >> 31);
  472. while (p[-1] == '0') p--;
  473. if (p[-1] == '.') p--;
  474. } else {
  475. /* %e (or %#g) - emit trailing zeroes. */
  476. while ((int32_t)prec > 0) { *p++ = '0'; prec--; }
  477. p += (int32_t)prec;
  478. }
  479. } else {
  480. p++;
  481. }
  482. *p++ = (sf & STRFMT_F_UPPER) ? 'E' : 'e';
  483. *p++ = eprefix; /* + or - */
  484. if (nde < 10) *p++ = '0'; /* Always at least two digits of exponent. */
  485. p = lj_strfmt_wint(p, nde);
  486. } else {
  487. /* %f (or, shortly, %g in %f style) */
  488. if (prec < (MSize)(0x3f & -(int32_t)ndlo) * 9) {
  489. /* Precision is sufficiently low as to maybe require rounding. */
  490. ndhi = nd_add_m10e(nd, ndhi, 5, 0 - prec - 1);
  491. }
  492. g_format_like_f:
  493. if ((sf & STRFMT_T_FP_E) && !(sf & STRFMT_F_ALT) && prec && width) {
  494. /* Decrease precision in order to strip trailing zeroes. */
  495. if (ndlo) {
  496. /* nd has a fractional part; we need to look at its digits. */
  497. char tail[9];
  498. uint32_t maxprec = (64 - ndlo) * 9;
  499. if (prec >= maxprec) prec = maxprec;
  500. else ndlo = 64 - (prec + 8) / 9;
  501. i = prec - ((63 - ndlo) * 9);
  502. lj_strfmt_wuint9(tail, nd[ndlo]);
  503. while (prec && tail[--i] == '0') {
  504. prec--;
  505. if (!i) {
  506. if (ndlo == 63) { prec = 0; break; }
  507. lj_strfmt_wuint9(tail, nd[++ndlo]);
  508. i = 9;
  509. }
  510. }
  511. } else {
  512. /* nd has no fractional part, so precision goes straight to zero. */
  513. prec = 0;
  514. }
  515. }
  516. len = ndhi * 9 + ndigits_dec(nd[ndhi]) + prec + (prefix != 0)
  517. + ((prec | (sf & STRFMT_F_ALT)) != 0);
  518. if (!p) p = lj_buf_more(sb, (width > len ? width : len) + 8);
  519. if (!(sf & (STRFMT_F_LEFT | STRFMT_F_ZERO))) {
  520. while (width-- > len) *p++ = ' ';
  521. }
  522. if (prefix) *p++ = prefix;
  523. if ((sf & (STRFMT_F_LEFT | STRFMT_F_ZERO)) == STRFMT_F_ZERO) {
  524. while (width-- > len) *p++ = '0';
  525. }
  526. /* Emit integer part. */
  527. p = lj_strfmt_wint(p, nd[ndhi]);
  528. i = ndhi;
  529. while (i) p = lj_strfmt_wuint9(p, nd[--i]);
  530. if ((prec | (sf & STRFMT_F_ALT))) {
  531. /* Emit fractional part. */
  532. *p++ = '.';
  533. /* Emit chunks of 9 digits (this may emit 8 digits too many). */
  534. while ((int32_t)prec > 0 && i != ndlo) {
  535. i = (i - 1) & 0x3f;
  536. p = lj_strfmt_wuint9(p, nd[i]);
  537. prec -= 9;
  538. }
  539. if ((sf & STRFMT_T_FP_E) && !(sf & STRFMT_F_ALT)) {
  540. /* %g (and not %#g) - strip trailing zeroes. */
  541. p += (int32_t)prec & ((int32_t)prec >> 31);
  542. while (p[-1] == '0') p--;
  543. if (p[-1] == '.') p--;
  544. } else {
  545. /* %f (or %#g) - emit trailing zeroes. */
  546. while ((int32_t)prec > 0) { *p++ = '0'; prec--; }
  547. p += (int32_t)prec;
  548. }
  549. }
  550. }
  551. }
  552. if ((sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' ';
  553. return p;
  554. }
  555. /* Add formatted floating-point number to buffer. */
  556. SBuf *lj_strfmt_putfnum(SBuf *sb, SFormat sf, lua_Number n)
  557. {
  558. sb->w = lj_strfmt_wfnum(sb, sf, n, NULL);
  559. return sb;
  560. }
  561. /* -- Conversions to strings ---------------------------------------------- */
  562. /* Convert number to string. */
  563. GCstr * LJ_FASTCALL lj_strfmt_num(lua_State *L, cTValue *o)
  564. {
  565. char buf[STRFMT_MAXBUF_NUM];
  566. MSize len = (MSize)(lj_strfmt_wfnum(NULL, STRFMT_G14, o->n, buf) - buf);
  567. return lj_str_new(L, buf, len);
  568. }