1 *** compface/arith.c	Tue Jan 15 23:58:46 2002
2 --- /home/lkoeller/tmp/ports/mail/faces/work/faces/compface/arith.c	Thu Oct 24 03:28:07 1991
3 ***************
4 *** 17,33 ****
5   #include "compface.h"
6 
7   void
8 ! RevPush(Prob *p)
9   {
10 !     if (NumProbs >= PIXELS * 2 - 1) {
11           longjmp(comp_env, ERR_INTERNAL);
12 -     }
13       ProbBuf[NumProbs++] = p;
14   }
15 
16 -
17   void
18 ! BigPush(Prob *p)
19   {
20       static WORD tmp;
21 
22 --- 16,32 ----
23   #include "compface.h"
24 
25   void
26 ! RevPush(p)
27 ! Prob *p;
28   {
29 ! 	if (NumProbs >= PIXELS * 2 - 1)
30   		longjmp(comp_env, ERR_INTERNAL);
31   	ProbBuf[NumProbs++] = p;
32   }
33 
34   void
35 ! BigPush(p)
36 ! Prob *p;
37   {
38   	static WORD tmp;
39 
40 ***************
41 *** 36,76 ****
42       BigAdd(tmp + p->p_offset);
43   }
44 
45 -
46   int
47 ! BigPop(Prob *p)
48   {
49       static WORD tmp;
50 !     int i;
51 
52       BigDiv(0, &tmp);
53       i = 0;
54 !     while ((tmp < p->p_offset) || (tmp >= p->p_range + p->p_offset)) {
55           p++;
56           i++;
57       }
58       BigMul(p->p_range);
59       BigAdd(tmp - p->p_offset);
60 !     return(i);
61   }
62 
63 -
64   #ifdef DEBUG
65   void
66 ! BigPrint()              /* Print a BigInt in HexaDecimal. */
67   {
68 !     int i, c, count;
69 !     WORD *w;
70 
71       count = 0;
72       w = B.b_word + (i = B.b_words);
73 !     while (i--) {
74           w--;
75           c = *((*w >> 4) + HexDigits);
76           putc(c, stderr);
77           c = *((*w & 0xf) + HexDigits);
78           putc(c, stderr);
79 !         if (++count >= 36) {
80               putc('\\', stderr);
81               putc('\n', stderr);
82               count = 0;
83 --- 35,79 ----
84   	BigAdd(tmp + p->p_offset);
85   }
86 
87   int
88 ! BigPop(p)
89 ! register Prob *p;
90   {
91   	static WORD tmp;
92 ! 	register int i;
93 
94   	BigDiv(0, &tmp);
95   	i = 0;
96 ! 	while ((tmp < p->p_offset) || (tmp >= p->p_range + p->p_offset))
97 ! 	{
98   		p++;
99   		i++;
100   	}
101   	BigMul(p->p_range);
102   	BigAdd(tmp - p->p_offset);
103 ! 	return i;
104   }
105 
106   #ifdef DEBUG
107 + /* Print a BigInt in HexaDecimal
108 +  */
109   void
110 ! BigPrint()
111   {
112 ! 	register int i, c, count;
113 ! 	register WORD *w;
114 
115   	count = 0;
116   	w = B.b_word + (i = B.b_words);
117 ! 	while (i--)
118 ! 	{
119   		w--;
120   		c = *((*w >> 4) + HexDigits);
121   		putc(c, stderr);
122   		c = *((*w & 0xf) + HexDigits);
123   		putc(c, stderr);
124 ! 		if (++count >= 36)
125 ! 		{
126   			putc('\\', stderr);
127   			putc('\n', stderr);
128   			count = 0;
129 ***************
130 *** 78,110 ****
131       }
132       putc('\n', stderr);
133   }
134 ! #endif /*DEBUG*/
135 !
136 
137   /* Divide B by a storing the result in B and the remainder in the word
138 !  * pointer to by r.
139    */
140 -
141   void
142 ! BigDiv(WORD a, WORD *r)
143   {
144 !     int i;
145 !     WORD *w;
146 !     COMP c, d;
147 
148       a &= WORDMASK;
149 !     if ((a == 1) || (B.b_words == 0)) {
150           *r = 0;
151           return;
152       }
153 !
154 ! /* Treat this as a == WORDCARRY and just shift everything right a WORD */
155 !
156 !     if (a == 0)    {
157           i = --B.b_words;
158           w = B.b_word;
159           *r = *w;
160 !         while (i--) {
161               *w = *(w + 1);
162               w++;
163           }
164 --- 81,112 ----
165   	}
166   	putc('\n', stderr);
167   }
168 ! #endif
169 
170   /* Divide B by a storing the result in B and the remainder in the word
171 !  * pointer to by r
172    */
173   void
174 ! BigDiv(a, r)
175 ! register WORD a, *r;
176   {
177 ! 	register int i;
178 ! 	register WORD *w;
179 ! 	register COMP c, d;
180 
181   	a &= WORDMASK;
182 ! 	if ((a == 1) || (B.b_words == 0))
183 ! 	{
184   		*r = 0;
185   		return;
186   	}
187 ! 	if (a == 0)	/* treat this as a == WORDCARRY */
188 ! 	{			/* and just shift everything right a WORD */
189   		i = --B.b_words;
190   		w = B.b_word;
191   		*r = *w;
192 ! 		while (i--)
193 ! 		{
194   			*w = *(w + 1);
195   			w++;
196   		}
197 ***************
198 *** 113,154 ****
199       }
200       w = B.b_word + (i = B.b_words);
201       c = 0;
202 !     while (i--) {
203           c <<= BITSPERWORD;
204 !         c += (COMP) *--w;
205 !         d = c / (COMP) a;
206 !         c = c % (COMP) a;
207 !         *w = (WORD) (d & WORDMASK);
208       }
209       *r = c;
210 !     if (B.b_word[B.b_words - 1] == 0) {
211           B.b_words--;
212 -     }
213   }
214 
215 !
216 ! /* Multiply a by B storing the result in B. */
217 !
218   void
219 ! BigMul(WORD a)
220   {
221 !     int i;
222 !     WORD *w;
223 !     COMP c;
224 
225       a &= WORDMASK;
226 !     if ((a == 1) || (B.b_words == 0)) {
227           return;
228 !     }
229 !
230 ! /* Treat this as a == WORDCARRY and just shift everything left a WORD */
231 !
232 !     if (a == 0) {
233 !         if ((i = B.b_words++) >= MAXWORDS - 1) {
234               longjmp(comp_env, ERR_INTERNAL);
235 -         }
236           w = B.b_word + i;
237 !         while (i--) {
238               *w = *(w - 1);
239               w--;
240           }
241 --- 115,153 ----
242   	}
243   	w = B.b_word + (i = B.b_words);
244   	c = 0;
245 ! 	while (i--)
246 ! 	{
247   		c <<= BITSPERWORD;
248 ! 		c += (COMP)*--w;
249 ! 		d = c / (COMP)a;
250 ! 		c = c % (COMP)a;
251 ! 		*w = (WORD)(d & WORDMASK);
252   	}
253   	*r = c;
254 ! 	if (B.b_word[B.b_words - 1] == 0)
255   		B.b_words--;
256   }
257 
258 ! /* Multiply a by B storing the result in B
259 !  */
260   void
261 ! BigMul(a)
262 ! register WORD a;
263   {
264 ! 	register int i;
265 ! 	register WORD *w;
266 ! 	register COMP c;
267 
268   	a &= WORDMASK;
269 ! 	if ((a == 1) || (B.b_words == 0))
270   		return;
271 ! 	if (a == 0)	/* treat this as a == WORDCARRY */
272 ! 	{			/* and just shift everything left a WORD */
273 ! 		if ((i = B.b_words++) >= MAXWORDS - 1)
274   			longjmp(comp_env, ERR_INTERNAL);
275   		w = B.b_word + i;
276 ! 		while (i--)
277 ! 		{
278   			*w = *(w - 1);
279   			w--;
280   		}
281 ***************
282 *** 158,239 ****
283       i = B.b_words;
284       w = B.b_word;
285       c = 0;
286 !     while (i--) {
287           c += (COMP)*w * (COMP)a;
288           *(w++) = (WORD)(c & WORDMASK);
289           c >>= BITSPERWORD;
290       }
291 !     if (c) {
292 !         if (B.b_words++ >= MAXWORDS) {
293               longjmp(comp_env, ERR_INTERNAL);
294 -         }
295           *w = (COMP)(c & WORDMASK);
296       }
297   }
298 
299 !
300 ! /* Subtract a from B storing the result in B. */
301 !
302   void
303 ! BigSub(WORD a)
304   {
305 !     int i;
306 !     WORD *w;
307 !     COMP c;
308 
309       a &= WORDMASK;
310 !     if (a == 0) {
311           return;
312 -     }
313       i = 1;
314       w = B.b_word;
315 !     c = (COMP) *w - (COMP) a;
316 !     *w = (WORD) (c & WORDMASK);
317 !     while (c & WORDCARRY) {
318 !         if (i >= B.b_words) {
319               longjmp(comp_env, ERR_INTERNAL);
320 !         }
321 !         c = (COMP) *++w - 1;
322 !         *w = (WORD) (c & WORDMASK);
323           i++;
324       }
325 !     if ((i == B.b_words) && (*w == 0) && (i > 0)) {
326           B.b_words--;
327 -     }
328   }
329 
330 !
331 ! /* Add to a to B storing the result in B. */
332 !
333   void
334 ! BigAdd(WORD a)
335   {
336 !     int i;
337 !     WORD *w;
338 !     COMP c;
339 
340       a &= WORDMASK;
341 !     if (a == 0) {
342           return;
343 -     }
344       i = 0;
345       w = B.b_word;
346       c = a;
347 !     while ((i < B.b_words) && c) {
348 !         c += (COMP) *w;
349 !         *w++ = (WORD) (c & WORDMASK);
350           c >>= BITSPERWORD;
351           i++;
352       }
353 !     if ((i == B.b_words) && c) {
354 !         if (B.b_words++ >= MAXWORDS) {
355               longjmp(comp_env, ERR_INTERNAL);
356 !         }
357 !         *w = (COMP) (c & WORDMASK);
358       }
359   }
360 
361 -
362   void
363   BigClear()
364   {
365 --- 157,238 ----
366   	i = B.b_words;
367   	w = B.b_word;
368   	c = 0;
369 ! 	while (i--)
370 ! 	{
371   		c += (COMP)*w * (COMP)a;
372   		*(w++) = (WORD)(c & WORDMASK);
373   		c >>= BITSPERWORD;
374   	}
375 ! 	if (c)
376 ! 	{
377 ! 		if (B.b_words++ >= MAXWORDS)
378   			longjmp(comp_env, ERR_INTERNAL);
379   		*w = (COMP)(c & WORDMASK);
380   	}
381   }
382 
383 ! #if 0
384 ! /* Subtract a from B storing the result in B
385 !  */
386   void
387 ! BigSub(a)
388 ! WORD a;
389   {
390 ! 	register int i;
391 ! 	register WORD *w;
392 ! 	register COMP c;
393 
394   	a &= WORDMASK;
395 ! 	if (a == 0)
396   		return;
397   	i = 1;
398   	w = B.b_word;
399 ! 	c = (COMP)*w - (COMP)a;
400 ! 	*w = (WORD)(c & WORDMASK);
401 ! 	while (c & WORDCARRY)
402 ! 	{
403 ! 		if (i >= B.b_words)
404   			longjmp(comp_env, ERR_INTERNAL);
405 ! 		c = (COMP)*++w - 1;
406 ! 		*w = (WORD)(c & WORDMASK);
407   		i++;
408   	}
409 ! 	if ((i == B.b_words) && (*w == 0) && (i > 0))
410   		B.b_words--;
411   }
412 + #endif
413 
414 ! /* Add to a to B storing the result in B
415 !  */
416   void
417 ! BigAdd(a)
418 ! WORD a;
419   {
420 ! 	register int i;
421 ! 	register WORD *w;
422 ! 	register COMP c;
423 
424   	a &= WORDMASK;
425 ! 	if (a == 0)
426   		return;
427   	i = 0;
428   	w = B.b_word;
429   	c = a;
430 ! 	while ((i < B.b_words) && c)
431 ! 	{
432 ! 		c += (COMP)*w;
433 ! 		*w++ = (WORD)(c & WORDMASK);
434   		c >>= BITSPERWORD;
435   		i++;
436   	}
437 ! 	if ((i == B.b_words) && c)
438 ! 	{
439 ! 		if (B.b_words++ >= MAXWORDS)
440   			longjmp(comp_env, ERR_INTERNAL);
441 ! 		*w = (COMP)(c & WORDMASK);
442   	}
443   }
444 
445   void
446   BigClear()
447   {
448