_fpmaxtostr.c 19 KB

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