示例#1
0
        public static void CrossProduct3D_Theory(Vector256 <double> left, Vector256 <double> right, Vector256 <double> discard, Vector3d expected)
        {
            Vector256 <double> result = Vector.CrossProduct3D(left, right);

            Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}");
        }
示例#2
0
 private void ValidateResult(Vector256 <Int64> result, Int64 expectedValue, [CallerMemberName] string method = "")
 {
     Int64[] resultElements = new Int64[ElementCount];
     Unsafe.WriteUnaligned(ref Unsafe.As <Int64, byte>(ref resultElements[0]), result);
     ValidateResult(resultElements, expectedValue, method);
 }
示例#3
0
        public static void Inequality_Theory(Vector256 <double> left, Vector256 <double> right, bool[] expected)
        {
            Vector256 <long> result = Vector.Inequality(left, right).AsInt64();

            Assert.True((bool)AreAllNotEqual(expected, result));
        }
    private ColorPacket256 GetNaturalColor(Vector256 <int> things, VectorPacket256 pos, VectorPacket256 norms, VectorPacket256 rds, Scene scene)
    {
        var colors = ColorPacket256Helper.DefaultColor;

        for (int i = 0; i < scene.Lights.Length; i++)
        {
            var lights       = scene.Lights[i];
            var zero         = SetZeroVector256 <float>();
            var colorPacket  = lights.Colors;
            var ldis         = lights.Positions - pos;
            var livec        = ldis.Normalize();
            var neatIsectDis = TestRay(new RayPacket256(pos, livec), scene);

            // is in shadow?
            var mask1      = Compare(neatIsectDis, ldis.Lengths, FloatComparisonMode.LessThanOrEqualOrderedNonSignaling);
            var mask2      = Compare(neatIsectDis, zero, FloatComparisonMode.NotEqualOrderedNonSignaling);
            var isInShadow = And(mask1, mask2);

            Vector256 <float> illum = VectorPacket256.DotProduct(livec, norms);
            Vector256 <float> illumGraterThanZero = Compare(illum, zero, FloatComparisonMode.GreaterThanOrderedNonSignaling);
            var tmpColor1             = illum * colorPacket;
            var defaultRGB            = zero;
            Vector256 <float> lcolorR = BlendVariable(defaultRGB, tmpColor1.Xs, illumGraterThanZero);
            Vector256 <float> lcolorG = BlendVariable(defaultRGB, tmpColor1.Ys, illumGraterThanZero);
            Vector256 <float> lcolorB = BlendVariable(defaultRGB, tmpColor1.Zs, illumGraterThanZero);
            ColorPacket256    lcolor  = new ColorPacket256(lcolorR, lcolorG, lcolorB);

            Vector256 <float> specular = VectorPacket256.DotProduct(livec, rds.Normalize());
            Vector256 <float> specularGraterThanZero = Compare(specular, zero, FloatComparisonMode.GreaterThanOrderedNonSignaling);

            var difColor  = new ColorPacket256(1, 1, 1);
            var splColor  = new ColorPacket256(1, 1, 1);
            var roughness = SetAllVector256 <float>(1);

            for (int j = 0; j < scene.Things.Length; j++)
            {
                Vector256 <float> thingMask = StaticCast <int, float>(CompareEqual(things, SetAllVector256 <int>(j)));
                var rgh = SetAllVector256 <float>(scene.Things[j].Surface.Roughness);
                var dif = scene.Things[j].Surface.Diffuse(pos);
                var spl = scene.Things[j].Surface.Specular;

                roughness = BlendVariable(roughness, rgh, thingMask);

                difColor.Xs = BlendVariable(difColor.Xs, dif.Xs, thingMask);
                difColor.Ys = BlendVariable(difColor.Ys, dif.Ys, thingMask);
                difColor.Zs = BlendVariable(difColor.Zs, dif.Zs, thingMask);

                splColor.Xs = BlendVariable(splColor.Xs, spl.Xs, thingMask);
                splColor.Ys = BlendVariable(splColor.Ys, spl.Ys, thingMask);
                splColor.Zs = BlendVariable(splColor.Zs, spl.Zs, thingMask);
            }

            var tmpColor2             = VectorMath.Pow(specular, roughness) * colorPacket;
            Vector256 <float> scolorR = BlendVariable(defaultRGB, tmpColor2.Xs, specularGraterThanZero);
            Vector256 <float> scolorG = BlendVariable(defaultRGB, tmpColor2.Ys, specularGraterThanZero);
            Vector256 <float> scolorB = BlendVariable(defaultRGB, tmpColor2.Zs, specularGraterThanZero);
            ColorPacket256    scolor  = new ColorPacket256(scolorR, scolorG, scolorB);

            var oldColor = colors;

            colors = colors + ColorPacket256Helper.Times(difColor, lcolor) + ColorPacket256Helper.Times(splColor, scolor);

            colors = new ColorPacket256(BlendVariable(colors.Xs, oldColor.Xs, isInShadow), BlendVariable(colors.Ys, oldColor.Ys, isInShadow), BlendVariable(colors.Zs, oldColor.Zs, isInShadow));
        }
        return(colors);
    }
