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.
crypto-aes
Paul Sokolovsky 5 years ago
parent 72491b3e40
commit 9dce823cfd

@ -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);

@ -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
}
}

@ -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
}
}

@ -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

@ -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);

@ -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
}
}

@ -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
}
}

@ -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);

@ -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;

@ -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

Loading…
Cancel
Save