ruby-changes:28998
From: akr <ko1@a...>
Date: Mon, 3 Jun 2013 23:49:42 +0900 (JST)
Subject: [ruby-changes:28998] akr:r41050 (trunk): * bignum.c (bitlength_bdigit): New function.
akr 2013-06-03 23:49:33 +0900 (Mon, 03 Jun 2013) New Revision: 41050 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41050 Log: * bignum.c (bitlength_bdigit): New function. (rb_big_pow): Use bitlength_bdigit instead of ffs. Modified files: trunk/ChangeLog trunk/bignum.c Index: ChangeLog =================================================================== --- ChangeLog (revision 41049) +++ ChangeLog (revision 41050) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon Jun 3 23:47:55 2013 Tanaka Akira <akr@f...> + + * bignum.c (bitlength_bdigit): New function. + (rb_big_pow): Use bitlength_bdigit instead of ffs. + Mon Jun 3 23:11:19 2013 Ayumu AIZAWA <ayumu.aizawa@g...> * lib/fileutils.rb: fix behavior when mkdir/mkdir_p accepted "/". Index: bignum.c =================================================================== --- bignum.c (revision 41049) +++ bignum.c (revision 41050) @@ -3178,6 +3178,24 @@ bigsqr(VALUE x) https://github.com/ruby/ruby/blob/trunk/bignum.c#L3178 return bigtrunc(bigmul0(x, x)); } +static int +bitlength_bdigit(BDIGIT v) +{ + if (v == 0) + return 0; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; +#if 2 < SIZEOF_BDIGITS + v |= v >> 16; +#endif + v++; + if (v == 0) + return SIZEOF_BDIGITS*CHAR_BIT; + return ffs(v); /* assumption: sizeof(BDIGIT) <= sizeof(int) */ +} + /* * call-seq: * big ** exponent -> numeric @@ -3219,7 +3237,7 @@ rb_big_pow(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L3237 VALUE z = 0; SIGNED_VALUE mask; const long xlen = RBIGNUM_LEN(x) - 1; - const long xbits = ffs(RBIGNUM_DIGITS(x)[xlen]) + SIZEOF_BDIGITS*BITSPERDIG*xlen; + const long xbits = bitlength_bdigit(RBIGNUM_DIGITS(x)[xlen]) + SIZEOF_BDIGITS*BITSPERDIG*xlen; const long BIGLEN_LIMIT = BITSPERDIG*1024*1024; if ((xbits > BIGLEN_LIMIT) || (xbits * yy > BIGLEN_LIMIT)) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/