示例#5
0
        public void RunReflectionScenario()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunReflectionScenario));
            Vector256 <UInt32> value;

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object byteResult = typeof(Vector256 <UInt32>)
                                .GetMethod(nameof(Vector256 <UInt32> .AsByte), new Type[] { })
                                .Invoke(value, new object[] { });

            ValidateResult((Vector256 <byte>)(byteResult), value);

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object doubleResult = typeof(Vector256 <UInt32>)
                                  .GetMethod(nameof(Vector256 <UInt32> .AsDouble), new Type[] { })
                                  .Invoke(value, new object[] { });

            ValidateResult((Vector256 <double>)(doubleResult), value);

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object shortResult = typeof(Vector256 <UInt32>)
                                 .GetMethod(nameof(Vector256 <UInt32> .AsInt16), new Type[] { })
                                 .Invoke(value, new object[] { });

            ValidateResult((Vector256 <short>)(shortResult), value);

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object intResult = typeof(Vector256 <UInt32>)
                               .GetMethod(nameof(Vector256 <UInt32> .AsInt32), new Type[] { })
                               .Invoke(value, new object[] { });

            ValidateResult((Vector256 <int>)(intResult), value);

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object longResult = typeof(Vector256 <UInt32>)
                                .GetMethod(nameof(Vector256 <UInt32> .AsInt64), new Type[] { })
                                .Invoke(value, new object[] { });

            ValidateResult((Vector256 <long>)(longResult), value);

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object sbyteResult = typeof(Vector256 <UInt32>)
                                 .GetMethod(nameof(Vector256 <UInt32> .AsSByte), new Type[] { })
                                 .Invoke(value, new object[] { });

            ValidateResult((Vector256 <sbyte>)(sbyteResult), value);

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object floatResult = typeof(Vector256 <UInt32>)
                                 .GetMethod(nameof(Vector256 <UInt32> .AsSingle), new Type[] { })
                                 .Invoke(value, new object[] { });

            ValidateResult((Vector256 <float>)(floatResult), value);

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object ushortResult = typeof(Vector256 <UInt32>)
                                  .GetMethod(nameof(Vector256 <UInt32> .AsUInt16), new Type[] { })
                                  .Invoke(value, new object[] { });

            ValidateResult((Vector256 <ushort>)(ushortResult), value);

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object uintResult = typeof(Vector256 <UInt32>)
                                .GetMethod(nameof(Vector256 <UInt32> .AsUInt32), new Type[] { })
                                .Invoke(value, new object[] { });

            ValidateResult((Vector256 <uint>)(uintResult), value);

            value = Vector256.Create(TestLibrary.Generator.GetUInt32());
            object ulongResult = typeof(Vector256 <UInt32>)
                                 .GetMethod(nameof(Vector256 <UInt32> .AsUInt64), new Type[] { })
                                 .Invoke(value, new object[] { });

            ValidateResult((Vector256 <ulong>)(ulongResult), value);
        }
