xref: /minix3/lib/libc/gdtoa/test/ddtest.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_ddfmt, strtoIdd, strtopdd, and strtordd.
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 hex3
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_ddfmt.
50*f14fb602SLionel Sambuc  */
51*f14fb602SLionel Sambuc 
52*f14fb602SLionel Sambuc #include "gdtoaimp.h"
53*f14fb602SLionel Sambuc #include <stdio.h>
54*f14fb602SLionel Sambuc #include <stdlib.h>
55*f14fb602SLionel Sambuc 
56*f14fb602SLionel Sambuc  extern int getround ANSI((int,char*));
57*f14fb602SLionel Sambuc 
58*f14fb602SLionel Sambuc  static char ibuf[2048], obuf[1024];
59*f14fb602SLionel Sambuc 
60*f14fb602SLionel Sambuc #define U (unsigned long)
61*f14fb602SLionel Sambuc 
62*f14fb602SLionel Sambuc  static void
63*f14fb602SLionel Sambuc #ifdef KR_headers
dprint(what,d)64*f14fb602SLionel Sambuc dprint(what, d) char *what; double d;
65*f14fb602SLionel Sambuc #else
66*f14fb602SLionel Sambuc dprint(char *what, double d)
67*f14fb602SLionel Sambuc #endif
68*f14fb602SLionel Sambuc {
69*f14fb602SLionel Sambuc 	char buf[32];
70*f14fb602SLionel Sambuc 	union { double d; ULong L[2]; } u;
71*f14fb602SLionel Sambuc 
72*f14fb602SLionel Sambuc 	u.d = d;
73*f14fb602SLionel Sambuc 	g_dfmt(buf,&d,0,sizeof(buf));
74*f14fb602SLionel Sambuc 	printf("%s = %s = #%lx %lx\n", what, buf, U u.L[_0], U u.L[_1]);
75*f14fb602SLionel Sambuc 	}
76*f14fb602SLionel Sambuc 
77*f14fb602SLionel Sambuc  int
main(Void)78*f14fb602SLionel Sambuc main(Void)
79*f14fb602SLionel Sambuc {
80*f14fb602SLionel Sambuc 	char *s, *s1, *se, *se1;
81*f14fb602SLionel Sambuc 	int dItry, i, j, r = 1, ndig = 0;
82*f14fb602SLionel Sambuc 	double ddI[4];
83*f14fb602SLionel Sambuc 	long LL[4];
84*f14fb602SLionel Sambuc 	union { double dd[2]; ULong L[4]; } u;
85*f14fb602SLionel Sambuc 
86*f14fb602SLionel Sambuc 	while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
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 			LL[0] = u.L[_0];
104*f14fb602SLionel Sambuc 			LL[1] = u.L[_1];
105*f14fb602SLionel Sambuc 			LL[2] = u.L[2+_0];
106*f14fb602SLionel Sambuc 			LL[3] = u.L[2+_1];
107*f14fb602SLionel Sambuc 			sscanf(s+1, "%lx %lx %lx %lx", &LL[0], &LL[1],
108*f14fb602SLionel Sambuc 				&LL[2], &LL[3]);
109*f14fb602SLionel Sambuc 			u.L[_0] = LL[0];
110*f14fb602SLionel Sambuc 			u.L[_1] = LL[1];
111*f14fb602SLionel Sambuc 			u.L[2+_0] = LL[2];
112*f14fb602SLionel Sambuc 			u.L[2+_1] = LL[3];
113*f14fb602SLionel Sambuc 			printf("\nInput: %s", ibuf);
114*f14fb602SLionel Sambuc 			printf(" --> f = #%lx %lx %lx %lx\n",
115*f14fb602SLionel Sambuc 				LL[0],LL[1],LL[2],LL[3]);
116*f14fb602SLionel Sambuc 			goto fmt_test;
117*f14fb602SLionel Sambuc 			}
118*f14fb602SLionel Sambuc 		printf("\nInput: %s", ibuf);
119*f14fb602SLionel Sambuc 		for(s1 = s; *s1 > ' '; s1++){};
120*f14fb602SLionel Sambuc 		while(*s1 <= ' ' && *s1) s1++;
121*f14fb602SLionel Sambuc 		if (!*s1) {
122*f14fb602SLionel Sambuc 			dItry = 1;
123*f14fb602SLionel Sambuc 			i = strtordd(ibuf, &se, r, u.dd);
124*f14fb602SLionel Sambuc 			if (r == 1) {
125*f14fb602SLionel Sambuc 				j = strtopdd(ibuf, &se1, ddI);
126*f14fb602SLionel Sambuc 				if (i != j || u.dd[0] != ddI[0]
127*f14fb602SLionel Sambuc 				 || u.dd[1] != ddI[1] || se != se1)
128*f14fb602SLionel Sambuc 					printf("***strtopdd and strtordd disagree!!\n:");
129*f14fb602SLionel Sambuc 				}
130*f14fb602SLionel Sambuc 			printf("strtopdd consumes %d bytes and returns %d\n",
131*f14fb602SLionel Sambuc 				(int)(se-ibuf), i);
132*f14fb602SLionel Sambuc 			}
133*f14fb602SLionel Sambuc 		else {
134*f14fb602SLionel Sambuc 			u.dd[0] = strtod(s, &se);
135*f14fb602SLionel Sambuc 			u.dd[1] = strtod(se, &se);
136*f14fb602SLionel Sambuc 			}
137*f14fb602SLionel Sambuc  fmt_test:
138*f14fb602SLionel Sambuc 		dprint("dd[0]", u.dd[0]);
139*f14fb602SLionel Sambuc 		dprint("dd[1]", u.dd[1]);
140*f14fb602SLionel Sambuc 		se = g_ddfmt(obuf, u.dd, ndig, sizeof(obuf));
141*f14fb602SLionel Sambuc 		printf("g_ddfmt(%d) gives %d bytes: \"%s\"\n\n",
142*f14fb602SLionel Sambuc 			ndig, (int)(se-obuf), se ? obuf : "<null>");
143*f14fb602SLionel Sambuc 		if (!dItry)
144*f14fb602SLionel Sambuc 			continue;
145*f14fb602SLionel Sambuc 		printf("strtoIdd returns %d,", strtoIdd(ibuf, &se, ddI,&ddI[2]));
146*f14fb602SLionel Sambuc 		printf(" consuming %d bytes.\n", (int)(se-ibuf));
147*f14fb602SLionel Sambuc 		if (ddI[0] == ddI[2] && ddI[1] == ddI[3]) {
148*f14fb602SLionel Sambuc 			if (ddI[0] == u.dd[0] && ddI[1] == u.dd[1])
149*f14fb602SLionel Sambuc 				printf("ddI[0] == ddI[1] == strtopdd\n");
150*f14fb602SLionel Sambuc 			else
151*f14fb602SLionel Sambuc 				printf("ddI[0] == ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %17.g\n",
152*f14fb602SLionel Sambuc 					U ((ULong*)ddI)[_0],
153*f14fb602SLionel Sambuc 					U ((ULong*)ddI)[_1],
154*f14fb602SLionel Sambuc 					U ((ULong*)ddI)[2+_0],
155*f14fb602SLionel Sambuc 					U ((ULong*)ddI)[2+_1],
156*f14fb602SLionel Sambuc 					ddI[0], ddI[1]);
157*f14fb602SLionel Sambuc 			}
158*f14fb602SLionel Sambuc 		else {
159*f14fb602SLionel Sambuc 			printf("ddI[0] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
160*f14fb602SLionel Sambuc 				U ((ULong*)ddI)[_0], U ((ULong*)ddI)[_1],
161*f14fb602SLionel Sambuc 				U ((ULong*)ddI)[2+_0], U ((ULong*)ddI)[2+_1],
162*f14fb602SLionel Sambuc 				ddI[0], ddI[1]);
163*f14fb602SLionel Sambuc 			printf("ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
164*f14fb602SLionel Sambuc 				U ((ULong*)ddI)[4+_0], U ((ULong*)ddI)[4+_1],
165*f14fb602SLionel Sambuc 				U ((ULong*)ddI)[6+_0], U ((ULong*)ddI)[6+_1],
166*f14fb602SLionel Sambuc 				ddI[2], ddI[3]);
167*f14fb602SLionel Sambuc 			if (ddI[0] == u.dd[0] && ddI[1] == u.dd[1])
168*f14fb602SLionel Sambuc 				printf("ddI[0] == strtod\n");
169*f14fb602SLionel Sambuc 			else if (ddI[2] == u.dd[0] && ddI[3] == u.dd[1])
170*f14fb602SLionel Sambuc 				printf("ddI[1] == strtod\n");
171*f14fb602SLionel Sambuc 			else
172*f14fb602SLionel Sambuc 				printf("**** Both differ from strtopdd ****\n");
173*f14fb602SLionel Sambuc 			}
174*f14fb602SLionel Sambuc 		printf("\n");
175*f14fb602SLionel Sambuc 		}
176*f14fb602SLionel Sambuc 	return 0;
177*f14fb602SLionel Sambuc 	}
178