Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mypyc/doc/int_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------

Expand Down
9 changes: 9 additions & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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))) {
Expand Down
11 changes: 11 additions & 0 deletions mypyc/lib-rt/int_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions mypyc/primitives/int_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
15 changes: 6 additions & 9 deletions mypyc/test-data/irbuild-any.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 16 additions & 0 deletions mypyc/test-data/run-integers.test
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading