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