-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
GenericsTest.VectorL.cs
162 lines (122 loc) · 5.99 KB
/
GenericsTest.VectorL.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using Xunit;
unsafe partial class GenericsNative
{
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> GetVectorL128(long e00, long e01);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> GetVectorL256(long e00, long e01, long e02, long e03);
[DllImport(nameof(GenericsNative))]
public static extern void GetVectorL128Out(long e00, long e01, Vector<long>* value);
[DllImport(nameof(GenericsNative))]
public static extern void GetVectorL256Out(long e00, long e01, long e02, long e03, Vector<long>* value);
[DllImport(nameof(GenericsNative))]
public static extern void GetVectorL128Out(long e00, long e01, out Vector<long> value);
[DllImport(nameof(GenericsNative))]
public static extern void GetVectorL256Out(long e00, long e01, long e02, long e03, out Vector<long> value);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long>* GetVectorL128Ptr(long e00, long e01);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long>* GetVectorL256Ptr(long e00, long e01, long e02, long e03);
[DllImport(nameof(GenericsNative), EntryPoint = "GetVectorL128Ptr")]
public static extern ref readonly Vector<long> GetVectorL128Ref(long e00, long e01);
[DllImport(nameof(GenericsNative), EntryPoint = "GetVectorL256Ptr")]
public static extern ref readonly Vector<long> GetVectorL256Ref(long e00, long e01, long e02, long e03);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> AddVectorL128(Vector<long> lhs, Vector<long> rhs);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> AddVectorL256(Vector<long> lhs, Vector<long> rhs);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> AddVectorL128s(Vector<long>* pValues, int count);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> AddVectorL256s(Vector<long>* pValues, int count);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> AddVectorL128s([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] Vector<long>[] pValues, int count);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> AddVectorL256s([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] Vector<long>[] pValues, int count);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> AddVectorL128s(in Vector<long> pValues, int count);
[DllImport(nameof(GenericsNative))]
public static extern Vector<long> AddVectorL256s(in Vector<long> pValues, int count);
}
unsafe partial class GenericsTest
{
private static void TestVectorL()
{
if (Vector<long>.Count == 4)
{
TestVectorL256();
}
else
{
Assert.Equal(Vector<long>.Count, 2);
TestVectorL128();
}
}
private static void TestVectorL128()
{
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.GetVectorL128(1L, 2L));
Vector<long> value2;
GenericsNative.GetVectorL128Out(1L, 2L, &value2);
Assert.Equal(value2[0], 1L);
Assert.Equal(value2[1], 2L);
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.GetVectorL128Out(1L, 2L, out Vector<long> value3));
Vector<long>* value4 = GenericsNative.GetVectorL128Ptr(1L, 2L);
Assert.Equal((*value4)[0], 1L);
Assert.Equal((*value4)[1], 2L);
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.GetVectorL128Ref(1L, 2L));
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.AddVectorL128(default, default));
Vector<long>[] values = new Vector<long>[] {
default,
value2,
default,
*value4,
default,
};
Assert.Throws<MarshalDirectiveException>(() => {
fixed (Vector<long>* pValues = &values[0])
{
GenericsNative.AddVectorL128s(pValues, values.Length);
}
});
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.AddVectorL128s(values, values.Length));
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.AddVectorL128s(in values[0], values.Length));
}
private static void TestVectorL256()
{
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.GetVectorL256(1L, 2L, 3L, 4L));
Vector<long> value2;
GenericsNative.GetVectorL256Out(1L, 2L, 3L, 4L, &value2);
Assert.Equal(value2[0], 1L);
Assert.Equal(value2[1], 2L);
Assert.Equal(value2[2], 3L);
Assert.Equal(value2[3], 4L);
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.GetVectorL256Out(1L, 2L, 3L, 4L, out Vector<long> value3));
Vector<long>* value4 = GenericsNative.GetVectorL256Ptr(1L, 2L, 3L, 4L);
Assert.Equal((*value4)[0], 1L);
Assert.Equal((*value4)[1], 2L);
Assert.Equal((*value4)[2], 3L);
Assert.Equal((*value4)[3], 4L);
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.GetVectorL256Ref(1L, 2L, 3L, 4L));
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.AddVectorL256(default, default));
Vector<long>[] values = new Vector<long>[] {
default,
value2,
default,
*value4,
default,
};
Assert.Throws<MarshalDirectiveException>(() => {
fixed (Vector<long>* pValues = &values[0])
{
GenericsNative.AddVectorL256s(pValues, values.Length);
}
});
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.AddVectorL256s(values, values.Length));
Assert.Throws<MarshalDirectiveException>(() => GenericsNative.AddVectorL256s(in values[0], values.Length));
}
}