xref: /titanic_50/usr/src/lib/libmp/common/mout.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate /* 	Portions Copyright(c) 1988, Sun Microsystems Inc.	*/
11*7c478bd9Sstevel@tonic-gate /*	All Rights Reserved					*/
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate /*
14*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Microsystems, Inc.
15*7c478bd9Sstevel@tonic-gate  * All rights reserved.
16*7c478bd9Sstevel@tonic-gate  */
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate #include <stdio.h>
23*7c478bd9Sstevel@tonic-gate #include <mp.h>
24*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
25*7c478bd9Sstevel@tonic-gate #include "libmp.h"
26*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate static int
m_in(MINT * a,short b,FILE * f)29*7c478bd9Sstevel@tonic-gate m_in(MINT *a, short b, FILE *f)
30*7c478bd9Sstevel@tonic-gate {
31*7c478bd9Sstevel@tonic-gate 	MINT x, y, ten;
32*7c478bd9Sstevel@tonic-gate 	int sign, c;
33*7c478bd9Sstevel@tonic-gate 	short qten, qy;
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate 	_mp_xfree(a);
36*7c478bd9Sstevel@tonic-gate 	sign = 1;
37*7c478bd9Sstevel@tonic-gate 	ten.len = 1;
38*7c478bd9Sstevel@tonic-gate 	ten.val = &qten;
39*7c478bd9Sstevel@tonic-gate 	qten = b;
40*7c478bd9Sstevel@tonic-gate 	x.len = 0;
41*7c478bd9Sstevel@tonic-gate 	y.len = 1;
42*7c478bd9Sstevel@tonic-gate 	y.val = &qy;
43*7c478bd9Sstevel@tonic-gate 	while ((c = getc(f)) != EOF)
44*7c478bd9Sstevel@tonic-gate 	switch (c) {
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	case '\\':
47*7c478bd9Sstevel@tonic-gate 		(void) getc(f);
48*7c478bd9Sstevel@tonic-gate 		continue;
49*7c478bd9Sstevel@tonic-gate 	case '\t':
50*7c478bd9Sstevel@tonic-gate 	case '\n':
51*7c478bd9Sstevel@tonic-gate 		a->len *= sign;
52*7c478bd9Sstevel@tonic-gate 		_mp_xfree(&x);
53*7c478bd9Sstevel@tonic-gate 		return (0);
54*7c478bd9Sstevel@tonic-gate 	case ' ':
55*7c478bd9Sstevel@tonic-gate 		continue;
56*7c478bd9Sstevel@tonic-gate 	case '-':
57*7c478bd9Sstevel@tonic-gate 		sign = -sign;
58*7c478bd9Sstevel@tonic-gate 		continue;
59*7c478bd9Sstevel@tonic-gate 	default:
60*7c478bd9Sstevel@tonic-gate 		if (c >= '0' && c <= '9') {
61*7c478bd9Sstevel@tonic-gate 			qy = c - '0';
62*7c478bd9Sstevel@tonic-gate 			mp_mult(&x, &ten, a);
63*7c478bd9Sstevel@tonic-gate 			mp_madd(a, &y, a);
64*7c478bd9Sstevel@tonic-gate 			_mp_move(a, &x);
65*7c478bd9Sstevel@tonic-gate 			continue;
66*7c478bd9Sstevel@tonic-gate 		} else {
67*7c478bd9Sstevel@tonic-gate 			(void) ungetc(c, stdin);
68*7c478bd9Sstevel@tonic-gate 			a->len *= sign;
69*7c478bd9Sstevel@tonic-gate 			return (0);
70*7c478bd9Sstevel@tonic-gate 		}
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 	return (EOF);
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate static void
m_out(MINT * a,short b,FILE * f)76*7c478bd9Sstevel@tonic-gate m_out(MINT *a, short b, FILE *f)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	int sign, xlen, i;
79*7c478bd9Sstevel@tonic-gate 	short r;
80*7c478bd9Sstevel@tonic-gate 	MINT x;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	char *obuf;
83*7c478bd9Sstevel@tonic-gate 	char *bp;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if (a == NULL)
86*7c478bd9Sstevel@tonic-gate 		return;
87*7c478bd9Sstevel@tonic-gate 	sign = 1;
88*7c478bd9Sstevel@tonic-gate 	xlen = a->len;
89*7c478bd9Sstevel@tonic-gate 	if (xlen < 0) {
90*7c478bd9Sstevel@tonic-gate 		xlen = -xlen;
91*7c478bd9Sstevel@tonic-gate 		sign = -1;
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 	if (xlen == 0) {
94*7c478bd9Sstevel@tonic-gate 		(void) fprintf(f, "0\n");
95*7c478bd9Sstevel@tonic-gate 		return;
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 	x.len = xlen;
98*7c478bd9Sstevel@tonic-gate 	x.val = _mp_xalloc(xlen, "m_out");
99*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < xlen; i++)
100*7c478bd9Sstevel@tonic-gate 		x.val[i] = a->val[i];
101*7c478bd9Sstevel@tonic-gate 	obuf = malloc(7 * (size_t)xlen);
102*7c478bd9Sstevel@tonic-gate 	bp = obuf + 7 * xlen - 1;
103*7c478bd9Sstevel@tonic-gate 	*bp-- = 0;
104*7c478bd9Sstevel@tonic-gate 	while (x.len > 0) {
105*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 10 && x.len > 0; i++) {
106*7c478bd9Sstevel@tonic-gate 			mp_sdiv(&x, b, &x, &r);
107*7c478bd9Sstevel@tonic-gate 			*bp-- = (char)(r + '0');
108*7c478bd9Sstevel@tonic-gate 		}
109*7c478bd9Sstevel@tonic-gate 		if (x.len > 0)
110*7c478bd9Sstevel@tonic-gate 			*bp-- = ' ';
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 	if (sign == -1)
113*7c478bd9Sstevel@tonic-gate 		*bp-- = '-';
114*7c478bd9Sstevel@tonic-gate 	(void) fprintf(f, "%s\n", bp + 1);
115*7c478bd9Sstevel@tonic-gate 	free(obuf);
116*7c478bd9Sstevel@tonic-gate 	_mp_xfree(&x);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate static void s_div(MINT *, short, MINT *, short *);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate void
mp_sdiv(MINT * a,short n,MINT * q,short * r)122*7c478bd9Sstevel@tonic-gate mp_sdiv(MINT *a, short n, MINT *q, short *r)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate 	MINT x, y;
125*7c478bd9Sstevel@tonic-gate 	short sign;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	sign = 1;
128*7c478bd9Sstevel@tonic-gate 	x.len = a->len;
129*7c478bd9Sstevel@tonic-gate 	x.val = a->val;
130*7c478bd9Sstevel@tonic-gate 	if (n < 0) {
131*7c478bd9Sstevel@tonic-gate 		sign = -sign;
132*7c478bd9Sstevel@tonic-gate 		n = -n;
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 	if (x.len < 0) {
135*7c478bd9Sstevel@tonic-gate 		sign = -sign;
136*7c478bd9Sstevel@tonic-gate 		x.len = -x.len;
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 	s_div(&x, n, &y, r);
139*7c478bd9Sstevel@tonic-gate 	_mp_xfree(q);
140*7c478bd9Sstevel@tonic-gate 	q->val = y.val;
141*7c478bd9Sstevel@tonic-gate 	q->len = sign * y.len;
142*7c478bd9Sstevel@tonic-gate 	*r = *r * sign;
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate static void
s_div(MINT * a,short n,MINT * q,short * r)146*7c478bd9Sstevel@tonic-gate s_div(MINT *a, short n, MINT *q, short *r)
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate 	int qlen;
149*7c478bd9Sstevel@tonic-gate 	int i;
150*7c478bd9Sstevel@tonic-gate 	int x;
151*7c478bd9Sstevel@tonic-gate 	short *qval;
152*7c478bd9Sstevel@tonic-gate 	short *aval;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	x = 0;
155*7c478bd9Sstevel@tonic-gate 	qlen = a->len;
156*7c478bd9Sstevel@tonic-gate 	q->val = _mp_xalloc(qlen, "s_div");
157*7c478bd9Sstevel@tonic-gate 	aval = a->val + qlen;
158*7c478bd9Sstevel@tonic-gate 	qval = q->val + qlen;
159*7c478bd9Sstevel@tonic-gate 	for (i = qlen - 1; i >= 0; i--) {
160*7c478bd9Sstevel@tonic-gate 		x = x * 0100000 + *--aval;
161*7c478bd9Sstevel@tonic-gate 		*--qval = (short)(x / n);
162*7c478bd9Sstevel@tonic-gate 		x = x % n;
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 	*r = (short)x;
165*7c478bd9Sstevel@tonic-gate 	if (qlen && q->val[qlen-1] == 0)
166*7c478bd9Sstevel@tonic-gate 		qlen--;
167*7c478bd9Sstevel@tonic-gate 	q->len = qlen;
168*7c478bd9Sstevel@tonic-gate 	if (qlen == 0)
169*7c478bd9Sstevel@tonic-gate 		free(q->val);
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate int
mp_min(MINT * a)173*7c478bd9Sstevel@tonic-gate mp_min(MINT *a)
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	return (m_in(a, 10, stdin));
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate int
mp_omin(MINT * a)179*7c478bd9Sstevel@tonic-gate mp_omin(MINT *a)
180*7c478bd9Sstevel@tonic-gate {
181*7c478bd9Sstevel@tonic-gate 	return (m_in(a, 8, stdin));
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate void
mp_mout(MINT * a)185*7c478bd9Sstevel@tonic-gate mp_mout(MINT *a)
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	m_out(a, 10, stdout);
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate void
mp_omout(MINT * a)191*7c478bd9Sstevel@tonic-gate mp_omout(MINT *a)
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate 	m_out(a, 8, stdout);
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate void
mp_fmout(MINT * a,FILE * f)197*7c478bd9Sstevel@tonic-gate mp_fmout(MINT *a, FILE *f)
198*7c478bd9Sstevel@tonic-gate {
199*7c478bd9Sstevel@tonic-gate 	m_out(a, 10, f);
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate int
mp_fmin(MINT * a,FILE * f)203*7c478bd9Sstevel@tonic-gate mp_fmin(MINT *a, FILE *f)
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate 	return (m_in(a, 10, f));
206*7c478bd9Sstevel@tonic-gate }
207