Skip to content

Commit 56cc25d

Browse files
committed
Non intrinsic constructor
1 parent 67f07aa commit 56cc25d

File tree

2 files changed

+118
-4
lines changed

2 files changed

+118
-4
lines changed

src/System.Private.CoreLib/shared/System/Numerics/Vector.cs

+92-2
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,98 @@ public unsafe Vector(T value)
373373
/// <summary>
374374
/// Constructs a vector from the given array. The size of the given array must be at least Vector'T.Count.
375375
/// </summary>
376-
[Intrinsic]
377-
public unsafe Vector(T[] values) : this(values, 0) { }
376+
public unsafe Vector(T[] values) : this()
377+
{
378+
if (values == null)
379+
{
380+
// Match the JIT's exception type here. For perf, a NullReference is thrown instead of an ArgumentNull.
381+
throw new NullReferenceException(SR.Arg_NullArgumentNullRef);
382+
}
383+
if (typeof(T) == typeof(Byte))
384+
{
385+
if (values.Length < Count)
386+
{
387+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
388+
}
389+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
390+
}
391+
else if (typeof(T) == typeof(SByte))
392+
{
393+
if (values.Length < Count)
394+
{
395+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
396+
}
397+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
398+
}
399+
else if (typeof(T) == typeof(UInt16))
400+
{
401+
if (values.Length < Count)
402+
{
403+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
404+
}
405+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
406+
}
407+
else if (typeof(T) == typeof(Int16))
408+
{
409+
if (values.Length < Count)
410+
{
411+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
412+
}
413+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
414+
}
415+
else if (typeof(T) == typeof(UInt32))
416+
{
417+
if (values.Length < Count)
418+
{
419+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
420+
}
421+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
422+
}
423+
else if (typeof(T) == typeof(Int32))
424+
{
425+
if (values.Length < Count)
426+
{
427+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
428+
}
429+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
430+
}
431+
else if (typeof(T) == typeof(UInt64))
432+
{
433+
if (values.Length < Count)
434+
{
435+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
436+
}
437+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
438+
}
439+
else if (typeof(T) == typeof(Int64))
440+
{
441+
if (values.Length < Count)
442+
{
443+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
444+
}
445+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
446+
}
447+
else if (typeof(T) == typeof(Single))
448+
{
449+
if (values.Length < Count)
450+
{
451+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
452+
}
453+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
454+
}
455+
else if (typeof(T) == typeof(Double))
456+
{
457+
if (values.Length < Count)
458+
{
459+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
460+
}
461+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
462+
}
463+
else
464+
{
465+
throw new NotSupportedException(SR.Arg_TypeNotSupported);
466+
}
467+
}
378468

379469
/// <summary>
380470
/// Constructs a vector from the given array, starting from the given index.

src/System.Private.CoreLib/shared/System/Numerics/Vector.tt

+26-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,32 @@ namespace System.Numerics
185185
/// <summary>
186186
/// Constructs a vector from the given array. The size of the given array must be at least Vector'T.Count.
187187
/// </summary>
188-
[Intrinsic]
189-
public unsafe Vector(T[] values) : this(values, 0) { }
188+
public unsafe Vector(T[] values) : this()
189+
{
190+
if (values == null)
191+
{
192+
// Match the JIT's exception type here. For perf, a NullReference is thrown instead of an ArgumentNull.
193+
throw new NullReferenceException(SR.Arg_NullArgumentNullRef);
194+
}
195+
<# foreach (Type type in supportedTypes)
196+
{
197+
#>
198+
<#=GenerateIfStatementHeader(type)#>
199+
{
200+
if (values.Length < Count)
201+
{
202+
throw new IndexOutOfRangeException(SR.Format(SR.Arg_InsufficientNumberOfElements, Vector<T>.Count, nameof(values)));
203+
}
204+
this = Unsafe.ReadUnaligned<Vector<T>>(ref values.GetRawSzArrayData());
205+
}
206+
<#
207+
}
208+
#>
209+
else
210+
{
211+
throw new NotSupportedException(SR.Arg_TypeNotSupported);
212+
}
213+
}
190214

191215
/// <summary>
192216
/// Constructs a vector from the given array, starting from the given index.

0 commit comments

Comments
 (0)