using System;
using System.Runtime.InteropServices;
namespace Math.Gmp.Native
{
///
/// Represents a part of a multiple precision number.
///
///
///
/// A limb means the part of a multi-precision number that fits in a single machine word.
/// (We chose this word because a limb of the human body is analogous to a digit, only larger,
/// and containing several digits.) Normally a limb is 32 or 64 bits.
///
///
///
///
///
public struct mp_limb_t
{
///
/// The value.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
public ulong Value;
///
/// Creates a new , and sets its .
///
/// The value of the new .
public mp_limb_t(ulong value)
{
this.Value = value;
}
///
/// Converts a value to an value.
///
/// A value.
/// An value.
public static implicit operator mp_limb_t(byte value)
{
return new mp_limb_t(value);
}
///
/// Converts a value to an value.
///
/// A value.
/// An value.
public static explicit operator mp_limb_t(sbyte value)
{
if (value < 0) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the mp_limb_t data type.", value));
return new mp_limb_t((ulong)value);
}
///
/// Converts a value to an value.
///
/// A value.
/// An value.
public static implicit operator mp_limb_t(ushort value)
{
return new mp_limb_t(value);
}
///
/// Converts an value to an value.
///
/// An value.
/// An value.
public static explicit operator mp_limb_t(short value)
{
if (value < 0) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the mp_limb_t data type.", value));
return new mp_limb_t((ulong)value);
}
///
/// Converts a value to an value.
///
/// A value.
/// An value.
public static implicit operator mp_limb_t(uint value)
{
return new mp_limb_t(value);
}
///
/// Converts an value to an value.
///
/// An value.
/// An value.
public static explicit operator mp_limb_t(int value)
{
if (value < 0) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the mp_limb_t data type.", value));
return new mp_limb_t((ulong)(uint)value);
}
///
/// Converts a value to an value.
///
/// A value.
/// An value.
public static implicit operator mp_limb_t(ulong value)
{
return new mp_limb_t(value);
}
///
/// Converts an value to an value.
///
/// An value.
/// An value.
public static explicit operator mp_limb_t(long value)
{
if (value < 0) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the mp_limb_t data type.", value));
return new mp_limb_t((ulong)value);
}
///
/// Converts a value to a value.
///
/// An value.
/// A value.
public static explicit operator byte(mp_limb_t value)
{
if (value.Value > byte.MaxValue) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the Byte data type.", value));
return (byte)value.Value;
}
///
/// Converts a value to an value.
///
/// An value.
/// An value.
public static explicit operator sbyte(mp_limb_t value)
{
if (value.Value > (ulong)sbyte.MaxValue) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the SByte data type.", value));
return (sbyte)value.Value;
}
///
/// Converts a value to a value.
///
/// An value.
/// A value.
public static explicit operator ushort(mp_limb_t value)
{
if (value.Value > ushort.MaxValue) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the UInt16 data type.", value));
return (ushort)value.Value;
}
///
/// Converts a value to an value.
///
/// An value.
/// An value.
public static explicit operator short(mp_limb_t value)
{
if (value.Value > (ulong)short.MaxValue) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the Int16 data type.", value));
return (short)value.Value;
}
///
/// Converts a value to a value.
///
/// An value.
/// A value.
public static explicit operator uint(mp_limb_t value)
{
if (value.Value > uint.MaxValue) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the UInt32 data type.", value));
return (uint)value.Value;
}
///
/// Converts a value to an value.
///
/// An value.
/// An value.
public static explicit operator int(mp_limb_t value)
{
if (value.Value > int.MaxValue) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the Int32 data type.", value));
return (int)value.Value;
}
///
/// Converts a value to a value.
///
/// An value.
/// A value.
public static implicit operator ulong(mp_limb_t value)
{
return value.Value;
}
///
/// Converts a value to an value.
///
/// An value.
/// An value.
public static explicit operator long(mp_limb_t value)
{
if (value.Value > long.MaxValue) throw new System.OverflowException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' is out of range of the Int64 data type.", value));
return (long)value.Value;
}
///
/// Gets the string representation of the .
///
/// The string representation of the .
public override string ToString()
{
return "0x" + Value.ToString(gmp_lib.mp_bytes_per_limb == 4 ? "x8" : "x16", System.Globalization.CultureInfo.InvariantCulture);
}
///
/// Returns a value indicating whether this instance is equal to a specified object.
///
/// An object to compare with this instance.
/// True if is an instance of and equals the value of this instance; otherwise, False.
public override bool Equals(object obj)
{
if (!(obj is mp_limb_t))
return false;
return Equals((mp_limb_t)obj);
}
///
/// Returns a value indicating whether this instance is equal to a specified value.
///
/// A value to compare to this instance.
/// True if has the same value as this instance; otherwise, False.
public bool Equals(mp_limb_t other)
{
return Value == other.Value;
}
///
/// Returns the hash code for this instance.
///
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
return Value.GetHashCode();
}
///
/// Gets a value that indicates whether the two argument values are equal.
///
/// A value.
/// A value.
/// True if the two values are equal, and False otherwise.
public static bool operator ==(mp_limb_t value1, mp_limb_t value2)
{
return value1.Equals(value2);
}
///
/// Gets a value that indicates whether the two argument values are different.
///
/// A value.
/// A value.
/// True if the two values are different, and False otherwise.
public static bool operator !=(mp_limb_t value1, mp_limb_t value2)
{
return !value1.Equals(value2);
}
}
}