1 --- vfwprintf.c.orig Sun May 30 01:25:05 2004
2 +++ vfwprintf.c Sun May 30 01:26:53 2004
8 #include "un-namespace.h"
10 #include "libc_private.h"
15 +#include <machine/cpu_capabilities.h>
17 +#define VECTORTYPE vector unsigned char
28 + VECTORTYPE vectorarg;
29 + unsigned char vuchararg[16];
30 + signed char vchararg[16];
31 + unsigned short vushortarg[8];
32 + signed short vshortarg[8];
33 + unsigned int vuintarg[4];
34 + signed int vintarg[4];
41 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
42 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET,
43 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
45 + T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR, T_VECTOR
46 +#else /* ! ALTIVEC */
47 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
51 static int __sbprintf(FILE *, const wchar_t *, va_list);
53 static void __find_arguments(const wchar_t *, va_list, union arg **);
54 static void __grow_type_table(int, enum typeid **, int *);
57 + * Get the argument indexed by nextarg. If the argument table is
58 + * built, use it to get the argument. If its not, get the next
59 + * argument (and arguments must be gotten sequentially).
61 +#define GETARG(type) \
62 + ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
63 + (nextarg++, va_arg(ap, type)))
66 +#define hasAltivec (_cpu_capabilities & kHasAltivec)
67 +/*-----------------------------------------------------------------------
68 + * getvec() must be a real subroutine. If it is a #define, then __vfprintf()
69 + * would have its calling sequence changed by Altivec so that a non-Altivec
70 + * processor would crash on illegal instruction. By isolating the calling
71 + * sequence in getvec(), __vprintf() is callable by a non-Altivec processor.
72 + *-----------------------------------------------------------------------*/
74 +getvec(union arg *dst, const union arg *argtable, int nextarg, va_list ap)
76 + dst->vectorarg = GETARG(VECTORTYPE);
80 +#define SETVEC(dst) \
82 + ap = getvec(&dst, argtable, nextarg, ap); \
88 * Helper function for `fprintf to unbuffered unix file': creates a
89 * temporary buffer. We only work on write-only files; this avoids
91 #define PTRDIFFT 0x800 /* ptrdiff_t */
92 #define INTMAXT 0x1000 /* intmax_t */
93 #define CHARINT 0x2000 /* print char using int format */
95 +#define VECTOR 0x4000 /* Altivec vector */
101 int nseps; /* number of group separators with ' */
102 int nrepeats; /* number of repeats of the last group */
105 + union arg vval; /* Vector argument. */
106 + wchar_t *pct; /* Pointer to '%' at beginning of specifier. */
107 + wchar_t vsep; /* Vector separator character. */
109 u_long ulval; /* integer arguments %[diouxX] */
110 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
111 int base; /* base for [diouxX] conversion */
116 - * Get the argument indexed by nextarg. If the argument table is
117 - * built, use it to get the argument. If its not, get the next
118 - * argument (and arguments must be gotten sequentially).
120 -#define GETARG(type) \
121 - ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
122 - (nextarg++, va_arg(ap, type)))
125 * To extend shorts properly, we need both signed and unsigned
126 * argument extraction methods.
129 val = GETARG (int); \
133 thousands_sep = '\0';
135 #ifndef NO_FLOATING_POINT
139 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
141 + if (cantwrite(fp)) {
146 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
147 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
154 +#endif /* ALTIVEC */
155 fmt++; /* skip over '%' */
163 + vsep = 'X'; /* Illegal value, changed to defaults later. */
164 +#endif /* ALTIVEC */
167 reswitch: switch (ch) {
173 + case ',': case ';': case ':': case '_':
176 +#endif /* ALTIVEC */
179 * ``A negative field width argument is taken as a
185 + if (flags & VECTOR) {
189 +#endif /* ALTIVEC */
191 *(cp = buf) = (wchar_t)GETARG(wint_t);
198 + if (flags & VECTOR) {
202 +#endif /* ALTIVEC */
203 if (flags & INTMAX_SIZE) {
205 if ((intmax_t)ujval < 0) {
206 @@ -868,10 +943,24 @@
211 + if (flags & VECTOR) {
216 +#endif /* ALTIVEC */
222 + if (flags & VECTOR) {
227 +#endif /* ALTIVEC */
228 expchar = ch - ('g' - 'e');
231 @@ -989,6 +1078,12 @@
236 + if (flags & VECTOR) {
240 +#endif /* ALTIVEC */
241 if (flags & INTMAX_SIZE)
244 @@ -1003,6 +1098,12 @@
249 + if (flags & VECTOR) {
253 +#endif /* ALTIVEC */
254 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
257 @@ -1055,6 +1156,12 @@
262 + if (flags & VECTOR) {
266 +#endif /* ALTIVEC */
267 if (flags & INTMAX_SIZE)
270 @@ -1067,6 +1174,12 @@
275 + if (flags & VECTOR) {
279 +#endif /* ALTIVEC */
280 if (flags & INTMAX_SIZE)
283 @@ -1111,6 +1224,14 @@
284 if (size > BUF) /* should never happen */
294 +#endif /* ALTIVEC */
295 default: /* "%?" prints ?, unless ? is NUL */
298 @@ -1122,6 +1243,183 @@
303 + if (flags & VECTOR) {
305 + * Do the minimum amount of work necessary to construct
306 + * a format specifier that can be used to recursively
307 + * call vfprintf() for each element in the vector.
309 + int i, j; /* Counter. */
310 + int vcnt; /* Number of elements in vector. */
311 + char *vfmt; /* Pointer to format specifier. */
312 + char vfmt_buf[32]; /* Static buffer for format spec. */
313 + int vwidth = 0; /* Width specified via '*'. */
314 + int vprec = 0; /* Precision specified via '*'. */
315 + union { /* Element. */
319 + char *vstr; /* Used for asprintf(). */
320 + int vlen; /* Length returned by asprintf(). */
323 + * Set vfmt. If vfmt_buf may not be big enough,
324 + * malloc() space, taking care to free it later.
326 + if (&fmt[-1] - pct < sizeof(vfmt_buf))
329 + vfmt = (char *)malloc(&fmt[-1] - pct + 1);
331 + /* Set the separator character, if not specified. */
339 + /* Create the format specifier. */
340 + for (i = j = 0; i < &fmt[-1] - pct; i++) {
342 + case ',': case ';': case ':': case '_':
343 + case 'v': case 'h': case 'l':
347 + if (pct[i - 1] != '.')
353 + vfmt[j++] = pct[i];
358 + * Determine the number of elements in the vector and
359 + * finish up the format specifier.
361 + if (flags & SHORTINT) {
365 + } else if (flags & LONGINT) {
382 + * The default case should never
399 +/* Get a vector element. */
400 +#define VPRINT(cnt, ind, args...) do { \
401 + if (flags & FPT) { \
402 + velm.f = vval.vfloatarg[ind]; \
403 + vlen = asprintf(&vstr, vfmt , ## args, velm.f); \
407 + /* The default case should never happen. */ \
409 + velm.i = (unsigned)vval.vintarg[ind]; \
412 + velm.i = (unsigned short)vval.vshortarg[ind]; \
415 + velm.i = (unsigned char)vval.vchararg[ind]; \
418 + vlen = asprintf(&vstr, vfmt , ## args, velm.i); \
421 + PRINT(vstr, vlen); \
425 + /* Actually print. */
428 + /* First element. */
430 + for (i = 1; i < vcnt; i++) {
438 + /* First element. */
439 + VPRINT(vcnt, 0, prec);
440 + for (i = 1; i < vcnt; i++) {
445 + VPRINT(vcnt, i, prec);
450 + /* First element. */
451 + VPRINT(vcnt, 0, width);
452 + for (i = 1; i < vcnt; i++) {
457 + VPRINT(vcnt, i, width);
460 + /* First element. */
461 + VPRINT(vcnt, 0, width, prec);
462 + for (i = 1; i < vcnt; i++) {
467 + VPRINT(vcnt, i, width, prec);
473 + if (vfmt != vfmt_buf)
478 +#endif /* ALTIVEC */
480 * All reasonable formats wind up here. At this point, `cp'
481 * points to a string which (if not flags&LADJUST) should be
482 @@ -1400,6 +1698,11 @@
487 + if (flags & VECTOR)
490 +#endif /* ALTIVEC */
494 @@ -1417,6 +1720,11 @@
499 + if (flags & VECTOR)
502 +#endif /* ALTIVEC */
504 ADDTYPE(T_LONG_DOUBLE);
506 @@ -1445,9 +1753,19 @@
511 + if (flags & VECTOR)
514 +#endif /* ALTIVEC */
519 + if (flags & VECTOR)
522 +#endif /* ALTIVEC */
526 @@ -1465,6 +1783,11 @@
531 + if (flags & VECTOR)
534 +#endif /* ALTIVEC */
537 default: /* "%?" prints ?, unless ? is NUL */
538 @@ -1550,6 +1873,12 @@
539 (*argtable) [n].longdoublearg = va_arg (ap, long double);
545 + ap = getvec( &((*argtable) [n]), NULL, 0, ap );
547 +#endif /* ALTIVEC */
549 (*argtable) [n].pchararg = va_arg (ap, char *);