mirror of
https://github.com/licsber/micropython.git
synced 2024-09-20 09:00:23 +08:00
py/{objfloat,objcomplex}: Optimise MP_UNARY_OP_ABS by reusing variables.
This commit is contained in:
parent
9dce823cfd
commit
fdb2aa81b7
@ -124,11 +124,8 @@ 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_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_POSITIVE: return o_in;
|
||||||
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
|
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
|
||||||
case MP_UNARY_OP_ABS: {
|
case MP_UNARY_OP_ABS:
|
||||||
mp_float_t real, imag;
|
return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(o->real*o->real + o->imag*o->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
|
default: return MP_OBJ_NULL; // op not supported
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,10 +163,9 @@ STATIC mp_obj_t float_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
|
|||||||
case MP_UNARY_OP_POSITIVE: return o_in;
|
case MP_UNARY_OP_POSITIVE: return o_in;
|
||||||
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val);
|
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val);
|
||||||
case MP_UNARY_OP_ABS: {
|
case MP_UNARY_OP_ABS: {
|
||||||
mp_float_t value = mp_obj_float_get(o_in);
|
|
||||||
// TODO check for NaN etc
|
// TODO check for NaN etc
|
||||||
if (value < 0) {
|
if (val < 0) {
|
||||||
return mp_obj_new_float(-value);
|
return mp_obj_new_float(-val);
|
||||||
} else {
|
} else {
|
||||||
return o_in;
|
return o_in;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user