_fpmaxtostr.c 18 KB

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