_fpmaxtostr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 attribute_hidden _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. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  180. uint_fast32_t base = 10;
  181. const __fpmax_t *power_table;
  182. int dpb = DIGITS_PER_BLOCK;
  183. int ndb = NUM_DIGIT_BLOCKS;
  184. int nd = DECIMAL_DIG;
  185. int sufficient_precision = 0;
  186. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  187. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  188. int num_groups = 0;
  189. int initial_group; /* This does not need to be initialized. */
  190. int tslen; /* This does not need to be initialized. */
  191. int nblk2; /* This does not need to be initialized. */
  192. const char *ts; /* This does not need to be initialized. */
  193. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  194. int round, o_exp;
  195. int exp;
  196. int width, preci;
  197. int cnt;
  198. char *s;
  199. char *e;
  200. intptr_t pc_fwi[3*MAX_CALLS];
  201. intptr_t *ppc;
  202. intptr_t *ppc_last;
  203. #ifdef __UCLIBC_MJN3_ONLY__
  204. #warning TODO: The size of exp_buf[] should really be determined by the float constants.
  205. #endif /* __UCLIBC_MJN3_ONLY__ */
  206. char exp_buf[16];
  207. char buf[BUF_SIZE];
  208. char sign_str[6]; /* Last 2 are for 1st digit + nul. */
  209. char o_mode;
  210. char mode;
  211. width = info->width;
  212. preci = info->prec;
  213. mode = info->spec;
  214. *exp_buf = 'e';
  215. if ((mode|0x20) == 'a') {
  216. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  217. *exp_buf = 'p';
  218. if (preci < 0) {
  219. preci = NUM_HEX_DIGITS;
  220. sufficient_precision = 1;
  221. }
  222. #else
  223. mode += ('g' - 'a');
  224. #endif
  225. }
  226. if (preci < 0) {
  227. preci = 6;
  228. }
  229. *sign_str = '\0';
  230. if (PRINT_INFO_FLAG_VAL(info,showsign)) {
  231. *sign_str = '+';
  232. } else if (PRINT_INFO_FLAG_VAL(info,space)) {
  233. *sign_str = ' ';
  234. }
  235. *(sign_str+1) = 0;
  236. pc_fwi[5] = INF_OFFSET;
  237. if (isnan(x)) { /* First, check for nan. */
  238. pc_fwi[5] = NAN_OFFSET;
  239. goto INF_NAN;
  240. }
  241. if (x == 0) { /* Handle 0 now to avoid false positive. */
  242. #if 1
  243. if (zeroisnegative(x)) { /* Handle 'signed' zero. */
  244. *sign_str = '-';
  245. }
  246. #endif
  247. exp = -1;
  248. goto GENERATE_DIGITS;
  249. }
  250. if (x < 0) { /* Convert negatives to positives. */
  251. *sign_str = '-';
  252. x = -x;
  253. }
  254. if (__FPMAX_ZERO_OR_INF_CHECK(x)) { /* Inf since zero handled above. */
  255. INF_NAN:
  256. info->pad = ' ';
  257. ppc = pc_fwi + 6;
  258. pc_fwi[3] = FPO_STR_PREC;
  259. pc_fwi[4] = 3;
  260. if (mode < 'a') {
  261. pc_fwi[5] += 4;
  262. }
  263. pc_fwi[5] = (intptr_t)(fmt + pc_fwi[5]);
  264. goto EXIT_SPECIAL;
  265. }
  266. {
  267. int i, j;
  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. {
  307. int exp_neg = 0;
  308. if (x < lower_bnd) { /* Do we need to scale up or down? */
  309. exp_neg = 1;
  310. }
  311. do {
  312. --i;
  313. if (exp_neg) {
  314. if (x * power_table[i] < upper_bnd) {
  315. x *= power_table[i];
  316. exp -= j;
  317. }
  318. } else {
  319. if (x / power_table[i] >= lower_bnd) {
  320. x /= power_table[i];
  321. exp += j;
  322. }
  323. }
  324. j >>= 1;
  325. } while (i);
  326. }
  327. }
  328. if (x >= upper_bnd) { /* Handle bad rounding case. */
  329. x /= power_table[0];
  330. ++exp;
  331. }
  332. assert(x < upper_bnd);
  333. GENERATE_DIGITS:
  334. {
  335. int i, j;
  336. s = buf + 2; /* Leave space for '\0' and '0'. */
  337. i = 0;
  338. do {
  339. uint_fast32_t digit_block = (uint_fast32_t) x;
  340. assert(digit_block < upper_bnd);
  341. #ifdef __UCLIBC_MJN3_ONLY__
  342. #warning CONSIDER: Can rounding be a problem?
  343. #endif /* __UCLIBC_MJN3_ONLY__ */
  344. x = (x - digit_block) * upper_bnd;
  345. s += dpb;
  346. j = 0;
  347. do {
  348. s[- ++j] = '0' + (digit_block % base);
  349. digit_block /= base;
  350. } while (j < dpb);
  351. } while (++i < ndb);
  352. }
  353. /*************************************************************************/
  354. if (mode < 'a') {
  355. *exp_buf -= ('a' - 'A'); /* e->E and p->P */
  356. mode += ('a' - 'A');
  357. }
  358. o_mode = mode;
  359. if ((mode == 'g') && (preci > 0)){
  360. --preci;
  361. }
  362. round = preci;
  363. if (mode == 'f') {
  364. round += exp;
  365. if (round < -1) {
  366. memset(buf, '0', DECIMAL_DIG); /* OK, since 'f' -> decimal case. */
  367. exp = -1;
  368. round = -1;
  369. }
  370. }
  371. s = buf;
  372. *s++ = 0; /* Terminator for rounding and 0-triming. */
  373. *s = '0'; /* Space to round. */
  374. {
  375. int i;
  376. i = 0;
  377. e = s + nd + 1;
  378. if (round < nd) {
  379. e = s + round + 2;
  380. if (*e >= '0' + (base/2)) { /* NOTE: We always round away from 0! */
  381. i = 1;
  382. }
  383. }
  384. do { /* Handle rounding and trim trailing 0s. */
  385. *--e += i; /* Add the carry. */
  386. } while ((*e == '0') || (*e > '0' - 1 + base));
  387. }
  388. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  389. if ((mode|0x20) == 'a') {
  390. char *q;
  391. for (q = e ; *q ; --q) {
  392. if (*q > '9') {
  393. *q += (*exp_buf - ('p' - 'a') - '9' - 1);
  394. }
  395. }
  396. if (e > s) {
  397. exp *= 4; /* Change from base 16 to base 2. */
  398. }
  399. }
  400. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  401. o_exp = exp;
  402. if (e <= s) { /* We carried into an extra digit. */
  403. ++o_exp;
  404. e = s; /* Needed if all 0s. */
  405. } else {
  406. ++s;
  407. }
  408. *++e = 0; /* Terminating nul char. */
  409. if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
  410. mode = 'f';
  411. preci = round - o_exp;
  412. }
  413. exp = o_exp;
  414. if (mode != 'f') {
  415. o_exp = 0;
  416. }
  417. if (o_exp < 0) { /* Exponent is < 0, so */
  418. *--s = '0'; /* fake the first 0 digit. */
  419. }
  420. pc_fwi[3] = FPO_ZERO_PAD;
  421. pc_fwi[4] = 1;
  422. pc_fwi[5] = (intptr_t)(sign_str + 4);
  423. sign_str[4] = *s++;
  424. sign_str[5] = 0;
  425. ppc = pc_fwi + 6;
  426. {
  427. int i = e - s; /* Total digits is 'i'. */
  428. if (o_exp >= 0) {
  429. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  430. const char *p;
  431. if (PRINT_INFO_FLAG_VAL(info,group)
  432. && *(p = __UCLIBC_CURLOCALE_DATA.grouping)
  433. ) {
  434. int nblk1;
  435. nblk2 = nblk1 = *p;
  436. if (*++p) {
  437. nblk2 = *p;
  438. assert(!*++p);
  439. }
  440. if (o_exp >= nblk1) {
  441. num_groups = (o_exp - nblk1) / nblk2 + 1;
  442. initial_group = (o_exp - nblk1) % nblk2;
  443. #ifdef __UCLIBC_HAS_WCHAR__
  444. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  445. /* _fp_out_wide() will fix this up. */
  446. ts = fmt + THOUSEP_OFFSET;
  447. tslen = 1;
  448. } else {
  449. #endif /* __UCLIBC_HAS_WCHAR__ */
  450. ts = __UCLIBC_CURLOCALE_DATA.thousands_sep;
  451. tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
  452. #ifdef __UCLIBC_HAS_WCHAR__
  453. }
  454. #endif /* __UCLIBC_HAS_WCHAR__ */
  455. width -= num_groups * tslen;
  456. }
  457. }
  458. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  459. ppc[0] = FPO_STR_PREC;
  460. ppc[2] = (intptr_t)(s);
  461. if (o_exp >= i) { /* all digit(s) left of decimal */
  462. ppc[1] = i;
  463. ppc += 3;
  464. o_exp -= i;
  465. i = 0;
  466. if (o_exp>0) { /* have 0s left of decimal */
  467. ppc[0] = FPO_ZERO_PAD;
  468. ppc[1] = o_exp;
  469. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  470. ppc += 3;
  471. }
  472. } else if (o_exp > 0) { /* decimal between digits */
  473. ppc[1] = o_exp;
  474. ppc += 3;
  475. s += o_exp;
  476. i -= o_exp;
  477. }
  478. o_exp = -1;
  479. }
  480. if (PRINT_INFO_FLAG_VAL(info,alt)
  481. || (i)
  482. || ((o_mode != 'g')
  483. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  484. && (o_mode != 'a')
  485. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  486. && (preci > 0))
  487. ) {
  488. ppc[0] = FPO_STR_PREC;
  489. #ifdef __LOCALE_C_ONLY
  490. ppc[1] = 1;
  491. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  492. #else /* __LOCALE_C_ONLY */
  493. #ifdef __UCLIBC_HAS_WCHAR__
  494. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  495. /* _fp_out_wide() will fix this up. */
  496. ppc[1] = 1;
  497. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  498. } else {
  499. #endif /* __UCLIBC_HAS_WCHAR__ */
  500. ppc[1] = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
  501. ppc[2] = (intptr_t)(__UCLIBC_CURLOCALE_DATA.decimal_point);
  502. #ifdef __UCLIBC_HAS_WCHAR__
  503. }
  504. #endif /* __UCLIBC_HAS_WCHAR__ */
  505. #endif /* __LOCALE_C_ONLY */
  506. ppc += 3;
  507. }
  508. if (++o_exp < 0) { /* Have 0s right of decimal. */
  509. ppc[0] = FPO_ZERO_PAD;
  510. ppc[1] = -o_exp;
  511. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  512. ppc += 3;
  513. }
  514. if (i) { /* Have digit(s) right of decimal. */
  515. ppc[0] = FPO_STR_PREC;
  516. ppc[1] = i;
  517. ppc[2] = (intptr_t)(s);
  518. ppc += 3;
  519. }
  520. if (((o_mode != 'g') || PRINT_INFO_FLAG_VAL(info,alt))
  521. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  522. && !sufficient_precision
  523. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  524. ) {
  525. i -= o_exp;
  526. if (i < preci) { /* Have 0s right of digits. */
  527. i = preci - i;
  528. ppc[0] = FPO_ZERO_PAD;
  529. ppc[1] = i;
  530. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  531. ppc += 3;
  532. }
  533. }
  534. }
  535. /* Build exponent string. */
  536. if (mode != 'f') {
  537. char *p = exp_buf + sizeof(exp_buf);
  538. int j;
  539. char exp_char = *exp_buf;
  540. char exp_sign = '+';
  541. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  542. int min_exp_dig_plus_2 = ((o_mode != 'a') ? (2+2) : (2+1));
  543. #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  544. #define min_exp_dig_plus_2 (2+2)
  545. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  546. if (exp < 0) {
  547. exp_sign = '-';
  548. exp = -exp;
  549. }
  550. *--p = 0; /* nul-terminate */
  551. j = 2; /* Count exp_char and exp_sign. */
  552. do {
  553. *--p = '0' + (exp % 10);
  554. exp /= 10;
  555. } while ((++j < min_exp_dig_plus_2) || exp); /* char+sign+mindigits */
  556. *--p = exp_sign;
  557. *--p = exp_char;
  558. ppc[0] = FPO_STR_PREC;
  559. ppc[1] = j;
  560. ppc[2] = (intptr_t)(p);
  561. ppc += 3;
  562. }
  563. EXIT_SPECIAL:
  564. {
  565. int i;
  566. ppc_last = ppc;
  567. ppc = pc_fwi + 4; /* Need width fields starting with second. */
  568. do {
  569. width -= *ppc;
  570. ppc += 3;
  571. } while (ppc < ppc_last);
  572. ppc = pc_fwi;
  573. ppc[0] = FPO_STR_WIDTH;
  574. ppc[1] = i = ((*sign_str) != 0);
  575. ppc[2] = (intptr_t) sign_str;
  576. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  577. if (((mode|0x20) == 'a') && (pc_fwi[3] >= 16)) { /* Hex sign handling. */
  578. /* Hex and not inf or nan, so prefix with 0x. */
  579. char *h = sign_str + i;
  580. *h = '0';
  581. *++h = 'x' - 'p' + *exp_buf;
  582. *++h = 0;
  583. ppc[1] = (i += 2);
  584. }
  585. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  586. if ((width -= i) > 0) {
  587. if (PRINT_INFO_FLAG_VAL(info,left)) { /* Left-justified. */
  588. ppc_last[0] = FPO_STR_WIDTH;
  589. ppc_last[1] = width;
  590. ppc_last[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  591. ppc_last += 3;
  592. } else if (info->pad == '0') { /* 0 padding */
  593. ppc[4] += width; /* Pad second field. */
  594. } else {
  595. ppc[1] += width; /* Pad first (sign) field. */
  596. }
  597. }
  598. cnt = 0;
  599. }
  600. do {
  601. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  602. if ((ppc == pc_fwi + 6) && num_groups) {
  603. const char *gp = (const char *) ppc[2];
  604. int len = ppc[1];
  605. int blk = initial_group;
  606. cnt += num_groups * tslen; /* Adjust count now for sep chars. */
  607. /* printf("\n"); */
  608. do {
  609. if (!blk) { /* Initial group could be 0 digits long! */
  610. blk = nblk2;
  611. } else if (len >= blk) { /* Enough digits for a group. */
  612. /* printf("norm: len=%d blk=%d \"%.*s\"\n", len, blk, blk, gp); */
  613. if (fp_outfunc(fp, *ppc, blk, (intptr_t) gp) != blk) {
  614. return -1;
  615. }
  616. assert(gp);
  617. if (*gp) {
  618. gp += blk;
  619. }
  620. len -= blk;
  621. } else { /* Transition to 0s. */
  622. /* printf("trans: len=%d blk=%d \"%.*s\"\n", len, blk, len, gp); */
  623. if (len) {
  624. /* printf("len\n"); */
  625. if (fp_outfunc(fp, *ppc, len, (intptr_t) gp) != len) {
  626. return -1;
  627. }
  628. gp += len;
  629. }
  630. if (ppc[3] == FPO_ZERO_PAD) { /* Need to group 0s */
  631. /* printf("zeropad\n"); */
  632. cnt += ppc[1];
  633. ppc += 3;
  634. gp = (const char *) ppc[2];
  635. blk -= len; /* blk > len, so blk still > 0. */
  636. len = ppc[1];
  637. continue; /* Don't decrement num_groups here. */
  638. } else {
  639. assert(num_groups == 0);
  640. break;
  641. }
  642. }
  643. if (num_groups <= 0) {
  644. break;
  645. }
  646. --num_groups;
  647. if (fp_outfunc(fp, FPO_STR_PREC, tslen, (intptr_t) ts) != tslen) {
  648. return -1;
  649. }
  650. blk = nblk2;
  651. /* printf("num_groups=%d blk=%d\n", num_groups, blk); */
  652. } while (1);
  653. } else
  654. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  655. { /* NOTE: Remember 'else' above! */
  656. if (fp_outfunc(fp, *ppc, ppc[1], ppc[2]) != ppc[1]) {
  657. return -1;
  658. }
  659. }
  660. cnt += ppc[1];
  661. ppc += 3;
  662. } while (ppc < ppc_last);
  663. return cnt;
  664. }