Skip to content

Commit 9868e73

Browse files
committed
Comment change and implement ShuffleUnsafe for sbyte
- Implement `ShuffleUnsafe` for `sbyte` - Remove remaining detailed comment on V128.ShuffleUnsafe (which specified which intrinsics were used): it is not good to only have it for this one, which could also theoretically change behaviour in the future e.g., with the introduction of RISC-V
1 parent 08589bd commit 9868e73

File tree

8 files changed

+394
-214
lines changed

8 files changed

+394
-214
lines changed

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector128.cs

+31-3
Original file line numberDiff line numberDiff line change
@@ -2363,9 +2363,7 @@ public static Vector128<sbyte> Shuffle(Vector128<sbyte> vector, Vector128<sbyte>
23632363
/// <param name="vector">The input vector from which values are selected.</param>
23642364
/// <param name="indices">The per-element indices used to select a value from <paramref name="vector" />.</param>
23652365
/// <returns>A new vector containing the values from <paramref name="vector" /> selected by the given <paramref name="indices" />.</returns>
2366-
/// <remarks>Unlike Shuffle, this method delegates to the underlying hardware intrinsic without ensuring that <paramref name="indices"/> are normalized to [0, 15].
2367-
/// On hardware with <see cref="Ssse3"/> support, indices are treated as modulo 16, and if the high bit is set, the result will be set to 0 for that element.
2368-
/// On hardware with <see cref="AdvSimd.Arm64"/> or <see cref="PackedSimd"/> support, this method behaves the same as Shuffle.</remarks>
2366+
/// <remarks>Unlike Shuffle, this method delegates to the underlying hardware intrinsic without ensuring that <paramref name="indices"/> are normalized to [0, 15].</remarks>
23692367
[Intrinsic]
23702368
[CompExactlyDependsOn(typeof(Ssse3))]
23712369
[CompExactlyDependsOn(typeof(AdvSimd))]
@@ -2390,6 +2388,36 @@ public static Vector128<byte> ShuffleUnsafe(Vector128<byte> vector, Vector128<by
23902388
return result;
23912389
}
23922390

2391+
/// <summary>Creates a new vector by selecting values from an input vector using a set of indices.
2392+
/// Behavior is platform-dependent for out-of-range indices.</summary>
2393+
/// <param name="vector">The input vector from which values are selected.</param>
2394+
/// <param name="indices">The per-element indices used to select a value from <paramref name="vector" />.</param>
2395+
/// <returns>A new vector containing the values from <paramref name="vector" /> selected by the given <paramref name="indices" />.</returns>
2396+
/// <remarks>Unlike Shuffle, this method delegates to the underlying hardware intrinsic without ensuring that <paramref name="indices"/> are normalized to [0, 15].</remarks>
2397+
[Intrinsic]
2398+
[CompExactlyDependsOn(typeof(Ssse3))]
2399+
[CompExactlyDependsOn(typeof(AdvSimd))]
2400+
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
2401+
[CompExactlyDependsOn(typeof(PackedSimd))]
2402+
public static Vector128<sbyte> ShuffleUnsafe(Vector128<sbyte> vector, Vector128<sbyte> indices)
2403+
{
2404+
Unsafe.SkipInit(out Vector128<sbyte> result);
2405+
2406+
for (int index = 0; index < Vector128<sbyte>.Count; index++)
2407+
{
2408+
byte selectedIndex = (byte)indices.GetElementUnsafe(index);
2409+
byte selectedValue = 0;
2410+
2411+
if (selectedIndex < Vector128<sbyte>.Count)
2412+
{
2413+
selectedValue = vector.GetElementUnsafe(selectedIndex);
2414+
}
2415+
result.SetElementUnsafe(index, selectedValue);
2416+
}
2417+
2418+
return result;
2419+
}
2420+
23932421
/// <summary>Creates a new vector by selecting values from an input vector using a set of indices.</summary>
23942422
/// <param name="vector">The input vector from which values are selected.</param>
23952423
/// <param name="indices">The per-element indices used to select a value from <paramref name="vector" />.</param>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector256.cs

+28
Original file line numberDiff line numberDiff line change
@@ -2369,6 +2369,34 @@ public static Vector256<byte> ShuffleUnsafe(Vector256<byte> vector, Vector256<by
23692369
return result;
23702370
}
23712371

2372+
/// <summary>Creates a new vector by selecting values from an input vector using a set of indices.
2373+
/// Behavior is platform-dependent for out-of-range indices.</summary>
2374+
/// <param name="vector">The input vector from which values are selected.</param>
2375+
/// <param name="indices">The per-element indices used to select a value from <paramref name="vector" />.</param>
2376+
/// <returns>A new vector containing the values from <paramref name="vector" /> selected by the given <paramref name="indices" />.</returns>
2377+
/// <remarks>Unlike Shuffle, this method delegates to the underlying hardware intrinsic without ensuring that <paramref name="indices"/> are normalized to [0, 31].</remarks>
2378+
[Intrinsic]
2379+
[CompExactlyDependsOn(typeof(Avx2))]
2380+
[CompExactlyDependsOn(typeof(Avx512Vbmi.VL))]
2381+
public static Vector256<sbyte> ShuffleUnsafe(Vector256<sbyte> vector, Vector256<sbyte> indices)
2382+
{
2383+
Unsafe.SkipInit(out Vector256<sbyte> result);
2384+
2385+
for (int index = 0; index < Vector256<sbyte>.Count; index++)
2386+
{
2387+
byte selectedIndex = (byte)indices.GetElementUnsafe(index);
2388+
byte selectedValue = 0;
2389+
2390+
if (selectedIndex < Vector256<byte>.Count)
2391+
{
2392+
selectedValue = vector.GetElementUnsafe(selectedIndex);
2393+
}
2394+
result.SetElementUnsafe(index, selectedValue);
2395+
}
2396+
2397+
return result;
2398+
}
2399+
23722400
/// <summary>Creates a new vector by selecting values from an input vector using a set of indices.</summary>
23732401
/// <param name="vector">The input vector from which values are selected.</param>
23742402
/// <param name="indices">The per-element indices used to select a value from <paramref name="vector" />.</param>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector512.cs

+27
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,33 @@ public static Vector512<byte> ShuffleUnsafe(Vector512<byte> vector, Vector512<by
24152415
return result;
24162416
}
24172417

2418+
/// <summary>Creates a new vector by selecting values from an input vector using a set of indices.
2419+
/// Behavior is platform-dependent for out-of-range indices.</summary>
2420+
/// <param name="vector">The input vector from which values are selected.</param>
2421+
/// <param name="indices">The per-element indices used to select a value from <paramref name="vector" />.</param>
2422+
/// <returns>A new vector containing the values from <paramref name="vector" /> selected by the given <paramref name="indices" />.</returns>
2423+
/// <remarks>Unlike Shuffle, this method delegates to the underlying hardware intrinsic without ensuring that <paramref name="indices"/> are normalized to [0, 63].</remarks>
2424+
[Intrinsic]
2425+
[CompExactlyDependsOn(typeof(Avx512Vbmi))]
2426+
public static Vector512<sbyte> ShuffleUnsafe(Vector512<sbyte> vector, Vector512<sbyte> indices)
2427+
{
2428+
Unsafe.SkipInit(out Vector512<byte> result);
2429+
2430+
for (int index = 0; index < Vector512<sbyte>.Count; index++)
2431+
{
2432+
byte selectedIndex = (byte)indices.GetElementUnsafe(index);
2433+
byte selectedValue = 0;
2434+
2435+
if (selectedIndex < Vector512<byte>.Count)
2436+
{
2437+
selectedValue = vector.GetElementUnsafe(selectedIndex);
2438+
}
2439+
result.SetElementUnsafe(index, selectedValue);
2440+
}
2441+
2442+
return result;
2443+
}
2444+
24182445
/// <summary>Creates a new vector by selecting values from an input vector using a set of indices.</summary>
24192446
/// <param name="vector">The input vector from which values are selected.</param>
24202447
/// <param name="indices">The per-element indices used to select a value from <paramref name="vector" />.</param>

src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector64.cs

+27
Original file line numberDiff line numberDiff line change
@@ -2284,6 +2284,33 @@ public static Vector64<byte> ShuffleUnsafe(Vector64<byte> vector, Vector64<byte>
22842284
return result;
22852285
}
22862286

2287+
/// <summary>Creates a new vector by selecting values from an input vector using a set of indices.
2288+
/// Behavior is platform-dependent for out-of-range indices.</summary>
2289+
/// <param name="vector">The input vector from which values are selected.</param>
2290+
/// <param name="indices">The per-element indices used to select a value from <paramref name="vector" />.</param>
2291+
/// <returns>A new vector containing the values from <paramref name="vector" /> selected by the given <paramref name="indices" />.</returns>
2292+
/// <remarks>Unlike Shuffle, this method delegates to the underlying hardware intrinsic without ensuring that <paramref name="indices"/> are normalized to [0, 7].</remarks>
2293+
[Intrinsic]
2294+
[CompExactlyDependsOn(typeof(AdvSimd))]
2295+
public static Vector64<sbyte> ShuffleUnsafe(Vector64<sbyte> vector, Vector64<sbyte> indices)
2296+
{
2297+
Unsafe.SkipInit(out Vector64<sbyte> result);
2298+
2299+
for (int index = 0; index < Vector64<byte>.Count; index++)
2300+
{
2301+
byte selectedIndex = (byte)indices.GetElementUnsafe(index);
2302+
byte selectedValue = 0;
2303+
2304+
if (selectedIndex < Vector64<byte>.Count)
2305+
{
2306+
selectedValue = vector.GetElementUnsafe(selectedIndex);
2307+
}
2308+
result.SetElementUnsafe(index, selectedValue);
2309+
}
2310+
2311+
return result;
2312+
}
2313+
22872314
/// <summary>Creates a new vector by selecting values from an input vector using a set of indices.</summary>
22882315
/// <param name="vector">The input vector from which values are selected.</param>
22892316
/// <param name="indices">The per-element indices used to select a value from <paramref name="vector" />.</param>

0 commit comments

Comments
 (0)