Debian Codegraph @

runtime/gdtoa/test/ddtest.c

raw symbols SCIP
1 /****************************************************************
2
3 The author of this software is David M. Gay.
4
5 Copyright (C) 1998-2001 by Lucent Technologies
6 All Rights Reserved
7
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
16 permission.
17
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 THIS SOFTWARE.
26
27 ****************************************************************/
28
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
31
32 /* Test program for g_ddfmt, strtoIdd, strtopdd, and strtordd.
33 *
34 * Inputs (on stdin):
35 * r rounding_mode
36 * n ndig
37 * number
38 * #hex0 hex1 hex2 hex3
39 *
40 * rounding_mode values:
41 * 0 = toward zero
42 * 1 = nearest
43 * 2 = toward +Infinity
44 * 3 = toward -Infinity
45 *
46 * where number is a decimal floating-point number,
47 * hex0 is a string of <= 8 Hex digits for the most significant
48 * word of the number, hex1 is a similar string for the next
49 * word, etc., and ndig is a parameters to g_ddfmt.
50 */
51
52 #include "gdtoaimp.h"
53 #include <stdio.h>
54 #include <stdlib.h>
55
56 extern int getround ANSI((int,char*));
57
58 static char ibuf[2048], obuf[1024], obuf1[2048];
59
60 #define UL (unsigned long)
61
62 static void
63 #ifdef KR_headers
64 dprint(what, d) char *what; double d;
65 #else
66 dprint(char *what, double d)
67 #endif
68 {
69 U u;
70 char buf[32];
71
72 u.d = d;
73 g_dfmt(buf,&d,0,sizeof(buf));
74 printf("%s = %s = #%lx %lx\n", what, buf, UL u.L[_0], UL u.L[_1]);
75 }
76
77 int
78 main(Void)
79 {
80 U ddI[4], u[2];
81 char *s, *s1, *se, *se1;
82 int dItry, i, j, nik, nike, r = 1, ndig = 0;
83 long LL[4];
84
85 while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
86 while(*s <= ' ')
87 if (!*s++)
88 continue;
89 dItry = 0;
90 switch(*s) {
91 case 'r':
92 r = getround(r, s);
93 continue;
94 case 'n':
95 i = s[1];
96 if (i <= ' ' || (i >= '0' && i <= '9')) {
97 ndig = atoi(s+1);
98 continue;
99 }
100 break; /* nan? */
101 case '#':
102 LL[0] = u[0].L[_0];
103 LL[1] = u[0].L[_1];
104 LL[2] = u[1].L[_0];
105 LL[3] = u[1].L[_1];
106 sscanf(s+1, "%lx %lx %lx %lx", &LL[0], &LL[1],
107 &LL[2], &LL[3]);
108 u[0].L[_0] = LL[0];
109 u[0].L[_1] = LL[1];
110 u[1].L[_0] = LL[2];
111 u[1].L[_1] = LL[3];
112 printf("\nInput: %s", ibuf);
113 printf(" --> f = #%lx %lx %lx %lx\n",
114 LL[0],LL[1],LL[2],LL[3]);
115 i = 0;
116 goto fmt_test;
117 }
118 printf("\nInput: %s", ibuf);
119 for(s1 = s; *s1 > ' '; s1++){};
120 while(*s1 <= ' ' && *s1) s1++;
121 if (!*s1) {
122 dItry = 1;
123 i = strtordd(ibuf, &se, r, &u[0].d);
124 if (r == 1) {
125 j = strtopdd(ibuf, &se1, &ddI[0].d);
126 if (i != j || u[0].d != ddI[0].d
127 || u[1].d != ddI[1].d || se != se1)
128 printf("***strtopdd and strtordd disagree!!\n:");
129 }
130 printf("strtopdd consumes %d bytes and returns %d\n",
131 (int)(se-ibuf), i);
132 }
133 else {
134 u[0].d = strtod(s, &se);
135 u[1].d = strtod(se, &se);
136 }
137 fmt_test:
138 dprint("dd[0]", u[0].d);
139 dprint("dd[1]", u[1].d);
140 se = g_ddfmt(obuf, &u[0].d, ndig, sizeof(obuf));
141 printf("g_ddfmt(%d) gives %d bytes: \"%s\"\n\n",
142 ndig, (int)(se-obuf), se ? obuf : "<null>");
143 se1 = g_ddfmt_p(obuf1, &u[0].d, ndig, sizeof(obuf1), 0);
144 if (se1 - obuf1 != se - obuf || strcmp(obuf, obuf1))
145 printf("Botch: g_ddfmt_p gives \"%s\" rather than \"%s\"\n",
146 obuf1, obuf);
147 if (!dItry)
148 continue;
149 printf("strtoIdd returns %d,", strtoIdd(ibuf, &se, &ddI[0].d, &ddI[2].d));
150 printf(" consuming %d bytes.\n", (int)(se-ibuf));
151 if (ddI[0].d == ddI[2].d && ddI[1].d == ddI[3].d) {
152 if (ddI[0].d == u[0].d && ddI[1].d == u[1].d)
153 printf("ddI[0] == ddI[1] == strtopdd\n");
154 else
155 printf("ddI[0] == ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %17.g\n",
156 UL ddI[0].L[_0],
157 UL ddI[0].L[_1],
158 UL ddI[1].L[_0],
159 UL ddI[1].L[_1],
160 ddI[0].d, ddI[1].d);
161 }
162 else {
163 printf("ddI[0] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
164 UL ddI[0].L[_0], UL ddI[0].L[_1],
165 UL ddI[1].L[_0],UL ddI[1].L[_1],
166 ddI[0].d, ddI[1].d);
167 printf("ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
168 UL ddI[2].L[_0], UL ddI[2].L[_1],
169 UL ddI[3].L[_0],UL ddI[3].L[_1],
170 ddI[2].d, ddI[3].d);
171 if (ddI[0].d == u[0].d && ddI[1].d == u[1].d)
172 printf("ddI[0] == strtod\n");
173 else if (ddI[2].d == u[0].d && ddI[3].d == u[1].d)
174 printf("ddI[1] == strtod\n");
175 else
176 printf("**** Both differ from strtopdd ****\n");
177 }
178 switch(i & STRTOG_Retmask) {
179 case STRTOG_Infinite:
180 for(nik = 0; nik < 6; ++nik) {
181 se1 = g_ddfmt_p(obuf1, &u[0].d, ndig, sizeof(obuf1), nik);
182 printf("g_ddfmt_p(...,%d): \"%s\"\n", nik, obuf1);
183 }
184 break;
185 case STRTOG_NaN:
186 case STRTOG_NaNbits:
187 for(i = 0; i < 3; ++i)
188 for(nik = 6*i, nike = nik + 3; nik < nike; ++nik) {
189 se1 = g_ddfmt_p(obuf1, &u[0].d, ndig, sizeof(obuf1), nik);
190 printf("g_ddfmt_p(...,%d): \"%s\"\n", nik, obuf1);
191 }
192 }
193 printf("\n");
194 }
195 return 0;
196 }