public IRubyObject op_plus_two(ThreadContext context) { long result = value + 2; //- if (result == Long.MIN_VALUE + 1) { //-code if (result < value) { //+code+patch; maybe use if (result <= value) { return addAsBignum(context, 2); } return newFixnum(context.runtime, result); }
private void fixnumEach(ThreadContext context, Block block) { // We must avoid integer overflows. long to = ((RubyFixnum) end).getLongValue(); if (isExclusive) { if (to == Long.MIN_VALUE) { return; } to--; } RubyInteger.fixnumUpto(context, ((RubyFixnum) begin).getLongValue(), to, block); }
public static RubyFixnum five(Ruby runtime) { return CACHE_OFFSET > 5 ? runtime.fixnumCache[CACHE_OFFSET + 5] : new RubyFixnum(runtime, 5); }
/** fix_mod * */ @Override public IRubyObject op_mod(ThreadContext context, IRubyObject other) { checkZeroDivisionError(context, other); if (other instanceof RubyFixnum) { return moduloFixnum(context, (RubyFixnum) other); } return coerceBin(context, sites(context).op_mod, other); }
private IRubyObject op_cmpCommon(ThreadContext context, IRubyObject other) { Ruby runtime = context.runtime; // deal with case when "other" is not a string if (other.respondsTo("to_str") && other.respondsTo("<=>")) { IRubyObject result = invokedynamic(context, other, OP_CMP, this); if (result.isNil()) return result; if (result instanceof RubyFixnum) { return RubyFixnum.newFixnum(runtime, -((RubyFixnum)result).getLongValue()); } else { return RubyFixnum.zero(runtime).callMethod(context, "-", result); } } return runtime.getNil(); }
private IRubyObject addFixnum(ThreadContext context, RubyFixnum other) { long otherValue = other.value; long result = value + otherValue; if (additionOverflowed(value, otherValue, result)) { return addAsBignum(context, other); } return newFixnum(context.runtime, result); }
private void numericStep(ThreadContext context, Ruby runtime, IRubyObject step, Block block) { final String method = isExclusive ? "<" : "<="; IRubyObject beg = begin; long i = 0; while (beg.callMethod(context, method, end).isTrue()) { block.yield(context, beg); i++; beg = begin.callMethod(context, "+", RubyFixnum.newFixnum(runtime, i).callMethod(context, "*", step)); } }
private static IRubyObject descendingFactorial(ThreadContext context, long from, long howMany) { Ruby runtime = context.runtime; IRubyObject cnt = howMany >= 0 ? RubyFixnum.one(runtime) : RubyFixnum.zero(runtime); CallSite op_times = sites(context).op_times; while (howMany-- > 0) { RubyFixnum v = RubyFixnum.newFixnum(runtime, from--); cnt = op_times.call(context, cnt, cnt, v); } return cnt; }
/** * * @param context ThreadContext * @param scalar IRubyObject * @return this Vec2 object rotated */ @JRubyMethod(name = "rotate!") public IRubyObject rotate_bang(ThreadContext context, IRubyObject scalar) { double theta = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); double x = (jx * Math.cos(theta) - jy * Math.sin(theta)); double y = (jx * Math.sin(theta) + jy * Math.cos(theta)); jx = x; jy = y; return this; }
public static RubyFixnum f_cmp(ThreadContext context, RubyInteger x, long y) { final int cmp; if (x instanceof RubyFixnum) { cmp = Long.compare(((RubyFixnum) x).getLongValue(), y); } else { // RubyBignum cmp = x.getBigIntegerValue().compareTo(BigInteger.valueOf(y)); } return RubyFixnum.newFixnum(context.runtime, cmp); }
/** fix_divmod * */ @Override public IRubyObject divmod(ThreadContext context, IRubyObject other) { checkZeroDivisionError(context, other); if (other instanceof RubyFixnum) { return divmodFixnum(context, (RubyFixnum) other); } return coerceBin(context, sites(context).divmod, other); }
public static IRubyObject newUnsigned64(Ruby runtime, long value) { return value < 0 ? RubyBignum.newBignum(runtime, BigInteger.valueOf(value & 0x7fffffffffffffffL).add(UINT64_BASE)) : RubyFixnum.newFixnum(runtime, value); }
/** fix_mod * */ @JRubyMethod(name = {"%", "modulo"}) public IRubyObject op_mod(ThreadContext context, IRubyObject other) { if (context.runtime.is1_9()) checkZeroDivisionError(context, other); if (other instanceof RubyFixnum) { return moduloFixnum(context, (RubyFixnum)other); } return coerceBin(context, "%", other); }
/** fix_divmod * */ @JRubyMethod(name = "divmod") @Override public IRubyObject divmod(ThreadContext context, IRubyObject other) { if (context.runtime.is1_9()) checkZeroDivisionError(context, other); if (other instanceof RubyFixnum) { return divmodFixnum(context, other); } return coerceBin(context, "divmod", other); }
/** * @param context * @return RubyInteger */ public IRubyObject op_minus_one(ThreadContext context) { try { return newFixnum(context.runtime, Math.subtractExact(value, 1)); } catch (ArithmeticException ae) { return subtractAsBignum(context, 1); } }
public static RubyFixnum newFixnum(Ruby runtime, long value) { if (USE_CACHE && isInCacheRange(value)) { return cachedFixnum(runtime, value); } return new RubyFixnum(runtime, value); }
/** fix_div * here is terrible MRI gotcha: * 1.div 3.0 -> 0 * 1 / 3.0 -> 0.3333333333333333 * * MRI is also able to do it in one place by looking at current frame in rb_num_coerce_bin: * rb_funcall(x, ruby_frame->orig_func, 1, y); * * also note that RubyFloat doesn't override Numeric.div */ @Override public IRubyObject idiv(ThreadContext context, IRubyObject other) { checkZeroDivisionError(context, other); return idiv(context, other, sites(context).div); }
public static RubyFixnum newFixnum(Ruby runtime, long value) { if (isInCacheRange(value)) { return runtime.fixnumCache[(int) value + CACHE_OFFSET]; } return new RubyFixnum(runtime, value); }