_fpmaxtostr.c 18 KB

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