summaryrefslogtreecommitdiff
path: root/tests/src/JIT/HardwareIntrinsics/Lzcnt.cs
blob: 9d40c08c5429c212efa87de458ede70e7d6ae772 (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
// 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.Runtime.Intrinsics.X86;

namespace IntelHardwareIntrinsicTest
{
    class Program
    {
        const int Pass = 100;
        const int Fail = 0;

        static int Main(string[] args)
        {
            ulong sl = 0, resl;
            int testResult = Pass;

            if (!Lzcnt.IsSupported || !Environment.Is64BitProcess)
            {
                try
                {
                    resl = Lzcnt.LeadingZeroCount(sl);
                    Console.WriteLine("Intrinsic Lzcnt.LeadingZeroCount is called on non-supported hardware.");
                    Console.WriteLine("Lzcnt.IsSupported " + Lzcnt.IsSupported);
                    Console.WriteLine("Environment.Is64BitProcess " + Environment.Is64BitProcess);
                    return Fail;
                }
                catch (PlatformNotSupportedException)
                {
                    testResult = Pass;
                }
            }


            if (Lzcnt.IsSupported)
            {
                if (Environment.Is64BitProcess)
                {
                    for (int i = 0; i < longLzcntTable.Length; i++)
                    {
                        sl = longLzcntTable[i].s;
                        resl = Lzcnt.LeadingZeroCount(sl);
                        if (resl != longLzcntTable[i].res)
                        {
                            Console.WriteLine("{0}: Inputs: 0x{1,16:x} Expected: 0x{3,16:x} actual: 0x{4,16:x}",
                                i, sl, longLzcntTable[i].res, resl);
                            testResult = Fail;
                        }
                    }
                }

                uint si, resi;
                for (int i = 0; i < intLzcntTable.Length; i++)
                {
                    si = intLzcntTable[i].s;
                    resi = Lzcnt.LeadingZeroCount(si);
                    if (resi != intLzcntTable[i].res)
                    {
                        Console.WriteLine("{0}: Inputs: 0x{1,16:x} Expected: 0x{3,16:x} actual: 0x{4,16:x}",
                            i, si, intLzcntTable[i].res, resi);
                        testResult = Fail;
                    }
                }
            }

            return testResult;
        }

        public struct LZCNT<T> where T : struct
        {
            public T s;
            public T res;
            public LZCNT(T a, T r)
            {
                this.s = a;
                this.res = r;
            }
        }

        public static LZCNT<ulong>[] longLzcntTable = {
            new LZCNT<ulong>(0x0000000000000000UL, 64),
            new LZCNT<ulong>(0x0000000000000001UL, 63),
            new LZCNT<ulong>(0xffffffffffffffffUL, 0),
            new LZCNT<ulong>(0xf000000000000000UL, 0),
            new LZCNT<ulong>(0x00050000000f423fUL, 13)
        };

        public static LZCNT<uint>[] intLzcntTable = {
            new LZCNT<uint>(0x00000000U, 32),
            new LZCNT<uint>(0x00000001U, 31),
            new LZCNT<uint>(0xffffffffU, 0),
            new LZCNT<uint>(0xf0000000U, 0),
            new LZCNT<uint>(0x0005423fU, 13)
        };
    }
}