2016-07-28 18:13:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:23 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-07-28 18:13:49 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Swapping.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Helpers.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Byte-swapping methods.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-12-19 10:45:18 +00:00
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2016-07-28 18:13:49 +01:00
|
|
|
// ****************************************************************************/
|
2017-12-19 19:33:46 +00:00
|
|
|
|
2023-10-05 01:05:21 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2022-03-07 07:36:42 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
namespace Aaru.Helpers;
|
|
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Helper operations to work with swapping endians</summary>
|
2023-10-05 01:05:21 +01:00
|
|
|
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
2022-03-06 13:29:37 +00:00
|
|
|
public static class Swapping
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Gets the PDP endian equivalent of the given little endian unsigned integer</summary>
|
|
|
|
|
/// <param name="x">Little endian unsigned integer</param>
|
|
|
|
|
/// <returns>PDP unsigned integer</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-10-03 23:25:24 +01:00
|
|
|
public static uint PDPFromLittleEndian(uint x) => (x & 0xffff) << 16 | (x & 0xffff0000) >> 16;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Gets the PDP endian equivalent of the given big endian unsigned integer</summary>
|
|
|
|
|
/// <param name="x">Big endian unsigned integer</param>
|
|
|
|
|
/// <returns>PDP unsigned integer</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-10-03 23:25:24 +01:00
|
|
|
public static uint PDPFromBigEndian(uint x) => (x & 0xff00ff) << 8 | (x & 0xff00ff00) >> 8;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Swaps the endian of the specified unsigned short integer</summary>
|
|
|
|
|
/// <param name="x">Unsigned short integer</param>
|
|
|
|
|
/// <returns>Swapped unsigned short integer</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-10-03 23:25:24 +01:00
|
|
|
public static ushort Swap(ushort x) => (ushort)(x << 8 | x >> 8);
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Swaps the endian of the specified signed short integer</summary>
|
|
|
|
|
/// <param name="x">Signed short integer</param>
|
|
|
|
|
/// <returns>Swapped signed short integer</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-10-03 23:25:24 +01:00
|
|
|
public static short Swap(short x) => (short)(x << 8 | x >> 8 & 0xFF);
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Swaps the endian of the specified unsigned integer</summary>
|
|
|
|
|
/// <param name="x">Unsigned integer</param>
|
|
|
|
|
/// <returns>Swapped unsigned integer</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static uint Swap(uint x)
|
|
|
|
|
{
|
2023-10-03 23:25:24 +01:00
|
|
|
x = x << 8 & 0xFF00FF00 | x >> 8 & 0xFF00FF;
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2023-10-03 23:25:24 +01:00
|
|
|
return x << 16 | x >> 16;
|
2022-03-06 13:29:37 +00:00
|
|
|
}
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Swaps the endian of the specified signed integer</summary>
|
|
|
|
|
/// <param name="x">Signed integer</param>
|
|
|
|
|
/// <returns>Swapped signed integer</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int Swap(int x)
|
|
|
|
|
{
|
2023-10-03 23:25:24 +01:00
|
|
|
x = (int)(x << 8 & 0xFF00FF00 | (uint)x >> 8 & 0xFF00FF);
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2023-10-03 23:25:24 +01:00
|
|
|
return (int)((uint)x << 16 | (uint)x >> 16 & 0xFFFF);
|
2022-03-06 13:29:37 +00:00
|
|
|
}
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Swaps the endian of the specified unsigned long integer</summary>
|
|
|
|
|
/// <param name="x">Unsigned long integer</param>
|
|
|
|
|
/// <returns>Swapped unsigned long integer</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static ulong Swap(ulong x)
|
|
|
|
|
{
|
2023-10-03 23:25:24 +01:00
|
|
|
x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
|
|
|
|
|
x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
|
|
|
|
|
x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return x;
|
|
|
|
|
}
|
2017-07-15 01:29:26 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Swaps the endian of the specified signed long integer</summary>
|
|
|
|
|
/// <param name="x">Signed long integer</param>
|
|
|
|
|
/// <returns>Swapped signed long integer</returns>
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static long Swap(long x)
|
|
|
|
|
{
|
2023-10-03 23:25:24 +01:00
|
|
|
x = (x & 0x00000000FFFFFFFF) << 32 | (long)(((ulong)x & 0xFFFFFFFF00000000) >> 32);
|
|
|
|
|
x = (x & 0x0000FFFF0000FFFF) << 16 | (long)(((ulong)x & 0xFFFF0000FFFF0000) >> 16);
|
|
|
|
|
x = (x & 0x00FF00FF00FF00FF) << 8 | (long)(((ulong)x & 0xFF00FF00FF00FF00) >> 8);
|
2019-11-25 00:54:39 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
return x;
|
2015-10-05 18:58:13 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|