classlib: add Integer/Long.compareUnsigned

This commit is contained in:
Alexey Andreev 2023-03-25 09:37:44 +01:00
parent d25cb1fa16
commit a70251fe83
6 changed files with 43 additions and 0 deletions

View File

@ -38,6 +38,13 @@ public class IntegerNativeGenerator implements Injector {
context.writeExpr(context.getArgument(1));
context.getWriter().append(")");
break;
case "compareUnsigned":
context.getWriter().append("$rt_ucmp(");
context.writeExpr(context.getArgument(0));
context.getWriter().append(",").ws();
context.writeExpr(context.getArgument(1));
context.getWriter().append(")");
break;
}
}
}

View File

@ -30,6 +30,11 @@ public class LongNativeGenerator implements Generator {
.append(context.getParameterName(2)).append(");").softNewLine();
context.useLongLibrary();
break;
case "compareUnsigned":
writer.append("return Long_ucompare(").append(context.getParameterName(1)).append(", ")
.append(context.getParameterName(2)).append(");").softNewLine();
context.useLongLibrary();
break;
case "divideUnsigned":
writer.append("return Long_udiv(").append(context.getParameterName(1)).append(", ")
.append(context.getParameterName(2)).append(");").softNewLine();

View File

@ -368,4 +368,8 @@ public class TInteger extends TNumber implements TComparable<TInteger> {
@InjectedBy(IntegerNativeGenerator.class)
@NoSideEffects
public static native int remainderUnsigned(int dividend, int divisor);
@InjectedBy(IntegerNativeGenerator.class)
@NoSideEffects
public static native int compareUnsigned(int a, int b);
}

View File

@ -359,4 +359,8 @@ public class TLong extends TNumber implements TComparable<TLong> {
@GeneratedBy(LongNativeGenerator.class)
@NoSideEffects
public static native long remainderUnsigned(long dividend, long divisor);
@GeneratedBy(LongNativeGenerator.class)
@NoSideEffects
public static native int compareUnsigned(long a, long b);
}

View File

@ -22,6 +22,7 @@ var Long_ge;
var Long_lt;
var Long_le;
var Long_compare;
var Long_ucompare;
var Long_add;
var Long_sub;
var Long_inc;
@ -185,6 +186,18 @@ if (typeof BigInt !== 'function') {
return (a.lo & 1) - (b.lo & 1);
}
Long_ucompare = function(a, b) {
var r = $rt_ucmp(a.hi, b.hi);
if (r !== 0) {
return r;
}
r = (a.lo >>> 1) - (b.lo >>> 1);
if (r !== 0) {
return r;
}
return (a.lo & 1) - (b.lo & 1);
}
Long_mul = function(a, b) {
var positive = Long_isNegative(a) === Long_isNegative(b);
if (Long_isNegative(a)) {
@ -608,6 +621,11 @@ if (typeof BigInt !== 'function') {
Long_compare = function(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}
Long_ucompare = function(a, b) {
a = BigInt.asUintN(64, a);
b = BigInt.asUintN(64, b);
return a < b ? -1 : a > b ? 1 : 0;
}
Long_mul = function(a, b) {
return BigInt.asIntN(64, a * b);

View File

@ -879,6 +879,11 @@ var $rt_udiv = function(a, b) {
var $rt_umod = function(a, b) {
return ((a >>> 0) % (b >>> 0)) >>> 0;
};
var $rt_ucmp = function(a, b) {
a >>>= 0;
b >>>= 0;
return a < b ? -1 : a > b ? 1 : 0;
};
function $rt_checkBounds(index, array) {
if (index < 0 || index >= array.length) {
$rt_throwAIOOBE();