1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998-2001 by Lucent Technologies
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
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
27 ****************************************************************/
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
32 /* Test program for g_Qfmt, strtoIQ, strtopQ, and strtorQ.
38 * #hex0 hex1 hex2 hex3
40 * rounding_mode values:
43 * 2 = toward +Infinity
44 * 3 = toward -Infinity
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_Qfmt.
57 extern int getround
ANSI((int,char*));
59 static char ibuf
[2048], obuf
[2048];
63 /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */
78 #define U (unsigned long)
83 char *s
, *s1
, *se
, *se1
;
84 int i
, dItry
, ndig
= 0, r
= 1;
85 union { long double d
; ULong bits
[4]; } u
, v
[2];
87 while( (s
= fgets(ibuf
, sizeof(ibuf
), stdin
)) !=0) {
98 if (i
<= ' ' || i
>= '0' && i
<= '9') {
104 /* sscanf(s+1, "%lx %lx %lx %lx", &u.bits[_0], */
105 /* &u.bits[_1], &u.bits[_2], &u.bits[_3]); */
106 u
.bits
[_0
] = (ULong
)strtoul(s1
= s
+1, &se
, 16);
108 u
.bits
[_1
] = (ULong
)strtoul(s1
= se
, &se
, 16);
110 u
.bits
[_2
] = (ULong
)strtoul(s1
= se
, &se
, 16);
112 u
.bits
[_3
] = (ULong
)strtoul(s1
= se
, &se
, 16);
115 printf("\nInput: %s", ibuf
);
116 printf(" --> f = #%lx %lx %lx %lx\n", u
.bits
[_0
],
117 u
.bits
[_1
], u
.bits
[_2
], u
.bits
[_3
]);
121 printf("\nInput: %s", ibuf
);
122 i
= strtorQ(ibuf
, &se
, r
, u
.bits
);
123 if (r
== 1 && (strtopQ(ibuf
,&se1
,v
[0].bits
) != i
124 || se
!= se1
|| memcmp(u
.bits
, v
[0].bits
, 16)))
125 printf("***strtoQ and strtorQ disagree!!\n:");
126 printf("\nstrtoQ consumes %d bytes and returns %d\n",
128 printf("with bits = #%lx %lx %lx %lx\n",
129 U u
.bits
[_0
], U u
.bits
[_1
], U u
.bits
[_2
], U u
.bits
[_3
]);
131 if (sizeof(long double) == 16)
132 printf("printf(\"%%.35Lg\") gives %.35Lg\n", u
.d
);
133 se
= g_Qfmt(obuf
, u
.bits
, ndig
, sizeof(obuf
));
134 printf("g_Qfmt(%d) gives %d bytes: \"%s\"\n\n", ndig
,
135 (int)(se
-obuf
), se
? obuf
: "<null>");
138 printf("strtoIQ returns %d,",
139 strtoIQ(ibuf
, &se
, v
[0].bits
, v
[1].bits
));
140 printf(" consuming %d bytes.\n", (int)(se
-ibuf
));
141 if (!memcmp(v
[0].bits
, v
[1].bits
, 16)) {
142 if (!memcpy(u
.bits
, v
[0].bits
, 16))
143 printf("fI[0] == fI[1] == strtoQ\n");
145 printf("fI[0] == fI[1] = #%lx %lx %lx %lx\n",
146 U v
[0].bits
[_0
], U v
[0].bits
[_1
],
147 U v
[0].bits
[_2
], U v
[0].bits
[_3
]);
148 if (sizeof(long double) == 16)
149 printf("= %.35Lg\n", v
[0].d
);
153 printf("fI[0] = #%lx %lx %lx %lx\n",
154 U v
[0].bits
[_0
], U v
[0].bits
[_1
],
155 U v
[0].bits
[_2
], U v
[0].bits
[_3
]);
156 if (sizeof(long double) == 16)
157 printf("= %.35Lg\n", v
[0].d
);
158 printf("fI[1] = #%lx %lx %lx %lx\n",
159 U v
[1].bits
[_0
], U v
[1].bits
[_1
],
160 U v
[1].bits
[_2
], U v
[1].bits
[_3
]);
161 if (sizeof(long double) == 16)
162 printf("= %.35Lg\n", v
[1].d
);
163 if (!memcmp(v
[0].bits
, u
.bits
, 16))
164 printf("fI[0] == strtod\n");
165 else if (!memcmp(v
[1].bits
, u
.bits
, 16))
166 printf("fI[1] == strtod\n");
168 printf("**** Both differ from strtod ****\n");