vfprintf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* Originally released by the musl project (http://www.musl-libc.org/) under the
  2. * MIT license. Taken from the file src/stdio/vfprintf.c */
  3. #include <ctype.h>
  4. #include "floatscan.h"
  5. #include "vfprintf.h"
  6. //int isdigit(int);
  7. //#define isdigit(a) (0 ? isdigit(a) : ((unsigned)(a)-'0') < 10)
  8. long double frexpl(long double x, int *e);
  9. //#include <math.h>
  10. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  11. long double frexpl(long double x, int *e)
  12. {
  13. return frexp(x, e);
  14. }
  15. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  16. long double frexpl(long double x, int *e)
  17. {
  18. union ldshape u = {x};
  19. int ee = u.i.se & 0x7fff;
  20. if (!ee) {
  21. if ((_Bool)x) {
  22. x = frexpl(x*0x1p120, e);
  23. *e -= 120;
  24. } else *e = 0;
  25. return x;
  26. } else if (ee == 0x7fff) {
  27. return x;
  28. }
  29. *e = ee - 0x3ffe;
  30. u.i.se &= 0x8000;
  31. u.i.se |= 0x3ffe;
  32. return u.f;
  33. }
  34. #endif
  35. int __signbitl(long double x);
  36. #if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  37. int __signbitl(long double x)
  38. {
  39. union ldshape u = {x};
  40. return u.i.se >> 15;
  41. }
  42. #elif LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  43. int __signbitl(long double x)
  44. {
  45. return __signbit(x);
  46. }
  47. #endif
  48. static __inline unsigned __FLOAT_BITS(float __f)
  49. {
  50. union {float __f; unsigned __i;} __u;
  51. __u.__f = __f;
  52. return __u.__i;
  53. }
  54. static __inline unsigned long long __DOUBLE_BITS(double __f)
  55. {
  56. union {double __f; unsigned long long __i;} __u;
  57. __u.__f = __f;
  58. return __u.__i;
  59. }
  60. #define signbit(x) ( \
  61. sizeof(x) == sizeof(float) ? (int)(__FLOAT_BITS((float)x)>>31) : \
  62. sizeof(x) == sizeof(double) ? (int)(__DOUBLE_BITS((double)x)>>63) : \
  63. __signbitl(x) )
  64. #define FP_NAN 0
  65. #define FP_INFINITE 1
  66. #define FP_ZERO 2
  67. #define FP_SUBNORMAL 3
  68. #define FP_NORMAL 4
  69. int __fpclassifyl(long double x);
  70. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  71. int __fpclassifyl(long double x)
  72. {
  73. return __fpclassify(x);
  74. }
  75. #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
  76. int __fpclassifyl(long double x)
  77. {
  78. union ldshape u = {x};
  79. int e = u.i.se & 0x7fff;
  80. int msb = (int)(u.i.m>>63);
  81. if (!e && !msb)
  82. return u.i.m ? FP_SUBNORMAL : FP_ZERO;
  83. if (!msb)
  84. return FP_NAN;
  85. if (e == 0x7fff)
  86. return u.i.m << 1 ? FP_NAN : FP_INFINITE;
  87. return FP_NORMAL;
  88. }
  89. #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
  90. int __fpclassifyl(long double x)
  91. {
  92. union ldshape u = {x};
  93. int e = u.i.se & 0x7fff;
  94. u.i.se = 0;
  95. if (!e)
  96. return u.i2.lo | u.i2.hi ? FP_SUBNORMAL : FP_ZERO;
  97. if (e == 0x7fff)
  98. return u.i2.lo | u.i2.hi ? FP_NAN : FP_INFINITE;
  99. return FP_NORMAL;
  100. }
  101. #endif
  102. #define isfinite(x) ( \
  103. sizeof(x) == sizeof(float) ? (__FLOAT_BITS((float)x) & 0x7fffffff) < 0x7f800000 : \
  104. sizeof(x) == sizeof(double) ? (__DOUBLE_BITS((double)x) & -1ULL>>1) < 0x7ffULL<<52 : \
  105. __fpclassifyl(x) > FP_INFINITE)
  106. #include "vfprintf.h"
  107. /* Some useful macros */
  108. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  109. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  110. /* Convenient bit representation for modifier flags, which all fall
  111. * within 31 codepoints of the space character. */
  112. #define ALT_FORM (1U<<('#'-' '))
  113. #define ZERO_PAD (1U<<('0'-' '))
  114. #define LEFT_ADJ (1U<<('-'-' '))
  115. #define PAD_POS (1U<<(' '-' '))
  116. #define MARK_POS (1U<<('+'-' '))
  117. #define GROUPED (1U<<('\''-' '))
  118. #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
  119. /* State machine to accept length modifiers + conversion specifiers.
  120. * Result is 0 on failure, or an argument type to pop on success. */
  121. enum {
  122. BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
  123. ZTPRE, JPRE,
  124. STOP,
  125. PTR, INT, UINT, ULLONG,
  126. LONG, ULONG,
  127. SHORT, USHORT, CHAR, UCHAR,
  128. LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
  129. DBL, LDBL,
  130. NOARG,
  131. MAXSTATE
  132. };
  133. #define S(x) [(x)-'A']
  134. /*
  135. static const unsigned char states[]['z'-'A'+1] = {
  136. { // 0: bare types
  137. S('d') = INT, S('i') = INT,
  138. S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
  139. S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
  140. S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
  141. S('c') = CHAR, S('C') = INT,
  142. S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
  143. S('m') = NOARG,
  144. S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
  145. S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
  146. }, { // 1: l-prefixed
  147. S('d') = LONG, S('i') = LONG,
  148. S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
  149. S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
  150. S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
  151. S('c') = INT, S('s') = PTR, S('n') = PTR,
  152. S('l') = LLPRE,
  153. }, { // 2: ll-prefixed
  154. S('d') = LLONG, S('i') = LLONG,
  155. S('o') = ULLONG, S('u') = ULLONG,
  156. S('x') = ULLONG, S('X') = ULLONG,
  157. S('n') = PTR,
  158. }, { //3: h-prefixed
  159. S('d') = SHORT, S('i') = SHORT,
  160. S('o') = USHORT, S('u') = USHORT,
  161. S('x') = USHORT, S('X') = USHORT,
  162. S('n') = PTR,
  163. S('h') = HHPRE,
  164. }, { // 4: hh-prefixed
  165. S('d') = CHAR, S('i') = CHAR,
  166. S('o') = UCHAR, S('u') = UCHAR,
  167. S('x') = UCHAR, S('X') = UCHAR,
  168. S('n') = PTR,
  169. }, { // 5: L-prefixed
  170. S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
  171. S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
  172. S('n') = PTR,
  173. }, { // 6: z- or t-prefixed (assumed to be same size)
  174. S('d') = PDIFF, S('i') = PDIFF,
  175. S('o') = SIZET, S('u') = SIZET,
  176. S('x') = SIZET, S('X') = SIZET,
  177. S('n') = PTR,
  178. }, { //7: j-prefixed
  179. S('d') = IMAX, S('i') = IMAX,
  180. S('o') = UMAX, S('u') = UMAX,
  181. S('x') = UMAX, S('X') = UMAX,
  182. S('n') = PTR,
  183. }
  184. };
  185. */
  186. #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
  187. static void out(char **sp, const char *s, size_t l)
  188. {
  189. //if (!(f->flags & F_ERR)) __fwritex((void *)s, l, f);
  190. while (l--) {
  191. **sp = *s;
  192. (*sp)++;
  193. s++;
  194. }
  195. }
  196. static void pad(char **sp, char c, int w, int l, int fl)
  197. {
  198. char pad[256];
  199. if ((unsigned int)fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
  200. l = w - l;
  201. memset(pad, c, (long unsigned int)l>sizeof pad ? sizeof pad : (long unsigned int)l);
  202. for (; (long unsigned int)l >= sizeof pad; l = l - (int)(sizeof pad))
  203. out(sp, pad, sizeof pad);
  204. out(sp, pad, (size_t)l);
  205. }
  206. static const char xdigits[17] = {"0123456789ABCDEF"};
  207. /*
  208. static char *fmt_x(uintmax_t x, char *s, int lower)
  209. {
  210. for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
  211. return s;
  212. }
  213. static char *fmt_o(uintmax_t x, char *s)
  214. {
  215. for (; x; x>>=3) *--s = '0' + (x&7);
  216. return s;
  217. }
  218. */
  219. static char *fmt_u(uintmax_t x, char *s)
  220. {
  221. unsigned long y;
  222. for ( ; x>ULONG_MAX; x/=10) *--s = (char)('0' + x%10);
  223. for (y=x; y; y/=10) *--s = (char)('0' + y%10);
  224. return s;
  225. }
  226. /* Do not override this check. The floating point printing code below
  227. * depends on the float.h constants being right. If they are wrong, it
  228. * may overflow the stack. */
  229. #if LDBL_MANT_DIG == 53
  230. typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)];
  231. #endif
  232. /*
  233. * w = string width, p = precision, fl = flags, t = type. "%20.5g gives w=20, p=5, fl=0, t='g'
  234. */
  235. int fmt_fp(char *output, long double y, int w, int p, int fl, int t)
  236. {
  237. char* sp = output;
  238. uint32_t big[(LDBL_MANT_DIG+28)/29 + 1 // mantissa expansion
  239. + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
  240. uint32_t *a, *d, *r, *z;
  241. int e2=0, e, i, j, l;
  242. char buf[9+LDBL_MANT_DIG/4], *s;
  243. const char *prefix="-0X+0X 0X-0x+0x 0x";
  244. int pl;
  245. char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr = NULL;
  246. pl=1;
  247. if (signbit(y)) {
  248. y=-y;
  249. } else if ((unsigned int)fl & MARK_POS) {
  250. prefix+=3;
  251. } else if ((unsigned int)fl & PAD_POS) {
  252. prefix+=6;
  253. } else prefix++, pl=0;
  254. if (!isfinite(y)) {
  255. s = (t&32)?"inf":"INF";
  256. if (y!=y) s=(t&32)?"nan":"NAN";
  257. pad(&sp, ' ', w, 3+pl, (int)((unsigned int)fl &~ ZERO_PAD));
  258. out(&sp, prefix, (size_t)pl);
  259. out(&sp, s, 3);
  260. pad(&sp, ' ', w, 3+pl, (int)((unsigned int)fl^LEFT_ADJ));
  261. return MAX(w, 3+pl);
  262. }
  263. y = frexpl(y, &e2) * 2;
  264. if ((_Bool)y) e2--;
  265. if ((t|32)=='a') {
  266. long double round = 8.0;
  267. int re;
  268. if (t&32) prefix += 9;
  269. pl += 2;
  270. if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
  271. else re=LDBL_MANT_DIG/4-1-p;
  272. if (re) {
  273. while (re--) round*=16;
  274. if (*prefix=='-') {
  275. y=-y;
  276. y-=round;
  277. y+=round;
  278. y=-y;
  279. } else {
  280. y+=round;
  281. y-=round;
  282. }
  283. }
  284. estr=fmt_u((uintmax_t )(e2<0 ? -e2 : e2), ebuf);
  285. if (estr==ebuf) *--estr='0';
  286. *--estr = (e2<0 ? '-' : '+');
  287. *--estr = (char)(t+('p'-'a'));
  288. s=buf;
  289. do {
  290. int x=(int)y;
  291. *s++=(char)(xdigits[x]|(t&32));
  292. y=16*(y-x);
  293. if (s-buf==1 && ((_Bool)y||p>0||((unsigned int)fl&ALT_FORM))) *s++='.';
  294. } while ((_Bool)y);
  295. if (p > INT_MAX-2-(ebuf-estr)-pl)
  296. return -1;
  297. if (p && s-buf-2 < p)
  298. l = (int)((p+2) + (ebuf-estr));
  299. else
  300. l = (int)((s-buf) + (ebuf-estr));
  301. pad(&sp, ' ', w, pl+l, fl);
  302. out(&sp, prefix, (size_t)pl);
  303. pad(&sp, '0', w, pl+l, (int)((unsigned int)fl^ZERO_PAD));
  304. out(&sp, buf, (size_t)(s-buf));
  305. pad(&sp, '0', (int)(l-(ebuf-estr)-(s-buf)), 0, 0);
  306. out(&sp, estr, (size_t)(ebuf-estr));
  307. pad(&sp, ' ', w, pl+l, (int)((unsigned int)fl^LEFT_ADJ));
  308. return MAX(w, pl+l);
  309. }
  310. if (p<0) p=6;
  311. if ((_Bool)y) y *= 0x1p28, e2-=28;
  312. if (e2<0) a=r=z=big;
  313. else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
  314. do {
  315. *z = (uint32_t)y;
  316. y = 1000000000*(y-*z++);
  317. } while ((_Bool)y);
  318. while (e2>0) {
  319. uint32_t carry=0;
  320. int sh=MIN(29,e2);
  321. for (d=z-1; d>=a; d--) {
  322. uint64_t x = ((uint64_t)*d<<sh)+carry;
  323. *d = (uint32_t)(x % 1000000000);
  324. carry = (uint32_t)(x / 1000000000);
  325. }
  326. if (carry) *--a = carry;
  327. while (z>a && !z[-1]) z--;
  328. e2-=sh;
  329. }
  330. while (e2<0) {
  331. uint32_t carry=0, *b;
  332. int sh=MIN(9,-e2), need=(int)(1+((unsigned int)p+LDBL_MANT_DIG/3U+8)/9);
  333. for (d=a; d<z; d++) {
  334. uint32_t rm = (*d & (uint32_t)((1<<sh)-1));
  335. *d = (*d>>sh) + carry;
  336. carry = ((uint32_t)(1000000000>>sh) * rm);
  337. }
  338. if (!*a) a++;
  339. if (carry) *z++ = carry;
  340. /* Avoid (slow!) computation past requested precision */
  341. b = (t|32)=='f' ? r : a;
  342. if (z-b > need) z = b+need;
  343. e2+=sh;
  344. }
  345. if (a<z) for (i=10, e=(int)(9*(r-a)); *a>=(unsigned)i; i*=10, e++);
  346. else e=0;
  347. /* Perform rounding: j is precision after the radix (possibly neg) */
  348. j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
  349. if (j < 9*(z-r-1)) {
  350. uint32_t x;
  351. /* We avoid C's broken division of negative numbers */
  352. d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
  353. j += 9*LDBL_MAX_EXP;
  354. j %= 9;
  355. for (i=10, j++; j<9; i*=10, j++);
  356. x = (*d % (uint32_t)i);
  357. /* Are there any significant digits past j? */
  358. if (x || d+1!=z) {
  359. long double round = 2/LDBL_EPSILON;
  360. long double small;
  361. if ((*d/(uint32_t)(i) & 1) || (i==1000000000 && d>a && (d[-1]&1)))
  362. round += 2;
  363. if (x<(unsigned)i/2) small=0x0.8p0;
  364. else if (x==(unsigned)i/2 && d+1==z) small=0x1.0p0;
  365. else small=0x1.8p0;
  366. if (pl && *prefix=='-') round*=-1, small*=-1;
  367. *d -= x;
  368. /* Decide whether to round by probing round+small */
  369. if (round+small != round) {
  370. *d = *d + (uint32_t)i;
  371. while (*d > 999999999) {
  372. *d--=0;
  373. if (d<a) *--a=0;
  374. (*d)++;
  375. }
  376. for (i=10, e=(int)(9*(r-a)); *a>=(unsigned)i; i*=10, e++);
  377. }
  378. }
  379. if (z>d+1) z=d+1;
  380. }
  381. for (; z>a && !z[-1]; z--);
  382. if ((t|32)=='g') {
  383. if (!p) p++;
  384. if (p>e && e>=-4) {
  385. t--;
  386. p-=e+1;
  387. } else {
  388. t-=2;
  389. p--;
  390. }
  391. if (!((uint32_t)fl&ALT_FORM)) {
  392. /* Count trailing zeros in last place */
  393. if (z>a && z[-1]) for (i=10, j=0; (z[-1]%(uint32_t)i)==0; i*=10, j++);
  394. else j=9;
  395. if ((t|32)=='f')
  396. p = (int)MIN(p,MAX(0,9*(z-r-1)-j));
  397. else
  398. p = (int)MIN(p,MAX(0,9*(z-r-1)+e-j));
  399. }
  400. }
  401. if (p > INT_MAX-1-(p || ((unsigned int)fl&ALT_FORM)))
  402. return -1;
  403. l = 1 + p + (p || ((unsigned int)fl&ALT_FORM));
  404. if ((t|32)=='f') {
  405. if (e > INT_MAX-l) return -1;
  406. if (e>0) l+=e;
  407. } else {
  408. estr=fmt_u((uintmax_t)(e<0 ? -e : e), ebuf);
  409. while(ebuf-estr<2) *--estr='0';
  410. *--estr = (e<0 ? '-' : '+');
  411. *--estr = (char)t;
  412. if (ebuf-estr > INT_MAX-l) return -1;
  413. l += (int)(ebuf-estr);
  414. }
  415. if (l > INT_MAX-pl) return -1;
  416. pad(&sp, ' ', w, pl+l, fl);
  417. out(&sp, prefix, (size_t)pl);
  418. pad(&sp, '0', w, pl+l, (int)((unsigned int)fl^ZERO_PAD));
  419. if ((t|32)=='f') {
  420. if (a>r) a=r;
  421. for (d=a; d<=r; d++) {
  422. s = fmt_u(*d, buf+9); //@@@
  423. if (d!=a) while (s>buf) *--s='0';
  424. else if (s==buf+9) *--s='0';
  425. out(&sp, s, (size_t)(buf+9-s));
  426. }
  427. if (p || ((unsigned int)fl&ALT_FORM)) out(&sp, ".", 1);
  428. for (; d<z && p>0; d++, p-=9) {
  429. s = fmt_u(*d, buf+9); //@@@
  430. while (s>buf) *--s='0';
  431. out(&sp, s, (size_t)(MIN(9,p)));
  432. }
  433. pad(&sp, '0', p+9, 9, 0);
  434. } else {
  435. if (z<=a) z=a+1;
  436. for (d=a; d<z && p>=0; d++) {
  437. s = fmt_u(*d, buf+9); //@@@
  438. if (s==buf+9) *--s='0';
  439. if (d!=a) while (s>buf) *--s='0';
  440. else {
  441. out(&sp, s++, 1);
  442. if (p>0||((unsigned int)fl&ALT_FORM)) out(&sp, ".", 1);
  443. }
  444. out(&sp, s, (size_t)(MIN(buf+9-s, p)));
  445. p -= (int)(buf+9-s);
  446. }
  447. pad(&sp, '0', p+18, 18, 0);
  448. out(&sp, estr, (size_t)(ebuf-estr));
  449. }
  450. pad(&sp, ' ', w, pl+l, (int)((unsigned int)fl^LEFT_ADJ));
  451. return MAX(w, pl+l);
  452. }
  453. /*
  454. static int getint(char **s) {
  455. int i;
  456. for (i=0; isdigit(**s); (*s)++) {
  457. if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
  458. else i = 10*i + (**s-'0');
  459. }
  460. return i;
  461. }
  462. */