From 909c4c0fd71603d85312b49dfd6275bac7867b52 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 25 Feb 2015 14:16:05 +0000 Subject: complex.c: specialize * complex.c (rb_nucomp_mul): specialize real numbers and purely imaginary numbers, and get rid of multiplication by zero. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49754 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- complex.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'complex.c') diff --git a/complex.c b/complex.c index f35ed293c8..b9fb706d20 100644 --- a/complex.c +++ b/complex.c @@ -756,20 +756,18 @@ rb_nucomp_mul(VALUE self, VALUE other) { if (k_complex_p(other)) { VALUE real, imag; + VALUE areal, aimag, breal, bimag; get_dat2(self, other); - real = f_sub(f_mul(adat->real, bdat->real), - f_mul(adat->imag, bdat->imag)); - imag = f_add(f_mul(adat->real, bdat->imag), - f_mul(adat->imag, bdat->real)); - - if ((RB_FLOAT_TYPE_P(real) && isnan(RFLOAT_VALUE(real))) || - (RB_FLOAT_TYPE_P(imag) && isnan(RFLOAT_VALUE(imag)))) { - VALUE abs = f_mul(nucomp_abs(self), nucomp_abs(other)); - VALUE arg = f_add(nucomp_arg(self), nucomp_arg(other)); - return f_complex_polar(CLASS_OF(self), abs, arg); - } + if (f_zero_p(areal = adat->real)) areal = ZERO; + if (f_zero_p(aimag = adat->imag)) aimag = ZERO; + if (f_zero_p(breal = bdat->real)) breal = ZERO; + if (f_zero_p(bimag = bdat->imag)) bimag = ZERO; + real = (areal == ZERO || breal == ZERO) ? ZERO : f_mul(areal, breal); + if (aimag != ZERO && bimag != ZERO) real = f_sub(real, f_mul(aimag, bimag)); + imag = (areal == ZERO || bimag == ZERO) ? ZERO : f_mul(areal, bimag); + if (aimag != ZERO && breal != ZERO) imag = f_add(imag, f_mul(aimag, breal)); return f_complex_new2(CLASS_OF(self), real, imag); } -- cgit v1.2.3