Simplify and optimize bit roll operations

Suggested in #1666, ran many testing to check it gives the same results
This commit is contained in:
altrisi committed 2023-03-08 13:24:37 +01:00
1 parent 3f19cce649
commit 93bec37dac
2 files changed
+8 -19

No files matched your search

+6 -5
View File
@@ -45,10 +45,11 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.BinaryOperator; import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleToLongFunction; import java.util.function.DoubleToLongFunction;
import java.util.function.DoubleUnaryOperator; import java.util.function.DoubleUnaryOperator;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.LongBinaryOperator;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -537,16 +538,16 @@ public class Expression
addUnaryFunction(name, (v) -> new NumericValue(fun.applyAsLong(NumericValue.asNumber(v).getDouble()))); addUnaryFunction(name, (v) -> new NumericValue(fun.applyAsLong(NumericValue.asNumber(v).getDouble())));
} }
public void addMathematicalBinaryIntFunction(String name, BinaryOperator<Long> fun) public void addMathematicalBinaryIntFunction(String name, LongBinaryOperator fun)
{ {
addBinaryFunction(name, (w, v) -> addBinaryFunction(name, (w, v) ->
new NumericValue(fun.apply(NumericValue.asNumber(w).getLong(), NumericValue.asNumber(v).getLong()))); new NumericValue(fun.applyAsLong(NumericValue.asNumber(w).getLong(), NumericValue.asNumber(v).getLong())));
} }
public void addMathematicalBinaryFunction(String name, BinaryOperator<Double> fun) public void addMathematicalBinaryFunction(String name, DoubleBinaryOperator fun)
{ {
addBinaryFunction(name, (w, v) -> addBinaryFunction(name, (w, v) ->
new NumericValue(fun.apply(NumericValue.asNumber(w).getDouble(), NumericValue.asNumber(v).getDouble()))); new NumericValue(fun.applyAsDouble(NumericValue.asNumber(w).getDouble(), NumericValue.asNumber(v).getDouble())));
} }
@@ -315,20 +315,8 @@ public class Operators
expression.addFunctionalEquivalence("<=", "nondecreasing"); expression.addFunctionalEquivalence("<=", "nondecreasing");
expression.addMathematicalBinaryIntFunction("bitwise_shift_left", (num, amount) -> num << amount); expression.addMathematicalBinaryIntFunction("bitwise_shift_left", (num, amount) -> num << amount);
expression.addMathematicalBinaryIntFunction("bitwise_shift_right", (num, amount) -> num >> amount); expression.addMathematicalBinaryIntFunction("bitwise_shift_right", (num, amount) -> num >> amount);
expression.addMathematicalBinaryIntFunction("bitwise_roll_left", (num, num2) -> { expression.addMathematicalBinaryIntFunction("bitwise_roll_left", (num, amount) -> Long.rotateLeft(num, (int)amount));
long amount = num2 % 64; expression.addMathematicalBinaryIntFunction("bitwise_roll_right", (num, amount) -> Long.rotateRight(num, (int)amount));
long amountToRoll = 64 - amount;
long rolledBits = ((-1L) >> amountToRoll) << amountToRoll;
long rolledAmount = (num & rolledBits) >> amountToRoll;
return num << amount | rolledAmount;
});
expression.addMathematicalBinaryIntFunction("bitwise_roll_right", (num, num2) -> {
long amount = num2 % 64;
long amountToRoll = 64 - amount;
long rolledBits = ((-1L) << amountToRoll) >> amountToRoll;
long rolledAmount = (num & rolledBits) << amountToRoll;
return num >> amount | rolledAmount;
});
expression.addMathematicalUnaryIntFunction("bitwise_not", d -> { expression.addMathematicalUnaryIntFunction("bitwise_not", d -> {
long num = (long) d; long num = (long) d;
return ~num; return ~num;