_fpmaxtostr.c 17 KB

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