diff --git a/mypyc/lib-rt/int_ops.c b/mypyc/lib-rt/int_ops.c index b7fff2535c125..e2c302eea5760 100644 --- a/mypyc/lib-rt/int_ops.c +++ b/mypyc/lib-rt/int_ops.c @@ -232,13 +232,6 @@ PyObject *CPyBool_Str(bool b) { return PyObject_Str(b ? Py_True : Py_False); } -static void CPyLong_NormalizeUnsigned(PyLongObject *v) { - Py_ssize_t i = CPY_LONG_SIZE_UNSIGNED(v); - while (i > 0 && CPY_LONG_DIGIT(v, i - 1) == 0) - i--; - CPyLong_SetUnsignedSize(v, i); -} - // Bitwise op '&', '|' or '^' using the generic (slow) API static CPyTagged GenericBitwiseOp(CPyTagged a, CPyTagged b, char op) { PyObject *aobj = CPyTagged_AsObject(a); @@ -302,7 +295,6 @@ CPyTagged CPyTagged_BitwiseLongOp_(CPyTagged a, CPyTagged b, char op) { digit *adigits = GetIntDigits(a, &asize, abuf); digit *bdigits = GetIntDigits(b, &bsize, bbuf); - PyLongObject *r; if (unlikely(asize < 0 || bsize < 0)) { // Negative operand. This is slower, but bitwise ops on them are pretty rare. return GenericBitwiseOp(a, b, op); @@ -317,31 +309,31 @@ CPyTagged CPyTagged_BitwiseLongOp_(CPyTagged a, CPyTagged b, char op) { asize = bsize; bsize = tmp_size; } - r = _PyLong_New(op == '&' ? asize : bsize); - if (unlikely(r == NULL)) { + void *digits = NULL; + PyLongWriter *writer = PyLongWriter_Create(0, op == '&' ? asize : bsize, &digits); + if (unlikely(writer == NULL)) { CPyError_OutOfMemory(); } Py_ssize_t i; if (op == '&') { for (i = 0; i < asize; i++) { - CPY_LONG_DIGIT(r, i) = adigits[i] & bdigits[i]; + ((digit *)digits)[i] = adigits[i] & bdigits[i]; } } else { if (op == '|') { for (i = 0; i < asize; i++) { - CPY_LONG_DIGIT(r, i) = adigits[i] | bdigits[i]; + ((digit *)digits)[i] = adigits[i] | bdigits[i]; } } else { for (i = 0; i < asize; i++) { - CPY_LONG_DIGIT(r, i) = adigits[i] ^ bdigits[i]; + ((digit *)digits)[i] = adigits[i] ^ bdigits[i]; } } for (; i < bsize; i++) { - CPY_LONG_DIGIT(r, i) = bdigits[i]; + ((digit *)digits)[i] = bdigits[i]; } } - CPyLong_NormalizeUnsigned(r); - return CPyTagged_StealFromObject((PyObject *)r); + return CPyTagged_StealFromObject(PyLongWriter_Finish(writer)); } // Bitwise '~' slow path diff --git a/mypyc/lib-rt/mypyc_util.h b/mypyc/lib-rt/mypyc_util.h index 66d5d106056b7..80019d23bb069 100644 --- a/mypyc/lib-rt/mypyc_util.h +++ b/mypyc/lib-rt/mypyc_util.h @@ -124,13 +124,6 @@ static inline CPyTagged CPyTagged_ShortFromSsize_t(Py_ssize_t x) { // Number of digits, assuming int is non-negative #define CPY_LONG_SIZE_UNSIGNED(o) CPY_LONG_SIZE(o) -static inline void CPyLong_SetUnsignedSize(PyLongObject *o, Py_ssize_t n) { - if (n == 0) - o->long_value.lv_tag = CPY_SIGN_ZERO; - else - o->long_value.lv_tag = n << CPY_NON_SIZE_BITS; -} - #else #define CPY_LONG_DIGIT(o, n) ((o)->ob_digit[n]) @@ -138,10 +131,6 @@ static inline void CPyLong_SetUnsignedSize(PyLongObject *o, Py_ssize_t n) { #define CPY_LONG_SIZE_SIGNED(o) ((o)->ob_base.ob_size) #define CPY_LONG_SIZE_UNSIGNED(o) ((o)->ob_base.ob_size) -static inline void CPyLong_SetUnsignedSize(PyLongObject *o, Py_ssize_t n) { - o->ob_base.ob_size = n; -} - #endif // Are we targeting Python 3.13 or newer?