ruby-changes:37353
From: nobu <ko1@a...>
Date: Thu, 29 Jan 2015 10:42:16 +0900 (JST)
Subject: [ruby-changes:37353] nobu:r49434 (trunk): math.c: deoptimize
nobu 2015-01-29 10:42:12 +0900 (Thu, 29 Jan 2015) New Revision: 49434 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49434 Log: math.c: deoptimize * math.c (Get_Double): restrict direct casting only when Fixnum#to_f is not redefined, and convert with rb_to_float(). [Feature #10785] Modified files: trunk/math.c Index: math.c =================================================================== --- math.c (revision 49433) +++ math.c (revision 49434) @@ -21,10 +21,18 @@ https://github.com/ruby/ruby/blob/trunk/math.c#L21 #define RB_BIGNUM_TYPE_P(x) RB_TYPE_P((x), T_BIGNUM) +static ID id_to_f; + VALUE rb_mMath; VALUE rb_eMathDomainError; -#define Get_Double(x) FIXNUM_P(x) ? (double)FIX2LONG(x) : NUM2DBL(x) +#define fix_to_f_optimizable() rb_method_basic_definition_p(rb_cFixnum, id_to_f) +#define Get_Float(x) \ + ((RB_TYPE_P((x), T_FLOAT) ? (void)0 : ((x) = rb_to_float(x))), RFLOAT_VALUE(x)) +#define Get_Double(x) \ + (FIXNUM_P(x) && fix_to_f_optimizable() ? \ + (double)FIX2LONG(x) : \ + Get_Float(x)) #define domain_error(msg) \ rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg) @@ -929,7 +937,7 @@ exp1(sqrt) https://github.com/ruby/ruby/blob/trunk/math.c#L937 void -Init_Math(void) +InitVM_Math(void) { rb_mMath = rb_define_module("Math"); rb_eMathDomainError = rb_define_class_under(rb_mMath, "DomainError", rb_eStandardError); @@ -983,3 +991,10 @@ Init_Math(void) https://github.com/ruby/ruby/blob/trunk/math.c#L991 rb_define_module_function(rb_mMath, "gamma", math_gamma, 1); rb_define_module_function(rb_mMath, "lgamma", math_lgamma, 1); } + +void +Init_Math(void) +{ + id_to_f = rb_intern_const("to_f"); + InitVM(Math); +} -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/