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