示例#6
0
    public static unsafe int Main()
    {
        init();

        if (Sse2.IsSupported)
        {
            Vector128 <float> result_v128         = F2_v128(0.5F);
            Vector128 <float> expectedResult_v128 = F1_v128(0.5F);

            float *result = stackalloc float[4];
            Sse.Store(result, result_v128);
            float *expResult = stackalloc float[4];
            Sse.Store(expResult, expectedResult_v128);

            for (int i = 0; i < 4; i++)
            {
                if (result[i] != expResult[i])
                {
                    Console.WriteLine("Vector128<float> test FAILED");
                    Console.Write("[ ");
                    for (int j = 0; j < 4; j++)
                    {
                        Console.Write(result[j] + ", ");
                    }
                    Console.WriteLine("]");
                    return(Fail);
                }
            }

            if (Vector128ReturnTest() != Pass)
            {
                Console.WriteLine("Vector128ReturnTest FAILED");
                return(Fail);
            }

            Vector128 <short> result_v128i         = F2_v128i(6);
            Vector128 <short> expectedResult_v128i = F1_v128i(6);

            short *results = stackalloc short[8];
            Sse2.Store(results, result_v128i);
            short *expResults = stackalloc short[8];
            Sse2.Store(expResults, expectedResult_v128i);

            for (int i = 0; i < 8; i++)
            {
                if (results[i] != expResults[i])
                {
                    Console.WriteLine("Vector128<short> test FAILED");
                    Console.Write("[ ");
                    for (int j = 0; j < 8; j++)
                    {
                        Console.Write(results[j] + ", ");
                    }
                    Console.WriteLine("]");
                    return(Fail);
                }
            }

            if (Vector128Int16ReturnTest() != Pass)
            {
                Console.WriteLine("Vector128Int16ReturnTest FAILED");
                return(Fail);
            }
        }

        if (Avx2.IsSupported)
        {
            Vector256 <float> result_v256         = F2_v256(0.7F);
            Vector256 <float> expectedResult_v256 = F1_v256(0.7F);

            float *result = stackalloc float[8];
            Avx.Store(result, result_v256);
            float *expResult = stackalloc float[8];
            Avx.Store(expResult, expectedResult_v256);

            for (int i = 0; i < 8; i++)
            {
                if (result[i] != expResult[i])
                {
                    Console.WriteLine("Vector256<float> test FAILED");
                    Console.Write("[ ");
                    for (int j = 0; j < 8; j++)
                    {
                        Console.Write(result[j] + ", ");
                    }
                    Console.WriteLine("]");
                    return(Fail);
                }
            }

            if (Vector256ReturnTest() != Pass)
            {
                Console.WriteLine("Vector256ReturnTest FAILED");
                return(Fail);
            }

            Vector256 <byte> result_v256i         = F2_v256i(7);
            Vector256 <byte> expectedResult_v256i = F1_v256i(7);

            byte *resultb = stackalloc byte[32];
            Avx.Store(resultb, result_v256i);
            byte *expResultb = stackalloc byte[32];
            Avx.Store(expResultb, expectedResult_v256i);

            for (int i = 0; i < 32; i++)
            {
                if (resultb[i] != expResultb[i])
                {
                    Console.WriteLine("Vector256<byte> test FAILED");
                    Console.Write("[ ");
                    for (int j = 0; j < 32; j++)
                    {
                        Console.Write(resultb[j] + ", ");
                    }
                    Console.WriteLine("]");
                    return(Fail);
                }
            }

            if (Vector256Int32ReturnTest() != Pass)
            {
                Console.WriteLine("Vector128Int16ReturnTest FAILED");
                return(Fail);
            }
        }

        Console.WriteLine("PASSED");
        return(Pass);
    }
        public void RunReflectionScenario(int imm = 0, bool expectedOutOfRangeException = false)
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunReflectionScenario));

            Double[] values = new Double[ElementCount];

            for (int i = 0; i < ElementCount; i++)
            {
                values[i] = TestLibrary.Generator.GetDouble();
            }

            Vector256 <Double> value = Vector256.Create(values[0], values[1], values[2], values[3]);

            bool succeeded = !expectedOutOfRangeException;

            try
            {
                object result = typeof(Vector256 <Double>)
                                .GetMethod(nameof(Vector256.GetElement), new Type[] { typeof(int) })
                                .Invoke(value, new object[] { imm });
                ValidateGetResult((Double)(result), values);
            }
            catch (TargetInvocationException e)
            {
                succeeded = expectedOutOfRangeException &&
                            e.InnerException is ArgumentOutOfRangeException;
            }

            if (!succeeded)
            {
                TestLibrary.TestFramework.LogInformation($"Vector256<Double.GetElement({imm}): {nameof(RunReflectionScenario)} failed to throw ArgumentOutOfRangeException.");
                TestLibrary.TestFramework.LogInformation(string.Empty);

                Succeeded = false;
            }

            succeeded = !expectedOutOfRangeException;

            Double insertedValue = TestLibrary.Generator.GetDouble();

            try
            {
                object result2 = typeof(Vector256 <Double>)
                                 .GetMethod(nameof(Vector256.WithElement), new Type[] { typeof(int), typeof(Double) })
                                 .Invoke(value, new object[] { imm, insertedValue });
                ValidateWithResult((Vector256 <Double>)(result2), values, insertedValue);
            }
            catch (TargetInvocationException e)
            {
                succeeded = expectedOutOfRangeException &&
                            e.InnerException is ArgumentOutOfRangeException;
            }

            if (!succeeded)
            {
                TestLibrary.TestFramework.LogInformation($"Vector256<Double.WithElement({imm}): {nameof(RunReflectionScenario)} failed to throw ArgumentOutOfRangeException.");
                TestLibrary.TestFramework.LogInformation(string.Empty);

                Succeeded = false;
            }
        }
示例#8
0
 public VectorArg256(Vector256 <float> _rgb)
 {
     this._rgb = _rgb;
 }
示例#9
0
        public VectorArg256 Change(float f)
        {
            Vector256 <float> t = Vector256.Create(f);

            return(new VectorArg256(Avx.Add(t, _rgb)));
        }
