xref: /freebsd/contrib/gdtoa/smisc.c (revision 50dad48bb740a8e56d185d9e8c165e0758f46e25)
1cc36ccd1SDavid Schultz /****************************************************************
2cc36ccd1SDavid Schultz 
3cc36ccd1SDavid Schultz The author of this software is David M. Gay.
4cc36ccd1SDavid Schultz 
5cc36ccd1SDavid Schultz Copyright (C) 1998, 1999 by Lucent Technologies
6cc36ccd1SDavid Schultz All Rights Reserved
7cc36ccd1SDavid Schultz 
8cc36ccd1SDavid Schultz Permission to use, copy, modify, and distribute this software and
9cc36ccd1SDavid Schultz its documentation for any purpose and without fee is hereby
10cc36ccd1SDavid Schultz granted, provided that the above copyright notice appear in all
11cc36ccd1SDavid Schultz copies and that both that the copyright notice and this
12cc36ccd1SDavid Schultz permission notice and warranty disclaimer appear in supporting
13cc36ccd1SDavid Schultz documentation, and that the name of Lucent or any of its entities
14cc36ccd1SDavid Schultz not be used in advertising or publicity pertaining to
15cc36ccd1SDavid Schultz distribution of the software without specific, written prior
16cc36ccd1SDavid Schultz permission.
17cc36ccd1SDavid Schultz 
18cc36ccd1SDavid Schultz LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19cc36ccd1SDavid Schultz INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20cc36ccd1SDavid Schultz IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21cc36ccd1SDavid Schultz SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22cc36ccd1SDavid Schultz WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23cc36ccd1SDavid Schultz IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24cc36ccd1SDavid Schultz ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25cc36ccd1SDavid Schultz THIS SOFTWARE.
26cc36ccd1SDavid Schultz 
27cc36ccd1SDavid Schultz ****************************************************************/
28cc36ccd1SDavid Schultz 
29c88250a5SDavid Schultz /* Please send bug reports to David M. Gay (dmg at acm dot org,
30c88250a5SDavid Schultz  * with " at " changed at "@" and " dot " changed to ".").	*/
31cc36ccd1SDavid Schultz 
32cc36ccd1SDavid Schultz #include "gdtoaimp.h"
33cc36ccd1SDavid Schultz 
34cc36ccd1SDavid Schultz  Bigint *
s2b(s,nd0,nd,y9,dplen)35cc36ccd1SDavid Schultz s2b
36cc36ccd1SDavid Schultz #ifdef KR_headers
374848dd08SDavid Schultz 	(s, nd0, nd, y9, dplen) CONST char *s; int dplen, nd0, nd; ULong y9;
38cc36ccd1SDavid Schultz #else
394848dd08SDavid Schultz 	(CONST char *s, int nd0, int nd, ULong y9, int dplen)
40cc36ccd1SDavid Schultz #endif
41cc36ccd1SDavid Schultz {
42cc36ccd1SDavid Schultz 	Bigint *b;
43cc36ccd1SDavid Schultz 	int i, k;
44cc36ccd1SDavid Schultz 	Long x, y;
45cc36ccd1SDavid Schultz 
46cc36ccd1SDavid Schultz 	x = (nd + 8) / 9;
47cc36ccd1SDavid Schultz 	for(k = 0, y = 1; x > y; y <<= 1, k++) ;
48cc36ccd1SDavid Schultz #ifdef Pack_32
49cc36ccd1SDavid Schultz 	b = Balloc(k);
50cc36ccd1SDavid Schultz 	b->x[0] = y9;
51cc36ccd1SDavid Schultz 	b->wds = 1;
52cc36ccd1SDavid Schultz #else
53cc36ccd1SDavid Schultz 	b = Balloc(k+1);
54cc36ccd1SDavid Schultz 	b->x[0] = y9 & 0xffff;
55cc36ccd1SDavid Schultz 	b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
56cc36ccd1SDavid Schultz #endif
57cc36ccd1SDavid Schultz 
58cc36ccd1SDavid Schultz 	i = 9;
59cc36ccd1SDavid Schultz 	if (9 < nd0) {
60cc36ccd1SDavid Schultz 		s += 9;
61cc36ccd1SDavid Schultz 		do b = multadd(b, 10, *s++ - '0');
62cc36ccd1SDavid Schultz 			while(++i < nd0);
634848dd08SDavid Schultz 		s += dplen;
64cc36ccd1SDavid Schultz 		}
65cc36ccd1SDavid Schultz 	else
664848dd08SDavid Schultz 		s += dplen + 9;
67cc36ccd1SDavid Schultz 	for(; i < nd; i++)
68cc36ccd1SDavid Schultz 		b = multadd(b, 10, *s++ - '0');
69cc36ccd1SDavid Schultz 	return b;
70cc36ccd1SDavid Schultz 	}
71cc36ccd1SDavid Schultz 
72cc36ccd1SDavid Schultz  double
ratio(a,b)73cc36ccd1SDavid Schultz ratio
74cc36ccd1SDavid Schultz #ifdef KR_headers
75cc36ccd1SDavid Schultz 	(a, b) Bigint *a, *b;
76cc36ccd1SDavid Schultz #else
77cc36ccd1SDavid Schultz 	(Bigint *a, Bigint *b)
78cc36ccd1SDavid Schultz #endif
79cc36ccd1SDavid Schultz {
80*50dad48bSDavid Schultz 	U da, db;
81cc36ccd1SDavid Schultz 	int k, ka, kb;
82cc36ccd1SDavid Schultz 
83*50dad48bSDavid Schultz 	dval(&da) = b2d(a, &ka);
84*50dad48bSDavid Schultz 	dval(&db) = b2d(b, &kb);
85cc36ccd1SDavid Schultz 	k = ka - kb + ULbits*(a->wds - b->wds);
86cc36ccd1SDavid Schultz #ifdef IBM
87cc36ccd1SDavid Schultz 	if (k > 0) {
88*50dad48bSDavid Schultz 		word0(&da) += (k >> 2)*Exp_msk1;
89cc36ccd1SDavid Schultz 		if (k &= 3)
90*50dad48bSDavid Schultz 			dval(&da) *= 1 << k;
91cc36ccd1SDavid Schultz 		}
92cc36ccd1SDavid Schultz 	else {
93cc36ccd1SDavid Schultz 		k = -k;
94*50dad48bSDavid Schultz 		word0(&db) += (k >> 2)*Exp_msk1;
95cc36ccd1SDavid Schultz 		if (k &= 3)
96*50dad48bSDavid Schultz 			dval(&db) *= 1 << k;
97cc36ccd1SDavid Schultz 		}
98cc36ccd1SDavid Schultz #else
99cc36ccd1SDavid Schultz 	if (k > 0)
100*50dad48bSDavid Schultz 		word0(&da) += k*Exp_msk1;
101cc36ccd1SDavid Schultz 	else {
102cc36ccd1SDavid Schultz 		k = -k;
103*50dad48bSDavid Schultz 		word0(&db) += k*Exp_msk1;
104cc36ccd1SDavid Schultz 		}
105cc36ccd1SDavid Schultz #endif
106*50dad48bSDavid Schultz 	return dval(&da) / dval(&db);
107cc36ccd1SDavid Schultz 	}
108cc36ccd1SDavid Schultz 
109cc36ccd1SDavid Schultz #ifdef INFNAN_CHECK
110cc36ccd1SDavid Schultz 
111cc36ccd1SDavid Schultz  int
match(sp,t)112cc36ccd1SDavid Schultz match
113cc36ccd1SDavid Schultz #ifdef KR_headers
114cc36ccd1SDavid Schultz 	(sp, t) char **sp, *t;
115cc36ccd1SDavid Schultz #else
116cc36ccd1SDavid Schultz 	(CONST char **sp, char *t)
117cc36ccd1SDavid Schultz #endif
118cc36ccd1SDavid Schultz {
119cc36ccd1SDavid Schultz 	int c, d;
120cc36ccd1SDavid Schultz 	CONST char *s = *sp;
121cc36ccd1SDavid Schultz 
122cc36ccd1SDavid Schultz 	while( (d = *t++) !=0) {
123cc36ccd1SDavid Schultz 		if ((c = *++s) >= 'A' && c <= 'Z')
124cc36ccd1SDavid Schultz 			c += 'a' - 'A';
125cc36ccd1SDavid Schultz 		if (c != d)
126cc36ccd1SDavid Schultz 			return 0;
127cc36ccd1SDavid Schultz 		}
128cc36ccd1SDavid Schultz 	*sp = s + 1;
129cc36ccd1SDavid Schultz 	return 1;
130cc36ccd1SDavid Schultz 	}
131cc36ccd1SDavid Schultz #endif /* INFNAN_CHECK */
132cc36ccd1SDavid Schultz 
133cc36ccd1SDavid Schultz  void
134cc36ccd1SDavid Schultz #ifdef KR_headers
copybits(c,n,b)135cc36ccd1SDavid Schultz copybits(c, n, b) ULong *c; int n; Bigint *b;
136cc36ccd1SDavid Schultz #else
137cc36ccd1SDavid Schultz copybits(ULong *c, int n, Bigint *b)
138cc36ccd1SDavid Schultz #endif
139cc36ccd1SDavid Schultz {
140cc36ccd1SDavid Schultz 	ULong *ce, *x, *xe;
141cc36ccd1SDavid Schultz #ifdef Pack_16
142cc36ccd1SDavid Schultz 	int nw, nw1;
143cc36ccd1SDavid Schultz #endif
144cc36ccd1SDavid Schultz 
145cc36ccd1SDavid Schultz 	ce = c + ((n-1) >> kshift) + 1;
146cc36ccd1SDavid Schultz 	x = b->x;
147cc36ccd1SDavid Schultz #ifdef Pack_32
148cc36ccd1SDavid Schultz 	xe = x + b->wds;
149cc36ccd1SDavid Schultz 	while(x < xe)
150cc36ccd1SDavid Schultz 		*c++ = *x++;
151cc36ccd1SDavid Schultz #else
152cc36ccd1SDavid Schultz 	nw = b->wds;
153cc36ccd1SDavid Schultz 	nw1 = nw & 1;
154cc36ccd1SDavid Schultz 	for(xe = x + (nw - nw1); x < xe; x += 2)
155cc36ccd1SDavid Schultz 		Storeinc(c, x[1], x[0]);
156cc36ccd1SDavid Schultz 	if (nw1)
157cc36ccd1SDavid Schultz 		*c++ = *x;
158cc36ccd1SDavid Schultz #endif
159cc36ccd1SDavid Schultz 	while(c < ce)
160cc36ccd1SDavid Schultz 		*c++ = 0;
161cc36ccd1SDavid Schultz 	}
162cc36ccd1SDavid Schultz 
163cc36ccd1SDavid Schultz  ULong
164cc36ccd1SDavid Schultz #ifdef KR_headers
any_on(b,k)165cc36ccd1SDavid Schultz any_on(b, k) Bigint *b; int k;
166cc36ccd1SDavid Schultz #else
167cc36ccd1SDavid Schultz any_on(Bigint *b, int k)
168cc36ccd1SDavid Schultz #endif
169cc36ccd1SDavid Schultz {
170cc36ccd1SDavid Schultz 	int n, nwds;
171cc36ccd1SDavid Schultz 	ULong *x, *x0, x1, x2;
172cc36ccd1SDavid Schultz 
173cc36ccd1SDavid Schultz 	x = b->x;
174cc36ccd1SDavid Schultz 	nwds = b->wds;
175cc36ccd1SDavid Schultz 	n = k >> kshift;
176cc36ccd1SDavid Schultz 	if (n > nwds)
177cc36ccd1SDavid Schultz 		n = nwds;
178cc36ccd1SDavid Schultz 	else if (n < nwds && (k &= kmask)) {
179cc36ccd1SDavid Schultz 		x1 = x2 = x[n];
180cc36ccd1SDavid Schultz 		x1 >>= k;
181cc36ccd1SDavid Schultz 		x1 <<= k;
182cc36ccd1SDavid Schultz 		if (x1 != x2)
183cc36ccd1SDavid Schultz 			return 1;
184cc36ccd1SDavid Schultz 		}
185cc36ccd1SDavid Schultz 	x0 = x;
186cc36ccd1SDavid Schultz 	x += n;
187cc36ccd1SDavid Schultz 	while(x > x0)
188cc36ccd1SDavid Schultz 		if (*--x)
189cc36ccd1SDavid Schultz 			return 1;
190cc36ccd1SDavid Schultz 	return 0;
191cc36ccd1SDavid Schultz 	}
192