jpayne@69: #ifndef Py_LONGOBJECT_H jpayne@69: #define Py_LONGOBJECT_H jpayne@69: #ifdef __cplusplus jpayne@69: extern "C" { jpayne@69: #endif jpayne@69: jpayne@69: jpayne@69: /* Long (arbitrary precision) integer object interface */ jpayne@69: jpayne@69: typedef struct _longobject PyLongObject; /* Revealed in longintrepr.h */ jpayne@69: jpayne@69: PyAPI_DATA(PyTypeObject) PyLong_Type; jpayne@69: jpayne@69: #define PyLong_Check(op) \ jpayne@69: PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS) jpayne@69: #define PyLong_CheckExact(op) (Py_TYPE(op) == &PyLong_Type) jpayne@69: jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromLong(long); jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLong(unsigned long); jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromSize_t(size_t); jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromSsize_t(Py_ssize_t); jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromDouble(double); jpayne@69: PyAPI_FUNC(long) PyLong_AsLong(PyObject *); jpayne@69: PyAPI_FUNC(long) PyLong_AsLongAndOverflow(PyObject *, int *); jpayne@69: PyAPI_FUNC(Py_ssize_t) PyLong_AsSsize_t(PyObject *); jpayne@69: PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *); jpayne@69: PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *); jpayne@69: PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *); jpayne@69: #ifndef Py_LIMITED_API jpayne@69: PyAPI_FUNC(int) _PyLong_AsInt(PyObject *); jpayne@69: #endif jpayne@69: PyAPI_FUNC(PyObject *) PyLong_GetInfo(void); jpayne@69: jpayne@69: /* It may be useful in the future. I've added it in the PyInt -> PyLong jpayne@69: cleanup to keep the extra information. [CH] */ jpayne@69: #define PyLong_AS_LONG(op) PyLong_AsLong(op) jpayne@69: jpayne@69: /* Issue #1983: pid_t can be longer than a C long on some systems */ jpayne@69: #if !defined(SIZEOF_PID_T) || SIZEOF_PID_T == SIZEOF_INT jpayne@69: #define _Py_PARSE_PID "i" jpayne@69: #define PyLong_FromPid PyLong_FromLong jpayne@69: #define PyLong_AsPid PyLong_AsLong jpayne@69: #elif SIZEOF_PID_T == SIZEOF_LONG jpayne@69: #define _Py_PARSE_PID "l" jpayne@69: #define PyLong_FromPid PyLong_FromLong jpayne@69: #define PyLong_AsPid PyLong_AsLong jpayne@69: #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG jpayne@69: #define _Py_PARSE_PID "L" jpayne@69: #define PyLong_FromPid PyLong_FromLongLong jpayne@69: #define PyLong_AsPid PyLong_AsLongLong jpayne@69: #else jpayne@69: #error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)" jpayne@69: #endif /* SIZEOF_PID_T */ jpayne@69: jpayne@69: #if SIZEOF_VOID_P == SIZEOF_INT jpayne@69: # define _Py_PARSE_INTPTR "i" jpayne@69: # define _Py_PARSE_UINTPTR "I" jpayne@69: #elif SIZEOF_VOID_P == SIZEOF_LONG jpayne@69: # define _Py_PARSE_INTPTR "l" jpayne@69: # define _Py_PARSE_UINTPTR "k" jpayne@69: #elif defined(SIZEOF_LONG_LONG) && SIZEOF_VOID_P == SIZEOF_LONG_LONG jpayne@69: # define _Py_PARSE_INTPTR "L" jpayne@69: # define _Py_PARSE_UINTPTR "K" jpayne@69: #else jpayne@69: # error "void* different in size from int, long and long long" jpayne@69: #endif /* SIZEOF_VOID_P */ jpayne@69: jpayne@69: #ifndef Py_LIMITED_API jpayne@69: PyAPI_FUNC(int) _PyLong_UnsignedShort_Converter(PyObject *, void *); jpayne@69: PyAPI_FUNC(int) _PyLong_UnsignedInt_Converter(PyObject *, void *); jpayne@69: PyAPI_FUNC(int) _PyLong_UnsignedLong_Converter(PyObject *, void *); jpayne@69: PyAPI_FUNC(int) _PyLong_UnsignedLongLong_Converter(PyObject *, void *); jpayne@69: PyAPI_FUNC(int) _PyLong_Size_t_Converter(PyObject *, void *); jpayne@69: #endif jpayne@69: jpayne@69: /* Used by Python/mystrtoul.c, _PyBytes_FromHex(), jpayne@69: _PyBytes_DecodeEscapeRecode(), etc. */ jpayne@69: #ifndef Py_LIMITED_API jpayne@69: PyAPI_DATA(unsigned char) _PyLong_DigitValue[256]; jpayne@69: #endif jpayne@69: jpayne@69: /* _PyLong_Frexp returns a double x and an exponent e such that the jpayne@69: true value is approximately equal to x * 2**e. e is >= 0. x is jpayne@69: 0.0 if and only if the input is 0 (in which case, e and x are both jpayne@69: zeroes); otherwise, 0.5 <= abs(x) < 1.0. On overflow, which is jpayne@69: possible if the number of bits doesn't fit into a Py_ssize_t, sets jpayne@69: OverflowError and returns -1.0 for x, 0 for e. */ jpayne@69: #ifndef Py_LIMITED_API jpayne@69: PyAPI_FUNC(double) _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e); jpayne@69: #endif jpayne@69: jpayne@69: PyAPI_FUNC(double) PyLong_AsDouble(PyObject *); jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromVoidPtr(void *); jpayne@69: PyAPI_FUNC(void *) PyLong_AsVoidPtr(PyObject *); jpayne@69: jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromLongLong(long long); jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLongLong(unsigned long long); jpayne@69: PyAPI_FUNC(long long) PyLong_AsLongLong(PyObject *); jpayne@69: PyAPI_FUNC(unsigned long long) PyLong_AsUnsignedLongLong(PyObject *); jpayne@69: PyAPI_FUNC(unsigned long long) PyLong_AsUnsignedLongLongMask(PyObject *); jpayne@69: PyAPI_FUNC(long long) PyLong_AsLongLongAndOverflow(PyObject *, int *); jpayne@69: jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromString(const char *, char **, int); jpayne@69: #ifndef Py_LIMITED_API jpayne@69: Py_DEPRECATED(3.3) jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromUnicode(Py_UNICODE*, Py_ssize_t, int); jpayne@69: PyAPI_FUNC(PyObject *) PyLong_FromUnicodeObject(PyObject *u, int base); jpayne@69: PyAPI_FUNC(PyObject *) _PyLong_FromBytes(const char *, Py_ssize_t, int); jpayne@69: #endif jpayne@69: jpayne@69: #ifndef Py_LIMITED_API jpayne@69: /* _PyLong_Sign. Return 0 if v is 0, -1 if v < 0, +1 if v > 0. jpayne@69: v must not be NULL, and must be a normalized long. jpayne@69: There are no error cases. jpayne@69: */ jpayne@69: PyAPI_FUNC(int) _PyLong_Sign(PyObject *v); jpayne@69: jpayne@69: jpayne@69: /* _PyLong_NumBits. Return the number of bits needed to represent the jpayne@69: absolute value of a long. For example, this returns 1 for 1 and -1, 2 jpayne@69: for 2 and -2, and 2 for 3 and -3. It returns 0 for 0. jpayne@69: v must not be NULL, and must be a normalized long. jpayne@69: (size_t)-1 is returned and OverflowError set if the true result doesn't jpayne@69: fit in a size_t. jpayne@69: */ jpayne@69: PyAPI_FUNC(size_t) _PyLong_NumBits(PyObject *v); jpayne@69: jpayne@69: /* _PyLong_DivmodNear. Given integers a and b, compute the nearest jpayne@69: integer q to the exact quotient a / b, rounding to the nearest even integer jpayne@69: in the case of a tie. Return (q, r), where r = a - q*b. The remainder r jpayne@69: will satisfy abs(r) <= abs(b)/2, with equality possible only if q is jpayne@69: even. jpayne@69: */ jpayne@69: PyAPI_FUNC(PyObject *) _PyLong_DivmodNear(PyObject *, PyObject *); jpayne@69: jpayne@69: /* _PyLong_FromByteArray: View the n unsigned bytes as a binary integer in jpayne@69: base 256, and return a Python int with the same numeric value. jpayne@69: If n is 0, the integer is 0. Else: jpayne@69: If little_endian is 1/true, bytes[n-1] is the MSB and bytes[0] the LSB; jpayne@69: else (little_endian is 0/false) bytes[0] is the MSB and bytes[n-1] the jpayne@69: LSB. jpayne@69: If is_signed is 0/false, view the bytes as a non-negative integer. jpayne@69: If is_signed is 1/true, view the bytes as a 2's-complement integer, jpayne@69: non-negative if bit 0x80 of the MSB is clear, negative if set. jpayne@69: Error returns: jpayne@69: + Return NULL with the appropriate exception set if there's not jpayne@69: enough memory to create the Python int. jpayne@69: */ jpayne@69: PyAPI_FUNC(PyObject *) _PyLong_FromByteArray( jpayne@69: const unsigned char* bytes, size_t n, jpayne@69: int little_endian, int is_signed); jpayne@69: jpayne@69: /* _PyLong_AsByteArray: Convert the least-significant 8*n bits of long jpayne@69: v to a base-256 integer, stored in array bytes. Normally return 0, jpayne@69: return -1 on error. jpayne@69: If little_endian is 1/true, store the MSB at bytes[n-1] and the LSB at jpayne@69: bytes[0]; else (little_endian is 0/false) store the MSB at bytes[0] and jpayne@69: the LSB at bytes[n-1]. jpayne@69: If is_signed is 0/false, it's an error if v < 0; else (v >= 0) n bytes jpayne@69: are filled and there's nothing special about bit 0x80 of the MSB. jpayne@69: If is_signed is 1/true, bytes is filled with the 2's-complement jpayne@69: representation of v's value. Bit 0x80 of the MSB is the sign bit. jpayne@69: Error returns (-1): jpayne@69: + is_signed is 0 and v < 0. TypeError is set in this case, and bytes jpayne@69: isn't altered. jpayne@69: + n isn't big enough to hold the full mathematical value of v. For jpayne@69: example, if is_signed is 0 and there are more digits in the v than jpayne@69: fit in n; or if is_signed is 1, v < 0, and n is just 1 bit shy of jpayne@69: being large enough to hold a sign bit. OverflowError is set in this jpayne@69: case, but bytes holds the least-significant n bytes of the true value. jpayne@69: */ jpayne@69: PyAPI_FUNC(int) _PyLong_AsByteArray(PyLongObject* v, jpayne@69: unsigned char* bytes, size_t n, jpayne@69: int little_endian, int is_signed); jpayne@69: jpayne@69: /* _PyLong_FromNbInt: Convert the given object to a PyLongObject jpayne@69: using the nb_int slot, if available. Raise TypeError if either the jpayne@69: nb_int slot is not available or the result of the call to nb_int jpayne@69: returns something not of type int. jpayne@69: */ jpayne@69: PyAPI_FUNC(PyObject *) _PyLong_FromNbInt(PyObject *); jpayne@69: jpayne@69: /* Convert the given object to a PyLongObject using the nb_index or jpayne@69: nb_int slots, if available (the latter is deprecated). jpayne@69: Raise TypeError if either nb_index and nb_int slots are not jpayne@69: available or the result of the call to nb_index or nb_int jpayne@69: returns something not of type int. jpayne@69: Should be replaced with PyNumber_Index after the end of the jpayne@69: deprecation period. jpayne@69: */ jpayne@69: PyAPI_FUNC(PyObject *) _PyLong_FromNbIndexOrNbInt(PyObject *); jpayne@69: jpayne@69: /* _PyLong_Format: Convert the long to a string object with given base, jpayne@69: appending a base prefix of 0[box] if base is 2, 8 or 16. */ jpayne@69: PyAPI_FUNC(PyObject *) _PyLong_Format(PyObject *obj, int base); jpayne@69: jpayne@69: PyAPI_FUNC(int) _PyLong_FormatWriter( jpayne@69: _PyUnicodeWriter *writer, jpayne@69: PyObject *obj, jpayne@69: int base, jpayne@69: int alternate); jpayne@69: jpayne@69: PyAPI_FUNC(char*) _PyLong_FormatBytesWriter( jpayne@69: _PyBytesWriter *writer, jpayne@69: char *str, jpayne@69: PyObject *obj, jpayne@69: int base, jpayne@69: int alternate); jpayne@69: jpayne@69: /* Format the object based on the format_spec, as defined in PEP 3101 jpayne@69: (Advanced String Formatting). */ jpayne@69: PyAPI_FUNC(int) _PyLong_FormatAdvancedWriter( jpayne@69: _PyUnicodeWriter *writer, jpayne@69: PyObject *obj, jpayne@69: PyObject *format_spec, jpayne@69: Py_ssize_t start, jpayne@69: Py_ssize_t end); jpayne@69: #endif /* Py_LIMITED_API */ jpayne@69: jpayne@69: /* These aren't really part of the int object, but they're handy. The jpayne@69: functions are in Python/mystrtoul.c. jpayne@69: */ jpayne@69: PyAPI_FUNC(unsigned long) PyOS_strtoul(const char *, char **, int); jpayne@69: PyAPI_FUNC(long) PyOS_strtol(const char *, char **, int); jpayne@69: jpayne@69: #ifndef Py_LIMITED_API jpayne@69: /* For use by the gcd function in mathmodule.c */ jpayne@69: PyAPI_FUNC(PyObject *) _PyLong_GCD(PyObject *, PyObject *); jpayne@69: #endif /* !Py_LIMITED_API */ jpayne@69: jpayne@69: #ifndef Py_LIMITED_API jpayne@69: PyAPI_DATA(PyObject *) _PyLong_Zero; jpayne@69: PyAPI_DATA(PyObject *) _PyLong_One; jpayne@69: jpayne@69: PyAPI_FUNC(PyObject *) _PyLong_Rshift(PyObject *, size_t); jpayne@69: PyAPI_FUNC(PyObject *) _PyLong_Lshift(PyObject *, size_t); jpayne@69: #endif jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: } jpayne@69: #endif jpayne@69: #endif /* !Py_LONGOBJECT_H */