Example #1
0
        public static unsafe int AVXVecMin(int[] x)
        {
            int len = x.Length;
            var min = Vector128.Create(int.MaxValue);

            fixed(int *pSource = x)
            {
                int i = 0;
                int lastBlockIndex = len - (len % 4);

                while (i < lastBlockIndex)
                {
                    min = Avx.Min(min, Avx.LoadVector128(pSource + i));
                    i  += 4;
                }
                var minValue = min.ToScalar();

                while (i < len)
                {
                    minValue = MinBranchFree(minValue, pSource[i]);
                    i       += 1;
                }
                return(minValue);
            }
        }
Example #2
0
        public static unsafe int AVXVecMinIndependent(int[] x)
        {
            int len  = x.Length;
            var min1 = Vector128.Create(int.MaxValue);
            var min2 = Vector128.Create(int.MaxValue);

            fixed(int *pSource = x)
            {
                int i = 0;
                int lastBlockIndex = len - (len % 8);

                while (i < lastBlockIndex)
                {
                    min1 = Avx.Min(min1, Avx.LoadVector128(pSource + i));
                    min2 = Avx.Min(min2, Avx.LoadVector128(pSource + i + 4));

                    i += 8;
                }
                var minValue = min1.ToScalar() + min2.ToScalar();

                while (i < len)
                {
                    minValue = MinBranchFree(minValue, pSource[i]);
                    i       += 1;
                }
                return(minValue);
            }
        }
Example #3
0
            public void RunStructFldScenario(SimpleBinaryOpTest__MinDouble testClass)
            {
                var result = Avx.Min(_fld1, _fld2);

                Unsafe.Write(testClass._dataTable.outArrayPtr, result);
                testClass.ValidateResult(_fld1, _fld2, testClass._dataTable.outArrayPtr);
            }
Example #4
0
        private static void MinAvx(ReadOnlySpan <float> a, float b, Span <float> s)
        {
            var remainder = a.Length & 7;
            var length    = a.Length - remainder;

            var scalar = Vector256.Create(b);

            fixed(float *ptr = a)
            {
                fixed(float *ptrS = s)
                {
                    for (var i = 0; i < length; i += 8)
                    {
                        var j = Avx.LoadVector256(ptr + i);

                        Avx.Store(ptrS + i, Avx.Min(j, scalar));
                    }
                }
            }

            if (remainder != 0)
            {
                MinNaive(a, b, s, length, a.Length);
            }
        }
Example #5
0
        public static int AVXMin(int x, int y)
        {
            var v1 = Vector128.CreateScalarUnsafe(x);
            var v2 = Vector128.CreateScalarUnsafe(y);

            return(Avx.Min(v1, v2).ToScalar());
        }
Example #6
0
        public void RunFldScenario()
        {
            var result = Avx.Min(_fld1, _fld2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_fld1, _fld2, _dataTable.outArrayPtr);
        }
Example #7
0
        public void RunStructLclFldScenario()
        {
            var test   = TestStruct.Create();
            var result = Avx.Min(test._fld1, test._fld2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
        }
Example #8
0
        // Minimum value.
        public static double Min(this IEnumerable <Vector256 <double> > @this)
        {
            var tmp    = @this.Aggregate((x, y) => Avx.Min(x, y));
            var tmp1   = Avx.Min(tmp.GetLower(), tmp.GetUpper());
            var result = Math.Min(tmp1.GetElement(0), tmp1.GetElement(1));

            return(result);
        }
Example #9
0
        public void RunLclFldScenario()
        {
            var test   = new SimpleBinaryOpTest__MinSingle();
            var result = Avx.Min(test._fld1, test._fld2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
        }
Example #10
0
        public void RunClassFldScenario()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunClassFldScenario));

            var result = Avx.Min(_fld1, _fld2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_fld1, _fld2, _dataTable.outArrayPtr);
        }
Example #11
0
        public void RunLclVarScenario_UnsafeRead()
        {
            var left   = Unsafe.Read <Vector256 <Single> >(_dataTable.inArray1Ptr);
            var right  = Unsafe.Read <Vector256 <Single> >(_dataTable.inArray2Ptr);
            var result = Avx.Min(left, right);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(left, right, _dataTable.outArrayPtr);
        }
