summaryrefslogtreecommitdiff
path: root/tests/src/JIT/HardwareIntrinsics/X86/Regression/GitHub_21666/GitHub_21666.cs
blob: fa436ae7e012a36333cb20186e7c2a92fa42f902 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Runtime.Intrinsics.X86;
using System.Runtime.Intrinsics;

namespace GitHub_21666
{
    // CRC32 is a special instruction that has 4-byte opcode but does not use SSE38 or SSE3A encoding,
    // so the compiler backend needs to specially check its code size.
    // Test LZCNT as well to ensure that future changes do not impact 3-byte opcode instructions.
    class GitHub_21666
    {
        const int Pass = 100;
        const int Fail = 0;

        static byte byteSF = 1;
        static ushort ushortSF = 1;
        static uint uintSF = 1;
        static ulong ulongSF = 1;

        readonly static byte[] byteArray = new byte[10];
        readonly static ushort[] ushortArray = new ushort[10];
        readonly static uint[] uintArray = new uint[10];
        readonly static ulong[] ulongArray = new ulong[10];

        static int Main(string[] args)
        {
            bool sucess = true;
            byteSF = 0;
            ushortSF = 0;
            uintSF = 0;
            ulongSF = 0;
            sucess = sucess && TestByteContainment();
            sucess = sucess && TestUInt16Containment();
            sucess = sucess && TestUInt32Containment();
            sucess = sucess && TestUInt64Containment();

            return sucess ? Pass : Fail;
        }

        static unsafe bool TestByteContainment()
        {
            byte value = (byte)0;
            byte* ptr = &value;
            if (Sse42.IsSupported)
            {
                if (Sse42.Crc32(0xffffffffU, (byte)0) != 0xad82acaeU)
                {
                    Console.WriteLine("TestByteContainment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, value) != 0xad82acaeU)
                {
                    Console.WriteLine("TestByteContainment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, *ptr) != 0xad82acaeU)
                {
                    Console.WriteLine("TestByteContainment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, byteArray[1]) != 0xad82acaeU)
                {
                    Console.WriteLine("TestByteContainment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, byteArray[*ptr + 1]) != 0xad82acaeU)
                {
                    Console.WriteLine("TestByteContainment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, byteSF) != 0xad82acaeU)
                {
                    Console.WriteLine("TestByteContainment failed on Crc32");
                    return false;
                }
            }

            return true;
        }

        static unsafe bool TestUInt16Containment()
        {
            ushort value = (ushort)0;
            ushort* ptr = &value;
            if (Sse42.IsSupported)
            {
                if (Sse42.Crc32(0xffffffffU, (ushort)0) != 0xe9e882dU)
                {
                    Console.WriteLine("TestUInt16Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, value) != 0xe9e882dU)
                {
                    Console.WriteLine("TestUInt16Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, *ptr) != 0xe9e882dU)
                {
                    Console.WriteLine("TestUInt16Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, ushortArray[1]) != 0xe9e882dU)
                {
                    Console.WriteLine("TestUInt16Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, ushortArray[*ptr + 1]) != 0xe9e882dU)
                {
                    Console.WriteLine("TestUInt16Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, ushortSF) != 0xe9e882dU)
                {
                    Console.WriteLine("TestUInt16Containment failed on Crc32");
                    return false;
                }
            }

            return true;
        }

        static unsafe bool TestUInt32Containment()
        {
            uint value = (uint)0;
            uint* ptr = &value;
            if (Lzcnt.IsSupported)
            {
                if (Lzcnt.LeadingZeroCount(*ptr) != 32)
                {
                    Console.WriteLine("TestUInt32Containment failed on LeadingZeroCount");
                    return false;
                }

                if (Lzcnt.LeadingZeroCount(uintArray[2]) != 32)
                {
                    Console.WriteLine("TestUInt32Containment failed on LeadingZeroCount");
                    return false;
                }

                if (Lzcnt.LeadingZeroCount(uintArray[*ptr + 2]) != 32)
                {
                    Console.WriteLine("TestUInt32Containment failed on LeadingZeroCount");
                    return false;
                }

            }

            uint* ptr1 = &value;
            if (Sse42.IsSupported)
            {
                if (Sse42.Crc32(0xffffffffU, (uint)0) != 0xb798b438U)
                {
                    Console.WriteLine("TestUInt32Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, value) != 0xb798b438U)
                {
                    Console.WriteLine("TestUInt32Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, *ptr1) != 0xb798b438U)
                {
                    Console.WriteLine("TestUInt32Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, uintArray[1]) != 0xb798b438U)
                {
                    Console.WriteLine("TestUInt32Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, uintArray[*ptr + 1]) != 0xb798b438U)
                {
                    Console.WriteLine("TestUInt32Containment failed on Crc32");
                    return false;
                }

                if (Sse42.Crc32(0xffffffffU, uintSF) != 0xb798b438U)
                {
                    Console.WriteLine("TestUInt32Containment failed on Crc32");
                    return false;
                }
            }

            return true;
        }

        static unsafe bool TestUInt64Containment()
        {
            ulong value = (ulong)0;
            ulong* ptr = &value;
            if (Lzcnt.X64.IsSupported)
            {
                if (Lzcnt.X64.LeadingZeroCount(*ptr) != 64)
                {
                    Console.WriteLine("TestUInt64Containment failed on LeadingZeroCount");
                    return false;
                }

                if (Lzcnt.X64.LeadingZeroCount(ulongArray[2]) != 64)
                {
                    Console.WriteLine("TestUInt64Containment failed on LeadingZeroCount");
                    return false;
                }

                if (Lzcnt.X64.LeadingZeroCount(ulongArray[*ptr + 2]) != 64)
                {
                    Console.WriteLine("TestUInt64Containment failed on LeadingZeroCount");
                    return false;
                }

            }

            ulong* ptr1 = &value;

            if (Sse42.X64.IsSupported)
            {
                if (Sse42.X64.Crc32(0xffffffffffffffffUL, 0) != 0x0000000073d74d75UL)
                {
                    Console.WriteLine("TestUInt64Containment failed on Crc32");
                    return false;
                }

                if (Sse42.X64.Crc32(0xffffffffffffffffUL, value) != 0x0000000073d74d75UL)
                {
                    Console.WriteLine("TestUInt64Containment failed on Crc32");
                    return false;
                }
                
                if (Sse42.X64.Crc32(0xffffffffffffffffUL, *ptr1) != 0x0000000073d74d75UL)
                {
                    Console.WriteLine("TestUInt64Containment failed on Crc32");
                    return false;
                }

                if (Sse42.X64.Crc32(0xffffffffffffffffUL, ulongArray[1]) != 0x0000000073d74d75UL)
                {
                    Console.WriteLine("TestUInt64Containment failed on Crc32");
                    return false;
                }

                if (Sse42.X64.Crc32(0xffffffffffffffffUL, ulongArray[*ptr + 1]) != 0x0000000073d74d75UL)
                {
                    Console.WriteLine("TestUInt64Containment failed on Crc32");
                    return false;
                }

                if (Sse42.X64.Crc32(0xffffffffffffffffUL, ulongSF) != 0x0000000073d74d75UL)
                {
                    Console.WriteLine("TestUInt64Containment failed on Crc32");
                    return false;
                }
            }

            return true;
        }
    }
}