|
| 1 | +/*********************************************************** |
| 2 | +Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | +Netherlands. |
| 4 | +
|
| 5 | + All Rights Reserved |
| 6 | +
|
| 7 | +Permission 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 names of Stichting Mathematisch |
| 12 | +Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | +distribution of the software without specific, written prior permission. |
| 14 | +
|
| 15 | +STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | +THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | +FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | +FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | +
|
| 23 | +******************************************************************/ |
| 24 | + |
| 25 | +/* Parameters of the long integer representation. |
| 26 | + These shouldn't have to be changed as C should guarantee that a short |
| 27 | + contains at least 16 bits, but it's made changeable any way. |
| 28 | + Note: 'digit' should be able to hold 2*MASK+1, and 'twodigits' |
| 29 | + should be able to hold the intermediate results in 'mul' |
| 30 | + (at most MASK << SHIFT). |
| 31 | + Also, x_sub assumes that 'digit' is an unsigned type, and overflow |
| 32 | + is handled by taking the result mod 2**N for some N > SHIFT. |
| 33 | + And, at some places it is assumed that MASK fits in an int, as well. */ |
| 34 | + |
| 35 | +typedef unsigned short digit; |
| 36 | +typedef unsigned long twodigits; |
| 37 | + |
| 38 | +#define SHIFT 15 |
| 39 | +#define BASE ((digit)1 << SHIFT) |
| 40 | +#define MASK ((int)(BASE - 1)) |
| 41 | + |
| 42 | +/* Long integer representation. |
| 43 | + The absolute value of a number is equal to |
| 44 | + SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i) |
| 45 | + Negative numbers are represented with ob_size < 0; |
| 46 | + zero is represented by ob_size == 0. |
| 47 | + In a normalized number, ob_digit[abs(ob_size)-1] (the most significant |
| 48 | + digit) is never zero. Also, in all cases, for all valid i, |
| 49 | + 0 <= ob_digit[i] <= MASK. |
| 50 | + The allocation fuction takes care of allocating extra memory |
| 51 | + so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available. */ |
| 52 | + |
| 53 | +typedef struct { |
| 54 | + OB_HEAD |
| 55 | + int ob_size; /* XXX Hack! newvarobj() stores it as unsigned! */ |
| 56 | + digit ob_digit[1]; |
| 57 | +} longobject; |
| 58 | + |
| 59 | +#define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 60 | + |
| 61 | +/* Internal use only */ |
| 62 | +longobject *alloclongobject PROTO((int)); |
| 63 | +longobject *long_normalize PROTO((longobject *)); |
| 64 | +longobject *mul1 PROTO((longobject *, digit)); |
| 65 | +longobject *muladd1 PROTO((longobject *, digit, digit)); |
| 66 | +longobject *divrem1 PROTO((longobject *, digit, digit *)); |
| 67 | + |
| 68 | +/* Check for interrupts during operations on long ints >= this size */ |
| 69 | +#define INTRLIMIT 20 |
0 commit comments