Adopt `Unsafe.As` in a few more places

This commit is contained in:
YoshiRulz 2024-08-16 22:45:32 +10:00
parent 58a9f64a18
commit ac4469db36
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 16 additions and 13 deletions

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using BizHawk.Common;
using BizHawk.Emulation.Common;
@ -287,7 +288,8 @@ namespace BizHawk.Client.Common
LogCallback($"Warning: Attempted read {addr} outside memory size of {d.Size}");
return default;
}
return BitConverter.ToSingle(BitConverter.GetBytes(d.PeekUint(addr, _isBigEndian)), 0);
var value = d.PeekUint(addr, _isBigEndian);
return Unsafe.As<uint, float>(ref value);
}
public void WriteFloat(long addr, float value, string domain = null)
@ -303,7 +305,7 @@ namespace BizHawk.Client.Common
LogCallback($"Warning: Attempted write {addr} outside memory size of {d.Size}");
return;
}
d.PokeUint(addr, BitConverter.ToUInt32(BitConverter.GetBytes(value), 0), _isBigEndian);
d.PokeUint(addr, Unsafe.As<float, uint>(ref value), _isBigEndian);
}
public int ReadS8(long addr, string domain = null) => (sbyte) ReadUnsigned(addr, 1, domain);

View File

@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -70,6 +72,8 @@ namespace BizHawk.Client.Common
/// <returns>True if value successfully sets; otherwise, false</returns>
public override bool Poke(string value)
{
static uint ReinterpretAsU32(float f)
=> Unsafe.As<float, uint>(ref f);
try
{
uint val = Type switch
@ -79,7 +83,7 @@ namespace BizHawk.Client.Common
WatchDisplayType.Hex => uint.Parse(value, NumberStyles.HexNumber),
WatchDisplayType.FixedPoint_20_12 => (uint)(double.Parse(value, NumberFormatInfo.InvariantInfo) * 4096.0),
WatchDisplayType.FixedPoint_16_16 => (uint)(double.Parse(value, NumberFormatInfo.InvariantInfo) * 65536.0),
WatchDisplayType.Float => BitConverter.ToUInt32(BitConverter.GetBytes(float.Parse(value, NumberFormatInfo.InvariantInfo)), 0),
WatchDisplayType.Float => ReinterpretAsU32(float.Parse(value, NumberFormatInfo.InvariantInfo)),
_ => 0
};
@ -127,12 +131,7 @@ namespace BizHawk.Client.Common
public string FormatValue(uint val)
{
string FormatFloat()
{
var bytes = BitConverter.GetBytes(val);
var _float = BitConverter.ToSingle(bytes, 0);
return _float.ToString(NumberFormatInfo.InvariantInfo);
}
=> Unsafe.As<uint, float>(ref val).ToString(NumberFormatInfo.InvariantInfo);
string FormatBinary()
{
var str = Convert.ToString(val, 2).PadLeft(32, '0');

View File

@ -1,5 +1,6 @@
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk.CustomControls;
@ -446,6 +447,8 @@ namespace BizHawk.Client.EmuHawk
public int? ToRawInt()
{
static int ReinterpretAsS32(float f)
=> Unsafe.As<float, int>(ref f);
try
{
return _type switch
@ -457,7 +460,7 @@ namespace BizHawk.Client.EmuHawk
WatchDisplayType.FixedPoint_12_4 => (int)(double.Parse(Text, NumberFormatInfo.InvariantInfo) * 16.0),
WatchDisplayType.FixedPoint_20_12 => (int)(double.Parse(Text, NumberFormatInfo.InvariantInfo) * 4096.0),
WatchDisplayType.FixedPoint_16_16 => (int)(double.Parse(Text, NumberFormatInfo.InvariantInfo) * 65536.0),
WatchDisplayType.Float => BitConverter.ToInt32(BitConverter.GetBytes(float.Parse(Text, NumberFormatInfo.InvariantInfo)), 0),
WatchDisplayType.Float => ReinterpretAsS32(float.Parse(Text, NumberFormatInfo.InvariantInfo)),
_ => int.Parse(Text)
};
}
@ -501,9 +504,8 @@ namespace BizHawk.Client.EmuHawk
Text = (val.Value / 65536.0).ToString("F5", NumberFormatInfo.InvariantInfo);
break;
case WatchDisplayType.Float:
var bytes = BitConverter.GetBytes(val.Value);
float _float = BitConverter.ToSingle(bytes, 0);
Text = _float.ToString("F6", NumberFormatInfo.InvariantInfo);
var i = val.Value;
Text = Unsafe.As<int, float>(ref i).ToString("F6", NumberFormatInfo.InvariantInfo);
break;
}
}