ruby-changes:31737
From: mrkn <ko1@a...>
Date: Sat, 23 Nov 2013 19:49:41 +0900 (JST)
Subject: [ruby-changes:31737] mrkn:r43816 (trunk): exp
mrkn 2013-11-23 19:49:36 +0900 (Sat, 23 Nov 2013) New Revision: 43816 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43816 Log: exp Modified files: trunk/ChangeLog trunk/ext/bigdecimal/bigdecimal.c Index: ChangeLog =================================================================== --- ChangeLog (revision 43815) +++ ChangeLog (revision 43816) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Nov 23 19:46:00 2013 Kenta Murata <mrkn@m...> + + * ext/bigdecimal/bigdecimal.c (BigMath_s_exp): Optimize the + calculation algorithm to reduce the number of divisions. + This optimization was proposed by Rafa Michalski. + [Feature #6857] [ruby-core:47130] + Sat Nov 23 19:20:00 2013 Kenta Murata <mrkn@m...> * ext/bigdecimal/bigdecimal.c (BigDecimal_div2): The signature was Index: ext/bigdecimal/bigdecimal.c =================================================================== --- ext/bigdecimal/bigdecimal.c (revision 43815) +++ ext/bigdecimal/bigdecimal.c (revision 43816) @@ -2675,7 +2675,7 @@ BigMath_s_exp(VALUE klass, VALUE x, VALU https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2675 { ssize_t prec, n, i; Real* vx = NULL; - VALUE one, d, x1, y, z; + VALUE one, d, y; int negative = 0; int infinite = 0; int nan = 0; @@ -2751,11 +2751,9 @@ BigMath_s_exp(VALUE klass, VALUE x, VALU https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2751 } one = ToValue(VpCreateRbObject(1, "1")); - x1 = one; y = one; d = y; - z = one; - i = 0; + i = 1; while (!VpIsZero((Real*)DATA_PTR(d))) { SIGNED_VALUE const ey = VpExponent10(DATA_PTR(y)); @@ -2771,11 +2769,10 @@ BigMath_s_exp(VALUE klass, VALUE x, VALU https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2769 m = rmpd_double_figures(); } - x1 = BigDecimal_mult2(x1, x, SSIZET2NUM(n)); - ++i; - z = BigDecimal_mult(z, SSIZET2NUM(i)); - d = BigDecimal_div2(x1, z, SSIZET2NUM(m)); - y = BigDecimal_add(y, d); + d = BigDecimal_mult(d, x); /* d <- d * x */ + d = BigDecimal_div2(d, SSIZET2NUM(i), SSIZET2NUM(m)); /* d <- d / i */ + y = BigDecimal_add(y, d); /* y <- y + d */ + ++i; /* i <- i + 1 */ } if (negative) { @@ -2788,10 +2785,8 @@ BigMath_s_exp(VALUE klass, VALUE x, VALU https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2785 RB_GC_GUARD(one); RB_GC_GUARD(x); - RB_GC_GUARD(x1); RB_GC_GUARD(y); RB_GC_GUARD(d); - RB_GC_GUARD(z); } /* call-seq: -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/