Beispiel #1
0
        /// <summary>
        /// Sum the squares of each item in the source.
        /// </summary>
        /// <param name="source">The source values.</param>
        /// <returns>The sum of the squares of all items in <paramref name="source"/>.</returns>
        public static float SumSq(ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (Avx.IsSupported)
            {
                return(AvxIntrinsics.SumSqU(source));
            }
            else if (Sse.IsSupported)
            {
                return(SseIntrinsics.SumSqU(source));
            }
            else
            {
                float result = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    result += source[i] * source[i];
                }
                return(result);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Sum the square of each item in the source and subtract the mean.
        /// </summary>
        /// <param name="mean">The mean value.</param>
        /// <param name="source">The source values.</param>
        /// <returns>The sum of all items in <paramref name="source"/> by <paramref name="mean"/>.</returns>
        public static float SumSq(float mean, ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (Avx.IsSupported)
            {
                return((mean == 0) ? AvxIntrinsics.SumSqU(source) : AvxIntrinsics.SumSqDiffU(mean, source));
            }
            else if (Sse.IsSupported)
            {
                return((mean == 0) ? SseIntrinsics.SumSqU(source) : SseIntrinsics.SumSqDiffU(mean, source));
            }
            else
            {
                float result = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    result += (source[i] - mean) * (source[i] - mean);
                }
                return(result);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Sum the absolute value of each item in the source and subtract the mean.
        /// </summary>
        /// <param name="mean">The mean value.</param>
        /// <param name="source">The source values.</param>
        /// <returns>The sum of all items by absolute value in <paramref name="source"/> subtracted by <paramref name="mean"/>.</returns>
        public static float SumAbs(float mean, ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (Avx.IsSupported)
            {
                return((mean == 0) ? AvxIntrinsics.SumAbsU(source) : AvxIntrinsics.SumAbsDiffU(mean, source));
            }
            else if (Sse.IsSupported)
            {
                return((mean == 0) ? SseIntrinsics.SumAbsU(source) : SseIntrinsics.SumAbsDiffU(mean, source));
            }
            else
            {
                float sum = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    sum += Math.Abs(source[i] - mean);
                }
                return(sum);
            }
        }
        public static float SumAbs(ReadOnlySpan <float> source)
        {
            Contracts.AssertNonEmpty(source);

            if (source.Length < MinInputSize || !Sse.IsSupported)
            {
                float sum = 0;
                for (int i = 0; i < source.Length; i++)
                {
                    sum += Math.Abs(source[i]);
                }
                return(sum);
            }
            else if (Avx.IsSupported)
            {
                return(AvxIntrinsics.SumAbsU(source));
            }
            else
            {
                return(SseIntrinsics.SumAbsU(source));
            }
        }
Beispiel #5
0
        /// <summary>
        /// Add from a source to a destination.
        /// </summary>
        /// <param name="source">The source values.</param>
        /// <param name="destination">The destination values.</param>
        /// <param name="count">The count of items.</param>
        public static void Add(ReadOnlySpan <float> source, Span <float> destination, int count)
        {
            Contracts.AssertNonEmpty(source);
            Contracts.AssertNonEmpty(destination);
            Contracts.Assert(count > 0);
            Contracts.Assert(count <= source.Length);
            Contracts.Assert(count <= destination.Length);

            if (Avx.IsSupported)
            {
                AvxIntrinsics.AddU(source, destination, count);
            }
            else if (Sse.IsSupported)
            {
                SseIntrinsics.AddU(source, destination, count);
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    destination[i] += source[i];
                }
            }
        }
        public static void AddScale(float scale, ReadOnlySpan <float> source, Span <float> destination, int count)
        {
            Contracts.AssertNonEmpty(source);
            Contracts.AssertNonEmpty(destination);
            Contracts.Assert(count > 0);
            Contracts.Assert(count <= source.Length);
            Contracts.Assert(count <= destination.Length);

            if (destination.Length < MinInputSize || !Sse.IsSupported)
            {
                for (int i = 0; i < count; i++)
                {
                    destination[i] += scale * source[i];
                }
            }
            else if (Avx.IsSupported)
            {
                AvxIntrinsics.AddScaleU(scale, source, destination, count);
            }
            else
            {
                SseIntrinsics.AddScaleU(scale, source, destination, count);
            }
        }
 public void ManagedAddSUPerf()
 {
     SseIntrinsics.AddSU(new Span <float>(src), new Span <int>(idx, 0, IDXLEN), new Span <float>(dst));
 }
 public void ManagedAddUPerf()
 {
     SseIntrinsics.AddU(new Span <float>(src, 0, LEN), new Span <float>(dst, 0, LEN));
 }
 public void ManagedAddScaleSUPerf()
 {
     SseIntrinsics.AddScaleSU(DEFAULT_SCALE, new Span <float>(src), new Span <int>(idx, 0, IDXLEN), new Span <float>(dst));
 }
Beispiel #10
0
 public float MaxAbsDiffU()
 => SseIntrinsics.MaxAbsDiffU(DefaultScale, new Span <float>(src, 0, Length));
