Debian Codegraph @

runtime/gdtoa/test/xLtest.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_xLfmt, strtoIxL, strtopxL, and strtorxL.
33 *
34 * Inputs (on stdin):
35 * r rounding_mode
36 * n ndig
37 * number
38 * #hex0 hex1 hex2
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_xLfmt.
50 */
51
52 #include "gdtoa.h"
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56
57 extern int getround ANSI((int,char*));
58
59 static char ibuf[2048], obuf[2048], obuf1[2048];
60
61 #define U (unsigned long)
62
63 #undef _0
64 #undef _1
65
66 /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
67
68 #ifdef IEEE_MC68k
69 #define _0 0
70 #define _1 1
71 #define _2 2
72 #endif
73 #ifdef IEEE_8087
74 #define _0 2
75 #define _1 1
76 #define _2 0
77 #endif
78
79 int
80 main(Void)
81 {
82 char *s, *s1, *se, *se1;
83 int dItry, i, ndig = 0, nik, nike, r = 1;
84 union { long double d; ULong bits[3]; } u, v[2];
85
86 while((s = fgets(ibuf, sizeof(ibuf), stdin))) {
87 while(*s <= ' ')
88 if (!*s++)
89 continue;
90 dItry = 0;
91 switch(*s) {
92 case 'r':
93 r = getround(r, s);
94 continue;
95 case 'n':
96 i = s[1];
97 if (i <= ' ' || (i >= '0' && i <= '9')) {
98 ndig = atoi(s+1);
99 continue;
100 }
101 break; /* nan? */
102 case '#':
103 /* sscanf(s+1, "%lx %lx %lx", &u.bits[_0], */
104 /* &u.bits[_1], &u.bits[_2]); */
105 u.bits[_0] = (ULong)strtoul(s1 = s+1, &se, 16);
106 if (se > s1) {
107 u.bits[_1] = (ULong)strtoul(s1=se, &se, 16);
108 if (se > s1)
109 u.bits[_2] = (ULong)strtoul(s1=se, &se, 16);
110 }
111 printf("\nInput: %s", ibuf);
112 printf(" --> f = #%lx %lx %lx\n", U u.bits[_0],
113 U u.bits[_1], U u.bits[_2]);
114 i = 0;
115 goto fmt_test;
116 }
117 dItry = 1;
118 printf("\nInput: %s", ibuf);
119 i = strtorxL(ibuf, &se, r, u.bits);
120 if (r == 1 && (i != strtopxL(ibuf, &se1, v[0].bits) || se1 != se
121 || memcmp(u.bits, v[0].bits, 12)))
122 printf("***strtoxL and strtorxL disagree!!\n:");
123 printf("\nstrtoxL consumes %d bytes and returns %d\n",
124 (int)(se-ibuf), i);
125 printf("with bits = #%lx %lx %lx\n",
126 U u.bits[_0], U u.bits[_1], U u.bits[_2]);
127 if (sizeof(long double) == 12)
128 printf("printf(\"%%.21Lg\") gives %.21Lg\n", u.d);
129 fmt_test:
130 se = g_xLfmt(obuf, u.bits, ndig, sizeof(obuf));
131 printf("g_xLfmt(%d) gives %d bytes: \"%s\"\n\n",
132 ndig, (int)(se-obuf), se ? obuf : "<null>");
133 se1 = g_xLfmt_p(obuf1, u.bits, ndig, sizeof(obuf1), 0);
134 if (se1 - obuf1 != se - obuf || strcmp(obuf, obuf1))
135 printf("Botch: g_xLfmt_p gives \"%s\" rather than \"%s\"\n",
136 obuf1, obuf);
137 if (!dItry)
138 continue;
139 printf("strtoIxL returns %d,",
140 strtoIxL(ibuf, &se, v[0].bits, v[1].bits));
141 printf(" consuming %d bytes.\n", (int)(se-ibuf));
142 if (!memcmp(v[0].bits, v[1].bits, 12)) {
143 if (!memcmp(u.bits, v[0].bits, 12))
144 printf("fI[0] == fI[1] == strtoxL\n");
145 else {
146 printf("fI[0] == fI[1] = #%lx %lx %lx\n",
147 U v[0].bits[_0], U v[0].bits[_1],
148 U v[0].bits[_2]);
149 if (sizeof(long double) == 12)
150 printf("= %.21Lg\n", v[0].d);
151 }
152 }
153 else {
154 printf("fI[0] = #%lx %lx %lx\n",
155 U v[0].bits[_0], U v[0].bits[_1],
156 U v[0].bits[_2]);
157 if (sizeof(long double) == 12)
158 printf("= %.21Lg\n", v[0].d);
159 printf("fI[1] = #%lx %lx %lx\n",
160 U v[1].bits[_0], U v[1].bits[_1],
161 U v[1].bits[_2]);
162 if (sizeof(long double) == 12)
163 printf("= %.21Lg\n", v[1].d);
164 if (!memcmp(v[0].bits, u.bits, 12))
165 printf("fI[0] == strtoxL\n");
166 else if (!memcmp(v[1].bits, u.bits, 12))
167 printf("fI[1] == strtoxL\n");
168 else
169 printf("**** Both differ from strtod ****\n");
170 }
171 printf("\n");
172 switch(i & STRTOG_Retmask) {
173 case STRTOG_Infinite:
174 for(nik = 0; nik < 6; ++nik) {
175 se1 = g_xLfmt_p(obuf1, u.bits, ndig, sizeof(obuf1), nik);
176 printf("g_xLfmt_p(...,%d): \"%s\"\n", nik, obuf1);
177 }
178 break;
179 case STRTOG_NaN:
180 case STRTOG_NaNbits:
181 for(i = 0; i < 3; ++i)
182 for(nik = 6*i, nike = nik + 3; nik < nike; ++nik) {
183 se1 = g_xLfmt_p(obuf1, u.bits, ndig, sizeof(obuf1), nik);
184 printf("g_xLfmt_p(...,%d): \"%s\"\n", nik, obuf1);
185 }
186 }
187 }
188 return 0;
189 }