_fpmaxtostr.c 18 KB

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