summaryrefslogtreecommitdiff
path: root/tests/src/JIT/SIMD/VectorUtil.cs
blob: 6501aac767515c39b75a14f498d618ea1ff1d230 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

using System;
using System.Numerics;
using System.IO;

internal partial class VectorTest
{
    public static bool CheckValue<T>(T value, T expectedValue)
    {
        bool returnVal;
        if (typeof(T) == typeof(float))
        {
            returnVal = Math.Abs(((float)(object)value) - ((float)(object)expectedValue)) <= Single.Epsilon;
        }
        if (typeof(T) == typeof(double))
        {
            returnVal = Math.Abs(((double)(object)value) - ((double)(object)expectedValue)) <= Double.Epsilon;
        }
        else
        {
            returnVal = value.Equals(expectedValue);
        }
        if (returnVal == false)
        {
            Console.WriteLine("CheckValue failed for " + expectedValue + " of type " + typeof(T).ToString());
        }
        return returnVal;
    }

    private static bool CheckVector<T>(Vector<T> V, T value) where T : struct, IComparable<T>, IEquatable<T>
    {
        for (int i = 0; i < Vector<T>.Count; i++)
        {
            if (!(CheckValue<T>(V[i], value)))
            {
                return false;
            }
        }
        return true;
    }

    public static T GetValueFromInt<T>(int value)
    {
        if (typeof(T) == typeof(float))
        {
            float floatValue = (float)value;
            return (T)(object)floatValue;
        }
        if (typeof(T) == typeof(double))
        {
            double doubleValue = (double)value;
            return (T)(object)doubleValue;
        }
        if (typeof(T) == typeof(int))
        {
            return (T)(object)value;
        }
        if (typeof(T) == typeof(uint))
        {
            uint uintValue = (uint)value;
            return (T)(object)uintValue;
        }
        if (typeof(T) == typeof(long))
        {
            long longValue = (long)value;
            return (T)(object)longValue;
        }
        if (typeof(T) == typeof(ulong))
        {
            ulong longValue = (ulong)value;
            return (T)(object)longValue;
        }
        if (typeof(T) == typeof(ushort))
        {
            return (T)(object)(ushort)value;
        }
        if (typeof(T) == typeof(byte))
        {
            return (T)(object)(byte)value;
        }
        if (typeof(T) == typeof(short))
        {
            return (T)(object)(short)value;
        }
        if (typeof(T) == typeof(sbyte))
        {
            return (T)(object)(sbyte)value;
        }
        else
        {
            throw new ArgumentException();
        }
    }

    private static void VectorPrint<T>(string mesg, Vector<T> v) where T : struct, IComparable<T>, IEquatable<T>
    {
        Console.Write(mesg + "[");
        for (int i = 0; i < Vector<T>.Count; i++)
        {
            Console.Write(" " + v[i]);
            if (i < (Vector<T>.Count - 1)) Console.Write(",");
        }
        Console.WriteLine(" ]");
    }

    private static T Add<T>(T left, T right) where T : struct, IComparable<T>, IEquatable<T>
    {
        if (typeof(T) == typeof(float))
        {
            return (T)(object)(((float)(object)left) + ((float)(object)right));
        }
        if (typeof(T) == typeof(double))
        {
            return (T)(object)(((double)(object)left) + ((double)(object)right));
        }
        if (typeof(T) == typeof(int))
        {
            return (T)(object)(((int)(object)left) + ((int)(object)right));
        }
        if (typeof(T) == typeof(uint))
        {
            return (T)(object)(((uint)(object)left) + ((uint)(object)right));
        }
        if (typeof(T) == typeof(ushort))
        {
            return (T)(object)(((ushort)(object)left) + ((ushort)(object)right));
        }
        if (typeof(T) == typeof(byte))
        {
            return (T)(object)(((byte)(object)left) + ((byte)(object)right));
        }
        if (typeof(T) == typeof(short))
        {
            return (T)(object)(((short)(object)left) + ((short)(object)right));
        }
        if (typeof(T) == typeof(sbyte))
        {
            return (T)(object)(((sbyte)(object)left) + ((sbyte)(object)right));
        }
        if (typeof(T) == typeof(long))
        {
            return (T)(object)(((long)(object)left) + ((long)(object)right));
        }
        if (typeof(T) == typeof(ulong))
        {
            return (T)(object)(((ulong)(object)left) + ((ulong)(object)right));
        }
        else
        {
            throw new ArgumentException();
        }
    }
    private static T Multiply<T>(T left, T right) where T : struct, IComparable<T>, IEquatable<T>
    {
        if (typeof(T) == typeof(float))
        {
            return (T)(object)(((float)(object)left) * ((float)(object)right));
        }
        if (typeof(T) == typeof(double))
        {
            return (T)(object)(((double)(object)left) * ((double)(object)right));
        }
        if (typeof(T) == typeof(int))
        {
            return (T)(object)(((int)(object)left) * ((int)(object)right));
        }
        if (typeof(T) == typeof(uint))
        {
            return (T)(object)(((uint)(object)left) * ((uint)(object)right));
        }
        if (typeof(T) == typeof(ushort))
        {
            return (T)(object)(((ushort)(object)left) * ((ushort)(object)right));
        }
        if (typeof(T) == typeof(byte))
        {
            return (T)(object)(((byte)(object)left) * ((byte)(object)right));
        }
        if (typeof(T) == typeof(short))
        {
            return (T)(object)(((short)(object)left) * ((short)(object)right));
        }
        if (typeof(T) == typeof(sbyte))
        {
            return (T)(object)(((sbyte)(object)left) * ((sbyte)(object)right));
        }
        if (typeof(T) == typeof(long))
        {
            return (T)(object)(((long)(object)left) * ((long)(object)right));
        }
        if (typeof(T) == typeof(ulong))
        {
            return (T)(object)(((ulong)(object)left) * ((ulong)(object)right));
        }
        else
        {
            throw new ArgumentException();
        }
    }

