|
62 | 62 |
|
63 | 63 | import static org.jruby.api.Convert.asBoolean; |
64 | 64 | import static org.jruby.api.Convert.asFixnum; |
| 65 | +import static org.jruby.api.Convert.numToInt; |
| 66 | +import static org.jruby.api.Convert.numToLong; |
65 | 67 | import static org.jruby.api.Create.newArray; |
66 | 68 | import static org.jruby.api.Create.newEmptyArray; |
67 | 69 | import static org.jruby.api.Error.argumentError; |
@@ -329,28 +331,25 @@ public IRubyObject times(ThreadContext context, Block block) { |
329 | 331 | */ |
330 | 332 | @Override |
331 | 333 | public IRubyObject ceil(ThreadContext context, IRubyObject arg){ |
332 | | - long ndigits = arg.convertToInteger().asLong(context); |
333 | | - if (ndigits >= 0) { |
334 | | - return this; |
335 | | - } else { |
336 | | - long self = this.value; |
337 | | - long posdigits = Math.abs(ndigits); |
338 | | - long exp = (long) Math.pow(10, posdigits); |
339 | | - long mod = (self % exp + exp) % exp; |
340 | | - long res = self; |
341 | | - if (mod != 0) { |
342 | | - res = self + (exp - (mod)); |
343 | | - } |
344 | | - return newFixnum(context.runtime, res); |
345 | | - } |
| 334 | + long ndigits = numToLong(context, arg); |
| 335 | + if (ndigits >= 0) return this; |
| 336 | + |
| 337 | + long self = this.value; |
| 338 | + long posdigits = Math.abs(ndigits); |
| 339 | + long exp = (long) Math.pow(10, posdigits); |
| 340 | + long mod = (self % exp + exp) % exp; |
| 341 | + long res = self; |
| 342 | + if (mod != 0) res = self + (exp - (mod)); |
| 343 | + |
| 344 | + return asFixnum(context, res); |
346 | 345 | } |
347 | 346 |
|
348 | 347 | /** rb_fix_floor |
349 | 348 | * |
350 | 349 | */ |
351 | 350 | @Override |
352 | 351 | public IRubyObject floor(ThreadContext context, IRubyObject arg){ |
353 | | - long ndigits = arg.convertToInteger().asLong(context); |
| 352 | + long ndigits = numToLong(context, arg); |
354 | 353 | if (ndigits >= 0) return this; |
355 | 354 |
|
356 | 355 | long self = this.value; |
@@ -888,22 +887,20 @@ protected IRubyObject intPowTmp1(ThreadContext context, RubyInteger y, long mm, |
888 | 887 | long tmp = 1L; |
889 | 888 | long yy; |
890 | 889 |
|
891 | | - for (/*NOP*/; !(y instanceof RubyFixnum); y = (RubyInteger) sites(context).op_rshift.call(context, y, y, RubyFixnum.one(context.runtime))) { |
892 | | - if (f_odd_p(context, y)) { |
893 | | - tmp = (tmp * xx) % mm; |
894 | | - } |
| 890 | + var one = asFixnum(context, 1); |
| 891 | + for (/*NOP*/; !(y instanceof RubyFixnum); y = (RubyInteger) sites(context).op_rshift.call(context, y, y, one)) { |
| 892 | + if (f_odd_p(context, y)) tmp = (tmp * xx) % mm; |
| 893 | + |
895 | 894 | xx = (xx * xx) % mm; |
896 | 895 | } |
897 | | - for (yy = y.convertToInteger().asLong(context); yy != 0L; yy >>= 1L) { |
898 | | - if ((yy & 1L) != 0L) { |
899 | | - tmp = (tmp * xx) % mm; |
900 | | - } |
| 896 | + for (yy = numToLong(context, y); yy != 0L; yy >>= 1L) { |
| 897 | + if ((yy & 1L) != 0L) tmp = (tmp * xx) % mm; |
| 898 | + |
901 | 899 | xx = (xx * xx) % mm; |
902 | 900 | } |
903 | 901 |
|
904 | | - if (negaFlg && (tmp != 0)) { |
905 | | - tmp -= mm; |
906 | | - } |
| 902 | + if (negaFlg && (tmp != 0)) tmp -= mm; |
| 903 | + |
907 | 904 | return asFixnum(context, tmp); |
908 | 905 | } |
909 | 906 |
|
@@ -1040,10 +1037,10 @@ public final int compareTo(IRubyObject other) { |
1040 | 1037 | } |
1041 | 1038 |
|
1042 | 1039 | private int compareToOther(IRubyObject other) { |
1043 | | - if (other instanceof RubyBignum) return BigInteger.valueOf(value).compareTo(((RubyBignum) other).value); |
1044 | | - if (other instanceof RubyFloat) return Double.compare((double) value, ((RubyFloat) other).value); |
1045 | | - ThreadContext context = metaClass.runtime.getCurrentContext(); |
1046 | | - return (int) coerceCmp(context, sites(context).op_cmp, other).convertToInteger().asLong(context); |
| 1040 | + if (other instanceof RubyBignum bignum) return BigInteger.valueOf(value).compareTo(bignum.value); |
| 1041 | + if (other instanceof RubyFloat flote) return Double.compare((double) value, flote.value); |
| 1042 | + ThreadContext context = getRuntime().getCurrentContext(); |
| 1043 | + return numToInt(context, coerceCmp(context, sites(context).op_cmp, other)); |
1047 | 1044 | } |
1048 | 1045 |
|
1049 | 1046 | /** fix_cmp |
|
0 commit comments