Debian Codegraph @

texk/ps2pk/arith.c

raw
1 /* $XConsortium: arith.c,v 1.2 91/10/10 11:14:06 rws Exp $ */
2 /* Copyright International Business Machines, Corp. 1991
3 * All Rights Reserved
4 * Copyright Lexmark International, Inc. 1991
5 * All Rights Reserved
6 *
7 * License to use, copy, modify, and distribute this software and its
8 * documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appear in all copies and that
10 * both that copyright notice and this permission notice appear in
11 * supporting documentation, and that the name of IBM or Lexmark not be
12 * used in advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission.
14 *
15 * IBM AND LEXMARK PROVIDE THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES OF
16 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO ANY
17 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
18 * AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE
19 * QUALITY AND PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
20 * OR MAINTAIN, BELONGS TO THE LICENSEE. SHOULD ANY PORTION OF THE
21 * SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM OR LEXMARK) ASSUMES THE
22 * ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION. IN NO EVENT SHALL
23 * IBM OR LEXMARK BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
24 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
25 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
26 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27 * THIS SOFTWARE.
28 */
29 /* ARITH CWEB V0006 ******** */
30 /*
31 :h1.ARITH Module - Portable Module for Multiple Precision Fixed Point Arithmetic
32
33 This module provides division and multiplication of 64-bit fixed point
34 numbers. (To be more precise, the module works on numbers that take
35 two 'longs' to store. That is almost always equivalent to saying 64-bit
36 numbers.)
37
38 Note: it is frequently easy and desirable to recode these functions in
39 assembly language for the particular processor being used, because
40 assembly language, unlike C, will have 64-bit multiply products and
41 64-bit dividends. This module is offered as a portable version.
42
43 &author. Jeffrey B. Lotspiech ([email protected]) and Sten F. Andler
44
45
46 :h3.Include Files
47
48 The included files are:
49 */
50
51 #include "types.h"
52 #include "objects.h"
53 #include "spaces.h"
54 #include "arith.h"
55
56 /*
57 :h3.
58 */
59 /*SHARED LINE(S) ORIGINATED HERE*/
60 /*
61 Reference for all algorithms: Donald E. Knuth, "The Art of Computer
62 Programming, Volume 2, Semi-Numerical Algorithms," Addison-Wesley Co.,
63 Massachusetts, 1969, pp. 229-279.
64
65 Knuth talks about a 'digit' being an arbitrary sized unit and a number
66 being a sequence of digits. We'll take a digit to be a 'short'.
67 The following assumption must be valid for these algorithms to work:
68 :ol.
69 :li.A 'long' is two 'short's.
70 :eol.
71 The following code is INDEPENDENT of:
72 :ol.
73 :li.The actual size of a short.
74 :li.Whether shorts and longs are stored most significant byte
75 first or least significant byte first.
76 :eol.
77
78 SHORTSIZE is the number of bits in a short; INT32SIZE is the number of
79 bits in an int32_t; MAXSHORT is the maximum unsigned short:
80 */
81 /*SHARED LINE(S) ORIGINATED HERE*/
82 /*
83 ASSEMBLE concatenates two shorts to form a long:
84 */
85 #define ASSEMBLE(hi,lo) ((((uint32_t)hi)<<SHORTSIZE)+(lo))
86 /*
87 HIGHDIGIT extracts the most significant short from a long; LOWDIGIT
88 extracts the least significant short from a long:
89 */
90 #define HIGHDIGIT(u) ((u)>>SHORTSIZE)
91 #define LOWDIGIT(u) ((u)&MAXSHORT)
92
93 /*
94 SIGNBITON tests the high order bit of a long 'w':
95 */
96 #define SIGNBITON(w) (((int32_t)w)<0)
97
98 /*SHARED LINE(S) ORIGINATED HERE*/
99
100 /*
101 :h2.Double Long Arithmetic
102
103 :h3.DLmult() - Multiply Two Longs to Yield a Double Long
104
105 The two multiplicands must be positive.
106 */
107
108 void DLmult(register doublelong *product, register uint32_t u, register uint32_t v)
109 {
110 register uint32_t u1, u2; /* the digits of u */
111 register uint32_t v1, v2; /* the digits of v */
112 register unsigned int w1, w2, w3, w4; /* the digits of w */
113 register uint32_t t; /* temporary variable */
114 /* printf("DLmult(? ?, %x, %x)\n", u, v); */
115 u1 = HIGHDIGIT(u);
116 u2 = LOWDIGIT(u);
117 v1 = HIGHDIGIT(v);
118 v2 = LOWDIGIT(v);
119
120 if (v2 == 0) w4 = w3 = w2 = 0;
121 else
122 {
123 t = u2 * v2;
124 w4 = LOWDIGIT(t);
125 t = u1 * v2 + HIGHDIGIT(t);
126 w3 = LOWDIGIT(t);
127 w2 = HIGHDIGIT(t);
128 }
129
130 if (v1 == 0) w1 = 0;
131 else
132 {
133 t = u2 * v1 + w3;
134 w3 = LOWDIGIT(t);
135 t = u1 * v1 + w2 + HIGHDIGIT(t);
136 w2 = LOWDIGIT(t);
137 w1 = HIGHDIGIT(t);
138 }
139
140 product->high = ASSEMBLE(w1, w2);
141 product->low = ASSEMBLE(w3, w4);
142 }
143
144 /*
145 :h2.DLdiv() - Divide Two Longs by One Long, Yielding Two Longs
146
147 Both the dividend and the divisor must be positive.
148 */
149
150 void DLdiv(
151 doublelong *quotient, /* also where dividend is, originally */
152 uint32_t divisor)
153 {
154 register uint32_t u1u2 = quotient->high;
155 register uint32_t u3u4 = quotient->low;
156 register int32_t u3; /* single digit of dividend */
157 register int v1,v2; /* divisor in registers */
158 register int32_t t; /* signed copy of u1u2 */
159 register int qhat; /* guess at the quotient digit */
160 register uint32_t q3q4; /* low two digits of quotient */
161 register int shift; /* holds the shift value for normalizing */
162 register int j; /* loop variable */
163
164 /* printf("DLdiv(%x %x, %x)\n", quotient->high, quotient->low, divisor); */
165 /*
166 * Knuth's algorithm works if the dividend is smaller than the
167 * divisor. We can get to that state quickly:
168 */
169 if (u1u2 >= divisor) {
170 quotient->high = u1u2 / divisor;
171 u1u2 %= divisor;
172 }
173 else
174 quotient->high = 0;
175
176 if (divisor <= MAXSHORT) {
177
178 /*
179 * This is the case where the divisor is contained in one
180 * 'short'. It is worthwhile making this fast:
181 */
182 u1u2 = ASSEMBLE(u1u2, HIGHDIGIT(u3u4));
183 q3q4 = u1u2 / divisor;
184 u1u2 %= divisor;
185 u1u2 = ASSEMBLE(u1u2, LOWDIGIT(u3u4));
186 quotient->low = ASSEMBLE(q3q4, u1u2 / divisor);
187 return;
188 }
189
190
191 /*
192 * At this point the divisor is a true 'long' so we must use
193 * Knuth's algorithm.
194 *
195 * Step D1: Normalize divisor and dividend (this makes our 'qhat'
196 * guesses more accurate):
197 */
198 for (shift=0; !SIGNBITON(divisor); shift++, divisor <<= 1) { ; }
199 shift--;
200 divisor >>= 1;
201
202 if ((u1u2 >> (INT32SIZE - shift)) != 0 && shift != 0)
203 t1_abort("DLdiv: dividend too large");
204 u1u2 = (u1u2 << shift) + ((shift == 0) ? 0 : u3u4 >> (INT32SIZE - shift));
205 u3u4 <<= shift;
206
207 /*
208 * Step D2: Begin Loop through digits, dividing u1,u2,u3 by v1,v2,
209 * then shifting U left by 1 digit:
210 */
211 v1 = HIGHDIGIT(divisor);
212 v2 = LOWDIGIT(divisor);
213 q3q4 = 0;
214 u3 = HIGHDIGIT(u3u4);
215
216 for (j=0; j < 2; j++) {
217
218 /*
219 * Step D3: make a guess (qhat) at the next quotient denominator:
220 */
221 qhat = (HIGHDIGIT(u1u2) == v1) ? MAXSHORT : u1u2 / v1;
222 /*
223 * At this point Knuth would have us further refine our
224 * guess, since we know qhat is too big if
225 *
226 * v2 * qhat > ASSEMBLE(u1u2 % v, u3)
227 *
228 * That would make sense if u1u2 % v was easy to find, as it
229 * would be in assembly language. I ignore this step, and
230 * repeat step D6 if qhat is too big.
231 */
232
233 /*
234 * Step D4: Multiply v1,v2 times qhat and subtract it from
235 * u1,u2,u3:
236 */
237 u3 -= qhat * v2;
238 /*
239 * The high digit of u3 now contains the "borrow" for the
240 * rest of the substraction from u1,u2.
241 * Sometimes we can lose the sign bit with the above.
242 * If so, we have to force the high digit negative:
243 */
244 t = HIGHDIGIT(u3);
245 if (t > 0)
246 t |= -1 << SHORTSIZE;
247 t += u1u2 - qhat * v1;
248 /* printf("..>divide step qhat=%x t=%x u3=%x u1u2=%x v1=%x v2=%x\n",
249 qhat, t, u3, u1u2, v1, v2); */
250 while (t < 0) { /* Test is Step D5. */
251
252 /*
253 * D6: Oops, qhat was too big. Add back in v1,v2 and
254 * decrease qhat by 1:
255 */
256 u3 = LOWDIGIT(u3) + v2;
257 t += HIGHDIGIT(u3) + v1;
258 qhat--;
259 /* printf("..>>qhat correction t=%x u3=%x qhat=%x\n", t, u3, qhat); */
260 }
261 /*
262 * Step D7: shift U left one digit and loop:
263 */
264 u1u2 = t;
265 if (HIGHDIGIT(u1u2) != 0)
266 t1_abort("divide algorithm error");
267 u1u2 = ASSEMBLE(u1u2, LOWDIGIT(u3));
268 u3 = LOWDIGIT(u3u4);
269 q3q4 = ASSEMBLE(q3q4, qhat);
270 }
271 quotient->low = q3q4;
272 /* printf("DLdiv returns %x %x\n", quotient->high, quotient->low); */
273 return;
274 }
275
276 /*
277 :h3.DLadd() - Add Two Double Longs
278
279 In this case, the doublelongs may be signed. The algorithm takes the
280 piecewise sum of the high and low longs, with the possibility that the
281 high should be incremented if there is a carry out of the low. How to
282 tell if there is a carry? Alex Harbury suggested that if the sum of
283 the lows is less than the max of the lows, there must have been a
284 carry. Conversely, if there was a carry, the sum of the lows must be
285 less than the max of the lows. So, the test is "if and only if".
286 */
287
288 void DLadd(doublelong *u, doublelong *v) /* u = u + v */
289 {
290 register uint32_t lowmax = MAX(u->low, v->low);
291
292 /* printf("DLadd(%x %x, %x %x)\n", u->high, u->low, v->high, v->low); */
293 u->high += v->high;
294 u->low += v->low;
295 if (lowmax > u->low)
296 u->high++;
297 }
298 /*
299 :h3.DLsub() - Subtract Two Double Longs
300
301 Testing for a borrow is even easier. If the v.low is greater than
302 u.low, there must be a borrow.
303 */
304
305 void DLsub(doublelong *u, doublelong *v) /* u = u - v */
306 {
307 /* printf("DLsub(%x %x, %x %x)\n", u->high, u->low, v->high, v->low);*/
308 u->high -= v->high;
309 if (v->low > u->low)
310 u->high--;
311 u->low -= v->low;
312 }
313 /*
314 :h3.DLrightshift() - Macro to Shift Double Long Right by N
315 */
316
317 /*SHARED LINE(S) ORIGINATED HERE*/
318
319 /*
320 :h2.Fractional Pel Arithmetic
321 */
322 /*
323 :h3.FPmult() - Multiply Two Fractional Pel Values
324
325 This funtion first calculates w = u * v to "doublelong" precision.
326 It then shifts w right by FRACTBITS bits, and checks that no
327 overflow will occur when the resulting value is passed back as
328 a fractpel.
329 */
330
331 fractpel FPmult(register fractpel u, register fractpel v)
332 {
333 doublelong w;
334 register int negative = FALSE; /* sign flag */
335
336 if ((u == 0) || (v == 0)) return (0);
337
338
339 if (u < 0) {u = -u; negative = TRUE;}
340 if (v < 0) {v = -v; negative = !negative;}
341
342 if (u == TOFRACTPEL(1)) return ((negative) ? -v : v);
343 if (v == TOFRACTPEL(1)) return ((negative) ? -u : u);
344
345 DLmult(&w, u, v);
346 DLrightshift(w, FRACTBITS);
347 if (w.high != 0 || SIGNBITON(w.low)) {
348 IfTrace2(TRUE,"FPmult: overflow, %dx%d\n", u, v);
349 w.low = TOFRACTPEL(MAXSHORT);
350 }
351
352 return ((negative) ? -w.low : w.low);
353 }
354
355 /*
356 :h3.FPdiv() - Divide Two Fractional Pel Values
357
358 These values may be signed. The function returns the quotient.
359 */
360
361 fractpel FPdiv(register fractpel dividend, register fractpel divisor)
362 {
363 doublelong w; /* result will be built here */
364 int negative = FALSE; /* flag for sign bit */
365
366 if (dividend < 0) {
367 dividend = -dividend;
368 negative = TRUE;
369 }
370 if (divisor < 0) {
371 divisor = -divisor;
372 negative = !negative;
373 }
374 w.low = dividend << FRACTBITS;
375 w.high = dividend >> (INT32SIZE - FRACTBITS);
376 DLdiv(&w, divisor);
377 if (w.high != 0 || SIGNBITON(w.low)) {
378 IfTrace2(TRUE,"FPdiv: overflow, %d/%d\n", dividend, divisor);
379 w.low = TOFRACTPEL(MAXSHORT);
380 }
381 return( (negative) ? -w.low : w.low);
382 }
383
384 /*
385 :h3.FPstarslash() - Multiply then Divide
386
387 Borrowing a chapter from the language Forth, it is useful to define
388 an operator that first multiplies by one constant then divides by
389 another, keeping the intermediate result in extended precision.
390 */
391
392 fractpel FPstarslash(register fractpel a, register fractpel b,
393 register fractpel c) /* result = a * b / c */
394 {
395 doublelong w; /* result will be built here */
396 int negative = FALSE;
397
398 if (a < 0) { a = -a; negative = TRUE; }
399 if (b < 0) { b = -b; negative = !negative; }
400 if (c < 0) { c = -c; negative = !negative; }
401
402 DLmult(&w, a, b);
403 DLdiv(&w, c);
404 if (w.high != 0 || SIGNBITON(w.low)) {
405 IfTrace3(TRUE,"FPstarslash: overflow, %d*%d/%d\n", a, b, c);
406 w.low = TOFRACTPEL(MAXSHORT);
407 }
408 return((negative) ? -w.low : w.low);
409 }