|
| 1 | +#include "Python.h" |
| 2 | +#include <stdbool.h> |
| 3 | + |
| 4 | +#if PY_VERSION_HEX >= 0x030C00A5 |
| 5 | +#define ob_digit(o) (((PyLongObject*)o)->long_value.ob_digit) |
| 6 | +#else |
| 7 | +#define ob_digit(o) (((PyLongObject*)o)->ob_digit) |
| 8 | +#endif |
| 9 | + |
| 10 | +#if PY_VERSION_HEX >= 0x030C00A7 |
| 11 | +// taken from cpython:Include/internal/pycore_long.h @ 3.12 |
| 12 | + |
| 13 | +/* Long value tag bits: |
| 14 | + * 0-1: Sign bits value = (1-sign), ie. negative=2, positive=0, zero=1. |
| 15 | + * 2: Reserved for immortality bit |
| 16 | + * 3+ Unsigned digit count |
| 17 | + */ |
| 18 | +#define SIGN_MASK 3 |
| 19 | +#define SIGN_ZERO 1 |
| 20 | +#define SIGN_NEGATIVE 2 |
| 21 | +#define NON_SIZE_BITS 3 |
| 22 | + |
| 23 | +static inline bool |
| 24 | +_PyLong_IsZero(const PyLongObject *op) |
| 25 | +{ |
| 26 | + return (op->long_value.lv_tag & SIGN_MASK) == SIGN_ZERO; |
| 27 | +} |
| 28 | + |
| 29 | +static inline bool |
| 30 | +_PyLong_IsNegative(const PyLongObject *op) |
| 31 | +{ |
| 32 | + return (op->long_value.lv_tag & SIGN_MASK) == SIGN_NEGATIVE; |
| 33 | +} |
| 34 | + |
| 35 | +static inline bool |
| 36 | +_PyLong_IsPositive(const PyLongObject *op) |
| 37 | +{ |
| 38 | + return (op->long_value.lv_tag & SIGN_MASK) == 0; |
| 39 | +} |
| 40 | + |
| 41 | +static inline Py_ssize_t |
| 42 | +_PyLong_DigitCount(const PyLongObject *op) |
| 43 | +{ |
| 44 | + assert(PyLong_Check(op)); |
| 45 | + return op->long_value.lv_tag >> NON_SIZE_BITS; |
| 46 | +} |
| 47 | + |
| 48 | +#define TAG_FROM_SIGN_AND_SIZE(sign, size) ((1 - (sign)) | ((size) << NON_SIZE_BITS)) |
| 49 | + |
| 50 | +static inline void |
| 51 | +_PyLong_SetSignAndDigitCount(PyLongObject *op, int sign, Py_ssize_t size) |
| 52 | +{ |
| 53 | + assert(size >= 0); |
| 54 | + assert(-1 <= sign && sign <= 1); |
| 55 | + assert(sign != 0 || size == 0); |
| 56 | + op->long_value.lv_tag = TAG_FROM_SIGN_AND_SIZE(sign, (size_t)size); |
| 57 | +} |
| 58 | + |
| 59 | +#else |
| 60 | +// fallback for < 3.12 |
| 61 | + |
| 62 | +static inline bool |
| 63 | +_PyLong_IsZero(const PyLongObject *op) |
| 64 | +{ |
| 65 | + return Py_SIZE(op) == 0; |
| 66 | +} |
| 67 | + |
| 68 | +static inline bool |
| 69 | +_PyLong_IsNegative(const PyLongObject *op) |
| 70 | +{ |
| 71 | + return Py_SIZE(op) < 0; |
| 72 | +} |
| 73 | + |
| 74 | +static inline bool |
| 75 | +_PyLong_IsPositive(const PyLongObject *op) |
| 76 | +{ |
| 77 | + return Py_SIZE(op) > 0; |
| 78 | +} |
| 79 | + |
| 80 | +static inline Py_ssize_t |
| 81 | +_PyLong_DigitCount(const PyLongObject *op) |
| 82 | +{ |
| 83 | + Py_ssize_t size = Py_SIZE(op); |
| 84 | + return size < 0 ? -size : size; |
| 85 | +} |
| 86 | + |
| 87 | +static inline void |
| 88 | +_PyLong_SetSignAndDigitCount(PyLongObject *op, int sign, Py_ssize_t size) |
| 89 | +{ |
| 90 | +#if (PY_MAJOR_VERSION == 3) && (PY_MINOR_VERSION < 9) |
| 91 | +// The function Py_SET_SIZE is defined starting with python 3.9. |
| 92 | + Py_SIZE(o) = size; |
| 93 | +#else |
| 94 | + Py_SET_SIZE(op, sign < 0 ? -size : size); |
| 95 | +#endif |
| 96 | +} |
| 97 | + |
| 98 | +#endif |
0 commit comments