_fpmaxtostr.c 18 KB

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