aboutsummaryrefslogtreecommitdiff
path: root/py/objint_mpz.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-09-18 00:06:43 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-09-18 00:06:43 +0300
commit9dce823cfd0a9991350184f08a1373f3887134f4 (patch)
tree214ca2cd6d0a6c2a93f3f76dd2f5c76857f3a49f /py/objint_mpz.c
parent72491b3e40db75e7edf5831e8914a1ca5c8e9937 (diff)
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.
Diffstat (limited to 'py/objint_mpz.c')
-rw-r--r--py/objint_mpz.c30
1 files changed, 9 insertions, 21 deletions
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 0e318b492..15aad1d4d 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
}
}