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