summaryrefslogtreecommitdiff
path: root/complex.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-25 14:16:05 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-25 14:16:05 +0000
commit909c4c0fd71603d85312b49dfd6275bac7867b52 (patch)
tree3acaf3a6d02e6930034be62bb5167bf225736b87 /complex.c
parenta96c88eca119b244812cc41eb90531b2138cf7b7 (diff)
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
Diffstat (limited to 'complex.c')
-rw-r--r--complex.c20
1 files changed, 9 insertions, 11 deletions
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);
}