Forum: Ruby-dev Fwd: akr:r42752 (trunk): * numeric.c (rb_fix_bit_length): Moved from bignum.c.

308cbef6e86dfc49cce3b2d4cf42aedc?d=identicon&s=25 SASADA Koichi (Guest)
on 2013-09-02 20:03
(Received via mailing list)
$BEDCf$5$s(B

compiling ../trunk/numeric.c
../trunk/numeric.c:3532:32: error: implicit conversion loses integer
precision:
      'long' to 'unsigned int' [-Werror,-Wshorten-64-to-32]
    return LONG2FIX(bit_length(v));
           ~~~~~~~~~~~~~~~~~~~~^~~
../trunk/internal.h:226:68: note: expanded from:
        (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) :
\
                                                                   ^
../trunk/include/ruby/ruby.h:232:29: note: expanded from:
#define LONG2FIX(i) INT2FIX(i)
                            ^
../trunk/include/ruby/ruby.h:231:45: note: expanded from:
#define INT2FIX(i) ((VALUE)(((SIGNED_VALUE)(i))<<1 | FIXNUM_FLAG))
                                            ^
1 error generated.
make: *** [numeric.o] Error 1

$B$3$s$J$N$,(B clang
$ clang --version
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM
3.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
$B$G=P$F$*$j$^$9!#(B


-------- Original Message --------
Subject: [ruby-changes:30673] akr:r42752 (trunk): * numeric.c
(rb_fix_bit_length): Moved from bignum.c.
Date: Sun,  1 Sep 2013 10:31:22 +0900 (JST)
From: akr <ko1@atdot.net>
Reply-To: ruby-changes@quickml.atdot.net
To: ruby-changes@quickml.atdot.net

akr  2013-09-01 10:31:16 +0900 (Sun, 01 Sep 2013)

  New Revision: 42752

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=r...

  Log:
    * numeric.c (rb_fix_bit_length): Moved from bignum.c.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/numeric.c
Index: ChangeLog
===================================================================
--- ChangeLog  (revision 42751)
+++ ChangeLog  (revision 42752)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Sep  1 10:30:42 2013  Tanaka Akira  <akr@fsij.org>
+
+  * numeric.c (rb_fix_bit_length): Moved from bignum.c.
+
 Sun Sep  1 09:55:45 2013  Tanaka Akira  <akr@fsij.org>

   * internal.h (bit_length): Moved from bignum.c.
Index: numeric.c
===================================================================
--- numeric.c  (revision 42751)
+++ numeric.c  (revision 42752)
@@ -3493,6 +3493,45 @@ fix_size(VALUE fix)
https://github.com/ruby/ruby/blob/trunk/numeric.c#L3493
     return INT2FIX(sizeof(long));
 }

+/*
+ *  call-seq:
+ *     int.bit_length -> integer
+ *
+ *  Returns the number of bits of the value of <i>int</i>.
+ *
+ *  "the number of bits" means that
+ *  the bit position of the highest bit which is different to the sign
bit.
+ *  (The bit position of the bit 2**n is n+1.)
+ *  If there is no such bit (zero or minus one), zero is returned.
+ *
+ *  I.e. This method returns ceil(log2(int < 0 ? -int : int+1)).
+ *
+ *     (-2**12-1).bit_length     #=> 13
+ *     (-2**12).bit_length       #=> 12
+ *     (-2**12+1).bit_length     #=> 12
+ *     -0x101.bit_length         #=> 9
+ *     -0x100.bit_length         #=> 8
+ *     -0xff.bit_length          #=> 8
+ *     -2.bit_length             #=> 1
+ *     -1.bit_length             #=> 0
+ *     0.bit_length              #=> 0
+ *     1.bit_length              #=> 1
+ *     0xff.bit_length           #=> 8
+ *     0x100.bit_length          #=> 9
+ *     (2**12-1).bit_length      #=> 12
+ *     (2**12).bit_length        #=> 13
+ *     (2**12+1).bit_length      #=> 13
+ */
+
+static VALUE
+rb_fix_bit_length(VALUE fix)
+{
+    long v = FIX2LONG(fix);
+    if (v < 0)
+        v = ~v;
+    return LONG2FIX(bit_length(v));
+}
+
 static VALUE
 int_upto_size(VALUE from, VALUE args, VALUE eobj)
 {
@@ -3864,6 +3903,7 @@ Init_Numeric(void)
https://github.com/ruby/ruby/blob/trunk/numeric.c#L3903

     rb_define_method(rb_cFixnum, "to_f", fix_to_f, 0);
     rb_define_method(rb_cFixnum, "size", fix_size, 0);
+    rb_define_method(rb_cFixnum, "bit_length", rb_fix_bit_length, 0);
     rb_define_method(rb_cFixnum, "zero?", fix_zero_p, 0);
     rb_define_method(rb_cFixnum, "odd?", fix_odd_p, 0);
     rb_define_method(rb_cFixnum, "even?", fix_even_p, 0);
Index: bignum.c
===================================================================
--- bignum.c  (revision 42751)
+++ bignum.c  (revision 42752)
@@ -6608,45 +6608,6 @@ rb_big_bit_length(VALUE big)
https://github.com/ruby/ruby/blob/trunk/bignum.c#L6608

 /*
  *  call-seq:
- *     int.bit_length -> integer
- *
- *  Returns the number of bits of the value of <i>int</i>.
- *
- *  "the number of bits" means that
- *  the bit position of the highest bit which is different to the sign
bit.
- *  (The bit position of the bit 2**n is n+1.)
- *  If there is no such bit (zero or minus one), zero is returned.
- *
- *  I.e. This method returns ceil(log2(int < 0 ? -int : int+1)).
- *
- *     (-2**12-1).bit_length     #=> 13
- *     (-2**12).bit_length       #=> 12
- *     (-2**12+1).bit_length     #=> 12
- *     -0x101.bit_length         #=> 9
- *     -0x100.bit_length         #=> 8
- *     -0xff.bit_length          #=> 8
- *     -2.bit_length             #=> 1
- *     -1.bit_length             #=> 0
- *     0.bit_length              #=> 0
- *     1.bit_length              #=> 1
- *     0xff.bit_length           #=> 8
- *     0x100.bit_length          #=> 9
- *     (2**12-1).bit_length      #=> 12
- *     (2**12).bit_length        #=> 13
- *     (2**12+1).bit_length      #=> 13
- */
-
-static VALUE
-rb_fix_bit_length(VALUE fix)
-{
-    long v = FIX2LONG(fix);
-    if (v < 0)
-        v = ~v;
-    return LONG2FIX(bit_length(v));
-}
-
-/*
- *  call-seq:
  *     big.odd? -> true or false
  *
  *  Returns <code>true</code> if <i>big</i> is an odd number.
@@ -6740,7 +6701,5 @@ Init_Bignum(void)
https://github.com/ruby/ruby/blob/trunk/bignum.c#L6701
     rb_define_method(rb_cBignum, "odd?", rb_big_odd_p, 0);
     rb_define_method(rb_cBignum, "even?", rb_big_even_p, 0);

-    rb_define_method(rb_cFixnum, "bit_length", rb_fix_bit_length, 0);
-
     power_cache_init();
 }
This topic is locked and can not be replied to.