_fpmaxtostr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. #include "_stdio.h"
  8. #include <printf.h>
  9. #include <float.h>
  10. #include <locale.h>
  11. #include <bits/uClibc_fpmax.h>
  12. libc_hidden_proto(memset)
  13. #ifdef __UCLIBC_HAS_LOCALE__
  14. libc_hidden_proto(__global_locale)
  15. #endif
  16. typedef size_t (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,
  17. intptr_t buf);
  18. /* Copyright (C) 2000, 2001, 2003 Manuel Novoa III
  19. *
  20. * Function:
  21. *
  22. * ssize_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  23. * __fp_outfunc_t fp_outfunc);
  24. *
  25. * This is derived from the old _dtostr, whic I wrote for uClibc to provide
  26. * floating point support for the printf functions. It handles +/- infinity,
  27. * nan, and signed 0 assuming you have ieee arithmetic. It also now handles
  28. * digit grouping (for the uClibc supported locales) and hexadecimal float
  29. * notation. Finally, via the fp_outfunc parameter, it now supports wide
  30. * output.
  31. *
  32. * Notes:
  33. *
  34. * At most DECIMAL_DIG significant digits are kept. Any trailing digits
  35. * are treated as 0 as they are really just the results of rounding noise
  36. * anyway. If you want to do better, use an arbitary precision arithmetic
  37. * package. ;-)
  38. *
  39. * It should also be fairly portable, as no assumptions are made about the
  40. * bit-layout of doubles. Of course, that does make it less efficient than
  41. * it could be.
  42. *
  43. */
  44. /*****************************************************************************/
  45. /* Don't change anything that follows unless you know what you're doing. */
  46. /*****************************************************************************/
  47. /* Fairly portable nan check. Bitwise for i386 generated larger code.
  48. * If you have a better version, comment this out.
  49. */
  50. #define isnan(x) ((x) != (x))
  51. /* Without seminumerical functions to examine the sign bit, this is
  52. * about the best we can do to test for '-0'.
  53. */
  54. #define zeroisnegative(x) ((1./(x)) < 0)
  55. /*****************************************************************************/
  56. /* Don't change anything that follows peroid!!! ;-) */
  57. /*****************************************************************************/
  58. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  59. #if FLT_RADIX != 2
  60. #error FLT_RADIX != 2 is not currently supported
  61. #endif
  62. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  63. #define NUM_HEX_DIGITS ((FPMAX_MANT_DIG + 3)/ 4)
  64. /* WARNING: Adjust _fp_out_wide() below if this changes! */
  65. /* With 32 bit ints, we can get 9 decimal digits per block. */
  66. #define DIGITS_PER_BLOCK 9
  67. #define HEX_DIGITS_PER_BLOCK 8
  68. /* Maximum number of subcases to output double is...
  69. * 0 - sign
  70. * 1 - padding and initial digit
  71. * 2 - digits left of the radix
  72. * 3 - 0s left of the radix or radix
  73. * 4 - radix or digits right of the radix
  74. * 5 - 0s right of the radix
  75. * 6 - exponent
  76. * 7 - trailing space padding
  77. * although not all cases may occur.
  78. */
  79. #define MAX_CALLS 8
  80. /*****************************************************************************/
  81. #define NUM_DIGIT_BLOCKS ((DECIMAL_DIG+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  82. #define NUM_HEX_DIGIT_BLOCKS \
  83. ((NUM_HEX_DIGITS+HEX_DIGITS_PER_BLOCK-1)/HEX_DIGITS_PER_BLOCK)
  84. /* WARNING: Adjust _fp_out_wide() below if this changes! */
  85. /* extra space for '-', '.', 'e+###', and nul */
  86. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  87. /*****************************************************************************/
  88. static const char fmt[] = "inf\0INF\0nan\0NAN\0.\0,";
  89. #define INF_OFFSET 0 /* must be 1st */
  90. #define NAN_OFFSET 8 /* must be 2nd.. see hex sign handling */
  91. #define DECPT_OFFSET 16
  92. #define THOUSEP_OFFSET 18
  93. #define EMPTY_STRING_OFFSET 3
  94. /*****************************************************************************/
  95. #if FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
  96. #error scaling code can not handle FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
  97. #endif
  98. static const __fpmax_t exp10_table[] =
  99. {
  100. 1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L, /* floats */
  101. #if FPMAX_MAX_10_EXP < 32
  102. #error unsupported FPMAX_MAX_10_EXP (< 32). ANSI/ISO C requires >= 37.
  103. #endif
  104. #if FPMAX_MAX_10_EXP >= 64
  105. 1e64L,
  106. #endif
  107. #if FPMAX_MAX_10_EXP >= 128
  108. 1e128L,
  109. #endif
  110. #if FPMAX_MAX_10_EXP >= 256
  111. 1e256L,
  112. #endif
  113. #if FPMAX_MAX_10_EXP >= 512
  114. 1e512L,
  115. #endif
  116. #if FPMAX_MAX_10_EXP >= 1024
  117. 1e1024L,
  118. #endif
  119. #if FPMAX_MAX_10_EXP >= 2048
  120. 1e2048L,
  121. #endif
  122. #if FPMAX_MAX_10_EXP >= 4096
  123. 1e4096L
  124. #endif
  125. #if FPMAX_MAX_10_EXP >= 8192
  126. #error unsupported FPMAX_MAX_10_EXP. please increase table
  127. #endif
  128. };
  129. #define EXP10_TABLE_SIZE (sizeof(exp10_table)/sizeof(exp10_table[0]))
  130. #define EXP10_TABLE_MAX (1U<<(EXP10_TABLE_SIZE-1))
  131. /*****************************************************************************/
  132. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  133. #if FLT_RADIX != 2
  134. #error FLT_RADIX != 2 is not currently supported
  135. #endif
  136. #if FPMAX_MAX_EXP < -FPMAX_MIN_EXP
  137. #error scaling code can not handle FPMAX_MAX_EXP < -FPMAX_MIN_EXP
  138. #endif
  139. static const __fpmax_t exp16_table[] = {
  140. 0x1.0p4L, 0x1.0p8L, 0x1.0p16L, 0x1.0p32L, 0x1.0p64L,
  141. #if FPMAX_MAX_EXP >= 128
  142. 0x1.0p128L,
  143. #endif
  144. #if FPMAX_MAX_EXP >= 256
  145. 0x1.0p256L,
  146. #endif
  147. #if FPMAX_MAX_EXP >= 512
  148. 0x1.0p512L,
  149. #endif
  150. #if FPMAX_MAX_EXP >= 1024
  151. 0x1.0p1024L,
  152. #endif
  153. #if FPMAX_MAX_EXP >= 2048
  154. 0x1.0p2048L,
  155. #endif
  156. #if FPMAX_MAX_EXP >= 4096
  157. 0x1.0p4096L,
  158. #endif
  159. #if FPMAX_MAX_EXP >= 8192
  160. 0x1.0p8192L,
  161. #endif
  162. #if FPMAX_MAX_EXP >= 16384
  163. 0x1.0p16384L
  164. #endif
  165. #if FPMAX_MAX_EXP >= 32768
  166. #error unsupported FPMAX_MAX_EXP. please increase table
  167. #endif
  168. };
  169. #define EXP16_TABLE_SIZE (sizeof(exp16_table)/sizeof(exp16_table[0]))
  170. #define EXP16_TABLE_MAX (1U<<(EXP16_TABLE_SIZE-1))
  171. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  172. /*****************************************************************************/
  173. #define FPO_ZERO_PAD (0x80 | '0')
  174. #define FPO_STR_WIDTH (0x80 | ' ');
  175. #define FPO_STR_PREC 'p'
  176. ssize_t attribute_hidden _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  177. __fp_outfunc_t fp_outfunc)
  178. {
  179. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  180. __fpmax_t lower_bnd;
  181. __fpmax_t upper_bnd = 1e9;
  182. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  183. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  184. uint_fast32_t base = 10;
  185. const __fpmax_t *power_table;
  186. int dpb = DIGITS_PER_BLOCK;
  187. int ndb = NUM_DIGIT_BLOCKS;
  188. int nd = DECIMAL_DIG;
  189. int sufficient_precision = 0;
  190. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  191. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  192. int num_groups = 0;
  193. int initial_group; /* This does not need to be initialized. */
  194. int tslen; /* This does not need to be initialized. */
  195. int nblk2; /* This does not need to be initialized. */
  196. const char *ts; /* This does not need to be initialized. */
  197. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  198. int round, o_exp;
  199. int exp;
  200. int width, preci;
  201. int cnt;
  202. char *s;
  203. char *e;
  204. intptr_t pc_fwi[3*MAX_CALLS];
  205. intptr_t *ppc;
  206. intptr_t *ppc_last;
  207. #ifdef __UCLIBC_MJN3_ONLY__
  208. #warning TODO: The size of exp_buf[] should really be determined by the float constants.
  209. #endif /* __UCLIBC_MJN3_ONLY__ */
  210. char exp_buf[16];
  211. char buf[BUF_SIZE];
  212. char sign_str[6]; /* Last 2 are for 1st digit + nul. */
  213. char o_mode;
  214. char mode;
  215. width = info->width;
  216. preci = info->prec;
  217. mode = info->spec;
  218. *exp_buf = 'e';
  219. if ((mode|0x20) == 'a') {
  220. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  221. *exp_buf = 'p';
  222. if (preci < 0) {
  223. preci = NUM_HEX_DIGITS;
  224. sufficient_precision = 1;
  225. }
  226. #else
  227. mode += ('g' - 'a');
  228. #endif
  229. }
  230. if (preci < 0) {
  231. preci = 6;
  232. }
  233. *sign_str = '\0';
  234. if (PRINT_INFO_FLAG_VAL(info,showsign)) {
  235. *sign_str = '+';
  236. } else if (PRINT_INFO_FLAG_VAL(info,space)) {
  237. *sign_str = ' ';
  238. }
  239. *(sign_str+1) = 0;
  240. pc_fwi[5] = INF_OFFSET;
  241. if (isnan(x)) { /* First, check for nan. */
  242. pc_fwi[5] = NAN_OFFSET;
  243. goto INF_NAN;
  244. }
  245. if (x == 0) { /* Handle 0 now to avoid false positive. */
  246. #if 1
  247. if (zeroisnegative(x)) { /* Handle 'signed' zero. */
  248. *sign_str = '-';
  249. }
  250. #endif
  251. exp = -1;
  252. goto GENERATE_DIGITS;
  253. }
  254. if (x < 0) { /* Convert negatives to positives. */
  255. *sign_str = '-';
  256. x = -x;
  257. }
  258. if (__FPMAX_ZERO_OR_INF_CHECK(x)) { /* Inf since zero handled above. */
  259. INF_NAN:
  260. info->pad = ' ';
  261. ppc = pc_fwi + 6;
  262. pc_fwi[3] = FPO_STR_PREC;
  263. pc_fwi[4] = 3;
  264. if (mode < 'a') {
  265. pc_fwi[5] += 4;
  266. }
  267. pc_fwi[5] = (intptr_t)(fmt + pc_fwi[5]);
  268. goto EXIT_SPECIAL;
  269. }
  270. {
  271. int i, j;
  272. #ifdef __UCLIBC_MJN3_ONLY__
  273. #warning TODO: Clean up defines when hexadecimal float notation is unsupported.
  274. #endif /* __UCLIBC_MJN3_ONLY__ */
  275. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  276. if ((mode|0x20) == 'a') {
  277. lower_bnd = 0x1.0p31L;
  278. upper_bnd = 0x1.0p32L;
  279. power_table = exp16_table;
  280. exp = HEX_DIGITS_PER_BLOCK - 1;
  281. i = EXP16_TABLE_SIZE;
  282. j = EXP16_TABLE_MAX;
  283. dpb = HEX_DIGITS_PER_BLOCK;
  284. ndb = NUM_HEX_DIGIT_BLOCKS;
  285. nd = NUM_HEX_DIGITS;
  286. base = 16;
  287. } else {
  288. lower_bnd = 1e8;
  289. /* upper_bnd = 1e9; */
  290. power_table = exp10_table;
  291. exp = DIGITS_PER_BLOCK - 1;
  292. i = EXP10_TABLE_SIZE;
  293. j = EXP10_TABLE_MAX;
  294. /* dpb = DIGITS_PER_BLOCK; */
  295. /* ndb = NUM_DIGIT_BLOCKS; */
  296. /* base = 10; */
  297. }
  298. #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  299. #define lower_bnd 1e8
  300. #define upper_bnd 1e9
  301. #define power_table exp10_table
  302. #define dpb DIGITS_PER_BLOCK
  303. #define base 10
  304. #define ndb NUM_DIGIT_BLOCKS
  305. #define nd DECIMAL_DIG
  306. exp = DIGITS_PER_BLOCK - 1;
  307. i = EXP10_TABLE_SIZE;
  308. j = EXP10_TABLE_MAX;
  309. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  310. {
  311. int exp_neg = 0;
  312. if (x < lower_bnd) { /* Do we need to scale up or down? */
  313. exp_neg = 1;
  314. }
  315. do {
  316. --i;
  317. if (exp_neg) {
  318. if (x * power_table[i] < upper_bnd) {
  319. x *= power_table[i];
  320. exp -= j;
  321. }
  322. } else {
  323. if (x / power_table[i] >= lower_bnd) {
  324. x /= power_table[i];
  325. exp += j;
  326. }
  327. }
  328. j >>= 1;
  329. } while (i);
  330. }
  331. }
  332. if (x >= upper_bnd) { /* Handle bad rounding case. */
  333. x /= power_table[0];
  334. ++exp;
  335. }
  336. assert(x < upper_bnd);
  337. GENERATE_DIGITS:
  338. {
  339. int i, j;
  340. s = buf + 2; /* Leave space for '\0' and '0'. */
  341. i = 0;
  342. do {
  343. uint_fast32_t digit_block = (uint_fast32_t) x;
  344. assert(digit_block < upper_bnd);
  345. #ifdef __UCLIBC_MJN3_ONLY__
  346. #warning CONSIDER: Can rounding be a problem?
  347. #endif /* __UCLIBC_MJN3_ONLY__ */
  348. x = (x - digit_block) * upper_bnd;
  349. s += dpb;
  350. j = 0;
  351. do {
  352. s[- ++j] = '0' + (digit_block % base);
  353. digit_block /= base;
  354. } while (j < dpb);
  355. } while (++i < ndb);
  356. }
  357. /*************************************************************************/
  358. if (mode < 'a') {
  359. *exp_buf -= ('a' - 'A'); /* e->E and p->P */
  360. mode += ('a' - 'A');
  361. }
  362. o_mode = mode;
  363. if ((mode == 'g') && (preci > 0)){
  364. --preci;
  365. }
  366. round = preci;
  367. if (mode == 'f') {
  368. round += exp;
  369. if (round < -1) {
  370. memset(buf, '0', DECIMAL_DIG); /* OK, since 'f' -> decimal case. */
  371. exp = -1;
  372. round = -1;
  373. }
  374. }
  375. s = buf;
  376. *s++ = 0; /* Terminator for rounding and 0-triming. */
  377. *s = '0'; /* Space to round. */
  378. {
  379. int i;
  380. i = 0;
  381. e = s + nd + 1;
  382. if (round < nd) {
  383. e = s + round + 2;
  384. if (*e >= '0' + (base/2)) { /* NOTE: We always round away from 0! */
  385. i = 1;
  386. }
  387. }
  388. do { /* Handle rounding and trim trailing 0s. */
  389. *--e += i; /* Add the carry. */
  390. } while ((*e == '0') || (*e > '0' - 1 + base));
  391. }
  392. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  393. if ((mode|0x20) == 'a') {
  394. char *q;
  395. for (q = e ; *q ; --q) {
  396. if (*q > '9') {
  397. *q += (*exp_buf - ('p' - 'a') - '9' - 1);
  398. }
  399. }
  400. if (e > s) {
  401. exp *= 4; /* Change from base 16 to base 2. */
  402. }
  403. }
  404. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  405. o_exp = exp;
  406. if (e <= s) { /* We carried into an extra digit. */
  407. ++o_exp;
  408. e = s; /* Needed if all 0s. */
  409. } else {
  410. ++s;
  411. }
  412. *++e = 0; /* Terminating nul char. */
  413. if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
  414. mode = 'f';
  415. preci = round - o_exp;
  416. }
  417. exp = o_exp;
  418. if (mode != 'f') {
  419. o_exp = 0;
  420. }
  421. if (o_exp < 0) { /* Exponent is < 0, so */
  422. *--s = '0'; /* fake the first 0 digit. */
  423. }
  424. pc_fwi[3] = FPO_ZERO_PAD;
  425. pc_fwi[4] = 1;
  426. pc_fwi[5] = (intptr_t)(sign_str + 4);
  427. sign_str[4] = *s++;
  428. sign_str[5] = 0;
  429. ppc = pc_fwi + 6;
  430. {
  431. int i = e - s; /* Total digits is 'i'. */
  432. if (o_exp >= 0) {
  433. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  434. const char *p;
  435. if (PRINT_INFO_FLAG_VAL(info,group)
  436. && *(p = __UCLIBC_CURLOCALE_DATA.grouping)
  437. ) {
  438. int nblk1;
  439. nblk2 = nblk1 = *p;
  440. if (*++p) {
  441. nblk2 = *p;
  442. assert(!*++p);
  443. }
  444. if (o_exp >= nblk1) {
  445. num_groups = (o_exp - nblk1) / nblk2 + 1;
  446. initial_group = (o_exp - nblk1) % nblk2;
  447. #ifdef __UCLIBC_HAS_WCHAR__
  448. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  449. /* _fp_out_wide() will fix this up. */
  450. ts = fmt + THOUSEP_OFFSET;
  451. tslen = 1;
  452. } else {
  453. #endif /* __UCLIBC_HAS_WCHAR__ */
  454. ts = __UCLIBC_CURLOCALE_DATA.thousands_sep;
  455. tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
  456. #ifdef __UCLIBC_HAS_WCHAR__
  457. }
  458. #endif /* __UCLIBC_HAS_WCHAR__ */
  459. width -= num_groups * tslen;
  460. }
  461. }
  462. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  463. ppc[0] = FPO_STR_PREC;
  464. ppc[2] = (intptr_t)(s);
  465. if (o_exp >= i) { /* all digit(s) left of decimal */
  466. ppc[1] = i;
  467. ppc += 3;
  468. o_exp -= i;
  469. i = 0;
  470. if (o_exp>0) { /* have 0s left of decimal */
  471. ppc[0] = FPO_ZERO_PAD;
  472. ppc[1] = o_exp;
  473. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  474. ppc += 3;
  475. }
  476. } else if (o_exp > 0) { /* decimal between digits */
  477. ppc[1] = o_exp;
  478. ppc += 3;
  479. s += o_exp;
  480. i -= o_exp;
  481. }
  482. o_exp = -1;
  483. }
  484. if (PRINT_INFO_FLAG_VAL(info,alt)
  485. || (i)
  486. || ((o_mode != 'g')
  487. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  488. && (o_mode != 'a')
  489. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  490. && (preci > 0))
  491. ) {
  492. ppc[0] = FPO_STR_PREC;
  493. #ifdef __LOCALE_C_ONLY
  494. ppc[1] = 1;
  495. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  496. #else /* __LOCALE_C_ONLY */
  497. #ifdef __UCLIBC_HAS_WCHAR__
  498. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  499. /* _fp_out_wide() will fix this up. */
  500. ppc[1] = 1;
  501. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  502. } else {
  503. #endif /* __UCLIBC_HAS_WCHAR__ */
  504. ppc[1] = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
  505. ppc[2] = (intptr_t)(__UCLIBC_CURLOCALE_DATA.decimal_point);
  506. #ifdef __UCLIBC_HAS_WCHAR__
  507. }
  508. #endif /* __UCLIBC_HAS_WCHAR__ */
  509. #endif /* __LOCALE_C_ONLY */
  510. ppc += 3;
  511. }
  512. if (++o_exp < 0) { /* Have 0s right of decimal. */
  513. ppc[0] = FPO_ZERO_PAD;
  514. ppc[1] = -o_exp;
  515. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  516. ppc += 3;
  517. }
  518. if (i) { /* Have digit(s) right of decimal. */
  519. ppc[0] = FPO_STR_PREC;
  520. ppc[1] = i;
  521. ppc[2] = (intptr_t)(s);
  522. ppc += 3;
  523. }
  524. if (((o_mode != 'g') || PRINT_INFO_FLAG_VAL(info,alt))
  525. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  526. && !sufficient_precision
  527. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  528. ) {
  529. i -= o_exp;
  530. if (i < preci) { /* Have 0s right of digits. */
  531. i = preci - i;
  532. ppc[0] = FPO_ZERO_PAD;
  533. ppc[1] = i;
  534. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  535. ppc += 3;
  536. }
  537. }
  538. }
  539. /* Build exponent string. */
  540. if (mode != 'f') {
  541. char *p = exp_buf + sizeof(exp_buf);
  542. int j;
  543. char exp_char = *exp_buf;
  544. char exp_sign = '+';
  545. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  546. int min_exp_dig_plus_2 = ((o_mode != 'a') ? (2+2) : (2+1));
  547. #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  548. #define min_exp_dig_plus_2 (2+2)
  549. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  550. if (exp < 0) {
  551. exp_sign = '-';
  552. exp = -exp;
  553. }
  554. *--p = 0; /* nul-terminate */
  555. j = 2; /* Count exp_char and exp_sign. */
  556. do {
  557. *--p = '0' + (exp % 10);
  558. exp /= 10;
  559. } while ((++j < min_exp_dig_plus_2) || exp); /* char+sign+mindigits */
  560. *--p = exp_sign;
  561. *--p = exp_char;
  562. ppc[0] = FPO_STR_PREC;
  563. ppc[1] = j;
  564. ppc[2] = (intptr_t)(p);
  565. ppc += 3;
  566. }
  567. EXIT_SPECIAL:
  568. {
  569. int i;
  570. ppc_last = ppc;
  571. ppc = pc_fwi + 4; /* Need width fields starting with second. */
  572. do {
  573. width -= *ppc;
  574. ppc += 3;
  575. } while (ppc < ppc_last);
  576. ppc = pc_fwi;
  577. ppc[0] = FPO_STR_WIDTH;
  578. ppc[1] = i = ((*sign_str) != 0);
  579. ppc[2] = (intptr_t) sign_str;
  580. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  581. if (((mode|0x20) == 'a') && (pc_fwi[3] >= 16)) { /* Hex sign handling. */
  582. /* Hex and not inf or nan, so prefix with 0x. */
  583. char *h = sign_str + i;
  584. *h = '0';
  585. *++h = 'x' - 'p' + *exp_buf;
  586. *++h = 0;
  587. ppc[1] = (i += 2);
  588. }
  589. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  590. if ((width -= i) > 0) {
  591. if (PRINT_INFO_FLAG_VAL(info,left)) { /* Left-justified. */
  592. ppc_last[0] = FPO_STR_WIDTH;
  593. ppc_last[1] = width;
  594. ppc_last[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  595. ppc_last += 3;
  596. } else if (info->pad == '0') { /* 0 padding */
  597. ppc[4] += width; /* Pad second field. */
  598. } else {
  599. ppc[1] += width; /* Pad first (sign) field. */
  600. }
  601. }
  602. cnt = 0;
  603. }
  604. do {
  605. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  606. if ((ppc == pc_fwi + 6) && num_groups) {
  607. const char *gp = (const char *) ppc[2];
  608. int len = ppc[1];
  609. int blk = initial_group;
  610. cnt += num_groups * tslen; /* Adjust count now for sep chars. */
  611. /* __printf("\n"); */
  612. do {
  613. if (!blk) { /* Initial group could be 0 digits long! */
  614. blk = nblk2;
  615. } else if (len >= blk) { /* Enough digits for a group. */
  616. /* __printf("norm: len=%d blk=%d \"%.*s\"\n", len, blk, blk, gp); */
  617. if (fp_outfunc(fp, *ppc, blk, (intptr_t) gp) != blk) {
  618. return -1;
  619. }
  620. assert(gp);
  621. if (*gp) {
  622. gp += blk;
  623. }
  624. len -= blk;
  625. } else { /* Transition to 0s. */
  626. /* __printf("trans: len=%d blk=%d \"%.*s\"\n", len, blk, len, gp); */
  627. if (len) {
  628. /* __printf("len\n"); */
  629. if (fp_outfunc(fp, *ppc, len, (intptr_t) gp) != len) {
  630. return -1;
  631. }
  632. gp += len;
  633. }
  634. if (ppc[3] == FPO_ZERO_PAD) { /* Need to group 0s */
  635. /* __printf("zeropad\n"); */
  636. cnt += ppc[1];
  637. ppc += 3;
  638. gp = (const char *) ppc[2];
  639. blk -= len; /* blk > len, so blk still > 0. */
  640. len = ppc[1];
  641. continue; /* Don't decrement num_groups here. */
  642. } else {
  643. assert(num_groups == 0);
  644. break;
  645. }
  646. }
  647. if (num_groups <= 0) {
  648. break;
  649. }
  650. --num_groups;
  651. if (fp_outfunc(fp, FPO_STR_PREC, tslen, (intptr_t) ts) != tslen) {
  652. return -1;
  653. }
  654. blk = nblk2;
  655. /* __printf("num_groups=%d blk=%d\n", num_groups, blk); */
  656. } while (1);
  657. } else
  658. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  659. { /* NOTE: Remember 'else' above! */
  660. if (fp_outfunc(fp, *ppc, ppc[1], ppc[2]) != ppc[1]) {
  661. return -1;
  662. }
  663. }
  664. cnt += ppc[1];
  665. ppc += 3;
  666. } while (ppc < ppc_last);
  667. return cnt;
  668. }