FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/Objects/longobject.c at master · java66liu/python · GitHub
java66liu
/
python
Public
forked from
glix/python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
Objects
/
longobject.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
3660 lines (3281 loc) · 91 KB
Breadcrumbs
python
/
Objects
/
longobject.c
Copy path
File metadata and controls
3660 lines (3281 loc) · 91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<ctype.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).
*/
#define
KARATSUBA_CUTOFF
70
#define
KARATSUBA_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.
*/
#define
FIVEARY_CUTOFF
8
#define
ABS
(
x
) ((x) < 0 ? -(x) : (x))
#undef
MIN
#undef
MAX
#define
MAX
(
x
,
y
) ((x) < (y) ? (y) : (x))
#define
MIN
(
x
,
y
) ((x) > (y) ? (y) : (x))
/* Forward */
static
PyLongObject
*
long_normalize
(
PyLongObject
*
);
static
PyLongObject
*
mul1
(
PyLongObject
*
,
wdigit
);
static
PyLongObject
*
muladd1
(
PyLongObject
*
,
wdigit
,
wdigit
);
static
PyLongObject
*
divrem1
(
PyLongObject
*
,
digit
,
digit
*
);
#define
SIGCHECK
(
PyTryBlock
) \
if (--_Py_Ticker < 0) { \
_Py_Ticker = _Py_CheckInterval; \
if (PyErr_CheckSignals()) PyTryBlock \
}
/* 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. */
static
PyLongObject
*
long_normalize
(register
PyLongObject
*
v
)
{
Py_ssize_t
j
=
ABS
(
Py_SIZE
(
v
));
Py_ssize_t
i
=
j
;
while
(
i
>
0
&&
v
->
ob_digit
[
i
-
1
]
==
0
)
--
i
;
if
(
i
!=
j
)
Py_SIZE
(
v
)
=
(
Py_SIZE
(
v
)
<
0
) ?
-
(
i
) :
i
;
return
v
;
}
/* Allocate a new long int object with size digits.
Return NULL and set exception if we run out of memory. */
PyLongObject
*
_PyLong_New
(
Py_ssize_t
size
)
{
if
(
size
>
PY_SSIZE_T_MAX
) {
PyErr_NoMemory
();
return
NULL
;
}
/* coverity[ampersand_in_size] */
/* XXX(nnorwitz): This can overflow --
PyObject_NEW_VAR / _PyObject_VAR_SIZE need to detect overflow */
return
PyObject_NEW_VAR
(
PyLongObject
,
&
PyLong_Type
,
size
);
}
PyObject
*
_PyLong_Copy
(
PyLongObject
*
src
)
{
PyLongObject
*
result
;
Py_ssize_t
i
;
assert
(
src
!=
NULL
);
i
=
src
->
ob_size
;
if
(
i
<
0
)
i
=
-
(
i
);
result
=
_PyLong_New
(
i
);
if
(
result
!=
NULL
) {
result
->
ob_size
=
src
->
ob_size
;
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
(
long
ival
)
{
PyLongObject
*
v
;
unsigned long
abs_ival
;
unsigned long
t
;
/* unsigned so >> doesn't propagate sign bit */
int
ndigits
=
0
;
int
negative
=
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
;
v
->
ob_size
=
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 long
ival
)
{
PyLongObject
*
v
;
unsigned long
t
;
int
ndigits
=
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
(
double
dval
)
{
PyLongObject
*
v
;
double
frac
;
int
i
,
ndig
,
expo
,
neg
;
neg
=
0
;
if
(
Py_IS_INFINITY
(
dval
)) {
PyErr_SetString
(
PyExc_OverflowError
,
"cannot convert float infinity to integer"
);
return
NULL
;
}
if
(
Py_IS_NAN
(
dval
)) {
PyErr_SetString
(
PyExc_ValueError
,
"cannot convert float NaN to integer"
);
return
NULL
;
}
if
(
dval
<
0.0
) {
neg
=
1
;
dval
=
-
dval
;
}
frac
=
frexp
(
dval
,
&
expo
);
/* dval = frac*2**expo; 0.0 <= frac < 1.0 */
if
(
expo
<=
0
)
return
PyLong_FromLong
(
0L
);
ndig
=
(
expo
-
1
) /
PyLong_SHIFT
+
1
;
/* Number of 'digits' in result */
v
=
_PyLong_New
(
ndig
);
if
(
v
==
NULL
)
return
NULL
;
frac
=
ldexp
(
frac
, (
expo
-
1
) %
PyLong_SHIFT
+
1
);
for
(
i
=
ndig
;
--
i
>=
0
; ) {
long
bits
=
(
long
)
frac
;
v
->
ob_digit
[
i
]
=
(
digit
)
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 postive 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-".
*/
#define
PY_ABS_LONG_MIN
(0-(unsigned long)LONG_MIN)
#define
PY_ABS_SSIZE_T_MIN
(0-(size_t)PY_SSIZE_T_MIN)
/* Get a C long int from a long int object.
Returns -1 and sets an error condition if overflow occurs. */
long
PyLong_AsLong
(
PyObject
*
vv
)
{
/* This version by Tim Peters */
register
PyLongObject
*
v
;
unsigned long
x
,
prev
;
Py_ssize_t
i
;
int
sign
;
if
(
vv
==
NULL
||
!
PyLong_Check
(
vv
)) {
if
(
vv
!=
NULL
&&
PyInt_Check
(
vv
))
return
PyInt_AsLong
(
vv
);
PyErr_BadInternalCall
();
return
-1
;
}
v
=
(
PyLongObject
*
)
vv
;
i
=
v
->
ob_size
;
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 long requires extra care
* (see comment above).
*/
if
(
x
<= (
unsigned long
)
LONG_MAX
) {
return
(
long
)
x
*
sign
;
}
else
if
(
sign
<
0
&&
x
==
PY_ABS_LONG_MIN
) {
return
LONG_MIN
;
}
/* else overflow */
overflow
:
PyErr_SetString
(
PyExc_OverflowError
,
"long int too large to convert to int"
);
return
-1
;
}
/* 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_t
x
,
prev
;
Py_ssize_t
i
;
int
sign
;
if
(
vv
==
NULL
||
!
PyLong_Check
(
vv
)) {
PyErr_BadInternalCall
();
return
-1
;
}
v
=
(
PyLongObject
*
)
vv
;
i
=
v
->
ob_size
;
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
;
}
else
if
(
sign
<
0
&&
x
==
PY_ABS_SSIZE_T_MIN
) {
return
PY_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 long
x
,
prev
;
Py_ssize_t
i
;
if
(
vv
==
NULL
||
!
PyLong_Check
(
vv
)) {
if
(
vv
!=
NULL
&&
PyInt_Check
(
vv
)) {
long
val
=
PyInt_AsLong
(
vv
);
if
(
val
<
0
) {
PyErr_SetString
(
PyExc_OverflowError
,
"can't convert negative value to unsigned long"
);
return
(
unsigned long
)
-1
;
}
return
val
;
}
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
;
}
}
return
x
;
}
/* 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 long
x
;
Py_ssize_t
i
;
int
sign
;
if
(
vv
==
NULL
||
!
PyLong_Check
(
vv
)) {
if
(
vv
!=
NULL
&&
PyInt_Check
(
vv
))
return
PyInt_AsUnsignedLongMask
(
vv
);
PyErr_BadInternalCall
();
return
(
unsigned long
)
-1
;
}
v
=
(
PyLongObject
*
)
vv
;
i
=
v
->
ob_size
;
sign
=
1
;
x
=
0
;
if
(
i
<
0
) {
sign
=
-1
;
i
=
-
i
;
}
while
(
--
i
>=
0
) {
x
=
(
x
<<
PyLong_SHIFT
)
+
v
->
ob_digit
[
i
];
}
return
x
*
sign
;
}
int
_PyLong_Sign
(
PyObject
*
vv
)
{
PyLongObject
*
v
=
(
PyLongObject
*
)
vv
;
assert
(
v
!=
NULL
);
assert
(
PyLong_Check
(
v
));
return
Py_SIZE
(
v
)
==
0
?
0
: (
Py_SIZE
(
v
)
<
0
?
-1
:
1
);
}
size_t
_PyLong_NumBits
(
PyObject
*
vv
)
{
PyLongObject
*
v
=
(
PyLongObject
*
)
vv
;
size_t
result
=
0
;
Py_ssize_t
ndigits
;
assert
(
v
!=
NULL
);
assert
(
PyLong_Check
(
v
));
ndigits
=
ABS
(
Py_SIZE
(
v
));
assert
(
ndigits
==
0
||
v
->
ob_digit
[
ndigits
-
1
]
!=
0
);
if
(
ndigits
>
0
) {
digit
msd
=
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
);
}
return
result
;
Overflow
:
PyErr_SetString
(
PyExc_OverflowError
,
"long has too many bits "
"to express in a platform size_t"
);
return
(
size_t
)
-1
;
}
PyObject
*
_PyLong_FromByteArray
(
const
unsigned
char
*
bytes
,
size_t
n
,
int
little_endian
,
int
is_signed
)
{
const
unsigned
char
*
pstartbyte
;
/* LSB of bytes */
int
incr
;
/* direction to move pstartbyte */
const
unsigned
char
*
pendbyte
;
/* MSB of bytes */
size_t
numsignificantbytes
;
/* number of bytes that matter */
size_t
ndigits
;
/* number of Python long digits */
PyLongObject
*
v
;
/* result */
int
idigit
=
0
;
/* next free index in v->ob_digit */
if
(
n
==
0
)
return
PyLong_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 insignficant if the number
is positive, and leading 0xff bytes if negative. */
{
size_t
i
;
const
unsigned
char
*
p
=
pendbyte
;
const
int
pincr
=
-
incr
;
/* search MSB to LSB */
const
unsigned
char
insignficant
=
is_signed
?
0xff
:
0x00
;
for
(
i
=
0
;
i
<
n
;
++
i
,
p
+=
pincr
) {
if
(
*
p
!=
insignficant
)
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. */
ndigits
=
(
numsignificantbytes
*
8
+
PyLong_SHIFT
-
1
) /
PyLong_SHIFT
;
if
(
ndigits
>
(
size_t
)
INT_MAX
)
return
PyErr_NoMemory
();
v
=
_PyLong_New
((
int
)
ndigits
);
if
(
v
==
NULL
)
return
NULL
;
/* 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_t
i
;
twodigits
carry
=
1
;
/* for 2's-comp calculation */
twodigits
accum
=
0
;
/* sliding register */
unsigned
int
accumbits
=
0
;
/* number of bits in accum */
const
unsigned
char
*
p
=
pstartbyte
;
for
(
i
=
0
;
i
<
numsignificantbytes
;
++
i
,
p
+=
incr
) {
twodigits
thisbyte
=
*
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
<
(
int
)
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
<
(
int
)
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_t
n
,
int
little_endian
,
int
is_signed
)
{
Py_ssize_t
i
;
/* index into v->ob_digit */
Py_ssize_t
ndigits
;
/* |v->ob_size| */
twodigits
accum
;
/* sliding register */
unsigned
int
accumbits
;
/* # bits in accum */
int
do_twos_comp
;
/* store 2's-comp? is_signed and v < 0 */
digit
carry
;
/* for computing 2's-comp */
size_t
j
;
/* # bytes filled */
unsigned
char
*
p
;
/* pointer to next byte in bytes */
int
pincr
;
/* 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_TypeError
,
"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
) {
digit
thisdigit
=
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. */
digit
s
=
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
;
}
else
if
(
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
char
msb
=
*
(
p
-
pincr
);
int
sign_bit_set
=
msb
>=
0x80
;
assert
(
accumbits
==
0
);
if
(
sign_bit_set
==
do_twos_comp
)
return
0
;
else
goto
Overflow
;
}
/* Fill remaining bytes with copies of the sign bit. */
{
unsigned
char
signbyte
=
do_twos_comp
?
0xffU
:
0U
;
for
( ;
j
<
n
;
++
j
,
p
+=
pincr
)
*
p
=
signbyte
;
}
return
0
;
Overflow
:
PyErr_SetString
(
PyExc_OverflowError
,
"long too big to convert"
);
return
-1
;
}
double
_PyLong_AsScaledDouble
(
PyObject
*
vv
,
int
*
exponent
)
{
/* NBITS_WANTED should be > the number of bits in a double's precision,
but small enough so that 2**NBITS_WANTED is within the normal double
range. nbitsneeded is set to 1 less than that because the most-significant
Python digit contains at least 1 significant bit, but we don't want to
bother counting them (catering to the worst case cheaply).
57 is one more than VAX-D double precision; I (Tim) don't know of a double
format with more precision than that; it's 1 larger so that we add in at
least one round bit to stand in for the ignored least-significant bits.
*/
#define
NBITS_WANTED
57
PyLongObject
*
v
;
double
x
;
const
double
multiplier
=
(
double
)(
1L
<<
PyLong_SHIFT
);
Py_ssize_t
i
;
int
sign
;
int
nbitsneeded
;
if
(
vv
==
NULL
||
!
PyLong_Check
(
vv
)) {
PyErr_BadInternalCall
();
return
-1
;
}
v
=
(
PyLongObject
*
)
vv
;
i
=
Py_SIZE
(
v
);
sign
=
1
;
if
(
i
<
0
) {
sign
=
-1
;
i
=
-
(
i
);
}
else
if
(
i
==
0
) {
*
exponent
=
0
;
return
0.0
;
}
--
i
;
x
=
(
double
)
v
->
ob_digit
[
i
];
nbitsneeded
=
NBITS_WANTED
-
1
;
/* Invariant: i Python digits remain unaccounted for. */
while
(
i
>
0
&&
nbitsneeded
>
0
) {
--
i
;
x
=
x
*
multiplier
+
(
double
)
v
->
ob_digit
[
i
];
nbitsneeded
-=
PyLong_SHIFT
;
}
/* There are i digits we didn't shift in. Pretending they're all
zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
*
exponent
=
i
;
assert
(
x
>
0.0
);
return
x
*
sign
;
#undef
NBITS_WANTED
}
/* Get a C double from a long int object. */
double
PyLong_AsDouble
(
PyObject
*
vv
)
{
int
e
=
-1
;
double
x
;
if
(
vv
==
NULL
||
!
PyLong_Check
(
vv
)) {
PyErr_BadInternalCall
();
return
-1
;
}
x
=
_PyLong_AsScaledDouble
(
vv
,
&
e
);
if
(
x
==
-1.0
&&
PyErr_Occurred
())
return
-1.0
;
/* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
set correctly after a successful _PyLong_AsScaledDouble() call */
assert
(
e
>=
0
);
if
(
e
>
INT_MAX
/
PyLong_SHIFT
)
goto
overflow
;
errno
=
0
;
x
=
ldexp
(
x
,
e
*
PyLong_SHIFT
);
if
(
Py_OVERFLOWED
(
x
))
goto
overflow
;
return
x
;
overflow
:
PyErr_SetString
(
PyExc_OverflowError
,
"long int too large to convert to float"
);
return
-1.0
;
}
/* Create a new long (or int) object from a C pointer */
PyObject
*
PyLong_FromVoidPtr
(
void
*
p
)
{
#if
SIZEOF_VOID_P
<=
SIZEOF_LONG
if
((
long
)
p
<
0
)
return
PyLong_FromUnsignedLong
((
unsigned long
)
p
);
return
PyInt_FromLong
((
long
)
p
);
#else
#ifndef
HAVE_LONG_LONG
# error
"PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
#endif
#if
SIZEOF_LONG_LONG
<
SIZEOF_VOID_P
# error
"PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
#endif
/* optimize null pointers */
if
(
p
==
NULL
)
return
PyInt_FromLong
(
0
);
return
PyLong_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"
*/
#if
SIZEOF_VOID_P
<=
SIZEOF_LONG
long
x
;
if
(
PyInt_Check
(
vv
))
x
=
PyInt_AS_LONG
(
vv
);
else
if
(
PyLong_Check
(
vv
)
&&
_PyLong_Sign
(
vv
)
<
0
)
x
=
PyLong_AsLong
(
vv
);
else
x
=
PyLong_AsUnsignedLong
(
vv
);
#else
#ifndef
HAVE_LONG_LONG
# error
"PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
#endif
#if
SIZEOF_LONG_LONG
<
SIZEOF_VOID_P
# error
"PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
#endif
PY_LONG_LONG
x
;
if
(
PyInt_Check
(
vv
))
x
=
PyInt_AS_LONG
(
vv
);
else
if
(
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
())
return
NULL
;
return
(
void
*
)
x
;
}
#ifdef
HAVE_LONG_LONG
/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
* rewritten to use the newer PyLong_{As,From}ByteArray API.
*/
#define
IS_LITTLE_ENDIAN
(int)*(unsigned char*)&one
/* Create a new long int object from a C PY_LONG_LONG int. */
PyObject
*
PyLong_FromLongLong
(
PY_LONG_LONG
ival
)
{
PyLongObject
*
v
;
unsigned
PY_LONG_LONG
abs_ival
;
unsigned
PY_LONG_LONG
t
;
/* unsigned so >> doesn't propagate sign bit */
int
ndigits
=
0
;
int
negative
=
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_LONG
ival
)
{
PyLongObject
*
v
;
unsigned
PY_LONG_LONG
t
;
int
ndigits
=
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_t
ival
)
{
Py_ssize_t
bytes
=
ival
;
int
one
=
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_t
ival
)
{
size_t
bytes
=
ival
;
int
one
=
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_LONG
bytes
;
int
one
=
1
;
int
res
;
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
=
vv
->
ob_type
->
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
);
return
bytes
;
}
if
(
PyLong_Check
(
io
)) {
bytes
=
PyLong_AsLongLong
(
io
);
Py_DECREF
(
io
);
return
bytes
;
}
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
return
bytes
;
}
/* 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_LONG
bytes
;
int
one
=
1
;
int
res
;
if
(
vv
==
NULL
||
!
PyLong_Check
(
vv
)) {
PyErr_BadInternalCall
();
return
(
unsigned
PY_LONG_LONG
)
-1
;
}
res
=
_PyLong_AsByteArray
(
(
PyLongObject
*
)
vv
, (
unsigned
char
*
)
&
bytes
,
SIZEOF_LONG_LONG
,
IS_LITTLE_ENDIAN
,
0
);
/* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL