Skip to content

Commit fdb2aa8

Browse files
committed
py/{objfloat,objcomplex}: Optimise MP_UNARY_OP_ABS by reusing variables.
1 parent 9dce823 commit fdb2aa8

2 files changed

Lines changed: 4 additions & 8 deletions

File tree

py/objcomplex.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,8 @@ STATIC mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
124124
case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag));
125125
case MP_UNARY_OP_POSITIVE: return o_in;
126126
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
127-
case MP_UNARY_OP_ABS: {
128-
mp_float_t real, imag;
129-
mp_obj_complex_get(o_in, &real, &imag);
130-
return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
131-
}
127+
case MP_UNARY_OP_ABS:
128+
return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(o->real*o->real + o->imag*o->imag));
132129
default: return MP_OBJ_NULL; // op not supported
133130
}
134131
}

py/objfloat.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,9 @@ STATIC mp_obj_t float_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
163163
case MP_UNARY_OP_POSITIVE: return o_in;
164164
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val);
165165
case MP_UNARY_OP_ABS: {
166-
mp_float_t value = mp_obj_float_get(o_in);
167166
// TODO check for NaN etc
168-
if (value < 0) {
169-
return mp_obj_new_float(-value);
167+
if (val < 0) {
168+
return mp_obj_new_float(-val);
170169
} else {
171170
return o_in;
172171
}

0 commit comments

Comments
 (0)