#if !NET6_0_OR_GREATER global using static PKHeX.Core.Buffers.Binary.Extra.BinaryPrimitives; using System; using System.Runtime.InteropServices; namespace PKHeX.Core.Buffers.Binary.Extra; internal static class BinaryPrimitives { public static float ReadSingleLittleEndian(ReadOnlySpan data) { var value = MemoryMarshal.Read(data); if (BitConverter.IsLittleEndian) return value; return ReverseEndianness(value); } public static double ReadDoubleLittleEndian(ReadOnlySpan data) { var value = MemoryMarshal.Read(data); if (BitConverter.IsLittleEndian) return value; return ReverseEndianness(value); } public static float ReadSingleBigEndian(Span data) { var value = MemoryMarshal.Read(data); if (!BitConverter.IsLittleEndian) return value; return ReverseEndianness(value); } public static double ReadDoubleBigEndian(Span data) { var value = MemoryMarshal.Read(data); if (!BitConverter.IsLittleEndian) return value; return ReverseEndianness(value); } public static void WriteSingleLittleEndian(Span data, float value) { if (!BitConverter.IsLittleEndian) value = ReverseEndianness(value); MemoryMarshal.Write(data, ref value); } public static void WriteDoubleLittleEndian(Span data, double value) { if (!BitConverter.IsLittleEndian) value = ReverseEndianness(value); MemoryMarshal.Write(data, ref value); } public static void WriteSingleBigEndian(Span data, float value) { if (BitConverter.IsLittleEndian) value = ReverseEndianness(value); MemoryMarshal.Write(data, ref value); } public static void WriteDoubleBigEndian(Span data, double value) { if (BitConverter.IsLittleEndian) value = ReverseEndianness(value); MemoryMarshal.Write(data, ref value); } public static float ReverseEndianness(float input) { Span span = stackalloc byte[4]; MemoryMarshal.Write(span, ref input); span.Reverse(); return MemoryMarshal.Read(span); } public static double ReverseEndianness(double input) { Span span = stackalloc byte[8]; MemoryMarshal.Write(span, ref input); span.Reverse(); return MemoryMarshal.Read(span); } } #endif