示例#10
0
 private void ValidateResult(Vector256 <UInt32> result, UInt32 expectedValue, [CallerMemberName] string method = "")
 {
     UInt32[] resultElements = new UInt32[ElementCount];
     Unsafe.WriteUnaligned(ref Unsafe.As <UInt32, byte>(ref resultElements[0]), result);
     ValidateResult(resultElements, expectedValue, method);
 }
        public unsafe int[,] IntegrateUnsafeVectorBranched()
        {
            int w = _data.Width();
            int h = _data.Height();

            int[,] res = new int[h, w];

            Vector256 <int> shiftRight = RotateRight;

            fixed(byte *pSource = &_data[0, 0])
            fixed(int *pTarget = &res[0, 0])
            {
                var pSrc = pSource;
                var pTrg = pTarget;

                for (var i = 0; i < h; i++)
                {
                    var j = 0;

                    var p  = Vector256.CreateScalar(0);
                    var pr = Vector256.CreateScalar(0);
                    //handle vector part
                    for (; j + Vector256 <int> .Count <= w; j += Vector256 <int> .Count)
                    {
                        var t = Avx2.ConvertToVector256Int32(pSrc); //(int)*(pSrc)

                        var s = Aggregate(p, t);                    // this code block has to be
                        p = t;                                      // added to handle the in-line
                        t = Avx2.Add(t, s);                         // recursion: S[i]=a[i]+S[i-1]

                        if (j > 0)
                        {
                            t = Avx2.Add(t, pr);    // t += *(pTrg - 1);
                        }
                        if (i > 0)
                        {
                            t = Avx2.Add(t, Avx.LoadVector256(pTrg - w));
                            if (j > 0)
                            {
                                t = Avx2.Subtract(t, Avx.LoadVector256(pTrg - w - 8));
                            }
                        }

                        Avx.Store(pTrg, t);
                        pr    = t;
                        pSrc += Vector256 <int> .Count;
                        pTrg += Vector256 <int> .Count;
                    }


                    // handle the tail
                    var pr2 = (j == 0 ? 0 : pr.GetElement(Vector256 <int> .Count - 1)); //  Vector256.CreateScalar(0);
                    for (; j < w; j++)
                    {
                        var t = (int)*(pSrc);           // Avx2.ConvertToVector256Int32(pSrc);
                        if (j > 0)
                        {
                            t += pr2;                   //    t = Avx2.Add(t, pr);
                        }
                        if (i > 0)
                        {
                            t += *(pTrg - w);           //  Avx2.Add(t, Avx.LoadVector256(pTrg - w));
                            if (j > 0)
                            {
                                t -= *(pTrg - w - 1);   //  Avx2.Subtract(t, Avx.LoadVector256(pTrg - w - 8));
                            }
                        }

                        *pTrg = t;                      // Avx2.Store(pTrg, t);
                        pr2 = t;                        // pr = t
                        pSrc++;
                        pTrg++;
                    }
                }
            }
            return(res);
        }
示例#12
0
        public void Negate_Theory(Vector256 <double> vector, Vector4d expected)
        {
            Vector256 <double> result = Vector.Negate(vector);

            Assert.True(TestHelpers.AreEqual(expected, result), $"Expected {expected}, got {result}");
        }
示例#13
0
            public void RunStructFldScenario(VectorBooleanBinaryOpTest__GreaterThanOrEqualAllUInt16 testClass)
            {
                var result = Vector256.GreaterThanOrEqualAll(_fld1, _fld2);

                testClass.ValidateResult(_fld1, _fld2, result);
            }
示例#14
0
 public Intersections(Vector256 <float> dis, Vector256 <int> things)
 {
     Distances    = dis;
     ThingIndices = things;
 }
示例#15
0
    public static Vector256 <T> Vector256PlusOne <T>(Vector256 <T> v1) where T : struct
    {
        Vector256 <T> v2 = Vector256One <T>();

        return(Vector256Add <T>(v1, v2));
    }
示例#16
0
        private void ValidateResult(Vector256 <Single> firstOp, Vector256 <Single> secondOp, Vector256 <Single> thirdOp, void *result, [CallerMemberName] string method = "")
        {
            Single[] inArray1 = new Single[Op1ElementCount];
            Single[] inArray2 = new Single[Op2ElementCount];
            Single[] inArray3 = new Single[Op3ElementCount];
            Single[] outArray = new Single[RetElementCount];

            Unsafe.WriteUnaligned(ref Unsafe.As <Single, byte>(ref inArray1[0]), firstOp);
            Unsafe.WriteUnaligned(ref Unsafe.As <Single, byte>(ref inArray2[0]), secondOp);
            Unsafe.WriteUnaligned(ref Unsafe.As <Single, byte>(ref inArray3[0]), thirdOp);
            Unsafe.CopyBlockUnaligned(ref Unsafe.As <Single, byte>(ref outArray[0]), ref Unsafe.AsRef <byte>(result), (uint)Unsafe.SizeOf <Vector256 <Single> >());

            ValidateResult(inArray1, inArray2, inArray3, outArray, method);
        }
