// - 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) 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(a) == BitCast(b);
if (sizeof(T) == sizeof(ushort))
return BitCast(a) == BitCast(b);
if (sizeof(T) == sizeof(uint))
return BitCast(a) == BitCast(b);
if (sizeof(T) == sizeof(ulong))
return BitCast(a) == BitCast(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(a) != BitCast(b);
if (sizeof(T) == sizeof(ushort))
return BitCast(a) != BitCast(b);
if (sizeof(T) == sizeof(uint))
return BitCast(a) != BitCast(b);
if (sizeof(T) == sizeof(ulong))
return BitCast(a) != BitCast(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(a) < BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(ushort))
return BitCast(a) < BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(uint))
return BitCast(a) < BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(ulong))
return BitCast(a) < BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(sbyte))
return BitCast(a) < BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(short))
return BitCast(a) < BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
return BitCast(a) < BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(long))
return BitCast(a) < BitCast(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(a) > BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(ushort))
return BitCast(a) > BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(uint))
return BitCast(a) > BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(ulong))
return BitCast(a) > BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(sbyte))
return BitCast(a) > BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(short))
return BitCast(a) > BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
return BitCast(a) > BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(long))
return BitCast(a) > BitCast(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(a) <= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(ushort))
return BitCast(a) <= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(uint))
return BitCast(a) <= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(ulong))
return BitCast(a) <= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(sbyte))
return BitCast(a) <= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(short))
return BitCast(a) <= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
return BitCast(a) <= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(long))
return BitCast(a) <= BitCast(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(a) >= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(ushort))
return BitCast(a) >= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(uint))
return BitCast(a) >= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(ulong))
return BitCast(a) >= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(sbyte))
return BitCast(a) >= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(short))
return BitCast(a) >= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
return BitCast(a) >= BitCast(b);
if (typeof(T).GetEnumUnderlyingType() == typeof(long))
return BitCast(a) >= BitCast(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)(BitCast(a) | BitCast(b)));
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)(BitCast(a) | BitCast(b)));
if (sizeof(T) == sizeof(uint))
return BitCast((uint)(BitCast(a) | BitCast(b)));
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)(BitCast(a) | BitCast(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)(BitCast(a) & BitCast(b)));
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)(BitCast(a) & BitCast(b)));
if (sizeof(T) == sizeof(uint))
return BitCast((uint)(BitCast(a) & BitCast(b)));
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)(BitCast(a) & BitCast(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)(BitCast(a) ^ BitCast(b)));
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)(BitCast(a) ^ BitCast(b)));
if (sizeof(T) == sizeof(uint))
return BitCast((uint)(BitCast(a) ^ BitCast(b)));
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)(BitCast(a) ^ BitCast(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)(BitCast(a) + 1));
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)(BitCast(a) + 1));
if (sizeof(T) == sizeof(uint))
return BitCast((uint)(BitCast(a) + 1));
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)(BitCast(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)(BitCast(a) - 1));
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)(BitCast(a) - 1));
if (sizeof(T) == sizeof(uint))
return BitCast((uint)(BitCast(a) - 1));
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)(BitCast(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)~BitCast(a));
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)~BitCast(a));
if (sizeof(T) == sizeof(uint))
return BitCast((uint)~BitCast(a));
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)~BitCast(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)value);
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)value);
if (sizeof(T) == sizeof(uint))
return BitCast((uint)value);
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)value);
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
}
}
extension(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(value);
if (sizeof(T) == sizeof(ushort))
return (int)BitCast(value);
if (sizeof(T) == sizeof(uint))
return (int)BitCast(value);
if (sizeof(T) == sizeof(ulong))
return (int)BitCast(value);
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
}
/// Left-shift this value: 1 << value, ie return a mask containing this value.
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public unsafe T ShiftLeft()
{
if (sizeof(T) == sizeof(byte))
return BitCast((byte)(1u << (byte)BitCast(value)));
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)(1u << (byte)BitCast(value)));
if (sizeof(T) == sizeof(uint))
return BitCast((uint)(1u << (byte)BitCast(value)));
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)(1ul << (byte)BitCast(value)));
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
}
/// Does the mask have only a single bit set?
/// (Mask / bit-field / [Flags] enums only.)
public unsafe bool IsPow2
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (sizeof(T) == sizeof(byte))
return BitOperations.IsPow2(BitCast(value));
if (sizeof(T) == sizeof(ushort))
return BitOperations.IsPow2(BitCast(value));
if (sizeof(T) == sizeof(uint))
return BitOperations.IsPow2(BitCast(value));
if (sizeof(T) == sizeof(ulong))
return BitOperations.IsPow2(BitCast(value));
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
}
}
/// If the mask has only a single bit set, get its index.
/// (Mask / bit-field / [Flags] enums only.)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe bool TryGetBitIndex(out byte index)
{
if (sizeof(T) == sizeof(byte))
return BitOperations.TryGetBitIndex(BitCast(value), out index);
if (sizeof(T) == sizeof(ushort))
return BitOperations.TryGetBitIndex(BitCast(value), out index);
if (sizeof(T) == sizeof(uint))
return BitOperations.TryGetBitIndex(BitCast(value), out index);
if (sizeof(T) == sizeof(ulong))
return BitOperations.TryGetBitIndex(BitCast(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) where T : unmanaged, Enum
{
/// Binary addition operators, for writing generic code.
///
/// 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.
///
/// 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.
///
/// 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)(BitCast(a) + BitCast(b)));
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)(BitCast(a) + BitCast(b)));
if (sizeof(T) == sizeof(uint))
return BitCast((uint)(BitCast(a) + BitCast(b)));
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)(BitCast(a) + BitCast(b)));
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
}
/// Binary addition operators, for writing generic code.
///
/// 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.
///
/// 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.
///
/// 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)(BitCast(a) - BitCast(b)));
if (sizeof(T) == sizeof(ushort))
return BitCast((ushort)(BitCast(a) - BitCast(b)));
if (sizeof(T) == sizeof(uint))
return BitCast((uint)(BitCast(a) - BitCast(b)));
if (sizeof(T) == sizeof(ulong))
return BitCast((ulong)(BitCast(a) - BitCast(b)));
throw new NotSupportedException($"unsupported type {typeof(T)} of size {sizeof(T)}");
}
}
}
}
// csharpier-ignore-end