Example #1
0
        public static int CountHexDigits(ulong value)
        {
            if (Lzcnt.IsSupported && IntPtr.Size == 8)
            {
                int right = 64 - (int)Lzcnt.LeadingZeroCount(value | 1);
                return((right + 3) >> 2);
            }

            int digits = 1;

            if (value > 0xFFFFFFFF)
            {
                digits += 8;
                value >>= 0x20;
            }
            if (value > 0xFFFF)
            {
                digits += 4;
                value >>= 0x10;
            }
            if (value > 0xFF)
            {
                digits += 2;
                value >>= 0x8;
            }
            if (value > 0xF)
            {
                digits++;
            }

            return(digits);
        }
Example #2
0
        public static int Log2(uint value)
        {
            // Enforce conventional contract 0->0 (Log(0) is undefined)
            if (value == 0)
            {
                return(0);
            }

            // value    lzcnt   actual  expected
            // ..0000   32      0        0 (by convention, guard clause)
            // ..0001   31      31-31    0
            // ..0010   30      31-30    1
            // 0010..    2      31-2    29
            // 0100..    1      31-1    30
            // 1000..    0      31-0    31
            if (Lzcnt.IsSupported)
            {
                // LZCNT contract is 0->32
                return(31 - (int)Lzcnt.LeadingZeroCount(value));
            }

            if (ArmBase.IsSupported)
            {
                return(31 - ArmBase.LeadingZeroCount(value));
            }

            // Fallback contract is 0->0
            return(Log2SoftwareFallback(value));
        }
 public void UseLeadingZeroOpNoSum()
 {
     for (int i = 0; i < N; i++)
     {
         var idx = 28 - Lzcnt.LeadingZeroCount(((uint)i - 1) | 15);
     }
 }
Example #4
0
 public static uint LeadingZeroCount(byte value)
 {
     if (Lzcnt.IsSupported)
     {
         return(Lzcnt.LeadingZeroCount((uint)value << 24));
     }
     return(default);
Example #5
0
        public static int Log2(uint value)
        {
            // The 0->0 contract is fulfilled by setting the LSB to 1.
            // Log(1) is 0, and setting the LSB for values > 1 does not change the log2 result.
            value |= 1;

            // value    lzcnt   actual  expected
            // ..0001   31      31-31    0
            // ..0010   30      31-30    1
            // 0010..    2      31-2    29
            // 0100..    1      31-1    30
            // 1000..    0      31-0    31
            if (Lzcnt.IsSupported)
            {
                return(31 ^ (int)Lzcnt.LeadingZeroCount(value));
            }

            if (ArmBase.IsSupported)
            {
                return(31 ^ ArmBase.LeadingZeroCount(value));
            }

            // BSR returns the log2 result directly. However BSR is slower than LZCNT
            // on AMD processors, so we leave it as a fallback only.
            if (X86Base.IsSupported)
            {
                return((int)X86Base.BitScanReverse(value));
            }

            // Fallback contract is 0->0
            return(Log2SoftwareFallback(value));
        }
Example #6
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);
        }
Example #7
0
        public static int LeadingZeroCount(uint value)
        {
            if (Lzcnt.IsSupported)
            {
                // LZCNT contract is 0->32
                return((int)Lzcnt.LeadingZeroCount(value));
            }

            if (ArmBase.IsSupported)
            {
                return(ArmBase.LeadingZeroCount(value));
            }

            // Unguarded fallback contract is 0->31, BSR contract is 0->undefined
            if (value == 0)
            {
                return(32);
            }

            if (X86Base.IsSupported)
            {
                // LZCNT returns index starting from MSB, whereas BSR gives the index from LSB.
                // 31 ^ BSR here is equivalent to 31 - BSR since the BSR result is always between 0 and 31.
                // This saves an instruction, as subtraction from constant requires either MOV/SUB or NEG/ADD.
                return(31 ^ (int)X86Base.BitScanReverse(value));
            }

            return(31 ^ Log2SoftwareFallback(value));
        }