示例#17
0
    public static Vector256 <int> GetVector256Int32PlusOne(Vector256 <int> v1)
    {
        Vector256 <int> v2 = GetVector256Int32One();

        return(Avx2.Add(v1, v2));
    }
示例#18
0
 return(new AvxVec3()
 {
     x = Avx.Add(x, other.x),
     y = Avx.Add(y, other.y),
     z = Avx.Add(z, other.z)
 });
示例#19
0
 private void ValidateWithResult(Vector256 <UInt16> result, UInt16[] values, UInt16 insertedValue, [CallerMemberName] string method = "")
 {
     UInt16[] resultElements = new UInt16[ElementCount];
     Unsafe.WriteUnaligned(ref Unsafe.As <UInt16, byte>(ref resultElements[0]), result);
     ValidateWithResult(resultElements, values, insertedValue, method);
 }
        public override void Step()
        {
            fixed(byte *
                  currentFieldPtr     = field,
                  upperLineSumOf2Ptr  = upperLineSumOf2,
                  upperLineSumOf3Ptr  = upperLineSumOf3,
                  middleLineSumOf2Ptr = middleLineSumOf2,
                  middleLineSumOf3Ptr = middleLineSumOf3,
                  lowerLineSumOf2Ptr  = lowerLineSumOf2,
                  lowerLineSumOf3Ptr  = lowerLineSumOf3)
            {
                byte *upper2 = upperLineSumOf2Ptr, upper3 = upperLineSumOf3Ptr;
                byte *middle2 = middleLineSumOf2Ptr, middle3 = middleLineSumOf3Ptr;
                byte *lower2 = lowerLineSumOf2Ptr, lower3 = lowerLineSumOf3Ptr;
                byte *nextLinePtr = currentFieldPtr + LINE_WIDTH;

                for (int x = 0; x < LINE_WIDTH; x += 32)
                {
                    Avx2.Store(upper2 + x, Vector256 <byte> .Zero);
                    Avx2.Store(upper3 + x, Vector256 <byte> .Zero);
                    Avx2.Store(middle2 + x, Vector256 <byte> .Zero);
                    Avx2.Store(middle3 + x, Vector256 <byte> .Zero);
                    Vector256 <byte> nextLeft   = Avx2.LoadVector256(nextLinePtr + x - 8);
                    Vector256 <byte> nextCenter = Avx2.LoadVector256(nextLinePtr + x);
                    Vector256 <byte> nextRight  = Avx2.LoadVector256(nextLinePtr + x + 8);
                    Vector256 <byte> lowerSum2  =
                        Avx2.Add(
                            Avx2.Add(
                                Avx2.ShiftRightLogical(nextCenter.AsUInt64(), 4), Avx2.ShiftLeftLogical(nextCenter.AsUInt64(), 4)),
                            Avx2.Add(
                                Avx2.ShiftRightLogical(nextLeft.AsUInt64(), 60), Avx2.ShiftLeftLogical(nextRight.AsUInt64(), 60))).AsByte();
                    Avx2.Store(lower2 + x, lowerSum2);
                    Vector256 <byte> lowerSum3 = Avx2.Add(lowerSum2, nextCenter);
                    Avx2.Store(lower3 + x, lowerSum3);
                }

                for (int y = 1; y < HEIGHT - 1; y++)
                {
                    nextLinePtr += LINE_WIDTH;

                    byte *temp2 = upper2;
                    byte *temp3 = upper3;
                    upper2  = middle2;
                    upper3  = middle3;
                    middle2 = lower2;
                    middle3 = lower3;
                    lower2  = temp2;
                    lower3  = temp3;

                    for (int x = 0; x < LINE_WIDTH; x += 32)
                    {
                        Vector256 <byte> nextLeft   = Avx2.LoadVector256(nextLinePtr + x - 8);
                        Vector256 <byte> nextCenter = Avx2.LoadVector256(nextLinePtr + x);
                        Vector256 <byte> nextRight  = Avx2.LoadVector256(nextLinePtr + x + 8);
                        Vector256 <byte> lowerSum2  =
                            Avx2.Add(
                                Avx2.Add(
                                    Avx2.ShiftRightLogical(nextCenter.AsUInt64(), 4), Avx2.ShiftLeftLogical(nextCenter.AsUInt64(), 4)),
                                Avx2.Add(
                                    Avx2.ShiftRightLogical(nextLeft.AsUInt64(), 60), Avx2.ShiftLeftLogical(nextRight.AsUInt64(), 60))).AsByte();
                        Avx2.Store(lower2 + x, lowerSum2);
                        Vector256 <byte> lowerSum3 = Avx2.Add(lowerSum2, nextCenter);
                        Avx2.Store(lower2 + x, lowerSum2);
                        Avx2.Store(lower3 + x, Avx2.Add(lowerSum2, nextCenter));

                        Vector256 <byte> neighbours =
                            Avx2.Add(
                                Avx2.LoadVector256(middle2 + x),
                                Avx2.Add(Avx2.LoadVector256(upper3 + x), lowerSum3));
                        Vector256 <byte> alive = Avx2.LoadVector256(nextLinePtr - LINE_WIDTH + x);

                        alive = Avx2.ShiftLeftLogical(alive.AsUInt64(), (byte)3).AsByte();
                        Vector256 <byte> mask = Avx2.Or(neighbours, alive);

                        Vector256 <byte> mask_hi = Avx2.And(mask, v_hi);
                        Vector256 <byte> mask_lo = Avx2.And(mask, v_lo);
                        mask_hi = Avx2.ShiftRightLogical(mask_hi.AsUInt64(), 4).AsByte();
                        Vector256 <byte> shouldBeAlive_hi = Avx2.Shuffle(v_lookup, mask_hi);
                        Vector256 <byte> shouldBeAlive_lo = Avx2.Shuffle(v_lookup, mask_lo);
                        shouldBeAlive_hi = Avx2.ShiftLeftLogical(shouldBeAlive_hi.AsUInt64(), 4).AsByte();

                        Vector256 <byte> shouldBeAlive = Avx2.Or(shouldBeAlive_hi, shouldBeAlive_lo);

                        Avx2.Store(nextLinePtr - LINE_WIDTH + x, shouldBeAlive);
                    }
                    *(byte *)(nextLinePtr - LINE_WIDTH) &= 0xF0;
                    *(byte *)(nextLinePtr - 1)          &= 0x0F;
                }
            }
        }
 private void ValidateWithResult(Vector256 <Double> result, Double[] values, Double insertedValue, [CallerMemberName] string method = "")
 {
     Double[] resultElements = new Double[ElementCount];
     Unsafe.WriteUnaligned(ref Unsafe.As <Double, byte>(ref resultElements[0]), result);
     ValidateWithResult(resultElements, values, insertedValue, method);
 }
