mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 20:48:24 +01:00
380 lines
18 KiB
C#
380 lines
18 KiB
C#
// <auto-generated /> - edit EnumArithmetic.tt to modify
|
|
#nullable enable
|
|
|
|
using System.Numerics;
|
|
using static System.Runtime.CompilerServices.Unsafe;
|
|
|
|
// csharpier-ignore-start
|
|
namespace Brutal.Enums
|
|
{
|
|
/// Extension operators for enums, allowing for generic arithmetic
|
|
public static class EnumOperators
|
|
{
|
|
extension<T>(T) where T : unmanaged, Enum
|
|
{
|
|
/// TEnum.Equals() boxes, avoid it like the plague.
|
|
/// And unfortunately, we can't replace the builtin Equals through an extension.
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe bool operator ==(T a, T b)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<T, byte>(a) == BitCast<T, byte>(b);
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<T, ushort>(a) == BitCast<T, ushort>(b);
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<T, uint>(a) == BitCast<T, uint>(b);
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<T, ulong>(a) == BitCast<T, ulong>(b);
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe bool operator !=(T a, T b)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<T, byte>(a) != BitCast<T, byte>(b);
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<T, ushort>(a) != BitCast<T, ushort>(b);
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<T, uint>(a) != BitCast<T, uint>(b);
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<T, ulong>(a) != BitCast<T, ulong>(b);
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe bool operator <(T a, T b)
|
|
{
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(byte))
|
|
return BitCast<T, byte>(a) < BitCast<T, byte>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(ushort))
|
|
return BitCast<T, ushort>(a) < BitCast<T, ushort>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(uint))
|
|
return BitCast<T, uint>(a) < BitCast<T, uint>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(ulong))
|
|
return BitCast<T, ulong>(a) < BitCast<T, ulong>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(sbyte))
|
|
return BitCast<T, sbyte>(a) < BitCast<T, sbyte>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(short))
|
|
return BitCast<T, short>(a) < BitCast<T, short>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
|
|
return BitCast<T, int>(a) < BitCast<T, int>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(long))
|
|
return BitCast<T, long>(a) < BitCast<T, long>(b);
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe bool operator >(T a, T b)
|
|
{
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(byte))
|
|
return BitCast<T, byte>(a) > BitCast<T, byte>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(ushort))
|
|
return BitCast<T, ushort>(a) > BitCast<T, ushort>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(uint))
|
|
return BitCast<T, uint>(a) > BitCast<T, uint>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(ulong))
|
|
return BitCast<T, ulong>(a) > BitCast<T, ulong>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(sbyte))
|
|
return BitCast<T, sbyte>(a) > BitCast<T, sbyte>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(short))
|
|
return BitCast<T, short>(a) > BitCast<T, short>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
|
|
return BitCast<T, int>(a) > BitCast<T, int>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(long))
|
|
return BitCast<T, long>(a) > BitCast<T, long>(b);
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe bool operator <=(T a, T b)
|
|
{
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(byte))
|
|
return BitCast<T, byte>(a) <= BitCast<T, byte>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(ushort))
|
|
return BitCast<T, ushort>(a) <= BitCast<T, ushort>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(uint))
|
|
return BitCast<T, uint>(a) <= BitCast<T, uint>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(ulong))
|
|
return BitCast<T, ulong>(a) <= BitCast<T, ulong>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(sbyte))
|
|
return BitCast<T, sbyte>(a) <= BitCast<T, sbyte>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(short))
|
|
return BitCast<T, short>(a) <= BitCast<T, short>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
|
|
return BitCast<T, int>(a) <= BitCast<T, int>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(long))
|
|
return BitCast<T, long>(a) <= BitCast<T, long>(b);
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe bool operator >=(T a, T b)
|
|
{
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(byte))
|
|
return BitCast<T, byte>(a) >= BitCast<T, byte>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(ushort))
|
|
return BitCast<T, ushort>(a) >= BitCast<T, ushort>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(uint))
|
|
return BitCast<T, uint>(a) >= BitCast<T, uint>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(ulong))
|
|
return BitCast<T, ulong>(a) >= BitCast<T, ulong>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(sbyte))
|
|
return BitCast<T, sbyte>(a) >= BitCast<T, sbyte>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(short))
|
|
return BitCast<T, short>(a) >= BitCast<T, short>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
|
|
return BitCast<T, int>(a) >= BitCast<T, int>(b);
|
|
if (typeof(T).GetEnumUnderlyingType() == typeof(long))
|
|
return BitCast<T, long>(a) >= BitCast<T, long>(b);
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe T operator |(T a, T b)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)(BitCast<T, byte>(a) | BitCast<T, byte>(b)));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)(BitCast<T, ushort>(a) | BitCast<T, ushort>(b)));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)(BitCast<T, uint>(a) | BitCast<T, uint>(b)));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)(BitCast<T, ulong>(a) | BitCast<T, ulong>(b)));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe T operator &(T a, T b)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)(BitCast<T, byte>(a) & BitCast<T, byte>(b)));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)(BitCast<T, ushort>(a) & BitCast<T, ushort>(b)));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)(BitCast<T, uint>(a) & BitCast<T, uint>(b)));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)(BitCast<T, ulong>(a) & BitCast<T, ulong>(b)));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe T operator ^(T a, T b)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)(BitCast<T, byte>(a) ^ BitCast<T, byte>(b)));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)(BitCast<T, ushort>(a) ^ BitCast<T, ushort>(b)));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)(BitCast<T, uint>(a) ^ BitCast<T, uint>(b)));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)(BitCast<T, ulong>(a) ^ BitCast<T, ulong>(b)));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe T operator ++(T a)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)(BitCast<T, byte>(a) + 1));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)(BitCast<T, ushort>(a) + 1));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)(BitCast<T, uint>(a) + 1));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)(BitCast<T, ulong>(a) + 1));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe T operator --(T a)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)(BitCast<T, byte>(a) - 1));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)(BitCast<T, ushort>(a) - 1));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)(BitCast<T, uint>(a) - 1));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)(BitCast<T, ulong>(a) - 1));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe T operator ~(T a)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)~BitCast<T, byte>(a));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)~BitCast<T, ushort>(a));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)~BitCast<T, uint>(a));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)~BitCast<T, ulong>(a));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
/// Convert from int to enum, unchecked: value will be truncated if out of underlying bounds
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
|
|
public static unsafe T CreateTruncating(int value)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)value);
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)value);
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)value);
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)value);
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
}
|
|
|
|
extension<T>(T value) where T : unmanaged, Enum
|
|
{
|
|
/// Convert this enum to int, unchecked: value will be truncated if out of int bounds
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
|
|
public unsafe int ToIntTruncating()
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return (int)BitCast<T, byte>(value);
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return (int)BitCast<T, ushort>(value);
|
|
if (sizeof(T) == sizeof(uint))
|
|
return (int)BitCast<T, uint>(value);
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return (int)BitCast<T, ulong>(value);
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
/// Left-shift this value: <c>1 << value</c>, ie return a mask containing this value.
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
|
|
public unsafe T ShiftLeft()
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)(1u << (byte)BitCast<T, byte>(value)));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)(1u << (byte)BitCast<T, ushort>(value)));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)(1u << (byte)BitCast<T, uint>(value)));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)(1ul << (byte)BitCast<T, ulong>(value)));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
/// Does the mask have only a single bit set?
|
|
/// <i>(Mask / bit-field / <c>[Flags]</c> enums only.)</i>
|
|
public unsafe bool IsPow2
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitOperations.IsPow2(BitCast<T, byte>(value));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitOperations.IsPow2(BitCast<T, ushort>(value));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitOperations.IsPow2(BitCast<T, uint>(value));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitOperations.IsPow2(BitCast<T, ulong>(value));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
}
|
|
|
|
/// If the mask has only a single bit set, get its index.
|
|
/// <i>(Mask / bit-field / <c>[Flags]</c> enums only.)</i>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public unsafe bool TryGetBitIndex(out byte index)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitOperations.TryGetBitIndex(BitCast<T, byte>(value), out index);
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitOperations.TryGetBitIndex(BitCast<T, ushort>(value), out index);
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitOperations.TryGetBitIndex(BitCast<T, uint>(value), out index);
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitOperations.TryGetBitIndex(BitCast<T, ulong>(value), out index);
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace Brutal.Enums.AdditionOperators
|
|
{
|
|
/// Extension operators for enums, allowing for generic arithmetic
|
|
public static class EnumAdditionOperators
|
|
{
|
|
extension<T>(T) where T : unmanaged, Enum
|
|
{
|
|
/// Binary addition operators, for writing generic code.
|
|
/// <br/>
|
|
/// Do not use in non-generic code, as enums normally can only operate with their underlying type, not themselves,
|
|
/// and we don't want to inadvertently inject that functionality in the language.
|
|
/// <br/>
|
|
/// So eg a byte enum will define TEnum + byte and byte + TEnum operators, but not a TEnum + TEnum operator.
|
|
/// We cannot define a TEnum + TUnderlying operator, as we have no way of generically accessing TUnderlying.
|
|
/// <br/>
|
|
/// We could define e.g. TEnum + long, but that would be misleading as long might not be the correct size.
|
|
/// TEnum + TEnum actually _is_ the correct size, if not the correct type.
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe T operator +(T a, T b)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)(BitCast<T, byte>(a) + BitCast<T, byte>(b)));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)(BitCast<T, ushort>(a) + BitCast<T, ushort>(b)));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)(BitCast<T, uint>(a) + BitCast<T, uint>(b)));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)(BitCast<T, ulong>(a) + BitCast<T, ulong>(b)));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
/// Binary addition operators, for writing generic code.
|
|
/// <br/>
|
|
/// Do not use in non-generic code, as enums normally can only operate with their underlying type, not themselves,
|
|
/// and we don't want to inadvertently inject that functionality in the language.
|
|
/// <br/>
|
|
/// So eg a byte enum will define TEnum + byte and byte + TEnum operators, but not a TEnum + TEnum operator.
|
|
/// We cannot define a TEnum + TUnderlying operator, as we have no way of generically accessing TUnderlying.
|
|
/// <br/>
|
|
/// We could define e.g. TEnum + long, but that would be misleading as long might not be the correct size.
|
|
/// TEnum + TEnum actually _is_ the correct size, if not the correct type.
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static unsafe T operator -(T a, T b)
|
|
{
|
|
if (sizeof(T) == sizeof(byte))
|
|
return BitCast<byte, T>((byte)(BitCast<T, byte>(a) - BitCast<T, byte>(b)));
|
|
if (sizeof(T) == sizeof(ushort))
|
|
return BitCast<ushort, T>((ushort)(BitCast<T, ushort>(a) - BitCast<T, ushort>(b)));
|
|
if (sizeof(T) == sizeof(uint))
|
|
return BitCast<uint, T>((uint)(BitCast<T, uint>(a) - BitCast<T, uint>(b)));
|
|
if (sizeof(T) == sizeof(ulong))
|
|
return BitCast<ulong, T>((ulong)(BitCast<T, ulong>(a) - BitCast<T, ulong>(b)));
|
|
|
|
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
// csharpier-ignore-end
|