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