blob: 4184f8a4449f216fc686e2dd2260a24483651ace [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF 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
35typedef unsigned short digit;
36typedef 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
53typedef 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 */
62longobject *alloclongobject PROTO((int));
63longobject *long_normalize PROTO((longobject *));
64longobject *mul1 PROTO((longobject *, digit));
65longobject *muladd1 PROTO((longobject *, digit, digit));
66longobject *divrem1 PROTO((longobject *, digit, digit *));
67
68/* Check for interrupts during operations on long ints >= this size */
69#define INTRLIMIT 20