Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Lzcnt.cs « Lzcnt « X86 « HardwareIntrinsics « JIT « tests « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 276abdf30398c8331caf687f39b277f15e67fce7 (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
// 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.Reflection;
using System.Runtime.Intrinsics.X86;

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

        static int Main(string[] args)
        {
            int testResult = Pass;

            if (Lzcnt.IsSupported)
            {

                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;
                    }

                    resi = Convert.ToUInt32(typeof(Lzcnt).GetMethod(nameof(Lzcnt.LeadingZeroCount), new Type[] { si.GetType() }).Invoke(null, new object[] { si }));
                    if (resi != intLzcntTable[i].res)
                    {
                        Console.WriteLine("{0}: Inputs: 0x{1,16:x} Expected: 0x{3,16:x} actual: 0x{4,16:x} - Reflection",
                            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<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)
        };
    }
}