Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rubinius/rubinius
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: fb798948c518^
Choose a base ref
...
head repository: rubinius/rubinius
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6b132e1dfb7f
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on May 8, 2015

  1. Added specs for rb_absint_size()

    Yorick Peterse committed May 8, 2015
    Copy the full SHA
    fb79894 View commit details
  2. Boilerplate code for rb_absint_size()

    Yorick Peterse committed May 8, 2015
    Copy the full SHA
    6b132e1 View commit details
Showing with 68 additions and 0 deletions.
  1. +36 −0 spec/ruby/optional/capi/bignum_spec.rb
  2. +13 −0 spec/ruby/optional/capi/ext/bignum_spec.c
  3. +1 −0 spec/ruby/optional/capi/ext/rubyspec.h
  4. +15 −0 vm/capi/bignum.cpp
  5. +3 −0 vm/include/capi/ruby/ruby.h
36 changes: 36 additions & 0 deletions spec/ruby/optional/capi/bignum_spec.rb
Original file line number Diff line number Diff line change
@@ -188,4 +188,40 @@ def ensure_bignum(n)
@s.RBIGNUM_LEN(bignum_value * 2).should == 3
end
end

describe "rb_absint_size" do
it "returns the size and leading bits of 1" do
@s.rb_absint_size(1).should == [1, 7]
end

it "returns the size and leading bits of 2" do
@s.rb_absint_size(2).should == [1, 6]
end

it "returns the size and leading bits of 3" do
@s.rb_absint_size(3).should == [1, 6]
end

it "returns the size and leading bits of 5" do
@s.rb_absint_size(5).should == [1, 5]
end

it "returns the size and leading bits of 10" do
@s.rb_absint_size(10).should == [1, 4]
end

it "returns the size and leading bits of 666" do
@s.rb_absint_size(666).should == [2, 6]
end

it "returns the size and leading bits of 4096" do
@s.rb_absint_size(4096).should == [2, 3]
end

it "returns the size and leading bits of a Bignum" do
size = @s.rb_absint_size(1923081093210932183209123810932013298103)

size.should == [17, 5]
end
end
end
13 changes: 13 additions & 0 deletions spec/ruby/optional/capi/ext/bignum_spec.c
Original file line number Diff line number Diff line change
@@ -83,6 +83,15 @@ static VALUE bignum_spec_RBIGNUM_LEN(VALUE self, VALUE num) {
}
#endif

#ifdef HAVE_RB_ABSINT_SIZE
static VALUE bignum_spec_rb_absint_size(VALUE self, VALUE val) {
int int_zero_bits = 0;
size_t size = rb_absint_size(val, &int_zero_bits);

return rb_ary_new_from_args(2, INT2NUM(size), INT2NUM(int_zero_bits));
}
#endif

void Init_bignum_spec() {
VALUE cls;
cls = rb_define_class("CApiBignumSpecs", rb_cObject);
@@ -134,6 +143,10 @@ void Init_bignum_spec() {
#ifdef HAVE_RBIGNUM_LEN
rb_define_method(cls, "RBIGNUM_LEN", bignum_spec_RBIGNUM_LEN, 1);
#endif

#ifdef HAVE_RB_ABSINT_SIZE
rb_define_method(cls, "rb_absint_size", bignum_spec_rb_absint_size, 1);
#endif
}

#ifdef __cplusplus
1 change: 1 addition & 0 deletions spec/ruby/optional/capi/ext/rubyspec.h
Original file line number Diff line number Diff line change
@@ -99,6 +99,7 @@
#define HAVE_RB_BIG2ULONG 1
#define HAVE_RB_BIG_CMP 1
#define HAVE_RB_BIG_PACK 1
#define HAVE_RB_ABSINT_SIZE 1

/* Class */
#define HAVE_RB_CALL_SUPER 1
15 changes: 15 additions & 0 deletions vm/capi/bignum.cpp
Original file line number Diff line number Diff line change
@@ -164,4 +164,19 @@ extern "C" {
INTEGER_PACK_LSWORD_FIRST | INTEGER_PACK_NATIVE_BYTE_ORDER |
INTEGER_PACK_2COMP);
}

size_t rb_absint_size(VALUE val, int *nlz_bits_ret)
{
VALUE abs = capi_fast_call(val, rb_intern("abs"), 0);
size_t size;

if(FIXNUM_P(abs)) {
size = 0;
// Bignum
} else {
size = rb_big_bytes_used(abs);
}

return size;
}
}
3 changes: 3 additions & 0 deletions vm/include/capi/ruby/ruby.h
Original file line number Diff line number Diff line change
@@ -1106,6 +1106,9 @@ struct RTypedData {
void rb_big_pack(VALUE val, unsigned long *buf, long num_longs);

int rb_big_sign(VALUE obj);

size_t rb_absint_size(VALUE val, int *nlz_bits_ret);

#define RBIGNUM_SIGN(obj) rb_big_sign(obj)
#define RBIGNUM_POSITIVE_P(b) RBIGNUM_SIGN(b)
#define RBIGNUM_NEGATIVE_P(b) (!RBIGNUM_SIGN(b))