Skip to content

Commit

Permalink
Use Value utility methos instead of raw Float and Double
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Oct 1, 2024
1 parent 55167d5 commit 0dd1477
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 28 deletions.
11 changes: 2 additions & 9 deletions aot/src/main/java/com/dylibso/chicory/aot/AotUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dylibso.chicory.aot;

import static com.dylibso.chicory.wasm.types.Value.REF_NULL_VALUE;
import static java.lang.invoke.MethodHandles.identity;
import static java.lang.invoke.MethodHandles.publicLookup;
import static java.lang.invoke.MethodType.methodType;
import static org.objectweb.asm.Type.getInternalName;
Expand Down Expand Up @@ -29,39 +30,31 @@ public enum StackSize {
}

private static final Method LONG_TO_I32;
private static final Method LONG_TO_I64;
private static final Method LONG_TO_F32;
private static final Method LONG_TO_F64;
private static final Method I32_TO_LONG;
private static final Method I64_TO_LONG;
private static final Method F32_TO_LONG;
private static final Method F64_TO_LONG;
private static final MethodHandle LONG_TO_I32_MH;
private static final MethodHandle LONG_TO_I64_MH;
private static final MethodHandle LONG_TO_F32_MH;
private static final MethodHandle LONG_TO_F64_MH;
private static final MethodHandle I32_TO_LONG_MH;
private static final MethodHandle I64_TO_LONG_MH;
private static final MethodHandle F32_TO_LONG_MH;
private static final MethodHandle F64_TO_LONG_MH;

static {
try {
LONG_TO_I32 = ValueConversions.class.getMethod("longToI32", long.class);
LONG_TO_I64 = ValueConversions.class.getMethod("longToI64", long.class);
LONG_TO_F32 = ValueConversions.class.getMethod("longToF32", long.class);
LONG_TO_F64 = ValueConversions.class.getMethod("longToF64", long.class);
I32_TO_LONG = ValueConversions.class.getMethod("i32ToLong", int.class);
I64_TO_LONG = ValueConversions.class.getMethod("i64ToLong", long.class);
F32_TO_LONG = ValueConversions.class.getMethod("f32ToLong", float.class);
F64_TO_LONG = ValueConversions.class.getMethod("f64ToLong", double.class);

LONG_TO_I32_MH = publicLookup().unreflect(LONG_TO_I32);
LONG_TO_I64_MH = publicLookup().unreflect(LONG_TO_I64);
LONG_TO_F32_MH = publicLookup().unreflect(LONG_TO_F32);
LONG_TO_F64_MH = publicLookup().unreflect(LONG_TO_F64);
I32_TO_LONG_MH = publicLookup().unreflect(I32_TO_LONG);
I64_TO_LONG_MH = publicLookup().unreflect(I64_TO_LONG);
F32_TO_LONG_MH = publicLookup().unreflect(F32_TO_LONG);
F64_TO_LONG_MH = publicLookup().unreflect(F64_TO_LONG);
} catch (NoSuchMethodException e) {
Expand Down Expand Up @@ -196,7 +189,7 @@ public static MethodHandle jvmToLongHandle(ValueType type) {
case FuncRef:
return I32_TO_LONG_MH;
case I64:
return I64_TO_LONG_MH;
return identity(long.class);
case F32:
return F32_TO_LONG_MH;
case F64:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ public static int longToI32(long val) {
return (int) val;
}

public static long longToI64(long val) {
return val;
}

public static float longToF32(long val) {
return Value.longToFloat(val);
}
Expand All @@ -28,10 +24,6 @@ public static long i32ToLong(int val) {
return val;
}

public static long i64ToLong(long val) {
return val;
}

public static long f32ToLong(float val) {
return Value.floatToLong(val);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private void processModule(TypeElement type) {
}
cu.addImport("com.dylibso.chicory.runtime.HostFunction");
cu.addImport("com.dylibso.chicory.runtime.Instance");
cu.addImport("com.dylibso.chicory.wasm.types.Value");
cu.addImport("com.dylibso.chicory.wasm.types.ValueType");
cu.addImport("java.util.List");

Expand Down Expand Up @@ -161,16 +162,14 @@ private Expression processMethod(
paramTypes.add(valueType("F32"));
arguments.add(
new MethodCallExpr(
new NameExpr("Float"),
"intBitsToFloat",
new NodeList<>(new CastExpr(parseType("int"), argExpr))));
new NameExpr("Value"), "longToFloat", new NodeList<>(argExpr)));
break;
case "double":
paramTypes.add(valueType("F64"));
arguments.add(
new MethodCallExpr(
new NameExpr("Double"),
"doubleToLongBits",
new NameExpr("Value"),
"longToDouble",
new NodeList<>(argExpr)));
break;
case "java.lang.String":
Expand Down Expand Up @@ -228,16 +227,16 @@ private Expression processMethod(
returnType.add(valueType("F32"));
returnExpr =
new MethodCallExpr(
new NameExpr("Float"),
"floatToRawIntBits",
new NameExpr("Value"),
"floatToLong",
new NodeList<>(new NameExpr("result")));
break;
case "double":
returnType.add(valueType("F64"));
returnExpr =
new MethodCallExpr(
new NameExpr("Double"),
"doubleToLongBits",
new NameExpr("Value"),
"doubleToLong",
new NodeList<>(new NameExpr("result")));
break;
default:
Expand Down
5 changes: 3 additions & 2 deletions function-processor/src/test/resources/BasicMathGenerated.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType;
import java.util.List;
import javax.annotation.processing.Generated;
Expand All @@ -27,8 +28,8 @@ public static HostFunction[] toHostFunctions(BasicMath functions) {
new HostFunction("math",
"square",
(Instance instance, long... args) -> {
double result = functions.pow2(Float.intBitsToFloat((int) args[0]));
return new long[] { Double.doubleToLongBits(result) };
double result = functions.pow2(Value.longToFloat(args[0]));
return new long[] { Value.doubleToLong(result) };
},
List.of(ValueType.F32),
List.of(ValueType.F64)), //
Expand Down
1 change: 1 addition & 0 deletions function-processor/src/test/resources/NestedGenerated.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import chicory.testing.Box.Nested;
import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType;
import java.util.List;
import javax.annotation.processing.Generated;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType;
import java.util.List;
import javax.annotation.processing.Generated;
Expand Down
1 change: 1 addition & 0 deletions function-processor/src/test/resources/SimpleGenerated.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType;
import java.util.List;
import javax.annotation.processing.Generated;
Expand Down

0 comments on commit 0dd1477

Please sign in to comment.