Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Address PR feedback
  • Loading branch information
stephentoub committed Jan 29, 2024
commit 36d15c69d7ee4ff593d8cc051ae7c209683d64b7
Original file line number Diff line number Diff line change
Expand Up @@ -14902,37 +14902,9 @@ internal readonly struct RotateLeftOperator<T>(int amount) : IStatefulUnaryOpera
public static bool Vectorizable => true;

public T Invoke(T x) => T.RotateLeft(x, _amount);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector128<T> Invoke(Vector128<T> x)
{
if (sizeof(T) == 1) return (Vector128.ShiftLeft(x.AsByte(), _amount & 7) | (Vector128.ShiftRightLogical(x.AsByte(), (8 - _amount) & 7))).As<byte, T>();
if (sizeof(T) == 2) return (Vector128.ShiftLeft(x.AsUInt16(), _amount & 15) | (Vector128.ShiftRightLogical(x.AsUInt16(), (16 - _amount) & 15))).As<ushort, T>();
if (sizeof(T) == 4) return (Vector128.ShiftLeft(x.AsUInt32(), _amount) | (Vector128.ShiftRightLogical(x.AsUInt32(), 32 - _amount))).As<uint, T>();
if (sizeof(T) == 8) return (Vector128.ShiftLeft(x.AsUInt64(), _amount) | (Vector128.ShiftRightLogical(x.AsUInt64(), 64 - _amount))).As<ulong, T>();
throw new NotSupportedException();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector256<T> Invoke(Vector256<T> x)
{
if (sizeof(T) == 1) return (Vector256.ShiftLeft(x.AsByte(), _amount & 7) | (Vector256.ShiftRightLogical(x.AsByte(), (8 - _amount) & 7))).As<byte, T>();
if (sizeof(T) == 2) return (Vector256.ShiftLeft(x.AsUInt16(), _amount & 15) | (Vector256.ShiftRightLogical(x.AsUInt16(), (16 - _amount) & 15))).As<ushort, T>();
if (sizeof(T) == 4) return (Vector256.ShiftLeft(x.AsUInt32(), _amount) | (Vector256.ShiftRightLogical(x.AsUInt32(), 32 - _amount))).As<uint, T>();
if (sizeof(T) == 8) return (Vector256.ShiftLeft(x.AsUInt64(), _amount) | (Vector256.ShiftRightLogical(x.AsUInt64(), 64 - _amount))).As<ulong, T>();
throw new NotSupportedException();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector512<T> Invoke(Vector512<T> x)
{
if (sizeof(T) == 1) return (Vector512.ShiftLeft(x.AsByte(), _amount & 7) | (Vector512.ShiftRightLogical(x.AsByte(), (8 - _amount) & 7))).As<byte, T>();
if (sizeof(T) == 2) return (Vector512.ShiftLeft(x.AsUInt16(), _amount & 15) | (Vector512.ShiftRightLogical(x.AsUInt16(), (16 - _amount) & 15))).As<ushort, T>();
if (sizeof(T) == 4) return (Vector512.ShiftLeft(x.AsUInt32(), _amount) | (Vector512.ShiftRightLogical(x.AsUInt32(), 32 - _amount))).As<uint, T>();
if (sizeof(T) == 8) return (Vector512.ShiftLeft(x.AsUInt64(), _amount) | (Vector512.ShiftRightLogical(x.AsUInt64(), 64 - _amount))).As<ulong, T>();

throw new NotSupportedException();
}
public Vector128<T> Invoke(Vector128<T> x) => (x << _amount) | (x >>> ((sizeof(T) * 8) - _amount));
public Vector256<T> Invoke(Vector256<T> x) => (x << _amount) | (x >>> ((sizeof(T) * 8) - _amount));
public Vector512<T> Invoke(Vector512<T> x) => (x << _amount) | (x >>> ((sizeof(T) * 8) - _amount));
}

/// <summary>T.RotateRight(amount)</summary>
Expand Down Expand Up @@ -14979,14 +14951,15 @@ public Vector512<T> Invoke(Vector512<T> x)
internal readonly struct ScaleBOperator<T>(int n) : IStatefulUnaryOperator<T> where T : IFloatingPointIeee754<T>
{
private readonly int _n = n;
private readonly T _pow2n = typeof(T) == typeof(float) || typeof(T) == typeof(double) ? T.Pow(T.CreateTruncating(2), T.CreateTruncating(n)) : default!;

public static bool Vectorizable => false; // TODO: Vectorize
public static bool Vectorizable => typeof(T) == typeof(float) || typeof(T) == typeof(double);

public T Invoke(T x) => T.ScaleB(x, _n);

public Vector128<T> Invoke(Vector128<T> x) => throw new NotSupportedException();
public Vector256<T> Invoke(Vector256<T> x) => throw new NotSupportedException();
public Vector512<T> Invoke(Vector512<T> x) => throw new NotSupportedException();
public Vector128<T> Invoke(Vector128<T> x) => x * Vector128.Create(_pow2n);
public Vector256<T> Invoke(Vector256<T> x) => x * Vector256.Create(_pow2n);
public Vector512<T> Invoke(Vector512<T> x) => x * Vector512.Create(_pow2n);
}

/// <summary>T.RootN(x, n)</summary>
Expand Down