//******************************************************************************************************
// Word.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 04/10/2009 - James R. Carroll
// Generated original version of source code.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System.Runtime.CompilerServices;
namespace GSF
{
///
/// Represents functions and extensions related to 16-bit words, 32-bit double-words and 64-bit quad-words.
///
public static class Word
{
///
/// Aligns word value on a 16-bit boundary.
///
/// Word value to align.
/// Word value aligned to next 16-bit boundary.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short AlignWord(this short word)
{
return (short)(word + 1 - (word - 1) % 2);
}
///
/// Aligns word value on a 16-bit boundary.
///
/// Word value to align.
/// Word value aligned to next 16-bit boundary.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort AlignWord(this ushort word)
{
return (ushort)(word + 1 - (word - 1) % 2);
}
///
/// Aligns double-word value on a 32-bit boundary.
///
/// Double-word value to align.
/// Double-word value aligned to next 32-bit boundary.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int AlignDoubleWord(this int doubleWord)
{
return doubleWord + 3 - (doubleWord - 1) % 4;
}
///
/// Aligns double-word value on a 32-bit boundary.
///
/// Double-word value to align.
/// Double-word value aligned to next 32-bit boundary.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint AlignDoubleWord(this uint doubleWord)
{
return doubleWord + 3 - (doubleWord - 1) % 4;
}
///
/// Aligns quad-word value on a 64-bit boundary.
///
/// Quad-word value to align.
/// Quad-word value aligned to next 64-bit boundary.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long AlignQuadWord(this long quadWord)
{
return quadWord + 7 - (quadWord - 1) % 8;
}
///
/// Aligns quad-word value on a 64-bit boundary.
///
/// Quad-word value to align.
/// Quad-word value aligned to next 64-bit boundary.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong AlignQuadWord(this ulong quadWord)
{
return quadWord + 7 - (quadWord - 1) % 8;
}
///
/// Returns the high-nibble (high 4-bits) from a byte.
///
/// Byte value.
/// The high-nibble of the specified byte value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte HighNibble(this byte value)
{
return (byte)((value & (byte)0xF0) >> 4);
}
///
/// Returns the high-byte from an unsigned word (UInt16).
///
/// 2-byte, 16-bit unsigned integer value.
/// The high-order byte of the specified 16-bit unsigned integer value.
///
/// On little-endian architectures (e.g., Intel platforms), this will be the byte value whose in-memory representation
/// is the same as the right-most, most-significant-byte of the integer value.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte HighByte(this ushort word)
{
return (byte)((word & (ushort)0xFF00) >> 8);
}
///
/// Returns the unsigned high-word (UInt16) from an unsigned double-word (UInt32).
///
/// 4-byte, 32-bit unsigned integer value.
/// The unsigned high-order word of the specified 32-bit unsigned integer value.
///
/// On little-endian architectures (e.g., Intel platforms), this will be the word value
/// whose in-memory representation is the same as the right-most, most-significant-word
/// of the integer value.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort HighWord(this uint doubleWord)
{
return (ushort)((doubleWord & 0xFFFF0000U) >> 16);
}
///
/// Returns the unsigned high-double-word (UInt32) from an unsigned quad-word (UInt64).
///
/// 8-byte, 64-bit unsigned integer value.
/// The high-order double-word of the specified 64-bit unsigned integer value.
///
/// On little-endian architectures (e.g., Intel platforms), this will be the word value
/// whose in-memory representation is the same as the right-most, most-significant-word
/// of the integer value.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint HighDoubleWord(this ulong quadWord)
{
return (uint)((quadWord & 0xFFFFFFFF00000000UL) >> 32);
}
///
/// Returns the low-nibble (low 4-bits) from a byte.
///
/// Byte value.
/// The low-nibble of the specified byte value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte LowNibble(this byte value)
{
return (byte)(value & (byte)0x0F);
}
///
/// Returns the low-byte from an unsigned word (UInt16).
///
/// 2-byte, 16-bit unsigned integer value.
/// The low-order byte of the specified 16-bit unsigned integer value.
///
/// On little-endian architectures (e.g., Intel platforms), this will be the byte value
/// whose in-memory representation is the same as the left-most, least-significant-byte
/// of the integer value.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte LowByte(this ushort word)
{
return (byte)(word & (ushort)0x00FF);
}
///
/// Returns the unsigned low-word (UInt16) from an unsigned double-word (UInt32).
///
/// 4-byte, 32-bit unsigned integer value.
/// The unsigned low-order word of the specified 32-bit unsigned integer value.
///
/// On little-endian architectures (e.g., Intel platforms), this will be the word value
/// whose in-memory representation is the same as the left-most, least-significant-word
/// of the integer value.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort LowWord(this uint doubleWord)
{
return (ushort)(doubleWord & 0x0000FFFFU);
}
///
/// Returns the unsigned low-double-word (UInt32) from an unsigned quad-word (UInt64).
///
/// 8-byte, 64-bit unsigned integer value.
/// The low-order double-word of the specified 64-bit unsigned integer value.
///
/// On little-endian architectures (e.g., Intel platforms), this will be the word value
/// whose in-memory representation is the same as the left-most, least-significant-word
/// of the integer value.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint LowDoubleWord(this ulong quadWord)
{
return (uint)(quadWord & 0x00000000FFFFFFFFUL);
}
///
/// Makes an unsigned word (UInt16) from two bytes.
///
/// High byte.
/// Low byte.
/// An unsigned 16-bit word made from the two specified bytes.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort MakeWord(byte high, byte low)
{
return (ushort)(low + ((ushort)high << 8));
}
///
/// Makes an unsigned double-word (UInt32) from two unsigned words (UInt16).
///
/// High word.
/// Low word.
/// An unsigned 32-bit double-word made from the two specified unsigned 16-bit words.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint MakeDoubleWord(ushort high, ushort low)
{
return (uint)(low + ((uint)high << 16));
}
///
/// Makes an unsigned quad-word (UInt64) from two unsigned double-words (UInt32).
///
/// High double-word.
/// Low double-word.
/// An unsigned 64-bit quad-word made from the two specified unsigned 32-bit double-words.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong MakeQuadWord(uint high, uint low)
{
return (ulong)(low + ((ulong)high << 32));
}
}
}