annotate Objects/longobject.c @ 3720:b341def4eef8 legacy-trunk

Ignore latex intermediate products.
author Guido van Rossum <guido@python.org>
date Wed, 26 Jun 1996 19:30:01 +0000
parents 0510b948178e
children 65d2d8e67da3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
2059
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
3 The Netherlands.
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 documentation for any purpose and without fee is hereby granted,
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 both that copyright notice and this permission notice appear in
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 distribution of the software without specific, written prior permission.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 ******************************************************************/
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* Long (arbitrary precision) integer object implementation */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
27 /* XXX The functional organization of this file is terrible */
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
28
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #include "allobjects.h"
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 #include "longintrepr.h"
2482
0510b948178e changes for MPW
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
31 #include "mymath.h"
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 #include <assert.h>
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
33 #include <ctype.h>
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
35 #define ABS(x) ((x) < 0 ? -(x) : (x))
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
36
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
37 /* Forward */
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
38 static longobject *long_normalize PROTO((longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
39 static longobject *mul1 PROTO((longobject *, wdigit));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
40 static longobject *muladd1 PROTO((longobject *, wdigit, wdigit));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
41 static longobject *divrem1 PROTO((longobject *, wdigit, digit *));
1391
7c3245c18677 * ceval.c, longobject.c, methodobject.c, listnode.c, arraymodule.c,
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
42 static object *long_format PROTO((object *aa, int base));
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
43
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
44 static int ticker; /* XXX Could be shared with ceval? */
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
45
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
46 #define SIGCHECK(block) \
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
47 if (--ticker < 0) { \
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
48 ticker = 100; \
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
49 if (sigcheck()) { block; } \
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
50 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
51
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 /* Normalize (remove leading zeros from) a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 Doesn't attempt to free the storage--in most cases, due to the nature
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 of the algorithms used, this could save at most be one word anyway. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
56 static longobject *
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 long_normalize(v)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 register longobject *v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
60 int j = ABS(v->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 register int i = j;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 while (i > 0 && v->ob_digit[i-1] == 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 --i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 if (i != j)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
66 v->ob_size = (v->ob_size < 0) ? -(i) : i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 return v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 /* Allocate a new long int object with size digits.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 Return NULL and set exception if we run out of memory. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 longobject *
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 alloclongobject(size)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 int size;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 return NEWVAROBJ(longobject, &Longtype, size);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 /* Create a new long int object from a C long int */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 object *
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 newlongobject(ival)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 long ival;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 /* Assume a C long fits in at most 3 'digits' */
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
87 /* XXX On 64 bit machines this isn't true!!! */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 longobject *v = alloclongobject(3);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 if (v != NULL) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if (ival < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 ival = -ival;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
92 v->ob_size = -(v->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 v->ob_digit[0] = ival & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 v->ob_digit[1] = (ival >> SHIFT) & MASK;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
96 v->ob_digit[2] = ((unsigned long)ival >> (2*SHIFT)) & MASK;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 v = long_normalize(v);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 return (object *)v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
102 /* Create a new long int object from a C double */
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
103
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
104 object *
2482
0510b948178e changes for MPW
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
105 #ifdef MPW
0510b948178e changes for MPW
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
106 dnewlongobject(double dval)
0510b948178e changes for MPW
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
107 #else
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
108 dnewlongobject(dval)
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
109 double dval;
2482
0510b948178e changes for MPW
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
110 #endif /* MPW */
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
111 {
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
112 longobject *v;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
113 double frac;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
114 int i, ndig, expo, neg;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
115 neg = 0;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
116 if (dval < 0.0) {
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
117 neg = 1;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
118 dval = -dval;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
119 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
120 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
121 if (expo <= 0)
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
122 return newlongobject(0L);
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
123 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
124 v = alloclongobject(ndig);
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
125 if (v == NULL)
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
126 return NULL;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
127 frac = ldexp(frac, (expo-1) % SHIFT + 1);
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
128 for (i = ndig; --i >= 0; ) {
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
129 long bits = (long)frac;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
130 v->ob_digit[i] = bits;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
131 frac = frac - (double)bits;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
132 frac = ldexp(frac, SHIFT);
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
133 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
134 if (neg)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
135 v->ob_size = -(v->ob_size);
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
136 return (object *)v;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
137 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
138
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 /* Get a C long int from a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 Returns -1 and sets an error condition if overflow occurs. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 long
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 getlongvalue(vv)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 object *vv;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 register longobject *v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 long x, prev;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 int i, sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 if (vv == NULL || !is_longobject(vv)) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 err_badcall();
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 v = (longobject *)vv;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 i = v->ob_size;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 x = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 if (i < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 sign = -1;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
160 i = -(i);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 while (--i >= 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 prev = x;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 x = (x << SHIFT) + v->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 if ((x >> SHIFT) != prev) {
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1837
diff changeset
166 err_setstr(OverflowError,
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 "long int too long to convert");
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 return x * sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 /* Get a C double from a long int object. No overflow check. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 double
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 dgetlongvalue(vv)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 object *vv;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 register longobject *v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 double x;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 double multiplier = (double) (1L << SHIFT);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 int i, sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 if (vv == NULL || !is_longobject(vv)) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 err_badcall();
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 v = (longobject *)vv;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 i = v->ob_size;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 x = 0.0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 if (i < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 sign = -1;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
195 i = -(i);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 while (--i >= 0) {
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
198 x = x*multiplier + (double)v->ob_digit[i];
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 return x * sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 /* Multiply by a single digit, ignoring the sign. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
205 static longobject *
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 mul1(a, n)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 longobject *a;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
208 wdigit n;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 return muladd1(a, n, (digit)0);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 /* Multiply by a single digit and add a single digit, ignoring the sign. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
215 static longobject *
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 muladd1(a, n, extra)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 longobject *a;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
218 wdigit n;
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
219 wdigit extra;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
221 int size_a = ABS(a->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 longobject *z = alloclongobject(size_a+1);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 twodigits carry = extra;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 for (i = 0; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 carry += (twodigits)a->ob_digit[i] * n;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 z->ob_digit[i] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 z->ob_digit[i] = carry;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 /* Divide a long integer by a digit, returning both the quotient
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 (as function result) and the remainder (through *prem).
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 The sign of a is ignored; n should not be zero. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
241 static longobject *
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 divrem1(a, n, prem)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 longobject *a;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
244 wdigit n;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 digit *prem;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
247 int size = ABS(a->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 longobject *z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 twodigits rem = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 assert(n > 0 && n <= MASK);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 z = alloclongobject(size);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 for (i = size; --i >= 0; ) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 rem = (rem << SHIFT) + a->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 z->ob_digit[i] = rem/n;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 rem %= n;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 *prem = rem;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 /* Convert a long int object to a string, using a given conversion base.
406
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
266 Return a string object.
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
267 If base is 8 or 16, add the proper prefix '0' or '0x'.
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
268 External linkage: used in bltinmodule.c by hex() and oct(). */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
270 static object *
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
271 long_format(aa, base)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
272 object *aa;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 int base;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 {
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
275 register longobject *a = (longobject *)aa;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 stringobject *str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 int i;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
278 int size_a = ABS(a->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 char *p;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 int bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 char sign = '\0';
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
282
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
283 if (a == NULL || !is_longobject(a)) {
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
284 err_badcall();
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
285 return NULL;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
286 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 assert(base >= 2 && base <= 36);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 /* Compute a rough upper bound for the length of the string */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 i = base;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 bits = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 while (i > 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 ++bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 i >>= 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
296 i = 6 + (size_a*SHIFT + bits-1) / bits;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 str = (stringobject *) newsizedstringobject((char *)0, i);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 if (str == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 p = GETSTRINGVALUE(str) + i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 *p = '\0';
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
302 *--p = 'L';
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
303 if (a->ob_size < 0)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
304 sign = '-';
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 INCREF(a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 do {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 digit rem;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 longobject *temp = divrem1(a, (digit)base, &rem);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 if (temp == NULL) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 DECREF(a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 DECREF(str);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 if (rem < 10)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 rem += '0';
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 rem += 'A'-10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 assert(p > GETSTRINGVALUE(str));
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 *--p = rem;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 DECREF(a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 a = temp;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
323 SIGCHECK({
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 DECREF(a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 DECREF(str);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 return NULL;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
327 })
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
328 } while (ABS(a->ob_size) != 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 DECREF(a);
958
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
330 if (base == 8) {
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
331 if (size_a != 0)
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
332 *--p = '0';
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
333 }
406
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
334 else if (base == 16) {
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
335 *--p = 'x';
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
336 *--p = '0';
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
337 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
338 else if (base != 10) {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
339 *--p = '#';
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
340 *--p = '0' + base%10;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
341 if (base > 10)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
342 *--p = '0' + base/10;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
343 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 if (sign)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 *--p = sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 if (p != GETSTRINGVALUE(str)) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 char *q = GETSTRINGVALUE(str);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 assert(p > q);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 do {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 } while ((*q++ = *p++) != '\0');
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
351 q--;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 resizestring((object **)&str, (int) (q - GETSTRINGVALUE(str)));
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 }
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
354 return (object *)str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
2158
29720c5d9e7e long_scan is no longer used
Guido van Rossum <guido@python.org>
parents: 2111
diff changeset
357 #if 0
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 /* Convert a string to a long int object, in a given base.
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
359 Base zero implies a default depending on the number.
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
360 External linkage: used in compile.c and stropmodule.c. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 object *
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 long_scan(str, base)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 char *str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 int base;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 {
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
367 return long_escan(str, (char **)NULL, base);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
368 }
2158
29720c5d9e7e long_scan is no longer used
Guido van Rossum <guido@python.org>
parents: 2111
diff changeset
369 #endif
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
370
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
371 object *
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
372 long_escan(str, pend, base)
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
373 char *str;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
374 char **pend;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
375 int base;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
376 {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 int sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 longobject *z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
379
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
380 if (base != 0 && base < 2 || base > 36) {
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
381 err_setstr(ValueError, "invalid base for long literal");
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
382 return NULL;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
383 }
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2158
diff changeset
384 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
385 str++;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 if (*str == '+')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 else if (*str == '-') {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 sign = -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 }
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2158
diff changeset
392 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
393 str++;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 if (base == 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 if (str[0] != '0')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 base = 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 else if (str[1] == 'x' || str[1] == 'X')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 base = 16;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 base = 8;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 str += 2;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 z = alloclongobject(0);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 for ( ; z != NULL; ++str) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 int k = -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 longobject *temp;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
408
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 if (*str <= '9')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 k = *str - '0';
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 else if (*str >= 'a')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 k = *str - 'a' + 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 else if (*str >= 'A')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 k = *str - 'A' + 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 if (k < 0 || k >= base)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 break;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 temp = muladd1(z, (digit)base, (digit)k);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 DECREF(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 z = temp;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
421 if (sign < 0 && z != NULL && z->ob_size != 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
422 z->ob_size = -(z->ob_size);
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
423 if (pend)
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
424 *pend = str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 return (object *) z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
427
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 static longobject *x_divrem PROTO((longobject *, longobject *, longobject **));
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
429 static object *long_pos PROTO((longobject *));
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
430 static long_divrem PROTO((longobject *, longobject *,
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
431 longobject **, longobject **));
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
432
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 /* Long division with remainder, top-level routine */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
434
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
435 static int
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
436 long_divrem(a, b, pdiv, prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 longobject *a, *b;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
438 longobject **pdiv;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 longobject **prem;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
441 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 longobject *z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 if (size_b == 0) {
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
445 err_setstr(ZeroDivisionError, "long division or modulo");
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
446 return -1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 if (size_a < size_b ||
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
449 size_a == size_b &&
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
450 a->ob_digit[size_a-1] < b->ob_digit[size_b-1]) {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 /* |a| < |b|. */
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
452 *pdiv = alloclongobject(0);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
453 INCREF(a);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
454 *prem = (longobject *) a;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
455 return 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 if (size_b == 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 digit rem = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 z = divrem1(a, b->ob_digit[0], &rem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
460 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
461 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
462 *prem = (longobject *) newlongobject((long)rem);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 }
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
464 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 z = x_divrem(a, b, prem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
466 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
467 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
468 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 /* Set the signs.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 The quotient z has the sign of a*b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 the remainder r has the sign of a,
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 so a = b*z + r. */
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
473 if ((a->ob_size < 0) != (b->ob_size < 0))
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
474 z->ob_size = -(z->ob_size);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
475 if (a->ob_size < 0 && (*prem)->ob_size != 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
476 (*prem)->ob_size = -((*prem)->ob_size);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
477 *pdiv = z;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
478 return 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
480
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
481 /* Unsigned long division with remainder -- the algorithm */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
482
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 static longobject *
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 x_divrem(v1, w1, prem)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 longobject *v1, *w1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 longobject **prem;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
488 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 digit d = (twodigits)BASE / (w1->ob_digit[size_w-1] + 1);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 longobject *v = mul1(v1, d);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 longobject *w = mul1(w1, d);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 longobject *a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 int j, k;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
494
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 if (v == NULL || w == NULL) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 XDECREF(v);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 XDECREF(w);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
500
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
502 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
503 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
505 size_v = ABS(v->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 a = alloclongobject(size_v - size_w + 1);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
507
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 twodigits q;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
511 stwodigits carry = 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
513
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
514 SIGCHECK({
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 DECREF(a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 a = NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 break;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
518 })
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 if (vj == w->ob_digit[size_w-1])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 q = MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 w->ob_digit[size_w-1];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 while (w->ob_digit[size_w-2]*q >
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 ((
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 ((twodigits)vj << SHIFT)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 + v->ob_digit[j-1]
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 - q*w->ob_digit[size_w-1]
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 ) << SHIFT)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 + v->ob_digit[j-2])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 --q;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
533
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 for (i = 0; i < size_w && i+k < size_v; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 twodigits z = w->ob_digit[i] * q;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 digit zz = z >> SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 carry += v->ob_digit[i+k] - z + ((twodigits)zz << SHIFT);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 v->ob_digit[i+k] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 carry = (carry >> SHIFT) - zz;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
541
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 if (i+k < size_v) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 carry += v->ob_digit[i+k];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 v->ob_digit[i+k] = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
546
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 if (carry == 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 a->ob_digit[k] = q;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 assert(carry == -1);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 a->ob_digit[k] = q-1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 carry = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 for (i = 0; i < size_w && i+k < size_v; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 carry += v->ob_digit[i+k] + w->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 v->ob_digit[i+k] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 } /* for j, k */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
560
2111
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
561 if (a == NULL)
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
562 *prem = NULL;
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
563 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 a = long_normalize(a);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
565 *prem = divrem1(v, d, &d);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
566 /* d receives the (unused) remainder */
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
567 if (*prem == NULL) {
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
568 DECREF(a);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
569 a = NULL;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
570 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 DECREF(v);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 DECREF(w);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 return a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
576
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 /* Methods */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
579 /* Forward */
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
580 static void long_dealloc PROTO((object *));
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
581 static object *long_repr PROTO((object *));
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
582 static int long_compare PROTO((longobject *, longobject *));
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
583 static long long_hash PROTO((longobject *));
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
584
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
585 static object *long_add PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
586 static object *long_sub PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
587 static object *long_mul PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
588 static object *long_div PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
589 static object *long_mod PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
590 static object *long_divmod PROTO((longobject *, longobject *));
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
591 static object *long_pow PROTO((longobject *, longobject *, longobject *));
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
592 static object *long_neg PROTO((longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
593 static object *long_pos PROTO((longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
594 static object *long_abs PROTO((longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
595 static int long_nonzero PROTO((longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
596 static object *long_invert PROTO((longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
597 static object *long_lshift PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
598 static object *long_rshift PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
599 static object *long_and PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
600 static object *long_xor PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
601 static object *long_or PROTO((longobject *, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
602
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 static void
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 long_dealloc(v)
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
605 object *v;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 DEL(v);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
609
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 static object *
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 long_repr(v)
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
612 object *v;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 {
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
614 return long_format(v, 10);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
616
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 static int
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 long_compare(a, b)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 longobject *a, *b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 int sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
622
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
623 if (a->ob_size != b->ob_size) {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
624 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
625 sign = 0;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
626 else
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
627 sign = a->ob_size - b->ob_size;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
628 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 else {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
630 int i = ABS(a->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 if (i < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 sign = 0;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
635 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
637 if (a->ob_size < 0)
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
638 sign = -sign;
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
639 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
641 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
643
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
644 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
645 long_hash(v)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
646 longobject *v;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
647 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
648 long x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
649 int i, sign;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
650
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
651 /* This is designed so that Python ints and longs with the
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
652 same value hash to the same value, otherwise comparisons
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
653 of mapping keys will turn out weird */
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
654 i = v->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
655 sign = 1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
656 x = 0;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
657 if (i < 0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
658 sign = -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
659 i = -(i);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
660 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
661 while (--i >= 0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
662 /* Force a 32-bit circular shift */
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
663 x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
664 x += v->ob_digit[i];
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
665 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
666 x = x * sign;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
667 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
668 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
669 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
670 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
671
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
672
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 /* Add the absolute values of two long integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
674
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 static longobject *x_add PROTO((longobject *, longobject *));
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 static longobject *
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 x_add(a, b)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 longobject *a, *b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
680 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
681 longobject *z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 digit carry = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
684
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
685 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 if (size_a < size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 { longobject *temp = a; a = b; b = temp; }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 { int size_temp = size_a; size_a = size_b; size_b = size_temp; }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 z = alloclongobject(size_a+1);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 carry += a->ob_digit[i] + b->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 z->ob_digit[i] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 /* The following assumes unsigned shifts don't
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 propagate the sign bit. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 carry += a->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 z->ob_digit[i] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 z->ob_digit[i] = carry;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
708
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 /* Subtract the absolute values of two integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
710
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 static longobject *x_sub PROTO((longobject *, longobject *));
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 static longobject *
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 x_sub(a, b)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 longobject *a, *b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
716 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 longobject *z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 int sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 digit borrow = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
721
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 if (size_a < size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 sign = -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 { longobject *temp = a; a = b; b = temp; }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 { int size_temp = size_a; size_a = size_b; size_b = size_temp; }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 else if (size_a == size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 /* Find highest digit where a and b differ: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 i = size_a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 if (i < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 return alloclongobject(0);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 if (a->ob_digit[i] < b->ob_digit[i]) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 sign = -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 { longobject *temp = a; a = b; b = temp; }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 size_a = size_b = i+1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
741 z = alloclongobject(size_a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
742 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
745 /* The following assumes unsigned arithmetic
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 works module 2**N for some N>SHIFT. */
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
747 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 z->ob_digit[i] = borrow & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 borrow >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 borrow &= 1; /* Keep only one sign bit */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
752 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 borrow = a->ob_digit[i] - borrow;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 z->ob_digit[i] = borrow & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 borrow >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 assert(borrow == 0);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
758 if (sign < 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
759 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
761 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
762
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 static object *
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
764 long_add(a, b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 longobject *a;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
766 longobject *b;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 longobject *z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
769
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 if (a->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
771 if (b->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
772 z = x_add(a, b);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
773 if (z != NULL && z->ob_size != 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
774 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
777 z = x_sub(b, a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
782 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
783 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 return (object *)z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
787
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 static object *
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
789 long_sub(a, b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
790 longobject *a;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
791 longobject *b;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
792 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
793 longobject *z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
794
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
795 if (a->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
797 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 z = x_add(a, b);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
800 if (z != NULL && z->ob_size != 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
801 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
802 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
803 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
804 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 return (object *)z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
811
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 static object *
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
813 long_mul(a, b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 longobject *a;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
815 longobject *b;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 int size_a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 int size_b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 longobject *z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
821
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
822 size_a = ABS(a->ob_size);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
823 size_b = ABS(b->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
824 z = alloclongobject(size_a + size_b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
825 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
826 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 for (i = 0; i < z->ob_size; ++i)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
828 z->ob_digit[i] = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
829 for (i = 0; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
830 twodigits carry = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
831 twodigits f = a->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
832 int j;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
833
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
834 SIGCHECK({
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
835 DECREF(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
836 return NULL;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
837 })
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
838 for (j = 0; j < size_b; ++j) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
839 carry += z->ob_digit[i+j] + b->ob_digit[j] * f;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
840 z->ob_digit[i+j] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
841 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
843 for (; carry != 0; ++j) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
844 assert(i+j < z->ob_size);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
845 carry += z->ob_digit[i+j];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
846 z->ob_digit[i+j] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
847 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
848 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 if (a->ob_size < 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
851 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
852 if (b->ob_size < 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
853 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 return (object *) long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
855 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
856
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
857 /* The / and % operators are now defined in terms of divmod().
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
858 The expression a mod b has the value a - b*floor(a/b).
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
859 The long_divrem function gives the remainder after division of
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
860 |a| by |b|, with the sign of a. This is also expressed
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
861 as a - b*trunc(a/b), if trunc truncates towards zero.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
862 Some examples:
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
863 a b a rem b a mod b
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
864 13 10 3 3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
865 -13 10 -3 7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
866 13 -10 3 -7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
867 -13 -10 -3 -3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
868 So, to get from rem to mod, we have to add b if a and b
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
869 have different signs. We then subtract one from the 'div'
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
870 part of the outcome to keep the invariant intact. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
871
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
872 static int l_divmod PROTO((longobject *, longobject *,
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
873 longobject **, longobject **));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
874 static int
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
875 l_divmod(v, w, pdiv, pmod)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
876 longobject *v;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
877 longobject *w;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
878 longobject **pdiv;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
879 longobject **pmod;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
880 {
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
881 longobject *div, *mod;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
882
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
883 if (long_divrem(v, w, &div, &mod) < 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
884 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
885 if (mod->ob_size < 0 && w->ob_size > 0 ||
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
886 mod->ob_size > 0 && w->ob_size < 0) {
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
887 longobject *temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
888 longobject *one;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
889 temp = (longobject *) long_add(mod, w);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
890 DECREF(mod);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
891 mod = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
892 if (mod == NULL) {
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
893 DECREF(div);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
894 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
895 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
896 one = (longobject *) newlongobject(1L);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
897 if (one == NULL ||
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
898 (temp = (longobject *) long_sub(div, one)) == NULL) {
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
899 DECREF(mod);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
900 DECREF(div);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
901 XDECREF(one);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
902 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
903 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
904 DECREF(one);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
905 DECREF(div);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
906 div = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
907 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
908 *pdiv = div;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
909 *pmod = mod;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
910 return 0;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
911 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
912
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
913 static object *
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
914 long_div(v, w)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
915 longobject *v;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
916 longobject *w;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
917 {
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
918 longobject *div, *mod;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
919 if (l_divmod(v, w, &div, &mod) < 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
920 return NULL;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
921 DECREF(mod);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
922 return (object *)div;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
923 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
924
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
925 static object *
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
926 long_mod(v, w)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
927 longobject *v;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
928 longobject *w;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
929 {
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
930 longobject *div, *mod;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
931 if (l_divmod(v, w, &div, &mod) < 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
932 return NULL;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
933 DECREF(div);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
934 return (object *)mod;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
935 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
936
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 static object *
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
938 long_divmod(v, w)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
939 longobject *v;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
940 longobject *w;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
942 object *z;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
943 longobject *div, *mod;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
944 if (l_divmod(v, w, &div, &mod) < 0)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
945 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
946 z = newtupleobject(2);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
947 if (z != NULL) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
948 settupleitem(z, 0, (object *) div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
949 settupleitem(z, 1, (object *) mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
951 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
952 DECREF(div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
953 DECREF(mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
954 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
955 return z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
956 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
957
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
958 static object *
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
959 long_pow(a, b, c)
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
960 longobject *a;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
961 longobject *b;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
962 longobject *c;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
963 {
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
964 longobject *z, *div, *mod;
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
965 int size_b, i;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
966
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
967 size_b = b->ob_size;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
968 if (size_b < 0) {
475
e2c3463c4d91 Use new exceptions.
Guido van Rossum <guido@python.org>
parents: 443
diff changeset
969 err_setstr(ValueError, "long integer to the negative power");
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
970 return NULL;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
971 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
972 z = (longobject *)newlongobject(1L);
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
973 INCREF(a);
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
974 for (i = 0; i < size_b; ++i) {
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
975 digit bi = b->ob_digit[i];
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
976 int j;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
977
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
978 for (j = 0; j < SHIFT; ++j) {
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
979 longobject *temp;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
980
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
981 if (bi & 1) {
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
982 temp = (longobject *)long_mul(z, a);
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
983 DECREF(z);
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
984 if ((object*)c!=None && temp!=NULL) {
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
985 l_divmod(temp, c, &div, &mod);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
986 XDECREF(div);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
987 DECREF(temp);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
988 temp = mod;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
989 }
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
990 z = temp;
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
991 if (z == NULL)
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
992 break;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
993 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
994 bi >>= 1;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
995 if (bi == 0 && i+1 == size_b)
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
996 break;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
997 temp = (longobject *)long_mul(a, a);
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
998 DECREF(a);
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
999 if ((object*)c!=None && temp!=NULL) {
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1000 l_divmod(temp, c, &div, &mod);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1001 XDECREF(div);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1002 DECREF(temp);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1003 temp = mod;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1004 }
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1005 a = temp;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1006 if (a == NULL) {
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1007 DECREF(z);
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1008 z = NULL;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1009 break;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1010 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1011 }
2111
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
1012 if (a == NULL || z == NULL)
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1013 break;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1014 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1015 XDECREF(a);
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1016 if ((object*)c!=None && z!=NULL) {
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1017 l_divmod(z, c, &div, &mod);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1018 XDECREF(div);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1019 DECREF(z);
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1020 z=mod;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1021 }
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1022 return (object *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 static object *
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1026 long_invert(v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 longobject *v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1028 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1029 /* Implement ~x as -(x+1) */
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1030 longobject *x;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1031 longobject *w;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1032 w = (longobject *)newlongobject(1L);
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1033 if (w == NULL)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1034 return NULL;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1035 x = (longobject *) long_add(v, w);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1036 DECREF(w);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1037 if (x == NULL)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1038 return NULL;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1039 if (x->ob_size != 0)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1040 x->ob_size = -(x->ob_size);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1041 return (object *)x;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1043
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1044 static object *
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1045 long_pos(v)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1046 longobject *v;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1047 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1048 INCREF(v);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1049 return (object *)v;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1050 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1051
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1052 static object *
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1053 long_neg(v)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1054 longobject *v;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1055 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1056 longobject *z;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1057 int i, n;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1058 n = ABS(v->ob_size);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1059 if (n == 0) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1060 /* -0 == 0 */
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1061 INCREF(v);
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1062 return (object *) v;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1063 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1064 z = alloclongobject(ABS(n));
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1065 if (z == NULL)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1066 return NULL;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1067 for (i = 0; i < n; i++)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1068 z->ob_digit[i] = v->ob_digit[i];
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1069 z->ob_size = -(v->ob_size);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1070 return (object *)z;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1071 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1072
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1073 static object *
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1074 long_abs(v)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1075 longobject *v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1076 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1077 if (v->ob_size < 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1078 return long_neg(v);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1079 else {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1080 INCREF(v);
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1081 return (object *)v;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1082 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1083 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1084
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1085 static int
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1086 long_nonzero(v)
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1087 longobject *v;
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1088 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1089 return ABS(v->ob_size) != 0;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1090 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1091
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1092 static object *
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1093 long_rshift(a, b)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1094 longobject *a;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1095 longobject *b;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1096 {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1097 longobject *z;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1098 long shiftby;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1099 int newsize, wordshift, loshift, hishift, i, j;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1100 digit lomask, himask;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1101
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1102 if (a->ob_size < 0) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1103 /* Right shifting negative numbers is harder */
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1104 longobject *a1, *a2, *a3;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1105 a1 = (longobject *) long_invert(a);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1106 if (a1 == NULL) return NULL;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1107 a2 = (longobject *) long_rshift(a1, b);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1108 DECREF(a1);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1109 if (a2 == NULL) return NULL;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1110 a3 = (longobject *) long_invert(a2);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1111 DECREF(a2);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1112 return (object *) a3;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1113 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1114
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1115 shiftby = getlongvalue((object *)b);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1116 if (shiftby == -1L && err_occurred())
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1117 return NULL;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1118 if (shiftby < 0) {
475
e2c3463c4d91 Use new exceptions.
Guido van Rossum <guido@python.org>
parents: 443
diff changeset
1119 err_setstr(ValueError, "negative shift count");
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1120 return NULL;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1121 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1122 wordshift = shiftby / SHIFT;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1123 newsize = ABS(a->ob_size) - wordshift;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1124 if (newsize <= 0) {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1125 z = alloclongobject(0);
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1126 return (object *)z;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1127 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1128 loshift = shiftby % SHIFT;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1129 hishift = SHIFT - loshift;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1130 lomask = ((digit)1 << hishift) - 1;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1131 himask = MASK ^ lomask;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1132 z = alloclongobject(newsize);
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1133 if (z == NULL)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1134 return NULL;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1135 if (a->ob_size < 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1136 z->ob_size = -(z->ob_size);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1137 for (i = 0, j = wordshift; i < newsize; i++, j++) {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1138 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1139 if (i+1 < newsize)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1140 z->ob_digit[i] |=
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1141 (a->ob_digit[j+1] << hishift) & himask;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1142 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1143 return (object *) long_normalize(z);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1144 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1145
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1146 static object *
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1147 long_lshift(a, b)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1148 longobject *a;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1149 longobject *b;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1150 {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1151 longobject *z;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1152 long shiftby;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1153 int newsize, wordshift, loshift, hishift, i, j;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1154 digit lomask, himask;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1155
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1156 shiftby = getlongvalue((object *)b);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1157 if (shiftby == -1L && err_occurred())
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1158 return NULL;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1159 if (shiftby < 0) {
475
e2c3463c4d91 Use new exceptions.
Guido van Rossum <guido@python.org>
parents: 443
diff changeset
1160 err_setstr(ValueError, "negative shift count");
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1161 return NULL;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1162 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1163 if (shiftby > MASK) {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1164 err_setstr(ValueError, "outrageous left shift count");
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1165 return NULL;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1166 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1167 if (shiftby % SHIFT == 0) {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1168 wordshift = shiftby / SHIFT;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1169 loshift = 0;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1170 hishift = SHIFT;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1171 newsize = ABS(a->ob_size) + wordshift;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1172 lomask = MASK;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1173 himask = 0;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1174 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1175 else {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1176 wordshift = shiftby / SHIFT + 1;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1177 loshift = SHIFT - shiftby%SHIFT;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1178 hishift = shiftby % SHIFT;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1179 newsize = ABS(a->ob_size) + wordshift;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1180 lomask = ((digit)1 << hishift) - 1;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1181 himask = MASK ^ lomask;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1182 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1183 z = alloclongobject(newsize);
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1184 if (z == NULL)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1185 return NULL;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1186 if (a->ob_size < 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1187 z->ob_size = -(z->ob_size);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1188 for (i = 0; i < wordshift; i++)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1189 z->ob_digit[i] = 0;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1190 for (i = wordshift, j = 0; i < newsize; i++, j++) {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1191 if (i > 0)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1192 z->ob_digit[i-1] |=
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1193 (a->ob_digit[j] << hishift) & himask;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1194 z->ob_digit[i] =
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1195 (a->ob_digit[j] >> loshift) & lomask;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1196 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1197 return (object *) long_normalize(z);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1198 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1199
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1200
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1201 /* Bitwise and/xor/or operations */
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1202
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1203 #define MAX(x, y) ((x) < (y) ? (y) : (x))
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1204 #define MIN(x, y) ((x) > (y) ? (y) : (x))
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1205
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1206 static object *long_bitwise PROTO((longobject *, int, longobject *));
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1207 static object *
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1208 long_bitwise(a, op, b)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1209 longobject *a;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1210 int op; /* '&', '|', '^' */
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1211 longobject *b;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1212 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1213 digit maska, maskb; /* 0 or MASK */
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1214 int negz;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1215 int size_a, size_b, size_z;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1216 longobject *z;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1217 int i;
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1218 digit diga, digb;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1219 object *v;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1220
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1221 if (a->ob_size < 0) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1222 a = (longobject *) long_invert(a);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1223 maska = MASK;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1224 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1225 else {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1226 INCREF(a);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1227 maska = 0;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1228 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1229 if (b->ob_size < 0) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1230 b = (longobject *) long_invert(b);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1231 maskb = MASK;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1232 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1233 else {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1234 INCREF(b);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1235 maskb = 0;
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1236 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1237
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1238 size_a = a->ob_size;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1239 size_b = b->ob_size;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1240 size_z = MAX(size_a, size_b);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1241 z = alloclongobject(size_z);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1242 if (a == NULL || b == NULL || z == NULL) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1243 XDECREF(a);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1244 XDECREF(b);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1245 XDECREF(z);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1246 return NULL;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1247 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1248
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1249 negz = 0;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1250 switch (op) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1251 case '^':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1252 if (maska != maskb) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1253 maska ^= MASK;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1254 negz = -1;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1255 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1256 break;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1257 case '&':
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1258 if (maska && maskb) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1259 op = '|';
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1260 maska ^= MASK;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1261 maskb ^= MASK;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1262 negz = -1;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1263 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1264 break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1265 case '|':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1266 if (maska || maskb) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1267 op = '&';
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1268 maska ^= MASK;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1269 maskb ^= MASK;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1270 negz = -1;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1271 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1272 break;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1273 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1274
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1275 for (i = 0; i < size_z; ++i) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1276 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1277 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1278 switch (op) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1279 case '&': z->ob_digit[i] = diga & digb; break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1280 case '|': z->ob_digit[i] = diga | digb; break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1281 case '^': z->ob_digit[i] = diga ^ digb; break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1282 }
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1283 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1284
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1285 DECREF(a);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1286 DECREF(b);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1287 z = long_normalize(z);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1288 if (negz == 0)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1289 return (object *) z;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1290 v = long_invert(z);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1291 DECREF(z);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1292 return v;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1293 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1294
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1295 static object *
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1296 long_and(a, b)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1297 longobject *a;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1298 longobject *b;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1299 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1300 return long_bitwise(a, '&', b);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1301 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1302
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1303 static object *
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1304 long_xor(a, b)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1305 longobject *a;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1306 longobject *b;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1307 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1308 return long_bitwise(a, '^', b);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1309 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1310
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1311 static object *
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1312 long_or(a, b)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1313 longobject *a;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1314 longobject *b;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1315 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1316 return long_bitwise(a, '|', b);
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1317 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1318
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1319 static int
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1320 long_coerce(pv, pw)
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1321 object **pv;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1322 object **pw;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1323 {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1324 if (is_intobject(*pw)) {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1325 *pw = newlongobject(getintvalue(*pw));
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1326 INCREF(*pv);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1327 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1328 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1329 return 1; /* Can't do it */
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1330 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1331
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1332 static object *
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1333 long_int(v)
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1334 object *v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1335 {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1336 long x;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1337 x = getlongvalue(v);
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1338 if (err_occurred())
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1339 return NULL;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1340 return newintobject(x);
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1341 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1342
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1343 static object *
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1344 long_long(v)
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1345 object *v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1346 {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1347 INCREF(v);
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1348 return v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1349 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1350
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1351 static object *
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1352 long_float(v)
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1353 object *v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1354 {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1355 return newfloatobject(dgetlongvalue(v));
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1356 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1357
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1358 static object *
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1359 long_oct(v)
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1360 object *v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1361 {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1362 return long_format(v, 8);
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1363 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1364
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1365 static object *
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1366 long_hex(v)
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1367 object *v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1368 {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1369 return long_format(v, 16);
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1370 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1371
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1372
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1373 #define UF (unaryfunc)
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1374 #define BF (binaryfunc)
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1375 #define TF (ternaryfunc)
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1376 #define IF (inquiry)
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1377
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1378 static number_methods long_as_number = {
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1379 BF long_add, /*nb_add*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1380 BF long_sub, /*nb_subtract*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1381 BF long_mul, /*nb_multiply*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1382 BF long_div, /*nb_divide*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1383 BF long_mod, /*nb_remainder*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1384 BF long_divmod, /*nb_divmod*/
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1385 TF long_pow, /*nb_power*/
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1386 UF long_neg, /*nb_negative*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1387 UF long_pos, /*tp_positive*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1388 UF long_abs, /*tp_absolute*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1389 IF long_nonzero,/*tp_nonzero*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1390 UF long_invert, /*nb_invert*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1391 BF long_lshift, /*nb_lshift*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1392 BF long_rshift, /*nb_rshift*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1393 BF long_and, /*nb_and*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1394 BF long_xor, /*nb_xor*/
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1395 BF long_or, /*nb_or*/
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1396 (int (*) FPROTO((object **, object **)))
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1397 (coercion)long_coerce, /*nb_coerce*/
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1398 UF long_int, /*nb_int*/
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1399 UF long_long, /*nb_long*/
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1400 UF long_float, /*nb_float*/
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1401 UF long_oct, /*nb_oct*/
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1402 UF long_hex, /*nb_hex*/
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1403 };
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1405 typeobject Longtype = {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1406 OB_HEAD_INIT(&Typetype)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1407 0,
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1408 "long int",
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1409 sizeof(longobject) - sizeof(digit),
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1410 sizeof(digit),
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1411 (destructor)long_dealloc, /*tp_dealloc*/
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
1412 0, /*tp_print*/
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1413 0, /*tp_getattr*/
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1414 0, /*tp_setattr*/
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1415 (int (*) FPROTO((object *, object *)))
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1416 (cmpfunc)long_compare, /*tp_compare*/
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1417 (reprfunc)long_repr, /*tp_repr*/
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1418 &long_as_number,/*tp_as_number*/
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1419 0, /*tp_as_sequence*/
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1420 0, /*tp_as_mapping*/
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1421 (long (*) FPROTO((object *)))
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1422 (hashfunc)long_hash, /*tp_hash*/
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1423 };