Example #8
0
        /// <summary>
        /// Calculates the number of trailing zeros in the given unsigned integer.
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static int CountLeadingZeros(this uint n)
        {
            if (n == 0)
            {
                return(32);
            }

#if NETCOREAPP3_0
            if (Lzcnt.IsSupported)
            {
                return((int)Lzcnt.LeadingZeroCount(n));
            }
#endif

            // do the smearing
            n |= n >> 1;
            n |= n >> 2;
            n |= n >> 4;
            n |= n >> 8;
            n |= n >> 16;

            // count the ones
            n -= n >> 1 & 0x55555555;
            n  = (n >> 2 & 0x33333333) + (n & 0x33333333);
            n  = (n >> 4) + n & 0x0f0f0f0f;
            n += n >> 8;
            n += n >> 16;
            return((int)(sizeof(uint) * 8 - (n & 0x0000003f)));
        }
Example #9
0
        public static int Log2(uint value)
        {
            // Enforce conventional contract 0->0 (Log(0) is undefined)
            if (value == 0)
            {
                return(0);
            }

            // value    lzcnt   actual  expected
            // ..0000   32      0        0 (by convention, guard clause)
            // ..0001   31      31-31    0
            // ..0010   30      31-30    1
            // 0010..    2      31-2    29
            // 0100..    1      31-1    30
            // 1000..    0      31-0    31
            if (Lzcnt.IsSupported)
            {
                return(31 ^ (int)Lzcnt.LeadingZeroCount(value));
            }

            if (ArmBase.IsSupported)
            {
                return(31 ^ ArmBase.LeadingZeroCount(value));
            }

            // BSR returns the answer we're looking for directly.
            // However BSR is much slower than LZCNT on AMD processors, so we leave it as a fallback only.
            if (X86Base.IsSupported)
            {
                return((int)X86Base.BitScanReverse(value));
            }

            // Fallback contract is 0->0
            return(Log2SoftwareFallback(value));
        }
