// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : Swapping.cs // Author(s) : Natalia Portillo // // 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 . // // ---------------------------------------------------------------------------- // Copyright © 2011-2025 Natalia Portillo // ****************************************************************************/ using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace Aaru.Helpers; /// Helper operations to work with swapping endians [SuppressMessage("ReSharper", "UnusedMember.Global")] public static class Swapping { /// Gets the PDP endian equivalent of the given little endian unsigned integer /// Little endian unsigned integer /// PDP unsigned integer [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint PDPFromLittleEndian(uint x) => (x & 0xffff) << 16 | (x & 0xffff0000) >> 16; /// Gets the PDP endian equivalent of the given big endian unsigned integer /// Big endian unsigned integer /// PDP unsigned integer [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint PDPFromBigEndian(uint x) => (x & 0xff00ff) << 8 | (x & 0xff00ff00) >> 8; /// Swaps the endian of the specified unsigned short integer /// Unsigned short integer /// Swapped unsigned short integer [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ushort Swap(ushort x) => (ushort)(x << 8 | x >> 8); /// Swaps the endian of the specified signed short integer /// Signed short integer /// Swapped signed short integer [MethodImpl(MethodImplOptions.AggressiveInlining)] public static short Swap(short x) => (short)(x << 8 | x >> 8 & 0xFF); /// Swaps the endian of the specified unsigned integer /// Unsigned integer /// Swapped unsigned integer [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint Swap(uint x) { x = x << 8 & 0xFF00FF00 | x >> 8 & 0xFF00FF; return x << 16 | x >> 16; } /// Swaps the endian of the specified signed integer /// Signed integer /// Swapped signed integer [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Swap(int x) { x = (int)(x << 8 & 0xFF00FF00 | (uint)x >> 8 & 0xFF00FF); return (int)((uint)x << 16 | (uint)x >> 16 & 0xFFFF); } /// Swaps the endian of the specified unsigned long integer /// Unsigned long integer /// Swapped unsigned long integer [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ulong Swap(ulong x) { x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32; x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16; x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8; return x; } /// Swaps the endian of the specified signed long integer /// Signed long integer /// Swapped signed long integer [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long Swap(long x) { 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); return x; } }