示例#22
0
        public static Vector256 <double> CompareEqual_Software(Vector256 <double> left, Vector256 <double> right)
        {
            double lX = X(left), rX = X(right);
            double lY = Y(left), rY = Y(right);
            double lZ = Z(left), rZ = Z(right);
            double lW = W(left), rW = W(right);

            return(Vector256.Create(
                       AsMaskDouble(lX == rX),
                       AsMaskDouble(lY == rY),
                       AsMaskDouble(lZ == rZ),
                       AsMaskDouble(lW == rW)
                       ));
        }
 private ColorPacket256 GetReflectionColor(Vector256 <int> things, VectorPacket256 pos, VectorPacket256 norms, VectorPacket256 rds, Scene scene, int depth)
 {
     return(scene.Reflect(things, pos) * TraceRay(new RayPacket256(pos, rds), scene, depth + 1));
 }
示例#24
0
        public static Vector256 <double> CompareGreaterThanOrEqual_Software(Vector256 <double> left, Vector256 <double> right)
        {
            double lX = X(left), rX = X(right);
            double lY = Y(left), rY = Y(right);
            double lZ = Z(left), rZ = Z(right);
            double lW = W(left), rW = W(right);

            return(Vector256.Create(
                       AsMaskDouble(lX >= rX || IsNan(lX, rX)),
                       AsMaskDouble(lY >= rY || IsNan(lY, rY)),
                       AsMaskDouble(lZ >= rZ || IsNan(lZ, rZ)),
                       AsMaskDouble(lW >= rW || IsNan(lW, rW))
                       ));
        }
示例#25
0
 public static Vector256 <float> Pow(Vector256 <float> left, Vector256 <float> right)
 {
     return(Exp(Multiply(right, Log(left))));
 }
示例#26
0
        public static Vector256 <double> CompareLessThanOrEqual_Software(Vector256 <double> left, Vector256 <double> right)
        {
            double lX = X(left), rX = X(right);
            double lY = Y(left), rY = Y(right);
            double lZ = Z(left), rZ = Z(right);
            double lW = W(left), rW = W(right);

            return(Vector256.Create(
                       AsMaskDouble(lX <= rX /* || IsNan(lX, rX)*/),
                       AsMaskDouble(lY <= rY /* || IsNan(lY, rY)*/),
                       AsMaskDouble(lZ <= rZ /* || IsNan(lZ, rZ)*/),
                       AsMaskDouble(lW <= rW /* || IsNan(lW, rW)*/)
                       ));
        }
示例#27
0
 private void ValidateResult(Vector256 <Double> result, Double expectedLowerValue, Double expectedUpperValue, [CallerMemberName] string method = "")
 {
     Double[] resultElements = new Double[ElementCount];
     Unsafe.WriteUnaligned(ref Unsafe.As <Double, byte>(ref resultElements[0]), result);
     ValidateResult(resultElements, expectedLowerValue, expectedUpperValue, method);
 }
