Skip to content

Commit

Permalink
Add rb_big2ull
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed May 14, 2010
1 parent 54dcc04 commit 5a658af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions vm/capi/bignum.cpp
Expand Up @@ -81,7 +81,27 @@ extern "C" {
rb_raise(rb_eArgError, "parameter is not a Bignum");

return 0;
}

unsigned long long rb_big2ull(VALUE obj) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();

Object* object = env->get_object(obj);

if(object->nil_p()) {
rb_raise(rb_eTypeError, "no implicit conversion from nil to unsigned long");
} else if(Bignum* big = try_as<Bignum>(object)) {
if((size_t)mp_count_bits(big->mp_val()) > sizeof(unsigned long long) * CHAR_BIT)
rb_raise(rb_eRangeError, "bignum too big to convert into unsigned long long");

unsigned long long val = big->to_ulong_long();
if(big->mp_val()->sign == MP_NEG) return -val;
return val;
}

rb_raise(rb_eArgError, "parameter is not a Bignum");

return 0;
}

double rb_big2dbl(VALUE obj) {
Expand Down
2 changes: 2 additions & 0 deletions vm/capi/ruby.h
Expand Up @@ -795,6 +795,8 @@ double rb_num2dbl(VALUE);

long long rb_big2ll(VALUE obj);

unsigned long long rb_big2ull(VALUE);

double rb_big2dbl(VALUE obj);

int rb_big_bytes_used(VALUE obj);
Expand Down

0 comments on commit 5a658af

Please sign in to comment.