Skip to content

Instantly share code, notes, and snippets.

@marek-safar
Created September 18, 2019 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marek-safar/19e0af908a30404a91b551ce7c182290 to your computer and use it in GitHub Desktop.
Save marek-safar/19e0af908a30404a91b551ce7c182290 to your computer and use it in GitHub Desktop.
[Serializable]
[DebuggerDisplay ("{v,nq}")]
public unsafe struct nfloat : IFormattable, IConvertible, IComparable, IComparable<nfloat>, IEquatable <nfloat>
{
internal nfloat (nfloat v) { this.v = v.v; }
public nfloat (Single v) { this.v = v; }
#if ARCH_32
public static readonly int Size = 4;
public static readonly nfloat MaxValue = Single.MaxValue;
public static readonly nfloat MinValue = Single.MinValue;
public static readonly nfloat Epsilon = (nfloat)Single.Epsilon;
public static readonly nfloat NaN = (nfloat)Single.NaN;
public static readonly nfloat NegativeInfinity = (nfloat)Single.NegativeInfinity;
public static readonly nfloat PositiveInfinity = (nfloat)Single.PositiveInfinity;
[DebuggerBrowsable (DebuggerBrowsableState.Never)]
internal Single v;
public nfloat (Double v) { this.v = (Single)v; }
#else
public static readonly int Size = 8;
public static readonly nfloat MaxValue = (nfloat) Double.MaxValue; // 64-bit only codepath
public static readonly nfloat MinValue = (nfloat) Double.MinValue; // 64-bit only codepath
public static readonly nfloat Epsilon = (nfloat)Double.Epsilon;
public static readonly nfloat NaN = (nfloat)Double.NaN;
public static readonly nfloat NegativeInfinity = (nfloat)Double.NegativeInfinity;
public static readonly nfloat PositiveInfinity = (nfloat)Double.PositiveInfinity;
[DebuggerBrowsable (DebuggerBrowsableState.Never)]
internal Double v;
public nfloat (Double v) { this.v = v; }
#endif
public static explicit operator nfloat (IntPtr v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat (*((float *)&v));
#else
return new nfloat (*((double *)&v));
#endif
}
public static explicit operator IntPtr (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return *((IntPtr *)&v.v);
#else
return *((IntPtr *)&v.v);
#endif
}
public static implicit operator nfloat (sbyte v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator sbyte (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (sbyte)v.v;
#else
return (sbyte)v.v;
#endif
}
public static implicit operator nfloat (byte v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator byte (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (byte)v.v;
#else
return (byte)v.v;
#endif
}
public static implicit operator nfloat (char v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator char (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (char)v.v;
#else
return (char)v.v;
#endif
}
public static implicit operator nfloat (short v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator short (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (short)v.v;
#else
return (short)v.v;
#endif
}
public static implicit operator nfloat (ushort v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator ushort (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (ushort)v.v;
#else
return (ushort)v.v;
#endif
}
public static implicit operator nfloat (int v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator int (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (int)v.v;
#else
return (int)v.v;
#endif
}
public static implicit operator nfloat (uint v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator uint (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (uint)v.v;
#else
return (uint)v.v;
#endif
}
public static implicit operator nfloat (long v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator long (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (long)v.v;
#else
return (long)v.v;
#endif
}
public static implicit operator nfloat (ulong v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator ulong (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (ulong)v.v;
#else
return (ulong)v.v;
#endif
}
public static implicit operator nfloat (float v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator float (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (float)v.v;
#else
return (float)v.v;
#endif
}
public static explicit operator nfloat (double v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static implicit operator double (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (double)v.v;
#else
return (double)v.v;
#endif
}
public static explicit operator nfloat (decimal v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return new nfloat ((float)v);
#else
return new nfloat ((double)v);
#endif
}
public static explicit operator decimal (nfloat v)
{
#if NINT_JIT_OPTIMIZED
throw new NotImplementedException ();
#elif ARCH_32
return (decimal)v.v;
#else
return (decimal)v.v;
#endif
}
#if NINT_JIT_OPTIMIZED
public static nfloat operator + (nfloat v) { throw new NotImplementedException (); }
public static nfloat operator - (nfloat v) { throw new NotImplementedException (); }
#else
public static nfloat operator + (nfloat v) { return new nfloat (+v.v); }
public static nfloat operator - (nfloat v) { return new nfloat (-v.v); }
#endif
#if NINT_JIT_OPTIMIZED
public static nfloat operator ++ (nfloat v) { throw new NotImplementedException (); }
public static nfloat operator -- (nfloat v) { throw new NotImplementedException (); }
#else
public static nfloat operator ++ (nfloat v) { return new nfloat (v.v + 1); }
public static nfloat operator -- (nfloat v) { return new nfloat (v.v - 1); }
#endif
#if NINT_JIT_OPTIMIZED
public static nfloat operator + (nfloat l, nfloat r) { throw new NotImplementedException (); }
public static nfloat operator - (nfloat l, nfloat r) { throw new NotImplementedException (); }
public static nfloat operator * (nfloat l, nfloat r) { throw new NotImplementedException (); }
public static nfloat operator / (nfloat l, nfloat r) { throw new NotImplementedException (); }
public static nfloat operator % (nfloat l, nfloat r) { throw new NotImplementedException (); }
#else
public static nfloat operator + (nfloat l, nfloat r) { return new nfloat (l.v + r.v); }
public static nfloat operator - (nfloat l, nfloat r) { return new nfloat (l.v - r.v); }
public static nfloat operator * (nfloat l, nfloat r) { return new nfloat (l.v * r.v); }
public static nfloat operator / (nfloat l, nfloat r) { return new nfloat (l.v / r.v); }
public static nfloat operator % (nfloat l, nfloat r) { return new nfloat (l.v % r.v); }
#endif
#if NINT_JIT_OPTIMIZED
public static bool operator == (nfloat l, nfloat r) { throw new NotImplementedException (); }
public static bool operator != (nfloat l, nfloat r) { throw new NotImplementedException (); }
public static bool operator < (nfloat l, nfloat r) { throw new NotImplementedException (); }
public static bool operator > (nfloat l, nfloat r) { throw new NotImplementedException (); }
public static bool operator <= (nfloat l, nfloat r) { throw new NotImplementedException (); }
public static bool operator >= (nfloat l, nfloat r) { throw new NotImplementedException (); }
#else
public static bool operator == (nfloat l, nfloat r) { return l.v == r.v; }
public static bool operator != (nfloat l, nfloat r) { return l.v != r.v; }
public static bool operator < (nfloat l, nfloat r) { return l.v < r.v; }
public static bool operator > (nfloat l, nfloat r) { return l.v > r.v; }
public static bool operator <= (nfloat l, nfloat r) { return l.v <= r.v; }
public static bool operator >= (nfloat l, nfloat r) { return l.v >= r.v; }
#endif
public int CompareTo (nfloat value) { return v.CompareTo (value.v); }
public int CompareTo (object value)
{
if (value is nfloat)
return v.CompareTo (((nfloat) value).v);
return v.CompareTo (value);
}
public bool Equals (nfloat obj) { return v.Equals (obj.v); }
public override bool Equals (object obj)
{
if (obj is nfloat)
return v.Equals (((nfloat) obj).v);
return v.Equals (obj);
}
public override int GetHashCode () { return v.GetHashCode (); }
#if ARCH_32
public static bool IsNaN (nfloat f) { return Single.IsNaN ((Single)f); }
public static bool IsInfinity (nfloat f) { return Single.IsInfinity ((Single)f); }
public static bool IsPositiveInfinity (nfloat f) { return Single.IsPositiveInfinity ((Single)f); }
public static bool IsNegativeInfinity (nfloat f) { return Single.IsNegativeInfinity ((Single)f); }
public static nfloat Parse (string s, IFormatProvider provider) { return (nfloat)Single.Parse (s, provider); }
public static nfloat Parse (string s, NumberStyles style) { return (nfloat)Single.Parse (s, style); }
public static nfloat Parse (string s) { return (nfloat)Single.Parse (s); }
public static nfloat Parse (string s, NumberStyles style, IFormatProvider provider) {
return (nfloat)Single.Parse (s, style, provider);
}
public static bool TryParse (string s, out nfloat result)
{
Single v;
var r = Single.TryParse (s, out v);
result = (nfloat)v;
return r;
}
public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out nfloat result)
{
Single v;
var r = Single.TryParse (s, style, provider, out v);
result = (nfloat)v;
return r;
}
#else
public static bool IsNaN (nfloat f) { return Double.IsNaN ((Double)f); }
public static bool IsInfinity (nfloat f) { return Double.IsInfinity ((Double)f); }
public static bool IsPositiveInfinity (nfloat f) { return Double.IsPositiveInfinity ((Double)f); }
public static bool IsNegativeInfinity (nfloat f) { return Double.IsNegativeInfinity ((Double)f); }
public static nfloat Parse (string s, IFormatProvider provider) { return (nfloat)Double.Parse (s, provider); }
public static nfloat Parse (string s, NumberStyles style) { return (nfloat)Double.Parse (s, style); }
public static nfloat Parse (string s) { return (nfloat)Double.Parse (s); }
public static nfloat Parse (string s, NumberStyles style, IFormatProvider provider) {
return (nfloat)Double.Parse (s, style, provider);
}
public static bool TryParse (string s, out nfloat result)
{
Double v;
var r = Double.TryParse (s, out v);
result = (nfloat)v;
return r;
}
public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out nfloat result)
{
Double v;
var r = Double.TryParse (s, style, provider, out v);
result = (nfloat)v;
return r;
}
#endif
public override string ToString () { return v.ToString (); }
public string ToString (IFormatProvider provider) { return v.ToString (provider); }
public string ToString (string format) { return v.ToString (format); }
public string ToString (string format, IFormatProvider provider) { return v.ToString (format, provider); }
public TypeCode GetTypeCode () { return v.GetTypeCode (); }
bool IConvertible.ToBoolean (IFormatProvider provider) { return ((IConvertible)v).ToBoolean (provider); }
byte IConvertible.ToByte (IFormatProvider provider) { return ((IConvertible)v).ToByte (provider); }
char IConvertible.ToChar (IFormatProvider provider) { return ((IConvertible)v).ToChar (provider); }
DateTime IConvertible.ToDateTime (IFormatProvider provider) { return ((IConvertible)v).ToDateTime (provider); }
decimal IConvertible.ToDecimal (IFormatProvider provider) { return ((IConvertible)v).ToDecimal (provider); }
double IConvertible.ToDouble (IFormatProvider provider) { return ((IConvertible)v).ToDouble (provider); }
short IConvertible.ToInt16 (IFormatProvider provider) { return ((IConvertible)v).ToInt16 (provider); }
int IConvertible.ToInt32 (IFormatProvider provider) { return ((IConvertible)v).ToInt32 (provider); }
long IConvertible.ToInt64 (IFormatProvider provider) { return ((IConvertible)v).ToInt64 (provider); }
sbyte IConvertible.ToSByte (IFormatProvider provider) { return ((IConvertible)v).ToSByte (provider); }
float IConvertible.ToSingle (IFormatProvider provider) { return ((IConvertible)v).ToSingle (provider); }
ushort IConvertible.ToUInt16 (IFormatProvider provider) { return ((IConvertible)v).ToUInt16 (provider); }
uint IConvertible.ToUInt32 (IFormatProvider provider) { return ((IConvertible)v).ToUInt32 (provider); }
ulong IConvertible.ToUInt64 (IFormatProvider provider) { return ((IConvertible)v).ToUInt64 (provider); }
object IConvertible.ToType (Type targetType, IFormatProvider provider) {
return ((IConvertible)v).ToType (targetType, provider);
}
public static void CopyArray (IntPtr source, nfloat [] destination, int startIndex, int length)
{
if (source == IntPtr.Zero)
throw new ArgumentNullException ("source");
if (destination == null)
throw new ArgumentNullException ("destination");
if (destination.Rank != 1)
throw new ArgumentException ("destination", "array is multi-dimensional");
if (startIndex < 0)
throw new ArgumentException ("startIndex", "must be >= 0");
if (length < 0)
throw new ArgumentException ("length", "must be >= 0");
if (startIndex + length > destination.Length)
throw new ArgumentException ("length", "startIndex + length > destination.Length");
for (int i = 0; i < length; i++)
destination [i + startIndex] = (nfloat)Marshal.ReadIntPtr (source, i * nfloat.Size);
}
public static void CopyArray (nfloat [] source, int startIndex, IntPtr destination, int length)
{
if (source == null)
throw new ArgumentNullException ("source");
if (destination == IntPtr.Zero)
throw new ArgumentNullException ("destination");
if (source.Rank != 1)
throw new ArgumentException ("source", "array is multi-dimensional");
if (startIndex < 0)
throw new ArgumentException ("startIndex", "must be >= 0");
if (length < 0)
throw new ArgumentException ("length", "must be >= 0");
if (startIndex + length > source.Length)
throw new ArgumentException ("length", "startIndex + length > source.Length");
for (int i = 0; i < length; i++)
Marshal.WriteIntPtr (destination, i * nfloat.Size, (IntPtr)source [i + startIndex]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment