1 --- vfwprintf.c.orig Tue Apr 6 17:39:47 2004
2 +++ vfwprintf.c Tue Apr 6 17:46:51 2004
8 #include "un-namespace.h"
10 #include "libc_private.h"
15 +#include <machine/cpu_capabilities.h>
17 +#define VECTORTYPE vector unsigned char
20 /* Define FLOATING_POINT to get floating point. */
21 #define FLOATING_POINT
23 +/* if no floating point, turn off HEXFLOAT as well */
24 +#if defined(HEXFLOAT) && !defined(FLOATING_POINT)
26 +#endif /* defined(HEXFLOAT) && !defined(FLOATING_POINT) */
33 long long *plonglongarg;
34 ptrdiff_t *pptrdiffarg;
45 + VECTORTYPE vectorarg;
46 + unsigned char vuchararg[16];
47 + signed char vchararg[16];
48 + unsigned short vushortarg[8];
49 + signed short vshortarg[8];
50 + unsigned int vuintarg[4];
51 + signed int vintarg[4];
58 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
59 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET,
60 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
62 + T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR, T_VECTOR
63 +#else /* ! ALTIVEC */
64 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
68 static int __sbprintf(FILE *, const wchar_t *, va_list);
70 static void __find_arguments(const wchar_t *, va_list, union arg **);
71 static void __grow_type_table(int, enum typeid **, int *);
74 + * Get the argument indexed by nextarg. If the argument table is
75 + * built, use it to get the argument. If its not, get the next
76 + * argument (and arguments must be gotten sequentially).
78 +#define GETARG(type) \
79 + ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
80 + (nextarg++, va_arg(ap, type)))
83 +#define hasAltivec (_cpu_capabilities & kHasAltivec)
84 +/*-----------------------------------------------------------------------
85 + * getvec() must be a real subroutine. If it is a #define, then __vfprintf()
86 + * would have its calling sequence changed by Altivec so that a non-Altivec
87 + * processor would crash on illegal instruction. By isolating the calling
88 + * sequence in getvec(), __vprintf() is callable by a non-Altivec processor.
89 + *-----------------------------------------------------------------------*/
91 +getvec(union arg *dst, const union arg *argtable, int nextarg, va_list ap)
93 + dst->vectorarg = GETARG(VECTORTYPE);
97 +#define SETVEC(dst) \
99 + ap = getvec(&dst, argtable, nextarg, ap); \
102 +#endif /* ALTIVEC */
105 * Helper function for `fprintf to unbuffered unix file': creates a
106 * temporary buffer. We only work on write-only files; this avoids
109 #endif /* FLOATING_POINT */
112 +extern int __hdtoa(double d, const char *xdigs, int prec, char *cp,
113 + int *expt, int *signflag, char **dtoaend);
114 +#if !__TYPE_LONGDOUBLE_IS_DOUBLE
115 +extern int __hldtoa(long double d, const char *xdigs, int prec, char *cp,
116 + int *expt, int *signflag, char **dtoaend);
117 +#endif /* !__TYPE_LONGDOUBLE_IS_DOUBLE */
118 +#endif /* HEXFLOAT */
121 * The size of the buffer we use as scratch space for integer
122 * conversions, among other things. Technically, we would need the
124 #define PTRDIFFT 0x800 /* ptrdiff_t */
125 #define INTMAXT 0x1000 /* intmax_t */
126 #define CHARINT 0x2000 /* print char using int format */
128 +#define VECTOR 0x4000 /* Altivec vector */
129 +#endif /* ALTIVEC */
132 * Non-MT-safe version
134 int nseps; /* number of group separators with ' */
135 int nrepeats; /* number of repeats of the last group */
138 + union arg vval; /* Vector argument. */
139 + wchar_t *pct; /* Pointer to '%' at beginning of specifier. */
140 + wchar_t vsep; /* Vector separator character. */
142 u_long ulval; /* integer arguments %[diouxX] */
143 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
144 int base; /* base for [diouxX] conversion */
147 static const wchar_t xdigs_lower[16] = L"0123456789abcdef";
148 static const wchar_t xdigs_upper[16] = L"0123456789ABCDEF";
150 +#define HEXFLOATDELTA 32
151 +#define HEXFLOATSTART 32
152 + static char *hexfloat = NULL;
153 + static int hexfloatlen = 0;
154 + const char *xdigs0; /* digits for [aA] conversion */
155 + static const char xdigs_lower0[16] = "0123456789abcdef";
156 + static const char xdigs_upper0[16] = "0123456789ABCDEF";
157 +#endif /* HEXFLOAT */
160 * BEWARE, these `goto error' on error, PRINT uses `n2' and
165 - * Get the argument indexed by nextarg. If the argument table is
166 - * built, use it to get the argument. If its not, get the next
167 - * argument (and arguments must be gotten sequentially).
169 -#define GETARG(type) \
170 - ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
171 - (nextarg++, va_arg(ap, type)))
174 * To extend shorts properly, we need both signed and unsigned
175 * argument extraction methods.
178 val = GETARG (int); \
182 thousands_sep = '\0';
184 #ifdef FLOATING_POINT
188 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
190 + if (cantwrite(fp)) {
195 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
196 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
203 +#endif /* ALTIVEC */
204 fmt++; /* skip over '%' */
212 + vsep = 'X'; /* Illegal value, changed to defaults later. */
213 +#endif /* ALTIVEC */
216 reswitch: switch (ch) {
222 + case ',': case ';': case ':': case '_':
225 +#endif /* ALTIVEC */
228 * ``A negative field width argument is taken as a
232 *(cp = buf) = (wchar_t)GETARG(wint_t);
235 + if (flags & VECTOR) {
239 + *(cp = buf) = (wchar_t)btowc(GETARG(int));
243 *(cp = buf) = (wchar_t)btowc(GETARG(int));
244 +#endif /* ALTIVEC */
253 + if (flags & VECTOR) {
257 +#endif /* ALTIVEC */
258 if (flags & INTMAX_SIZE) {
260 if ((intmax_t)ujval < 0) {
261 @@ -825,38 +927,74 @@
266 + if (flags & VECTOR) {
271 +#endif /* ALTIVEC */
274 - xdigs = xdigs_lower;
275 + xdigs0 = xdigs_lower0;
279 - xdigs = xdigs_upper;
280 + xdigs0 = xdigs_upper0;
284 - * XXX We don't actually have a conversion
285 - * XXX routine for this yet.
288 + hexfloat = malloc(hexfloatlen = HEXFLOATSTART);
292 + /* one extra for integer part and another for null */
293 + if (prec > hexfloatlen - 2) {
294 + int hlen = prec + HEXFLOATDELTA;
295 + char *hf = realloc(hexfloat, hlen);
299 + hexfloatlen = hlen;
301 if (flags & LONGDBL) {
302 - fparg.ldbl = (double)GETARG(long double);
304 - __hldtoa(fparg.ldbl, xdigs, prec,
305 - &expt, &signflag, &dtoaend);
306 +#if __TYPE_LONGDOUBLE_IS_DOUBLE
307 + fparg.dbl = (double)GETARG(long double);
308 + prec = __hdtoa(fparg.dbl, xdigs0, prec,
309 + hexfloat, &expt, &signflag, &dtoaend);
310 +#else /* ! __TYPE_LONGDOUBLE_IS_DOUBLE */
311 + fparg.ldbl = GETARG(long double);
312 + prec = __hldtoa(fparg.ldbl, xdigs0, prec,
313 + hexfloat, &expt, &signflag, &dtoaend);
314 +#endif /* __TYPE_LONGDOUBLE_IS_DOUBLE */
316 fparg.dbl = GETARG(double);
318 - __hdtoa(fparg.dbl, xdigs, prec,
319 - &expt, &signflag, &dtoaend);
320 + prec = __hdtoa(fparg.dbl, xdigs0, prec,
321 + hexfloat, &expt, &signflag, &dtoaend);
324 + if (expt == INT_MAX) {
330 + ndig = dtoaend - hexfloat;
334 - cp = convbuf = __mbsconv(dtoaresult, -1);
335 - freedtoa(dtoaresult);
337 + cp = convbuf = __mbsconv(hexfloat, -1);
343 + if (flags & VECTOR) {
348 +#endif /* ALTIVEC */
350 if (prec < 0) /* account for digit before decpt */
352 @@ -865,10 +1003,24 @@
357 + if (flags & VECTOR) {
362 +#endif /* ALTIVEC */
368 + if (flags & VECTOR) {
373 +#endif /* ALTIVEC */
374 expchar = ch - ('g' - 'e');
377 @@ -877,6 +1029,17 @@
381 +#if __TYPE_LONGDOUBLE_IS_DOUBLE
382 + if (flags & LONGDBL)
383 + fparg.ldbl = GETARG(long double);
385 + fparg.dbl = GETARG(double);
387 + dtoa(fparg.dbl, expchar ? 2 : 3, prec,
388 + &expt, &signflag, &dtoaend);
391 +#else /* ! __TYPE_LONGDOUBLE_IS_DOUBLE */
392 if (flags & LONGDBL) {
393 fparg.ldbl = GETARG(long double);
395 @@ -890,9 +1053,13 @@
399 +#endif /* __TYPE_LONGDOUBLE_IS_DOUBLE */
400 ndig = dtoaend - dtoaresult;
401 cp = convbuf = __mbsconv(dtoaresult, -1);
402 freedtoa(dtoaresult);
405 +#endif /* HEXFLOAT */
408 if (expt == INT_MAX) { /* inf or nan */
409 @@ -985,6 +1152,12 @@
414 + if (flags & VECTOR) {
418 +#endif /* ALTIVEC */
419 if (flags & INTMAX_SIZE)
422 @@ -999,6 +1172,12 @@
427 + if (flags & VECTOR) {
431 +#endif /* ALTIVEC */
432 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
435 @@ -1051,6 +1230,12 @@
440 + if (flags & VECTOR) {
444 +#endif /* ALTIVEC */
445 if (flags & INTMAX_SIZE)
448 @@ -1063,6 +1248,12 @@
453 + if (flags & VECTOR) {
457 +#endif /* ALTIVEC */
458 if (flags & INTMAX_SIZE)
461 @@ -1107,6 +1298,14 @@
462 if (size > BUF) /* should never happen */
472 +#endif /* ALTIVEC */
473 default: /* "%?" prints ?, unless ? is NUL */
476 @@ -1118,6 +1317,185 @@
481 + if (flags & VECTOR) {
483 + * Do the minimum amount of work necessary to construct
484 + * a format specifier that can be used to recursively
485 + * call vfprintf() for each element in the vector.
487 + int i, j; /* Counter. */
488 + int vcnt; /* Number of elements in vector. */
489 + char *vfmt; /* Pointer to format specifier. */
490 + char vfmt_buf[32]; /* Static buffer for format spec. */
491 + int vwidth = 0; /* Width specified via '*'. */
492 + int vprec = 0; /* Precision specified via '*'. */
493 + union { /* Element. */
497 + char *vstr; /* Used for asprintf(). */
498 + int vlen; /* Length returned by asprintf(). */
501 + * Set vfmt. If vfmt_buf may not be big enough,
502 + * malloc() space, taking care to free it later.
504 + if (&fmt[-1] - pct < sizeof(vfmt_buf))
507 + vfmt = (char *)malloc(&fmt[-1] - pct + 1);
509 + /* Set the separator character, if not specified. */
517 + /* Create the format specifier. */
518 + for (i = j = 0; i < &fmt[-1] - pct; i++) {
520 + case ',': case ';': case ':': case '_':
521 + case 'v': case 'h': case 'l':
525 + if (pct[i - 1] != '.')
531 + vfmt[j++] = pct[i];
536 + * Determine the number of elements in the vector and
537 + * finish up the format specifier.
539 + if (flags & SHORTINT) {
542 + } else if (flags & LONGINT) {
558 + * The default case should never
575 +/* Get a vector element. */
576 +#define VPRINT(cnt, ind, args...) do { \
577 + if (flags & FPT) { \
578 + velm.f = vval.vfloatarg[ind]; \
579 + vlen = asprintf(&vstr, vfmt , ## args, velm.f); \
583 + /* The default case should never happen. */ \
585 + velm.i = (unsigned)vval.vintarg[ind]; \
588 + velm.i = (unsigned short)vval.vshortarg[ind]; \
591 + velm.i = (unsigned char)vval.vchararg[ind]; \
594 + vlen = asprintf(&vstr, vfmt , ## args, velm.i); \
597 + PRINT(vstr, vlen); \
601 + /* Actually print. */
604 + /* First element. */
606 + for (i = 1; i < vcnt; i++) {
615 + /* First element. */
616 + VPRINT(vcnt, 0, prec);
617 + for (i = 1; i < vcnt; i++) {
623 + VPRINT(vcnt, i, prec);
628 + /* First element. */
629 + VPRINT(vcnt, 0, width);
630 + for (i = 1; i < vcnt; i++) {
636 + VPRINT(vcnt, i, width);
639 + /* First element. */
640 + VPRINT(vcnt, 0, width, prec);
641 + for (i = 1; i < vcnt; i++) {
647 + VPRINT(vcnt, i, width, prec);
653 + if (vfmt != vfmt_buf)
658 +#endif /* ALTIVEC */
660 * All reasonable formats wind up here. At this point, `cp'
661 * points to a string which (if not flags&LADJUST) should be
662 @@ -1135,7 +1513,7 @@
663 realsz = dprec > size ? dprec : size;
670 prsize = width > realsz ? width : realsz;
671 @@ -1149,9 +1527,9 @@
672 PAD(width - realsz, blanks);
678 - } else if (ox[1]) { /* ox[1] is either x, X, or \0 */
679 + if (ox[1]) { /* ox[1] is either x, X, or \0 */
683 @@ -1394,6 +1772,11 @@
688 + if (flags & VECTOR)
691 +#endif /* ALTIVEC */
695 @@ -1413,6 +1796,11 @@
700 + if (flags & VECTOR)
703 +#endif /* ALTIVEC */
705 ADDTYPE(T_LONG_DOUBLE);
707 @@ -1441,9 +1829,19 @@
712 + if (flags & VECTOR)
715 +#endif /* ALTIVEC */
720 + if (flags & VECTOR)
723 +#endif /* ALTIVEC */
727 @@ -1461,6 +1859,11 @@
732 + if (flags & VECTOR)
735 +#endif /* ALTIVEC */
738 default: /* "%?" prints ?, unless ? is NUL */
739 @@ -1546,6 +1949,12 @@
740 (*argtable) [n].longdoublearg = va_arg (ap, long double);
746 + ap = getvec( &((*argtable) [n]), NULL, 0, ap );
748 +#endif /* ALTIVEC */
750 (*argtable) [n].pchararg = va_arg (ap, char *);