/Objects/longobject.c

http://unladen-swallow.googlecode.com/ · C · 3590 lines · 2711 code · 340 blank · 539 comment · 728 complexity · 251be7320969b50e535e9d86a3bcbd95 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* Long (arbitrary precision) integer object implementation */
  2. /* XXX The functional organization of this file is terrible */
  3. #include "Python.h"
  4. #include "longintrepr.h"
  5. #include <ctype.h>
  6. /* For long multiplication, use the O(N**2) school algorithm unless
  7. * both operands contain more than KARATSUBA_CUTOFF digits (this
  8. * being an internal Python long digit, in base PyLong_BASE).
  9. */
  10. #define KARATSUBA_CUTOFF 70
  11. #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
  12. /* For exponentiation, use the binary left-to-right algorithm
  13. * unless the exponent contains more than FIVEARY_CUTOFF digits.
  14. * In that case, do 5 bits at a time. The potential drawback is that
  15. * a table of 2**5 intermediate results is computed.
  16. */
  17. #define FIVEARY_CUTOFF 8
  18. #define ABS(x) ((x) < 0 ? -(x) : (x))
  19. #undef MIN
  20. #undef MAX
  21. #define MAX(x, y) ((x) < (y) ? (y) : (x))
  22. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  23. /* Forward */
  24. static PyLongObject *long_normalize(PyLongObject *);
  25. static PyLongObject *mul1(PyLongObject *, wdigit);
  26. static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
  27. static PyLongObject *divrem1(PyLongObject *, digit, digit *);
  28. #define SIGCHECK(PyTryBlock) \
  29. if (--_Py_Ticker < 0) { \
  30. _Py_Ticker = _Py_CheckInterval; \
  31. if (PyErr_CheckSignals()) PyTryBlock \
  32. }
  33. /* Normalize (remove leading zeros from) a long int object.
  34. Doesn't attempt to free the storage--in most cases, due to the nature
  35. of the algorithms used, this could save at most be one word anyway. */
  36. static PyLongObject *
  37. long_normalize(register PyLongObject *v)
  38. {
  39. Py_ssize_t j = ABS(Py_SIZE(v));
  40. Py_ssize_t i = j;
  41. while (i > 0 && v->ob_digit[i-1] == 0)
  42. --i;
  43. if (i != j)
  44. Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
  45. return v;
  46. }
  47. /* Allocate a new long int object with size digits.
  48. Return NULL and set exception if we run out of memory. */
  49. PyLongObject *
  50. _PyLong_New(Py_ssize_t size)
  51. {
  52. if (size > PY_SSIZE_T_MAX) {
  53. PyErr_NoMemory();
  54. return NULL;
  55. }
  56. /* coverity[ampersand_in_size] */
  57. /* XXX(nnorwitz): This can overflow --
  58. PyObject_NEW_VAR / _PyObject_VAR_SIZE need to detect overflow */
  59. return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
  60. }
  61. PyObject *
  62. _PyLong_Copy(PyLongObject *src)
  63. {
  64. PyLongObject *result;
  65. Py_ssize_t i;
  66. assert(src != NULL);
  67. i = src->ob_size;
  68. if (i < 0)
  69. i = -(i);
  70. result = _PyLong_New(i);
  71. if (result != NULL) {
  72. result->ob_size = src->ob_size;
  73. while (--i >= 0)
  74. result->ob_digit[i] = src->ob_digit[i];
  75. }
  76. return (PyObject *)result;
  77. }
  78. /* Create a new long int object from a C long int */
  79. PyObject *
  80. PyLong_FromLong(long ival)
  81. {
  82. PyLongObject *v;
  83. unsigned long abs_ival;
  84. unsigned long t; /* unsigned so >> doesn't propagate sign bit */
  85. int ndigits = 0;
  86. int negative = 0;
  87. if (ival < 0) {
  88. /* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then
  89. ANSI C says that the result of -ival is undefined when ival
  90. == LONG_MIN. Hence the following workaround. */
  91. abs_ival = (unsigned long)(-1-ival) + 1;
  92. negative = 1;
  93. }
  94. else {
  95. abs_ival = (unsigned long)ival;
  96. }
  97. /* Count the number of Python digits.
  98. We used to pick 5 ("big enough for anything"), but that's a
  99. waste of time and space given that 5*15 = 75 bits are rarely
  100. needed. */
  101. t = abs_ival;
  102. while (t) {
  103. ++ndigits;
  104. t >>= PyLong_SHIFT;
  105. }
  106. v = _PyLong_New(ndigits);
  107. if (v != NULL) {
  108. digit *p = v->ob_digit;
  109. v->ob_size = negative ? -ndigits : ndigits;
  110. t = abs_ival;
  111. while (t) {
  112. *p++ = (digit)(t & PyLong_MASK);
  113. t >>= PyLong_SHIFT;
  114. }
  115. }
  116. return (PyObject *)v;
  117. }
  118. /* Create a new long int object from a C unsigned long int */
  119. PyObject *
  120. PyLong_FromUnsignedLong(unsigned long ival)
  121. {
  122. PyLongObject *v;
  123. unsigned long t;
  124. int ndigits = 0;
  125. /* Count the number of Python digits. */
  126. t = (unsigned long)ival;
  127. while (t) {
  128. ++ndigits;
  129. t >>= PyLong_SHIFT;
  130. }
  131. v = _PyLong_New(ndigits);
  132. if (v != NULL) {
  133. digit *p = v->ob_digit;
  134. Py_SIZE(v) = ndigits;
  135. while (ival) {
  136. *p++ = (digit)(ival & PyLong_MASK);
  137. ival >>= PyLong_SHIFT;
  138. }
  139. }
  140. return (PyObject *)v;
  141. }
  142. /* Create a new long int object from a C double */
  143. PyObject *
  144. PyLong_FromDouble(double dval)
  145. {
  146. PyLongObject *v;
  147. double frac;
  148. int i, ndig, expo, neg;
  149. neg = 0;
  150. if (Py_IS_INFINITY(dval)) {
  151. PyErr_SetString(PyExc_OverflowError,
  152. "cannot convert float infinity to integer");
  153. return NULL;
  154. }
  155. if (Py_IS_NAN(dval)) {
  156. PyErr_SetString(PyExc_ValueError,
  157. "cannot convert float NaN to integer");
  158. return NULL;
  159. }
  160. if (dval < 0.0) {
  161. neg = 1;
  162. dval = -dval;
  163. }
  164. frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
  165. if (expo <= 0)
  166. return PyLong_FromLong(0L);
  167. ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
  168. v = _PyLong_New(ndig);
  169. if (v == NULL)
  170. return NULL;
  171. frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
  172. for (i = ndig; --i >= 0; ) {
  173. long bits = (long)frac;
  174. v->ob_digit[i] = (digit) bits;
  175. frac = frac - (double)bits;
  176. frac = ldexp(frac, PyLong_SHIFT);
  177. }
  178. if (neg)
  179. Py_SIZE(v) = -(Py_SIZE(v));
  180. return (PyObject *)v;
  181. }
  182. /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
  183. * anything about what happens when a signed integer operation overflows,
  184. * and some compilers think they're doing you a favor by being "clever"
  185. * then. The bit pattern for the largest postive signed long is
  186. * (unsigned long)LONG_MAX, and for the smallest negative signed long
  187. * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
  188. * However, some other compilers warn about applying unary minus to an
  189. * unsigned operand. Hence the weird "0-".
  190. */
  191. #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
  192. #define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
  193. /* Get a C long int from a long int object.
  194. Returns -1 and sets an error condition if overflow occurs. */
  195. long
  196. PyLong_AsLong(PyObject *vv)
  197. {
  198. /* This version by Tim Peters */
  199. register PyLongObject *v;
  200. unsigned long x, prev;
  201. Py_ssize_t i;
  202. int sign;
  203. if (vv == NULL || !PyLong_Check(vv)) {
  204. if (vv != NULL && PyInt_Check(vv))
  205. return PyInt_AsLong(vv);
  206. PyErr_BadInternalCall();
  207. return -1;
  208. }
  209. v = (PyLongObject *)vv;
  210. i = v->ob_size;
  211. sign = 1;
  212. x = 0;
  213. if (i < 0) {
  214. sign = -1;
  215. i = -(i);
  216. }
  217. while (--i >= 0) {
  218. prev = x;
  219. x = (x << PyLong_SHIFT) + v->ob_digit[i];
  220. if ((x >> PyLong_SHIFT) != prev)
  221. goto overflow;
  222. }
  223. /* Haven't lost any bits, but casting to long requires extra care
  224. * (see comment above).
  225. */
  226. if (x <= (unsigned long)LONG_MAX) {
  227. return (long)x * sign;
  228. }
  229. else if (sign < 0 && x == PY_ABS_LONG_MIN) {
  230. return LONG_MIN;
  231. }
  232. /* else overflow */
  233. overflow:
  234. PyErr_SetString(PyExc_OverflowError,
  235. "long int too large to convert to int");
  236. return -1;
  237. }
  238. /* Get a Py_ssize_t from a long int object.
  239. Returns -1 and sets an error condition if overflow occurs. */
  240. Py_ssize_t
  241. PyLong_AsSsize_t(PyObject *vv) {
  242. register PyLongObject *v;
  243. size_t x, prev;
  244. Py_ssize_t i;
  245. int sign;
  246. if (vv == NULL || !PyLong_Check(vv)) {
  247. PyErr_BadInternalCall();
  248. return -1;
  249. }
  250. v = (PyLongObject *)vv;
  251. i = v->ob_size;
  252. sign = 1;
  253. x = 0;
  254. if (i < 0) {
  255. sign = -1;
  256. i = -(i);
  257. }
  258. while (--i >= 0) {
  259. prev = x;
  260. x = (x << PyLong_SHIFT) + v->ob_digit[i];
  261. if ((x >> PyLong_SHIFT) != prev)
  262. goto overflow;
  263. }
  264. /* Haven't lost any bits, but casting to a signed type requires
  265. * extra care (see comment above).
  266. */
  267. if (x <= (size_t)PY_SSIZE_T_MAX) {
  268. return (Py_ssize_t)x * sign;
  269. }
  270. else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
  271. return PY_SSIZE_T_MIN;
  272. }
  273. /* else overflow */
  274. overflow:
  275. PyErr_SetString(PyExc_OverflowError,
  276. "long int too large to convert to int");
  277. return -1;
  278. }
  279. /* Get a C unsigned long int from a long int object.
  280. Returns -1 and sets an error condition if overflow occurs. */
  281. unsigned long
  282. PyLong_AsUnsignedLong(PyObject *vv)
  283. {
  284. register PyLongObject *v;
  285. unsigned long x, prev;
  286. Py_ssize_t i;
  287. if (vv == NULL || !PyLong_Check(vv)) {
  288. if (vv != NULL && PyInt_Check(vv)) {
  289. long val = PyInt_AsLong(vv);
  290. if (val < 0) {
  291. PyErr_SetString(PyExc_OverflowError,
  292. "can't convert negative value to unsigned long");
  293. return (unsigned long) -1;
  294. }
  295. return val;
  296. }
  297. PyErr_BadInternalCall();
  298. return (unsigned long) -1;
  299. }
  300. v = (PyLongObject *)vv;
  301. i = Py_SIZE(v);
  302. x = 0;
  303. if (i < 0) {
  304. PyErr_SetString(PyExc_OverflowError,
  305. "can't convert negative value to unsigned long");
  306. return (unsigned long) -1;
  307. }
  308. while (--i >= 0) {
  309. prev = x;
  310. x = (x << PyLong_SHIFT) + v->ob_digit[i];
  311. if ((x >> PyLong_SHIFT) != prev) {
  312. PyErr_SetString(PyExc_OverflowError,
  313. "long int too large to convert");
  314. return (unsigned long) -1;
  315. }
  316. }
  317. return x;
  318. }
  319. /* Get a C unsigned long int from a long int object, ignoring the high bits.
  320. Returns -1 and sets an error condition if an error occurs. */
  321. unsigned long
  322. PyLong_AsUnsignedLongMask(PyObject *vv)
  323. {
  324. register PyLongObject *v;
  325. unsigned long x;
  326. Py_ssize_t i;
  327. int sign;
  328. if (vv == NULL || !PyLong_Check(vv)) {
  329. if (vv != NULL && PyInt_Check(vv))
  330. return PyInt_AsUnsignedLongMask(vv);
  331. PyErr_BadInternalCall();
  332. return (unsigned long) -1;
  333. }
  334. v = (PyLongObject *)vv;
  335. i = v->ob_size;
  336. sign = 1;
  337. x = 0;
  338. if (i < 0) {
  339. sign = -1;
  340. i = -i;
  341. }
  342. while (--i >= 0) {
  343. x = (x << PyLong_SHIFT) + v->ob_digit[i];
  344. }
  345. return x * sign;
  346. }
  347. int
  348. _PyLong_Sign(PyObject *vv)
  349. {
  350. PyLongObject *v = (PyLongObject *)vv;
  351. assert(v != NULL);
  352. assert(PyLong_Check(v));
  353. return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
  354. }
  355. size_t
  356. _PyLong_NumBits(PyObject *vv)
  357. {
  358. PyLongObject *v = (PyLongObject *)vv;
  359. size_t result = 0;
  360. Py_ssize_t ndigits;
  361. assert(v != NULL);
  362. assert(PyLong_Check(v));
  363. ndigits = ABS(Py_SIZE(v));
  364. assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
  365. if (ndigits > 0) {
  366. digit msd = v->ob_digit[ndigits - 1];
  367. result = (ndigits - 1) * PyLong_SHIFT;
  368. if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
  369. goto Overflow;
  370. do {
  371. ++result;
  372. if (result == 0)
  373. goto Overflow;
  374. msd >>= 1;
  375. } while (msd);
  376. }
  377. return result;
  378. Overflow:
  379. PyErr_SetString(PyExc_OverflowError, "long has too many bits "
  380. "to express in a platform size_t");
  381. return (size_t)-1;
  382. }
  383. PyObject *
  384. _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
  385. int little_endian, int is_signed)
  386. {
  387. const unsigned char* pstartbyte;/* LSB of bytes */
  388. int incr; /* direction to move pstartbyte */
  389. const unsigned char* pendbyte; /* MSB of bytes */
  390. size_t numsignificantbytes; /* number of bytes that matter */
  391. size_t ndigits; /* number of Python long digits */
  392. PyLongObject* v; /* result */
  393. int idigit = 0; /* next free index in v->ob_digit */
  394. if (n == 0)
  395. return PyLong_FromLong(0L);
  396. if (little_endian) {
  397. pstartbyte = bytes;
  398. pendbyte = bytes + n - 1;
  399. incr = 1;
  400. }
  401. else {
  402. pstartbyte = bytes + n - 1;
  403. pendbyte = bytes;
  404. incr = -1;
  405. }
  406. if (is_signed)
  407. is_signed = *pendbyte >= 0x80;
  408. /* Compute numsignificantbytes. This consists of finding the most
  409. significant byte. Leading 0 bytes are insignficant if the number
  410. is positive, and leading 0xff bytes if negative. */
  411. {
  412. size_t i;
  413. const unsigned char* p = pendbyte;
  414. const int pincr = -incr; /* search MSB to LSB */
  415. const unsigned char insignficant = is_signed ? 0xff : 0x00;
  416. for (i = 0; i < n; ++i, p += pincr) {
  417. if (*p != insignficant)
  418. break;
  419. }
  420. numsignificantbytes = n - i;
  421. /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
  422. actually has 2 significant bytes. OTOH, 0xff0001 ==
  423. -0x00ffff, so we wouldn't *need* to bump it there; but we
  424. do for 0xffff = -0x0001. To be safe without bothering to
  425. check every case, bump it regardless. */
  426. if (is_signed && numsignificantbytes < n)
  427. ++numsignificantbytes;
  428. }
  429. /* How many Python long digits do we need? We have
  430. 8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT
  431. bits, so it's the ceiling of the quotient. */
  432. ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
  433. if (ndigits > (size_t)INT_MAX)
  434. return PyErr_NoMemory();
  435. v = _PyLong_New((int)ndigits);
  436. if (v == NULL)
  437. return NULL;
  438. /* Copy the bits over. The tricky parts are computing 2's-comp on
  439. the fly for signed numbers, and dealing with the mismatch between
  440. 8-bit bytes and (probably) 15-bit Python digits.*/
  441. {
  442. size_t i;
  443. twodigits carry = 1; /* for 2's-comp calculation */
  444. twodigits accum = 0; /* sliding register */
  445. unsigned int accumbits = 0; /* number of bits in accum */
  446. const unsigned char* p = pstartbyte;
  447. for (i = 0; i < numsignificantbytes; ++i, p += incr) {
  448. twodigits thisbyte = *p;
  449. /* Compute correction for 2's comp, if needed. */
  450. if (is_signed) {
  451. thisbyte = (0xff ^ thisbyte) + carry;
  452. carry = thisbyte >> 8;
  453. thisbyte &= 0xff;
  454. }
  455. /* Because we're going LSB to MSB, thisbyte is
  456. more significant than what's already in accum,
  457. so needs to be prepended to accum. */
  458. accum |= (twodigits)thisbyte << accumbits;
  459. accumbits += 8;
  460. if (accumbits >= PyLong_SHIFT) {
  461. /* There's enough to fill a Python digit. */
  462. assert(idigit < (int)ndigits);
  463. v->ob_digit[idigit] = (digit)(accum & PyLong_MASK);
  464. ++idigit;
  465. accum >>= PyLong_SHIFT;
  466. accumbits -= PyLong_SHIFT;
  467. assert(accumbits < PyLong_SHIFT);
  468. }
  469. }
  470. assert(accumbits < PyLong_SHIFT);
  471. if (accumbits) {
  472. assert(idigit < (int)ndigits);
  473. v->ob_digit[idigit] = (digit)accum;
  474. ++idigit;
  475. }
  476. }
  477. Py_SIZE(v) = is_signed ? -idigit : idigit;
  478. return (PyObject *)long_normalize(v);
  479. }
  480. int
  481. _PyLong_AsByteArray(PyLongObject* v,
  482. unsigned char* bytes, size_t n,
  483. int little_endian, int is_signed)
  484. {
  485. Py_ssize_t i; /* index into v->ob_digit */
  486. Py_ssize_t ndigits; /* |v->ob_size| */
  487. twodigits accum; /* sliding register */
  488. unsigned int accumbits; /* # bits in accum */
  489. int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
  490. digit carry; /* for computing 2's-comp */
  491. size_t j; /* # bytes filled */
  492. unsigned char* p; /* pointer to next byte in bytes */
  493. int pincr; /* direction to move p */
  494. assert(v != NULL && PyLong_Check(v));
  495. if (Py_SIZE(v) < 0) {
  496. ndigits = -(Py_SIZE(v));
  497. if (!is_signed) {
  498. PyErr_SetString(PyExc_TypeError,
  499. "can't convert negative long to unsigned");
  500. return -1;
  501. }
  502. do_twos_comp = 1;
  503. }
  504. else {
  505. ndigits = Py_SIZE(v);
  506. do_twos_comp = 0;
  507. }
  508. if (little_endian) {
  509. p = bytes;
  510. pincr = 1;
  511. }
  512. else {
  513. p = bytes + n - 1;
  514. pincr = -1;
  515. }
  516. /* Copy over all the Python digits.
  517. It's crucial that every Python digit except for the MSD contribute
  518. exactly PyLong_SHIFT bits to the total, so first assert that the long is
  519. normalized. */
  520. assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
  521. j = 0;
  522. accum = 0;
  523. accumbits = 0;
  524. carry = do_twos_comp ? 1 : 0;
  525. for (i = 0; i < ndigits; ++i) {
  526. digit thisdigit = v->ob_digit[i];
  527. if (do_twos_comp) {
  528. thisdigit = (thisdigit ^ PyLong_MASK) + carry;
  529. carry = thisdigit >> PyLong_SHIFT;
  530. thisdigit &= PyLong_MASK;
  531. }
  532. /* Because we're going LSB to MSB, thisdigit is more
  533. significant than what's already in accum, so needs to be
  534. prepended to accum. */
  535. accum |= (twodigits)thisdigit << accumbits;
  536. /* The most-significant digit may be (probably is) at least
  537. partly empty. */
  538. if (i == ndigits - 1) {
  539. /* Count # of sign bits -- they needn't be stored,
  540. * although for signed conversion we need later to
  541. * make sure at least one sign bit gets stored. */
  542. digit s = do_twos_comp ? thisdigit ^ PyLong_MASK :
  543. thisdigit;
  544. while (s != 0) {
  545. s >>= 1;
  546. accumbits++;
  547. }
  548. }
  549. else
  550. accumbits += PyLong_SHIFT;
  551. /* Store as many bytes as possible. */
  552. while (accumbits >= 8) {
  553. if (j >= n)
  554. goto Overflow;
  555. ++j;
  556. *p = (unsigned char)(accum & 0xff);
  557. p += pincr;
  558. accumbits -= 8;
  559. accum >>= 8;
  560. }
  561. }
  562. /* Store the straggler (if any). */
  563. assert(accumbits < 8);
  564. assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
  565. if (accumbits > 0) {
  566. if (j >= n)
  567. goto Overflow;
  568. ++j;
  569. if (do_twos_comp) {
  570. /* Fill leading bits of the byte with sign bits
  571. (appropriately pretending that the long had an
  572. infinite supply of sign bits). */
  573. accum |= (~(twodigits)0) << accumbits;
  574. }
  575. *p = (unsigned char)(accum & 0xff);
  576. p += pincr;
  577. }
  578. else if (j == n && n > 0 && is_signed) {
  579. /* The main loop filled the byte array exactly, so the code
  580. just above didn't get to ensure there's a sign bit, and the
  581. loop below wouldn't add one either. Make sure a sign bit
  582. exists. */
  583. unsigned char msb = *(p - pincr);
  584. int sign_bit_set = msb >= 0x80;
  585. assert(accumbits == 0);
  586. if (sign_bit_set == do_twos_comp)
  587. return 0;
  588. else
  589. goto Overflow;
  590. }
  591. /* Fill remaining bytes with copies of the sign bit. */
  592. {
  593. unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
  594. for ( ; j < n; ++j, p += pincr)
  595. *p = signbyte;
  596. }
  597. return 0;
  598. Overflow:
  599. PyErr_SetString(PyExc_OverflowError, "long too big to convert");
  600. return -1;
  601. }
  602. double
  603. _PyLong_AsScaledDouble(PyObject *vv, int *exponent)
  604. {
  605. /* NBITS_WANTED should be > the number of bits in a double's precision,
  606. but small enough so that 2**NBITS_WANTED is within the normal double
  607. range. nbitsneeded is set to 1 less than that because the most-significant
  608. Python digit contains at least 1 significant bit, but we don't want to
  609. bother counting them (catering to the worst case cheaply).
  610. 57 is one more than VAX-D double precision; I (Tim) don't know of a double
  611. format with more precision than that; it's 1 larger so that we add in at
  612. least one round bit to stand in for the ignored least-significant bits.
  613. */
  614. #define NBITS_WANTED 57
  615. PyLongObject *v;
  616. double x;
  617. const double multiplier = (double)(1L << PyLong_SHIFT);
  618. Py_ssize_t i;
  619. int sign;
  620. int nbitsneeded;
  621. if (vv == NULL || !PyLong_Check(vv)) {
  622. PyErr_BadInternalCall();
  623. return -1;
  624. }
  625. v = (PyLongObject *)vv;
  626. i = Py_SIZE(v);
  627. sign = 1;
  628. if (i < 0) {
  629. sign = -1;
  630. i = -(i);
  631. }
  632. else if (i == 0) {
  633. *exponent = 0;
  634. return 0.0;
  635. }
  636. --i;
  637. x = (double)v->ob_digit[i];
  638. nbitsneeded = NBITS_WANTED - 1;
  639. /* Invariant: i Python digits remain unaccounted for. */
  640. while (i > 0 && nbitsneeded > 0) {
  641. --i;
  642. x = x * multiplier + (double)v->ob_digit[i];
  643. nbitsneeded -= PyLong_SHIFT;
  644. }
  645. /* There are i digits we didn't shift in. Pretending they're all
  646. zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
  647. *exponent = i;
  648. assert(x > 0.0);
  649. return x * sign;
  650. #undef NBITS_WANTED
  651. }
  652. /* Get a C double from a long int object. */
  653. double
  654. PyLong_AsDouble(PyObject *vv)
  655. {
  656. int e = -1;
  657. double x;
  658. if (vv == NULL || !PyLong_Check(vv)) {
  659. PyErr_BadInternalCall();
  660. return -1;
  661. }
  662. x = _PyLong_AsScaledDouble(vv, &e);
  663. if (x == -1.0 && PyErr_Occurred())
  664. return -1.0;
  665. /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
  666. set correctly after a successful _PyLong_AsScaledDouble() call */
  667. assert(e >= 0);
  668. if (e > INT_MAX / PyLong_SHIFT)
  669. goto overflow;
  670. errno = 0;
  671. x = ldexp(x, e * PyLong_SHIFT);
  672. if (Py_OVERFLOWED(x))
  673. goto overflow;
  674. return x;
  675. overflow:
  676. PyErr_SetString(PyExc_OverflowError,
  677. "long int too large to convert to float");
  678. return -1.0;
  679. }
  680. /* Create a new long (or int) object from a C pointer */
  681. PyObject *
  682. PyLong_FromVoidPtr(void *p)
  683. {
  684. #if SIZEOF_VOID_P <= SIZEOF_LONG
  685. if ((long)p < 0)
  686. return PyLong_FromUnsignedLong((unsigned long)p);
  687. return PyInt_FromLong((long)p);
  688. #else
  689. #ifndef HAVE_LONG_LONG
  690. # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
  691. #endif
  692. #if SIZEOF_LONG_LONG < SIZEOF_VOID_P
  693. # error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
  694. #endif
  695. /* optimize null pointers */
  696. if (p == NULL)
  697. return PyInt_FromLong(0);
  698. return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
  699. #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
  700. }
  701. /* Get a C pointer from a long object (or an int object in some cases) */
  702. void *
  703. PyLong_AsVoidPtr(PyObject *vv)
  704. {
  705. /* This function will allow int or long objects. If vv is neither,
  706. then the PyLong_AsLong*() functions will raise the exception:
  707. PyExc_SystemError, "bad argument to internal function"
  708. */
  709. #if SIZEOF_VOID_P <= SIZEOF_LONG
  710. long x;
  711. if (PyInt_Check(vv))
  712. x = PyInt_AS_LONG(vv);
  713. else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
  714. x = PyLong_AsLong(vv);
  715. else
  716. x = PyLong_AsUnsignedLong(vv);
  717. #else
  718. #ifndef HAVE_LONG_LONG
  719. # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
  720. #endif
  721. #if SIZEOF_LONG_LONG < SIZEOF_VOID_P
  722. # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
  723. #endif
  724. PY_LONG_LONG x;
  725. if (PyInt_Check(vv))
  726. x = PyInt_AS_LONG(vv);
  727. else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
  728. x = PyLong_AsLongLong(vv);
  729. else
  730. x = PyLong_AsUnsignedLongLong(vv);
  731. #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
  732. if (x == -1 && PyErr_Occurred())
  733. return NULL;
  734. return (void *)x;
  735. }
  736. #ifdef HAVE_LONG_LONG
  737. /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
  738. * rewritten to use the newer PyLong_{As,From}ByteArray API.
  739. */
  740. #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
  741. /* Create a new long int object from a C PY_LONG_LONG int. */
  742. PyObject *
  743. PyLong_FromLongLong(PY_LONG_LONG ival)
  744. {
  745. PyLongObject *v;
  746. unsigned PY_LONG_LONG abs_ival;
  747. unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
  748. int ndigits = 0;
  749. int negative = 0;
  750. if (ival < 0) {
  751. /* avoid signed overflow on negation; see comments
  752. in PyLong_FromLong above. */
  753. abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1;
  754. negative = 1;
  755. }
  756. else {
  757. abs_ival = (unsigned PY_LONG_LONG)ival;
  758. }
  759. /* Count the number of Python digits.
  760. We used to pick 5 ("big enough for anything"), but that's a
  761. waste of time and space given that 5*15 = 75 bits are rarely
  762. needed. */
  763. t = abs_ival;
  764. while (t) {
  765. ++ndigits;
  766. t >>= PyLong_SHIFT;
  767. }
  768. v = _PyLong_New(ndigits);
  769. if (v != NULL) {
  770. digit *p = v->ob_digit;
  771. Py_SIZE(v) = negative ? -ndigits : ndigits;
  772. t = abs_ival;
  773. while (t) {
  774. *p++ = (digit)(t & PyLong_MASK);
  775. t >>= PyLong_SHIFT;
  776. }
  777. }
  778. return (PyObject *)v;
  779. }
  780. /* Create a new long int object from a C unsigned PY_LONG_LONG int. */
  781. PyObject *
  782. PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
  783. {
  784. PyLongObject *v;
  785. unsigned PY_LONG_LONG t;
  786. int ndigits = 0;
  787. /* Count the number of Python digits. */
  788. t = (unsigned PY_LONG_LONG)ival;
  789. while (t) {
  790. ++ndigits;
  791. t >>= PyLong_SHIFT;
  792. }
  793. v = _PyLong_New(ndigits);
  794. if (v != NULL) {
  795. digit *p = v->ob_digit;
  796. Py_SIZE(v) = ndigits;
  797. while (ival) {
  798. *p++ = (digit)(ival & PyLong_MASK);
  799. ival >>= PyLong_SHIFT;
  800. }
  801. }
  802. return (PyObject *)v;
  803. }
  804. /* Create a new long int object from a C Py_ssize_t. */
  805. PyObject *
  806. PyLong_FromSsize_t(Py_ssize_t ival)
  807. {
  808. Py_ssize_t bytes = ival;
  809. int one = 1;
  810. return _PyLong_FromByteArray(
  811. (unsigned char *)&bytes,
  812. SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1);
  813. }
  814. /* Create a new long int object from a C size_t. */
  815. PyObject *
  816. PyLong_FromSize_t(size_t ival)
  817. {
  818. size_t bytes = ival;
  819. int one = 1;
  820. return _PyLong_FromByteArray(
  821. (unsigned char *)&bytes,
  822. SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
  823. }
  824. /* Get a C PY_LONG_LONG int from a long int object.
  825. Return -1 and set an error if overflow occurs. */
  826. PY_LONG_LONG
  827. PyLong_AsLongLong(PyObject *vv)
  828. {
  829. PY_LONG_LONG bytes;
  830. int one = 1;
  831. int res;
  832. if (vv == NULL) {
  833. PyErr_BadInternalCall();
  834. return -1;
  835. }
  836. if (!PyLong_Check(vv)) {
  837. PyNumberMethods *nb;
  838. PyObject *io;
  839. if (PyInt_Check(vv))
  840. return (PY_LONG_LONG)PyInt_AsLong(vv);
  841. if ((nb = vv->ob_type->tp_as_number) == NULL ||
  842. nb->nb_int == NULL) {
  843. PyErr_SetString(PyExc_TypeError, "an integer is required");
  844. return -1;
  845. }
  846. io = (*nb->nb_int) (vv);
  847. if (io == NULL)
  848. return -1;
  849. if (PyInt_Check(io)) {
  850. bytes = PyInt_AsLong(io);
  851. Py_DECREF(io);
  852. return bytes;
  853. }
  854. if (PyLong_Check(io)) {
  855. bytes = PyLong_AsLongLong(io);
  856. Py_DECREF(io);
  857. return bytes;
  858. }
  859. Py_DECREF(io);
  860. PyErr_SetString(PyExc_TypeError, "integer conversion failed");
  861. return -1;
  862. }
  863. res = _PyLong_AsByteArray(
  864. (PyLongObject *)vv, (unsigned char *)&bytes,
  865. SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
  866. /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
  867. if (res < 0)
  868. return (PY_LONG_LONG)-1;
  869. else
  870. return bytes;
  871. }
  872. /* Get a C unsigned PY_LONG_LONG int from a long int object.
  873. Return -1 and set an error if overflow occurs. */
  874. unsigned PY_LONG_LONG
  875. PyLong_AsUnsignedLongLong(PyObject *vv)
  876. {
  877. unsigned PY_LONG_LONG bytes;
  878. int one = 1;
  879. int res;
  880. if (vv == NULL || !PyLong_Check(vv)) {
  881. PyErr_BadInternalCall();
  882. return (unsigned PY_LONG_LONG)-1;
  883. }
  884. res = _PyLong_AsByteArray(
  885. (PyLongObject *)vv, (unsigned char *)&bytes,
  886. SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
  887. /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
  888. if (res < 0)
  889. return (unsigned PY_LONG_LONG)res;
  890. else
  891. return bytes;
  892. }
  893. /* Get a C unsigned long int from a long int object, ignoring the high bits.
  894. Returns -1 and sets an error condition if an error occurs. */
  895. unsigned PY_LONG_LONG
  896. PyLong_AsUnsignedLongLongMask(PyObject *vv)
  897. {
  898. register PyLongObject *v;
  899. unsigned PY_LONG_LONG x;
  900. Py_ssize_t i;
  901. int sign;
  902. if (vv == NULL || !PyLong_Check(vv)) {
  903. PyErr_BadInternalCall();
  904. return (unsigned long) -1;
  905. }
  906. v = (PyLongObject *)vv;
  907. i = v->ob_size;
  908. sign = 1;
  909. x = 0;
  910. if (i < 0) {
  911. sign = -1;
  912. i = -i;
  913. }
  914. while (--i >= 0) {
  915. x = (x << PyLong_SHIFT) + v->ob_digit[i];
  916. }
  917. return x * sign;
  918. }
  919. #undef IS_LITTLE_ENDIAN
  920. #endif /* HAVE_LONG_LONG */
  921. static int
  922. convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
  923. if (PyLong_Check(v)) {
  924. *a = (PyLongObject *) v;
  925. Py_INCREF(v);
  926. }
  927. else if (PyInt_Check(v)) {
  928. *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
  929. }
  930. else {
  931. return 0;
  932. }
  933. if (PyLong_Check(w)) {
  934. *b = (PyLongObject *) w;
  935. Py_INCREF(w);
  936. }
  937. else if (PyInt_Check(w)) {
  938. *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
  939. }
  940. else {
  941. Py_DECREF(*a);
  942. return 0;
  943. }
  944. return 1;
  945. }
  946. #define CONVERT_BINOP(v, w, a, b) \
  947. if (!convert_binop(v, w, a, b)) { \
  948. Py_INCREF(Py_NotImplemented); \
  949. return Py_NotImplemented; \
  950. }
  951. /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
  952. * is modified in place, by adding y to it. Carries are propagated as far as
  953. * x[m-1], and the remaining carry (0 or 1) is returned.
  954. */
  955. static digit
  956. v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
  957. {
  958. Py_ssize_t i;
  959. digit carry = 0;
  960. assert(m >= n);
  961. for (i = 0; i < n; ++i) {
  962. carry += x[i] + y[i];
  963. x[i] = carry & PyLong_MASK;
  964. carry >>= PyLong_SHIFT;
  965. assert((carry & 1) == carry);
  966. }
  967. for (; carry && i < m; ++i) {
  968. carry += x[i];
  969. x[i] = carry & PyLong_MASK;
  970. carry >>= PyLong_SHIFT;
  971. assert((carry & 1) == carry);
  972. }
  973. return carry;
  974. }
  975. /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
  976. * is modified in place, by subtracting y from it. Borrows are propagated as
  977. * far as x[m-1], and the remaining borrow (0 or 1) is returned.
  978. */
  979. static digit
  980. v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
  981. {
  982. Py_ssize_t i;
  983. digit borrow = 0;
  984. assert(m >= n);
  985. for (i = 0; i < n; ++i) {
  986. borrow = x[i] - y[i] - borrow;
  987. x[i] = borrow & PyLong_MASK;
  988. borrow >>= PyLong_SHIFT;
  989. borrow &= 1; /* keep only 1 sign bit */
  990. }
  991. for (; borrow && i < m; ++i) {
  992. borrow = x[i] - borrow;
  993. x[i] = borrow & PyLong_MASK;
  994. borrow >>= PyLong_SHIFT;
  995. borrow &= 1;
  996. }
  997. return borrow;
  998. }
  999. /* Multiply by a single digit, ignoring the sign. */
  1000. static PyLongObject *
  1001. mul1(PyLongObject *a, wdigit n)
  1002. {
  1003. return muladd1(a, n, (digit)0);
  1004. }
  1005. /* Multiply by a single digit and add a single digit, ignoring the sign. */
  1006. static PyLongObject *
  1007. muladd1(PyLongObject *a, wdigit n, wdigit extra)
  1008. {
  1009. Py_ssize_t size_a = ABS(Py_SIZE(a));
  1010. PyLongObject *z = _PyLong_New(size_a+1);
  1011. twodigits carry = extra;
  1012. Py_ssize_t i;
  1013. if (z == NULL)
  1014. return NULL;
  1015. for (i = 0; i < size_a; ++i) {
  1016. carry += (twodigits)a->ob_digit[i] * n;
  1017. z->ob_digit[i] = (digit) (carry & PyLong_MASK);
  1018. carry >>= PyLong_SHIFT;
  1019. }
  1020. z->ob_digit[i] = (digit) carry;
  1021. return long_normalize(z);
  1022. }
  1023. /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
  1024. in pout, and returning the remainder. pin and pout point at the LSD.
  1025. It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
  1026. _PyLong_Format, but that should be done with great care since longs are
  1027. immutable. */
  1028. static digit
  1029. inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
  1030. {
  1031. twodigits rem = 0;
  1032. assert(n > 0 && n <= PyLong_MASK);
  1033. pin += size;
  1034. pout += size;
  1035. while (--size >= 0) {
  1036. digit hi;
  1037. rem = (rem << PyLong_SHIFT) + *--pin;
  1038. *--pout = hi = (digit)(rem / n);
  1039. rem -= (twodigits)hi * n;
  1040. }
  1041. return (digit)rem;
  1042. }
  1043. /* Divide a long integer by a digit, returning both the quotient
  1044. (as function result) and the remainder (through *prem).
  1045. The sign of a is ignored; n should not be zero. */
  1046. static PyLongObject *
  1047. divrem1(PyLongObject *a, digit n, digit *prem)
  1048. {
  1049. const Py_ssize_t size = ABS(Py_SIZE(a));
  1050. PyLongObject *z;
  1051. assert(n > 0 && n <= PyLong_MASK);
  1052. z = _PyLong_New(size);
  1053. if (z == NULL)
  1054. return NULL;
  1055. *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
  1056. return long_normalize(z);
  1057. }
  1058. /* Convert the long to a string object with given base,
  1059. appending a base prefix of 0[box] if base is 2, 8 or 16.
  1060. Add a trailing "L" if addL is non-zero.
  1061. If newstyle is zero, then use the pre-2.6 behavior of octal having
  1062. a leading "0", instead of the prefix "0o" */
  1063. PyAPI_FUNC(PyObject *)
  1064. _PyLong_Format(PyObject *aa, int base, int addL, int newstyle)
  1065. {
  1066. register PyLongObject *a = (PyLongObject *)aa;
  1067. PyStringObject *str;
  1068. Py_ssize_t i, sz;
  1069. Py_ssize_t size_a;
  1070. char *p;
  1071. int bits;
  1072. char sign = '\0';
  1073. if (a == NULL || !PyLong_Check(a)) {
  1074. PyErr_BadInternalCall();
  1075. return NULL;
  1076. }
  1077. assert(base >= 2 && base <= 36);
  1078. size_a = ABS(Py_SIZE(a));
  1079. /* Compute a rough upper bound for the length of the string */
  1080. i = base;
  1081. bits = 0;
  1082. while (i > 1) {
  1083. ++bits;
  1084. i >>= 1;
  1085. }
  1086. i = 5 + (addL ? 1 : 0);
  1087. /* ensure we don't get signed overflow in sz calculation */
  1088. if (size_a > (PY_SSIZE_T_MAX - i) / PyLong_SHIFT) {
  1089. PyErr_SetString(PyExc_OverflowError,
  1090. "long is too large to format");
  1091. return NULL;
  1092. }
  1093. sz = i + 1 + (size_a * PyLong_SHIFT - 1) / bits;
  1094. assert(sz >= 0);
  1095. str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz);
  1096. if (str == NULL)
  1097. return NULL;
  1098. p = PyString_AS_STRING(str) + sz;
  1099. *p = '\0';
  1100. if (addL)
  1101. *--p = 'L';
  1102. if (a->ob_size < 0)
  1103. sign = '-';
  1104. if (a->ob_size == 0) {
  1105. *--p = '0';
  1106. }
  1107. else if ((base & (base - 1)) == 0) {
  1108. /* JRH: special case for power-of-2 bases */
  1109. twodigits accum = 0;
  1110. int accumbits = 0; /* # of bits in accum */
  1111. int basebits = 1; /* # of bits in base-1 */
  1112. i = base;
  1113. while ((i >>= 1) > 1)
  1114. ++basebits;
  1115. for (i = 0; i < size_a; ++i) {
  1116. accum |= (twodigits)a->ob_digit[i] << accumbits;
  1117. accumbits += PyLong_SHIFT;
  1118. assert(accumbits >= basebits);
  1119. do {
  1120. char cdigit = (char)(accum & (base - 1));
  1121. cdigit += (cdigit < 10) ? '0' : 'a'-10;
  1122. assert(p > PyString_AS_STRING(str));
  1123. *--p = cdigit;
  1124. accumbits -= basebits;
  1125. accum >>= basebits;
  1126. } while (i < size_a-1 ? accumbits >= basebits :
  1127. accum > 0);
  1128. }
  1129. }
  1130. else {
  1131. /* Not 0, and base not a power of 2. Divide repeatedly by
  1132. base, but for speed use the highest power of base that
  1133. fits in a digit. */
  1134. Py_ssize_t size = size_a;
  1135. digit *pin = a->ob_digit;
  1136. PyLongObject *scratch;
  1137. /* powbasw <- largest power of base that fits in a digit. */
  1138. digit powbase = base; /* powbase == base ** power */
  1139. int power = 1;
  1140. for (;;) {
  1141. unsigned long newpow = powbase * (unsigned long)base;
  1142. if (newpow >> PyLong_SHIFT)
  1143. /* doesn't fit in a digit */
  1144. break;
  1145. powbase = (digit)newpow;
  1146. ++power;
  1147. }
  1148. /* Get a scratch area for repeated division. */
  1149. scratch = _PyLong_New(size);
  1150. if (scratch == NULL) {
  1151. Py_DECREF(str);
  1152. return NULL;
  1153. }
  1154. /* Repeatedly divide by powbase. */
  1155. do {
  1156. int ntostore = power;
  1157. digit rem = inplace_divrem1(scratch->ob_digit,
  1158. pin, size, powbase);
  1159. pin = scratch->ob_digit; /* no need to use a again */
  1160. if (pin[size - 1] == 0)
  1161. --size;
  1162. SIGCHECK({
  1163. Py_DECREF(scratch);
  1164. Py_DECREF(str);
  1165. return NULL;
  1166. })
  1167. /* Break rem into digits. */
  1168. assert(ntostore > 0);
  1169. do {
  1170. digit nextrem = (digit)(rem / base);
  1171. char c = (char)(rem - nextrem * base);
  1172. assert(p > PyString_AS_STRING(str));
  1173. c += (c < 10) ? '0' : 'a'-10;
  1174. *--p = c;
  1175. rem = nextrem;
  1176. --ntostore;
  1177. /* Termination is a bit delicate: must not
  1178. store leading zeroes, so must get out if
  1179. remaining quotient and rem are both 0. */
  1180. } while (ntostore && (size || rem));
  1181. } while (size != 0);
  1182. Py_DECREF(scratch);
  1183. }
  1184. if (base == 2) {
  1185. *--p = 'b';
  1186. *--p = '0';
  1187. }
  1188. else if (base == 8) {
  1189. if (newstyle) {
  1190. *--p = 'o';
  1191. *--p = '0';
  1192. }
  1193. else
  1194. if (size_a != 0)
  1195. *--p = '0';
  1196. }
  1197. else if (base == 16) {
  1198. *--p = 'x';
  1199. *--p = '0';
  1200. }
  1201. else if (base != 10) {
  1202. *--p = '#';
  1203. *--p = '0' + base%10;
  1204. if (base > 10)
  1205. *--p = '0' + base/10;
  1206. }
  1207. if (sign)
  1208. *--p = sign;
  1209. if (p != PyString_AS_STRING(str)) {
  1210. char *q = PyString_AS_STRING(str);
  1211. assert(p > q);
  1212. do {
  1213. } while ((*q++ = *p++) != '\0');
  1214. q--;
  1215. _PyString_Resize((PyObject **)&str,
  1216. (Py_ssize_t) (q - PyString_AS_STRING(str)));
  1217. }
  1218. return (PyObject *)str;
  1219. }
  1220. /* Table of digit values for 8-bit string -> integer conversion.
  1221. * '0' maps to 0, ..., '9' maps to 9.
  1222. * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
  1223. * All other indices map to 37.
  1224. * Note that when converting a base B string, a char c is a legitimate
  1225. * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
  1226. */
  1227. int _PyLong_DigitValue[256] = {
  1228. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1229. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1230. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1231. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
  1232. 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  1233. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
  1234. 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  1235. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
  1236. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1237. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1238. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1239. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1240. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1241. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1242. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1243. 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
  1244. };
  1245. /* *str points to the first digit in a string of base `base` digits. base
  1246. * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
  1247. * non-digit (which may be *str!). A normalized long is returned.
  1248. * The point to this routine is that it takes time linear in the number of
  1249. * string characters.
  1250. */
  1251. static PyLongObject *
  1252. long_from_binary_base(char **str, int base)
  1253. {
  1254. char *p = *str;
  1255. char *start = p;
  1256. int bits_per_char;
  1257. Py_ssize_t n;
  1258. PyLongObject *z;
  1259. twodigits accum;
  1260. int bits_in_accum;
  1261. digit *pdigit;
  1262. assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
  1263. n = base;
  1264. for (bits_per_char = -1; n; ++bits_per_char)
  1265. n >>= 1;
  1266. /* n <- total # of bits needed, while setting p to end-of-string */
  1267. while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
  1268. ++p;
  1269. *str = p;
  1270. /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
  1271. n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
  1272. if (n / bits_per_char < p - start) {
  1273. PyErr_SetString(PyExc_ValueError,
  1274. "long string too large to convert");
  1275. return NULL;
  1276. }
  1277. n = n / PyLong_SHIFT;
  1278. z = _PyLong_New(n);
  1279. if (z == NULL)
  1280. return NULL;
  1281. /* Read string from right, and fill in long from left; i.e.,
  1282. * from least to most significant in both.
  1283. */
  1284. accum = 0;
  1285. bits_in_accum = 0;
  1286. pdigit = z->ob_digit;
  1287. while (--p >= start) {
  1288. int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
  1289. assert(k >= 0 && k < base);
  1290. accum |= (twodigits)k << bits_in_accum;
  1291. bits_in_accum += bits_per_char;
  1292. if (bits_in_accum >= PyLong_SHIFT) {
  1293. *pdigit++ = (digit)(accum & PyLong_MASK);
  1294. assert(pdigit - z->ob_digit <= n);
  1295. accum >>= PyLong_SHIFT;
  1296. bits_in_accum -= PyLong_SHIFT;
  1297. assert(bits_in_accum < PyLong_SHIFT);
  1298. }
  1299. }
  1300. if (bits_in_accum) {
  1301. assert(bits_in_accum <= PyLong_SHIFT);
  1302. *pdigit++ = (digit)accum;
  1303. assert(pdigit - z->ob_digit <= n);
  1304. }
  1305. while (pdigit - z->ob_digit < n)
  1306. *pdigit++ = 0;
  1307. return long_normalize(z);
  1308. }
  1309. PyObject *
  1310. PyLong_FromString(char *str, char **pend, int base)
  1311. {
  1312. int sign = 1;
  1313. char *start, *orig_str = str;
  1314. PyLongObject *z;
  1315. PyObject *strobj, *strrepr;
  1316. Py_ssize_t slen;
  1317. if ((base != 0 && base < 2) || base > 36) {
  1318. PyErr_SetString(PyExc_ValueError,
  1319. "long() arg 2 must be >= 2 and <= 36");
  1320. return NULL;
  1321. }
  1322. while (*str != '\0' && isspace(Py_CHARMASK(*str)))
  1323. str++;
  1324. if (*str == '+')
  1325. ++str;
  1326. else if (*str == '-') {
  1327. ++str;
  1328. sign = -1;
  1329. }
  1330. while (*str != '\0' && isspace(Py_CHARMASK(*str)))
  1331. str++;
  1332. if (base == 0) {
  1333. /* No base given. Deduce the base from the contents
  1334. of the string */
  1335. if (str[0] != '0')
  1336. base = 10;
  1337. else if (str[1] == 'x' || str[1] == 'X')
  1338. base = 16;
  1339. else if (str[1] == 'o' || str[1] == 'O')
  1340. base = 8;
  1341. else if (str[1] == 'b' || str[1] == 'B')
  1342. base = 2;
  1343. else
  1344. /* "old" (C-style) octal literal, still valid in
  1345. 2.x, although illegal in 3.x */
  1346. base = 8;
  1347. }
  1348. /* Whether or not we were deducing the base, skip leading chars
  1349. as needed */
  1350. if (str[0] == '0' &&
  1351. ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
  1352. (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
  1353. (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
  1354. str += 2;
  1355. start = str;
  1356. if ((base & (base - 1)) == 0)
  1357. z = long_from_binary_base(&str, base);
  1358. else {
  1359. /***
  1360. Binary bases can be converted in time linear in the number of digits, because
  1361. Python's representation base is binary. Other bases (including decimal!) use
  1362. the simple quadratic-time algorithm below, complicated by some speed tricks.
  1363. First some math: the largest integer that can be expressed in N base-B digits
  1364. is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
  1365. case number of Python digits needed to hold it is the smallest integer n s.t.
  1366. PyLong_BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
  1367. PyLong_BASE**n >= B**N [taking logs to base PyLong_BASE]
  1368. n >= log(B**N)/log(PyLong_BASE) = N * log(B)/log(PyLong_BASE)
  1369. The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so we can compute
  1370. this quickly. A Python long with that much space is reserved near the start,
  1371. and the result is computed into it.
  1372. The input string is actually treated as being in base base**i (i.e., i digits
  1373. are processed at a time), where two more static arrays hold:
  1374. convwidth_base[base] = the largest integer i such that base**i <= PyLong_BASE
  1375. convmultmax_base[base] = base ** convwidth_base[base]
  1376. The first of these is the largest i such that i consecutive input digits
  1377. must fit in a single Python digit. The second is effectively the input
  1378. base we're really using.
  1379. Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
  1380. convmultmax_base[base], the result is "simply"
  1381. (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
  1382. where B = convmultmax_base[base].
  1383. Error analysis: as above, the number of Python digits `n` needed is worst-
  1384. case
  1385. n >= N * log(B)/log(PyLong_BASE)
  1386. where `N` is the number of input digits in base `B`. This is computed via
  1387. size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
  1388. below. Two numeric concerns are how much space this can waste, and whether
  1389. the computed result can be too small. To be concrete, assume PyLong_BASE = 2**15,
  1390. which is the default (and it's unlikely anyone changes that).
  1391. Waste isn't a problem: provided the first input digit isn't 0, the difference
  1392. between the worst-case input with N digits and the smallest input with N
  1393. digits is about a factor of B, but B is small compared to PyLong_BASE so at most
  1394. one allocated Python digit can remain unused on that count. If
  1395. N*log(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating that
  1396. and adding 1 returns a result 1 larger than necessary. However, that can't
  1397. happen: whenever B is a power of 2, long_from_binary_base() is called
  1398. instead, and it's impossible for B**i to be an integer power of 2**15 when
  1399. B is not a power of 2 (i.e., it's impossible for N*log(B)/log(PyLong_BASE) to be
  1400. an exact integer when B is not a power of 2, since B**i has a prime factor
  1401. other than 2 in that case, but (2**15)**j's only prime factor is 2).
  1402. The computed result can be too small if the true value of N*log(B)/log(PyLong_BASE)
  1403. is a little bit larger than an exact integer, but due to roundoff errors (in
  1404. computing log(B), log(PyLong_BASE), their quotient, and/or multiplying that by N)
  1405. yields a numeric result a little less than that integer. Unfortunately, "how
  1406. close can a transcendental function get to an integer over some range?"
  1407. questions are generally theoretically intractable. Computer analysis via
  1408. continued fractions is practical: expand log(B)/log(PyLong_BASE) via continued
  1409. fractions, giving a sequence i/j of "the best" rational approximations. Then
  1410. j*log(B)/log(PyLong_BASE) is approximately equal to (the integer) i. This shows that
  1411. we can get very close to being in trouble, but very rarely. For example,
  1412. 76573 is a denominator in one of the continued-fraction approximations to
  1413. log(10)/log(2**15), and indeed:
  1414. >>> log(10)/log(2**15)*76573
  1415. 16958.000000654003
  1416. is very close to an integer. If we were working with IEEE single-precision,
  1417. rounding errors could kill us. Finding worst cases in IEEE double-precision
  1418. requires better-than-double-precision log() functions, and Tim didn't bother.
  1419. Instead the code checks to see whether the allocated space is enough as each
  1420. new Python digit is added, and copies the whole thing to a larger long if not.
  1421. This should happen extremely rarely, and in fact I don't have a test case
  1422. that triggers it(!). Instead the code was tested by artificially allocating
  1423. just 1 digit at the start, so that the copying code was exercised for every
  1424. digit beyond the first.
  1425. ***/
  1426. register twodigits c; /* current input character */
  1427. Py_ssize_t size_z;
  1428. int i;
  1429. int convwidth;
  1430. twodigits convmultmax, convmult;
  1431. digit *pz, *pzstop;
  1432. char* scan;
  1433. static double log_base_PyLong_BASE[37] = {0.0e0,};
  1434. static int convwidth_base[37] = {0,};
  1435. static twodigits convmultmax_base[37] = {0,};
  1436. if (log_base_PyLong_BASE[base] == 0.0) {
  1437. twodigits convmax = base;
  1438. int i = 1;
  1439. log_base_PyLong_BASE[base] = log((double)base) /
  1440. log((double)PyLong_BASE);
  1441. for (;;) {
  1442. twodigits next = convmax * base;
  1443. if (next > PyLong_BASE)
  1444. break;
  1445. convmax = next;
  1446. ++i;
  1447. }
  1448. convmultmax_base[base] = convmax;
  1449. assert(i > 0);
  1450. convwidth_base[base] = i;
  1451. }
  1452. /* Find length of the string of numeric characters. */
  1453. scan = str;
  1454. while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
  1455. ++scan;
  1456. /* Create a long object that can contain the largest possible
  1457. * integer with this base and length. Note that there's no
  1458. * need to initialize z->ob_digit -- no slot is read up before
  1459. * being stored into.
  1460. */
  1461. size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
  1462. /* Uncomment next line to test exceedingly rare copy code */
  1463. /* size_z = 1; */
  1464. assert(size_z > 0);
  1465. z = _PyLong_New(size_z);
  1466. if (z == NULL)
  1467. return NULL;
  1468. Py_SIZE(z) = 0;
  1469. /* `convwidth` consecutive input digits are treated as a single
  1470. * digit in base `convmultmax`.
  1471. */
  1472. convwidth = convwidth_base[base];
  1473. convmultmax = convmultmax_base[base];
  1474. /* Work ;-) */
  1475. while (str < scan) {
  1476. /* grab up to convwidth digits from the input string */
  1477. c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
  1478. for (i = 1; i < convwidth && str != scan; ++i, ++str) {
  1479. c = (twodigits)(c * base +
  1480. _PyLong_DigitValue[Py_CHARMASK(*str)]);
  1481. assert(c < PyLong_BASE);
  1482. }
  1483. convmult = convmultmax;
  1484. /* Calculate the shift only if we couldn't get
  1485. * convwidth digits.
  1486. */
  1487. if (i != convwidth) {
  1488. convmult = base;
  1489. for ( ; i > 1; --i)
  1490. convmult *= base;
  1491. }
  1492. /* Multiply z by convmult, and add c. */
  1493. pz = z->ob_digit;
  1494. pzstop = pz + Py_SIZE(z);
  1495. for (; pz < pzstop; ++pz) {
  1496. c += (twodigits)*pz * convmult;
  1497. *pz = (digit)(c & PyLong_MASK);
  1498. c >>= PyLong_SHIFT;
  1499. }
  1500. /* carry off the current end? */
  1501. if (c) {
  1502. assert(c < PyLong_BASE);
  1503. if (Py_SIZE(z) < size_z) {
  1504. *pz = (digit)c;
  1505. ++Py_SIZE(z);
  1506. }
  1507. else {
  1508. PyLongObject *tmp;
  1509. /* Extremely rare. Get more space. */
  1510. assert(Py_SIZE(z) == size_z);
  1511. tmp = _PyLong_New(size_z + 1);
  1512. if (tmp == NULL) {
  1513. Py_DECREF(z);
  1514. return NULL;
  1515. }
  1516. memcpy(tmp->ob_digit,
  1517. z->ob_digit,
  1518. sizeof(digit) * size_z);
  1519. Py_DECREF(z);
  1520. z = tmp;
  1521. z->ob_digit[size_z] = (digit)c;
  1522. ++size_z;
  1523. }
  1524. }
  1525. }
  1526. }
  1527. if (z == NULL)
  1528. return NULL;
  1529. if (str == start)
  1530. goto onError;
  1531. if (sign < 0)
  1532. Py_SIZE(z) = -(Py_SIZE(z));
  1533. if (*str == 'L' || *str == 'l')
  1534. str++;
  1535. while (*str && isspace(Py_CHARMASK(*str)))
  1536. str++;
  1537. if (*str != '\0')
  1538. goto onError;
  1539. if (pend)
  1540. *pend = str;
  1541. return (PyObject *) z;
  1542. onError:
  1543. Py_XDECREF(z);
  1544. slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
  1545. strobj = PyString_FromStringAndSize(orig_str, slen);
  1546. if (strobj == NULL)
  1547. return NULL;
  1548. strrepr = PyObject_Repr(strobj);
  1549. Py_DECREF(strobj);
  1550. if (strrepr == NULL)
  1551. return NULL;
  1552. PyErr_Format(PyExc_ValueError,
  1553. "invalid literal for long() with base %d: %s",
  1554. base, PyString_AS_STRING(strrepr));
  1555. Py_DECREF(strrepr);
  1556. return NULL;
  1557. }
  1558. #ifdef Py_USING_UNICODE
  1559. PyObject *
  1560. PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
  1561. {
  1562. PyObject *result;
  1563. char *buffer = (char *)PyMem_MALLOC(length+1);
  1564. if (buffer == NULL)
  1565. return NULL;
  1566. if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
  1567. PyMem_FREE(buffer);
  1568. return NULL;
  1569. }
  1570. result = PyLong_FromString(buffer, NULL, base);
  1571. PyMem_FREE(buffer);
  1572. return result;
  1573. }
  1574. #endif
  1575. /* forward */
  1576. static PyLongObject *x_divrem
  1577. (PyLongObject *, PyLongObject *, PyLongObject **);
  1578. static PyObject *long_long(PyObject *v);
  1579. static int long_divrem(PyLongObject *, PyLongObject *,
  1580. PyLongObject **, PyLongObject **);
  1581. /* Long division with remainder, top-level routine */
  1582. static int
  1583. long_divrem(PyLongObject *a, PyLongObject *b,
  1584. PyLongObject **pdiv, PyLongObject **prem)
  1585. {
  1586. Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
  1587. PyLongObject *z;
  1588. if (size_b == 0) {
  1589. PyErr_SetString(PyExc_ZeroDivisionError,
  1590. "long division or modulo by zero");
  1591. return -1;
  1592. }
  1593. if (size_a < size_b ||
  1594. (size_a == size_b &&
  1595. a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
  1596. /* |a| < |b|. */
  1597. *pdiv = _PyLong_New(0);
  1598. if (*pdiv == NULL)
  1599. return -1;
  1600. Py_INCREF(a);
  1601. *prem = (PyLongObject *) a;
  1602. return 0;
  1603. }
  1604. if (size_b == 1) {
  1605. digit rem = 0;
  1606. z = divrem1(a, b->ob_digit[0], &rem);
  1607. if (z == NULL)
  1608. return -1;
  1609. *prem = (PyLongObject *) PyLong_FromLong((long)rem);
  1610. if (*prem == NULL) {
  1611. Py_DECREF(z);
  1612. return -1;
  1613. }
  1614. }
  1615. else {
  1616. z = x_divrem(a, b, prem);
  1617. if (z == NULL)
  1618. return -1;
  1619. }
  1620. /* Set the signs.
  1621. The quotient z has the sign of a*b;
  1622. the remainder r has the sign of a,
  1623. so a = b*z + r. */
  1624. if ((a->ob_size < 0) != (b->ob_size < 0))
  1625. z->ob_size = -(z->ob_size);
  1626. if (a->ob_size < 0 && (*prem)->ob_size != 0)
  1627. (*prem)->ob_size = -((*prem)->ob_size);
  1628. *pdiv = z;
  1629. return 0;
  1630. }
  1631. /* Unsigned long division with remainder -- the algorithm */
  1632. static PyLongObject *
  1633. x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
  1634. {
  1635. Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1));
  1636. digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1));
  1637. PyLongObject *v = mul1(v1, d);
  1638. PyLongObject *w = mul1(w1, d);
  1639. PyLongObject *a;
  1640. Py_ssize_t j, k;
  1641. if (v == NULL || w == NULL) {
  1642. Py_XDECREF(v);
  1643. Py_XDECREF(w);
  1644. return NULL;
  1645. }
  1646. assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
  1647. assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */
  1648. assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */
  1649. size_v = ABS(Py_SIZE(v));
  1650. k = size_v - size_w;
  1651. a = _PyLong_New(k + 1);
  1652. for (j = size_v; a != NULL && k >= 0; --j, --k) {
  1653. digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
  1654. twodigits q;
  1655. stwodigits carry = 0;
  1656. Py_ssize_t i;
  1657. SIGCHECK({
  1658. Py_DECREF(a);
  1659. a = NULL;
  1660. break;
  1661. })
  1662. if (vj == w->ob_digit[size_w-1])
  1663. q = PyLong_MASK;
  1664. else
  1665. q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) /
  1666. w->ob_digit[size_w-1];
  1667. while (w->ob_digit[size_w-2]*q >
  1668. ((
  1669. ((twodigits)vj << PyLong_SHIFT)
  1670. + v->ob_digit[j-1]
  1671. - q*w->ob_digit[size_w-1]
  1672. ) << PyLong_SHIFT)
  1673. + v->ob_digit[j-2])
  1674. --q;
  1675. for (i = 0; i < size_w && i+k < size_v; ++i) {
  1676. twodigits z = w->ob_digit[i] * q;
  1677. digit zz = (digit) (z >> PyLong_SHIFT);
  1678. carry += v->ob_digit[i+k] - z
  1679. + ((twodigits)zz << PyLong_SHIFT);
  1680. v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
  1681. carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
  1682. carry, PyLong_SHIFT);
  1683. carry -= zz;
  1684. }
  1685. if (i+k < size_v) {
  1686. carry += v->ob_digit[i+k];
  1687. v->ob_digit[i+k] = 0;
  1688. }
  1689. if (carry == 0)
  1690. a->ob_digit[k] = (digit) q;
  1691. else {
  1692. assert(carry == -1);
  1693. a->ob_digit[k] = (digit) q-1;
  1694. carry = 0;
  1695. for (i = 0; i < size_w && i+k < size_v; ++i) {
  1696. carry += v->ob_digit[i+k] + w->ob_digit[i];
  1697. v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
  1698. carry = Py_ARITHMETIC_RIGHT_SHIFT(
  1699. BASE_TWODIGITS_TYPE,
  1700. carry, PyLong_SHIFT);
  1701. }
  1702. }
  1703. } /* for j, k */
  1704. if (a == NULL)
  1705. *prem = NULL;
  1706. els