Skip to content

Commit

Permalink
Merge pull request #131 from Ladicek/bitwise-operations
Browse files Browse the repository at this point in the history
Bitwise operations
  • Loading branch information
Ladicek committed Oct 19, 2022
2 parents c5b8084 + af71b0c commit 9c1c4b3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
27 changes: 27 additions & 0 deletions src/main/java/io/quarkus/gizmo/BytecodeCreator.java
Expand Up @@ -996,6 +996,33 @@ default void breakScope() {
*/
ResultHandle multiply(ResultHandle a1, ResultHandle a2);

/**
* Computes the bitwise AND of the two result handles and returns the result
*
* @param a1 The first number
* @param a2 The second number
* @return The result
*/
ResultHandle bitwiseAnd(ResultHandle a1, ResultHandle a2);

/**
* Computes the bitwise OR of the two result handles and returns the result
*
* @param a1 The first number
* @param a2 The second number
* @return The result
*/
ResultHandle bitwiseOr(ResultHandle a1, ResultHandle a2);

/**
* Computes the bitwise XOR of the two result handles and returns the result
*
* @param a1 The first number
* @param a2 The second number
* @return The result
*/
ResultHandle bitwiseXor(ResultHandle a1, ResultHandle a2);

/**
* Increments a ResultHandle
*
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/io/quarkus/gizmo/BytecodeCreatorImpl.java
Expand Up @@ -27,7 +27,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1249,15 +1248,30 @@ public ForEachLoop forEach(ResultHandle iterable) {

@Override
public ResultHandle add(ResultHandle a1, ResultHandle a2) {
return emitArithmetic(Opcodes.IADD, a1, a2);
return emitBinaryArithmetic(Opcodes.IADD, a1, a2);
}

@Override
public ResultHandle multiply(ResultHandle a1, ResultHandle a2) {
return emitArithmetic(Opcodes.IMUL, a1, a2);
return emitBinaryArithmetic(Opcodes.IMUL, a1, a2);
}

private ResultHandle emitArithmetic(int intOpcode, ResultHandle a1, ResultHandle a2) {
@Override
public ResultHandle bitwiseAnd(ResultHandle a1, ResultHandle a2) {
return emitBinaryArithmetic(Opcodes.IAND, a1, a2);
}

@Override
public ResultHandle bitwiseOr(ResultHandle a1, ResultHandle a2) {
return emitBinaryArithmetic(Opcodes.IOR, a1, a2);
}

@Override
public ResultHandle bitwiseXor(ResultHandle a1, ResultHandle a2) {
return emitBinaryArithmetic(Opcodes.IXOR, a1, a2);
}

private ResultHandle emitBinaryArithmetic(int intOpcode, ResultHandle a1, ResultHandle a2) {
Objects.requireNonNull(a1);
Objects.requireNonNull(a2);
if (!a1.getType().equals(a2.getType())) {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/io/quarkus/gizmo/Gizmo.java
Expand Up @@ -1023,6 +1023,9 @@ public ResultHandle getInstance() {
}

private static class EqualsHashCodeToStringGenerator {
private static final MethodDescriptor FLOAT_TO_INT_BITS = MethodDescriptor.ofMethod(Float.class, "floatToIntBits", int.class, float.class);
private static final MethodDescriptor DOUBLE_TO_LONG_BITS = MethodDescriptor.ofMethod(Double.class, "doubleToLongBits", long.class, double.class);

private static final MethodDescriptor BOOLEAN_ARRAY_EQUALS = MethodDescriptor.ofMethod(Arrays.class, "equals", boolean.class, boolean[].class, boolean[].class);
private static final MethodDescriptor BYTE_ARRAY_EQUALS = MethodDescriptor.ofMethod(Arrays.class, "equals", boolean.class, byte[].class, byte[].class);
private static final MethodDescriptor SHORT_ARRAY_EQUALS = MethodDescriptor.ofMethod(Arrays.class, "equals", boolean.class, short[].class, short[].class);
Expand Down Expand Up @@ -1111,13 +1114,13 @@ private void generateEquals() {
.falseBranch().returnBoolean(false);
break;
case "F": // float
// could use `Float.compare` instead of `==`
equals.ifZero(equals.compareFloat(thisValue, thatValue, false))
// this is consistent with Arrays.equals() and it's also what IntelliJ generates
equals.ifIntegerEqual(equals.invokeStaticMethod(FLOAT_TO_INT_BITS, thisValue), equals.invokeStaticMethod(FLOAT_TO_INT_BITS, thatValue))
.falseBranch().returnBoolean(false);
break;
case "D": // double
// could use `Double.compare` instead of `==`
equals.ifZero(equals.compareDouble(thisValue, thatValue, false))
// this is consistent with Arrays.equals() and it's also what IntelliJ generates
equals.ifZero(equals.compareLong(equals.invokeStaticMethod(DOUBLE_TO_LONG_BITS, thisValue), equals.invokeStaticMethod(DOUBLE_TO_LONG_BITS, thatValue)))
.falseBranch().returnBoolean(false);
break;
case "[Z": // boolean[]
Expand Down

0 comments on commit 9c1c4b3

Please sign in to comment.