    public static T[] GetRandomArray<T>(int size, Random random)
        where T : struct, IComparable<T>, IEquatable<T>
    {
        T[] result = new T[size];
        for (int i = 0; i < size; i++)
        {
            int data = random.Next(100);
            result[i] = GetValueFromInt<T>(data);
        }
        return result;
    }

}

class JitLog : IDisposable
{
    FileStream      fileStream;
    bool            simdIntrinsicsSupported;

    private static String GetLogFileName()
    {
        String jitLogFileName = Environment.GetEnvironmentVariable("COMPlus_JitFuncInfoLogFile");
        return jitLogFileName;
    }

    public JitLog()
    {
        fileStream = null;
        simdIntrinsicsSupported = Vector.IsHardwareAccelerated;
        String jitLogFileName = GetLogFileName();
        if (jitLogFileName == null)
        {
            Console.WriteLine("No JitLogFile");
            return;
        }
        if (!File.Exists(jitLogFileName))
        {
            Console.WriteLine("JitLogFile " + jitLogFileName + " not found.");
            return;
        }
        File.Copy(jitLogFileName, "Temp.log", true);
        fileStream = new FileStream("Temp.log", FileMode.Open, FileAccess.Read, FileShare.Read);
    }

    public void Dispose()
    {
        if (fileStream != null)
        {
            fileStream.Dispose();
        }
    }
    public bool IsEnabled()
    {
        return (fileStream != null);
    }

    //------------------------------------------------------------------------
    // Check: Check to see whether 'method' was recognized as an intrinsic by the JIT.
    //
    // Arguments:
    //    method - The method name, as a string
    //
    // Return Value:
    //    If the JitLog is not enabled (either the environment variable is not set,
    //    or the file was not found, e.g. if the JIT doesn't support it):
    //     - Returns true
    //    If SIMD intrinsics are enabled:
    //     - Returns true if the method was NOT compiled, otherwise false
    //    Else (if SIMD intrinsics are NOT enabled):
    //     - Returns true.
    //       Note that it might be useful to return false if the method was not compiled,
    //       but it will not be logged as compiled if it is inlined.
    //
    // Assumptions:
    //    The JitLog constructor queries Vector.IsHardwareAccelerated to determine
    //    if SIMD intrinsics are enabled, and depends on its correctness.
    //
    // Notes:
    //    It searches for the string verbatim. If 'method' is not fully qualified
    //    or its signature is not provided, it may result in false matches.
    //
    // Example:
    //    CheckJitLog("System.Numerics.Vector4:op_Addition(struct,struct):struct");
    //
    public bool Check(String method)
    {
        // Console.WriteLine("Checking for " + method + ":");
        if (!IsEnabled())
        {
            Console.WriteLine("JitLog not enabled.");
            return true;
        }
        try
        {
            fileStream.Position = 0;
            StreamReader reader = new StreamReader(fileStream);
            bool methodFound = false;
            while ((reader.Peek() >= 0) && (methodFound == false))
            {
                String s = reader.ReadLine();
                if (s.IndexOf(method) != -1)
                {
                    methodFound = true;
                }
            }
            if (simdIntrinsicsSupported && methodFound)
            {
                Console.WriteLine("Method " + method + " was compiled but should not have been");
                return false;
            }
            // Useful when developing / debugging just to be sure that we reached here:
            // Console.WriteLine(method + ((methodFound) ? " WAS COMPILED" : " WAS NOT COMPILED"));
            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error checking JitLogFile: " + e.Message);
            return false;
        }
    }

    // Check: Check to see Vector<'elementType'>:'method' was recognized as an
    //        intrinsic by the JIT.
    //
    // Arguments:
    //    method - The method name, without its containing type (which is Vector<T>)
    //    elementType - The type with which Vector<T> is instantiated.
    //
    // Return Value:
    //    See the other overload, above.
    //
    // Assumptions:
    //    The JitLog constructor queries Vector.IsHardwareAccelerated to determine
    //    if SIMD intrinsics are enabled, and depends on its correctness.
    //
    // Notes:
    //    It constructs the search string based on the way generic types are currently
    //    dumped by the CLR. If the signature is not provided for the method, it
    //    may result in false matches.
    //
    // Example:
    //    CheckJitLog("op_Addition(struct,struct):struct", "Single");
    //
    public bool Check(String method, String elementType)
    {
        String checkString = "System.Numerics.Vector`1[" + elementType + "][System." + elementType + "]:" + method;
        return Check(checkString);
    }
}