-
-
Notifications
You must be signed in to change notification settings - Fork 29.8k
/
intobject.c
1576 lines (1428 loc) · 45.5 KB
/
intobject.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Integer object implementation */
#include "Python.h"
#include <ctype.h>
#include <float.h>
static PyObject *int_int(PyIntObject *v);
long
PyInt_GetMax(void)
{
return LONG_MAX; /* To initialize sys.maxint */
}
/* Integers are quite normal objects, to make object handling uniform.
(Using odd pointers to represent integers would save much space
but require extra checks for this special case throughout the code.)
Since a typical Python program spends much of its time allocating
and deallocating integers, these operations should be very fast.
Therefore we use a dedicated allocation scheme with a much lower
overhead (in space and time) than straight malloc(): a simple
dedicated free list, filled when necessary with memory from malloc().
block_list is a singly-linked list of all PyIntBlocks ever allocated,
linked via their next members. PyIntBlocks are never returned to the
system before shutdown (PyInt_Fini).
free_list is a singly-linked list of available PyIntObjects, linked
via abuse of their ob_type members.
*/
#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
struct _intblock {
struct _intblock *next;
PyIntObject objects[N_INTOBJECTS];
};
typedef struct _intblock PyIntBlock;
static PyIntBlock *block_list = NULL;
static PyIntObject *free_list = NULL;
static PyIntObject *
fill_free_list(void)
{
PyIntObject *p, *q;
/* Python's object allocator isn't appropriate for large blocks. */
p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
if (p == NULL)
return (PyIntObject *) PyErr_NoMemory();
((PyIntBlock *)p)->next = block_list;
block_list = (PyIntBlock *)p;
/* Link the int objects together, from rear to front, then return
the address of the last int object in the block. */
p = &((PyIntBlock *)p)->objects[0];
q = p + N_INTOBJECTS;
while (--q > p)
Py_TYPE(q) = (struct _typeobject *)(q-1);
Py_TYPE(q) = NULL;
return p + N_INTOBJECTS - 1;
}
#ifndef NSMALLPOSINTS
#define NSMALLPOSINTS 257
#endif
#ifndef NSMALLNEGINTS
#define NSMALLNEGINTS 5
#endif
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
/* References to small integers are saved in this array so that they
can be shared.
The integers that are saved are those in the range
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
#endif
#ifdef COUNT_ALLOCS
Py_ssize_t quick_int_allocs;
Py_ssize_t quick_neg_int_allocs;
#endif
PyObject *
PyInt_FromLong(long ival)
{
register PyIntObject *v;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
v = small_ints[ival + NSMALLNEGINTS];
Py_INCREF(v);
#ifdef COUNT_ALLOCS
if (ival >= 0)
quick_int_allocs++;
else
quick_neg_int_allocs++;
#endif
return (PyObject *) v;
}
#endif
if (free_list == NULL) {
if ((free_list = fill_free_list()) == NULL)
return NULL;
}
/* Inline PyObject_New */
v = free_list;
free_list = (PyIntObject *)Py_TYPE(v);
(void)PyObject_INIT(v, &PyInt_Type);
v->ob_ival = ival;
return (PyObject *) v;
}
PyObject *
PyInt_FromSize_t(size_t ival)
{
if (ival <= LONG_MAX)
return PyInt_FromLong((long)ival);
return _PyLong_FromSize_t(ival);
}
PyObject *
PyInt_FromSsize_t(Py_ssize_t ival)
{
if (ival >= LONG_MIN && ival <= LONG_MAX)
return PyInt_FromLong((long)ival);
return _PyLong_FromSsize_t(ival);
}
static void
int_dealloc(PyIntObject *v)
{
if (PyInt_CheckExact(v)) {
Py_TYPE(v) = (struct _typeobject *)free_list;
free_list = v;
}
else
Py_TYPE(v)->tp_free((PyObject *)v);
}
long
PyInt_AsLong(register PyObject *op)
{
PyNumberMethods *nb;
PyIntObject *io;
long val;
if (op && PyInt_Check(op))
return PyInt_AS_LONG((PyIntObject*) op);
if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
nb->nb_int == NULL) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
return -1;
}
if (PyLong_CheckExact(op)) {
/* avoid creating temporary int object */
return PyLong_AsLong(op);
}
io = (PyIntObject*) (*nb->nb_int) (op);
if (io == NULL)
return -1;
if (!PyInt_Check(io)) {
if (PyLong_Check(io)) {
/* got a long? => retry int conversion */
val = PyLong_AsLong((PyObject *)io);
Py_DECREF(io);
return val;
}
else
{
Py_DECREF(io);
PyErr_SetString(PyExc_TypeError,
"__int__ method should return an integer");
return -1;
}
}
val = PyInt_AS_LONG(io);
Py_DECREF(io);
return val;
}
int
_PyInt_AsInt(PyObject *obj)
{
long result = PyInt_AsLong(obj);
if (result == -1 && PyErr_Occurred())
return -1;
if (result > INT_MAX || result < INT_MIN) {
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to C int");
return -1;
}
return (int)result;
}
Py_ssize_t
PyInt_AsSsize_t(register PyObject *op)
{
#if SIZEOF_SIZE_T != SIZEOF_LONG
PyNumberMethods *nb;
PyObject *io;
Py_ssize_t val;
#endif
if (op == NULL) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
return -1;
}
if (PyInt_Check(op))
return PyInt_AS_LONG((PyIntObject*) op);
if (PyLong_Check(op))
return _PyLong_AsSsize_t(op);
#if SIZEOF_SIZE_T == SIZEOF_LONG
return PyInt_AsLong(op);
#else
if ((nb = Py_TYPE(op)->tp_as_number) == NULL ||
(nb->nb_int == NULL && nb->nb_long == 0)) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
return -1;
}
if (nb->nb_long != 0)
io = (*nb->nb_long)(op);
else
io = (*nb->nb_int)(op);
if (io == NULL)
return -1;
if (!PyInt_Check(io)) {
if (PyLong_Check(io)) {
/* got a long? => retry int conversion */
val = _PyLong_AsSsize_t(io);
Py_DECREF(io);
if ((val == -1) && PyErr_Occurred())
return -1;
return val;
}
else
{
Py_DECREF(io);
PyErr_SetString(PyExc_TypeError,
"__int__ method should return an integer");
return -1;
}
}
val = PyInt_AS_LONG(io);
Py_DECREF(io);
return val;
#endif
}
unsigned long
PyInt_AsUnsignedLongMask(register PyObject *op)
{
PyNumberMethods *nb;
PyIntObject *io;
unsigned long val;
if (op && PyInt_Check(op))
return PyInt_AS_LONG((PyIntObject*) op);
if (op && PyLong_Check(op))
return PyLong_AsUnsignedLongMask(op);
if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
nb->nb_int == NULL) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
return (unsigned long)-1;
}
io = (PyIntObject*) (*nb->nb_int) (op);
if (io == NULL)
return (unsigned long)-1;
if (!PyInt_Check(io)) {
if (PyLong_Check(io)) {
val = PyLong_AsUnsignedLongMask((PyObject *)io);
Py_DECREF(io);
if (PyErr_Occurred())
return (unsigned long)-1;
return val;
}
else
{
Py_DECREF(io);
PyErr_SetString(PyExc_TypeError,
"__int__ method should return an integer");
return (unsigned long)-1;
}
}
val = PyInt_AS_LONG(io);
Py_DECREF(io);
return val;
}
#ifdef HAVE_LONG_LONG
unsigned PY_LONG_LONG
PyInt_AsUnsignedLongLongMask(register PyObject *op)
{
PyNumberMethods *nb;
PyIntObject *io;
unsigned PY_LONG_LONG val;
if (op && PyInt_Check(op))
return PyInt_AS_LONG((PyIntObject*) op);
if (op && PyLong_Check(op))
return PyLong_AsUnsignedLongLongMask(op);
if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
nb->nb_int == NULL) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
return (unsigned PY_LONG_LONG)-1;
}
io = (PyIntObject*) (*nb->nb_int) (op);
if (io == NULL)
return (unsigned PY_LONG_LONG)-1;
if (!PyInt_Check(io)) {
if (PyLong_Check(io)) {
val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
Py_DECREF(io);
if (PyErr_Occurred())
return (unsigned PY_LONG_LONG)-1;
return val;
}
else
{
Py_DECREF(io);
PyErr_SetString(PyExc_TypeError,
"__int__ method should return an integer");
return (unsigned PY_LONG_LONG)-1;
}
}
val = PyInt_AS_LONG(io);
Py_DECREF(io);
return val;
}
#endif
PyObject *
PyInt_FromString(char *s, char **pend, int base)
{
char *end;
long x;
Py_ssize_t slen;
PyObject *sobj, *srepr;
if ((base != 0 && base < 2) || base > 36) {
PyErr_SetString(PyExc_ValueError,
"int() base must be >= 2 and <= 36, or 0");
return NULL;
}
while (*s && isspace(Py_CHARMASK(*s)))
s++;
errno = 0;
if (base == 0 && s[0] == '0') {
x = (long) PyOS_strtoul(s, &end, base);
if (x < 0)
return PyLong_FromString(s, pend, base);
}
else
x = PyOS_strtol(s, &end, base);
if (end == s || !isalnum(Py_CHARMASK(end[-1])))
goto bad;
while (*end && isspace(Py_CHARMASK(*end)))
end++;
if (*end != '\0') {
bad:
slen = strlen(s) < 200 ? strlen(s) : 200;
sobj = PyString_FromStringAndSize(s, slen);
if (sobj == NULL)
return NULL;
srepr = PyObject_Repr(sobj);
Py_DECREF(sobj);
if (srepr == NULL)
return NULL;
PyErr_Format(PyExc_ValueError,
"invalid literal for int() with base %d: %s",
base, PyString_AS_STRING(srepr));
Py_DECREF(srepr);
return NULL;
}
else if (errno != 0)
return PyLong_FromString(s, pend, base);
if (pend)
*pend = end;
return PyInt_FromLong(x);
}
#ifdef Py_USING_UNICODE
PyObject *
PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
{
PyObject *result;
char *buffer = (char *)PyMem_MALLOC(length+1);
if (buffer == NULL)
return PyErr_NoMemory();
if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
PyMem_FREE(buffer);
return NULL;
}
result = PyInt_FromString(buffer, NULL, base);
PyMem_FREE(buffer);
return result;
}
#endif
/* Methods */
/* Integers are seen as the "smallest" of all numeric types and thus
don't have any knowledge about conversion of other types to
integers. */
#define CONVERT_TO_LONG(obj, lng) \
if (PyInt_Check(obj)) { \
lng = PyInt_AS_LONG(obj); \
} \
else { \
Py_INCREF(Py_NotImplemented); \
return Py_NotImplemented; \
}
/* ARGSUSED */
static int
int_print(PyIntObject *v, FILE *fp, int flags)
/* flags -- not used but required by interface */
{
long int_val = v->ob_ival;
Py_BEGIN_ALLOW_THREADS
fprintf(fp, "%ld", int_val);
Py_END_ALLOW_THREADS
return 0;
}
static int
int_compare(PyIntObject *v, PyIntObject *w)
{
register long i = v->ob_ival;
register long j = w->ob_ival;
return (i < j) ? -1 : (i > j) ? 1 : 0;
}
static long
int_hash(PyIntObject *v)
{
/* XXX If this is changed, you also need to change the way
Python's long, float and complex types are hashed. */
long x = v -> ob_ival;
if (x == -1)
x = -2;
return x;
}
static PyObject *
int_add(PyIntObject *v, PyIntObject *w)
{
register long a, b, x;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
/* casts in the line below avoid undefined behaviour on overflow */
x = (long)((unsigned long)a + b);
if ((x^a) >= 0 || (x^b) >= 0)
return PyInt_FromLong(x);
return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
}
static PyObject *
int_sub(PyIntObject *v, PyIntObject *w)
{
register long a, b, x;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
/* casts in the line below avoid undefined behaviour on overflow */
x = (long)((unsigned long)a - b);
if ((x^a) >= 0 || (x^~b) >= 0)
return PyInt_FromLong(x);
return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
(PyObject *)w);
}
/*
Integer overflow checking for * is painful: Python tried a couple ways, but
they didn't work on all platforms, or failed in endcases (a product of
-sys.maxint-1 has been a particular pain).
Here's another way:
The native long product x*y is either exactly right or *way* off, being
just the last n bits of the true product, where n is the number of bits
in a long (the delivered product is the true product plus i*2**n for
some integer i).
The native double product (double)x * (double)y is subject to three
rounding errors: on a sizeof(long)==8 box, each cast to double can lose
info, and even on a sizeof(long)==4 box, the multiplication can lose info.
But, unlike the native long product, it's not in *range* trouble: even
if sizeof(long)==32 (256-bit longs), the product easily fits in the
dynamic range of a double. So the leading 50 (or so) bits of the double
product are correct.
We check these two ways against each other, and declare victory if they're
approximately the same. Else, because the native long product is the only
one that can lose catastrophic amounts of information, it's the native long
product that must have overflowed.
*/
static PyObject *
int_mul(PyObject *v, PyObject *w)
{
long a, b;
long longprod; /* a*b in native long arithmetic */
double doubled_longprod; /* (double)longprod */
double doubleprod; /* (double)a * (double)b */
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
/* casts in the next line avoid undefined behaviour on overflow */
longprod = (long)((unsigned long)a * b);
doubleprod = (double)a * (double)b;
doubled_longprod = (double)longprod;
/* Fast path for normal case: small multiplicands, and no info
is lost in either method. */
if (doubled_longprod == doubleprod)
return PyInt_FromLong(longprod);
/* Somebody somewhere lost info. Close enough, or way off? Note
that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
The difference either is or isn't significant compared to the
true value (of which doubleprod is a good approximation).
*/
{
const double diff = doubled_longprod - doubleprod;
const double absdiff = diff >= 0.0 ? diff : -diff;
const double absprod = doubleprod >= 0.0 ? doubleprod :
-doubleprod;
/* absdiff/absprod <= 1/32 iff
32 * absdiff <= absprod -- 5 good bits is "close enough" */
if (32.0 * absdiff <= absprod)
return PyInt_FromLong(longprod);
else
return PyLong_Type.tp_as_number->nb_multiply(v, w);
}
}
/* Integer overflow checking for unary negation: on a 2's-complement
* box, -x overflows iff x is the most negative long. In this case we
* get -x == x. However, -x is undefined (by C) if x /is/ the most
* negative long (it's a signed overflow case), and some compilers care.
* So we cast x to unsigned long first. However, then other compilers
* warn about applying unary minus to an unsigned operand. Hence the
* weird "0-".
*/
#define UNARY_NEG_WOULD_OVERFLOW(x) \
((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
/* Return type of i_divmod */
enum divmod_result {
DIVMOD_OK, /* Correct result */
DIVMOD_OVERFLOW, /* Overflow, try again using longs */
DIVMOD_ERROR /* Exception raised */
};
static enum divmod_result
i_divmod(register long x, register long y,
long *p_xdivy, long *p_xmody)
{
long xdivy, xmody;
if (y == 0) {
PyErr_SetString(PyExc_ZeroDivisionError,
"integer division or modulo by zero");
return DIVMOD_ERROR;
}
/* (-sys.maxint-1)/-1 is the only overflow case. */
if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
return DIVMOD_OVERFLOW;
xdivy = x / y;
/* xdiv*y can overflow on platforms where x/y gives floor(x/y)
* for x and y with differing signs. (This is unusual
* behaviour, and C99 prohibits it, but it's allowed by C89;
* for an example of overflow, take x = LONG_MIN, y = 5 or x =
* LONG_MAX, y = -5.) However, x - xdivy*y is always
* representable as a long, since it lies strictly between
* -abs(y) and abs(y). We add casts to avoid intermediate
* overflow.
*/
xmody = (long)(x - (unsigned long)xdivy * y);
/* If the signs of x and y differ, and the remainder is non-0,
* C89 doesn't define whether xdivy is now the floor or the
* ceiling of the infinitely precise quotient. We want the floor,
* and we have it iff the remainder's sign matches y's.
*/
if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
xmody += y;
--xdivy;
assert(xmody && ((y ^ xmody) >= 0));
}
*p_xdivy = xdivy;
*p_xmody = xmody;
return DIVMOD_OK;
}
static PyObject *
int_div(PyIntObject *x, PyIntObject *y)
{
long xi, yi;
long d, m;
CONVERT_TO_LONG(x, xi);
CONVERT_TO_LONG(y, yi);
switch (i_divmod(xi, yi, &d, &m)) {
case DIVMOD_OK:
return PyInt_FromLong(d);
case DIVMOD_OVERFLOW:
return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
(PyObject *)y);
default:
return NULL;
}
}
static PyObject *
int_classic_div(PyIntObject *x, PyIntObject *y)
{
long xi, yi;
long d, m;
CONVERT_TO_LONG(x, xi);
CONVERT_TO_LONG(y, yi);
if (Py_DivisionWarningFlag &&
PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
return NULL;
switch (i_divmod(xi, yi, &d, &m)) {
case DIVMOD_OK:
return PyInt_FromLong(d);
case DIVMOD_OVERFLOW:
return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
(PyObject *)y);
default:
return NULL;
}
}
static PyObject *
int_true_divide(PyIntObject *x, PyIntObject *y)
{
long xi, yi;
/* If they aren't both ints, give someone else a chance. In
particular, this lets int/long get handled by longs, which
underflows to 0 gracefully if the long is too big to convert
to float. */
CONVERT_TO_LONG(x, xi);
CONVERT_TO_LONG(y, yi);
if (yi == 0) {
PyErr_SetString(PyExc_ZeroDivisionError,
"division by zero");
return NULL;
}
if (xi == 0)
return PyFloat_FromDouble(yi < 0 ? -0.0 : 0.0);
#define WIDTH_OF_ULONG (CHAR_BIT*SIZEOF_LONG)
#if DBL_MANT_DIG < WIDTH_OF_ULONG
if ((xi >= 0 ? 0UL + xi : 0UL - xi) >> DBL_MANT_DIG ||
(yi >= 0 ? 0UL + yi : 0UL - yi) >> DBL_MANT_DIG)
/* Large x or y. Use long integer arithmetic. */
return PyLong_Type.tp_as_number->nb_true_divide(
(PyObject *)x, (PyObject *)y);
else
#endif
/* Both ints can be exactly represented as doubles. Do a
floating-point division. */
return PyFloat_FromDouble((double)xi / (double)yi);
}
static PyObject *
int_mod(PyIntObject *x, PyIntObject *y)
{
long xi, yi;
long d, m;
CONVERT_TO_LONG(x, xi);
CONVERT_TO_LONG(y, yi);
switch (i_divmod(xi, yi, &d, &m)) {
case DIVMOD_OK:
return PyInt_FromLong(m);
case DIVMOD_OVERFLOW:
return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
(PyObject *)y);
default:
return NULL;
}
}
static PyObject *
int_divmod(PyIntObject *x, PyIntObject *y)
{
long xi, yi;
long d, m;
CONVERT_TO_LONG(x, xi);
CONVERT_TO_LONG(y, yi);
switch (i_divmod(xi, yi, &d, &m)) {
case DIVMOD_OK:
return Py_BuildValue("(ll)", d, m);
case DIVMOD_OVERFLOW:
return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
(PyObject *)y);
default:
return NULL;
}
}
static PyObject *
int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
{
register long iv, iw, iz=0, ix, temp, prev;
CONVERT_TO_LONG(v, iv);
CONVERT_TO_LONG(w, iw);
if (iw < 0) {
if ((PyObject *)z != Py_None) {
PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
"cannot be negative when 3rd argument specified");
return NULL;
}
/* Return a float. This works because we know that
this calls float_pow() which converts its
arguments to double. */
return PyFloat_Type.tp_as_number->nb_power(
(PyObject *)v, (PyObject *)w, (PyObject *)z);
}
if ((PyObject *)z != Py_None) {
CONVERT_TO_LONG(z, iz);
if (iz == 0) {
PyErr_SetString(PyExc_ValueError,
"pow() 3rd argument cannot be 0");
return NULL;
}
}
/*
* XXX: The original exponentiation code stopped looping
* when temp hit zero; this code will continue onwards
* unnecessarily, but at least it won't cause any errors.
* Hopefully the speed improvement from the fast exponentiation
* will compensate for the slight inefficiency.
* XXX: Better handling of overflows is desperately needed.
*/
temp = iv;
ix = 1;
while (iw > 0) {
prev = ix; /* Save value for overflow check */
if (iw & 1) {
/*
* The (unsigned long) cast below ensures that the multiplication
* is interpreted as an unsigned operation rather than a signed one
* (C99 6.3.1.8p1), thus avoiding the perils of undefined behaviour
* from signed arithmetic overflow (C99 6.5p5). See issue #12973.
*/
ix = (unsigned long)ix * temp;
if (temp == 0)
break; /* Avoid ix / 0 */
if (ix / temp != prev) {
return PyLong_Type.tp_as_number->nb_power(
(PyObject *)v,
(PyObject *)w,
(PyObject *)z);
}
}
iw >>= 1; /* Shift exponent down by 1 bit */
if (iw==0) break;
prev = temp;
temp = (unsigned long)temp * temp; /* Square the value of temp */
if (prev != 0 && temp / prev != prev) {
return PyLong_Type.tp_as_number->nb_power(
(PyObject *)v, (PyObject *)w, (PyObject *)z);
}
if (iz) {
/* If we did a multiplication, perform a modulo */
ix = ix % iz;
temp = temp % iz;
}
}
if (iz) {
long div, mod;
switch (i_divmod(ix, iz, &div, &mod)) {
case DIVMOD_OK:
ix = mod;
break;
case DIVMOD_OVERFLOW:
return PyLong_Type.tp_as_number->nb_power(
(PyObject *)v, (PyObject *)w, (PyObject *)z);
default:
return NULL;
}
}
return PyInt_FromLong(ix);
}
static PyObject *
int_neg(PyIntObject *v)
{
register long a;
a = v->ob_ival;
/* check for overflow */
if (UNARY_NEG_WOULD_OVERFLOW(a)) {
PyObject *o = PyLong_FromLong(a);
if (o != NULL) {
PyObject *result = PyNumber_Negative(o);
Py_DECREF(o);
return result;
}
return NULL;
}
return PyInt_FromLong(-a);
}
static PyObject *
int_abs(PyIntObject *v)
{
if (v->ob_ival >= 0)
return int_int(v);
else
return int_neg(v);
}
static int
int_nonzero(PyIntObject *v)
{
return v->ob_ival != 0;
}
static PyObject *
int_invert(PyIntObject *v)
{
return PyInt_FromLong(~v->ob_ival);
}
static PyObject *
int_lshift(PyIntObject *v, PyIntObject *w)
{
long a, b, c;
PyObject *vv, *ww, *result;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
if (b < 0) {
PyErr_SetString(PyExc_ValueError, "negative shift count");
return NULL;
}
if (a == 0 || b == 0)
return int_int(v);
if (b >= LONG_BIT) {
vv = PyLong_FromLong(PyInt_AS_LONG(v));
if (vv == NULL)
return NULL;
ww = PyLong_FromLong(PyInt_AS_LONG(w));
if (ww == NULL) {
Py_DECREF(vv);
return NULL;
}
result = PyNumber_Lshift(vv, ww);
Py_DECREF(vv);
Py_DECREF(ww);
return result;
}
c = a << b;
if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
vv = PyLong_FromLong(PyInt_AS_LONG(v));
if (vv == NULL)
return NULL;
ww = PyLong_FromLong(PyInt_AS_LONG(w));
if (ww == NULL) {
Py_DECREF(vv);
return NULL;
}
result = PyNumber_Lshift(vv, ww);
Py_DECREF(vv);
Py_DECREF(ww);
return result;
}
return PyInt_FromLong(c);
}
static PyObject *
int_rshift(PyIntObject *v, PyIntObject *w)
{
register long a, b;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
if (b < 0) {
PyErr_SetString(PyExc_ValueError, "negative shift count");
return NULL;
}
if (a == 0 || b == 0)
return int_int(v);
if (b >= LONG_BIT) {
if (a < 0)
a = -1;
else
a = 0;
}
else {
a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
}
return PyInt_FromLong(a);
}
static PyObject *
int_and(PyIntObject *v, PyIntObject *w)
{
register long a, b;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
return PyInt_FromLong(a & b);
}
static PyObject *
int_xor(PyIntObject *v, PyIntObject *w)
{
register long a, b;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
return PyInt_FromLong(a ^ b);
}
static PyObject *
int_or(PyIntObject *v, PyIntObject *w)
{
register long a, b;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
return PyInt_FromLong(a | b);
}
static int
int_coerce(PyObject **pv, PyObject **pw)
{
if (PyInt_Check(*pw)) {
Py_INCREF(*pv);
Py_INCREF(*pw);
return 0;
}
return 1; /* Can't do it */
}
static PyObject *
int_int(PyIntObject *v)
{
if (PyInt_CheckExact(v))
Py_INCREF(v);
else
v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
return (PyObject *)v;
}
static PyObject *
int_long(PyIntObject *v)
{
return PyLong_FromLong((v -> ob_ival));
}
static const unsigned char BitLengthTable[32] = {
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
};
static int
bits_in_ulong(unsigned long d)
{
int d_bits = 0;
while (d >= 32) {
d_bits += 6;
d >>= 6;
}
d_bits += (int)BitLengthTable[d];
return d_bits;
}
#if 8*SIZEOF_LONG-1 <= DBL_MANT_DIG
/* Every Python int can be exactly represented as a float. */
static PyObject *
int_float(PyIntObject *v)
{
return PyFloat_FromDouble((double)(v -> ob_ival));
}
#else
/* Here not all Python ints are exactly representable as floats, so we may