1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

complex.c: optimize Numeric#polar and Numeric#arg

* complex.c (numeric_polar): optimize for Integer, Float, and Rational.

* complex.c (numeric_arg): directly create the value of pi.

* complex.c (f_negative_p): optimize for Integer, Float, and Rational.

* rational.c (INT_NEGATIVE_P): move the definition into internal.h.

* internal.h (INT_NEGATIVE_P): ditto.

* numeric.c (rb_float_abs): rename from flo_abs and export to be used
  from other source files..

* internal.h (rb_float_abs): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56809 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2016-11-16 04:25:35 +00:00
parent ab9b7890c2
commit affa0f845c
4 changed files with 37 additions and 10 deletions

View file

@ -182,8 +182,12 @@ fun2(quo)
inline static int inline static int
f_negative_p(VALUE x) f_negative_p(VALUE x)
{ {
if (FIXNUM_P(x)) if (RB_INTEGER_TYPE_P(x))
return FIXNUM_NEGATIVE_P(x); return INT_NEGATIVE_P(x);
else if (RB_FLOAT_TYPE_P(x))
return RFLOAT_VALUE(x) < 0.0;
else if (RB_TYPE_P(x, T_RATIONAL))
return INT_NEGATIVE_P(RRATIONAL(x)->num);
return rb_num_negative_p(x); return rb_num_negative_p(x);
} }
@ -2045,8 +2049,8 @@ static VALUE
numeric_arg(VALUE self) numeric_arg(VALUE self)
{ {
if (f_positive_p(self)) if (f_positive_p(self))
return INT2FIX(0); return INT2FIX(0);
return rb_const_get(rb_mMath, id_PI); return DBL2NUM(M_PI);
} }
/* /*
@ -2062,6 +2066,8 @@ numeric_rect(VALUE self)
return rb_assoc_new(self, INT2FIX(0)); return rb_assoc_new(self, INT2FIX(0));
} }
static VALUE float_arg(VALUE self);
/* /*
* call-seq: * call-seq:
* num.polar -> array * num.polar -> array
@ -2071,7 +2077,25 @@ numeric_rect(VALUE self)
static VALUE static VALUE
numeric_polar(VALUE self) numeric_polar(VALUE self)
{ {
return rb_assoc_new(f_abs(self), f_arg(self)); VALUE abs, arg;
if (RB_INTEGER_TYPE_P(self)) {
abs = rb_int_abs(self);
arg = numeric_arg(self);
}
else if (RB_FLOAT_TYPE_P(self)) {
abs = rb_float_abs(self);
arg = float_arg(self);
}
else if (RB_TYPE_P(self, T_RATIONAL)) {
abs = rb_rational_abs(self);
arg = numeric_arg(self);
}
else {
abs = f_abs(self);
arg = f_arg(self);
}
return rb_assoc_new(abs, arg);
} }
/* /*

View file

@ -1141,6 +1141,8 @@ void Init_newline(void);
#define FIXNUM_NEGATIVE_P(num) ((SIGNED_VALUE)(num) < 0) #define FIXNUM_NEGATIVE_P(num) ((SIGNED_VALUE)(num) < 0)
#define FIXNUM_ZERO_P(num) ((num) == INT2FIX(0)) #define FIXNUM_ZERO_P(num) ((num) == INT2FIX(0))
#define INT_NEGATIVE_P(x) (FIXNUM_P(x) ? FIXNUM_NEGATIVE_P(x) : BIGNUM_NEGATIVE_P(x))
#ifndef ROUND_DEFAULT #ifndef ROUND_DEFAULT
# define ROUND_DEFAULT RUBY_NUM_ROUND_HALF_EVEN # define ROUND_DEFAULT RUBY_NUM_ROUND_HALF_EVEN
#endif #endif
@ -1181,6 +1183,7 @@ VALUE rb_int_and(VALUE x, VALUE y);
VALUE rb_int_lshift(VALUE x, VALUE y); VALUE rb_int_lshift(VALUE x, VALUE y);
VALUE rb_int_div(VALUE x, VALUE y); VALUE rb_int_div(VALUE x, VALUE y);
VALUE rb_int_abs(VALUE num); VALUE rb_int_abs(VALUE num);
VALUE rb_float_abs(VALUE flt);
#if USE_FLONUM #if USE_FLONUM
#define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n))) #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n)))
@ -1373,6 +1376,7 @@ VALUE rb_rational_plus(VALUE self, VALUE other);
VALUE rb_lcm(VALUE x, VALUE y); VALUE rb_lcm(VALUE x, VALUE y);
VALUE rb_rational_reciprocal(VALUE x); VALUE rb_rational_reciprocal(VALUE x);
VALUE rb_cstr_to_rat(const char *, int); VALUE rb_cstr_to_rat(const char *, int);
VALUE rb_rational_abs(VALUE self);
/* re.c */ /* re.c */
VALUE rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline); VALUE rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline);

View file

@ -1678,8 +1678,8 @@ flo_to_f(VALUE num)
* *
*/ */
static VALUE VALUE
flo_abs(VALUE flt) rb_float_abs(VALUE flt)
{ {
double val = fabs(RFLOAT_VALUE(flt)); double val = fabs(RFLOAT_VALUE(flt));
return DBL2NUM(val); return DBL2NUM(val);
@ -5406,8 +5406,8 @@ Init_Numeric(void)
rb_define_method(rb_cFloat, "eql?", flo_eql, 1); rb_define_method(rb_cFloat, "eql?", flo_eql, 1);
rb_define_method(rb_cFloat, "hash", flo_hash, 0); rb_define_method(rb_cFloat, "hash", flo_hash, 0);
rb_define_method(rb_cFloat, "to_f", flo_to_f, 0); rb_define_method(rb_cFloat, "to_f", flo_to_f, 0);
rb_define_method(rb_cFloat, "abs", flo_abs, 0); rb_define_method(rb_cFloat, "abs", rb_float_abs, 0);
rb_define_method(rb_cFloat, "magnitude", flo_abs, 0); rb_define_method(rb_cFloat, "magnitude", rb_float_abs, 0);
rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0); rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0);
rb_define_method(rb_cFloat, "to_i", flo_to_i, 0); rb_define_method(rb_cFloat, "to_i", flo_to_i, 0);

View file

@ -28,7 +28,6 @@
#define GMP_GCD_DIGITS 1 #define GMP_GCD_DIGITS 1
#define INT_POSITIVE_P(x) (FIXNUM_P(x) ? FIXNUM_POSITIVE_P(x) : BIGNUM_POSITIVE_P(x)) #define INT_POSITIVE_P(x) (FIXNUM_P(x) ? FIXNUM_POSITIVE_P(x) : BIGNUM_POSITIVE_P(x))
#define INT_NEGATIVE_P(x) (FIXNUM_P(x) ? FIXNUM_NEGATIVE_P(x) : BIGNUM_NEGATIVE_P(x))
#define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x)) #define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
VALUE rb_cRational; VALUE rb_cRational;