Skip to content

Commit

Permalink
Fix rb_type() for values >= 1**62 and < 1**63 and corresponding negat…
Browse files Browse the repository at this point in the history
…ive values

* So rb_type(value) == T_FIXNUM is equivalent to RB_FIXNUM_P().
* Fix related rb_big* functions so they handle such values (which are not RubyBignum objects).
  • Loading branch information
eregon committed Nov 13, 2023
1 parent 0d966be commit 8404d4f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
17 changes: 16 additions & 1 deletion lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ module Truffle::CExt

T_MASK = 0x1f

RUBY_FIXNUM_MIN = -(1 << 62)
RUBY_FIXNUM_MAX = (1 << 62) - 1

# This list of types is derived from MRI's error.c builtin_types array.
BUILTIN_TYPES = [
'',
Expand Down Expand Up @@ -276,7 +279,11 @@ def rb_tr_cached_type(value, type)
T_OBJECT
end
elsif type == T_FIXNUM
Truffle::Type.fits_into_long?(value) ? T_FIXNUM : T_BIGNUM
if RUBY_FIXNUM_MIN <= value && value <= RUBY_FIXNUM_MAX
T_FIXNUM
else
T_BIGNUM
end
else
type
end
Expand Down Expand Up @@ -427,6 +434,14 @@ def rb_num2long(val)
Primitive.rb_num2long(val)
end

def rb_big_sign(val)
val >= 0
end

def rb_big_cmp(x, y)
x <=> y
end

def rb_big2dbl(val)
Truffle::Type.rb_big2dbl(val)
end
Expand Down
4 changes: 2 additions & 2 deletions src/main/c/cext/integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ size_t rb_absint_size(VALUE value, int *nlz_bits_ret) {
}

int rb_big_sign(VALUE x) {
return RTEST(RUBY_INVOKE(x, ">=", INT2FIX(0))) ? 1 : 0;
return RTEST(RUBY_CEXT_INVOKE("rb_big_sign", x)) ? 1 : 0;
}

int rb_cmpint(VALUE val, VALUE a, VALUE b) {
return polyglot_as_i32(RUBY_CEXT_INVOKE_NO_WRAP("rb_cmpint", val, a, b));
}

VALUE rb_big_cmp(VALUE x, VALUE y) {
return RUBY_INVOKE(x, "<=>", y);
return RUBY_CEXT_INVOKE("rb_big_cmp", x, y);
}

void rb_big_pack(VALUE val, unsigned long *buf, long num_longs) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/c/cext/numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,6 @@ long rb_big2long(VALUE x) {
return polyglot_as_i64(RUBY_CEXT_INVOKE_NO_WRAP("rb_num2long", x));
}

VALUE rb_big2str(VALUE x, int base) {
return rb_tr_wrap(polyglot_invoke(rb_tr_unwrap(x), "to_s", base));
}

unsigned long rb_big2ulong(VALUE x) {
return polyglot_as_i64(RUBY_CEXT_INVOKE_NO_WRAP("rb_num2ulong", x));
}
Expand Down Expand Up @@ -262,6 +258,10 @@ VALUE rb_fix2str(VALUE x, int base) {
return RUBY_CEXT_INVOKE("rb_fix2str", x, INT2FIX(base));
}

VALUE rb_big2str(VALUE x, int base) {
return rb_fix2str(x, base);
}

VALUE rb_to_int(VALUE object) {
return RUBY_CEXT_INVOKE("rb_to_int", object);
}

0 comments on commit 8404d4f

Please sign in to comment.