Skip to content

Commit

Permalink
Showing 1 changed file with 68 additions and 67 deletions.
135 changes: 68 additions & 67 deletions core/src/main/java/org/jruby/util/Numeric.java
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
* rights and limitations under the License.
*
* Copyright (C) 2006 Charles O Nutter <headius@headius.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
@@ -28,6 +28,7 @@
package org.jruby.util;

import org.joni.Regex;
import org.joni.WarnCallback;
import org.jcodings.specific.ASCIIEncoding;
import org.jruby.Ruby;
import org.jruby.RubyBignum;
@@ -46,7 +47,7 @@ public class Numeric {
public static final boolean CANON = true;

/** f_add
*
*
*/
public static IRubyObject f_add(ThreadContext context, IRubyObject x, IRubyObject y) {
if (y instanceof RubyFixnum && ((RubyFixnum)y).getLongValue() == 0) return x;
@@ -59,7 +60,7 @@ public static RubyInteger f_add(ThreadContext context, RubyInteger x, RubyIntege
}

/** f_cmp
*
*
*/
public static IRubyObject f_cmp(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyInteger && y instanceof RubyInteger) {
@@ -91,15 +92,15 @@ public static RubyFixnum f_cmp(ThreadContext context, RubyInteger x, long y) {
}

/** f_div
*
*
*/
public static IRubyObject f_div(ThreadContext context, IRubyObject x, IRubyObject y) {
if (y instanceof RubyFixnum && ((RubyFixnum) y).getLongValue() == 1) return x;
return sites(context).op_quo.call(context, x, x, y);
}

/** f_gt_p
*
/** f_gt_p
*
*/
public static boolean f_gt_p(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyFixnum && y instanceof RubyFixnum) {
@@ -115,8 +116,8 @@ public static boolean f_gt_p(ThreadContext context, RubyInteger x, RubyInteger y
return x.getBigIntegerValue().compareTo(y.getBigIntegerValue()) > 0;
}

/** f_lt_p
*
/** f_lt_p
*
*/
public static boolean f_lt_p(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyFixnum && y instanceof RubyFixnum) {
@@ -133,14 +134,14 @@ public static boolean f_lt_p(ThreadContext context, RubyInteger x, RubyInteger y
}

/** f_mod
*
*
*/
public static IRubyObject f_mod(ThreadContext context, IRubyObject x, IRubyObject y) {
return sites(context).op_mod.call(context, x, x, y);
}

/** f_mul
*
*
*/
public static IRubyObject f_mul(ThreadContext context, IRubyObject x, IRubyObject y) {
Ruby runtime = context.runtime;
@@ -180,7 +181,7 @@ public static IRubyObject safe_mul(ThreadContext context, IRubyObject a, IRubyOb
}

/** f_sub
*
*
*/
public static IRubyObject f_sub(ThreadContext context, IRubyObject x, IRubyObject y) {
if (y instanceof RubyFixnum && ((RubyFixnum)y).getLongValue() == 0) return x;
@@ -192,7 +193,7 @@ public static RubyInteger f_sub(ThreadContext context, RubyInteger x, RubyIntege
}

/** f_xor
*
*
*/
public static IRubyObject f_xor(ThreadContext context, IRubyObject x, IRubyObject y) {
return sites(context).op_xor.call(context, x, x, y);
@@ -203,7 +204,7 @@ public static IRubyObject f_xor(ThreadContext context, RubyInteger x, RubyIntege
}

/** f_abs
*
*
*/
public static IRubyObject f_abs(ThreadContext context, IRubyObject x) {
return sites(context).abs.call(context, x, x);
@@ -218,56 +219,56 @@ public static RubyFloat f_abs(ThreadContext context, RubyFloat x) {
}

/** f_abs2
*
*
*/
public static IRubyObject f_abs2(ThreadContext context, IRubyObject x) {
return sites(context).abs2.call(context, x, x);
}

/** f_arg
*
*
*/
public static IRubyObject f_arg(ThreadContext context, IRubyObject x) {
return sites(context).arg.call(context, x, x);
}

/** f_conjugate
*
*
*/
public static IRubyObject f_conjugate(ThreadContext context, IRubyObject x) {
return sites(context).conjugate.call(context, x, x);
}

/** f_denominator
*
*
*/
public static IRubyObject f_denominator(ThreadContext context, IRubyObject x) {
return sites(context).denominator.call(context, x, x);
}

/** f_exact_p
*
*
*/ // NOTE: not (really) used
public static boolean f_exact_p(ThreadContext context, IRubyObject x) {
return sites(context).exact.call(context, x, x).isTrue();
}

/** f_numerator
*
*
*/
public static IRubyObject f_numerator(ThreadContext context, IRubyObject x) {
return sites(context).numerator.call(context, x, x);
}

/** f_polar
*
*
*/
public static IRubyObject f_polar(ThreadContext context, IRubyObject x) {
return sites(context).polar.call(context, x, x);
}

/** f_real_p
*
*
*/
public static boolean f_real_p(ThreadContext context, IRubyObject x) {
// NOTE: can not use instanceof RubyNumeric + ((RubyNumeric) x).isReal()
@@ -286,7 +287,7 @@ public static boolean f_real_p(ThreadContext context, IRubyObject x) {
}

/** f_integer_p
*
*
*/
public static boolean f_integer_p(ThreadContext context, IRubyObject x) {
return sites(context).integer.call(context, x, x).isTrue();
@@ -306,7 +307,7 @@ public static boolean f_integer_p(ThreadContext context, RubyNumeric x) {
}

/** f_divmod
*
*
*/
public static IRubyObject f_divmod(ThreadContext context, IRubyObject x, IRubyObject y) {
return sites(context).divmod.call(context, x, x, y);
@@ -317,21 +318,21 @@ public static IRubyObject f_divmod(ThreadContext context, RubyInteger x, IRubyOb
}

/** f_floor
*
*
*/
public static IRubyObject f_floor(ThreadContext context, IRubyObject x) {
return sites(context).floor.call(context, x, x);
}

/** f_inspect
*
*
*/
public static IRubyObject f_inspect(ThreadContext context, IRubyObject x) {
return sites(context).inspect.call(context, x, x);
}

/** f_negate
*
*
*/
public static IRubyObject f_negate(ThreadContext context, IRubyObject x) {
return sites(context).op_uminus.call(context, x, x);
@@ -342,21 +343,21 @@ public static RubyInteger f_negate(ThreadContext context, RubyInteger x) {
}

/** f_to_f
*
*
*/
public static IRubyObject f_to_f(ThreadContext context, IRubyObject x) {
return sites(context).to_f.call(context, x, x);
}

/** f_to_i
*
*
*/
public static IRubyObject f_to_i(ThreadContext context, IRubyObject x) {
return sites(context).to_i.call(context, x, x);
}

/** f_to_r
*
*
*/
public static IRubyObject f_to_r(ThreadContext context, IRubyObject x) {
return sites(context).to_r.call(context, x, x);
@@ -367,24 +368,24 @@ public static RubyNumeric f_to_r(ThreadContext context, RubyInteger x) {
}

/** f_to_s
*
*
*/
public static IRubyObject f_to_s(ThreadContext context, IRubyObject x) {
return sites(context).to_s.call(context, x, x);
}

/** f_truncate
*
*
*/
public static IRubyObject f_truncate(ThreadContext context, IRubyObject x) {
return sites(context).truncate.call(context, x, x);
}

/** f_equal
*
* Note: This may not return a value which is a boolean. other.== can
* return non-boolean (which unless it is nil it will be isTrue()).
*
*
*/
public static IRubyObject f_equal(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyFixnum && y instanceof RubyFixnum) {
@@ -399,7 +400,7 @@ public static IRubyObject f_equal(ThreadContext context, RubyInteger x, RubyInte
}

/** f_expt
*
*
*/
public static IRubyObject f_expt(ThreadContext context, IRubyObject x, IRubyObject y) {
return sites(context).op_exp.call(context, x, x, y);
@@ -410,7 +411,7 @@ public static RubyNumeric f_expt(ThreadContext context, RubyInteger x, RubyInteg
}

/** f_idiv
*
*
*/
public static IRubyObject f_idiv(ThreadContext context, IRubyObject x, IRubyObject y) {
return sites(context).div.call(context, x, x, y);
@@ -421,7 +422,7 @@ public static RubyInteger f_idiv(ThreadContext context, RubyInteger x, RubyInteg
}

/** f_quo
*
*
*/
public static IRubyObject f_quo(ThreadContext context, IRubyObject x, IRubyObject y) {
return sites(context).quo.call(context, x, x, y);
@@ -432,21 +433,21 @@ public static IRubyObject f_quo(ThreadContext context, RubyFloat x, RubyFloat y)
}

/** f_rshift
*
*
*/
public static IRubyObject f_rshift(ThreadContext context, IRubyObject x, IRubyObject y) {
return sites(context).op_rshift.call(context, x, x, y);
}

/** f_lshift
*
*
*/
public static IRubyObject f_lshift(ThreadContext context, IRubyObject x, IRubyObject y) {
return sites(context).op_lshift.call(context, x, x, y);
}

/** f_negative_p
*
*
*/
public static boolean f_negative_p(ThreadContext context, IRubyObject x) {
if (x instanceof RubyInteger) return ((RubyInteger) x).signum() == -1;
@@ -462,7 +463,7 @@ public static boolean f_negative_p(ThreadContext context, RubyFloat x) {
}

/** f_zero_p
*
*
*/
public static boolean f_zero_p(ThreadContext context, IRubyObject x) {
if (x instanceof RubyInteger) return ((RubyInteger) x).isZero();
@@ -473,9 +474,9 @@ public static boolean f_zero_p(ThreadContext context, IRubyObject x) {
public static boolean f_zero_p(ThreadContext context, RubyInteger x) {
return x.isZero();
}

/** f_one_p
*
*
*/
public static boolean f_one_p(ThreadContext context, IRubyObject x) {
if (x instanceof RubyFixnum) return ((RubyFixnum) x).getLongValue() == 1;
@@ -505,7 +506,7 @@ public static boolean f_odd_p(ThreadContext context, IRubyObject i) {


/** i_gcd
*
*
*/
public static long i_gcd(long x, long y) {
if (x < 0) x = -x;
@@ -522,21 +523,21 @@ public static long i_gcd(long x, long y) {

return y;
}

/** f_gcd
*
*
*/
public static IRubyObject f_gcd(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyFixnum && y instanceof RubyFixnum) {
return RubyFixnum.newFixnum(context.runtime, i_gcd(((RubyFixnum)x).getLongValue(), ((RubyFixnum)y).getLongValue()));
}

if (f_negative_p(context, x)) x = f_negate(context, x);
if (f_negative_p(context, y)) y = f_negate(context, y);

if (f_zero_p(context, x)) return y;
if (f_zero_p(context, y)) return x;

for (;;) {
if (x instanceof RubyFixnum) {
if (((RubyFixnum)x).getLongValue() == 0) return y;
@@ -565,15 +566,15 @@ public static RubyInteger f_gcd(ThreadContext context, RubyInteger x, RubyIntege
}

/** f_lcm
*
*
*/
public static IRubyObject f_lcm(ThreadContext context, IRubyObject x, IRubyObject y) {
if (f_zero_p(context, x) || f_zero_p(context, y)) {
return RubyFixnum.zero(context.runtime);
}
return f_abs(context, f_mul(context, f_div(context, x, f_gcd(context, x, y)), y));
}

public static long i_ilog2(ThreadContext context, IRubyObject x) {
long q = (sites(context).size.call(context, x, x).convertToInteger().getLongValue() - 8) * 8 + 1;

@@ -591,7 +592,7 @@ public static long i_ilog2(ThreadContext context, IRubyObject x) {

return q + r;
}

public static double ldexp(double f, long e) {
return f * Math.pow(2.0, e);
}
@@ -617,7 +618,7 @@ public static double frexp(double mantissa, long[]e) {
e[0] = exponent;
return sign * mantissa;
}

private static final long SQRT_LONG_MAX = ((long)1) << ((8 * 8 - 1) / 2);
static boolean fitSqrtLong(long n) {
return n < SQRT_LONG_MAX && n >= -SQRT_LONG_MAX;
@@ -632,10 +633,10 @@ public static RubyNumeric int_pow(ThreadContext context, long x, long y) {
} else {
neg = false;
}

y &= ~1;
Ruby runtime = context.runtime;

do {
while (y % 2 == 0) {
if (!fitSqrtLong(x)) {
@@ -646,7 +647,7 @@ public static RubyNumeric int_pow(ThreadContext context, long x, long y) {
x *= x;
y >>= 1;
}

if (multiplyOverflows(x, z)) {
IRubyObject v = RubyBignum.newBignum(runtime, x).op_pow(context, y);
if (z != 1) v = RubyBignum.newBignum(runtime, neg ? -z : z).op_mul(context, v);
@@ -694,10 +695,10 @@ public static final class ComplexPatterns {
String PATTERN0 = "\\A" + WS + "(" + NUMBER + ")@(" + NUMBER + ")" + WS;
String PATTERN1 = "\\A" + WS + "([-+])?(" + NUMBER + ")?[iIjJ]" + WS;
String PATTERN2 = "\\A" + WS + "(" + NUMBER + ")(([-+])(" + NUMBERNOS + ")?[iIjJ])?" + WS;
comp_pat0 = new Regex(PATTERN0.getBytes(), 0, PATTERN0.length(), 0, ASCIIEncoding.INSTANCE);
comp_pat1 = new Regex(PATTERN1.getBytes(), 0, PATTERN1.length(), 0, ASCIIEncoding.INSTANCE);
comp_pat2 = new Regex(PATTERN2.getBytes(), 0, PATTERN2.length(), 0, ASCIIEncoding.INSTANCE);
underscores_pat = new Regex("_+".getBytes(), 0, 2, 0, ASCIIEncoding.INSTANCE);
comp_pat0 = new Regex(PATTERN0.getBytes(), 0, PATTERN0.length(), 0, ASCIIEncoding.INSTANCE, WarnCallback.NONE);
comp_pat1 = new Regex(PATTERN1.getBytes(), 0, PATTERN1.length(), 0, ASCIIEncoding.INSTANCE, WarnCallback.NONE);
comp_pat2 = new Regex(PATTERN2.getBytes(), 0, PATTERN2.length(), 0, ASCIIEncoding.INSTANCE, WarnCallback.NONE);
underscores_pat = new Regex("_+".getBytes(), 0, 2, 0, ASCIIEncoding.INSTANCE, WarnCallback.NONE);
}
}

@@ -709,9 +710,9 @@ public static final class RationalPatterns {
String NUMERATOR = "(?:" + DIGITS + "?\\.)?" + DIGITS + "(?:[eE][-+]?" + DIGITS + ")?";
String DENOMINATOR = DIGITS;
String PATTERN = "\\A" + WS + "([-+])?(" + NUMERATOR + ")(?:\\/(" + DENOMINATOR + "))?" + WS;
rat_pat = new Regex(PATTERN.getBytes(), 0, PATTERN.length(), 0, ASCIIEncoding.INSTANCE);
an_e_pat = new Regex("[Ee]".getBytes(), 0, 4, 0, ASCIIEncoding.INSTANCE);
a_dot_pat = new Regex("\\.".getBytes(), 0, 2, 0, ASCIIEncoding.INSTANCE);
rat_pat = new Regex(PATTERN.getBytes(), 0, PATTERN.length(), 0, ASCIIEncoding.INSTANCE, WarnCallback.NONE);
an_e_pat = new Regex("[Ee]".getBytes(), 0, 4, 0, ASCIIEncoding.INSTANCE, WarnCallback.NONE);
a_dot_pat = new Regex("\\.".getBytes(), 0, 2, 0, ASCIIEncoding.INSTANCE, WarnCallback.NONE);
}
}

0 comments on commit 7171a22

Please sign in to comment.