annotate Objects/longobject.c @ 4566:6403c94de50d legacy-trunk

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