Example #10
0
        private static byte Clz(uint x)
        {
            byte result = 255;
            var  arch   = RuntimeInformation.ProcessArchitecture;

            if (arch is Architecture.X86 or Architecture.X64 && Lzcnt.IsSupported)
            {
                result = (byte)Lzcnt.LeadingZeroCount(x);
            }
Example #11
0
 public int[] LeadingZeroCount_HW()
 {
     int[] results = new int[100000];
     for (uint i = 1; i < results.Length; i++)
     {
         results[i] = (int)Lzcnt.LeadingZeroCount(i);
     }
     return(results);
 }
Example #12
0
        public static uint NextPowerOfTwoExponent(uint v)
        {
            if (Lzcnt.IsSupported)
            {
                return(32 - Lzcnt.LeadingZeroCount(--v));
            }

            throw new NotImplementedException();
        }
        public void UseLeadingZeroOp()
        {
            var sum = 0UL;

            for (int i = 0; i < N; i++)
            {
                var idx = 28 - Lzcnt.LeadingZeroCount(((uint)i - 1) | 15);
                sum += idx;
            }
        }
Example #14
0
        public unsafe void NativeLeadingZeroCountX86()
        {
            uint count = 0;

            for (int i = 0; i < N; i++)
            {
                count += Lzcnt.LeadingZeroCount(uintValue);
            }
            Console.WriteLine($"NativeLeadingZeroCountX86:{count}");
        }
Example #15
0
 public void SelectBucketIndexTest2()
 {
     for (int i = 0; i < 100_000_000; i++)
     {
         var minimumLength = i;
         var expected      = (uint)BitOperations.Log2((uint)minimumLength - 1 | 15) - 3;
         var actual        = 28 - Lzcnt.LeadingZeroCount(((uint)i - 1) | 15);
         Assert.AreEqual(expected, actual);
     }
 }
Example #16
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 unsafe ReadOnlyProposal[] ForVectorized()
        {
            ProposalResult p = ProposalBuilder.GetSortedVectorizedInsurances();

            var insuranceId = Vector256.Create(SearchedInsuranceId);

            fixed(int *iip = p.InsuranceIds)
            {
                int i      = 0;
                int length = p.InsuranceIds.Length - Vector256 <int> .Count + 1;
                int mask   = 8;

                while (mask == 8 && i < length)
                {
                    mask = (int)Lzcnt.LeadingZeroCount(
                        (uint)Avx2.MoveMask(
                            Vector256.AsByte(
                                Avx2.CompareEqual(
                                    Avx2.LoadVector256(iip + i),
                                    insuranceId
                                    )
                                ).Reverse()
                            )
                        ) >> 2;
                    i += Vector256 <int> .Count;
                }
                i -= Vector256 <int> .Count;

                int initial = i + mask;

                if (initial == p.InsuranceIds.Length)
                {
                    return(Array.Empty <ReadOnlyProposal>());
                }

                mask = 0;
                while (mask == 0 && i < length)
                {
                    mask = (int)Lzcnt.LeadingZeroCount(
                        (uint)Avx2.MoveMask(
                            Vector256.AsByte(
                                Avx2.CompareEqual(
                                    Avx2.LoadVector256(iip + i),
                                    insuranceId
                                    )
                                )
                            )
                        ) >> 2;
                    i += Vector256 <int> .Count;
                }
                return(p.Proposals.AsSpan(initial, i - mask - initial).ToArray());
            }
        }
Example #18
0
        // removed, not testing loop speed
        //[Benchmark]
        public unsafe ReadOnlyProposal[] PositionalSortedVectorized()
        {
            ProposalResult[] proposals = ProposalBuilder.GetPositionalSortedVectorizedInsurances();
            ProposalResult   p         = proposals[SearchedInsuranceId];

            var minPremium = Vector256.Create(decimal.ToOACurrency(SearchedNetPremium));

            fixed(long *npp = p.NetPremiums)
            {
                int i       = 0;
                int initial = 0;

                for (; i < p.NetPremiums.Length - Vector256 <long> .Count + 1; i += Vector256 <long> .Count)
                {
                    int mask = (int)Lzcnt.LeadingZeroCount(
                        (uint)Avx2.MoveMask(
                            Vector256.AsByte(
                                Avx2.CompareGreaterThan(
                                    Avx2.LoadVector256(npp + i),
                                    minPremium
                                    )
                                ).Reverse()
                            )
                        ) >> 3;

                    if (mask != 8)
                    {
                        initial = i + mask;
                        break;
                    }
                }
                for (; i < p.NetPremiums.Length - Vector256 <long> .Count + 1; i += Vector256 <long> .Count)
                {
                    int mask = (int)Lzcnt.LeadingZeroCount(
                        (uint)Avx2.MoveMask(
                            Vector256.AsByte(
                                Avx2.CompareGreaterThan(
                                    Avx2.LoadVector256(npp + i),
                                    minPremium
                                    )
                                )
                            )
                        ) >> 3;
                    if (mask != 0)
                    {
                        int length = i + Vector256 <long> .Count - mask - initial;
                        return(p.Proposals.AsSpan(initial, length).ToArray());
                    }
                }
                return(p.Proposals.AsSpan(initial).ToArray());
            }
        }
        public void UseLeadingZeroCount()
        {
            var sum = 0UL;

            for (int i = 0; i < N; i++)
            {
                var idx = 28 - Lzcnt.LeadingZeroCount((uint)i);
                if (i == (8 << (int)idx))
                {
                    idx--;
                }
                sum += idx;
            }
        }
Example #20
0
 public void SelectBucketIndexTest()
 {
     for (int i = 16; i < 100_000_000; i++)
     {
         var minimumLength = i;
         var v1            = 28 - Lzcnt.LeadingZeroCount((uint)minimumLength);
         if (minimumLength == (8 << (int)v1))
         {
             v1--;
         }
         var v2 = (uint)BitOperations.Log2((uint)minimumLength - 1 | 15) - 3;
         var v3 = 28 - Lzcnt.LeadingZeroCount(((uint)i - 1) | 15);
         Assert.AreEqual(v1, v2);
         Assert.AreEqual(v1, v3);
     }
 }
Example #21
0
        public static int LeadingZeroCount(uint value)
        {
            if (Lzcnt.IsSupported)
            {
                // LZCNT contract is 0->32
                return((int)Lzcnt.LeadingZeroCount(value));
            }

            // Unguarded fallback contract is 0->31
            if (value == 0)
            {
                return(32);
            }

            return(31 - Log2SoftwareFallback(value));
        }
Example #22
0
        public static int LeadingZeroCount(uint value)
        {
            if (Lzcnt.IsSupported)
            {
                // Note that LZCNT contract specifies 0->32
                return((int)Lzcnt.LeadingZeroCount(value));
            }

            // Software fallback has behavior 0->0, so special-case to match intrinsic path 0->32
            if (value == 0)
            {
                return(32);
            }

            return(31 - Log2(value));
        }
Example #23
0
        public static uint NextPowerOfTwo(uint v)
        {
            if (Lzcnt.IsSupported)
            {
                return((uint)1 << (int)(32 - Lzcnt.LeadingZeroCount(--v)));
            }

            if (v != 0U)
            {
                v--;
            }
            v |= v >> 1;
            v |= v >> 2;
            v |= v >> 4;
            v |= v >> 8;
            v |= v >> 16;
            v++;

            return(v);
        }
Example #24
0
        public override ulong Run(CancellationToken cancellationToken)
        {
            if (!Lzcnt.IsSupported)
            {
                return(0uL);
            }

            var iterations = 0uL;

            while (!cancellationToken.IsCancellationRequested)
            {
                for (var i = 0; i < LENGTH; i++)
                {
                    data = Lzcnt.LeadingZeroCount(data);
                }

                iterations++;
            }

            return(iterations);
        }
Example #25
0
        internal static int SelectBucketIndex(int bufferSize)
        {
            Debug.Assert(bufferSize >= 0);
            if (Lzcnt.IsSupported)
            {
                uint bits = ((uint)bufferSize - 1) >> 4;
                return(32 - (int)Lzcnt.LeadingZeroCount(bits));
            }

            // bufferSize of 0 will underflow here, causing a huge
            // index which the caller will discard because it is not
            // within the bounds of the bucket array.
            uint bitsRemaining = ((uint)bufferSize - 1) >> 4;

            int poolIndex = 0;

            if (bitsRemaining > 0xFFFF)
            {
                bitsRemaining >>= 16; poolIndex = 16;
            }
            if (bitsRemaining > 0xFF)
            {
                bitsRemaining >>= 8;  poolIndex += 8;
            }
            if (bitsRemaining > 0xF)
            {
                bitsRemaining >>= 4;  poolIndex += 4;
            }
            if (bitsRemaining > 0x3)
            {
                bitsRemaining >>= 2;  poolIndex += 2;
            }
            if (bitsRemaining > 0x1)
            {
                bitsRemaining >>= 1;  poolIndex += 1;
            }

            return(poolIndex + (int)bitsRemaining);
        }
        public unsafe static int LeadingZeroCount(UInt32[] value)
        {
            uint cnt = 0;

            fixed(UInt32 *v = value)
            {
                for (int i = value.Length - 1; i >= 0; i--)
                {
                    if (v[i] == 0)
                    {
                        cnt += UIntUtil.UInt32Bits;
                    }
                    else
                    {
                        cnt += Lzcnt.LeadingZeroCount(v[i]);
                        break;
                    }
                }
            }

            return(checked ((int)cnt));
        }
Example #27
0
        public static int Log2(uint value)
        {
            // value    lzcnt   actual  expected
            // ..0000   32      0        0 (by convention, guard clause)
            // ..0001   31      31-31    0
            // ..0010   30      31-30    1
            // 0010..    2      31-2    29
            // 0100..    1      31-1    30
            // 1000..    0      31-0    31
            if (Lzcnt.IsSupported)
            {
                // Enforce conventional contract 0->0 (since Log(0) is undefined)
                if (value == 0)
                {
                    return(0);
                }

                // Note that LZCNT contract specifies 0->32
                return(31 - (int)Lzcnt.LeadingZeroCount(value));
            }

            // Already has contract 0->0, without branching
            return(Log2SoftwareFallback(value));
        }
Example #28
0
    static int Main()
    {
        s_success = true;

        // We expect the AOT compiler generated HW intrinsics with the following characteristics:
        //
        // * TRUE = IsSupported assumed to be true, no runtime check
        // * NULL = IsSupported is a runtime check, code should be behind the check or bad things happen
        // * FALSE = IsSupported assumed to be false, no runtime check, PlatformNotSupportedException if used
        //
        // The test is compiled with multiple defines to test this.

#if BASELINE_INTRINSICS
        bool vectorsAccelerated = true;
        int  byteVectorLength   = 16;
        bool?Sse2AndBelow       = true;
        bool?Sse3Group          = null;
        bool?AesLzPcl           = null;
        bool?Sse4142            = null;
        bool?PopCnt             = null;
        bool?Avx12    = false;
        bool?FmaBmi12 = false;
        bool?Avxvnni  = false;
#elif NON_VEX_INTRINSICS
        bool vectorsAccelerated = true;
        int  byteVectorLength   = 16;
        bool?Sse2AndBelow       = true;
        bool?Sse3Group          = true;
        bool?AesLzPcl           = null;
        bool?Sse4142            = true;
        bool?PopCnt             = null;
        bool?Avx12    = false;
        bool?FmaBmi12 = false;
        bool?Avxvnni  = false;
#elif VEX_INTRINSICS
        bool vectorsAccelerated = true;
        int  byteVectorLength   = 32;
        bool?Sse2AndBelow       = true;
        bool?Sse3Group          = true;
        bool?AesLzPcl           = null;
        bool?Sse4142            = true;
        bool?PopCnt             = null;
        bool?Avx12    = true;
        bool?FmaBmi12 = null;
        bool?Avxvnni  = null;
#else
#error Who dis?
#endif

        if (vectorsAccelerated != Vector.IsHardwareAccelerated)
        {
            throw new Exception($"Vectors HW acceleration state unexpected - expected {vectorsAccelerated}, got {Vector.IsHardwareAccelerated}");
        }

        if (byteVectorLength != Vector <byte> .Count)
        {
            throw new Exception($"Unexpected vector length - expected {byteVectorLength}, got {Vector<byte>.Count}");
        }

        Check("Sse", Sse2AndBelow, &SseIsSupported, Sse.IsSupported, () => Sse.Subtract(Vector128 <float> .Zero, Vector128 <float> .Zero).Equals(Vector128 <float> .Zero));
        Check("Sse.X64", Sse2AndBelow, &SseX64IsSupported, Sse.X64.IsSupported, () => Sse.X64.ConvertToInt64WithTruncation(Vector128 <float> .Zero) == 0);

        Check("Sse2", Sse2AndBelow, &Sse2IsSupported, Sse2.IsSupported, () => Sse2.Extract(Vector128 <ushort> .Zero, 0) == 0);
        Check("Sse2.X64", Sse2AndBelow, &Sse2X64IsSupported, Sse2.X64.IsSupported, () => Sse2.X64.ConvertToInt64(Vector128 <double> .Zero) == 0);

        Check("Sse3", Sse3Group, &Sse3IsSupported, Sse3.IsSupported, () => Sse3.MoveHighAndDuplicate(Vector128 <float> .Zero).Equals(Vector128 <float> .Zero));
        Check("Sse3.X64", Sse3Group, &Sse3X64IsSupported, Sse3.X64.IsSupported, null);

        Check("Ssse3", Sse3Group, &Ssse3IsSupported, Ssse3.IsSupported, () => Ssse3.Abs(Vector128 <short> .Zero).Equals(Vector128 <ushort> .Zero));
        Check("Ssse3.X64", Sse3Group, &Ssse3X64IsSupported, Ssse3.X64.IsSupported, null);

        Check("Sse41", Sse4142, &Sse41IsSupported, Sse41.IsSupported, () => Sse41.Max(Vector128 <int> .Zero, Vector128 <int> .Zero).Equals(Vector128 <int> .Zero));
        Check("Sse41.X64", Sse4142, &Sse41X64IsSupported, Sse41.X64.IsSupported, () => Sse41.X64.Extract(Vector128 <long> .Zero, 0) == 0);

        Check("Sse42", Sse4142, &Sse42IsSupported, Sse42.IsSupported, () => Sse42.Crc32(0, 0) == 0);
        Check("Sse42.X64", Sse4142, &Sse42X64IsSupported, Sse42.X64.IsSupported, () => Sse42.X64.Crc32(0, 0) == 0);

        Check("Aes", AesLzPcl, &AesIsSupported, Aes.IsSupported, () => Aes.KeygenAssist(Vector128 <byte> .Zero, 0).Equals(Vector128.Create((byte)99)));
        Check("Aes.X64", AesLzPcl, &AesX64IsSupported, Aes.X64.IsSupported, null);

        Check("Avx", Avx12, &AvxIsSupported, Avx.IsSupported, () => Avx.Add(Vector256 <double> .Zero, Vector256 <double> .Zero).Equals(Vector256 <double> .Zero));
        Check("Avx.X64", Avx12, &AvxX64IsSupported, Avx.X64.IsSupported, null);

        Check("Avx2", Avx12, &Avx2IsSupported, Avx2.IsSupported, () => Avx2.Abs(Vector256 <int> .Zero).Equals(Vector256 <uint> .Zero));
        Check("Avx2.X64", Avx12, &Avx2X64IsSupported, Avx2.X64.IsSupported, null);

        Check("Bmi1", FmaBmi12, &Bmi1IsSupported, Bmi1.IsSupported, () => Bmi1.AndNot(0, 0) == 0);
        Check("Bmi1.X64", FmaBmi12, &Bmi1X64IsSupported, Bmi1.X64.IsSupported, () => Bmi1.X64.AndNot(0, 0) == 0);

        Check("Bmi2", FmaBmi12, &Bmi2IsSupported, Bmi2.IsSupported, () => Bmi2.MultiplyNoFlags(0, 0) == 0);
        Check("Bmi2.X64", FmaBmi12, &Bmi2X64IsSupported, Bmi2.X64.IsSupported, () => Bmi2.X64.MultiplyNoFlags(0, 0) == 0);

        Check("Fma", FmaBmi12, &FmaIsSupported, Fma.IsSupported, () => Fma.MultiplyAdd(Vector128 <float> .Zero, Vector128 <float> .Zero, Vector128 <float> .Zero).Equals(Vector128 <float> .Zero));
        Check("Fma.X64", FmaBmi12, &FmaX64IsSupported, Fma.X64.IsSupported, null);

        Check("Lzcnt", AesLzPcl, &LzcntIsSupported, Lzcnt.IsSupported, () => Lzcnt.LeadingZeroCount(0) == 32);
        Check("Lzcnt.X64", AesLzPcl, &LzcntX64IsSupported, Lzcnt.X64.IsSupported, () => Lzcnt.X64.LeadingZeroCount(0) == 64);

        Check("Pclmulqdq", AesLzPcl, &PclmulqdqIsSupported, Pclmulqdq.IsSupported, () => Pclmulqdq.CarrylessMultiply(Vector128 <long> .Zero, Vector128 <long> .Zero, 0).Equals(Vector128 <long> .Zero));
        Check("Pclmulqdq.X64", AesLzPcl, &PclmulqdqX64IsSupported, Pclmulqdq.X64.IsSupported, null);

        Check("Popcnt", PopCnt, &PopcntIsSupported, Popcnt.IsSupported, () => Popcnt.PopCount(0) == 0);
        Check("Popcnt.X64", PopCnt, &PopcntX64IsSupported, Popcnt.X64.IsSupported, () => Popcnt.X64.PopCount(0) == 0);

        Check("AvxVnni", Avxvnni, &AvxVnniIsSupported, AvxVnni.IsSupported, () => AvxVnni.MultiplyWideningAndAdd(Vector128 <int> .Zero, Vector128 <byte> .Zero, Vector128 <sbyte> .Zero).Equals(Vector128 <int> .Zero));
        Check("AvxVnni.X64", Avxvnni, &AvxVnniX64IsSupported, AvxVnni.X64.IsSupported, null);

        return(s_success ? 100 : 1);
    }
Example #29
0
        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);
        }
Example #30
0
        public static uint Log2(uint value)
        {
            Debug.Assert(value > 0);

            return(32 - 1 - Lzcnt.LeadingZeroCount(value));
        }