示例#28
0
        public static void GreaterThanOrEqualTo_Theory(Vector256 <double> left, Vector256 <double> right, bool[] expected)
        {
            Vector256 <long> result1 = Vector.GreaterThanOrEqual(left, right).AsInt64();

            Assert.True((bool)AreAllEqual(expected, result1));
        }
示例#29
0
        public static unsafe bool TryGetAsciiString(byte *input, char *output, int count)
        {
            Debug.Assert(input != null);
            Debug.Assert(output != null);

            var end = input + count;

            Debug.Assert((long)end >= Vector256 <sbyte> .Count);

            if (Sse2.IsSupported)
            {
                if (Avx2.IsSupported && input <= end - Vector256 <sbyte> .Count)
                {
                    Vector256 <sbyte> zero = Vector256 <sbyte> .Zero;

                    do
                    {
                        var vector = Avx.LoadVector256(input).AsSByte();
                        if (!CheckBytesInAsciiRange(vector, zero))
                        {
                            return(false);
                        }

                        var tmp0 = Avx2.UnpackLow(vector, zero);
                        var tmp1 = Avx2.UnpackHigh(vector, zero);

                        // Bring into the right order
                        var out0 = Avx2.Permute2x128(tmp0, tmp1, 0x20);
                        var out1 = Avx2.Permute2x128(tmp0, tmp1, 0x31);

                        Avx.Store((ushort *)output, out0.AsUInt16());
                        Avx.Store((ushort *)output + Vector256 <ushort> .Count, out1.AsUInt16());

                        input  += Vector256 <sbyte> .Count;
                        output += Vector256 <sbyte> .Count;
                    } while (input <= end - Vector256 <sbyte> .Count);

                    if (input == end)
                    {
                        return(true);
                    }
                }

                if (input <= end - Vector128 <sbyte> .Count)
                {
                    Vector128 <sbyte> zero = Vector128 <sbyte> .Zero;

                    do
                    {
                        var vector = Sse2.LoadVector128(input).AsSByte();
                        if (!CheckBytesInAsciiRange(vector, zero))
                        {
                            return(false);
                        }

                        var c0 = Sse2.UnpackLow(vector, zero).AsUInt16();
                        var c1 = Sse2.UnpackHigh(vector, zero).AsUInt16();

                        Sse2.Store((ushort *)output, c0);
                        Sse2.Store((ushort *)output + Vector128 <ushort> .Count, c1);

                        input  += Vector128 <sbyte> .Count;
                        output += Vector128 <sbyte> .Count;
                    } while (input <= end - Vector128 <sbyte> .Count);

                    if (input == end)
                    {
                        return(true);
                    }
                }
            }
            else if (Vector.IsHardwareAccelerated)
            {
                while (input <= end - Vector <sbyte> .Count)
                {
                    var vector = Unsafe.AsRef <Vector <sbyte> >(input);
                    if (!CheckBytesInAsciiRange(vector))
                    {
                        return(false);
                    }

                    Vector.Widen(
                        vector,
                        out Unsafe.AsRef <Vector <short> >(output),
                        out Unsafe.AsRef <Vector <short> >(output + Vector <short> .Count));

                    input  += Vector <sbyte> .Count;
                    output += Vector <sbyte> .Count;
                }

                if (input == end)
                {
                    return(true);
                }
            }

            if (Environment.Is64BitProcess) // Use Intrinsic switch for branch elimination
            {
                // 64-bit: Loop longs by default
                while (input <= end - sizeof(long))
                {
                    var value = *(long *)input;
                    if (!CheckBytesInAsciiRange(value))
                    {
                        return(false);
                    }

                    if (Bmi2.X64.IsSupported)
                    {
                        // BMI2 will work regardless of the processor's endianness.
                        ((ulong *)output)[0] = Bmi2.X64.ParallelBitDeposit((ulong)value, 0x00FF00FF_00FF00FFul);
                        ((ulong *)output)[1] = Bmi2.X64.ParallelBitDeposit((ulong)(value >> 32), 0x00FF00FF_00FF00FFul);
                    }
                    else
                    {
                        output[0] = (char)input[0];
                        output[1] = (char)input[1];
                        output[2] = (char)input[2];
                        output[3] = (char)input[3];
                        output[4] = (char)input[4];
                        output[5] = (char)input[5];
                        output[6] = (char)input[6];
                        output[7] = (char)input[7];
                    }

                    input  += sizeof(long);
                    output += sizeof(long);
                }

                if (input <= end - sizeof(int))
                {
                    var value = *(int *)input;
                    if (!CheckBytesInAsciiRange(value))
                    {
                        return(false);
                    }

                    if (Bmi2.IsSupported)
                    {
                        // BMI2 will work regardless of the processor's endianness.
                        ((uint *)output)[0] = Bmi2.ParallelBitDeposit((uint)value, 0x00FF00FFu);
                        ((uint *)output)[1] = Bmi2.ParallelBitDeposit((uint)(value >> 16), 0x00FF00FFu);
                    }
                    else
                    {
                        output[0] = (char)input[0];
                        output[1] = (char)input[1];
                        output[2] = (char)input[2];
                        output[3] = (char)input[3];
                    }

                    input  += sizeof(int);
                    output += sizeof(int);
                }
            }
            else
            {
                // 32-bit: Loop ints by default
                while (input <= end - sizeof(int))
                {
                    var value = *(int *)input;
                    if (!CheckBytesInAsciiRange(value))
                    {
                        return(false);
                    }

                    if (Bmi2.IsSupported)
                    {
                        // BMI2 will work regardless of the processor's endianness.
                        ((uint *)output)[0] = Bmi2.ParallelBitDeposit((uint)value, 0x00FF00FFu);
                        ((uint *)output)[1] = Bmi2.ParallelBitDeposit((uint)(value >> 16), 0x00FF00FFu);
                    }
                    else
                    {
                        output[0] = (char)input[0];
                        output[1] = (char)input[1];
                        output[2] = (char)input[2];
                        output[3] = (char)input[3];
                    }

                    input  += sizeof(int);
                    output += sizeof(int);
                }
            }

            if (input <= end - sizeof(short))
            {
                if (!CheckBytesInAsciiRange(((short *)input)[0]))
                {
                    return(false);
                }

                output[0] = (char)input[0];
                output[1] = (char)input[1];

                input  += sizeof(short);
                output += sizeof(short);
            }

            if (input < end)
            {
                if (!CheckBytesInAsciiRange(((sbyte *)input)[0]))
                {
                    return(false);
                }
                output[0] = (char)input[0];
            }

            return(true);
        }
