Mercurial > cpython-lru_cache-weakref
annotate Objects/intobject.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 | 739b8480f1ac |
| rev | line source |
|---|---|
| 145 | 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. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2111
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:
2111
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 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:
2111
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2111
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:
2111
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:
2111
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2111
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2111
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:
2111
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:
2111
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:
2111
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2111
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:
2111
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:
2111
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:
2111
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:
2111
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:
2111
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:
2111
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:
2111
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 6 | 32 /* Integer object implementation */ |
| 33 | |
| 86 | 34 #include "allobjects.h" |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
35 #include "modsupport.h" |
| 6 | 36 |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
37 #ifdef HAVE_LIMITS_H |
|
1365
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
38 #include <limits.h> |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
39 #endif |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
40 |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
41 #ifndef LONG_MAX |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
42 #define LONG_MAX 0X7FFFFFFFL |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
43 #endif |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
44 |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
45 #ifndef LONG_MIN |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
46 #define LONG_MIN (-LONG_MAX-1) |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
47 #endif |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
48 |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
49 #ifndef CHAR_BIT |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
50 #define CHAR_BIT 8 |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
51 #endif |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
52 |
|
1413
c914b10bf2ed
* timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents:
1365
diff
changeset
|
53 #ifndef LONG_BIT |
|
1365
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
54 #define LONG_BIT (CHAR_BIT * sizeof(long)) |
|
1413
c914b10bf2ed
* timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents:
1365
diff
changeset
|
55 #endif |
|
1365
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
56 |
|
1448
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
57 long |
|
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
58 getmaxint() |
|
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
59 { |
|
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
60 return LONG_MAX; /* To initialize sys.maxint */ |
|
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
61 } |
|
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
62 |
| 6 | 63 /* Standard Booleans */ |
| 86 | 64 |
| 6 | 65 intobject FalseObject = { |
| 66 OB_HEAD_INIT(&Inttype) | |
| 67 0 | |
| 68 }; | |
| 86 | 69 |
| 6 | 70 intobject TrueObject = { |
| 71 OB_HEAD_INIT(&Inttype) | |
| 72 1 | |
| 73 }; | |
| 74 | |
| 11 | 75 static object * |
| 471 | 76 err_ovf(msg) |
| 77 char *msg; | |
| 11 | 78 { |
| 471 | 79 err_setstr(OverflowError, msg); |
| 11 | 80 return NULL; |
| 81 } | |
| 82 | |
| 86 | 83 /* Integers are quite normal objects, to make object handling uniform. |
| 84 (Using odd pointers to represent integers would save much space | |
| 85 but require extra checks for this special case throughout the code.) | |
| 86 Since, a typical Python program spends much of its time allocating | |
| 87 and deallocating integers, these operations should be very fast. | |
| 88 Therefore we use a dedicated allocation scheme with a much lower | |
| 89 overhead (in space and time) than straight malloc(): a simple | |
| 90 dedicated free list, filled when necessary with memory from malloc(). | |
| 91 */ | |
| 92 | |
| 93 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ | |
| 94 #define N_INTOBJECTS (BLOCK_SIZE / sizeof(intobject)) | |
| 95 | |
| 96 static intobject * | |
| 97 fill_free_list() | |
| 98 { | |
| 99 intobject *p, *q; | |
| 100 p = NEW(intobject, N_INTOBJECTS); | |
| 101 if (p == NULL) | |
| 102 return (intobject *)err_nomem(); | |
| 103 q = p + N_INTOBJECTS; | |
| 104 while (--q > p) | |
| 105 *(intobject **)q = q-1; | |
| 106 *(intobject **)q = NULL; | |
| 107 return p + N_INTOBJECTS - 1; | |
| 108 } | |
| 109 | |
| 110 static intobject *free_list = NULL; | |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
111 #ifndef NSMALLPOSINTS |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
112 #define NSMALLPOSINTS 100 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
113 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
114 #ifndef NSMALLNEGINTS |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
115 #define NSMALLNEGINTS 1 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
116 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
117 #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
118 /* References to small integers are saved in this array so that they |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
119 can be shared. |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
120 The integers that are saved are those in the range |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
121 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
122 */ |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
123 static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
124 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
125 #ifdef COUNT_ALLOCS |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
126 int quick_int_allocs, quick_neg_int_allocs; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
127 #endif |
| 86 | 128 |
| 6 | 129 object * |
| 130 newintobject(ival) | |
| 131 long ival; | |
| 132 { | |
| 86 | 133 register intobject *v; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
134 #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
135 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS && |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
136 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) { |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
137 INCREF(v); |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
138 #ifdef COUNT_ALLOCS |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
139 if (ival >= 0) |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
140 quick_int_allocs++; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
141 else |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
142 quick_neg_int_allocs++; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
143 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
144 return (object *) v; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
145 } |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
146 #endif |
| 86 | 147 if (free_list == NULL) { |
| 148 if ((free_list = fill_free_list()) == NULL) | |
| 149 return NULL; | |
| 6 | 150 } |
| 86 | 151 v = free_list; |
| 152 free_list = *(intobject **)free_list; | |
| 153 v->ob_type = &Inttype; | |
| 154 v->ob_ival = ival; | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1271
diff
changeset
|
155 NEWREF(v); |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
156 #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
157 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
158 /* save this one for a following allocation */ |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
159 INCREF(v); |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
160 small_ints[ival + NSMALLNEGINTS] = v; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
161 } |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
162 #endif |
| 86 | 163 return (object *) v; |
| 164 } | |
| 165 | |
| 166 static void | |
| 167 int_dealloc(v) | |
| 168 intobject *v; | |
| 169 { | |
| 170 *(intobject **)v = free_list; | |
| 171 free_list = v; | |
| 6 | 172 } |
| 173 | |
| 174 long | |
| 175 getintvalue(op) | |
| 176 register object *op; | |
| 177 { | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
178 number_methods *nb; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
179 intobject *io; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
180 long val; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
181 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
182 if (op && is_intobject(op)) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
183 return GETINTVALUE((intobject*) op); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
184 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
185 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
186 nb->nb_int == NULL) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
187 err_badarg(); |
| 6 | 188 return -1; |
| 189 } | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
190 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
191 io = (intobject*) (*nb->nb_int) (op); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
192 if (io == NULL) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
193 return -1; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
194 if (!is_intobject(io)) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
195 err_setstr(TypeError, "nb_int should return int object"); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
196 return -1; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
197 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
198 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
199 val = GETINTVALUE(io); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
200 DECREF(io); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
201 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
202 return val; |
| 6 | 203 } |
| 204 | |
| 205 /* Methods */ | |
| 206 | |
| 750 | 207 /* ARGSUSED */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
240
diff
changeset
|
208 static int |
| 86 | 209 int_print(v, fp, flags) |
| 6 | 210 intobject *v; |
| 211 FILE *fp; | |
| 750 | 212 int flags; /* Not used but required by interface */ |
| 6 | 213 { |
| 214 fprintf(fp, "%ld", v->ob_ival); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
240
diff
changeset
|
215 return 0; |
| 6 | 216 } |
| 217 | |
| 218 static object * | |
| 86 | 219 int_repr(v) |
| 6 | 220 intobject *v; |
| 221 { | |
| 222 char buf[20]; | |
| 223 sprintf(buf, "%ld", v->ob_ival); | |
| 224 return newstringobject(buf); | |
| 225 } | |
| 226 | |
| 227 static int | |
| 86 | 228 int_compare(v, w) |
| 6 | 229 intobject *v, *w; |
| 230 { | |
| 231 register long i = v->ob_ival; | |
| 232 register long j = w->ob_ival; | |
| 233 return (i < j) ? -1 : (i > j) ? 1 : 0; | |
| 234 } | |
| 235 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
236 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
237 int_hash(v) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
238 intobject *v; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
239 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
240 long x = v -> ob_ival; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
241 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
242 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
243 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
244 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
245 |
| 6 | 246 static object * |
| 86 | 247 int_add(v, w) |
| 6 | 248 intobject *v; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
249 intobject *w; |
| 6 | 250 { |
| 251 register long a, b, x; | |
| 252 a = v->ob_ival; | |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
253 b = w->ob_ival; |
| 6 | 254 x = a + b; |
| 11 | 255 if ((x^a) < 0 && (x^b) < 0) |
| 471 | 256 return err_ovf("integer addition"); |
| 6 | 257 return newintobject(x); |
| 258 } | |
| 259 | |
| 260 static object * | |
| 86 | 261 int_sub(v, w) |
| 6 | 262 intobject *v; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
263 intobject *w; |
| 6 | 264 { |
| 265 register long a, b, x; | |
| 266 a = v->ob_ival; | |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
267 b = w->ob_ival; |
| 6 | 268 x = a - b; |
| 11 | 269 if ((x^a) < 0 && (x^~b) < 0) |
| 471 | 270 return err_ovf("integer subtraction"); |
| 6 | 271 return newintobject(x); |
| 272 } | |
| 273 | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
274 /* |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
275 Integer overflow checking used to be done using a double, but on 64 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
276 bit machines (where both long and double are 64 bit) this fails |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
277 because the double doesn't have enouvg precision. John Tromp suggests |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
278 the following algorithm: |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
279 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
280 Suppose again we normalize a and b to be nonnegative. |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
281 Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.). |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
282 Now we test ah and bh against zero and get essentially 3 possible outcomes. |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
283 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
284 1) both ah and bh > 0 : then report overflow |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
285 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
286 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
287 negative |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
288 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
289 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
290 compute al*bl and report overflow if it's negative |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
291 add (ah*bl)<<32 to al*bl and report overflow if |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
292 it's negative |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
293 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
294 In case of no overflow the result is then negated if necessary. |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
295 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
296 The majority of cases will be 2), in which case this method is the same as |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
297 what I suggested before. If multiplication is expensive enough, then the |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
298 other method is faster on case 3), but also more work to program, so I |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
299 guess the above is the preferred solution. |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
300 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
301 */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
302 |
| 6 | 303 static object * |
| 86 | 304 int_mul(v, w) |
| 6 | 305 intobject *v; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
306 intobject *w; |
| 6 | 307 { |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
308 long a, b, ah, bh, x, y; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
309 int s = 1; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
310 |
| 6 | 311 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
312 b = w->ob_ival; |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
313 ah = a >> (LONG_BIT/2); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
314 bh = b >> (LONG_BIT/2); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
315 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
316 /* Quick test for common case: two small positive ints */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
317 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
318 if (ah == 0 && bh == 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
319 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
320 if (x < 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
321 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
322 return newintobject(x); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
323 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
324 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
325 /* Arrange that a >= b >= 0 */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
326 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
327 if (a < 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
328 a = -a; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
329 if (a < 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
330 /* Largest negative */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
331 if (b == 0 || b == 1) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
332 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
333 goto ok; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
334 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
335 else |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
336 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
337 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
338 s = -s; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
339 ah = a >> (LONG_BIT/2); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
340 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
341 if (b < 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
342 b = -b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
343 if (b < 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
344 /* Largest negative */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
345 if (a == 0 || a == 1 && s == 1) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
346 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
347 goto ok; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
348 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
349 else |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
350 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
351 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
352 s = -s; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
353 bh = b >> (LONG_BIT/2); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
354 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
355 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
356 /* 1) both ah and bh > 0 : then report overflow */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
357 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
358 if (ah != 0 && bh != 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
359 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
360 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
361 /* 2) both ah and bh = 0 : then compute a*b and report |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
362 overflow if it comes out negative */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
363 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
364 if (ah == 0 && bh == 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
365 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
366 if (x < 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
367 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
368 return newintobject(x*s); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
369 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
370 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
371 if (a < b) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
372 /* Swap */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
373 x = a; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
374 a = b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
375 b = x; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
376 ah = bh; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
377 /* bh not used beyond this point */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
378 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
379 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
380 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
381 it's >= 2^31 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
382 compute al*bl and report overflow if it's negative |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
383 add (ah*bl)<<32 to al*bl and report overflow if |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
384 it's negative |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
385 (NB b == bl in this case, and we make a = al) */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
386 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
387 y = ah*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
388 if (y >= (1L << (LONG_BIT/2))) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
389 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
390 a &= (1L << (LONG_BIT/2)) - 1; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
391 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
392 if (x < 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
393 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
394 x += y << LONG_BIT/2; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
395 if (x < 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
396 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
397 ok: |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
398 return newintobject(x * s); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
399 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
400 bad: |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
401 return err_ovf("integer multiplication"); |
| 6 | 402 } |
| 403 | |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
404 static int |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
405 i_divmod(x, y, p_xdivy, p_xmody) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
406 register intobject *x, *y; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
407 long *p_xdivy, *p_xmody; |
| 6 | 408 { |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
409 long xi = x->ob_ival; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
410 long yi = y->ob_ival; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
411 long xdivy, xmody; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
412 |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
413 if (yi == 0) { |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
414 err_setstr(ZeroDivisionError, "integer division or modulo"); |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
415 return -1; |
| 6 | 416 } |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
417 if (yi < 0) { |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
418 if (xi < 0) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
419 xdivy = -xi / -yi; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
420 else |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
421 xdivy = - (xi / -yi); |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
422 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
423 else { |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
424 if (xi < 0) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
425 xdivy = - (-xi / yi); |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
426 else |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
427 xdivy = xi / yi; |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
428 } |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
429 xmody = xi - xdivy*yi; |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
430 if (xmody < 0 && yi > 0 || xmody > 0 && yi < 0) { |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
431 xmody += yi; |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
432 xdivy -= 1; |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
433 } |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
434 *p_xdivy = xdivy; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
435 *p_xmody = xmody; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
436 return 0; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
437 } |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
438 |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
439 static object * |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
440 int_div(x, y) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
441 intobject *x; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
442 intobject *y; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
443 { |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
444 long d, m; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
445 if (i_divmod(x, y, &d, &m) < 0) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
446 return NULL; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
447 return newintobject(d); |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
448 } |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
449 |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
450 static object * |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
451 int_mod(x, y) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
452 intobject *x; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
453 intobject *y; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
454 { |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
455 long d, m; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
456 if (i_divmod(x, y, &d, &m) < 0) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
457 return NULL; |
| 750 | 458 return newintobject(m); |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
459 } |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
460 |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
461 static object * |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
462 int_divmod(x, y) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
463 intobject *x; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
464 intobject *y; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
465 { |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
466 long d, m; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
467 if (i_divmod(x, y, &d, &m) < 0) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
468 return NULL; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
469 return mkvalue("(ll)", d, m); |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
470 } |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
471 |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
472 static object * |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
473 int_pow(v, w, z) |
| 6 | 474 intobject *v; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
475 intobject *w; |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
476 intobject *z; |
| 6 | 477 { |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
478 #if 1 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
479 register long iv, iw, iz, ix, temp, prev; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
480 int zset = 0; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
481 iv = v->ob_ival; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
482 iw = w->ob_ival; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
483 if (iw < 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
484 err_setstr(ValueError, "integer to the negative power"); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
485 return NULL; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
486 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
487 if ((object *)z != None) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
488 iz = z->ob_ival; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
489 zset = 1; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
490 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
491 /* |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
492 * XXX: The original exponentiation code stopped looping |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
493 * when temp hit zero; this code will continue onwards |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
494 * unnecessarily, but at least it won't cause any errors. |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
495 * Hopefully the speed improvement from the fast exponentiation |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
496 * will compensate for the slight inefficiency. |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
497 * XXX: Better handling of overflows is desperately needed. |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
498 */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
499 temp = iv; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
500 ix = 1; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
501 while (iw > 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
502 prev = ix; /* Save value for overflow check */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
503 if (iw & 1) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
504 ix = ix*temp; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
505 if (temp == 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
506 break; /* Avoid ix / 0 */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
507 if (ix / temp != prev) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
508 return err_ovf("integer pow()"); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
509 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
510 iw >>= 1; /* Shift exponent down by 1 bit */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
511 if (iw==0) break; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
512 prev = temp; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
513 temp *= temp; /* Square the value of temp */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
514 if (prev!=0 && temp/prev!=prev) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
515 return err_ovf("integer pow()"); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
516 if (zset) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
517 /* If we did a multiplication, perform a modulo */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
518 ix = ix % iz; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
519 temp = temp % iz; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
520 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
521 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
522 if (zset) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
523 object *t1, *t2; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
524 long int div, mod; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
525 t1=newintobject(ix); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
526 t2=newintobject(iz); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
527 if (t1==NULL || t2==NULL || |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
528 i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
529 XDECREF(t1); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
530 XDECREF(t2); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
531 return(NULL); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
532 } |
|
2111
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
533 DECREF(t1); |
|
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
534 DECREF(t2); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
535 ix=mod; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
536 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
537 return newintobject(ix); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
538 #else |
| 6 | 539 register long iv, iw, ix; |
| 540 iv = v->ob_ival; | |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
541 iw = w->ob_ival; |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
542 if (iw < 0) { |
| 471 | 543 err_setstr(ValueError, "integer to the negative power"); |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
544 return NULL; |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
545 } |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
546 if ((object *)z != None) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
547 err_setstr(TypeError, "pow(int, int, int) not yet supported"); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
548 return NULL; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
549 } |
| 6 | 550 ix = 1; |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
551 while (--iw >= 0) { |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
552 long prev = ix; |
| 6 | 553 ix = ix * iv; |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
554 if (iv == 0) |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
555 break; /* 0 to some power -- avoid ix / 0 */ |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
556 if (ix / iv != prev) |
| 471 | 557 return err_ovf("integer pow()"); |
| 6 | 558 } |
| 559 return newintobject(ix); | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
560 #endif |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
561 } |
| 6 | 562 |
| 563 static object * | |
| 86 | 564 int_neg(v) |
| 6 | 565 intobject *v; |
| 566 { | |
| 567 register long a, x; | |
| 568 a = v->ob_ival; | |
| 569 x = -a; | |
| 11 | 570 if (a < 0 && x < 0) |
| 471 | 571 return err_ovf("integer negation"); |
| 6 | 572 return newintobject(x); |
| 573 } | |
| 574 | |
| 575 static object * | |
| 86 | 576 int_pos(v) |
| 6 | 577 intobject *v; |
| 578 { | |
| 579 INCREF(v); | |
| 580 return (object *)v; | |
| 581 } | |
| 582 | |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
583 static object * |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
584 int_abs(v) |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
585 intobject *v; |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
586 { |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
587 if (v->ob_ival >= 0) |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
588 return int_pos(v); |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
589 else |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
590 return int_neg(v); |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
591 } |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
592 |
| 240 | 593 static int |
| 594 int_nonzero(v) | |
| 595 intobject *v; | |
| 596 { | |
| 597 return v->ob_ival != 0; | |
| 598 } | |
| 599 | |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
600 static object * |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
601 int_invert(v) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
602 intobject *v; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
603 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
604 return newintobject(~v->ob_ival); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
605 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
606 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
607 static object * |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
608 int_lshift(v, w) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
609 intobject *v; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
610 intobject *w; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
611 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
612 register long a, b; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
613 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
614 b = w->ob_ival; |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
615 if (b < 0) { |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
616 err_setstr(ValueError, "negative shift count"); |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
617 return NULL; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
618 } |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
619 if (a == 0 || b == 0) { |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
620 INCREF(v); |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
621 return (object *) v; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
622 } |
|
1365
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
623 if (b >= LONG_BIT) { |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
624 return newintobject(0L); |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
625 } |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
626 a = (unsigned long)a << b; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
627 return newintobject(a); |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
628 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
629 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
630 static object * |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
631 int_rshift(v, w) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
632 intobject *v; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
633 intobject *w; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
634 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
635 register long a, b; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
636 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
637 b = w->ob_ival; |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
638 if (b < 0) { |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
639 err_setstr(ValueError, "negative shift count"); |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
640 return NULL; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
641 } |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
642 if (a == 0 || b == 0) { |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
643 INCREF(v); |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
644 return (object *) v; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
645 } |
|
1365
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
646 if (b >= LONG_BIT) { |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
647 if (a < 0) |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
648 a = -1; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
649 else |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
650 a = 0; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
651 } |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
652 else { |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
653 if (a < 0) |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
654 a = ~( ~(unsigned long)a >> b ); |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
655 else |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
656 a = (unsigned long)a >> b; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
657 } |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
658 return newintobject(a); |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
659 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
660 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
661 static object * |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
662 int_and(v, w) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
663 intobject *v; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
664 intobject *w; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
665 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
666 register long a, b; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
667 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
668 b = w->ob_ival; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
669 return newintobject(a & b); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
670 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
671 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
672 static object * |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
673 int_xor(v, w) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
674 intobject *v; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
675 intobject *w; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
676 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
677 register long a, b; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
678 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
679 b = w->ob_ival; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
680 return newintobject(a ^ b); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
681 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
682 |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
683 static object * |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
684 int_or(v, w) |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
685 intobject *v; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
686 intobject *w; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
687 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
688 register long a, b; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
689 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
690 b = w->ob_ival; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
691 return newintobject(a | b); |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
692 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
693 |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
694 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
695 int_int(v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
696 intobject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
697 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
698 INCREF(v); |
|
1271
2919b448e385
* Microscopic corrections to make things compile on the Cray APP.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
699 return (object *)v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
700 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
701 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
702 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
703 int_long(v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
704 intobject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
705 { |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
706 return newlongobject((v -> ob_ival)); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
707 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
708 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
709 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
710 int_float(v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
711 intobject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
712 { |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
713 return newfloatobject((double)(v -> ob_ival)); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
714 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
715 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
716 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
717 int_oct(v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
718 intobject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
719 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
720 char buf[20]; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
721 long x = v -> ob_ival; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
722 if (x == 0) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
723 strcpy(buf, "0"); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
724 else if (x > 0) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
725 sprintf(buf, "0%lo", x); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
726 else |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
727 sprintf(buf, "-0%lo", -x); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
728 return newstringobject(buf); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
729 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
730 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
731 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
732 int_hex(v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
733 intobject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
734 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
735 char buf[20]; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
736 long x = v -> ob_ival; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
737 if (x >= 0) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
738 sprintf(buf, "0x%lx", x); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
739 else |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
740 sprintf(buf, "-0x%lx", -x); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
741 return newstringobject(buf); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
742 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
743 |
| 6 | 744 static number_methods int_as_number = { |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
745 (binaryfunc)int_add, /*nb_add*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
746 (binaryfunc)int_sub, /*nb_subtract*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
747 (binaryfunc)int_mul, /*nb_multiply*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
748 (binaryfunc)int_div, /*nb_divide*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
749 (binaryfunc)int_mod, /*nb_remainder*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
750 (binaryfunc)int_divmod, /*nb_divmod*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
751 (ternaryfunc)int_pow, /*nb_power*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
752 (unaryfunc)int_neg, /*nb_negative*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
753 (unaryfunc)int_pos, /*nb_positive*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
754 (unaryfunc)int_abs, /*nb_absolute*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
755 (inquiry)int_nonzero, /*nb_nonzero*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
756 (unaryfunc)int_invert, /*nb_invert*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
757 (binaryfunc)int_lshift, /*nb_lshift*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
758 (binaryfunc)int_rshift, /*nb_rshift*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
759 (binaryfunc)int_and, /*nb_and*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
760 (binaryfunc)int_xor, /*nb_xor*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
761 (binaryfunc)int_or, /*nb_or*/ |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
762 0, /*nb_coerce*/ |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
763 (unaryfunc)int_int, /*nb_int*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
764 (unaryfunc)int_long, /*nb_long*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
765 (unaryfunc)int_float, /*nb_float*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
766 (unaryfunc)int_oct, /*nb_oct*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
767 (unaryfunc)int_hex, /*nb_hex*/ |
| 6 | 768 }; |
| 769 | |
| 770 typeobject Inttype = { | |
| 771 OB_HEAD_INIT(&Typetype) | |
| 772 0, | |
| 773 "int", | |
| 774 sizeof(intobject), | |
| 775 0, | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
776 (destructor)int_dealloc, /*tp_dealloc*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
777 (printfunc)int_print, /*tp_print*/ |
| 6 | 778 0, /*tp_getattr*/ |
| 779 0, /*tp_setattr*/ | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
780 (cmpfunc)int_compare, /*tp_compare*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
781 (reprfunc)int_repr, /*tp_repr*/ |
| 6 | 782 &int_as_number, /*tp_as_number*/ |
| 783 0, /*tp_as_sequence*/ | |
| 784 0, /*tp_as_mapping*/ | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
785 (hashfunc)int_hash, /*tp_hash*/ |
| 6 | 786 }; |
