diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 1a03f049ecb0..9e85647226fe 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -129,20 +129,20 @@ Py_ssize_t CPyTagged_AsSsize_t(CPyTagged x); void CPyTagged_IncRef(CPyTagged x); void CPyTagged_DecRef(CPyTagged x); void CPyTagged_XDecRef(CPyTagged x); -CPyTagged CPyTagged_Negate(CPyTagged num); -CPyTagged CPyTagged_Invert(CPyTagged num); -CPyTagged CPyTagged_Add(CPyTagged left, CPyTagged right); -CPyTagged CPyTagged_Subtract(CPyTagged left, CPyTagged right); -CPyTagged CPyTagged_Multiply(CPyTagged left, CPyTagged right); -CPyTagged CPyTagged_FloorDivide(CPyTagged left, CPyTagged right); -CPyTagged CPyTagged_Remainder(CPyTagged left, CPyTagged right); -CPyTagged CPyTagged_And(CPyTagged left, CPyTagged right); -CPyTagged CPyTagged_Or(CPyTagged left, CPyTagged right); -CPyTagged CPyTagged_Xor(CPyTagged left, CPyTagged right); -CPyTagged CPyTagged_Rshift(CPyTagged left, CPyTagged right); -CPyTagged CPyTagged_Lshift(CPyTagged left, CPyTagged right); + bool CPyTagged_IsEq_(CPyTagged left, CPyTagged right); bool CPyTagged_IsLt_(CPyTagged left, CPyTagged right); +CPyTagged CPyTagged_Negate_(CPyTagged num); +CPyTagged CPyTagged_Invert_(CPyTagged num); +CPyTagged CPyTagged_Add_(CPyTagged left, CPyTagged right); +CPyTagged CPyTagged_Subtract_(CPyTagged left, CPyTagged right); +CPyTagged CPyTagged_Multiply_(CPyTagged left, CPyTagged right); +CPyTagged CPyTagged_FloorDivide_(CPyTagged left, CPyTagged right); +CPyTagged CPyTagged_Remainder_(CPyTagged left, CPyTagged right); +CPyTagged CPyTagged_BitwiseLongOp_(CPyTagged a, CPyTagged b, char op); +CPyTagged CPyTagged_Rshift_(CPyTagged left, CPyTagged right); +CPyTagged CPyTagged_Lshift_(CPyTagged left, CPyTagged right); + PyObject *CPyTagged_Str(CPyTagged n); CPyTagged CPyTagged_FromFloat(double f); PyObject *CPyLong_FromStrWithBase(PyObject *o, CPyTagged base); @@ -286,6 +286,144 @@ static inline bool CPyTagged_IsLe(CPyTagged left, CPyTagged right) { } } +static inline CPyTagged CPyTagged_Negate(CPyTagged num) { + if (likely(CPyTagged_CheckShort(num) + && num != (CPyTagged) ((Py_ssize_t)1 << (CPY_INT_BITS - 1)))) { + // The only possibility of an overflow error happening when negating a short is if we + // attempt to negate the most negative number. + return -num; + } + return CPyTagged_Negate_(num); +} + +static inline CPyTagged CPyTagged_Add(CPyTagged left, CPyTagged right) { + // TODO: Use clang/gcc extension __builtin_saddll_overflow instead. + if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { + CPyTagged sum = left + right; + if (likely(!CPyTagged_IsAddOverflow(sum, left, right))) { + return sum; + } + } + return CPyTagged_Add_(left, right); +} + +static inline CPyTagged CPyTagged_Subtract(CPyTagged left, CPyTagged right) { + // TODO: Use clang/gcc extension __builtin_saddll_overflow instead. + if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { + CPyTagged diff = left - right; + if (likely(!CPyTagged_IsSubtractOverflow(diff, left, right))) { + return diff; + } + } + return CPyTagged_Subtract_(left, right); +} + +static inline CPyTagged CPyTagged_Multiply(CPyTagged left, CPyTagged right) { + // TODO: Consider using some clang/gcc extension to check for overflow + if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) { + if (!CPyTagged_IsMultiplyOverflow(left, right)) { + return left * CPyTagged_ShortAsSsize_t(right); + } + } + return CPyTagged_Multiply_(left, right); +} + +static inline CPyTagged CPyTagged_FloorDivide(CPyTagged left, CPyTagged right) { + if (CPyTagged_CheckShort(left) + && CPyTagged_CheckShort(right) + && !CPyTagged_MaybeFloorDivideFault(left, right)) { + Py_ssize_t result = CPyTagged_ShortAsSsize_t(left) / CPyTagged_ShortAsSsize_t(right); + if (((Py_ssize_t)left < 0) != (((Py_ssize_t)right) < 0)) { + if (result * right != left) { + // Round down + result--; + } + } + return result << 1; + } + return CPyTagged_FloorDivide_(left, right); +} + +static inline CPyTagged CPyTagged_Remainder(CPyTagged left, CPyTagged right) { + if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right) + && !CPyTagged_MaybeRemainderFault(left, right)) { + Py_ssize_t result = (Py_ssize_t)left % (Py_ssize_t)right; + if (((Py_ssize_t)right < 0) != ((Py_ssize_t)left < 0) && result != 0) { + result += right; + } + return result; + } + return CPyTagged_Remainder_(left, right); +} + +// Bitwise '~' +static inline CPyTagged CPyTagged_Invert(CPyTagged num) { + if (likely(CPyTagged_CheckShort(num) && num != CPY_TAGGED_ABS_MIN)) { + return ~num & ~CPY_INT_TAG; + } + return CPyTagged_Invert_(num); +} + +// Bitwise '&' +static inline CPyTagged CPyTagged_And(CPyTagged left, CPyTagged right) { + if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { + return left & right; + } + return CPyTagged_BitwiseLongOp_(left, right, '&'); +} + +// Bitwise '|' +static inline CPyTagged CPyTagged_Or(CPyTagged left, CPyTagged right) { + if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { + return left | right; + } + return CPyTagged_BitwiseLongOp_(left, right, '|'); +} + +// Bitwise '^' +static inline CPyTagged CPyTagged_Xor(CPyTagged left, CPyTagged right) { + if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { + return left ^ right; + } + return CPyTagged_BitwiseLongOp_(left, right, '^'); +} + +// Bitwise '>>' +static inline CPyTagged CPyTagged_Rshift(CPyTagged left, CPyTagged right) { + if (likely(CPyTagged_CheckShort(left) + && CPyTagged_CheckShort(right) + && (Py_ssize_t)right >= 0)) { + CPyTagged count = CPyTagged_ShortAsSsize_t(right); + if (unlikely(count >= CPY_INT_BITS)) { + if ((Py_ssize_t)left >= 0) { + return 0; + } else { + return CPyTagged_ShortFromInt(-1); + } + } + return ((Py_ssize_t)left >> count) & ~CPY_INT_TAG; + } + return CPyTagged_Rshift_(left, right); +} + +static inline bool IsShortLshiftOverflow(Py_ssize_t short_int, Py_ssize_t shift) { + return ((Py_ssize_t)(short_int << shift) >> shift) != short_int; +} + +// Bitwise '<<' +static inline CPyTagged CPyTagged_Lshift(CPyTagged left, CPyTagged right) { + if (likely(CPyTagged_CheckShort(left) + && CPyTagged_CheckShort(right) + && (Py_ssize_t)right >= 0 + && right < CPY_INT_BITS * 2)) { + CPyTagged shift = CPyTagged_ShortAsSsize_t(right); + if (!IsShortLshiftOverflow(left, shift)) + // Short integers, no overflow + return left << shift; + } + return CPyTagged_Lshift_(left, right); +} + // Float operations diff --git a/mypyc/lib-rt/int_ops.c b/mypyc/lib-rt/int_ops.c index b57d88c6ac93..b1b3d6e125f3 100644 --- a/mypyc/lib-rt/int_ops.c +++ b/mypyc/lib-rt/int_ops.c @@ -135,13 +135,8 @@ void CPyTagged_XDecRef(CPyTagged x) { } } -CPyTagged CPyTagged_Negate(CPyTagged num) { - if (CPyTagged_CheckShort(num) - && num != (CPyTagged) ((Py_ssize_t)1 << (CPY_INT_BITS - 1))) { - // The only possibility of an overflow error happening when negating a short is if we - // attempt to negate the most negative number. - return -num; - } +// Tagged int negation slow path, where the result may be a long integer +CPyTagged CPyTagged_Negate_(CPyTagged num) { PyObject *num_obj = CPyTagged_AsObject(num); PyObject *result = PyNumber_Negative(num_obj); if (result == NULL) { @@ -151,14 +146,8 @@ CPyTagged CPyTagged_Negate(CPyTagged num) { return CPyTagged_StealFromObject(result); } -CPyTagged CPyTagged_Add(CPyTagged left, CPyTagged right) { - // TODO: Use clang/gcc extension __builtin_saddll_overflow instead. - if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { - CPyTagged sum = left + right; - if (likely(!CPyTagged_IsAddOverflow(sum, left, right))) { - return sum; - } - } +// Tagged int addition slow path, where the result may be a long integer +CPyTagged CPyTagged_Add_(CPyTagged left, CPyTagged right) { PyObject *left_obj = CPyTagged_AsObject(left); PyObject *right_obj = CPyTagged_AsObject(right); PyObject *result = PyNumber_Add(left_obj, right_obj); @@ -170,14 +159,8 @@ CPyTagged CPyTagged_Add(CPyTagged left, CPyTagged right) { return CPyTagged_StealFromObject(result); } -CPyTagged CPyTagged_Subtract(CPyTagged left, CPyTagged right) { - // TODO: Use clang/gcc extension __builtin_saddll_overflow instead. - if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { - CPyTagged diff = left - right; - if (likely(!CPyTagged_IsSubtractOverflow(diff, left, right))) { - return diff; - } - } +// Tagged int subraction slow path, where the result may be a long integer +CPyTagged CPyTagged_Subtract_(CPyTagged left, CPyTagged right) { PyObject *left_obj = CPyTagged_AsObject(left); PyObject *right_obj = CPyTagged_AsObject(right); PyObject *result = PyNumber_Subtract(left_obj, right_obj); @@ -189,13 +172,8 @@ CPyTagged CPyTagged_Subtract(CPyTagged left, CPyTagged right) { return CPyTagged_StealFromObject(result); } -CPyTagged CPyTagged_Multiply(CPyTagged left, CPyTagged right) { - // TODO: Consider using some clang/gcc extension - if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) { - if (!CPyTagged_IsMultiplyOverflow(left, right)) { - return left * CPyTagged_ShortAsSsize_t(right); - } - } +// Tagged int multiplication slow path, where the result may be a long integer +CPyTagged CPyTagged_Multiply_(CPyTagged left, CPyTagged right) { PyObject *left_obj = CPyTagged_AsObject(left); PyObject *right_obj = CPyTagged_AsObject(right); PyObject *result = PyNumber_Multiply(left_obj, right_obj); @@ -207,19 +185,8 @@ CPyTagged CPyTagged_Multiply(CPyTagged left, CPyTagged right) { return CPyTagged_StealFromObject(result); } -CPyTagged CPyTagged_FloorDivide(CPyTagged left, CPyTagged right) { - if (CPyTagged_CheckShort(left) - && CPyTagged_CheckShort(right) - && !CPyTagged_MaybeFloorDivideFault(left, right)) { - Py_ssize_t result = CPyTagged_ShortAsSsize_t(left) / CPyTagged_ShortAsSsize_t(right); - if (((Py_ssize_t)left < 0) != (((Py_ssize_t)right) < 0)) { - if (result * right != left) { - // Round down - result--; - } - } - return result << 1; - } +// Tagged int // slow path, where the result may be a long integer (or raise) +CPyTagged CPyTagged_FloorDivide_(CPyTagged left, CPyTagged right) { PyObject *left_obj = CPyTagged_AsObject(left); PyObject *right_obj = CPyTagged_AsObject(right); PyObject *result = PyNumber_FloorDivide(left_obj, right_obj); @@ -233,15 +200,8 @@ CPyTagged CPyTagged_FloorDivide(CPyTagged left, CPyTagged right) { } } -CPyTagged CPyTagged_Remainder(CPyTagged left, CPyTagged right) { - if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right) - && !CPyTagged_MaybeRemainderFault(left, right)) { - Py_ssize_t result = (Py_ssize_t)left % (Py_ssize_t)right; - if (((Py_ssize_t)right < 0) != ((Py_ssize_t)left < 0) && result != 0) { - result += right; - } - return result; - } +// Tagged int % slow path, where the result may be a long integer (or raise) +CPyTagged CPyTagged_Remainder_(CPyTagged left, CPyTagged right) { PyObject *left_obj = CPyTagged_AsObject(left); PyObject *right_obj = CPyTagged_AsObject(right); PyObject *result = PyNumber_Remainder(left_obj, right_obj); @@ -368,7 +328,7 @@ static digit *GetIntDigits(CPyTagged n, Py_ssize_t *size, digit *buf) { // Shared implementation of bitwise '&', '|' and '^' (specified by op) for at least // one long operand. This is somewhat optimized for performance. -static CPyTagged BitwiseLongOp(CPyTagged a, CPyTagged b, char op) { +CPyTagged CPyTagged_BitwiseLongOp_(CPyTagged a, CPyTagged b, char op) { // Directly access the digits, as there is no fast C API function for this. digit abuf[3]; digit bbuf[3]; @@ -419,89 +379,34 @@ static CPyTagged BitwiseLongOp(CPyTagged a, CPyTagged b, char op) { return CPyTagged_StealFromObject((PyObject *)r); } -// Bitwise '&' -CPyTagged CPyTagged_And(CPyTagged left, CPyTagged right) { - if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { - return left & right; - } - return BitwiseLongOp(left, right, '&'); -} - -// Bitwise '|' -CPyTagged CPyTagged_Or(CPyTagged left, CPyTagged right) { - if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { - return left | right; - } - return BitwiseLongOp(left, right, '|'); -} - -// Bitwise '^' -CPyTagged CPyTagged_Xor(CPyTagged left, CPyTagged right) { - if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { - return left ^ right; - } - return BitwiseLongOp(left, right, '^'); -} - -// Bitwise '~' -CPyTagged CPyTagged_Invert(CPyTagged num) { - if (likely(CPyTagged_CheckShort(num) && num != CPY_TAGGED_ABS_MIN)) { - return ~num & ~CPY_INT_TAG; - } else { - PyObject *obj = CPyTagged_AsObject(num); - PyObject *result = PyNumber_Invert(obj); - if (unlikely(result == NULL)) { - CPyError_OutOfMemory(); - } - Py_DECREF(obj); - return CPyTagged_StealFromObject(result); +// Bitwise '~' slow path +CPyTagged CPyTagged_Invert_(CPyTagged num) { + PyObject *obj = CPyTagged_AsObject(num); + PyObject *result = PyNumber_Invert(obj); + if (unlikely(result == NULL)) { + CPyError_OutOfMemory(); } + Py_DECREF(obj); + return CPyTagged_StealFromObject(result); } -// Bitwise '>>' -CPyTagged CPyTagged_Rshift(CPyTagged left, CPyTagged right) { - if (likely(CPyTagged_CheckShort(left) - && CPyTagged_CheckShort(right) - && (Py_ssize_t)right >= 0)) { - CPyTagged count = CPyTagged_ShortAsSsize_t(right); - if (unlikely(count >= CPY_INT_BITS)) { - if ((Py_ssize_t)left >= 0) { - return 0; - } else { - return CPyTagged_ShortFromInt(-1); - } - } - return ((Py_ssize_t)left >> count) & ~CPY_INT_TAG; - } else { - // Long integer or negative shift -- use generic op - PyObject *lobj = CPyTagged_AsObject(left); - PyObject *robj = CPyTagged_AsObject(right); - PyObject *result = PyNumber_Rshift(lobj, robj); - Py_DECREF(lobj); - Py_DECREF(robj); - if (result == NULL) { - // Propagate error (could be negative shift count) - return CPY_INT_TAG; - } - return CPyTagged_StealFromObject(result); +// Bitwise '>>' slow path +CPyTagged CPyTagged_Rshift_(CPyTagged left, CPyTagged right) { + // Long integer or negative shift -- use generic op + PyObject *lobj = CPyTagged_AsObject(left); + PyObject *robj = CPyTagged_AsObject(right); + PyObject *result = PyNumber_Rshift(lobj, robj); + Py_DECREF(lobj); + Py_DECREF(robj); + if (result == NULL) { + // Propagate error (could be negative shift count) + return CPY_INT_TAG; } + return CPyTagged_StealFromObject(result); } -static inline bool IsShortLshiftOverflow(Py_ssize_t short_int, Py_ssize_t shift) { - return ((Py_ssize_t)(short_int << shift) >> shift) != short_int; -} - -// Bitwise '<<' -CPyTagged CPyTagged_Lshift(CPyTagged left, CPyTagged right) { - if (likely(CPyTagged_CheckShort(left) - && CPyTagged_CheckShort(right) - && (Py_ssize_t)right >= 0 - && right < CPY_INT_BITS * 2)) { - CPyTagged shift = CPyTagged_ShortAsSsize_t(right); - if (!IsShortLshiftOverflow(left, shift)) - // Short integers, no overflow - return left << shift; - } +// Bitwise '<<' slow path +CPyTagged CPyTagged_Lshift_(CPyTagged left, CPyTagged right) { // Long integer or out of range shift -- use generic op PyObject *lobj = CPyTagged_AsObject(left); PyObject *robj = CPyTagged_AsObject(right);