Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b2b7d2c

Browse files
FeiPengInteljkotas
authored andcommitted
Generic hardware intrinsics throw exception for non-numeric types (#15068)
1 parent fc8bd03 commit b2b7d2c

File tree

7 files changed

+267
-50
lines changed

7 files changed

+267
-50
lines changed

src/mscorlib/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3697,4 +3697,7 @@
36973697
<data name="HashCode_EqualityNotSupported" xml:space="preserve">
36983698
<value>HashCode is a mutable struct and should not be compared with other HashCodes.</value>
36993699
</data>
3700+
<data name="Arg_TypeNotSupported" xml:space="preserve">
3701+
<value>Specified type is not supported</value>
3702+
</data>
37003703
</root>

src/mscorlib/src/System/Runtime/Intrinsics/X86/Avx.cs

Lines changed: 141 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -183,42 +183,78 @@ public static class Avx
183183
/// <summary>
184184
/// __int8 _mm256_extract_epi8 (__m256i a, const int index)
185185
/// </summary>
186-
public static sbyte ExtractSbyte<T>(Vector256<T> value, byte index) where T : struct => ExtractSbyte<T>(value, index);
186+
public static sbyte ExtractSbyte<T>(Vector256<T> value, byte index) where T : struct
187+
{
188+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
189+
return ExtractSbyte<T>(value, index);
190+
}
187191
/// <summary>
188192
/// __int8 _mm256_extract_epi8 (__m256i a, const int index)
189193
/// </summary>
190-
public static byte ExtractByte<T>(Vector256<T> value, byte index) where T : struct => ExtractByte<T>(value, index);
194+
public static byte ExtractByte<T>(Vector256<T> value, byte index) where T : struct
195+
{
196+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
197+
return ExtractByte<T>(value, index);
198+
}
191199
/// <summary>
192200
/// __int16 _mm256_extract_epi16 (__m256i a, const int index)
193201
/// </summary>
194-
public static short ExtractShort<T>(Vector256<T> value, byte index) where T : struct => ExtractShort<T>(value, index);
202+
public static short ExtractShort<T>(Vector256<T> value, byte index) where T : struct
203+
{
204+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
205+
return ExtractShort<T>(value, index);
206+
}
195207
/// <summary>
196208
/// __int16 _mm256_extract_epi16 (__m256i a, const int index)
197209
/// </summary>
198-
public static ushort ExtractUshort<T>(Vector256<T> value, byte index) where T : struct => ExtractUshort<T>(value, index);
210+
public static ushort ExtractUshort<T>(Vector256<T> value, byte index) where T : struct
211+
{
212+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
213+
return ExtractUshort<T>(value, index);
214+
}
199215
/// <summary>
200216
/// __int32 _mm256_extract_epi32 (__m256i a, const int index)
201217
/// </summary>
202-
public static int ExtractInt<T>(Vector256<T> value, byte index) where T : struct => ExtractInt<T>(value, index);
218+
public static int ExtractInt<T>(Vector256<T> value, byte index) where T : struct
219+
{
220+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
221+
return ExtractInt<T>(value, index);
222+
}
203223
/// <summary>
204224
/// __int32 _mm256_extract_epi32 (__m256i a, const int index)
205225
/// </summary>
206-
public static uint ExtractUint<T>(Vector256<T> value, byte index) where T : struct => ExtractUint<T>(value, index);
226+
public static uint ExtractUint<T>(Vector256<T> value, byte index) where T : struct
227+
{
228+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
229+
return ExtractUint<T>(value, index);
230+
}
207231
/// <summary>
208232
/// __int64 _mm256_extract_epi64 (__m256i a, const int index)
209233
/// </summary>
210-
public static long ExtractLong<T>(Vector256<T> value, byte index) where T : struct => ExtractLong<T>(value, index);
234+
public static long ExtractLong<T>(Vector256<T> value, byte index) where T : struct
235+
{
236+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
237+
return ExtractLong<T>(value, index);
238+
}
211239
/// <summary>
212240
/// __int64 _mm256_extract_epi64 (__m256i a, const int index)
213241
/// </summary>
214-
public static ulong ExtractUlong<T>(Vector256<T> value, byte index) where T : struct => ExtractUlong<T>(value, index);
242+
public static ulong ExtractUlong<T>(Vector256<T> value, byte index) where T : struct
243+
{
244+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
245+
return ExtractUlong<T>(value, index);
246+
}
215247

216248
/// <summary>
217249
/// __m128 _mm256_extractf128_ps (__m256 a, const int imm8)
218250
/// __m128d _mm256_extractf128_pd (__m256d a, const int imm8)
219251
/// __m128i _mm256_extractf128_si256 (__m256i a, const int imm8)
220252
/// </summary>
221-
public static Vector128<T> ExtractVector128<T>(Vector256<T> value, byte index) where T : struct => ExtractVector128<T>(value, index);
253+
public static Vector128<T> ExtractVector128<T>(Vector256<T> value, byte index) where T : struct
254+
{
255+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
256+
return ExtractVector128<T>(value, index);
257+
}
222258

223259
/// <summary>
224260
/// __m128i _mm256_extractf128_si256 (__m256i a, const int imm8)
@@ -266,7 +302,11 @@ public static class Avx
266302
/// __m256 _mm256_castps128_ps256 (__m128 a)
267303
/// __m256i _mm256_castsi128_si256 (__m128i a)
268304
/// </summary>
269-
public static Vector256<T> ExtendToVector256<T>(Vector128<T> value) where T : struct => ExtendToVector256<T>(value);
305+
public static Vector256<T> ExtendToVector256<T>(Vector128<T> value) where T : struct
306+
{
307+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
308+
return ExtendToVector256<T>(value);
309+
}
270310

271311
/// <summary>
272312
/// __m256 _mm256_floor_ps (__m256 a)
@@ -282,7 +322,11 @@ public static class Avx
282322
/// __m128 _mm256_castps256_ps128 (__m256 a)
283323
/// __m128i _mm256_castsi256_si128 (__m256i a)
284324
/// </summary>
285-
public static Vector128<T> GetLowerHalf<T>(Vector256<T> value) where T : struct => GetLowerHalf<T>(value);
325+
public static Vector128<T> GetLowerHalf<T>(Vector256<T> value) where T : struct
326+
{
327+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
328+
return GetLowerHalf<T>(value);
329+
}
286330

287331
/// <summary>
288332
/// __m256 _mm256_hadd_ps (__m256 a, __m256 b)
@@ -305,42 +349,78 @@ public static class Avx
305349
/// <summary>
306350
/// __m256i _mm256_insert_epi8 (__m256i a, __int8 i, const int index)
307351
/// </summary>
308-
public static Vector256<T> InsertSbyte<T>(Vector256<T> value, sbyte data, byte index) where T : struct => InsertSbyte<T>(value, data, index);
352+
public static Vector256<T> InsertSbyte<T>(Vector256<T> value, sbyte data, byte index) where T : struct
353+
{
354+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
355+
return InsertSbyte<T>(value, data, index);
356+
}
309357
/// <summary>
310358
/// __m256i _mm256_insert_epi8 (__m256i a, __int8 i, const int index)
311359
/// </summary>
312-
public static Vector256<T> InsertByte<T>(Vector256<T> value, byte data, byte index) where T : struct => InsertByte<T>(value, data, index);
360+
public static Vector256<T> InsertByte<T>(Vector256<T> value, byte data, byte index) where T : struct
361+
{
362+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
363+
return InsertByte<T>(value, data, index);
364+
}
313365
/// <summary>
314366
/// __m256i _mm256_insert_epi16 (__m256i a, __int16 i, const int index)
315367
/// </summary>
316-
public static Vector256<T> InsertShort<T>(Vector256<T> value, short data, byte index) where T : struct => InsertShort<T>(value, data, index);
368+
public static Vector256<T> InsertShort<T>(Vector256<T> value, short data, byte index) where T : struct
369+
{
370+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
371+
return InsertShort<T>(value, data, index);
372+
}
317373
/// <summary>
318374
/// __m256i _mm256_insert_epi16 (__m256i a, __int16 i, const int index)
319375
/// </summary>
320-
public static Vector256<T> InsertUshort<T>(Vector256<T> value, ushort data, byte index) where T : struct => InsertUshort<T>(value, data, index);
376+
public static Vector256<T> InsertUshort<T>(Vector256<T> value, ushort data, byte index) where T : struct
377+
{
378+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
379+
return InsertUshort<T>(value, data, index);
380+
}
321381
/// <summary>
322382
/// __m256i _mm256_insert_epi32 (__m256i a, __int32 i, const int index)
323383
/// </summary>
324-
public static Vector256<T> InsertInt<T>(Vector256<T> value, int data, byte index) where T : struct => InsertInt<T>(value, data, index);
384+
public static Vector256<T> InsertInt<T>(Vector256<T> value, int data, byte index) where T : struct
385+
{
386+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
387+
return InsertInt<T>(value, data, index);
388+
}
325389
/// <summary>
326390
/// __m256i _mm256_insert_epi32 (__m256i a, __int32 i, const int index)
327391
/// </summary>
328-
public static Vector256<T> InsertUint<T>(Vector256<T> value, uint data, byte index) where T : struct => InsertUint<T>(value, data, index);
392+
public static Vector256<T> InsertUint<T>(Vector256<T> value, uint data, byte index) where T : struct
393+
{
394+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
395+
return InsertUint<T>(value, data, index);
396+
}
329397
/// <summary>
330398
/// __m256i _mm256_insert_epi64 (__m256i a, __int64 i, const int index)
331399
/// </summary>
332-
public static Vector256<T> InsertLong<T>(Vector256<T> value, long data, byte index) where T : struct => InsertLong<T>(value, data, index);
400+
public static Vector256<T> InsertLong<T>(Vector256<T> value, long data, byte index) where T : struct
401+
{
402+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
403+
return InsertLong<T>(value, data, index);
404+
}
333405
/// <summary>
334406
/// __m256i _mm256_insert_epi64 (__m256i a, __int64 i, const int index)
335407
/// </summary>
336-
public static Vector256<T> InsertUlong<T>(Vector256<T> value, ulong data, byte index) where T : struct => InsertUlong<T>(value, data, index);
408+
public static Vector256<T> InsertUlong<T>(Vector256<T> value, ulong data, byte index) where T : struct
409+
{
410+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
411+
return InsertUlong<T>(value, data, index);
412+
}
337413

338414
/// <summary>
339415
/// __m256 _mm256_insertf128_ps (__m256 a, __m128 b, int imm8)
340416
/// __m256d _mm256_insertf128_pd (__m256d a, __m128d b, int imm8)
341417
/// __m256i _mm256_insertf128_si256 (__m256i a, __m128i b, int imm8)
342418
/// </summary>
343-
public static Vector256<T> Insert<T>(Vector256<T> value, Vector128<T> data, byte index) where T : struct => Insert<T>(value, data, index);
419+
public static Vector256<T> Insert<T>(Vector256<T> value, Vector128<T> data, byte index) where T : struct
420+
{
421+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
422+
return Insert<T>(value, data, index);
423+
}
344424

345425
/// <summary>
346426
/// __m256i _mm256_insertf128_si256 (__m256i a, __m128i b, int imm8)
@@ -726,21 +806,33 @@ public static class Avx
726806
/// __m256 _mm256_set1_ps (float a)
727807
/// __m256d _mm256_set1_pd (double a)
728808
/// </summary>
729-
public static Vector256<T> Set1<T>(T value) where T : struct => Set1<T>(value);
809+
public static Vector256<T> Set1<T>(T value) where T : struct
810+
{
811+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
812+
return Set1<T>(value);
813+
}
730814

731815
/// <summary>
732816
/// __m256 _mm256_set_m128 (__m128 hi, __m128 lo)
733817
/// __m256d _mm256_set_m128d (__m128d hi, __m128d lo)
734818
/// __m256i _mm256_set_m128i (__m128i hi, __m128i lo)
735819
/// </summary>
736-
public static Vector256<T> SetHiLo<T>(Vector128<T> hi, Vector128<T> lo) where T : struct => SetHiLo<T>(hi, lo);
820+
public static Vector256<T> SetHiLo<T>(Vector128<T> hi, Vector128<T> lo) where T : struct
821+
{
822+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
823+
return SetHiLo<T>(hi, lo);
824+
}
737825

738826
/// <summary>
739827
/// __m256i _mm256_setzero_si256 (void)
740828
/// __m256 _mm256_setzero_ps (void)
741829
/// __m256d _mm256_setzero_pd (void)
742830
/// </summary>
743-
public static Vector256<T> SetZero<T>() where T : struct => SetZero<T>();
831+
public static Vector256<T> SetZero<T>() where T : struct
832+
{
833+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
834+
return SetZero<T>();
835+
}
744836

745837
/// <summary>
746838
/// __m256 _mm256_shuffle_ps (__m256 a, __m256 b, const int imm8)
@@ -768,7 +860,12 @@ public static class Avx
768860
/// __m256d _mm256_castsi256_pd (__m256i a)
769861
/// __m256 _mm256_castsi256_ps (__m256i a)
770862
/// </summary>
771-
public static Vector256<U> StaticCast<T, U>(Vector256<T> value) where T : struct where U : struct => StaticCast<T, U>(value);
863+
public static Vector256<U> StaticCast<T, U>(Vector256<T> value) where T : struct where U : struct
864+
{
865+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
866+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
867+
return StaticCast<T, U>(value);
868+
}
772869

773870
/// <summary>
774871
/// void _mm256_store_si256 (__m256i * mem_addr, __m256i a)
@@ -916,7 +1013,11 @@ public static class Avx
9161013
/// int _mm256_testc_ps (__m256 a, __m256 b)
9171014
/// int _mm256_testc_pd (__m256d a, __m256d b)
9181015
/// </summary>
919-
public static bool TestC<T>(Vector256<T> left, Vector256<T> right) where T : struct => TestC<T>(left, right);
1016+
public static bool TestC<T>(Vector256<T> left, Vector256<T> right) where T : struct
1017+
{
1018+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
1019+
return TestC<T>(left, right);
1020+
}
9201021

9211022
/// <summary>
9221023
/// int _mm_testnzc_ps (__m128 a, __m128 b)
@@ -932,7 +1033,11 @@ public static class Avx
9321033
/// int _mm256_testnzc_ps (__m256 a, __m256 b)
9331034
/// int _mm256_testnzc_pd (__m256d a, __m256d b)
9341035
/// </summary>
935-
public static bool TestNotZAndNotC<T>(Vector256<T> left, Vector256<T> right) where T : struct => TestNotZAndNotC<T>(left, right);
1036+
public static bool TestNotZAndNotC<T>(Vector256<T> left, Vector256<T> right) where T : struct
1037+
{
1038+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
1039+
return TestNotZAndNotC<T>(left, right);
1040+
}
9361041

9371042
/// <summary>
9381043
/// int _mm_testz_ps (__m128 a, __m128 b)
@@ -948,7 +1053,11 @@ public static class Avx
9481053
/// int _mm256_testz_ps (__m256 a, __m256 b)
9491054
/// int _mm256_testz_pd (__m256d a, __m256d b)
9501055
/// </summary>
951-
public static bool TestZ<T>(Vector256<T> left, Vector256<T> right) where T : struct => TestZ<T>(left, right);
1056+
public static bool TestZ<T>(Vector256<T> left, Vector256<T> right) where T : struct
1057+
{
1058+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
1059+
return TestZ<T>(left, right);
1060+
}
9521061

9531062
/// <summary>
9541063
/// __m256 _mm256_unpackhi_ps (__m256 a, __m256 b)
@@ -991,6 +1100,10 @@ public static class Avx
9911100
/// __m256 _mm256_zextps128_ps256 (__m128 a)
9921101
/// __m256i _mm256_zextsi128_si256 (__m128i a)
9931102
/// </summary>
994-
public static Vector256<T> ZeroExtendToVector256<T>(Vector128<T> value) where T : struct => ZeroExtendToVector256<T>(value);
1103+
public static Vector256<T> ZeroExtendToVector256<T>(Vector128<T> value) where T : struct
1104+
{
1105+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
1106+
return ZeroExtendToVector256<T>(value);
1107+
}
9951108
}
9961109
}

