Пример #1
0
    public static int Main()
    {
        // float
        AssertEqual(Vector256.Create(1f).ToString(), "<1, 1, 1, 1, 1, 1, 1, 1>");
        AssertEqual(Vector256.CreateScalar(1f).ToString(), "<1, 0, 0, 0, 0, 0, 0, 0>");
        AssertEqual(Vector256.CreateScalarUnsafe(1f).ToScalar().ToString(), "1");
        AssertEqual(Vector256.Create(0.0f, 1, 2, 3, 4, 5, 6, 7).ToString(), "<0, 1, 2, 3, 4, 5, 6, 7>");

        // double
        AssertEqual(Vector256.Create(1.0).ToString(), "<1, 1, 1, 1>");
        AssertEqual(Vector256.CreateScalar(1.0).ToString(), "<1, 0, 0, 0>");
        AssertEqual(Vector256.CreateScalarUnsafe(1.0).ToScalar().ToString(), "1");
        AssertEqual(Vector256.Create(0.0, 1, 2, 3).ToString(), "<0, 1, 2, 3>");

        // ushort
        AssertEqual(Vector256.Create((ushort)1).ToString(), "<1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1>");
        AssertEqual(Vector256.CreateScalar((ushort)1).ToString(), "<1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>");
        AssertEqual(Vector256.CreateScalarUnsafe((ushort)1).ToScalar().ToString(), "1");
        AssertEqual(Vector256.Create((ushort)0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15).ToString(), "<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>");

        // long
        AssertEqual(Vector256.Create((long)1).ToString(), "<1, 1, 1, 1>");
        AssertEqual(Vector256.CreateScalar((long)1).ToString(), "<1, 0, 0, 0>");
        AssertEqual(Vector256.CreateScalarUnsafe((long)1).ToScalar().ToString(), "1");
        AssertEqual(Vector256.Create((long)0, 1, 2, 3).ToString(), "<0, 1, 2, 3>");
        return(retCode);
    }
Пример #2
0
        public void RunBasicScenario()
        {
            TestLibrary.TestFramework.BeginScenario(nameof(RunBasicScenario));

            Double             value  = TestLibrary.Generator.GetDouble();
            Vector256 <Double> result = Vector256.CreateScalarUnsafe(value);

            ValidateResult(result, value);
        }
Пример #3
0
        public static Vector256 <uint> Create4UInt(uint a, uint b, uint c, uint d)
        {
            if (Avx2.IsSupported)
            {
                var t0 = Avx2.UnpackLow(Vector256.CreateScalarUnsafe(a).AsUInt64(), Vector256.CreateScalarUnsafe(b).AsUInt64()).AsUInt32();
                var t1 = Avx2.UnpackLow(Vector256.CreateScalarUnsafe(c).AsUInt64(), Vector256.CreateScalarUnsafe(d).AsUInt64()).AsUInt32();

                return(Avx2.Permute2x128(t0, t1, 0x20));
            }

            return(Vector256.Create(a, 0, b, 0, c, 0, d, 0));
        }
Пример #4
0
        public static float DotMultiplyIntrinsicWFma(ref Memory <float> mem1, ref Memory <float> mem2)
        {
            var span1   = mem1.Span;
            var span2   = mem2.Span;
            var cnt     = Math.Min(span1.Length, span2.Length);
            var v3      = Vector256.CreateScalarUnsafe(0f);
            var vectLen = Vector256 <float> .Count;
            var vectCnt = cnt / vectLen;

#if TEST
            var file = Path.GetTempFileName();
            using var writer = new StreamWriter(file);
            Console.WriteLine($"Intrinsic with Fma Mult. results will be written into {file}");
#endif

            int i;
            unsafe
            {
                for (i = 0; i < vectCnt; i++)
                {
                    var index = i * vectLen;
                    var v1    = Avx.LoadVector256((float *)Unsafe.AsPointer(ref span1[index]));
                    var v2    = Avx.LoadVector256((float *)Unsafe.AsPointer(ref span2[index]));
                    v3 = Fma.MultiplyAdd(v1, v2, v3);
#if TEST
                    writer.WriteLine($"{v1.ToString()}\t{v2.ToString()}\t{v3.ToString()}");
#endif
                }
            }

            var total = 0f;
            for (i = 0; i < vectLen; i++)
            {
                total += v3.GetElement(i);
            }

            for (i = vectCnt * vectLen; i < cnt; i++)
            {
                total += span1[i] * span2[i];
            }

            if (span1.Length != span2.Length)
            {
                var h = span1.Length > span2.Length ? span1 : span2;
                for (var j = cnt; j < h.Length; j++)
                {
                    total += h[j];
                }
            }

            return(total);
        }
Пример #5
0
 public static Vector256 <byte> create(byte src)
 {
     return(Vector256.CreateScalarUnsafe(src));
 }
Пример #6
0
        public static float DotMultiplyIntrinsicWFmaWSpanPtr(ref Memory <float> vector1, ref Memory <float> vector2)
        {
            var span1   = vector1.Span;
            var span2   = vector2.Span;
            var cnt     = Math.Min(span1.Length, span2.Length);
            var v3      = Vector256.CreateScalarUnsafe(0f);
            var vectLen = Vector256 <float> .Count;
            var vectCnt = cnt / vectLen;
            var total   = 0f;

#if TEST
            var file = Path.GetTempFileName();
            using var writer = new StreamWriter(file);
            Console.WriteLine($"Intrinsic with FmaWPtr Mult. results will be written into {file}");
#endif

            unsafe
            {
                int i;
                var ptr1 = (float *)Unsafe.AsPointer(ref span1[0]);
                var ptr2 = (float *)Unsafe.AsPointer(ref span2[0]);

                for (i = 0; i < vectCnt; i++)
                {
                    var v1 = Avx.LoadVector256(ptr1);
                    var v2 = Avx.LoadVector256(ptr2);
                    v3    = Fma.MultiplyAdd(v1, v2, v3);
                    ptr1 += vectLen;
                    ptr2 += vectLen;
#if TEST
                    writer.WriteLine($"{v1.ToString()}\t{v2.ToString()}\t{v3.ToString()}");
#endif
                }

                for (i = 0; i < vectLen; i++)
                {
                    total += v3.GetElement(i);
                }

                i = vectCnt * vectLen;
                if (cnt % vectLen > 0)
                {
                    ptr1 = (float *)Unsafe.AsPointer(ref span1[i]);
                    ptr2 = (float *)Unsafe.AsPointer(ref span2[i]);
                    for (; i < cnt; i++)
                    {
                        total += *ptr1++ **ptr2++;
                    }
                }
            }

            if (vector1.Length != vector2.Length)
            {
                var h = vector1.Length > vector2.Length ? span1 : span2;
                for (var j = cnt; j < h.Length; j++)
                {
                    total += h[j];
                }
            }

            return(total);
        }