diff options
author | 2008-09-07 20:36:06 +0000 | |
---|---|---|
committer | 2008-09-07 20:36:06 +0000 | |
commit | 7b36286a70b46b494e2bca4a889ab49ef62ba86c (patch) | |
tree | b2b9870e3495ed88715e57b0d4a9888b16264fe0 /lib/libc/stdio/vfprintf.c | |
parent | sparc now requires this bloated library to be -fPIC (diff) | |
download | wireguard-openbsd-7b36286a70b46b494e2bca4a889ab49ef62ba86c.tar.xz wireguard-openbsd-7b36286a70b46b494e2bca4a889ab49ef62ba86c.zip |
- replace dtoa w/ David's gdtoa, version 2008-03-15
- provide proper dtoa locks
- use the real strtof implementation
- add strtold, __hdtoa, __hldtoa
- add %a/%A support
- don't lose precision in printf, don't round to double anymore
- implement extended-precision versions of libc functions: fpclassify,
isnan, isinf, signbit, isnormal, isfinite, now that the ieee.h is
fixed
- separate vax versions of strtof, and __hdtoa
- add complex math support. added functions: cacos, casin, catan,
ccos, csin, ctan, cacosh, casinh, catanh, ccosh, csinh, ctanh, cexp,
clog, cabs, cpow, csqrt, carg, cimag, conj, cproj, creal, cacosf,
casinf, catanf, ccosf, csinf, ctanf, cacoshf, casinhf, catanhf,
ccoshf, csinhf, ctanhf, cexpf, clogf, cabsf, cpowf, csqrtf, cargf,
cimagf, conjf, cprojf, crealf
- add fdim, fmax, fmin
- add log2. (adapted implementation e_log.c. could be more acruate
& faster, but it's good enough for now)
- remove wrappers & cruft in libm, supposed to work-around mistakes
in SVID, etc.; use ieee versions. fixes issues in python 2.6 for
djm@
- make _digittoint static
- proper definitions for i386, and amd64 in ieee.h
- sh, powerpc don't really have extended-precision
- add missing definitions for mips64 (quad), m{6,8}k (96-bit) float.h
for LDBL_*
- merge lead to frac for m{6,8}k, for gdtoa to work properly
- add FRAC*BITS & EXT_TO_ARRAY32 definitions in ieee.h, for hdtoa&ldtoa
to use
- add EXT_IMPLICIT_NBIT definition, which indicates implicit
normalization bit
- add regression tests for libc: fpclassify and printf
- arith.h & gd_qnan.h definitions
- update ieee.h: hppa doesn't have quad-precision, hppa64 does
- add missing prototypes to gdtoaimp
- on 64-bit platforms make sure gdtoa doesn't use a long when it
really wants an int
- etc., what i may have forgotten...
- bump libm major, due to removed&changed symbols
- no libc bump, since this is riding on djm's libc major crank from
a day ago
discussed with / requested by / testing theo, sthen@, djm@, jsg@,
merdely@, jsing@, tedu@, brad@, jakemsr@, and others.
looks good to millert@
parts of the diff ok kettenis@
this commit does not include:
- man page changes
Diffstat (limited to 'lib/libc/stdio/vfprintf.c')
-rw-r--r-- | lib/libc/stdio/vfprintf.c | 356 |
1 files changed, 195 insertions, 161 deletions
diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c index 9e651cab145..59af7eb7a22 100644 --- a/lib/libc/stdio/vfprintf.c +++ b/lib/libc/stdio/vfprintf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vfprintf.c,v 1.51 2008/08/27 00:40:38 martynas Exp $ */ +/* $OpenBSD: vfprintf.c,v 1.52 2008/09/07 20:36:08 martynas Exp $ */ /*- * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. @@ -140,22 +140,27 @@ __sbprintf(FILE *fp, const char *fmt, va_list ap) #ifdef FLOATING_POINT +#include <float.h> #include <locale.h> #include <math.h> #include "floatio.h" -#define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ #define DEFPREC 6 extern char *__dtoa(double, int, int, int *, int *, char **); extern void __freedtoa(char *); -static char *cvt(double, int, int, int *, int *, int, int *); static int exponent(char *, int, int); - -#else /* no FLOATING_POINT */ -#define BUF 40 #endif /* FLOATING_POINT */ +/* + * The size of the buffer we use as scratch space for integer + * conversions, among other things. Technically, we would need the + * most space for base 10 conversions with thousands' grouping + * characters between each pair of digits. 100 bytes is a + * conservative overestimate even for a 128-bit uintmax_t. + */ +#define BUF 100 + #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ @@ -170,7 +175,6 @@ static int exponent(char *, int, int); * Flags used during conversion. */ #define ALT 0x0001 /* alternate form */ -#define HEXPREFIX 0x0002 /* add 0x or 0X prefix */ #define LADJUST 0x0004 /* left adjustment */ #define LONGDBL 0x0008 /* long double; unimplemented */ #define LONGINT 0x0010 /* long integer */ @@ -194,32 +198,52 @@ vfprintf(FILE *fp, const char *fmt0, __va_list ap) int flags; /* flags as above */ int ret; /* return value accumulator */ int width; /* width from format (%8d), or 0 */ - int prec; /* precision from format (%.3d), or -1 */ + int prec; /* precision from format; <0 for N/A */ char sign; /* sign prefix (' ', '+', '-', or \0) */ wchar_t wc; mbstate_t ps; #ifdef FLOATING_POINT + /* + * We can decompose the printed representation of floating + * point numbers into several parts, some of which may be empty: + * + * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ + * A B ---C--- D E F + * + * A: 'sign' holds this value if present; '\0' otherwise + * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal + * C: cp points to the string MMMNNN. Leading and trailing + * zeros are not in the string and must be added. + * D: expchar holds this character; '\0' if no exponent, e.g. %f + * F: at least two digits for decimal, at least one digit for hex + */ char *decimal_point = localeconv()->decimal_point; - int softsign; /* temporary negative sign for floats */ - double _double; /* double precision arguments %[eEfFgG] */ + int signflag; /* true if float is negative */ + union { /* floating point arguments %[aAeEfFgG] */ + double dbl; + long double ldbl; + } fparg; int expt; /* integer value of exponent */ + char expchar; /* exponent character: [eEpP\0] */ + char *dtoaend; /* pointer to end of converted digits */ int expsize; /* character count for expstr */ - int ndig; /* actual number of digits returned by cvt */ - char expstr[7]; /* buffer for exponent string */ + int lead; /* sig figs before decimal or group sep */ + int ndig; /* actual number of digits returned by dtoa */ + char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ char *dtoaresult = NULL; #endif uintmax_t _umax; /* integer arguments %[diouxX] */ - enum { OCT, DEC, HEX } base;/* base for %[diouxX] conversion */ + enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */ int dprec; /* a copy of prec if %[diouxX], 0 otherwise */ int realsz; /* field size expanded by dprec */ int size; /* size of converted field or string */ - char *xdigs; /* digits for %[xX] conversion */ + const char *xdigs; /* digits for %[xX] conversion */ #define NIOV 8 struct __suio uio; /* output information: summary */ struct __siov iov[NIOV];/* ... and individual io vectors */ - char buf[BUF]; /* space for %c, %[diouxX], %[eEfFgG] */ - char ox[2]; /* space for 0x hex-prefix */ + char buf[BUF]; /* buffer with space for digits of uintmax_t */ + char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */ union arg *argtable; /* args, built due to positional arg */ union arg statargtable[STATIC_ARG_TBL_SIZE]; size_t argtablesiz; @@ -237,6 +261,9 @@ vfprintf(FILE *fp, const char *fmt0, __va_list ap) static char zeroes[PADSIZE] = {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; + static const char xdigs_lower[16] = "0123456789abcdef"; + static const char xdigs_upper[16] = "0123456789ABCDEF"; + /* * BEWARE, these `goto error' on error, and PAD uses `n'. */ @@ -260,6 +287,14 @@ vfprintf(FILE *fp, const char *fmt0, __va_list ap) PRINT(with, n); \ } \ } while (0) +#define PRINTANDPAD(p, ep, len, with) do { \ + n2 = (ep) - (p); \ + if (n2 > (len)) \ + n2 = (len); \ + if (n2 > 0) \ + PRINT((p), n2); \ + PAD((len) - (n2 > 0 ? n2 : 0), (with)); \ +} while(0) #define FLUSH() do { \ if (uio.uio_resid && __sprint(fp, &uio)) \ goto error; \ @@ -386,6 +421,7 @@ vfprintf(FILE *fp, const char *fmt0, __va_list ap) width = 0; prec = -1; sign = '\0'; + ox[1] = '\0'; rflag: ch = *fmt++; reswitch: switch (ch) { @@ -520,82 +556,122 @@ reswitch: switch (ch) { base = DEC; goto number; #ifdef FLOATING_POINT + case 'a': + case 'A': + if (ch == 'a') { + ox[1] = 'x'; + xdigs = xdigs_lower; + expchar = 'p'; + } else { + ox[1] = 'X'; + xdigs = xdigs_upper; + expchar = 'P'; + } + if (prec >= 0) + prec++; + if (dtoaresult) + __freedtoa(dtoaresult); + if (flags & LONGDBL) { + fparg.ldbl = GETARG(long double); + dtoaresult = cp = + __hldtoa(fparg.ldbl, xdigs, prec, + &expt, &signflag, &dtoaend); + } else { + fparg.dbl = GETARG(double); + dtoaresult = cp = + __hdtoa(fparg.dbl, xdigs, prec, + &expt, &signflag, &dtoaend); + } + if (prec < 0) + prec = dtoaend - cp; + if (expt == INT_MAX) + ox[1] = '\0'; + goto fp_common; case 'e': case 'E': + expchar = ch; + if (prec < 0) /* account for digit before decpt */ + prec = DEFPREC + 1; + else + prec++; + goto fp_begin; case 'f': case 'F': + expchar = '\0'; + goto fp_begin; case 'g': case 'G': - if (prec == -1) { + expchar = ch - ('g' - 'e'); + if (prec == 0) + prec = 1; +fp_begin: + if (prec < 0) prec = DEFPREC; - } else if ((ch == 'g' || ch == 'G') && prec == 0) { - prec = 1; - } - + if (dtoaresult) + __freedtoa(dtoaresult); if (flags & LONGDBL) { - _double = (double) GETARG(long double); + fparg.ldbl = GETARG(long double); + dtoaresult = cp = + __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, + &expt, &signflag, &dtoaend); } else { - _double = GETARG(double); - } - - /* do this before tricky precision changes */ - if (isinf(_double)) { - if (_double < 0) - sign = '-'; - if (ch == 'E' || ch == 'F' || ch == 'G') - cp = "INF"; - else - cp = "inf"; - size = 3; - flags &= ~ZEROPAD; - break; - } - if (isnan(_double)) { - if (ch == 'E' || ch == 'F' || ch == 'G') - cp = "NAN"; - else - cp = "nan"; - size = 3; + fparg.dbl = GETARG(double); + dtoaresult = cp = + __dtoa(fparg.dbl, expchar ? 2 : 3, prec, + &expt, &signflag, &dtoaend); + if (expt == 9999) + expt = INT_MAX; + } +fp_common: + if (signflag) + sign = '-'; + if (expt == INT_MAX) { /* inf or nan */ + if (*cp == 'N') { + cp = (ch >= 'a') ? "nan" : "NAN"; + sign = '\0'; + } else + cp = (ch >= 'a') ? "inf" : "INF"; + size = 3; flags &= ~ZEROPAD; - break; - } - + break; + } flags |= FPT; - if (dtoaresult) - __freedtoa(dtoaresult); - dtoaresult = cp = cvt(_double, prec, flags, &softsign, - &expt, ch, &ndig); - if (ch == 'g' || ch == 'G') { - if (expt <= -4 || expt > prec) - ch = (ch == 'g') ? 'e' : 'E'; - else - ch = 'g'; - } - if (ch == 'e' || ch == 'E') { /* 'e' or 'E' fmt */ - --expt; - expsize = exponent(expstr, expt, ch); - size = expsize + ndig; - if (ndig > 1 || flags & ALT) - ++size; - } else if (ch == 'f' || ch == 'F') { - /* 'f' or 'F' fmt */ - if (expt > 0) { - size = expt; - if (prec || flags & ALT) - size += prec + 1; - } else { /* "0.X" */ - size = prec + 2; + ndig = dtoaend - cp; + if (ch == 'g' || ch == 'G') { + if (expt > -4 && expt <= prec) { + /* Make %[gG] smell like %[fF] */ + expchar = '\0'; + if (flags & ALT) + prec -= expt; + else + prec = ndig - expt; + if (prec < 0) + prec = 0; + } else { + /* + * Make %[gG] smell like %[eE], but + * trim trailing zeroes if no # flag. + */ + if (!(flags & ALT)) + prec = ndig; } - } else if (expt >= ndig) { /* fixed g fmt */ - size = expt; - if (flags & ALT) - ++size; + } + if (expchar) { + expsize = exponent(expstr, expt - 1, expchar); + size = expsize + prec; + if (prec > 1 || flags & ALT) + ++size; } else { - size = ndig + (expt > 0 ? 1 : 2 - expt); + /* space for digits before decimal point */ + if (expt > 0) + size = expt; + else /* "0" */ + size = 1; + /* space for decimal pt and following digits */ + if (prec || flags & ALT) + size += prec + 1; + lead = expt; } - - if (softsign) - sign = '-'; break; #endif /* FLOATING_POINT */ case 'n': @@ -634,9 +710,8 @@ reswitch: switch (ch) { /* NOSTRICT */ _umax = (u_long)GETARG(void *); base = HEX; - xdigs = "0123456789abcdef"; - flags |= HEXPREFIX; - ch = 'x'; + xdigs = xdigs_lower; + ox[1] = 'x'; goto nosign; case 's': if ((cp = GETARG(char *)) == NULL) @@ -667,15 +742,15 @@ reswitch: switch (ch) { base = DEC; goto nosign; case 'X': - xdigs = "0123456789ABCDEF"; + xdigs = xdigs_upper; goto hex; case 'x': - xdigs = "0123456789abcdef"; + xdigs = xdigs_lower; hex: _umax = UARG(); base = HEX; /* leading 0x/X only if non-zero */ if (flags & ALT && _umax != 0) - flags |= HEXPREFIX; + ox[1] = ch; /* unsigned conversions */ nosign: sign = '\0'; @@ -733,6 +808,8 @@ number: if ((dprec = prec) >= 0) } } size = buf + BUF - cp; + if (size > BUF) /* should never happen */ + abort(); skipsize: break; default: /* "%?" prints ?, unless ? is NUL */ @@ -763,7 +840,7 @@ number: if ((dprec = prec) >= 0) realsz = dprec > size ? dprec : size; if (sign) realsz++; - else if (flags & HEXPREFIX) + if (ox[1]) realsz+= 2; /* right-adjusting blank padding */ @@ -771,11 +848,10 @@ number: if ((dprec = prec) >= 0) PAD(width - realsz, blanks); /* prefix */ - if (sign) { + if (sign) PRINT(&sign, 1); - } else if (flags & HEXPREFIX) { + if (ox[1]) { /* ox[1] is either x, X, or \0 */ ox[0] = '0'; - ox[1] = ch; PRINT(ox, 2); } @@ -791,41 +867,28 @@ number: if ((dprec = prec) >= 0) if ((flags & FPT) == 0) { PRINT(cp, size); } else { /* glue together f_p fragments */ - if (ch >= 'f' || ch == 'F') { /* 'f', 'g' or 'F' */ - if (_double == 0) { - /* kludge for __dtoa irregularity */ - PRINT("0", 1); - if (expt < ndig || (flags & ALT) != 0) { + if (!expchar) { /* %[fF] or sufficiently short %[gG] */ + if (expt <= 0) { + PRINT(zeroes, 1); + if (prec || flags & ALT) PRINT(decimal_point, 1); - PAD(ndig - 1, zeroes); - } - } else if (expt <= 0) { - PRINT("0", 1); - PRINT(decimal_point, 1); PAD(-expt, zeroes); - PRINT(cp, ndig); - } else if (expt >= ndig) { - PRINT(cp, ndig); - PAD(expt - ndig, zeroes); - if (flags & ALT) + /* already handled initial 0's */ + prec += expt; + } else { + PRINTANDPAD(cp, dtoaend, lead, zeroes); + cp += lead; + if (prec || flags & ALT) PRINT(".", 1); - } else { - PRINT(cp, expt); - cp += expt; - PRINT(".", 1); - PRINT(cp, ndig-expt); } - } else { /* 'e' or 'E' */ - if (ndig > 1 || flags & ALT) { - ox[0] = *cp++; - ox[1] = '.'; - PRINT(ox, 2); - if (_double) { - PRINT(cp, ndig-1); - } else {/* 0.[0..] */ - /* __dtoa irregularity */ - PAD(ndig - 1, zeroes); - } + PRINTANDPAD(cp, dtoaend, prec, zeroes); + } else { /* %[eE] or sufficiently long %[gG] */ + if (prec > 1 || flags & ALT) { + buf[0] = *cp++; + buf[1] = '.'; + PRINT(buf, 2); + PRINT(cp, ndig-1); + PAD(prec - ndig, zeroes); } else { /* XeYYY */ PRINT(cp, 1); } @@ -1076,6 +1139,8 @@ reswitch: switch (ch) { ADDSARG(); break; #ifdef FLOATING_POINT + case 'a': + case 'A': case 'e': case 'E': case 'f': @@ -1266,49 +1331,11 @@ __grow_type_table(unsigned char **typetable, int *tablesize) #ifdef FLOATING_POINT - -static char * -cvt(double value, int ndigits, int flags, int *sign, int *decpt, int ch, - int *length) -{ - int mode; - char *digits, *bp, *rve; - - if (ch == 'f' || ch == 'F') { - mode = 3; /* ndigits after the decimal point */ - } else { - /* To obtain ndigits after the decimal point for the 'e' - * and 'E' formats, round to ndigits + 1 significant - * figures. - */ - if (ch == 'e' || ch == 'E') { - ndigits++; - } - mode = 2; /* ndigits significant digits */ - } - - digits = __dtoa(value, mode, ndigits, decpt, sign, &rve); - if ((ch != 'g' && ch != 'G') || flags & ALT) {/* Print trailing zeros */ - bp = digits + ndigits; - if (ch == 'f' || ch == 'F') { - if (*digits == '0' && value) - *decpt = -ndigits + 1; - bp += *decpt; - } - if (value == 0) /* kludge for __dtoa irregularity */ - rve = bp; - while (rve < bp) - *rve++ = '0'; - } - *length = rve - digits; - return (digits); -} - static int exponent(char *p0, int exp, int fmtch) { char *p, *t; - char expbuf[MAXEXP]; + char expbuf[MAXEXPDIG]; p = p0; *p++ = fmtch; @@ -1317,16 +1344,23 @@ exponent(char *p0, int exp, int fmtch) *p++ = '-'; } else *p++ = '+'; - t = expbuf + MAXEXP; + t = expbuf + MAXEXPDIG; if (exp > 9) { do { *--t = to_char(exp % 10); } while ((exp /= 10) > 9); *--t = to_char(exp); - for (; t < expbuf + MAXEXP; *p++ = *t++) + for (; t < expbuf + MAXEXPDIG; *p++ = *t++) /* nothing */; } else { - *p++ = '0'; + /* + * Exponents for decimal floating point conversions + * (%[eEgG]) must be at least two characters long, + * whereas exponents for hexadecimal conversions can + * be only one character long. + */ + if (fmtch == 'e' || fmtch == 'E') + *p++ = '0'; *p++ = to_char(exp); } return (p - p0); |