Example #12
0
        public void RunLclVarScenario_LoadAligned()
        {
            var left   = Avx.LoadAlignedVector256((Single *)(_dataTable.inArray1Ptr));
            var right  = Avx.LoadAlignedVector256((Single *)(_dataTable.inArray2Ptr));
            var result = Avx.Min(left, right);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(left, right, _dataTable.outArrayPtr);
        }
Example #13
0
        public static Vector256 <double> Min(Vector256 <double> left, Vector256 <double> right)
        {
            if (Avx.IsSupported)
            {
                return(Avx.Min(left, right));
            }

            return(Min_Software(left, right));
        }
Example #14
0
        public void RunStructLclFldScenario()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunStructLclFldScenario));

            var test   = TestStruct.Create();
            var result = Avx.Min(test._fld1, test._fld2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
        }
Example #15
0
        public void RunBasicScenario_UnsafeRead()
        {
            var result = Avx.Min(
                Unsafe.Read <Vector256 <Single> >(_dataTable.inArray1Ptr),
                Unsafe.Read <Vector256 <Single> >(_dataTable.inArray2Ptr)
                );

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
        }
Example #16
0
        public void RunClassLclFldScenario()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunClassLclFldScenario));

            var test   = new SimpleBinaryOpTest__MinDouble();
            var result = Avx.Min(test._fld1, test._fld2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
        }
Example #17
0
        public void RunBasicScenario_LoadAligned()
        {
            var result = Avx.Min(
                Avx.LoadAlignedVector256((Single *)(_dataTable.inArray1Ptr)),
                Avx.LoadAlignedVector256((Single *)(_dataTable.inArray2Ptr))
                );

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
        }
Example #18
0
        public void RunClsVarScenario()
        {
            var result = Avx.Min(
                _clsVar1,
                _clsVar2
                );

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_clsVar1, _clsVar2, _dataTable.outArrayPtr);
        }
Example #19
0
        public void RunLclVarScenario_UnsafeRead()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunLclVarScenario_UnsafeRead));

            var op1    = Unsafe.Read <Vector256 <Single> >(_dataTable.inArray1Ptr);
            var op2    = Unsafe.Read <Vector256 <Single> >(_dataTable.inArray2Ptr);
            var result = Avx.Min(op1, op2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(op1, op2, _dataTable.outArrayPtr);
        }
Example #20
0
        public void RunLclVarScenario_UnsafeRead()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunLclVarScenario_UnsafeRead));

            var left   = Unsafe.Read <Vector256 <Double> >(_dataTable.inArray1Ptr);
            var right  = Unsafe.Read <Vector256 <Double> >(_dataTable.inArray2Ptr);
            var result = Avx.Min(left, right);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(left, right, _dataTable.outArrayPtr);
        }
Example #21
0
        public void RunLclVarScenario_LoadAligned()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunLclVarScenario_LoadAligned));

            var left   = Avx.LoadAlignedVector256((Double *)(_dataTable.inArray1Ptr));
            var right  = Avx.LoadAlignedVector256((Double *)(_dataTable.inArray2Ptr));
            var result = Avx.Min(left, right);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(left, right, _dataTable.outArrayPtr);
        }
Example #22
0
        public void RunLclVarScenario_LoadAligned()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunLclVarScenario_LoadAligned));

            var op1    = Avx.LoadAlignedVector256((Single *)(_dataTable.inArray1Ptr));
            var op2    = Avx.LoadAlignedVector256((Single *)(_dataTable.inArray2Ptr));
            var result = Avx.Min(op1, op2);

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(op1, op2, _dataTable.outArrayPtr);
        }
Example #23
0
        public void RunBasicScenario_UnsafeRead()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunBasicScenario_UnsafeRead));

            var result = Avx.Min(
                Unsafe.Read <Vector256 <Double> >(_dataTable.inArray1Ptr),
                Unsafe.Read <Vector256 <Double> >(_dataTable.inArray2Ptr)
                );

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
        }
Example #24
0
        public void RunBasicScenario_Load()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunBasicScenario_Load));

            var result = Avx.Min(
                Avx.LoadVector256((Double *)(_dataTable.inArray1Ptr)),
                Avx.LoadVector256((Double *)(_dataTable.inArray2Ptr))
                );

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
        }
