Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f32 and f64 specs passing #27

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<configuration>
<wastToProcess>i32.wast,
i64.wast,
f32.wast,
f64.wast,
return.wast,
local_get.wast,
local_set.wast,
Expand Down
172 changes: 172 additions & 0 deletions runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java
Original file line number Diff line number Diff line change
Expand Up @@ -814,13 +814,185 @@ void eval(List<Instruction> code) throws ChicoryException {
this.stack.push(Value.i32(z));
break;
}
case F32_ADD:
{
var a = this.stack.pop().asFloat();
var b = this.stack.pop().asFloat();
this.stack.push(Value.fromFloat(a + b));
break;
}
case F64_ADD:
{
var a = this.stack.pop().asDouble();
var b = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(a + b));
break;
}
case F32_SUB:
{
var a = this.stack.pop().asFloat();
var b = this.stack.pop().asFloat();
this.stack.push(Value.fromFloat(b - a));
break;
}
case F64_SUB:
{
var a = this.stack.pop().asDouble();
var b = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(b - a));
break;
}
case F32_MUL:
{
var a = this.stack.pop().asFloat();
var b = this.stack.pop().asFloat();
this.stack.push(Value.fromFloat(b * a));
break;
}
case F64_MUL:
{
var a = this.stack.pop().asDouble();
var b = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(b * a));
break;
}
case F32_DIV:
{
var a = this.stack.pop().asFloat();
var b = this.stack.pop().asFloat();
this.stack.push(Value.fromFloat(b / a));
break;
}
case F64_DIV:
{
var a = this.stack.pop().asDouble();
var b = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(b / a));
break;
}
case F32_MIN:
{
var a = this.stack.pop().asFloat();
var b = this.stack.pop().asFloat();
this.stack.push(Value.fromFloat(Math.min(a, b)));
break;
}
case F64_MIN:
{
var a = this.stack.pop().asDouble();
var b = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(Math.min(a, b)));
break;
}
case F32_MAX:
{
var a = this.stack.pop().asFloat();
var b = this.stack.pop().asFloat();
this.stack.push(Value.fromFloat(Math.max(a, b)));
break;
}
case F64_MAX:
{
var a = this.stack.pop().asDouble();
var b = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(Math.max(a, b)));
break;
}
case F32_SQRT:
{
var val = this.stack.pop().asFloat();
this.stack.push(
Value.fromFloat(
Double.valueOf(
Math.sqrt(
Float.valueOf(val)
.doubleValue()))
.floatValue()));
break;
}
case F64_SQRT:
{
var val = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(Math.sqrt(val)));
break;
}
case F32_FLOOR:
{
var val = this.stack.pop().asFloat();
this.stack.push(
Value.fromFloat(
Double.valueOf(
Math.floor(
Float.valueOf(val)
.doubleValue()))
.floatValue()));
break;
}
case F64_FLOOR:
{
var val = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(Math.floor(val)));
break;
}
case F32_CEIL:
{
var val = this.stack.pop().asFloat();
this.stack.push(
Value.fromFloat(
Double.valueOf(
Math.ceil(
Float.valueOf(val)
.doubleValue()))
.floatValue()));
break;
}
case F64_CEIL:
{
var val = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(Math.ceil(val)));
break;
}
case F32_TRUNC:
{
var val = this.stack.pop().asFloat();
this.stack.push(
Value.fromFloat(
Double.valueOf(
(val < 0)
? Math.ceil(
Float.valueOf(val)
.doubleValue())
: Math.floor(
Float.valueOf(val)
.doubleValue()))
.floatValue()));
break;
}
case F64_TRUNC:
{
var val = this.stack.pop().asDouble();
this.stack.push(
Value.fromDouble((val < 0) ? Math.ceil(val) : Math.floor(val)));
break;
}
case F32_NEAREST:
{
var val = this.stack.pop().asFloat();
this.stack.push(
Value.fromFloat(
Double.valueOf(
Math.rint(
Float.valueOf(val)
.doubleValue()))
.floatValue()));
break;
}
case F64_NEAREST:
{
var val = this.stack.pop().asDouble();
this.stack.push(Value.fromDouble(Math.rint(val)));
break;
}
// For the extend_* operations, note that java
// automatically does this when casting from
// smaller to larger primitives
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,33 @@ public String toJavaValue() {
case I64:
return "Long.parseUnsignedLong(\"" + value + "\")";
case F32:
return "Float.parseFloat(\"" + value + "\")";
if (value != null) {
switch (value) {
case "nan:canonical":
case "nan:arithmetic":
return "Float.NaN";
default:
return "Float.intBitsToFloat(Integer.parseUnsignedInt(\""
+ value
+ "\"))";
}
} else {
return "null";
}
case F64:
return "Double.parseDouble(\"" + value + "\")";
if (value != null) {
switch (value) {
case "nan:canonical":
case "nan:arithmetic":
return "Double.NaN";
default:
return "Double.longBitsToDouble(Long.parseUnsignedLong(\""
+ value
+ "\"))";
}
} else {
return "null";
}
default:
throw new IllegalArgumentException("Type not recognized " + type);
}
Expand All @@ -36,13 +60,13 @@ public String toJavaValue() {
public String toWasmValue() {
switch (type) {
case I32:
return "Value.i32(Integer.parseUnsignedInt(\"" + value + "\"))";
return "Value.i32(" + toJavaValue() + ")";
case I64:
return "Value.i64(Long.parseUnsignedLong(\"" + value + "\"))";
return "Value.i64(" + toJavaValue() + ")";
case F32:
return "Value.fromFloat(Float.parseFloat(\"" + value + "\"))";
return "Value.fromFloat(" + toJavaValue() + ")";
case F64:
return "Value.fromDouble(Double.parseDouble(\"" + value + "\"))";
return "Value.fromDouble(" + toJavaValue() + ")";
default:
throw new IllegalArgumentException("Type not recognized " + type);
}
Expand Down
15 changes: 0 additions & 15 deletions wasm/src/main/java/com/dylibso/chicory/wasm/Encoding.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.dylibso.chicory.wasm;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public final class Encoding {
/**
Expand Down Expand Up @@ -87,18 +86,4 @@ public static int computeLeb128Size(int value) {
} while (value != 0);
return size;
}

public static float longToFloat(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES).order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt((int) x);
buffer.rewind();
return buffer.getFloat();
}

public static double longToDouble(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.LITTLE_ENDIAN);
buffer.putLong(x);
buffer.rewind();
return buffer.getDouble();
}
}
10 changes: 5 additions & 5 deletions wasm/src/main/java/com/dylibso/chicory/wasm/types/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class Value {
}

public static Value fromFloat(float data) {
return Value.f32((int) data);
return Value.f32(Float.floatToIntBits(data));
}

public static Value fromDouble(double data) {
return Value.f64((long) data);
return Value.f64(Double.doubleToLongBits(data));
}

public static Value i32(long data) {
Expand Down Expand Up @@ -151,11 +151,11 @@ public short asShort() {
}

public float asFloat() {
return (float) asInt();
return Float.intBitsToFloat(asInt());
}

public double asDouble() {
return (double) asLong();
return Double.longBitsToDouble(asLong());
}

public String toString() {
Expand All @@ -177,7 +177,7 @@ public String toString() {
return this.asDouble() + "@f64";
}
default:
throw new RuntimeException("TODO handle float");
throw new RuntimeException("TODO handle missing types");
}
}

Expand Down
4 changes: 2 additions & 2 deletions wasm/src/test/java/com/dylibso/chicory/wasm/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ public void shouldParseFloats() {
var module = parser.parseModule();
var codeSection = module.getCodeSection();
var fbody = codeSection.getFunctionBodies()[0];
var f32 = Encoding.longToFloat(fbody.getInstructions().get(0).getOperands()[0]);
var f32 = Float.intBitsToFloat((int) fbody.getInstructions().get(0).getOperands()[0]);
assertEquals(0.12345678f, f32, 0.0);
var f64 = Encoding.longToDouble(fbody.getInstructions().get(1).getOperands()[0]);
var f64 = Double.longBitsToDouble(fbody.getInstructions().get(1).getOperands()[0]);
assertEquals(0.123456789012345d, f64, 0.0);
}

Expand Down
16 changes: 14 additions & 2 deletions wasm/src/test/java/com/dylibso/chicory/wasm/types/ValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@ public void shouldEncodeValuesFromLong() {
var i64 = Value.i64(123L);
assertEquals("123@i64", i64.toString());
var f32 = Value.f32(123L);
assertEquals("123.0@f32", f32.toString());
assertEquals("1.72E-43@f32", f32.toString());
var f64 = Value.f64(123L);
assertEquals("123.0@f64", f64.toString());
assertEquals("6.1E-322@f64", f64.toString());
}

@Test
public void shouldConvertFloats() {
var f32Ref = 0.12345678f;
var f32 = Value.f32(1039980265L);
assertEquals(f32Ref, f32.asFloat(), 0.0);
assertArrayEquals(f32.getData(), Value.fromFloat(f32Ref).getData());
var f64Ref = 0.123456789012345d;
var f64 = Value.f64(4593560419847042606L);
assertEquals(f64Ref, f64.asDouble(), 0.0);
assertArrayEquals(f64.getData(), Value.fromDouble(f64Ref).getData());
}
}