Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Double/Float casting and comparisons, push floats as 64bit to stack (#…
Browse files Browse the repository at this point in the history
…5813)

Fix double cast
push floats and doubles to stack as double
add some more tests around float/double comparisons
  • Loading branch information
yowl authored and morganbr committed May 17, 2018
1 parent 94f3464 commit 09437ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/ILCompiler.WebAssembly/src/CodeGen/EvaluationStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public LLVMValueRef ValueForStackKind(StackValueKind kind, LLVMBuilderRef builde
else if (kind == StackValueKind.Int64)
return ValueAsInt64(builder, signExtend);
else if (kind == StackValueKind.Float)
return ValueAsType(LLVM.FloatType(), builder);
return ValueAsType(LLVM.DoubleType(), builder);
else if (kind == StackValueKind.NativeInt || kind == StackValueKind.ByRef || kind == StackValueKind.ObjRef)
return ValueAsInt32(builder, false);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ internal static LLVMValueRef CastIfNecessary(LLVMBuilderRef builder, LLVMValueRe
if (sourceType.Pointer == valueType.Pointer)
return source;

LLVMTypeKind toStoreKind = LLVM.GetTypeKind(LLVM.TypeOf(source));
LLVMTypeKind toStoreKind = LLVM.GetTypeKind(sourceType);
LLVMTypeKind valueTypeKind = LLVM.GetTypeKind(valueType);

LLVMValueRef typedToStore = source;
Expand Down Expand Up @@ -613,19 +613,16 @@ internal static LLVMValueRef CastIfNecessary(LLVMBuilderRef builder, LLVMValueRe
Debug.Assert(toStoreKind != LLVMTypeKind.LLVMPointerTypeKind && valueTypeKind != LLVMTypeKind.LLVMPointerTypeKind);
typedToStore = LLVM.BuildIntCast(builder, source, valueType, "CastInt" + (name ?? ""));
}
else if (toStoreKind != LLVMTypeKind.LLVMFloatTypeKind && valueTypeKind == LLVMTypeKind.LLVMFloatTypeKind)
else if (toStoreKind == LLVMTypeKind.LLVMIntegerTypeKind && (valueTypeKind == LLVMTypeKind.LLVMDoubleTypeKind || valueTypeKind == LLVMTypeKind.LLVMFloatTypeKind))
{
typedToStore = LLVM.BuildFPCast(builder, source, valueType, "CastFloat" + (name ?? ""));
//TODO: keep track of the TypeDesc so we can call BuildUIToFP when the integer is unsigned
typedToStore = LLVM.BuildSIToFP(builder, source, valueType, "CastSIToFloat" + (name ?? ""));
}
else if ((toStoreKind == LLVMTypeKind.LLVMDoubleTypeKind || toStoreKind == LLVMTypeKind.LLVMFloatTypeKind) &&
valueTypeKind == LLVMTypeKind.LLVMIntegerTypeKind)
{
//TODO: keep track of the TypeDesc so we can call BuildFPToUI when the integer is unsigned
typedToStore = LLVM.BuildFPToSI(builder, source, valueType, "CastFloat" + (name ?? ""));
}
else if (toStoreKind == LLVMTypeKind.LLVMIntegerTypeKind && valueTypeKind == LLVMTypeKind.LLVMDoubleTypeKind)
{
throw new NotImplementedException();
typedToStore = LLVM.BuildFPToSI(builder, source, valueType, "CastFloatSI" + (name ?? ""));
}

return typedToStore;
Expand Down
30 changes: 30 additions & 0 deletions tests/src/Simple/HelloWasm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,36 @@ private static unsafe int Main(string[] args)
PrintLine("Runtime.Helpers array initialization test: Ok.");
}

int intToCast = 1;
double castedDouble = (double)intToCast;
if (castedDouble == 1d)
{
PrintLine("(double) cast test: Ok.");
}
else
{
var toInt = (int)castedDouble;
// PrintLine("expected 1m, but was " + castedDouble.ToString()); // double.ToString is not compiling at the time of writing, but this would be better output
PrintLine($"(double) cast test : Failed. Back to int on next line");
PrintLine(toInt.ToString());
}

if (1f < 2d && 1d < 2f && 1f == 1d)
{
PrintLine("different width float comparisons: Ok.");
}

// floats are 7 digits precision, so check some double more precise to make sure there is no loss occurring through some inadvertent cast to float
if (10.23456789d != 10.234567891d)
{
PrintLine("double precision comparison: Ok.");
}

if (12.34567f == 12.34567f && 12.34567f != 12.34568f)
{
PrintLine("float comparison: Ok.");
}

// This test should remain last to get other results before stopping the debugger
PrintLine("Debugger.Break() test: Ok if debugger is open and breaks.");
System.Diagnostics.Debugger.Break();
Expand Down

0 comments on commit 09437ab

Please sign in to comment.