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
|
---|
30 | David M. Gay
|
---|
31 | Bell Laboratories, Room 2C-463
|
---|
32 | 600 Mountain Avenue
|
---|
33 | Murray Hill, NJ 07974-0636
|
---|
34 | U.S.A.
|
---|
35 | dmg@bell-labs.com
|
---|
36 | */
|
---|
37 |
|
---|
38 | /* Test program for g_Qfmt, strtoIQ, strtopQ, and strtorQ.
|
---|
39 | *
|
---|
40 | * Inputs (on stdin):
|
---|
41 | * r rounding_mode
|
---|
42 | * n ndig
|
---|
43 | * number
|
---|
44 | * #hex0 hex1 hex2 hex3
|
---|
45 | *
|
---|
46 | * rounding_mode values:
|
---|
47 | * 0 = toward zero
|
---|
48 | * 1 = nearest
|
---|
49 | * 2 = toward +Infinity
|
---|
50 | * 3 = toward -Infinity
|
---|
51 | *
|
---|
52 | * where number is a decimal floating-point number,
|
---|
53 | * hex0 is a string of <= 8 Hex digits for the most significant
|
---|
54 | * word of the number, hex1 is a similar string for the next
|
---|
55 | * word, etc., and ndig is a parameters to g_Qfmt.
|
---|
56 | */
|
---|
57 |
|
---|
58 | #include "gdtoa.h"
|
---|
59 | #include <stdio.h>
|
---|
60 | #include <stdlib.h>
|
---|
61 | #include <string.h>
|
---|
62 |
|
---|
63 | extern int getround ANSI((int,char*));
|
---|
64 |
|
---|
65 | static char ibuf[2048], obuf[2048];
|
---|
66 | #undef _0
|
---|
67 | #undef _1
|
---|
68 |
|
---|
69 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
|
---|
70 |
|
---|
71 | #ifdef IEEE_MC68k
|
---|
72 | #define _0 0
|
---|
73 | #define _1 1
|
---|
74 | #define _2 2
|
---|
75 | #define _3 3
|
---|
76 | #endif
|
---|
77 | #ifdef IEEE_8087
|
---|
78 | #define _0 3
|
---|
79 | #define _1 2
|
---|
80 | #define _2 1
|
---|
81 | #define _3 0
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | #define U (unsigned long)
|
---|
85 |
|
---|
86 | int
|
---|
87 | main(Void)
|
---|
88 | {
|
---|
89 | char *s, *se, *se1;
|
---|
90 | int i, dItry, ndig = 0, r = 1;
|
---|
91 | union { long double d; ULong bits[4]; } u, v[2];
|
---|
92 |
|
---|
93 | while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
|
---|
94 | while(*s <= ' ')
|
---|
95 | if (!*s++)
|
---|
96 | continue;
|
---|
97 | dItry = 0;
|
---|
98 | switch(*s) {
|
---|
99 | case 'r':
|
---|
100 | r = getround(r, s);
|
---|
101 | continue;
|
---|
102 | case 'n':
|
---|
103 | i = s[1];
|
---|
104 | if (i <= ' ' || i >= '0' && i <= '9') {
|
---|
105 | ndig = atoi(s+1);
|
---|
106 | continue;
|
---|
107 | }
|
---|
108 | break; /* nan? */
|
---|
109 | case '#':
|
---|
110 | sscanf(s+1, "%lx %lx %lx %lx", &u.bits[_0],
|
---|
111 | &u.bits[_1], &u.bits[_2], &u.bits[_3]);
|
---|
112 | printf("\nInput: %s", ibuf);
|
---|
113 | printf(" --> f = #%lx %lx %lx %lx\n", u.bits[_0],
|
---|
114 | u.bits[_1], u.bits[_2], u.bits[_3]);
|
---|
115 | goto fmt_test;
|
---|
116 | }
|
---|
117 | dItry = 1;
|
---|
118 | printf("\nInput: %s", ibuf);
|
---|
119 | i = strtorQ(ibuf, &se, r, u.bits);
|
---|
120 | if (r == 1 && (strtopQ(ibuf,&se1,v[0].bits) != i
|
---|
121 | || se != se1 || memcmp(u.bits, v[0].bits, 16)))
|
---|
122 | printf("***strtoQ and strtorQ disagree!!\n:");
|
---|
123 | printf("\nstrtoQ consumes %d bytes and returns %d\n",
|
---|
124 | (int)(se-ibuf), i);
|
---|
125 | printf("with bits = #%lx %lx %lx %lx\n",
|
---|
126 | U u.bits[_0], U u.bits[_1], U u.bits[_2], U u.bits[_3]);
|
---|
127 | fmt_test:
|
---|
128 | if (sizeof(long double) == 16)
|
---|
129 | printf("printf(\"%%.35Lg\") gives %.35Lg\n", u.d);
|
---|
130 | se = g_Qfmt(obuf, u.bits, ndig, sizeof(obuf));
|
---|
131 | printf("g_Qfmt(%d) gives %d bytes: \"%s\"\n\n", ndig,
|
---|
132 | (int)(se-obuf), se ? obuf : "<null>");
|
---|
133 | if (!dItry)
|
---|
134 | continue;
|
---|
135 | printf("strtoIQ returns %d,",
|
---|
136 | strtoIQ(ibuf, &se, v[0].bits, v[1].bits));
|
---|
137 | printf(" consuming %d bytes.\n", (int)(se-ibuf));
|
---|
138 | if (!memcmp(v[0].bits, v[1].bits, 16)) {
|
---|
139 | if (!memcpy(u.bits, v[0].bits, 16))
|
---|
140 | printf("fI[0] == fI[1] == strtoQ\n");
|
---|
141 | else {
|
---|
142 | printf("fI[0] == fI[1] = #%lx %lx %lx %lx\n",
|
---|
143 | U v[0].bits[_0], U v[0].bits[_1],
|
---|
144 | U v[0].bits[_2], U v[0].bits[_3]);
|
---|
145 | if (sizeof(long double) == 16)
|
---|
146 | printf("= %.35Lg\n", v[0].d);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | else {
|
---|
150 | printf("fI[0] = #%lx %lx %lx %lx\n",
|
---|
151 | U v[0].bits[_0], U v[0].bits[_1],
|
---|
152 | U v[0].bits[_2], U v[0].bits[_3]);
|
---|
153 | if (sizeof(long double) == 16)
|
---|
154 | printf("= %.35Lg\n", v[0].d);
|
---|
155 | printf("fI[1] = #%lx %lx %lx %lx\n",
|
---|
156 | U v[1].bits[_0], U v[1].bits[_1],
|
---|
157 | U v[1].bits[_2], U v[1].bits[_3]);
|
---|
158 | if (sizeof(long double) == 16)
|
---|
159 | printf("= %.35Lg\n", v[1].d);
|
---|
160 | if (!memcmp(v[0].bits, u.bits, 16))
|
---|
161 | printf("fI[0] == strtod\n");
|
---|
162 | else if (!memcmp(v[1].bits, u.bits, 16))
|
---|
163 | printf("fI[1] == strtod\n");
|
---|
164 | else
|
---|
165 | printf("**** Both differ from strtod ****\n");
|
---|
166 | }
|
---|
167 | printf("\n");
|
---|
168 | }
|
---|
169 | return 0;
|
---|
170 | }
|
---|