From f3b8427acb3a18181fe9d8e581d8b329e5809409 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Tue, 7 Mar 2023 16:21:36 +0100 Subject: [PATCH] [wasm] Add narrow methods to PackedSimd (#83084) * [wasm] Add narrow methods to PackedSimd Add them as internal as the approved API contains wrong methods for these. https://github.com/dotnet/runtime/issues/53730#issuecomment-1453900537 This will allow faster implementation of IndexOfAnyValues for wasm. https://github.com/dotnet/runtime/pull/82789#issuecomment-1451056305 * Fix build --- .../Wasm/PackedSimd.PlatformNotSupported.cs | 6 ++++ .../Runtime/Intrinsics/Wasm/PackedSimd.cs | 24 ++++++++++++++ src/mono/mono/mini/simd-intrinsics.c | 32 +++++++++++++++++++ src/mono/mono/mini/simd-methods.h | 2 ++ 4 files changed, 64 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/PackedSimd.PlatformNotSupported.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/PackedSimd.PlatformNotSupported.cs index 2720f30f65426..69f53e6e2f134 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/PackedSimd.PlatformNotSupported.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/PackedSimd.PlatformNotSupported.cs @@ -151,5 +151,11 @@ public abstract class PackedSimd public static Vector128 CompareNotEqual(Vector128 left, Vector128 right) { throw new PlatformNotSupportedException(); } public static Vector128 CompareNotEqual(Vector128 left, Vector128 right) { throw new PlatformNotSupportedException(); } public static Vector128 CompareNotEqual(Vector128 left, Vector128 right) { throw new PlatformNotSupportedException(); } + + internal static Vector128 ConvertNarrowingSignedSaturate(Vector128 lower, Vector128 upper) { throw new PlatformNotSupportedException(); } + internal static Vector128 ConvertNarrowingSignedSaturate(Vector128 lower, Vector128 upper) { throw new PlatformNotSupportedException(); } + + internal static Vector128 ConvertNarrowingUnsignedSaturate(Vector128 lower, Vector128 upper) { throw new PlatformNotSupportedException(); } + internal static Vector128 ConvertNarrowingUnsignedSaturate(Vector128 lower, Vector128 upper) { throw new PlatformNotSupportedException(); } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/PackedSimd.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/PackedSimd.cs index ab5da4a00e449..d4d37df9096cf 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/PackedSimd.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Wasm/PackedSimd.cs @@ -665,5 +665,29 @@ public abstract class PackedSimd /// [Intrinsic] public static Vector128 CompareNotEqual(Vector128 left, Vector128 right) => CompareNotEqual(left, right); + + /// + /// i8x16.narrow_i16x8_s + /// + [Intrinsic] + internal static Vector128 ConvertNarrowingSignedSaturate(Vector128 lower, Vector128 upper) => ConvertNarrowingSignedSaturate(lower, upper); + + /// + /// i16x8.narrow_i32x4_s + /// + [Intrinsic] + internal static Vector128 ConvertNarrowingSignedSaturate(Vector128 lower, Vector128 upper) => ConvertNarrowingSignedSaturate(lower, upper); + + /// + /// i8x16.narrow_i16x8_u + /// + [Intrinsic] + internal static Vector128 ConvertNarrowingUnsignedSaturate(Vector128 lower, Vector128 upper) => ConvertNarrowingUnsignedSaturate(lower, upper); + + /// + /// i16x8.narrow_i32x4_u + /// + [Intrinsic] + internal static Vector128 ConvertNarrowingUnsignedSaturate(Vector128 lower, Vector128 upper) => ConvertNarrowingUnsignedSaturate(lower, upper); } } diff --git a/src/mono/mono/mini/simd-intrinsics.c b/src/mono/mono/mini/simd-intrinsics.c index c0db54a717616..6ee5a4fc84e4e 100644 --- a/src/mono/mono/mini/simd-intrinsics.c +++ b/src/mono/mono/mini/simd-intrinsics.c @@ -4613,6 +4613,8 @@ static SimdIntrinsic packedsimd_methods [] = { {SN_Bitmask}, {SN_CompareEqual}, {SN_CompareNotEqual}, + {SN_ConvertNarrowingSignedSaturate}, + {SN_ConvertNarrowingUnsignedSaturate}, {SN_Dot}, {SN_ExtractLane}, {SN_Multiply}, @@ -4708,6 +4710,36 @@ emit_wasm_supported_intrinsics ( return emit_simd_ins_for_sig (cfg, klass, type_enum_is_float (arg0_type) ? OP_XCOMPARE_FP : OP_XCOMPARE, CMP_EQ, arg0_type, fsig, args); case SN_CompareNotEqual: return emit_simd_ins_for_sig (cfg, klass, type_enum_is_float (arg0_type) ? OP_XCOMPARE_FP : OP_XCOMPARE, CMP_NE, arg0_type, fsig, args); + case SN_ConvertNarrowingSignedSaturate: { + int intrins = -1; + switch (arg0_type) { + case MONO_TYPE_I2: + intrins = INTRINS_WASM_NARROW_SIGNED_V16; + break; + case MONO_TYPE_I4: + intrins = INTRINS_WASM_NARROW_SIGNED_V8; + break; + } + if (intrins != -1) + return emit_simd_ins_for_sig (cfg, klass, OP_XOP_X_X_X, intrins, arg0_type, fsig, args); + + return NULL; + } + case SN_ConvertNarrowingUnsignedSaturate: { + int intrins = -1; + switch (arg0_type) { + case MONO_TYPE_I2: + intrins = INTRINS_WASM_NARROW_UNSIGNED_V16; + break; + case MONO_TYPE_I4: + intrins = INTRINS_WASM_NARROW_UNSIGNED_V8; + break; + } + if (intrins != -1) + return emit_simd_ins_for_sig (cfg, klass, OP_XOP_X_X_X, intrins, arg0_type, fsig, args); + + return NULL; + } case SN_ExtractLane: { int extract_op = type_to_xextract_op (arg0_type); return emit_simd_ins_for_sig (cfg, klass, extract_op, -1, arg0_type, fsig, args); diff --git a/src/mono/mono/mini/simd-methods.h b/src/mono/mono/mini/simd-methods.h index 2d4b6ddee42f9..5b442c307bbcb 100644 --- a/src/mono/mono/mini/simd-methods.h +++ b/src/mono/mono/mini/simd-methods.h @@ -639,3 +639,5 @@ METHOD(Splat) METHOD(ExtractLane) METHOD(ReplaceLane) METHOD(Swizzle) +METHOD(ConvertNarrowingSignedSaturate) +METHOD(ConvertNarrowingUnsignedSaturate)