1/****************************************************************
2
3The author of this software is David M. Gay.
4
5Copyright (C) 2009 by David M. Gay
6All Rights Reserved
7
8Permission to use, copy, modify, and distribute this software and
9its documentation for any purpose and without fee is hereby
10granted, provided that the above copyright notice appear in all
11source-code copies and that both that the copyright notice and this
12permission notice and warranty disclaimer appear in supporting
13documentation.
14
15THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21PERFORMANCE OF THIS SOFTWARE.
22
23****************************************************************/
24#include "stdio1.h"
25#include "gdtoa.h"
26#include <string.h>
27
28#undef allow_Quad
29#undef want_Quad
30#undef want_Ux
31#define want_LD
32typedef union Ud {double x; unsigned int u[2]; } Ud;
33#ifdef __x86_64 /*{{*/
34#define want_Ux
35#ifndef NO_GDTOA_i386_Quad /*{*/
36typedef union UQ {__float128 x; unsigned int u[4]; } UQ;
37#define allow_Quad(x) x
38#define want_Quad
39#endif /*}*/
40#else /*}{*/
41#ifdef __i386 /*{{*/
42#define want_Ux
43#else /*}{*/
44#ifdef __sparc /*{{*/
45typedef union UQ {long double x; unsigned int u[4]; } Ux;
46#else /*}{*/
47#ifdef __INTEL_COMPILER /*{*/
48#undef want_Quad
49#undef want_Ux
50#undef want_LD
51#endif /*}*/
52#endif /*}}*/
53#endif /*}}*/
54#endif /*}}*/
55
56#ifndef allow_Quad
57#define allow_Quad(x) /*nothing*/
58#endif
59
60#ifdef want_Ux /*{{*/
61typedef union Ux {long double x; unsigned short u[5]; } Ux;
62#else /*}{*/
63#ifdef __sparc
64#define want_Ux
65#endif
66#endif /*}}*/
67
68 int
69main(void)
70{
71	Ud d;
72	allow_Quad(UQ q;)
73	char *b, buf[256], fmt[32], *s;
74#ifdef want_Ux
75	Ux x;
76	x.x = 0.;
77#endif
78	int k;
79
80	k = 0;
81	strcpy(fmt, "%.g");
82	d.x = 0.;
83	allow_Quad(q.x = 0.;)
84	while(fgets(buf, sizeof(buf), stdin)) {
85		for(b = buf; *b && *b != '\n'; ++b);
86		*b = 0;
87		if (b == buf)
88			continue;
89		b = buf;
90		if (*b == '%') {
91			for(k = 0; *b > ' '; ++b)
92#ifdef want_LD /*{{*/
93				switch(*b) {
94				  case 'L':
95					k = 1;
96#ifdef want_Quad
97					break;
98				  case 'q':
99					if (k >= 1)
100						k = 2;
101#endif
102				  }
103#else /*}{*/
104				;
105#endif /*}}*/
106			if (*b)
107				*b++ = 0;
108			if (b - buf < sizeof(fmt)) {
109				strcpy(fmt, buf);
110				}
111			}
112		if (*b) {
113			switch(k) {
114			  case 0:
115				d.x = strtod(b,&s);
116				break;
117			  case 1:
118#ifdef want_Ux
119#ifdef __sparc
120				strtopQ(b,&s,&x.x);
121#else
122				strtopx(b,&s,&x.x);
123#endif
124#else
125				strtopQ(b,&s,&q.x);
126#endif
127				break;
128			  allow_Quad(case 2: strtopQ(b,&s,&q.x);)
129			  }
130			if (*s)
131				printf("Ignoring \"%s\"\n", s);
132			}
133		switch(k) {
134			case 0:
135				printf("d.x = %.g = #%x %x; %s ==> ", d.x, d.u[1], d.u[0], fmt);
136				printf(fmt, d.x);
137				break;
138			case 1:
139#ifdef __sparc
140				printf("x.x = %.Lg = #%x %x %x %x; %s ==> ", x.x,
141					x.u[0], x.u[1], x.u[2], x.u[3], fmt);
142#else
143				printf("x.x = %.Lg = #%x %x %x %x %x; %s ==> ", x.x,
144					x.u[4], x.u[3], x.u[2], x.u[1], x.u[0], fmt);
145#endif
146				printf(fmt, x.x);
147#ifdef want_Quad
148				break;
149			case 2:
150				printf("q.x = %.Lqg = #%x %x %x %x; %s ==> ", q.x,
151					q.u[3], q.u[2], q.u[1], q.u[0], fmt);
152				printf(fmt, q.x);
153#endif
154			}
155		putchar('\n');
156		}
157	return 0;
158	}
159