summaryrefslogtreecommitdiff
path: root/py
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
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')
-rw-r--r--py/modbuiltins.c21
-rw-r--r--py/objcomplex.c5
-rw-r--r--py/objfloat.c9
-rw-r--r--py/objint.c10
-rw-r--r--py/objint.h1
-rw-r--r--py/objint_longlong.c34
-rw-r--r--py/objint_mpz.c30
-rw-r--r--py/runtime.c9
-rw-r--r--py/runtime0.h1
9 files changed, 44 insertions, 76 deletions
diff --git a/py/modbuiltins.c b/py/modbuiltins.c
index 1c76b8073..0486251b6 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 07b9d5d41..e7a3c244a 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 fadbbcb79..fefd78314 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 6a73b4382..000a0404c 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 a4d4ff32d..4b95acde9 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 ddfdae744..8d8ca1b2c 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 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
}
}
diff --git a/py/runtime.c b/py/runtime.c
index c533558da..f21eed204 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 a3e9d46b9..54bf192d3 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;