Beispiel #11
0
 public float MaxAbsU()
 => SseIntrinsics.MaxAbsU(new Span <float>(src, 0, Length));
 public void ManagedSdcaL1UpdateUPerf()
 {
     SseIntrinsics.SdcaL1UpdateU(DEFAULT_SCALE, new Span <float>(src, 0, LEN), DEFAULT_SCALE, new Span <float>(dst, 0, LEN), new Span <float>(result, 0, LEN));
 }
 public float ManagedDotSUPerf()
 {
     return(SseIntrinsics.DotSU(new Span <float>(src), new Span <float>(dst), new Span <int>(idx, 0, IDXLEN)));
 }
 public float ManagedMaxAbsUPerf()
 {
     return(SseIntrinsics.MaxAbsU(new Span <float>(src, 0, LEN)));
 }
 public void ManagedMulElementWiseUPerf()
 {
     SseIntrinsics.MulElementWiseU(new Span <float>(src1, 0, LEN), new Span <float>(src2, 0, LEN),
                                   new Span <float>(dst, 0, LEN));
 }
Beispiel #16
0
 public void SdcaL1UpdateSU()
 => SseIntrinsics.SdcaL1UpdateSU(DefaultScale, IndexLength, src, idx, DefaultScale, dst, result);
Beispiel #17
0
 public void SdcaL1UpdateU()
 => SseIntrinsics.SdcaL1UpdateU(DefaultScale, Length, src, DefaultScale, dst, result);
Beispiel #18
0
 public float Dist2()
 => SseIntrinsics.Dist2(src, dst, Length);
Beispiel #19
0
 public float DotSU()
 => SseIntrinsics.DotSU(src, dst, idx, IndexLength);
Beispiel #20
0
 public float DotU()
 => SseIntrinsics.DotU(src, dst, Length);
 public float ManagedSumSqUPerf()
 {
     return(SseIntrinsics.SumSqU(new Span <float>(src, 0, LEN)));
 }
Beispiel #22
0
        /// <summary>
        /// Multiplies a matrix times a source.
        /// </summary>
        /// <param name="transpose"><see langword="true"/> to transpose the matrix; otherwise <see langword="false"/>.</param>
        /// <param name="matrix">The input matrix.</param>
        /// <param name="source">The source matrix.</param>
        /// <param name="destination">The destination matrix.</param>
        /// <param name="stride">The column stride.</param>
        public static void MatrixTimesSource(bool transpose, AlignedArray matrix, AlignedArray source, AlignedArray destination, int stride)
        {
            Contracts.Assert(matrix.Size == destination.Size * source.Size);
            Contracts.Assert(stride >= 0);

            if (Avx.IsSupported)
            {
                if (!transpose)
                {
                    Contracts.Assert(stride <= destination.Size);
                    AvxIntrinsics.MatMul(matrix, source, destination, stride, source.Size);
                }
                else
                {
                    Contracts.Assert(stride <= source.Size);
                    AvxIntrinsics.MatMulTran(matrix, source, destination, destination.Size, stride);
                }
            }
            else if (Sse.IsSupported)
            {
                if (!transpose)
                {
                    Contracts.Assert(stride <= destination.Size);
                    SseIntrinsics.MatMul(matrix, source, destination, stride, source.Size);
                }
                else
                {
                    Contracts.Assert(stride <= source.Size);
                    SseIntrinsics.MatMulTran(matrix, source, destination, destination.Size, stride);
                }
            }
            else
            {
                if (!transpose)
                {
                    Contracts.Assert(stride <= destination.Size);
                    for (int i = 0; i < stride; i++)
                    {
                        float dotProduct = 0;
                        for (int j = 0; j < source.Size; j++)
                        {
                            dotProduct += matrix[i * source.Size + j] * source[j];
                        }

                        destination[i] = dotProduct;
                    }
                }
                else
                {
                    Contracts.Assert(stride <= source.Size);
                    for (int i = 0; i < destination.Size; i++)
                    {
                        float dotProduct = 0;
                        for (int j = 0; j < stride; j++)
                        {
                            dotProduct += matrix[j * destination.Size + i] * source[j];
                        }

                        destination[i] = dotProduct;
                    }
                }
            }
        }
 public float ManagedMaxAbsDiffUPerf()
 {
     return(SseIntrinsics.MaxAbsDiffU(DEFAULT_SCALE, new Span <float>(src, 0, LEN)));
 }
 public void ManagedScaleAddUPerf()
 {
     SseIntrinsics.ScaleAddU(DEFAULT_SCALE, DEFAULT_SCALE, new Span <float>(dst, 0, LEN));
 }
 public float ManagedDist2Perf()
 {
     return(SseIntrinsics.Dist2(new Span <float>(src, 0, LEN), new Span <float>(dst, 0, LEN)));
 }
Beispiel #26
0
 public float SumSqU()
 => SseIntrinsics.SumSqU(new Span <float>(src, 0, Length));
 public void ManagedSdcaL1UpdateSUPerf()
 {
     SseIntrinsics.SdcaL1UpdateSU(DEFAULT_SCALE, new Span <float>(src, 0, IDXLEN), new Span <int>(idx, 0, IDXLEN), DEFAULT_SCALE, new Span <float>(dst), new Span <float>(result));
 }
Beispiel #28
0
 public float SumSqDiffU()
 => SseIntrinsics.SumSqDiffU(DefaultScale, new Span <float>(src, 0, Length));
 public void ManagedAddScaleUPerf()
 {
     SseIntrinsics.AddScaleU(DEFAULT_SCALE, new Span <float>(src, 0, LEN), new Span <float>(dst, 0, LEN));
 }
Beispiel #30
0
 public void MulElementWiseU()
 => SseIntrinsics.MulElementWiseU(src1, src2, dst, Length);