Skip to content

Commit caf8daa

Browse files
committedMar 27, 2018
Revert "Add Integer#pow. #4876"
This reverts commit b119ccb.
1 parent b119ccb commit caf8daa

File tree

3 files changed

+1
-164
lines changed

3 files changed

+1
-164
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/RubyFixnum.java

-85
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
import org.jruby.util.Numeric;
6161
import org.jruby.util.cli.Options;
6262

63-
import static org.jruby.util.Numeric.f_odd_p;
64-
6563
/**
6664
* Implementation of the Fixnum class.
6765
*/
@@ -880,89 +878,6 @@ private RubyNumeric powerFixnum(ThreadContext context, RubyFixnum other) {
880878
return Numeric.int_pow(context, a, b);
881879
}
882880

883-
// MRI: int_pow_tmp1
884-
protected IRubyObject intPowTmp1(ThreadContext context, RubyInteger y, long mm, boolean negaFlg) {
885-
Ruby runtime = context.runtime;
886-
887-
long xx = getLongValue();
888-
long tmp = 1L;
889-
long yy;
890-
891-
for (/*NOP*/; !(y instanceof RubyFixnum); y = (RubyInteger) sites(context).op_rshift.call(context, y, y, RubyFixnum.one(runtime))) {
892-
if (f_odd_p(context, y)) {
893-
tmp = (tmp * xx) % mm;
894-
}
895-
xx = (xx * xx) % mm;
896-
}
897-
for (yy = y.convertToInteger().getLongValue(); yy != 0L; yy >>= 1L) {
898-
if ((yy & 1L) != 0L) {
899-
tmp = (tmp * xx) % mm;
900-
}
901-
xx = (xx * xx) % mm;
902-
}
903-
904-
if (negaFlg && (tmp != 0)) {
905-
tmp -= mm;
906-
}
907-
return runtime.newFixnum(tmp);
908-
}
909-
910-
// MRI: int_pow_tmp2
911-
protected IRubyObject intPowTmp2(ThreadContext context, IRubyObject y, long mm, boolean negaFlg) {
912-
Ruby runtime = context.runtime;
913-
914-
long tmp = 1L;
915-
long yy;
916-
917-
final IRubyObject m = runtime.newFixnum(mm);
918-
RubyFixnum tmp2 = runtime.newFixnum(tmp);
919-
RubyFixnum xx = (RubyFixnum) this;
920-
921-
for (/*NOP*/; !(y instanceof RubyFixnum); y = sites(context).op_rshift.call(context, y, y, RubyFixnum.one(runtime))) {
922-
if (f_odd_p(context, y)) {
923-
tmp2 = mulModulo(context, tmp2, xx, m);
924-
}
925-
xx = mulModulo(context, xx, xx, m);
926-
}
927-
for (yy = ((RubyFixnum) y).getLongValue(); yy != 0; yy >>= 1L) {
928-
if ((yy & 1L) != 0) {
929-
tmp2 = mulModulo(context, tmp2, xx, m);
930-
}
931-
xx = mulModulo(context, xx, xx, m);
932-
}
933-
934-
tmp = tmp2.getLongValue();
935-
if (negaFlg && (tmp != 0)) {
936-
tmp -= mm;
937-
}
938-
return runtime.newFixnum(tmp);
939-
}
940-
941-
protected IRubyObject intPowTmp3(ThreadContext context, RubyInteger y, RubyBignum m, boolean negaFlg) {
942-
Ruby runtime = context.runtime;
943-
944-
BigInteger xn, yn, mn, zn;
945-
946-
xn = BigInteger.valueOf(this.getLongValue());
947-
if (y instanceof RubyFixnum) {
948-
yn = BigInteger.valueOf(y.getLongValue());
949-
} else {
950-
yn = y.getBigIntegerValue();
951-
}
952-
mn = m.getBigIntegerValue();
953-
954-
zn = xn.modPow(yn, mn);
955-
if (negaFlg & zn.signum() == 1) {
956-
zn = zn.negate();
957-
}
958-
return RubyBignum.bignorm(runtime, zn);
959-
}
960-
961-
// MRI: MUL_MODULO macro defined within int_pow_tmp2 in numeric.c
962-
private static RubyFixnum mulModulo(ThreadContext context, RubyFixnum a, RubyFixnum b, IRubyObject c) {
963-
return (RubyFixnum) ((RubyInteger) a.op_mul(context, b.getLongValue())).modulo(context, c);
964-
}
965-
966881
/** fix_abs
967882
*
968883
*/

Diff for: ‎core/src/main/java/org/jruby/RubyInteger.java

