Skip to content

Fix conv.r4/r8 conversion and type hint in ConvHandler - #158

Merged
Washi1337 merged 1 commit into
Washi1337:masterfrom
wondercrash:fix/conv-r4-r8
Mar 2, 2026
Merged

Fix conv.r4/r8 conversion and type hint in ConvHandler#158
Washi1337 merged 1 commit into
Washi1337:masterfrom
wondercrash:fix/conv-r4-r8

Conversation

@wondercrash

@wondercrash wondercrash commented Feb 26, 2026

Copy link
Copy Markdown
Contributor

Sometimes conv.r4/conv.r8 would produce garbage values because the handler didn't actually convert them. Also, HandleUnknownFloatConversion was using an Integer type hint for floats instead of the Float type hint.

Example:

using System;
using System.Linq;
using AsmResolver.DotNet;
using Echo.Platforms.AsmResolver.Emulation;

var module = ModuleDefinition.FromModule(typeof(ConvTest).Module);
var type = module.TopLevelTypes.First(t => t.Name == nameof(ConvTest));

var vm = new CilVirtualMachine(module, false);
var thread = vm.CreateThread();

var convToFloat = type.Methods.First(m => m.Name == nameof(ConvTest.IntToFloat));
var r4 = thread.Call(convToFloat, [42]);
Console.WriteLine($"IntToFloat(42) = {r4!.AsSpan().F32} expected: 42");

var convToDouble = type.Methods.First(m => m.Name == nameof(ConvTest.IntToDouble));
var r8 = thread.Call(convToDouble, [-1]);
Console.WriteLine($"IntToDouble(-1) = {r8!.AsSpan().F64} expected: -1");

public class ConvTest
{
    public static float IntToFloat(int x) => (float)x;
    public static double IntToDouble(int x) => (double)x;
}

outputs:

IntToFloat(42) = 5.9E-44 expected: 42
IntToDouble(-1) = NaN expected: -1

The double cast in the Write call is necessary because otherwise it would implicitly convert it to a float. I kept the float cast for readability.

I also added a lot of tests for conv which I believe are correct.

@Washi1337 Washi1337 added bug Something isn't working emulation Issue related to the emulation modules of Echo. dotnet-asmresolver Issues related to the .NET back-end powered by AsmResolver labels Mar 2, 2026
@Washi1337
Washi1337 merged commit ba8e549 into Washi1337:master Mar 2, 2026
2 checks passed
@Washi1337

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working dotnet-asmresolver Issues related to the .NET back-end powered by AsmResolver emulation Issue related to the emulation modules of Echo.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants