_fpmaxtostr.c 19 KB

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