1*c5e820caSchristos /* $NetBSD: smisc.c,v 1.5 2012/03/13 21:13:34 christos Exp $ */
27684d5e0Skleink
37684d5e0Skleink /****************************************************************
47684d5e0Skleink
57684d5e0Skleink The author of this software is David M. Gay.
67684d5e0Skleink
77684d5e0Skleink Copyright (C) 1998, 1999 by Lucent Technologies
87684d5e0Skleink All Rights Reserved
97684d5e0Skleink
107684d5e0Skleink Permission to use, copy, modify, and distribute this software and
117684d5e0Skleink its documentation for any purpose and without fee is hereby
127684d5e0Skleink granted, provided that the above copyright notice appear in all
137684d5e0Skleink copies and that both that the copyright notice and this
147684d5e0Skleink permission notice and warranty disclaimer appear in supporting
157684d5e0Skleink documentation, and that the name of Lucent or any of its entities
167684d5e0Skleink not be used in advertising or publicity pertaining to
177684d5e0Skleink distribution of the software without specific, written prior
187684d5e0Skleink permission.
197684d5e0Skleink
207684d5e0Skleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
217684d5e0Skleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
227684d5e0Skleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
237684d5e0Skleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
247684d5e0Skleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
257684d5e0Skleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
267684d5e0Skleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
277684d5e0Skleink THIS SOFTWARE.
287684d5e0Skleink
297684d5e0Skleink ****************************************************************/
307684d5e0Skleink
317684d5e0Skleink /* Please send bug reports to David M. Gay (dmg at acm dot org,
327684d5e0Skleink * with " at " changed at "@" and " dot " changed to "."). */
337684d5e0Skleink
347684d5e0Skleink #include "gdtoaimp.h"
357684d5e0Skleink
367684d5e0Skleink Bigint *
s2b(s,nd0,nd,y9,dplen)377684d5e0Skleink s2b
387684d5e0Skleink #ifdef KR_headers
3961e56760Schristos (s, nd0, nd, y9, dplen) CONST char *s; int dplen, nd0, nd; ULong y9;
407684d5e0Skleink #else
41*c5e820caSchristos (CONST char *s, int nd0, int nd, ULong y9, size_t dplen)
427684d5e0Skleink #endif
437684d5e0Skleink {
447684d5e0Skleink Bigint *b;
457684d5e0Skleink int i, k;
467684d5e0Skleink Long x, y;
477684d5e0Skleink
487684d5e0Skleink x = (nd + 8) / 9;
497684d5e0Skleink for(k = 0, y = 1; x > y; y <<= 1, k++) ;
507684d5e0Skleink #ifdef Pack_32
517684d5e0Skleink b = Balloc(k);
52ab625449Schristos if (b == NULL)
53ab625449Schristos return NULL;
547684d5e0Skleink b->x[0] = y9;
557684d5e0Skleink b->wds = 1;
567684d5e0Skleink #else
577684d5e0Skleink b = Balloc(k+1);
58ab625449Schristos if (b == NULL)
59ab625449Schristos return NULL;
607684d5e0Skleink b->x[0] = y9 & 0xffff;
617684d5e0Skleink b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
627684d5e0Skleink #endif
637684d5e0Skleink
647684d5e0Skleink i = 9;
657684d5e0Skleink if (9 < nd0) {
667684d5e0Skleink s += 9;
67ab625449Schristos do {
68ab625449Schristos b = multadd(b, 10, *s++ - '0');
69ab625449Schristos if (b == NULL)
70ab625449Schristos return NULL;
71ab625449Schristos } while(++i < nd0);
7261e56760Schristos s += dplen;
737684d5e0Skleink }
747684d5e0Skleink else
7561e56760Schristos s += dplen + 9;
76ab625449Schristos for(; i < nd; i++) {
777684d5e0Skleink b = multadd(b, 10, *s++ - '0');
78ab625449Schristos if (b == NULL)
79ab625449Schristos return NULL;
80ab625449Schristos }
817684d5e0Skleink return b;
827684d5e0Skleink }
837684d5e0Skleink double
ratio(a,b)847684d5e0Skleink ratio
857684d5e0Skleink #ifdef KR_headers
867684d5e0Skleink (a, b) Bigint *a, *b;
877684d5e0Skleink #else
887684d5e0Skleink (Bigint *a, Bigint *b)
897684d5e0Skleink #endif
907684d5e0Skleink {
9161e56760Schristos U da, db;
927684d5e0Skleink int k, ka, kb;
937684d5e0Skleink
9461e56760Schristos dval(&da) = b2d(a, &ka);
9561e56760Schristos dval(&db) = b2d(b, &kb);
967684d5e0Skleink k = ka - kb + ULbits*(a->wds - b->wds);
977684d5e0Skleink #ifdef IBM
987684d5e0Skleink if (k > 0) {
9961e56760Schristos word0(&da) += (k >> 2)*Exp_msk1;
1007684d5e0Skleink if (k &= 3)
10161e56760Schristos dval(&da) *= 1 << k;
1027684d5e0Skleink }
1037684d5e0Skleink else {
1047684d5e0Skleink k = -k;
10561e56760Schristos word0(&db) += (k >> 2)*Exp_msk1;
1067684d5e0Skleink if (k &= 3)
10761e56760Schristos dval(&db) *= 1 << k;
1087684d5e0Skleink }
1097684d5e0Skleink #else
1107684d5e0Skleink if (k > 0)
11161e56760Schristos word0(&da) += k*Exp_msk1;
1127684d5e0Skleink else {
1137684d5e0Skleink k = -k;
11461e56760Schristos word0(&db) += k*Exp_msk1;
1157684d5e0Skleink }
1167684d5e0Skleink #endif
11761e56760Schristos return dval(&da) / dval(&db);
1187684d5e0Skleink }
1197684d5e0Skleink
1207684d5e0Skleink #ifdef INFNAN_CHECK
1217684d5e0Skleink
1227684d5e0Skleink int
match(sp,t)1237684d5e0Skleink match
1247684d5e0Skleink #ifdef KR_headers
125ac898a26Skleink (sp, t) CONST char **sp, *t;
1267684d5e0Skleink #else
127ac898a26Skleink (CONST char **sp, CONST char *t)
1287684d5e0Skleink #endif
1297684d5e0Skleink {
1307684d5e0Skleink int c, d;
1317684d5e0Skleink CONST char *s = *sp;
1327684d5e0Skleink
1337684d5e0Skleink while( (d = *t++) !=0) {
1347684d5e0Skleink if ((c = *++s) >= 'A' && c <= 'Z')
1357684d5e0Skleink c += 'a' - 'A';
1367684d5e0Skleink if (c != d)
1377684d5e0Skleink return 0;
1387684d5e0Skleink }
1397684d5e0Skleink *sp = s + 1;
1407684d5e0Skleink return 1;
1417684d5e0Skleink }
1427684d5e0Skleink #endif /* INFNAN_CHECK */
1437684d5e0Skleink
1447684d5e0Skleink void
1457684d5e0Skleink #ifdef KR_headers
copybits(c,n,b)1467684d5e0Skleink copybits(c, n, b) ULong *c; int n; Bigint *b;
1477684d5e0Skleink #else
1487684d5e0Skleink copybits(ULong *c, int n, Bigint *b)
1497684d5e0Skleink #endif
1507684d5e0Skleink {
1517684d5e0Skleink ULong *ce, *x, *xe;
1527684d5e0Skleink #ifdef Pack_16
1537684d5e0Skleink int nw, nw1;
1547684d5e0Skleink #endif
1557684d5e0Skleink
156ac898a26Skleink ce = c + ((unsigned int)(n-1) >> kshift) + 1;
1577684d5e0Skleink x = b->x;
1587684d5e0Skleink #ifdef Pack_32
1597684d5e0Skleink xe = x + b->wds;
1607684d5e0Skleink while(x < xe)
1617684d5e0Skleink *c++ = *x++;
1627684d5e0Skleink #else
1637684d5e0Skleink nw = b->wds;
1647684d5e0Skleink nw1 = nw & 1;
1657684d5e0Skleink for(xe = x + (nw - nw1); x < xe; x += 2)
1667684d5e0Skleink Storeinc(c, x[1], x[0]);
1677684d5e0Skleink if (nw1)
1687684d5e0Skleink *c++ = *x;
1697684d5e0Skleink #endif
1707684d5e0Skleink while(c < ce)
1717684d5e0Skleink *c++ = 0;
1727684d5e0Skleink }
1737684d5e0Skleink
1747684d5e0Skleink ULong
1757684d5e0Skleink #ifdef KR_headers
any_on(b,k)1767684d5e0Skleink any_on(b, k) Bigint *b; int k;
1777684d5e0Skleink #else
1787684d5e0Skleink any_on(Bigint *b, int k)
1797684d5e0Skleink #endif
1807684d5e0Skleink {
1817684d5e0Skleink int n, nwds;
1827684d5e0Skleink ULong *x, *x0, x1, x2;
1837684d5e0Skleink
1847684d5e0Skleink x = b->x;
1857684d5e0Skleink nwds = b->wds;
186ac898a26Skleink n = (unsigned int)k >> kshift;
1877684d5e0Skleink if (n > nwds)
1887684d5e0Skleink n = nwds;
1897684d5e0Skleink else if (n < nwds && (k &= kmask)) {
1907684d5e0Skleink x1 = x2 = x[n];
1917684d5e0Skleink x1 >>= k;
1927684d5e0Skleink x1 <<= k;
1937684d5e0Skleink if (x1 != x2)
1947684d5e0Skleink return 1;
1957684d5e0Skleink }
1967684d5e0Skleink x0 = x;
1977684d5e0Skleink x += n;
1987684d5e0Skleink while(x > x0)
1997684d5e0Skleink if (*--x)
2007684d5e0Skleink return 1;
2017684d5e0Skleink return 0;
2027684d5e0Skleink }
203