Example #25
0
        public void RunStructLclFldScenario_Load()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunStructLclFldScenario_Load));

            var test   = TestStruct.Create();
            var result = Avx.Min(
                Avx.LoadVector256((Double *)(&test._fld1)),
                Avx.LoadVector256((Double *)(&test._fld2))
                );

            Unsafe.Write(_dataTable.outArrayPtr, result);
            ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
        }
Example #26
0
            public void RunStructFldScenario_Load(SimpleBinaryOpTest__MinDouble testClass)
            {
                fixed(Vector256 <Double> *pFld1 = &_fld1)
                fixed(Vector256 <Double> *pFld2 = &_fld2)
                {
                    var result = Avx.Min(
                        Avx.LoadVector256((Double *)(pFld1)),
                        Avx.LoadVector256((Double *)(pFld2))
                        );

                    Unsafe.Write(testClass._dataTable.outArrayPtr, result);
                    testClass.ValidateResult(_fld1, _fld2, testClass._dataTable.outArrayPtr);
                }
            }
Example #27
0
        public static unsafe double Min(this Matrix <double> matrix)
        {
            var i = 0;

            fixed(double *ptr = matrix.GetArray())
            {
                var span      = new Span <double>(ptr, matrix.Length);
                var minScalar = span[0];

                if (Avx.IsSupported)
                {
                    var minValues = stackalloc double[] { span[0], span[0], span[0], span[0] };
                    var min       = Avx.LoadVector256(minValues);
                    while (i < span.Length - 4)
                    {
                        var vector256 = Avx.LoadVector256(ptr + i);
                        min = Avx.Min(vector256, min);
                        i  += 4;
                    }

                    minScalar = min.MinVector256(4);
                }
                else if (Sse2.IsSupported)
                {
                    var minValues = stackalloc double[2] {
                        span[0], span[0]
                    };
                    var min = Sse2.LoadVector128(minValues);
                    while (i < span.Length - 2)
                    {
                        var vector128 = Sse2.LoadVector128(ptr + i);
                        min = Sse2.Min(vector128, min);
                        i  += 2;
                    }

                    minScalar = min.MinVector128(2);
                }

                while (i < span.Length)
                {
                    if (minScalar > span[i])
                    {
                        minScalar = span[i];
                    }
                    i++;
                }

                return(minScalar);
            }
        }
Example #28
0
        public void RunClassFldScenario_Load()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunClassFldScenario_Load));

            fixed(Vector256 <Double> *pFld1 = &_fld1)
            fixed(Vector256 <Double> *pFld2 = &_fld2)
            {
                var result = Avx.Min(
                    Avx.LoadVector256((Double *)(pFld1)),
                    Avx.LoadVector256((Double *)(pFld2))
                    );

                Unsafe.Write(_dataTable.outArrayPtr, result);
                ValidateResult(_fld1, _fld2, _dataTable.outArrayPtr);
            }
        }
Example #29
0
        public void RunClsVarScenario_Load()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunClsVarScenario_Load));

            fixed(Vector256 <Single> *pClsVar1 = &_clsVar1)
            fixed(Vector256 <Single> *pClsVar2 = &_clsVar2)
            {
                var result = Avx.Min(
                    Avx.LoadVector256((Single *)(pClsVar1)),
                    Avx.LoadVector256((Single *)(pClsVar2))
                    );

                Unsafe.Write(_dataTable.outArrayPtr, result);
                ValidateResult(_clsVar1, _clsVar2, _dataTable.outArrayPtr);
            }
        }
Example #30
0
        public void RunClassLclFldScenario_Load()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunClassLclFldScenario_Load));

            var test = new SimpleBinaryOpTest__MinDouble();

            fixed(Vector256 <Double> *pFld1 = &test._fld1)
            fixed(Vector256 <Double> *pFld2 = &test._fld2)
            {
                var result = Avx.Min(
                    Avx.LoadVector256((Double *)(pFld1)),
                    Avx.LoadVector256((Double *)(pFld2))
                    );

                Unsafe.Write(_dataTable.outArrayPtr, result);
                ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
            }
        }