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