Clean up long-->float helpers in `RamSearchEngine`, remove some tests

This commit is contained in:
YoshiRulz 2024-08-16 23:49:16 +10:00
parent 60366c13b5
commit 58a9f64a18
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 2 additions and 47 deletions

View File

@ -6,12 +6,6 @@ namespace BizHawk.Client.Common.RamSearchEngine
{
internal static class Extensions
{
public static float ToFloat(this long val)
{
var bytes = BitConverter.GetBytes((int)val);
return BitConverter.ToSingle(bytes, 0);
}
public static IEnumerable<IMiniWatch> ToBytes(this IEnumerable<long> addresses, SearchEngineSettings settings)
=> settings.IsDetailed()
? addresses.ToDetailedBytes(settings.Domain)

View File

@ -12,10 +12,10 @@ namespace BizHawk.Client.Common.RamSearchEngine
{
public class RamSearchEngine
{
/// <remarks>TODO move to BizHawk.Common</remarks>
private static float ReinterpretAsF32(long l)
{
return Unsafe.As<long, float>(ref l);
var lsw = unchecked((uint) l);
return Unsafe.As<uint, float>(ref lsw);
}
private Compare _compareTo = Compare.Previous;

View File

@ -1,39 +0,0 @@
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
namespace BizHawk.Tests.Common;
// test for RamSearchEngine.ReinterpretAsF32
[TestClass]
public class ConversionTests
{
private static float ReinterpretAsF32Unsafe(long l)
{
return Unsafe.As<long, float>(ref l);
}
private static readonly byte[] ScratchSpace = new byte[8];
private static float ReinterpretAsF32BitConverter(long l)
{
BinaryPrimitives.WriteInt64LittleEndian(ScratchSpace, l);
return BitConverter.ToSingle(ScratchSpace, startIndex: 0);
}
[TestMethod]
[DoNotParallelize] // old implementation is not thread safe
[DataRow(0, 0)]
[DataRow(1, 1.401298E-45F)]
[DataRow(1109917696, 42)]
[DataRow(1123477881, 123.456F)]
[DataRow(3212836864, -1)]
[DataRow(0x7fffffffbf800000, -1)]
public void TestReinterpretAsF32(long input, float expected)
{
float f32BitConverter = ReinterpretAsF32BitConverter(input);
float f32Unsafe = ReinterpretAsF32Unsafe(input);
Assert.AreEqual(expected, f32BitConverter);
Assert.AreEqual(expected, f32Unsafe);
}
}