_fpmaxtostr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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 size_t (__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. * ssize_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. ssize_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  173. __fp_outfunc_t fp_outfunc) attribute_hidden;
  174. ssize_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  175. __fp_outfunc_t fp_outfunc)
  176. {
  177. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  178. __fpmax_t lower_bnd;
  179. __fpmax_t upper_bnd = 1e9;
  180. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  181. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  182. uint_fast32_t base = 10;
  183. const __fpmax_t *power_table;
  184. int dpb = DIGITS_PER_BLOCK;
  185. int ndb = NUM_DIGIT_BLOCKS;
  186. int nd = DECIMAL_DIG;
  187. int sufficient_precision = 0;
  188. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  189. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  190. int num_groups = 0;
  191. int initial_group; /* This does not need to be initialized. */
  192. int tslen; /* This does not need to be initialized. */
  193. int nblk2; /* This does not need to be initialized. */
  194. const char *ts; /* This does not need to be initialized. */
  195. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  196. int round, o_exp;
  197. int exp;
  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. #ifdef __UCLIBC_HAVE_SIGNED_ZERO__
  245. if (zeroisnegative(x)) { /* Handle 'signed' zero. */
  246. *sign_str = '-';
  247. }
  248. #endif /* __UCLIBC_HAVE_SIGNED_ZERO__ */
  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. {
  269. int i, j;
  270. #ifdef __UCLIBC_MJN3_ONLY__
  271. #warning TODO: Clean up defines when hexadecimal float notation is unsupported.
  272. #endif /* __UCLIBC_MJN3_ONLY__ */
  273. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  274. if ((mode|0x20) == 'a') {
  275. lower_bnd = 0x1.0p31L;
  276. upper_bnd = 0x1.0p32L;
  277. power_table = exp16_table;
  278. exp = HEX_DIGITS_PER_BLOCK - 1;
  279. i = EXP16_TABLE_SIZE;
  280. j = EXP16_TABLE_MAX;
  281. dpb = HEX_DIGITS_PER_BLOCK;
  282. ndb = NUM_HEX_DIGIT_BLOCKS;
  283. nd = NUM_HEX_DIGITS;
  284. base = 16;
  285. } else {
  286. lower_bnd = 1e8;
  287. /* upper_bnd = 1e9; */
  288. power_table = exp10_table;
  289. exp = DIGITS_PER_BLOCK - 1;
  290. i = EXP10_TABLE_SIZE;
  291. j = EXP10_TABLE_MAX;
  292. /* dpb = DIGITS_PER_BLOCK; */
  293. /* ndb = NUM_DIGIT_BLOCKS; */
  294. /* base = 10; */
  295. }
  296. #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  297. #define lower_bnd 1e8
  298. #define upper_bnd 1e9
  299. #define power_table exp10_table
  300. #define dpb DIGITS_PER_BLOCK
  301. #define base 10
  302. #define ndb NUM_DIGIT_BLOCKS
  303. #define nd DECIMAL_DIG
  304. exp = DIGITS_PER_BLOCK - 1;
  305. i = EXP10_TABLE_SIZE;
  306. j = EXP10_TABLE_MAX;
  307. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  308. {
  309. int exp_neg = 0;
  310. if (x < lower_bnd) { /* Do we need to scale up or down? */
  311. exp_neg = 1;
  312. }
  313. do {
  314. --i;
  315. if (exp_neg) {
  316. if (x * power_table[i] < upper_bnd) {
  317. x *= power_table[i];
  318. exp -= j;
  319. }
  320. } else {
  321. if (x / power_table[i] >= lower_bnd) {
  322. x /= power_table[i];
  323. exp += j;
  324. }
  325. }
  326. j >>= 1;
  327. } while (i);
  328. }
  329. }
  330. if (x >= upper_bnd) { /* Handle bad rounding case. */
  331. x /= power_table[0];
  332. ++exp;
  333. }
  334. assert(x < upper_bnd);
  335. GENERATE_DIGITS:
  336. {
  337. int i, j;
  338. s = buf + 2; /* Leave space for '\0' and '0'. */
  339. i = 0;
  340. do {
  341. uint_fast32_t digit_block = (uint_fast32_t) x;
  342. assert(digit_block < upper_bnd);
  343. #ifdef __UCLIBC_MJN3_ONLY__
  344. #warning CONSIDER: Can rounding be a problem?
  345. #endif /* __UCLIBC_MJN3_ONLY__ */
  346. x = (x - digit_block) * upper_bnd;
  347. s += dpb;
  348. j = 0;
  349. do {
  350. s[- ++j] = '0' + (digit_block % base);
  351. digit_block /= base;
  352. } while (j < dpb);
  353. } while (++i < ndb);
  354. }
  355. /*************************************************************************/
  356. if (mode < 'a') {
  357. *exp_buf -= ('a' - 'A'); /* e->E and p->P */
  358. mode += ('a' - 'A');
  359. }
  360. o_mode = mode;
  361. if ((mode == 'g') && (preci > 0)){
  362. --preci;
  363. }
  364. round = preci;
  365. if (mode == 'f') {
  366. round += exp;
  367. if (round < -1) {
  368. memset(buf, '0', DECIMAL_DIG); /* OK, since 'f' -> decimal case. */
  369. exp = -1;
  370. round = -1;
  371. }
  372. }
  373. s = buf;
  374. *s++ = 0; /* Terminator for rounding and 0-triming. */
  375. *s = '0'; /* Space to round. */
  376. {
  377. int i;
  378. i = 0;
  379. e = s + nd + 1;
  380. if (round < nd) {
  381. e = s + round + 2;
  382. if (*e >= '0' + (base/2)) { /* NOTE: We always round away from 0! */
  383. i = 1;
  384. }
  385. }
  386. do { /* Handle rounding and trim trailing 0s. */
  387. *--e += i; /* Add the carry. */
  388. } while ((*e == '0') || (*e > '0' - 1 + base));
  389. }
  390. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  391. if ((mode|0x20) == 'a') {
  392. char *q;
  393. for (q = e ; *q ; --q) {
  394. if (*q > '9') {
  395. *q += (*exp_buf - ('p' - 'a') - '9' - 1);
  396. }
  397. }
  398. if (e > s) {
  399. exp *= 4; /* Change from base 16 to base 2. */
  400. }
  401. }
  402. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  403. o_exp = exp;
  404. if (e <= s) { /* We carried into an extra digit. */
  405. ++o_exp;
  406. e = s; /* Needed if all 0s. */
  407. } else {
  408. ++s;
  409. }
  410. *++e = 0; /* Terminating nul char. */
  411. if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
  412. mode = 'f';
  413. preci = round - o_exp;
  414. }
  415. exp = o_exp;
  416. if (mode != 'f') {
  417. o_exp = 0;
  418. }
  419. if (o_exp < 0) { /* Exponent is < 0, so */
  420. *--s = '0'; /* fake the first 0 digit. */
  421. }
  422. pc_fwi[3] = FPO_ZERO_PAD;
  423. pc_fwi[4] = 1;
  424. pc_fwi[5] = (intptr_t)(sign_str + 4);
  425. sign_str[4] = *s++;
  426. sign_str[5] = 0;
  427. ppc = pc_fwi + 6;
  428. {
  429. int i = e - s; /* Total digits is 'i'. */
  430. if (o_exp >= 0) {
  431. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  432. const char *p;
  433. if (PRINT_INFO_FLAG_VAL(info,group)
  434. && *(p = __UCLIBC_CURLOCALE->grouping)
  435. ) {
  436. int nblk1;
  437. nblk2 = nblk1 = *p;
  438. if (*++p) {
  439. nblk2 = *p;
  440. assert(!*++p);
  441. }
  442. if (o_exp >= nblk1) {
  443. num_groups = (o_exp - nblk1) / nblk2 + 1;
  444. initial_group = (o_exp - nblk1) % nblk2;
  445. #ifdef __UCLIBC_HAS_WCHAR__
  446. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  447. /* _fp_out_wide() will fix this up. */
  448. ts = fmt + THOUSEP_OFFSET;
  449. tslen = 1;
  450. } else {
  451. #endif /* __UCLIBC_HAS_WCHAR__ */
  452. ts = __UCLIBC_CURLOCALE->thousands_sep;
  453. tslen = __UCLIBC_CURLOCALE->thousands_sep_len;
  454. #ifdef __UCLIBC_HAS_WCHAR__
  455. }
  456. #endif /* __UCLIBC_HAS_WCHAR__ */
  457. width -= num_groups * tslen;
  458. }
  459. }
  460. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  461. ppc[0] = FPO_STR_PREC;
  462. ppc[2] = (intptr_t)(s);
  463. if (o_exp >= i) { /* all digit(s) left of decimal */
  464. ppc[1] = i;
  465. ppc += 3;
  466. o_exp -= i;
  467. i = 0;
  468. if (o_exp>0) { /* have 0s left of decimal */
  469. ppc[0] = FPO_ZERO_PAD;
  470. ppc[1] = o_exp;
  471. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  472. ppc += 3;
  473. }
  474. } else if (o_exp > 0) { /* decimal between digits */
  475. ppc[1] = o_exp;
  476. ppc += 3;
  477. s += o_exp;
  478. i -= o_exp;
  479. }
  480. o_exp = -1;
  481. }
  482. if (PRINT_INFO_FLAG_VAL(info,alt)
  483. || (i)
  484. || ((o_mode != 'g')
  485. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  486. && (o_mode != 'a')
  487. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  488. && (preci > 0))
  489. ) {
  490. ppc[0] = FPO_STR_PREC;
  491. #ifdef __LOCALE_C_ONLY
  492. ppc[1] = 1;
  493. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  494. #else /* __LOCALE_C_ONLY */
  495. #ifdef __UCLIBC_HAS_WCHAR__
  496. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  497. /* _fp_out_wide() will fix this up. */
  498. ppc[1] = 1;
  499. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  500. } else {
  501. #endif /* __UCLIBC_HAS_WCHAR__ */
  502. ppc[1] = __UCLIBC_CURLOCALE->decimal_point_len;
  503. ppc[2] = (intptr_t)(__UCLIBC_CURLOCALE->decimal_point);
  504. #ifdef __UCLIBC_HAS_WCHAR__
  505. }
  506. #endif /* __UCLIBC_HAS_WCHAR__ */
  507. #endif /* __LOCALE_C_ONLY */
  508. ppc += 3;
  509. }
  510. if (++o_exp < 0) { /* Have 0s right of decimal. */
  511. ppc[0] = FPO_ZERO_PAD;
  512. ppc[1] = -o_exp;
  513. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  514. ppc += 3;
  515. }
  516. if (i) { /* Have digit(s) right of decimal. */
  517. ppc[0] = FPO_STR_PREC;
  518. ppc[1] = i;
  519. ppc[2] = (intptr_t)(s);
  520. ppc += 3;
  521. }
  522. if (((o_mode != 'g') || PRINT_INFO_FLAG_VAL(info,alt))
  523. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  524. && !sufficient_precision
  525. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  526. ) {
  527. i -= o_exp;
  528. if (i < preci) { /* Have 0s right of digits. */
  529. i = preci - i;
  530. ppc[0] = FPO_ZERO_PAD;
  531. ppc[1] = i;
  532. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  533. ppc += 3;
  534. }
  535. }
  536. }
  537. /* Build exponent string. */
  538. if (mode != 'f') {
  539. char *p = exp_buf + sizeof(exp_buf);
  540. int j;
  541. char exp_char = *exp_buf;
  542. char exp_sign = '+';
  543. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  544. int min_exp_dig_plus_2 = ((o_mode != 'a') ? (2+2) : (2+1));
  545. #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  546. #define min_exp_dig_plus_2 (2+2)
  547. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  548. if (exp < 0) {
  549. exp_sign = '-';
  550. exp = -exp;
  551. }
  552. *--p = 0; /* nul-terminate */
  553. j = 2; /* Count exp_char and exp_sign. */
  554. do {
  555. *--p = '0' + (exp % 10);
  556. exp /= 10;
  557. } while ((++j < min_exp_dig_plus_2) || exp); /* char+sign+mindigits */
  558. *--p = exp_sign;
  559. *--p = exp_char;
  560. ppc[0] = FPO_STR_PREC;
  561. ppc[1] = j;
  562. ppc[2] = (intptr_t)(p);
  563. ppc += 3;
  564. }
  565. EXIT_SPECIAL:
  566. {
  567. int i;
  568. ppc_last = ppc;
  569. ppc = pc_fwi + 4; /* Need width fields starting with second. */
  570. do {
  571. width -= *ppc;
  572. ppc += 3;
  573. } while (ppc < ppc_last);
  574. ppc = pc_fwi;
  575. ppc[0] = FPO_STR_WIDTH;
  576. ppc[1] = i = ((*sign_str) != 0);
  577. ppc[2] = (intptr_t) sign_str;
  578. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  579. if (((mode|0x20) == 'a') && (pc_fwi[3] >= 16)) { /* Hex sign handling. */
  580. /* Hex and not inf or nan, so prefix with 0x. */
  581. char *h = sign_str + i;
  582. *h = '0';
  583. *++h = 'x' - 'p' + *exp_buf;
  584. *++h = 0;
  585. ppc[1] = (i += 2);
  586. }
  587. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  588. if ((width -= i) > 0) {
  589. if (PRINT_INFO_FLAG_VAL(info,left)) { /* Left-justified. */
  590. ppc_last[0] = FPO_STR_WIDTH;
  591. ppc_last[1] = width;
  592. ppc_last[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  593. ppc_last += 3;
  594. } else if (info->pad == '0') { /* 0 padding */
  595. ppc[4] += width; /* Pad second field. */
  596. } else {
  597. ppc[1] += width; /* Pad first (sign) field. */
  598. }
  599. }
  600. cnt = 0;
  601. }
  602. do {
  603. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  604. if ((ppc == pc_fwi + 6) && num_groups) {
  605. const char *gp = (const char *) ppc[2];
  606. int len = ppc[1];
  607. int blk = initial_group;
  608. cnt += num_groups * tslen; /* Adjust count now for sep chars. */
  609. /* __printf("\n"); */
  610. do {
  611. if (!blk) { /* Initial group could be 0 digits long! */
  612. blk = nblk2;
  613. } else if (len >= blk) { /* Enough digits for a group. */
  614. /* __printf("norm: len=%d blk=%d \"%.*s\"\n", len, blk, blk, gp); */
  615. if (fp_outfunc(fp, *ppc, blk, (intptr_t) gp) != blk) {
  616. return -1;
  617. }
  618. assert(gp);
  619. if (*gp) {
  620. gp += blk;
  621. }
  622. len -= blk;
  623. } else { /* Transition to 0s. */
  624. /* __printf("trans: len=%d blk=%d \"%.*s\"\n", len, blk, len, gp); */
  625. if (len) {
  626. /* __printf("len\n"); */
  627. if (fp_outfunc(fp, *ppc, len, (intptr_t) gp) != len) {
  628. return -1;
  629. }
  630. gp += len;
  631. }
  632. if (ppc[3] == FPO_ZERO_PAD) { /* Need to group 0s */
  633. /* __printf("zeropad\n"); */
  634. cnt += ppc[1];
  635. ppc += 3;
  636. gp = (const char *) ppc[2];
  637. blk -= len; /* blk > len, so blk still > 0. */
  638. len = ppc[1];
  639. continue; /* Don't decrement num_groups here. */
  640. } else {
  641. assert(num_groups == 0);
  642. break;
  643. }
  644. }
  645. if (num_groups <= 0) {
  646. break;
  647. }
  648. --num_groups;
  649. if (fp_outfunc(fp, FPO_STR_PREC, tslen, (intptr_t) ts) != tslen) {
  650. return -1;
  651. }
  652. blk = nblk2;
  653. /* __printf("num_groups=%d blk=%d\n", num_groups, blk); */
  654. } while (1);
  655. } else
  656. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  657. { /* NOTE: Remember 'else' above! */
  658. if (fp_outfunc(fp, *ppc, ppc[1], ppc[2]) != ppc[1]) {
  659. return -1;
  660. }
  661. }
  662. cnt += ppc[1];
  663. ppc += 3;
  664. } while (ppc < ppc_last);
  665. return cnt;
  666. }