Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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; |
Guido van Rossum | badb116 | 1991-05-14 12:06:16 +0000 | [diff] [blame] | 36 | typedef unsigned int wdigit; /* digit widened to parameter size */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 37 | typedef unsigned long twodigits; |
Guido van Rossum | badb116 | 1991-05-14 12:06:16 +0000 | [diff] [blame] | 38 | typedef long stwodigits; /* signed variant of twodigits */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 39 | |
| 40 | #define SHIFT 15 |
| 41 | #define BASE ((digit)1 << SHIFT) |
| 42 | #define MASK ((int)(BASE - 1)) |
| 43 | |
| 44 | /* Long integer representation. |
| 45 | The absolute value of a number is equal to |
| 46 | SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i) |
| 47 | Negative numbers are represented with ob_size < 0; |
| 48 | zero is represented by ob_size == 0. |
| 49 | In a normalized number, ob_digit[abs(ob_size)-1] (the most significant |
| 50 | digit) is never zero. Also, in all cases, for all valid i, |
| 51 | 0 <= ob_digit[i] <= MASK. |
| 52 | The allocation fuction takes care of allocating extra memory |
| 53 | so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available. */ |
| 54 | |
| 55 | typedef struct { |
| 56 | OB_HEAD |
| 57 | int ob_size; /* XXX Hack! newvarobj() stores it as unsigned! */ |
| 58 | digit ob_digit[1]; |
| 59 | } longobject; |
| 60 | |
| 61 | #define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 62 | |
| 63 | /* Internal use only */ |
| 64 | longobject *alloclongobject PROTO((int)); |
| 65 | longobject *long_normalize PROTO((longobject *)); |
Guido van Rossum | badb116 | 1991-05-14 12:06:16 +0000 | [diff] [blame] | 66 | longobject *mul1 PROTO((longobject *, wdigit)); |
| 67 | longobject *muladd1 PROTO((longobject *, wdigit, wdigit)); |
| 68 | longobject *divrem1 PROTO((longobject *, wdigit, digit *)); |