longobject.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef Py_CPYTHON_LONGOBJECT_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. PyAPI_FUNC(int) _PyLong_AsInt(PyObject *);
  5. PyAPI_FUNC(int) _PyLong_UnsignedShort_Converter(PyObject *, void *);
  6. PyAPI_FUNC(int) _PyLong_UnsignedInt_Converter(PyObject *, void *);
  7. PyAPI_FUNC(int) _PyLong_UnsignedLong_Converter(PyObject *, void *);
  8. PyAPI_FUNC(int) _PyLong_UnsignedLongLong_Converter(PyObject *, void *);
  9. PyAPI_FUNC(int) _PyLong_Size_t_Converter(PyObject *, void *);
  10. /* _PyLong_Frexp returns a double x and an exponent e such that the
  11. true value is approximately equal to x * 2**e. e is >= 0. x is
  12. 0.0 if and only if the input is 0 (in which case, e and x are both
  13. zeroes); otherwise, 0.5 <= abs(x) < 1.0. On overflow, which is
  14. possible if the number of bits doesn't fit into a Py_ssize_t, sets
  15. OverflowError and returns -1.0 for x, 0 for e. */
  16. PyAPI_FUNC(double) _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e);
  17. PyAPI_FUNC(PyObject *) PyLong_FromUnicodeObject(PyObject *u, int base);
  18. PyAPI_FUNC(PyObject *) _PyLong_FromBytes(const char *, Py_ssize_t, int);
  19. /* _PyLong_Sign. Return 0 if v is 0, -1 if v < 0, +1 if v > 0.
  20. v must not be NULL, and must be a normalized long.
  21. There are no error cases.
  22. */
  23. PyAPI_FUNC(int) _PyLong_Sign(PyObject *v);
  24. /* _PyLong_NumBits. Return the number of bits needed to represent the
  25. absolute value of a long. For example, this returns 1 for 1 and -1, 2
  26. for 2 and -2, and 2 for 3 and -3. It returns 0 for 0.
  27. v must not be NULL, and must be a normalized long.
  28. (size_t)-1 is returned and OverflowError set if the true result doesn't
  29. fit in a size_t.
  30. */
  31. PyAPI_FUNC(size_t) _PyLong_NumBits(PyObject *v);
  32. /* _PyLong_DivmodNear. Given integers a and b, compute the nearest
  33. integer q to the exact quotient a / b, rounding to the nearest even integer
  34. in the case of a tie. Return (q, r), where r = a - q*b. The remainder r
  35. will satisfy abs(r) <= abs(b)/2, with equality possible only if q is
  36. even.
  37. */
  38. PyAPI_FUNC(PyObject *) _PyLong_DivmodNear(PyObject *, PyObject *);
  39. /* _PyLong_FromByteArray: View the n unsigned bytes as a binary integer in
  40. base 256, and return a Python int with the same numeric value.
  41. If n is 0, the integer is 0. Else:
  42. If little_endian is 1/true, bytes[n-1] is the MSB and bytes[0] the LSB;
  43. else (little_endian is 0/false) bytes[0] is the MSB and bytes[n-1] the
  44. LSB.
  45. If is_signed is 0/false, view the bytes as a non-negative integer.
  46. If is_signed is 1/true, view the bytes as a 2's-complement integer,
  47. non-negative if bit 0x80 of the MSB is clear, negative if set.
  48. Error returns:
  49. + Return NULL with the appropriate exception set if there's not
  50. enough memory to create the Python int.
  51. */
  52. PyAPI_FUNC(PyObject *) _PyLong_FromByteArray(
  53. const unsigned char* bytes, size_t n,
  54. int little_endian, int is_signed);
  55. /* _PyLong_AsByteArray: Convert the least-significant 8*n bits of long
  56. v to a base-256 integer, stored in array bytes. Normally return 0,
  57. return -1 on error.
  58. If little_endian is 1/true, store the MSB at bytes[n-1] and the LSB at
  59. bytes[0]; else (little_endian is 0/false) store the MSB at bytes[0] and
  60. the LSB at bytes[n-1].
  61. If is_signed is 0/false, it's an error if v < 0; else (v >= 0) n bytes
  62. are filled and there's nothing special about bit 0x80 of the MSB.
  63. If is_signed is 1/true, bytes is filled with the 2's-complement
  64. representation of v's value. Bit 0x80 of the MSB is the sign bit.
  65. Error returns (-1):
  66. + is_signed is 0 and v < 0. TypeError is set in this case, and bytes
  67. isn't altered.
  68. + n isn't big enough to hold the full mathematical value of v. For
  69. example, if is_signed is 0 and there are more digits in the v than
  70. fit in n; or if is_signed is 1, v < 0, and n is just 1 bit shy of
  71. being large enough to hold a sign bit. OverflowError is set in this
  72. case, but bytes holds the least-significant n bytes of the true value.
  73. */
  74. PyAPI_FUNC(int) _PyLong_AsByteArray(PyLongObject* v,
  75. unsigned char* bytes, size_t n,
  76. int little_endian, int is_signed);
  77. /* _PyLong_Format: Convert the long to a string object with given base,
  78. appending a base prefix of 0[box] if base is 2, 8 or 16. */
  79. PyAPI_FUNC(PyObject *) _PyLong_Format(PyObject *obj, int base);
  80. /* For use by the gcd function in mathmodule.c */
  81. PyAPI_FUNC(PyObject *) _PyLong_GCD(PyObject *, PyObject *);
  82. PyAPI_FUNC(PyObject *) _PyLong_Rshift(PyObject *, size_t);
  83. PyAPI_FUNC(PyObject *) _PyLong_Lshift(PyObject *, size_t);
  84. PyAPI_FUNC(int) PyUnstable_Long_IsCompact(const PyLongObject* op);
  85. PyAPI_FUNC(Py_ssize_t) PyUnstable_Long_CompactValue(const PyLongObject* op);