src/mscorlib/src/System/Runtime/Intrinsics/X86/Avx2.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ public static class Avx2
200200
/// __m128 _mm_broadcastss_ps (__m128 a)
201201
/// __m128d _mm_broadcastsd_pd (__m128d a)
202202
/// </summary>
203-
public static Vector128<T> BroadcastElementToVector128<T>(Vector128<T> value) where T : struct => BroadcastElementToVector128<T>(value);
203+
public static Vector128<T> BroadcastElementToVector128<T>(Vector128<T> value) where T : struct
204+
{
205+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
206+
return BroadcastElementToVector128<T>(value);
207+
}
204208

205209
/// <summary>
206210
/// __m256i _mm256_broadcastb_epi8 (__m128i a)
@@ -210,7 +214,11 @@ public static class Avx2
210214
/// __m256 _mm256_broadcastss_ps (__m128 a)
211215
/// __m256d _mm256_broadcastsd_pd (__m128d a)
212216
/// </summary>
213-
public static Vector256<T> BroadcastElementToVector256<T>(Vector128<T> value) where T : struct => BroadcastElementToVector256<T>(value);
217+
public static Vector256<T> BroadcastElementToVector256<T>(Vector128<T> value) where T : struct
218+
{
219+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
220+
return BroadcastElementToVector256<T>(value);
221+
}
214222

215223
/// <summary>
216224
/// __m256i _mm256_broadcastsi128_si256 (__m128i a)

src/mscorlib/src/System/Runtime/Intrinsics/X86/Sse.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ public static class Sse
168168
/// __m128d _mm_castsi128_pd (__m128i a)
169169
/// __m128 _mm_castsi128_ps (__m128i a)
170170
/// </summary>
171-
public static Vector128<U> StaticCast<T, U>(Vector128<T> value) where T : struct where U : struct => StaticCast<T, U>(value);
171+
public static Vector128<U> StaticCast<T, U>(Vector128<T> value) where T : struct where U : struct
172+
{
173+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<T>();
174+
ThrowHelper.ThrowNotSupportedExceptionIfNonNumericType<U>();
175+
return StaticCast<T, U>(value);
176+
}
172177

173178
/// <summary>
174179
/// __m128 _mm_shuffle_ps (__m128 a, __m128 b, unsigned int control)

0 commit comments

Comments
 (0)