示例#30
0
        public static IEnumerable <object[]> Data(VectorDimensions dimension)
        {
            var objs = new[]
            {
                new object[4] {
                    Vector256.Create(0d), Vector256.Create(0d), Vector256.Create(0d), default(Vector4d)
                },
                new object[4] {
                    Vector256.Create(1d), Vector256.Create(1d), Vector256.Create(1d), default(Vector4d)
                },
                new object[4] {
                    Vector256.Create(-1d), Vector256.Create(-1d), Vector256.Create(-1d), default(Vector4d)
                },
                new object[4] {
                    Vector256.Create(-1d), Vector256.Create(1d, 4d, 9d, 16d), Vector256.Create(100d, 200d, 300d, 400d), default(Vector4d)
                },
                new object[4] {
                    Vector256.Create(1d, 2d, 3d, 4d), Vector256.Create(4d, -5d, 6d, 9d), Vector256.Create(0d), default(Vector4d)
                },
                new object[4] {
                    Vector256.Create(double.PositiveInfinity), Vector256.Create(double.PositiveInfinity), Vector256.Create(0d), default(Vector4d)
                },
                new object[4] {
                    Vector256.Create(double.PositiveInfinity), Vector256.Create(double.NegativeInfinity), Vector256.Create(0d), default(Vector4d)
                },
                new object[4] {
                    Vector256.Create(double.NaN), Vector256.Create(double.NegativeInfinity), Vector256.Create(double.PositiveInfinity), default(Vector4d)
                },
                new object[4] {
                    Vector256.Create(double.MaxValue, double.MinValue, double.NaN, 0), Vector256.Create(double.MaxValue, double.MinValue, double.NaN, 0), Vector256.Create(10d), default(Vector4d)
                },
            };

            foreach (object[] set in objs)
            {
                switch (dimension)
                {
                case VectorDimensions.V2D:
                {
                    var v1 = (Vector256 <double>)set[0];
                    var v2 = (Vector256 <double>)set[1];
                    set[3] = TestHelpers.ByValToSlowVector2d(SoftwareFallbacks.CrossProduct2D_Software(v1, v2));
                    break;
                }

                case VectorDimensions.V3D:
                {
                    var v1 = (Vector256 <double>)set[0];
                    var v2 = (Vector256 <double>)set[1];
                    set[3] = TestHelpers.ByValToSlowVector3d(SoftwareFallbacks.CrossProduct3D_Software(v1, v2));
                    break;
                }

                case VectorDimensions.V4D:
                {
                    var v1 = (Vector256 <double>)set[0];
                    var v2 = (Vector256 <double>)set[1];
                    var v3 = (Vector256 <double>)set[2];
                    set[3] = TestHelpers.ByValToSlowVector4d(SoftwareFallbacks.CrossProduct4D_Software(v1, v2, v3));
                    break;
                }

                default:
                    throw new ArgumentException(nameof(dimension));
                }
            }

            return(objs);
        }