diff --git a/src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs b/src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs index 260e94bc92..b680102584 100644 --- a/src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs +++ b/src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs @@ -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(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(ref value), _isBigEndian); } public int ReadS8(long addr, string domain = null) => (sbyte) ReadUnsigned(addr, 1, domain); diff --git a/src/BizHawk.Client.Common/tools/Watch/DWordWatch.cs b/src/BizHawk.Client.Common/tools/Watch/DWordWatch.cs index 558a47e691..db40eac822 100644 --- a/src/BizHawk.Client.Common/tools/Watch/DWordWatch.cs +++ b/src/BizHawk.Client.Common/tools/Watch/DWordWatch.cs @@ -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 /// True if value successfully sets; otherwise, false public override bool Poke(string value) { + static uint ReinterpretAsU32(float f) + => Unsafe.As(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(ref val).ToString(NumberFormatInfo.InvariantInfo); string FormatBinary() { var str = Convert.ToString(val, 2).PadLeft(32, '0'); diff --git a/src/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs b/src/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs index 7cddd85f2b..47646a7057 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs @@ -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(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(ref i).ToString("F6", NumberFormatInfo.InvariantInfo); break; } }