- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathlongobject.c
4406 lines (3938 loc) · 132 KB
/
longobject.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
/* Long (arbitrary precision) integer object implementation */
/* XXX The functional organization of this file is terrible */
#include"Python.h"
#include"longintrepr.h"
#include"structseq.h"
#include<float.h>
#include<ctype.h>
#include<stddef.h>
/* For long multiplication, use the O(N**2) school algorithm unless
* both operands contain more than KARATSUBA_CUTOFF digits (this
* being an internal Python long digit, in base PyLong_BASE).
*/
#defineKARATSUBA_CUTOFF 70
#defineKARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
/* For exponentiation, use the binary left-to-right algorithm
* unless the exponent contains more than FIVEARY_CUTOFF digits.
* In that case, do 5 bits at a time. The potential drawback is that
* a table of 2**5 intermediate results is computed.
*/
#defineFIVEARY_CUTOFF 8
#defineABS(x) ((x) < 0 ? -(x) : (x))
#undef MIN
#undef MAX
#defineMAX(x, y) ((x) < (y) ? (y) : (x))
#defineMIN(x, y) ((x) > (y) ? (y) : (x))
#defineSIGCHECK(PyTryBlock) \
do { \
if (--_Py_Ticker < 0) { \
_Py_Ticker = _Py_CheckInterval; \
if (PyErr_CheckSignals()) PyTryBlock \
} \
} while(0)
/* Normalize (remove leading zeros from) a long int object.
Doesn't attempt to free the storage--in most cases, due to the nature
of the algorithms used, this could save at most be one word anyway. */
staticPyLongObject*
long_normalize(register PyLongObject*v)
{
Py_ssize_tj=ABS(Py_SIZE(v));
Py_ssize_ti=j;
while (i>0&&v->ob_digit[i-1] ==0)
--i;
if (i!=j)
Py_SIZE(v) = (Py_SIZE(v) <0) ? -(i) : i;
returnv;
}
/* Allocate a new long int object with size digits.
Return NULL and set exception if we run out of memory. */
#defineMAX_LONG_DIGITS \
((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit))
PyLongObject*
_PyLong_New(Py_ssize_tsize)
{
if (size> (Py_ssize_t)MAX_LONG_DIGITS) {
PyErr_SetString(PyExc_OverflowError,
"too many digits in integer");
returnNULL;
}
/* coverity[ampersand_in_size] */
/* XXX(nnorwitz): PyObject_NEW_VAR / _PyObject_VAR_SIZE need to detect
overflow */
returnPyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
}
PyObject*
_PyLong_Copy(PyLongObject*src)
{
PyLongObject*result;
Py_ssize_ti;
assert(src!=NULL);
i=Py_SIZE(src);
if (i<0)
i=-(i);
result=_PyLong_New(i);
if (result!=NULL) {
Py_SIZE(result) =Py_SIZE(src);
while (--i >= 0)
result->ob_digit[i] =src->ob_digit[i];
}
return (PyObject*)result;
}
/* Create a new long int object from a C long int */
PyObject*
PyLong_FromLong(longival)
{
PyLongObject*v;
unsigned longabs_ival;
unsigned longt; /* unsigned so >> doesn't propagate sign bit */
intndigits=0;
intnegative=0;
if (ival<0) {
/* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then
ANSI C says that the result of -ival is undefined when ival
== LONG_MIN. Hence the following workaround. */
abs_ival= (unsigned long)(-1-ival) +1;
negative=1;
}
else {
abs_ival= (unsigned long)ival;
}
/* Count the number of Python digits.
We used to pick 5 ("big enough for anything"), but that's a
waste of time and space given that 5*15 = 75 bits are rarely
needed. */
t=abs_ival;
while (t) {
++ndigits;
t >>= PyLong_SHIFT;
}
v=_PyLong_New(ndigits);
if (v!=NULL) {
digit*p=v->ob_digit;
Py_SIZE(v) =negative ? -ndigits : ndigits;
t=abs_ival;
while (t) {
*p++= (digit)(t&PyLong_MASK);
t >>= PyLong_SHIFT;
}
}
return (PyObject*)v;
}
/* Create a new long int object from a C unsigned long int */
PyObject*
PyLong_FromUnsignedLong(unsigned longival)
{
PyLongObject*v;
unsigned longt;
intndigits=0;
/* Count the number of Python digits. */
t= (unsigned long)ival;
while (t) {
++ndigits;
t >>= PyLong_SHIFT;
}
v=_PyLong_New(ndigits);
if (v!=NULL) {
digit*p=v->ob_digit;
Py_SIZE(v) =ndigits;
while (ival) {
*p++= (digit)(ival&PyLong_MASK);
ival >>= PyLong_SHIFT;
}
}
return (PyObject*)v;
}
/* Create a new long int object from a C double */
PyObject*
PyLong_FromDouble(doubledval)
{
PyLongObject*v;
doublefrac;
inti, ndig, expo, neg;
neg=0;
if (Py_IS_INFINITY(dval)) {
PyErr_SetString(PyExc_OverflowError,
"cannot convert float infinity to integer");
returnNULL;
}
if (Py_IS_NAN(dval)) {
PyErr_SetString(PyExc_ValueError,
"cannot convert float NaN to integer");
returnNULL;
}
if (dval<0.0) {
neg=1;
dval=-dval;
}
frac=frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
if (expo <= 0)
returnPyLong_FromLong(0L);
ndig= (expo-1) / PyLong_SHIFT+1; /* Number of 'digits' in result */
v=_PyLong_New(ndig);
if (v==NULL)
returnNULL;
frac=ldexp(frac, (expo-1) % PyLong_SHIFT+1);
for (i=ndig; --i >= 0; ) {
digitbits= (digit)frac;
v->ob_digit[i] =bits;
frac=frac- (double)bits;
frac=ldexp(frac, PyLong_SHIFT);
}
if (neg)
Py_SIZE(v) =-(Py_SIZE(v));
return (PyObject*)v;
}
/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
* anything about what happens when a signed integer operation overflows,
* and some compilers think they're doing you a favor by being "clever"
* then. The bit pattern for the largest positive signed long is
* (unsigned long)LONG_MAX, and for the smallest negative signed long
* it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
* However, some other compilers warn about applying unary minus to an
* unsigned operand. Hence the weird "0-".
*/
#definePY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
#definePY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
/* Get a C long int from a Python long or Python int object.
On overflow, returns -1 and sets *overflow to 1 or -1 depending
on the sign of the result. Otherwise *overflow is 0.
For other errors (e.g., type error), returns -1 and sets an error
condition.
*/
long
PyLong_AsLongAndOverflow(PyObject*vv, int*overflow)
{
/* This version by Tim Peters */
register PyLongObject*v;
unsigned longx, prev;
longres;
Py_ssize_ti;
intsign;
intdo_decref=0; /* if nb_int was called */
*overflow=0;
if (vv==NULL) {
PyErr_BadInternalCall();
return-1;
}
if(PyInt_Check(vv))
returnPyInt_AsLong(vv);
if (!PyLong_Check(vv)) {
PyNumberMethods*nb;
nb=vv->ob_type->tp_as_number;
if (nb==NULL||nb->nb_int==NULL) {
PyErr_SetString(PyExc_TypeError,
"an integer is required");
return-1;
}
vv= (*nb->nb_int) (vv);
if (vv==NULL)
return-1;
do_decref=1;
if(PyInt_Check(vv)) {
res=PyInt_AsLong(vv);
goto exit;
}
if (!PyLong_Check(vv)) {
Py_DECREF(vv);
PyErr_SetString(PyExc_TypeError,
"nb_int should return int object");
return-1;
}
}
res=-1;
v= (PyLongObject*)vv;
i=Py_SIZE(v);
switch (i) {
case-1:
res=-(sdigit)v->ob_digit[0];
break;
case0:
res=0;
break;
case1:
res=v->ob_digit[0];
break;
default:
sign=1;
x=0;
if (i<0) {
sign=-1;
i=-(i);
}
while (--i >= 0) {
prev=x;
x= (x << PyLong_SHIFT) +v->ob_digit[i];
if ((x >> PyLong_SHIFT) !=prev) {
*overflow=sign;
goto exit;
}
}
/* Haven't lost any bits, but casting to long requires extra
* care (see comment above).
*/
if (x <= (unsigned long)LONG_MAX) {
res= (long)x*sign;
}
elseif (sign<0&&x==PY_ABS_LONG_MIN) {
res=LONG_MIN;
}
else {
*overflow=sign;
/* res is already set to -1 */
}
}
exit:
if (do_decref) {
Py_DECREF(vv);
}
returnres;
}
/* Get a C long int from a long int object.
Returns -1 and sets an error condition if overflow occurs. */
long
PyLong_AsLong(PyObject*obj)
{
intoverflow;
longresult=PyLong_AsLongAndOverflow(obj, &overflow);
if (overflow) {
/* XXX: could be cute and give a different
message for overflow == -1 */
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to C long");
}
returnresult;
}
/* Get a C int from a long int object or any object that has an __int__
method. Return -1 and set an error if overflow occurs. */
int
_PyLong_AsInt(PyObject*obj)
{
intoverflow;
longresult=PyLong_AsLongAndOverflow(obj, &overflow);
if (overflow||result>INT_MAX||result<INT_MIN) {
/* XXX: could be cute and give a different
message for overflow == -1 */
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to C int");
return-1;
}
return (int)result;
}
/* Get a Py_ssize_t from a long int object.
Returns -1 and sets an error condition if overflow occurs. */
Py_ssize_t
PyLong_AsSsize_t(PyObject*vv) {
register PyLongObject*v;
size_tx, prev;
Py_ssize_ti;
intsign;
if (vv==NULL|| !PyLong_Check(vv)) {
PyErr_BadInternalCall();
return-1;
}
v= (PyLongObject*)vv;
i=Py_SIZE(v);
sign=1;
x=0;
if (i<0) {
sign=-1;
i=-(i);
}
while (--i >= 0) {
prev=x;
x= (x << PyLong_SHIFT) | v->ob_digit[i];
if ((x >> PyLong_SHIFT) !=prev)
goto overflow;
}
/* Haven't lost any bits, but casting to a signed type requires
* extra care (see comment above).
*/
if (x <= (size_t)PY_SSIZE_T_MAX) {
return (Py_ssize_t)x*sign;
}
elseif (sign<0&&x==PY_ABS_SSIZE_T_MIN) {
returnPY_SSIZE_T_MIN;
}
/* else overflow */
overflow:
PyErr_SetString(PyExc_OverflowError,
"long int too large to convert to int");
return-1;
}
/* Get a C unsigned long int from a long int object.
Returns -1 and sets an error condition if overflow occurs. */
unsigned long
PyLong_AsUnsignedLong(PyObject*vv)
{
register PyLongObject*v;
unsigned longx, prev;
Py_ssize_ti;
if (vv==NULL|| !PyLong_Check(vv)) {
if (vv!=NULL&&PyInt_Check(vv)) {
longval=PyInt_AsLong(vv);
if (val<0) {
PyErr_SetString(PyExc_OverflowError,
"can't convert negative value "
"to unsigned long");
return (unsigned long) -1;
}
returnval;
}
PyErr_BadInternalCall();
return (unsigned long) -1;
}
v= (PyLongObject*)vv;
i=Py_SIZE(v);
x=0;
if (i<0) {
PyErr_SetString(PyExc_OverflowError,
"can't convert negative value to unsigned long");
return (unsigned long) -1;
}
while (--i >= 0) {
prev=x;
x= (x << PyLong_SHIFT) | v->ob_digit[i];
if ((x >> PyLong_SHIFT) !=prev) {
PyErr_SetString(PyExc_OverflowError,
"long int too large to convert");
return (unsigned long) -1;
}
}
returnx;
}
/* Get a C unsigned long int from a long int object, ignoring the high bits.
Returns -1 and sets an error condition if an error occurs. */
unsigned long
PyLong_AsUnsignedLongMask(PyObject*vv)
{
register PyLongObject*v;
unsigned longx;
Py_ssize_ti;
intsign;
if (vv==NULL|| !PyLong_Check(vv)) {
if (vv!=NULL&&PyInt_Check(vv))
returnPyInt_AsUnsignedLongMask(vv);
PyErr_BadInternalCall();
return (unsigned long) -1;
}
v= (PyLongObject*)vv;
i=Py_SIZE(v);
sign=1;
x=0;
if (i<0) {
sign=-1;
i=-i;
}
while (--i >= 0) {
x= (x << PyLong_SHIFT) | v->ob_digit[i];
}
returnx*sign;
}
int
_PyLong_Sign(PyObject*vv)
{
PyLongObject*v= (PyLongObject*)vv;
assert(v!=NULL);
assert(PyLong_Check(v));
returnPy_SIZE(v) ==0 ? 0 : (Py_SIZE(v) <0 ? -1 : 1);
}
size_t
_PyLong_NumBits(PyObject*vv)
{
PyLongObject*v= (PyLongObject*)vv;
size_tresult=0;
Py_ssize_tndigits;
assert(v!=NULL);
assert(PyLong_Check(v));
ndigits=ABS(Py_SIZE(v));
assert(ndigits==0||v->ob_digit[ndigits-1] !=0);
if (ndigits>0) {
digitmsd=v->ob_digit[ndigits-1];
result= (ndigits-1) *PyLong_SHIFT;
if (result / PyLong_SHIFT!= (size_t)(ndigits-1))
goto Overflow;
do {
++result;
if (result==0)
goto Overflow;
msd >>= 1;
} while (msd);
}
returnresult;
Overflow:
PyErr_SetString(PyExc_OverflowError, "long has too many bits "
"to express in a platform size_t");
return (size_t)-1;
}
PyObject*
_PyLong_FromByteArray(constunsigned char*bytes, size_tn,
intlittle_endian, intis_signed)
{
constunsigned char*pstartbyte; /* LSB of bytes */
intincr; /* direction to move pstartbyte */
constunsigned char*pendbyte; /* MSB of bytes */
size_tnumsignificantbytes; /* number of bytes that matter */
Py_ssize_tndigits; /* number of Python long digits */
PyLongObject*v; /* result */
Py_ssize_tidigit=0; /* next free index in v->ob_digit */
if (n==0)
returnPyLong_FromLong(0L);
if (little_endian) {
pstartbyte=bytes;
pendbyte=bytes+n-1;
incr=1;
}
else {
pstartbyte=bytes+n-1;
pendbyte=bytes;
incr=-1;
}
if (is_signed)
is_signed=*pendbyte >= 0x80;
/* Compute numsignificantbytes. This consists of finding the most
significant byte. Leading 0 bytes are insignificant if the number
is positive, and leading 0xff bytes if negative. */
{
size_ti;
constunsigned char*p=pendbyte;
constintpincr=-incr; /* search MSB to LSB */
constunsigned charinsignificant=is_signed ? 0xff : 0x00;
for (i=0; i<n; ++i, p+=pincr) {
if (*p!=insignificant)
break;
}
numsignificantbytes=n-i;
/* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
actually has 2 significant bytes. OTOH, 0xff0001 ==
-0x00ffff, so we wouldn't *need* to bump it there; but we
do for 0xffff = -0x0001. To be safe without bothering to
check every case, bump it regardless. */
if (is_signed&&numsignificantbytes<n)
++numsignificantbytes;
}
/* How many Python long digits do we need? We have
8*numsignificantbytes bits, and each Python long digit has
PyLong_SHIFT bits, so it's the ceiling of the quotient. */
/* catch overflow before it happens */
if (numsignificantbytes> (PY_SSIZE_T_MAX-PyLong_SHIFT) / 8) {
PyErr_SetString(PyExc_OverflowError,
"byte array too long to convert to int");
returnNULL;
}
ndigits= (numsignificantbytes*8+PyLong_SHIFT-1) / PyLong_SHIFT;
v=_PyLong_New(ndigits);
if (v==NULL)
returnNULL;
/* Copy the bits over. The tricky parts are computing 2's-comp on
the fly for signed numbers, and dealing with the mismatch between
8-bit bytes and (probably) 15-bit Python digits.*/
{
size_ti;
twodigitscarry=1; /* for 2's-comp calculation */
twodigitsaccum=0; /* sliding register */
unsigned intaccumbits=0; /* number of bits in accum */
constunsigned char*p=pstartbyte;
for (i=0; i<numsignificantbytes; ++i, p+=incr) {
twodigitsthisbyte=*p;
/* Compute correction for 2's comp, if needed. */
if (is_signed) {
thisbyte= (0xff ^ thisbyte) +carry;
carry=thisbyte >> 8;
thisbyte &= 0xff;
}
/* Because we're going LSB to MSB, thisbyte is
more significant than what's already in accum,
so needs to be prepended to accum. */
accum |= (twodigits)thisbyte << accumbits;
accumbits+=8;
if (accumbits >= PyLong_SHIFT) {
/* There's enough to fill a Python digit. */
assert(idigit<ndigits);
v->ob_digit[idigit] = (digit)(accum&PyLong_MASK);
++idigit;
accum >>= PyLong_SHIFT;
accumbits-=PyLong_SHIFT;
assert(accumbits<PyLong_SHIFT);
}
}
assert(accumbits<PyLong_SHIFT);
if (accumbits) {
assert(idigit<ndigits);
v->ob_digit[idigit] = (digit)accum;
++idigit;
}
}
Py_SIZE(v) =is_signed ? -idigit : idigit;
return (PyObject*)long_normalize(v);
}
int
_PyLong_AsByteArray(PyLongObject*v,
unsigned char*bytes, size_tn,
intlittle_endian, intis_signed)
{
Py_ssize_ti; /* index into v->ob_digit */
Py_ssize_tndigits; /* |v->ob_size| */
twodigitsaccum; /* sliding register */
unsigned intaccumbits; /* # bits in accum */
intdo_twos_comp; /* store 2's-comp? is_signed and v < 0 */
digitcarry; /* for computing 2's-comp */
size_tj; /* # bytes filled */
unsigned char*p; /* pointer to next byte in bytes */
intpincr; /* direction to move p */
assert(v!=NULL&&PyLong_Check(v));
if (Py_SIZE(v) <0) {
ndigits=-(Py_SIZE(v));
if (!is_signed) {
PyErr_SetString(PyExc_OverflowError,
"can't convert negative long to unsigned");
return-1;
}
do_twos_comp=1;
}
else {
ndigits=Py_SIZE(v);
do_twos_comp=0;
}
if (little_endian) {
p=bytes;
pincr=1;
}
else {
p=bytes+n-1;
pincr=-1;
}
/* Copy over all the Python digits.
It's crucial that every Python digit except for the MSD contribute
exactly PyLong_SHIFT bits to the total, so first assert that the long is
normalized. */
assert(ndigits==0||v->ob_digit[ndigits-1] !=0);
j=0;
accum=0;
accumbits=0;
carry=do_twos_comp ? 1 : 0;
for (i=0; i<ndigits; ++i) {
digitthisdigit=v->ob_digit[i];
if (do_twos_comp) {
thisdigit= (thisdigit ^ PyLong_MASK) +carry;
carry=thisdigit >> PyLong_SHIFT;
thisdigit &= PyLong_MASK;
}
/* Because we're going LSB to MSB, thisdigit is more
significant than what's already in accum, so needs to be
prepended to accum. */
accum |= (twodigits)thisdigit << accumbits;
/* The most-significant digit may be (probably is) at least
partly empty. */
if (i==ndigits-1) {
/* Count # of sign bits -- they needn't be stored,
* although for signed conversion we need later to
* make sure at least one sign bit gets stored. */
digits=do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit;
while (s!=0) {
s >>= 1;
accumbits++;
}
}
else
accumbits+=PyLong_SHIFT;
/* Store as many bytes as possible. */
while (accumbits >= 8) {
if (j >= n)
goto Overflow;
++j;
*p= (unsigned char)(accum&0xff);
p+=pincr;
accumbits-=8;
accum >>= 8;
}
}
/* Store the straggler (if any). */
assert(accumbits<8);
assert(carry==0); /* else do_twos_comp and *every* digit was 0 */
if (accumbits>0) {
if (j >= n)
goto Overflow;
++j;
if (do_twos_comp) {
/* Fill leading bits of the byte with sign bits
(appropriately pretending that the long had an
infinite supply of sign bits). */
accum |= (~(twodigits)0) << accumbits;
}
*p= (unsigned char)(accum&0xff);
p+=pincr;
}
elseif (j==n&&n>0&&is_signed) {
/* The main loop filled the byte array exactly, so the code
just above didn't get to ensure there's a sign bit, and the
loop below wouldn't add one either. Make sure a sign bit
exists. */
unsigned charmsb=*(p-pincr);
intsign_bit_set=msb >= 0x80;
assert(accumbits==0);
if (sign_bit_set==do_twos_comp)
return0;
else
goto Overflow;
}
/* Fill remaining bytes with copies of the sign bit. */
{
unsigned charsignbyte=do_twos_comp ? 0xffU : 0U;
for ( ; j<n; ++j, p+=pincr)
*p=signbyte;
}
return0;
Overflow:
PyErr_SetString(PyExc_OverflowError, "long too big to convert");
return-1;
}
/* Create a new long (or int) object from a C pointer */
PyObject*
PyLong_FromVoidPtr(void*p)
{
#ifSIZEOF_VOID_P <= SIZEOF_LONG
if ((long)p<0)
returnPyLong_FromUnsignedLong((unsigned long)p);
returnPyInt_FromLong((long)p);
#else
#ifndefHAVE_LONG_LONG
# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
#endif
#ifSIZEOF_LONG_LONG<SIZEOF_VOID_P
# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
#endif
/* optimize null pointers */
if (p==NULL)
returnPyInt_FromLong(0);
returnPyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
#endif/* SIZEOF_VOID_P <= SIZEOF_LONG */
}
/* Get a C pointer from a long object (or an int object in some cases) */
void*
PyLong_AsVoidPtr(PyObject*vv)
{
/* This function will allow int or long objects. If vv is neither,
then the PyLong_AsLong*() functions will raise the exception:
PyExc_SystemError, "bad argument to internal function"
*/
#ifSIZEOF_VOID_P <= SIZEOF_LONG
longx;
if (PyInt_Check(vv))
x=PyInt_AS_LONG(vv);
elseif (PyLong_Check(vv) &&_PyLong_Sign(vv) <0)
x=PyLong_AsLong(vv);
else
x=PyLong_AsUnsignedLong(vv);
#else
#ifndefHAVE_LONG_LONG
# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
#endif
#ifSIZEOF_LONG_LONG<SIZEOF_VOID_P
# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
#endif
PY_LONG_LONGx;
if (PyInt_Check(vv))
x=PyInt_AS_LONG(vv);
elseif (PyLong_Check(vv) &&_PyLong_Sign(vv) <0)
x=PyLong_AsLongLong(vv);
else
x=PyLong_AsUnsignedLongLong(vv);
#endif/* SIZEOF_VOID_P <= SIZEOF_LONG */
if (x==-1&&PyErr_Occurred())
returnNULL;
return (void*)x;
}
#ifdefHAVE_LONG_LONG
/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
* rewritten to use the newer PyLong_{As,From}ByteArray API.
*/
#defineIS_LITTLE_ENDIAN (int)*(unsigned char*)&one
#definePY_ABS_LLONG_MIN (0-(unsigned PY_LONG_LONG)PY_LLONG_MIN)
/* Create a new long int object from a C PY_LONG_LONG int. */
PyObject*
PyLong_FromLongLong(PY_LONG_LONGival)
{
PyLongObject*v;
unsigned PY_LONG_LONGabs_ival;
unsigned PY_LONG_LONGt; /* unsigned so >> doesn't propagate sign bit */
intndigits=0;
intnegative=0;
if (ival<0) {
/* avoid signed overflow on negation; see comments
in PyLong_FromLong above. */
abs_ival= (unsigned PY_LONG_LONG)(-1-ival) +1;
negative=1;
}
else {
abs_ival= (unsigned PY_LONG_LONG)ival;
}
/* Count the number of Python digits.
We used to pick 5 ("big enough for anything"), but that's a
waste of time and space given that 5*15 = 75 bits are rarely
needed. */
t=abs_ival;
while (t) {
++ndigits;
t >>= PyLong_SHIFT;
}
v=_PyLong_New(ndigits);
if (v!=NULL) {
digit*p=v->ob_digit;
Py_SIZE(v) =negative ? -ndigits : ndigits;
t=abs_ival;
while (t) {
*p++= (digit)(t&PyLong_MASK);
t >>= PyLong_SHIFT;
}
}
return (PyObject*)v;
}
/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
PyObject*
PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONGival)
{
PyLongObject*v;
unsigned PY_LONG_LONGt;
intndigits=0;
/* Count the number of Python digits. */
t= (unsigned PY_LONG_LONG)ival;
while (t) {
++ndigits;
t >>= PyLong_SHIFT;
}
v=_PyLong_New(ndigits);
if (v!=NULL) {
digit*p=v->ob_digit;
Py_SIZE(v) =ndigits;
while (ival) {
*p++= (digit)(ival&PyLong_MASK);
ival >>= PyLong_SHIFT;
}
}
return (PyObject*)v;
}
/* Create a new long int object from a C Py_ssize_t. */
PyObject*
PyLong_FromSsize_t(Py_ssize_tival)
{
Py_ssize_tbytes=ival;
intone=1;
return_PyLong_FromByteArray((unsigned char*)&bytes,
SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1);
}
/* Create a new long int object from a C size_t. */
PyObject*
PyLong_FromSize_t(size_tival)
{
size_tbytes=ival;
intone=1;
return_PyLong_FromByteArray((unsigned char*)&bytes,
SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
}
/* Get a C PY_LONG_LONG int from a long int object.
Return -1 and set an error if overflow occurs. */
PY_LONG_LONG
PyLong_AsLongLong(PyObject*vv)
{
PY_LONG_LONGbytes;
intone=1;
intres;
if (vv==NULL) {
PyErr_BadInternalCall();
return-1;
}
if (!PyLong_Check(vv)) {
PyNumberMethods*nb;
PyObject*io;
if (PyInt_Check(vv))
return (PY_LONG_LONG)PyInt_AsLong(vv);
if ((nb=Py_TYPE(vv)->tp_as_number) ==NULL||
nb->nb_int==NULL) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
return-1;
}
io= (*nb->nb_int) (vv);
if (io==NULL)
return-1;
if (PyInt_Check(io)) {
bytes=PyInt_AsLong(io);
Py_DECREF(io);
returnbytes;
}
if (PyLong_Check(io)) {
bytes=PyLong_AsLongLong(io);
Py_DECREF(io);
returnbytes;
}
Py_DECREF(io);
PyErr_SetString(PyExc_TypeError, "integer conversion failed");
return-1;
}
res=_PyLong_AsByteArray((PyLongObject*)vv, (unsigned char*)&bytes,
SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
/* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
if (res<0)
return (PY_LONG_LONG)-1;
else
returnbytes;
}
/* Get a C unsigned PY_LONG_LONG int from a long int object.
Return -1 and set an error if overflow occurs. */
unsigned PY_LONG_LONG
PyLong_AsUnsignedLongLong(PyObject*vv)
{
unsigned PY_LONG_LONGbytes;
intone=1;
intres;
if (vv==NULL|| !PyLong_Check(vv)) {
PyErr_BadInternalCall();
return (unsigned PY_LONG_LONG)-1;
}