Mercurial > cpython-lru_cache-weakref
annotate Objects/intobject.c @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | d1447950fa04 |
| children | 80d24364b932 |
| 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 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
34 #include "Python.h" |
| 6 | 35 |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
36 #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
|
37 #include <limits.h> |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
38 #endif |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
39 |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
40 #ifndef LONG_MAX |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
41 #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
|
42 #endif |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
43 |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
44 #ifndef LONG_MIN |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
45 #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
|
46 #endif |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
47 |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
48 #ifndef CHAR_BIT |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
49 #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
|
50 #endif |
|
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
51 |
|
1413
c914b10bf2ed
* timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents:
1365
diff
changeset
|
52 #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
|
53 #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
|
54 #endif |
|
1365
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
55 |
|
1448
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
56 long |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
57 PyInt_GetMax() |
|
1448
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
58 { |
|
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
59 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
|
60 } |
|
328f446d2016
Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents:
1413
diff
changeset
|
61 |
| 6 | 62 /* Standard Booleans */ |
| 86 | 63 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
64 PyIntObject _Py_ZeroStruct = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
65 PyObject_HEAD_INIT(&PyInt_Type) |
| 6 | 66 0 |
| 67 }; | |
| 86 | 68 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
69 PyIntObject _Py_TrueStruct = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
70 PyObject_HEAD_INIT(&PyInt_Type) |
| 6 | 71 1 |
| 72 }; | |
| 73 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
74 static PyObject * |
| 471 | 75 err_ovf(msg) |
| 76 char *msg; | |
| 11 | 77 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
78 PyErr_SetString(PyExc_OverflowError, msg); |
| 11 | 79 return NULL; |
| 80 } | |
| 81 | |
| 86 | 82 /* Integers are quite normal objects, to make object handling uniform. |
| 83 (Using odd pointers to represent integers would save much space | |
| 84 but require extra checks for this special case throughout the code.) | |
| 85 Since, a typical Python program spends much of its time allocating | |
| 86 and deallocating integers, these operations should be very fast. | |
| 87 Therefore we use a dedicated allocation scheme with a much lower | |
| 88 overhead (in space and time) than straight malloc(): a simple | |
| 89 dedicated free list, filled when necessary with memory from malloc(). | |
| 90 */ | |
| 91 | |
| 92 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
93 #define N_INTOBJECTS (BLOCK_SIZE / sizeof(PyIntObject)) |
| 86 | 94 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
95 static PyIntObject * |
| 86 | 96 fill_free_list() |
| 97 { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
98 PyIntObject *p, *q; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
99 p = PyMem_NEW(PyIntObject, N_INTOBJECTS); |
| 86 | 100 if (p == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
101 return (PyIntObject *)PyErr_NoMemory(); |
| 86 | 102 q = p + N_INTOBJECTS; |
| 103 while (--q > p) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
104 *(PyIntObject **)q = q-1; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
105 *(PyIntObject **)q = NULL; |
| 86 | 106 return p + N_INTOBJECTS - 1; |
| 107 } | |
| 108 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
109 static PyIntObject *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
|
110 #ifndef NSMALLPOSINTS |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
111 #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
|
112 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
113 #ifndef NSMALLNEGINTS |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
114 #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
|
115 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
116 #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
|
117 /* 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
|
118 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
|
119 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
|
120 -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
|
121 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
122 static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
123 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
124 #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
|
125 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
|
126 #endif |
| 86 | 127 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
128 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
129 PyInt_FromLong(ival) |
| 6 | 130 long ival; |
| 131 { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
132 register PyIntObject *v; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
133 #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
|
134 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
|
135 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
136 Py_INCREF(v); |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
137 #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
|
138 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
|
139 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
|
140 else |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
141 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
|
142 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
143 return (PyObject *) v; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
144 } |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
145 #endif |
| 86 | 146 if (free_list == NULL) { |
| 147 if ((free_list = fill_free_list()) == NULL) | |
| 148 return NULL; | |
| 6 | 149 } |
| 86 | 150 v = free_list; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
151 free_list = *(PyIntObject **)free_list; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
152 v->ob_type = &PyInt_Type; |
| 86 | 153 v->ob_ival = ival; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
154 _Py_NewReference(v); |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
155 #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
|
156 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
|
157 /* save this one for a following allocation */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
158 Py_INCREF(v); |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
159 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
|
160 } |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
161 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
162 return (PyObject *) v; |
| 86 | 163 } |
| 164 | |
| 165 static void | |
| 166 int_dealloc(v) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
167 PyIntObject *v; |
| 86 | 168 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
169 *(PyIntObject **)v = free_list; |
| 86 | 170 free_list = v; |
| 6 | 171 } |
| 172 | |
| 173 long | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
174 PyInt_AsLong(op) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
175 register PyObject *op; |
| 6 | 176 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
177 PyNumberMethods *nb; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
178 PyIntObject *io; |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
179 long val; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
180 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
181 if (op && PyInt_Check(op)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
182 return PyInt_AS_LONG((PyIntObject*) op); |
|
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 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
|
185 nb->nb_int == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
186 PyErr_BadArgument(); |
| 6 | 187 return -1; |
| 188 } | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
189 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
190 io = (PyIntObject*) (*nb->nb_int) (op); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
191 if (io == NULL) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
192 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
193 if (!PyInt_Check(io)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
194 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
195 "nb_int should return int object"); |
|
1838
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
199 val = PyInt_AS_LONG(io); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
200 Py_DECREF(io); |
|
1838
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) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
210 PyIntObject *v; |
| 6 | 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 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
218 static PyObject * |
| 86 | 219 int_repr(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
220 PyIntObject *v; |
| 6 | 221 { |
| 222 char buf[20]; | |
| 223 sprintf(buf, "%ld", v->ob_ival); | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
224 return PyString_FromString(buf); |
| 6 | 225 } |
| 226 | |
| 227 static int | |
| 86 | 228 int_compare(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
229 PyIntObject *v, *w; |
| 6 | 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) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
238 PyIntObject *v; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
239 { |
|
4903
6a629d278a18
Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents:
4606
diff
changeset
|
240 /* XXX If this is changed, you also need to change the way |
|
6a629d278a18
Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents:
4606
diff
changeset
|
241 Python's long, float and complex types are hashed. */ |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
242 long x = v -> ob_ival; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
243 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
244 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
245 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
246 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
247 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
248 static PyObject * |
| 86 | 249 int_add(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
250 PyIntObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
251 PyIntObject *w; |
| 6 | 252 { |
| 253 register long a, b, x; | |
| 254 a = v->ob_ival; | |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
255 b = w->ob_ival; |
| 6 | 256 x = a + b; |
| 11 | 257 if ((x^a) < 0 && (x^b) < 0) |
| 471 | 258 return err_ovf("integer addition"); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
259 return PyInt_FromLong(x); |
| 6 | 260 } |
| 261 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
262 static PyObject * |
| 86 | 263 int_sub(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
264 PyIntObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
265 PyIntObject *w; |
| 6 | 266 { |
| 267 register long a, b, x; | |
| 268 a = v->ob_ival; | |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
269 b = w->ob_ival; |
| 6 | 270 x = a - b; |
| 11 | 271 if ((x^a) < 0 && (x^~b) < 0) |
| 471 | 272 return err_ovf("integer subtraction"); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
273 return PyInt_FromLong(x); |
| 6 | 274 } |
| 275 | |
|
1838
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 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
|
278 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
|
279 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
|
280 the following algorithm: |
|
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 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
|
283 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
|
284 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
|
285 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
286 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
|
287 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
288 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
|
289 negative |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
290 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
291 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
|
292 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
|
293 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
|
294 it's negative |
|
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 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
|
297 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
298 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
|
299 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
|
300 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
|
301 guess the above is the preferred solution. |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
302 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
303 */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
304 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
305 static PyObject * |
| 86 | 306 int_mul(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
307 PyIntObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
308 PyIntObject *w; |
| 6 | 309 { |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
310 long a, b, ah, bh, x, y; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
311 int s = 1; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
312 |
| 6 | 313 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
314 b = w->ob_ival; |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
315 ah = a >> (LONG_BIT/2); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
316 bh = b >> (LONG_BIT/2); |
|
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 /* 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
|
319 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
320 if (ah == 0 && bh == 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
321 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
322 if (x < 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
323 goto bad; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
324 return PyInt_FromLong(x); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
325 } |
|
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 /* Arrange that a >= b >= 0 */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
328 |
|
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 a = -a; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
331 if (a < 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
332 /* Largest negative */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
333 if (b == 0 || b == 1) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
334 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
335 goto ok; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
336 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
337 else |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
338 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
339 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
340 s = -s; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
341 ah = a >> (LONG_BIT/2); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
342 } |
|
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 b = -b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
345 if (b < 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
346 /* Largest negative */ |
|
4606
739b8480f1ac
Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
347 if (a == 0 || (a == 1 && s == 1)) { |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
348 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
349 goto ok; |
|
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 else |
|
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 s = -s; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
355 bh = b >> (LONG_BIT/2); |
|
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 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
358 /* 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
|
359 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
360 if (ah != 0 && bh != 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
361 goto bad; |
|
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 /* 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
|
364 overflow if it comes out negative */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
365 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
366 if (ah == 0 && bh == 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
367 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
368 if (x < 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
369 goto bad; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
370 return PyInt_FromLong(x*s); |
|
1838
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 if (a < b) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
374 /* Swap */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
375 x = a; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
376 a = b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
377 b = x; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
378 ah = bh; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
379 /* bh not used beyond this point */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
380 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
381 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
382 /* 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
|
383 it's >= 2^31 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
384 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
|
385 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
|
386 it's negative |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
387 (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
|
388 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
389 y = ah*b; |
|
4903
6a629d278a18
Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents:
4606
diff
changeset
|
390 if (y >= (1L << (LONG_BIT/2 - 1))) |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
391 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
392 a &= (1L << (LONG_BIT/2)) - 1; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
393 x = a*b; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
394 if (x < 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
395 goto bad; |
|
4903
6a629d278a18
Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents:
4606
diff
changeset
|
396 x += y << (LONG_BIT/2); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
397 if (x < 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
398 goto bad; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
399 ok: |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
400 return PyInt_FromLong(x * s); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
401 |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
402 bad: |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
403 return err_ovf("integer multiplication"); |
| 6 | 404 } |
| 405 | |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
406 static int |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
407 i_divmod(x, y, p_xdivy, p_xmody) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
408 register PyIntObject *x, *y; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
409 long *p_xdivy, *p_xmody; |
| 6 | 410 { |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
411 long xi = x->ob_ival; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
412 long yi = y->ob_ival; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
413 long xdivy, xmody; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
414 |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
415 if (yi == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
416 PyErr_SetString(PyExc_ZeroDivisionError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
417 "integer division or modulo"); |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
418 return -1; |
| 6 | 419 } |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
420 if (yi < 0) { |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
421 if (xi < 0) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
422 xdivy = -xi / -yi; |
|
409
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 xdivy = - (xi / -yi); |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
425 } |
|
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 if (xi < 0) |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
428 xdivy = - (-xi / yi); |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
429 else |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
430 xdivy = xi / yi; |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
431 } |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
432 xmody = xi - xdivy*yi; |
|
4606
739b8480f1ac
Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
433 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) { |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
434 xmody += yi; |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
435 xdivy -= 1; |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
436 } |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
437 *p_xdivy = xdivy; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
438 *p_xmody = xmody; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
439 return 0; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
440 } |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
441 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
442 static PyObject * |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
443 int_div(x, y) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
444 PyIntObject *x; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
445 PyIntObject *y; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
446 { |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
447 long d, m; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
448 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
|
449 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
450 return PyInt_FromLong(d); |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
451 } |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
452 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
453 static PyObject * |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
454 int_mod(x, y) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
455 PyIntObject *x; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
456 PyIntObject *y; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
457 { |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
458 long d, m; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
459 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
|
460 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
461 return PyInt_FromLong(m); |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
462 } |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
463 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
464 static PyObject * |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
465 int_divmod(x, y) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
466 PyIntObject *x; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
467 PyIntObject *y; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
468 { |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
469 long d, m; |
|
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
470 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
|
471 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
472 return Py_BuildValue("(ll)", d, m); |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
473 } |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
474 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
475 static PyObject * |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
476 int_pow(v, w, z) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
477 PyIntObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
478 PyIntObject *w; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
479 PyIntObject *z; |
| 6 | 480 { |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
481 #if 1 |
|
4606
739b8480f1ac
Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
482 register long iv, iw, iz=0, ix, temp, prev; |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
483 iv = v->ob_ival; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
484 iw = w->ob_ival; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
485 if (iw < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
486 PyErr_SetString(PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
487 "integer to the negative power"); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
488 return NULL; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
489 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
490 if ((PyObject *)z != Py_None) { |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
491 iz = z->ob_ival; |
|
4606
739b8480f1ac
Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
492 if (iz == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
493 PyErr_SetString(PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
494 "pow(x, y, z) with z==0"); |
|
4606
739b8480f1ac
Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
495 return NULL; |
|
739b8480f1ac
Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
496 } |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
497 } |
|
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 * XXX: The original exponentiation code stopped looping |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
500 * 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
|
501 * 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
|
502 * 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
|
503 * will compensate for the slight inefficiency. |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
504 * 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
|
505 */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
506 temp = iv; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
507 ix = 1; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
508 while (iw > 0) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
509 prev = ix; /* Save value for overflow check */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
510 if (iw & 1) { |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
511 ix = ix*temp; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
512 if (temp == 0) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
513 break; /* Avoid ix / 0 */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
514 if (ix / temp != 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 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
517 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
|
518 if (iw==0) break; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
519 prev = temp; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
520 temp *= temp; /* Square the value of temp */ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
521 if (prev!=0 && temp/prev!=prev) |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
522 return err_ovf("integer pow()"); |
|
4606
739b8480f1ac
Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
523 if (iz) { |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
524 /* 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
|
525 ix = ix % iz; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
526 temp = temp % iz; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
527 } |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
528 } |
|
4606
739b8480f1ac
Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
529 if (iz) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
530 PyObject *t1, *t2; |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
531 long int div, mod; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
532 t1=PyInt_FromLong(ix); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
533 t2=PyInt_FromLong(iz); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
534 if (t1==NULL || t2==NULL || |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
535 i_divmod((PyIntObject *)t1, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
536 (PyIntObject *)t2, &div, &mod)<0) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
537 { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
538 Py_XDECREF(t1); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
539 Py_XDECREF(t2); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
540 return(NULL); |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
541 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
542 Py_DECREF(t1); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
543 Py_DECREF(t2); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
544 ix=mod; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
545 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
546 return PyInt_FromLong(ix); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
547 #else |
| 6 | 548 register long iv, iw, ix; |
| 549 iv = v->ob_ival; | |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
550 iw = w->ob_ival; |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
551 if (iw < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
552 PyErr_SetString(PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
553 "integer to the negative power"); |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
554 return NULL; |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
555 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
556 if ((PyObject *)z != Py_None) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
557 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
558 "pow(int, int, int) not yet supported"); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
559 return NULL; |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
560 } |
| 6 | 561 ix = 1; |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
562 while (--iw >= 0) { |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
563 long prev = ix; |
| 6 | 564 ix = ix * iv; |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
565 if (iv == 0) |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
566 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
|
567 if (ix / iv != prev) |
| 471 | 568 return err_ovf("integer pow()"); |
| 6 | 569 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
570 return PyInt_FromLong(ix); |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
571 #endif |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
572 } |
| 6 | 573 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
574 static PyObject * |
| 86 | 575 int_neg(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
576 PyIntObject *v; |
| 6 | 577 { |
| 578 register long a, x; | |
| 579 a = v->ob_ival; | |
| 580 x = -a; | |
| 11 | 581 if (a < 0 && x < 0) |
| 471 | 582 return err_ovf("integer negation"); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
583 return PyInt_FromLong(x); |
| 6 | 584 } |
| 585 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
586 static PyObject * |
| 86 | 587 int_pos(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
588 PyIntObject *v; |
| 6 | 589 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
590 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
591 return (PyObject *)v; |
| 6 | 592 } |
| 593 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
594 static PyObject * |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
595 int_abs(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
596 PyIntObject *v; |
|
229
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
597 { |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
598 if (v->ob_ival >= 0) |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
599 return int_pos(v); |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
600 else |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
601 return int_neg(v); |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
602 } |
|
08597e2b63d3
Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
603 |
| 240 | 604 static int |
| 605 int_nonzero(v) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
606 PyIntObject *v; |
| 240 | 607 { |
| 608 return v->ob_ival != 0; | |
| 609 } | |
| 610 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
611 static PyObject * |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
612 int_invert(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
613 PyIntObject *v; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
614 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
615 return PyInt_FromLong(~v->ob_ival); |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
616 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
617 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
618 static PyObject * |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
619 int_lshift(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
620 PyIntObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
621 PyIntObject *w; |
|
409
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 register long a, b; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
624 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
625 b = w->ob_ival; |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
626 if (b < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
627 PyErr_SetString(PyExc_ValueError, "negative shift count"); |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
628 return NULL; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
629 } |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
630 if (a == 0 || b == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
631 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
632 return (PyObject *) v; |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
633 } |
|
1365
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
634 if (b >= LONG_BIT) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
635 return PyInt_FromLong(0L); |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
636 } |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
637 a = (unsigned long)a << b; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
638 return PyInt_FromLong(a); |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
639 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
640 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
641 static PyObject * |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
642 int_rshift(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
643 PyIntObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
644 PyIntObject *w; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
645 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
646 register long a, b; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
647 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
648 b = w->ob_ival; |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
649 if (b < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
650 PyErr_SetString(PyExc_ValueError, "negative shift count"); |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
651 return NULL; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
652 } |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
653 if (a == 0 || b == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
654 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
655 return (PyObject *) v; |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
656 } |
|
1365
a5fc104849aa
Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
657 if (b >= LONG_BIT) { |
|
589
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
658 if (a < 0) |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
659 a = -1; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
660 else |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
661 a = 0; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
662 } |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
663 else { |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
664 if (a < 0) |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
665 a = ~( ~(unsigned long)a >> b ); |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
666 else |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
667 a = (unsigned long)a >> b; |
|
6077704dab7a
Different shift implementation.
Guido van Rossum <guido@python.org>
parents:
471
diff
changeset
|
668 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
669 return PyInt_FromLong(a); |
|
409
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
672 static PyObject * |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
673 int_and(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
674 PyIntObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
675 PyIntObject *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; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
680 return PyInt_FromLong(a & b); |
|
409
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
683 static PyObject * |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
684 int_xor(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
685 PyIntObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
686 PyIntObject *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; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
691 return PyInt_FromLong(a ^ b); |
|
409
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
694 static PyObject * |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
695 int_or(v, w) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
696 PyIntObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
697 PyIntObject *w; |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
698 { |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
699 register long a, b; |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
700 a = v->ob_ival; |
|
610
dcc6de1c37a4
Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents:
589
diff
changeset
|
701 b = w->ob_ival; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
702 return PyInt_FromLong(a | b); |
|
409
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
703 } |
|
b5fe6482072f
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
704 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
705 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
706 int_int(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
707 PyIntObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
708 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
709 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
710 return (PyObject *)v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
711 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
712 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
713 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
714 int_long(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
715 PyIntObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
716 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
717 return PyLong_FromLong((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
|
718 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
719 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
720 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
721 int_float(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
722 PyIntObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
723 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
724 return PyFloat_FromDouble((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
|
725 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
726 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
727 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
728 int_oct(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
729 PyIntObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
730 { |
|
4952
05fd432d9e2b
Increased buffer sizes used by hex() and oct() -- on 64-bit or 128-bit
Guido van Rossum <guido@python.org>
parents:
4937
diff
changeset
|
731 char buf[100]; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
732 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
|
733 if (x == 0) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
734 strcpy(buf, "0"); |
|
4937
789de7c66712
Changed hex() and oct() again, to never emit a '-' sign.
Guido van Rossum <guido@python.org>
parents:
4926
diff
changeset
|
735 else |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
736 sprintf(buf, "0%lo", x); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
737 return PyString_FromString(buf); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
738 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
739 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
740 static PyObject * |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
741 int_hex(v) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
742 PyIntObject *v; |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
743 { |
|
4952
05fd432d9e2b
Increased buffer sizes used by hex() and oct() -- on 64-bit or 128-bit
Guido van Rossum <guido@python.org>
parents:
4937
diff
changeset
|
744 char buf[100]; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
745 long x = v -> ob_ival; |
|
4937
789de7c66712
Changed hex() and oct() again, to never emit a '-' sign.
Guido van Rossum <guido@python.org>
parents:
4926
diff
changeset
|
746 sprintf(buf, "0x%lx", x); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
747 return PyString_FromString(buf); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
748 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
749 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
750 static PyNumberMethods int_as_number = { |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
751 (binaryfunc)int_add, /*nb_add*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
752 (binaryfunc)int_sub, /*nb_subtract*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
753 (binaryfunc)int_mul, /*nb_multiply*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
754 (binaryfunc)int_div, /*nb_divide*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
755 (binaryfunc)int_mod, /*nb_remainder*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
756 (binaryfunc)int_divmod, /*nb_divmod*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
757 (ternaryfunc)int_pow, /*nb_power*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
758 (unaryfunc)int_neg, /*nb_negative*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
759 (unaryfunc)int_pos, /*nb_positive*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
760 (unaryfunc)int_abs, /*nb_absolute*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
761 (inquiry)int_nonzero, /*nb_nonzero*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
762 (unaryfunc)int_invert, /*nb_invert*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
763 (binaryfunc)int_lshift, /*nb_lshift*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
764 (binaryfunc)int_rshift, /*nb_rshift*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
765 (binaryfunc)int_and, /*nb_and*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
766 (binaryfunc)int_xor, /*nb_xor*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
767 (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
|
768 0, /*nb_coerce*/ |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
769 (unaryfunc)int_int, /*nb_int*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
770 (unaryfunc)int_long, /*nb_long*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
771 (unaryfunc)int_float, /*nb_float*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
772 (unaryfunc)int_oct, /*nb_oct*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
773 (unaryfunc)int_hex, /*nb_hex*/ |
| 6 | 774 }; |
| 775 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
776 PyTypeObject PyInt_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
777 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 778 0, |
| 779 "int", | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4952
diff
changeset
|
780 sizeof(PyIntObject), |
| 6 | 781 0, |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
782 (destructor)int_dealloc, /*tp_dealloc*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
783 (printfunc)int_print, /*tp_print*/ |
| 6 | 784 0, /*tp_getattr*/ |
| 785 0, /*tp_setattr*/ | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
786 (cmpfunc)int_compare, /*tp_compare*/ |
|
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
787 (reprfunc)int_repr, /*tp_repr*/ |
| 6 | 788 &int_as_number, /*tp_as_number*/ |
| 789 0, /*tp_as_sequence*/ | |
| 790 0, /*tp_as_mapping*/ | |
|
1838
5f6acd1ab5c5
mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents:
1448
diff
changeset
|
791 (hashfunc)int_hash, /*tp_hash*/ |
| 6 | 792 }; |
