From 9dce823cfd0a9991350184f08a1373f3887134f4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 18 Sep 2017 00:06:43 +0300 Subject: [PATCH] py/modbuiltins: Implement abs() by dispatching to MP_UNARY_OP_ABS. This allows user classes to implement __abs__ special method, and saves code size (104 bytes for x86_64), even though during refactor, an issue was fixed and few optimizations were made: * abs() of minimum (negative) small int value is calculated properly. * objint_longlong and objint_mpz avoid allocating new object is the argument is already non-negative. --- py/modbuiltins.c | 21 +------------------- py/objcomplex.c | 5 +++++ py/objfloat.c | 9 +++++++++ py/objint.c | 10 ---------- py/objint.h | 1 - py/objint_longlong.c | 34 ++++++++++---------------------- py/objint_mpz.c | 30 +++++++++------------------- py/runtime.c | 9 +++++++++ py/runtime0.h | 1 + tests/unix/extra_coverage.py.exp | 2 +- 10 files changed, 45 insertions(+), 77 deletions(-) diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 1c76b80739..0486251b66 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -91,26 +91,7 @@ STATIC mp_obj_t mp_builtin___build_class__(size_t n_args, const mp_obj_t *args) MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__); STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) { - #if MICROPY_PY_BUILTINS_FLOAT - if (mp_obj_is_float(o_in)) { - mp_float_t value = mp_obj_float_get(o_in); - // TODO check for NaN etc - if (value < 0) { - return mp_obj_new_float(-value); - } else { - return o_in; - } - #if MICROPY_PY_BUILTINS_COMPLEX - } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) { - mp_float_t real, imag; - mp_obj_complex_get(o_in, &real, &imag); - return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)); - #endif - } - #endif - - // this will raise a TypeError if the argument is not integral - return mp_obj_int_abs(o_in); + return mp_unary_op(MP_UNARY_OP_ABS, o_in); } MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs); diff --git a/py/objcomplex.c b/py/objcomplex.c index 07b9d5d410..e7a3c244a5 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -124,6 +124,11 @@ STATIC mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag)); case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag); + case MP_UNARY_OP_ABS: { + mp_float_t real, imag; + mp_obj_complex_get(o_in, &real, &imag); + return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)); + } default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/objfloat.c b/py/objfloat.c index fadbbcb795..fefd78314c 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -162,6 +162,15 @@ STATIC mp_obj_t float_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(val)); case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val); + case MP_UNARY_OP_ABS: { + mp_float_t value = mp_obj_float_get(o_in); + // TODO check for NaN etc + if (value < 0) { + return mp_obj_new_float(-value); + } else { + return o_in; + } + } default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/objint.c b/py/objint.c index 6a73b43825..000a0404c7 100644 --- a/py/objint.c +++ b/py/objint.c @@ -314,16 +314,6 @@ int mp_obj_int_sign(mp_obj_t self_in) { } } -// This must handle int and bool types, and must raise a -// TypeError if the argument is not integral -mp_obj_t mp_obj_int_abs(mp_obj_t self_in) { - mp_int_t val = mp_obj_get_int(self_in); - if (val < 0) { - val = -val; - } - return MP_OBJ_NEW_SMALL_INT(val); -} - // This is called for operations on SMALL_INT that are not handled by mp_unary_op mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { return MP_OBJ_NULL; // op not supported diff --git a/py/objint.h b/py/objint.h index a4d4ff32d8..4b95acde9f 100644 --- a/py/objint.h +++ b/py/objint.h @@ -57,7 +57,6 @@ mp_int_t mp_obj_int_hash(mp_obj_t self_in); mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf); void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf); int mp_obj_int_sign(mp_obj_t self_in); -mp_obj_t mp_obj_int_abs(mp_obj_t self_in); mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in); mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in); mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in); diff --git a/py/objint_longlong.c b/py/objint_longlong.c index ddfdae744e..8d8ca1b2ce 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -94,30 +94,6 @@ int mp_obj_int_sign(mp_obj_t self_in) { } } -// This must handle int and bool types, and must raise a -// TypeError if the argument is not integral -mp_obj_t mp_obj_int_abs(mp_obj_t self_in) { - if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) { - mp_obj_int_t *self = self_in; - self = mp_obj_new_int_from_ll(self->val); - if (self->val < 0) { - // TODO could overflow long long - self->val = -self->val; - } - return self; - } else { - mp_int_t val = mp_obj_get_int(self_in); - if (val == MP_SMALL_INT_MIN) { - return mp_obj_new_int_from_ll(-val); - } else { - if (val < 0) { - val = -val; - } - return MP_OBJ_NEW_SMALL_INT(val); - } - } -} - mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { mp_obj_int_t *o = o_in; switch (op) { @@ -130,6 +106,16 @@ mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val); case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val); + case MP_UNARY_OP_ABS: { + mp_obj_int_t *self = MP_OBJ_TO_PTR(o_in); + if (self->val >= 0) { + return o_in; + } + self = mp_obj_new_int_from_ll(self->val); + // TODO could overflow long long + self->val = -self->val; + return MP_OBJ_FROM_PTR(self); + } default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 0e318b492b..15aad1d4d2 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -141,27 +141,6 @@ int mp_obj_int_sign(mp_obj_t self_in) { } } -// This must handle int and bool types, and must raise a -// TypeError if the argument is not integral -mp_obj_t mp_obj_int_abs(mp_obj_t self_in) { - if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) { - mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_int_t *self2 = mp_obj_int_new_mpz(); - mpz_abs_inpl(&self2->mpz, &self->mpz); - return MP_OBJ_FROM_PTR(self2); - } else { - mp_int_t val = mp_obj_get_int(self_in); - if (val == MP_SMALL_INT_MIN) { - return mp_obj_new_int_from_ll(-val); - } else { - if (val < 0) { - val = -val; - } - return MP_OBJ_NEW_SMALL_INT(val); - } - } -} - mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { mp_obj_int_t *o = MP_OBJ_TO_PTR(o_in); switch (op) { @@ -170,6 +149,15 @@ mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) { case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return MP_OBJ_FROM_PTR(o2); } case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return MP_OBJ_FROM_PTR(o2); } + case MP_UNARY_OP_ABS: { + mp_obj_int_t *self = MP_OBJ_TO_PTR(o_in); + if (self->mpz.neg == 0) { + return o_in; + } + mp_obj_int_t *self2 = mp_obj_int_new_mpz(); + mpz_abs_inpl(&self2->mpz, &self->mpz); + return MP_OBJ_FROM_PTR(self2); + } default: return MP_OBJ_NULL; // op not supported } } diff --git a/py/runtime.c b/py/runtime.c index c533558dab..f21eed204c 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -231,6 +231,15 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) { } else { return MP_OBJ_NEW_SMALL_INT(-val); } + case MP_UNARY_OP_ABS: + if (val >= 0) { + return arg; + } else if (val == MP_SMALL_INT_MIN) { + // check for overflow + return mp_obj_new_int(-val); + } else { + return MP_OBJ_NEW_SMALL_INT(-val); + } default: assert(op == MP_UNARY_OP_INVERT); return MP_OBJ_NEW_SMALL_INT(~val); diff --git a/py/runtime0.h b/py/runtime0.h index a3e9d46b97..54bf192d33 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -50,6 +50,7 @@ typedef enum { MP_UNARY_OP_NEGATIVE, MP_UNARY_OP_INVERT, MP_UNARY_OP_NOT, + MP_UNARY_OP_ABS, // __abs__ MP_UNARY_OP_SIZEOF, // for sys.getsizeof() } mp_unary_op_t; diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index ab638a632d..4dc421dab5 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -38,7 +38,7 @@ ementation 0 0 # runtime utils -TypeError: can't convert str to int +TypeError: unsupported type for : 'str' TypeError: unsupported types for : 'str', 'str' Warning: test # format float