-74
Original file line numberDiff line numberDiff line change
@@ -725,80 +725,6 @@ public IRubyObject op_mod(ThreadContext context, long other) {
725725
@JRubyMethod(name = "**")
726726
public abstract IRubyObject op_pow(ThreadContext context, IRubyObject other);
727727

728-
@JRubyMethod(name = "pow")
729-
public IRubyObject pow(ThreadContext context, IRubyObject other) {
730-
return sites(context).op_pow.call(context, this, this, other);
731-
}
732-
733-
private final long HALF_LONG_MSB = 0x80000000L;
734-
735-
// MRI: rb_int_powm
736-
@JRubyMethod(name = "pow")
737-
public IRubyObject pow(ThreadContext context, IRubyObject b, IRubyObject m) {
738-
Ruby runtime = context.runtime;
739-
740-
RubyInteger a = this;
741-
742-
boolean negaFlg = false;
743-
if (!(b instanceof RubyInteger)) {
744-
throw runtime.newTypeError("Integer#pow() 2nd argument not allowed unless a 1st argument is integer");
745-
}
746-
RubyInteger intB = (RubyInteger) b;
747-
if (negativeInt(context, intB)) {
748-
throw runtime.newRangeError("Integer#pow() 1st argument cannot be negative when 2nd argument specified");
749-
}
750-
if (!(m instanceof RubyInteger)) {
751-
throw runtime.newTypeError("Integer#pow() 2nd argument not allowed unless all arguments are integers");
752-
}
753-
754-
if (negativeInt(context, m)) {
755-
m = sites(context).op_uminus.call(context, m, m);
756-
negaFlg = true;
757-
}
758-
759-
if (!positiveInt(context, m)) {
760-
throw runtime.newZeroDivisionError();
761-
}
762-
if (m instanceof RubyFixnum) {
763-
long halfVal = HALF_LONG_MSB;
764-
long mm = m.convertToInteger().getLongValue();
765-
RubyFixnum modulo = (RubyFixnum) a.modulo(context, m);
766-
if (mm <= halfVal) {
767-
return modulo.intPowTmp1(context, intB, mm, negaFlg);
768-
} else {
769-
return modulo.intPowTmp2(context, intB, mm, negaFlg);
770-
}
771-
} else if (m instanceof RubyBignum) {
772-
return ((RubyInteger) a.modulo(context, m)).intPowTmp3(context, intB, (RubyBignum) m, negaFlg);
773-
}
774-
// not reached
775-
throw new RuntimeException("BUG: unexpected type " + m.getType());
776-
}
777-
778-
protected IRubyObject intPowTmp3(ThreadContext context, RubyInteger y, RubyBignum m, boolean negaFlg) {
779-
Ruby runtime = context.runtime;
780-
781-
BigInteger xn, yn, mn, zn;
782-
783-
if (this instanceof RubyFixnum) {
784-
xn = BigInteger.valueOf(this.getLongValue());
785-
} else {
786-
xn = this.getBigIntegerValue();
787-
}
788-
if (y instanceof RubyFixnum) {
789-
yn = BigInteger.valueOf(y.getLongValue());
790-
} else {
791-
yn = y.getBigIntegerValue();
792-
}
793-
mn = m.getBigIntegerValue();
794-
795-
zn = xn.modPow(yn, mn);
796-
if (negaFlg & zn.signum() == 1) {
797-
zn = zn.negate();
798-
}
799-
return RubyBignum.bignorm(runtime, zn);
800-
}
801-
802728
@JRubyMethod(name = "abs")
803729
public abstract IRubyObject abs(ThreadContext context);
804730

Diff for: ‎core/src/main/java/org/jruby/runtime/JavaSites.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,7 @@ public static class IntegerSites {
210210
public final CallSite op_minus = new FunctionalCachingCallSite("-");
211211
public final CallSite op_quo = new FunctionalCachingCallSite("/");
212212
public final CallSite op_mod = new FunctionalCachingCallSite("%");
213-
public final CallSite size = new FunctionalCachingCallSite("**");
214-
public final CallSite op_pow = new FunctionalCachingCallSite("size");
215-
public final CallSite op_uminus = new FunctionalCachingCallSite("-@");
213+
public final CallSite size = new FunctionalCachingCallSite("size");
216214
public final CheckedSites to_i_checked = new CheckedSites("to_i");
217215
}
218216

@@ -237,8 +235,6 @@ public static class FixnumSites {
237235
public final CallSite op_lt_bignum = new FunctionalCachingCallSite("<");
238236
public final CallSite op_exp_rational = new FunctionalCachingCallSite("**");
239237
public final CallSite fdiv = new FunctionalCachingCallSite("fdiv");
240-
public final CallSite op_uminus = new FunctionalCachingCallSite("-@");
241-
public final CallSite op_rshift = new FunctionalCachingCallSite(">>");
242238
public final CheckedSites checked_op_and = new CheckedSites("&");
243239
public final CheckedSites checked_op_or = new CheckedSites("|");
244240
public final CheckedSites checked_op_xor = new CheckedSites("^");

0 commit comments

Comments
 (0)
Please sign in to comment.