From 142f9634ef833a6f94501d920ac1057ed7387ce8 Mon Sep 17 00:00:00 2001 From: Pixeless_ Date: Wed, 4 Nov 2026 10:12:51 +0800 Subject: [PATCH] [mypyc] Add primitive for abs(int) --- mypyc/doc/int_operations.rst | 5 +++++ mypyc/lib-rt/CPy.h | 9 +++++++++ mypyc/lib-rt/int_ops.c | 11 +++++++++++ mypyc/primitives/int_ops.py | 9 +++++++++ mypyc/test-data/irbuild-any.test | 15 ++++++--------- mypyc/test-data/run-integers.test | 16 ++++++++++++++++ 6 files changed, 56 insertions(+), 9 deletions(-) diff --git a/mypyc/doc/int_operations.rst b/mypyc/doc/int_operations.rst index ad67ea908cd1f..28a3dea408de4 100644 --- a/mypyc/doc/int_operations.rst +++ b/mypyc/doc/int_operations.rst @@ -143,6 +143,11 @@ and the ``int`` operand is coerced to the native integer type:: b = 1 - x # Similarly, type of "b" is "i64" +Functions +--------- + +* ``abs(n)`` + Attributes ---------- diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 424db8faa454d..ebe81769a768d 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -185,6 +185,7 @@ void CPyTagged_XDecRef(CPyTagged x); bool CPyTagged_IsEq_(CPyTagged left, CPyTagged right); bool CPyTagged_IsLt_(CPyTagged left, CPyTagged right); CPyTagged CPyTagged_Negate_(CPyTagged num); +CPyTagged CPyTagged_Absolute_(CPyTagged num); CPyTagged CPyTagged_Invert_(CPyTagged num); CPyTagged CPyTagged_Add_(CPyTagged left, CPyTagged right); CPyTagged CPyTagged_Subtract_(CPyTagged left, CPyTagged right); @@ -496,6 +497,14 @@ static inline CPyTagged CPyTagged_Negate(CPyTagged num) { return CPyTagged_Negate_(num); } +static inline CPyTagged CPyTagged_Absolute(CPyTagged num) { + if (likely(CPyTagged_CheckShort(num) + && num != (CPyTagged) ((Py_ssize_t)1 << (CPY_INT_BITS - 1)))) { + return (Py_ssize_t)num < 0 ? -num : num; + } + return CPyTagged_Absolute_(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))) { diff --git a/mypyc/lib-rt/int_ops.c b/mypyc/lib-rt/int_ops.c index 11fc777b7f9a0..e8c3849f9ed78 100644 --- a/mypyc/lib-rt/int_ops.c +++ b/mypyc/lib-rt/int_ops.c @@ -126,6 +126,17 @@ CPyTagged CPyTagged_Negate_(CPyTagged num) { return CPyTagged_StealFromObject(result); } +// Tagged int absolute value slow path, where the result may be a long integer +CPyTagged CPyTagged_Absolute_(CPyTagged num) { + PyObject *num_obj = CPyTagged_AsObject(num); + PyObject *result = PyNumber_Absolute(num_obj); + if (result == NULL) { + CPyError_OutOfMemory(); + } + Py_DECREF(num_obj); + return CPyTagged_StealFromObject(result); +} + // 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); diff --git a/mypyc/primitives/int_ops.py b/mypyc/primitives/int_ops.py index 11e86bea4f498..03743c11979c9 100644 --- a/mypyc/primitives/int_ops.py +++ b/mypyc/primitives/int_ops.py @@ -198,6 +198,15 @@ def int_unary_op(name: str, c_function_name: str) -> PrimitiveDescription: int_neg_op = int_unary_op("-", "CPyTagged_Negate") int_invert_op = int_unary_op("~", "CPyTagged_Invert") +# abs(int) +function_op( + name="builtins.abs", + arg_types=[int_rprimitive], + return_type=int_rprimitive, + c_function_name="CPyTagged_Absolute", + error_kind=ERR_NEVER, +) + # Primitives related to integer comparison operations: diff --git a/mypyc/test-data/irbuild-any.test b/mypyc/test-data/irbuild-any.test index 55783a9a94980..20d010c13387d 100644 --- a/mypyc/test-data/irbuild-any.test +++ b/mypyc/test-data/irbuild-any.test @@ -176,16 +176,13 @@ def f() -> None: b = abs(1.1) [out] def f(): - r0, r1 :: object - r2, a :: int - r3, b :: float + r0, a :: int + r1, b :: float L0: - r0 = object 1 - r1 = PyNumber_Absolute(r0) - r2 = unbox(int, r1) - a = r2 - r3 = fabs(1.1) - b = r3 + r0 = CPyTagged_Absolute(2) + a = r0 + r1 = fabs(1.1) + b = r1 return 1 [case testFunctionBasedOps] diff --git a/mypyc/test-data/run-integers.test b/mypyc/test-data/run-integers.test index c2cbe2e3843a4..524bd283d24f7 100644 --- a/mypyc/test-data/run-integers.test +++ b/mypyc/test-data/run-integers.test @@ -163,6 +163,22 @@ assert neg(-9223372036854775807) == 9223372036854775807 assert neg(9223372036854775808) == -9223372036854775808 assert neg(-9223372036854775808) == 9223372036854775808 +[case testAbs] +def int_abs(x: int) -> int: + return abs(x) + +def bool_abs(x: bool) -> int: + return abs(x) +[file driver.py] +from native import int_abs, bool_abs +for n in (0, 1, 5, 1073741823, 1073741824, 2147483648, 4611686018427387903, + 4611686018427387904, 4611686018427387905, 9223372036854775807, + 9223372036854775808, 10**30): + assert int_abs(n) == n + assert int_abs(-n) == n +assert bool_abs(True) == 1 +assert bool_abs(False) == 0 + [case testIsinstanceIntAndNotBool] def test_isinstance_int_and_not_bool(value: object) -> bool: return isinstance(value, int) and not isinstance(value, bool)