public static float MaxAbsDiff(float mean, ReadOnlySpan <float> src) { Contracts.AssertNonEmpty(src); if (Avx.IsSupported) { return(AvxIntrinsics.MaxAbsDiffU(mean, src)); } else if (Sse.IsSupported) { return(SseIntrinsics.MaxAbsDiffU(mean, src)); } else { float max = 0; for (int i = 0; i < src.Length; i++) { float abs = Math.Abs(src[i] - mean); if (abs > max) { max = abs; } } return(max); } }
public static void Add(ReadOnlySpan <float> src, ReadOnlySpan <int> indices, Span <float> dst, int count) { Contracts.AssertNonEmpty(src); Contracts.AssertNonEmpty(indices); Contracts.AssertNonEmpty(dst); Contracts.Assert(count > 0); Contracts.Assert(count <= src.Length); Contracts.Assert(count <= indices.Length); Contracts.Assert(count < dst.Length); if (Avx.IsSupported) { AvxIntrinsics.AddSU(src, indices, dst, count); } else if (Sse.IsSupported) { SseIntrinsics.AddSU(src, indices, dst, count); } else { for (int i = 0; i < count; i++) { int index = indices[i]; dst[index] += src[i]; } } }
public static void MulElementWise(ReadOnlySpan <float> src1, ReadOnlySpan <float> src2, Span <float> dst, int count) { Contracts.AssertNonEmpty(src1); Contracts.AssertNonEmpty(src2); Contracts.AssertNonEmpty(dst); Contracts.Assert(count > 0); Contracts.Assert(count <= src1.Length); Contracts.Assert(count <= src2.Length); Contracts.Assert(count <= dst.Length); if (Avx.IsSupported) { AvxIntrinsics.MulElementWiseU(src1, src2, dst, count); } else if (Sse.IsSupported) { SseIntrinsics.MulElementWiseU(src1, src2, dst, count); } else { for (int i = 0; i < count; i++) { dst[i] = src1[i] * src2[i]; } } }
public static void AddScaleCopy(float a, ReadOnlySpan <float> src, ReadOnlySpan <float> dst, Span <float> res, int count) { Contracts.AssertNonEmpty(src); Contracts.AssertNonEmpty(dst); Contracts.AssertNonEmpty(res); Contracts.Assert(count > 0); Contracts.Assert(count <= src.Length); Contracts.Assert(count <= dst.Length); Contracts.Assert(count <= res.Length); if (Avx.IsSupported) { AvxIntrinsics.AddScaleCopyU(a, src, dst, res, count); } else if (Sse.IsSupported) { SseIntrinsics.AddScaleCopyU(a, src, dst, res, count); } else { for (int i = 0; i < count; i++) { res[i] = a * src[i] + dst[i]; } } }
public static void MatrixTimesSource(ReadOnlySpan <float> matrix, ReadOnlySpan <int> rgposSrc, ReadOnlySpan <float> sourceValues, int posMin, int iposMin, int iposLimit, Span <float> destination, int stride) { Contracts.Assert(iposMin >= 0); Contracts.Assert(iposMin <= iposLimit); Contracts.Assert(iposLimit <= rgposSrc.Length); Contracts.AssertNonEmpty(matrix); Contracts.AssertNonEmpty(sourceValues); Contracts.AssertNonEmpty(destination); Contracts.AssertNonEmpty(rgposSrc); Contracts.Assert(stride > 0); Contracts.Assert(matrix.Length == destination.Length * sourceValues.Length); if (iposMin >= iposLimit) { destination.Clear(); return; } Contracts.AssertNonEmpty(rgposSrc); Contracts.Assert(stride >= 0); if (Avx.IsSupported) { Contracts.Assert(stride <= destination.Length); AvxIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Length); } else if (Sse.IsSupported) { Contracts.Assert(stride <= destination.Length); SseIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Length); } else { Contracts.Assert(stride <= destination.Length); for (int i = 0; i < stride; i++) { float dotProduct = 0; for (int j = iposMin; j < iposLimit; j++) { int col = rgposSrc[j] - posMin; dotProduct += matrix[i * sourceValues.Length + col] * sourceValues[col]; } destination[i] = dotProduct; } } }
private static void MulElementWise(Span <float> src1, Span <float> src2, Span <float> dst) { if (Avx.IsSupported) { AvxIntrinsics.MulElementWiseU(src1, src2, dst); } else if (Sse.IsSupported) { SseIntrinsics.MulElementWiseU(src1, src2, dst); } else { for (int i = 0; i < dst.Length; i++) { dst[i] = src1[i] * src2[i]; } } }
private static void Add(float a, Span <float> dst) { if (Avx.IsSupported) { AvxIntrinsics.AddScalarU(a, dst); } else if (Sse.IsSupported) { SseIntrinsics.AddScalarU(a, dst); } else { for (int i = 0; i < dst.Length; i++) { dst[i] += a; } } }
private static void Scale(float a, Span <float> src, Span <float> dst) { if (Avx.IsSupported) { AvxIntrinsics.ScaleSrcU(a, src, dst); } else if (Sse.IsSupported) { SseIntrinsics.ScaleSrcU(a, src, dst); } else { for (int i = 0; i < dst.Length; i++) { dst[i] = a * src[i]; } } }
private static void ScaleAdd(float a, float b, Span <float> dst) { if (Avx.IsSupported) { AvxIntrinsics.ScaleAddU(a, b, dst); } else if (Sse.IsSupported) { SseIntrinsics.ScaleAddU(a, b, dst); } else { for (int i = 0; i < dst.Length; i++) { dst[i] = a * (dst[i] + b); } } }
private static void AddScaleCopy(float a, Span <float> src, Span <float> dst, Span <float> res) { if (Avx.IsSupported) { AvxIntrinsics.AddScaleCopyU(a, src, dst, res); } else if (Sse.IsSupported) { SseIntrinsics.AddScaleCopyU(a, src, dst, res); } else { for (int i = 0; i < res.Length; i++) { res[i] = a * src[i] + dst[i]; } } }
private static void Add(Span <float> src, Span <float> dst) { if (Avx.IsSupported) { AvxIntrinsics.AddU(src, dst); } else if (Sse.IsSupported) { SseIntrinsics.AddU(src, dst); } else { for (int i = 0; i < dst.Length; i++) { dst[i] += src[i]; } } }
public static void MatTimesSrc(AlignedArray mat, int[] rgposSrc, AlignedArray srcValues, int posMin, int iposMin, int iposLim, AlignedArray dst, int crun) { Contracts.AssertValue(rgposSrc); Contracts.Assert(iposMin >= 0); Contracts.Assert(iposMin <= iposLim); Contracts.Assert(iposLim <= rgposSrc.Length); Contracts.Assert(mat.Size == dst.Size * srcValues.Size); if (iposMin >= iposLim) { dst.ZeroItems(); return; } Contracts.AssertNonEmpty(rgposSrc); Contracts.Assert(crun >= 0); if (Avx.IsSupported) { Contracts.Assert(crun <= dst.Size); AvxIntrinsics.MatMulP(mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size); } else if (Sse.IsSupported) { Contracts.Assert(crun <= dst.Size); SseIntrinsics.MatMulP(mat, rgposSrc, srcValues, posMin, iposMin, iposLim, dst, crun, srcValues.Size); } else { Contracts.Assert(crun <= dst.Size); for (int i = 0; i < crun; i++) { float dotProduct = 0; for (int j = iposMin; j < iposLim; j++) { int col = rgposSrc[j] - posMin; dotProduct += mat[i * srcValues.Size + col] * srcValues[col]; } dst[i] = dotProduct; } } }
public static void MatrixTimesSource(AlignedArray matrix, ReadOnlySpan <int> rgposSrc, AlignedArray sourceValues, int posMin, int iposMin, int iposLimit, AlignedArray destination, int stride) { Contracts.AssertValue(rgposSrc); Contracts.Assert(iposMin >= 0); Contracts.Assert(iposMin <= iposLimit); Contracts.Assert(iposLimit <= rgposSrc.Length); Contracts.Assert(matrix.Size == destination.Size * sourceValues.Size); if (iposMin >= iposLimit) { destination.ZeroItems(); return; } Contracts.AssertNonEmpty(rgposSrc); Contracts.Assert(stride >= 0); if (Avx.IsSupported) { Contracts.Assert(stride <= destination.Size); AvxIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Size); } else if (Sse.IsSupported) { Contracts.Assert(stride <= destination.Size); SseIntrinsics.MatMulP(matrix, rgposSrc, sourceValues, posMin, iposMin, iposLimit, destination, stride, sourceValues.Size); } else { Contracts.Assert(stride <= destination.Size); for (int i = 0; i < stride; i++) { float dotProduct = 0; for (int j = iposMin; j < iposLimit; j++) { int col = rgposSrc[j] - posMin; dotProduct += matrix[i * sourceValues.Size + col] * sourceValues[col]; } destination[i] = dotProduct; } } }
private static void Add(Span <float> src, Span <int> indices, Span <float> dst) { if (Avx.IsSupported) { AvxIntrinsics.AddSU(src, indices, dst); } else if (Sse.IsSupported) { SseIntrinsics.AddSU(src, indices, dst); } else { for (int i = 0; i < indices.Length; i++) { int index = indices[i]; dst[index] += src[i]; } } }
private static float SumAbs(float mean, Span <float> src) { if (Avx.IsSupported) { return((mean == 0) ? AvxIntrinsics.SumAbsU(src) : AvxIntrinsics.SumAbsDiffU(mean, src)); } else if (Sse.IsSupported) { return((mean == 0) ? SseIntrinsics.SumAbsU(src) : SseIntrinsics.SumAbsDiffU(mean, src)); } else { float sum = 0; for (int i = 0; i < src.Length; i++) { sum += Math.Abs(src[i] - mean); } return(sum); } }
public static void Add(float value, Span <float> destination) { Contracts.AssertNonEmpty(destination); if (destination.Length < MinInputSize || !Sse.IsSupported) { for (int i = 0; i < destination.Length; i++) { destination[i] += value; } } else if (Avx.IsSupported) { AvxIntrinsics.AddScalarU(value, destination); } else { SseIntrinsics.AddScalarU(value, destination); } }
// dst[i] = a * (dst[i] + b) public static void ScaleAdd(float a, float b, Span <float> dst) { Contracts.AssertNonEmpty(dst); if (Avx.IsSupported) { AvxIntrinsics.ScaleAddU(a, b, dst); } else if (Sse.IsSupported) { SseIntrinsics.ScaleAddU(a, b, dst); } else { for (int i = 0; i < dst.Length; i++) { dst[i] = a * (dst[i] + b); } } }
public static void Scale(float value, Span <float> destination) { Contracts.AssertNonEmpty(destination); if (Avx.IsSupported) { AvxIntrinsics.Scale(value, destination); } else if (Sse.IsSupported) { SseIntrinsics.Scale(value, destination); } else { for (int i = 0; i < destination.Length; i++) { destination[i] *= value; } } }
// destination[i] = scale * (destination[i] + addend) public static void ScaleAdd(float scale, float addend, Span <float> destination) { Contracts.AssertNonEmpty(destination); if (Avx.IsSupported) { AvxIntrinsics.ScaleAddU(scale, addend, destination); } else if (Sse.IsSupported) { SseIntrinsics.ScaleAddU(scale, addend, destination); } else { for (int i = 0; i < destination.Length; i++) { destination[i] = scale * (destination[i] + addend); } } }
private static float DotProductDense(Span <float> a, Span <float> b) { if (Avx.IsSupported) { return(AvxIntrinsics.DotU(a, b)); } else if (Sse.IsSupported) { return(SseIntrinsics.DotU(a, b)); } else { float result = 0; for (int i = 0; i < b.Length; i++) { result += a[i] * b[i]; } return(result); } }
private static float SumSq(float mean, Span <float> src) { if (Avx.IsSupported) { return((mean == 0) ? AvxIntrinsics.SumSqU(src) : AvxIntrinsics.SumSqDiffU(mean, src)); } else if (Sse.IsSupported) { return((mean == 0) ? SseIntrinsics.SumSqU(src) : SseIntrinsics.SumSqDiffU(mean, src)); } else { float result = 0; for (int i = 0; i < src.Length; i++) { result += (src[i] - mean) * (src[i] - mean); } return(result); } }
private static void SdcaL1UpdateDense(float primalUpdate, Span <float> src, float threshold, Span <float> v, Span <float> w) { if (Avx.IsSupported) { AvxIntrinsics.SdcaL1UpdateU(primalUpdate, src, threshold, v, w); } else if (Sse.IsSupported) { SseIntrinsics.SdcaL1UpdateU(primalUpdate, src, threshold, v, w); } else { for (int i = 0; i < src.Length; i++) { v[i] += src[i] * primalUpdate; float value = v[i]; w[i] = Math.Abs(value) > threshold ? (value > 0 ? value - threshold : value + threshold) : 0; } } }
private static float SumSq(Span <float> src) { if (Avx.IsSupported) { return(AvxIntrinsics.SumSqU(src)); } else if (Sse.IsSupported) { return(SseIntrinsics.SumSqU(src)); } else { float result = 0; for (int i = 0; i < src.Length; i++) { result += src[i] * src[i]; } return(result); } }
private static float SumAbs(Span <float> src) { if (Avx.IsSupported) { return(AvxIntrinsics.SumAbsU(src)); } else if (Sse.IsSupported) { return(SseIntrinsics.SumAbsU(src)); } else { float sum = 0; for (int i = 0; i < src.Length; i++) { sum += Math.Abs(src[i]); } return(sum); } }
private static float L2DistSquared(Span <float> a, Span <float> b) { if (Avx.IsSupported) { return(AvxIntrinsics.Dist2(a, b)); } else if (Sse.IsSupported) { return(SseIntrinsics.Dist2(a, b)); } else { float norm = 0; for (int i = 0; i < b.Length; i++) { float distance = a[i] - b[i]; norm += distance * distance; } return(norm); } }
private static void SdcaL1UpdateSparse(float primalUpdate, Span <float> src, Span <int> indices, float threshold, Span <float> v, Span <float> w) { if (Avx.IsSupported) { AvxIntrinsics.SdcaL1UpdateSU(primalUpdate, src, indices, threshold, v, w); } else if (Sse.IsSupported) { SseIntrinsics.SdcaL1UpdateSU(primalUpdate, src, indices, threshold, v, w); } else { for (int i = 0; i < indices.Length; i++) { int index = indices[i]; v[index] += src[i] * primalUpdate; float value = v[index]; w[index] = Math.Abs(value) > threshold ? (value > 0 ? value - threshold : value + threshold) : 0; } } }
private static float DotProductSparse(Span <float> a, Span <float> b, Span <int> indices) { if (Avx.IsSupported) { return(AvxIntrinsics.DotSU(a, b, indices)); } else if (Sse.IsSupported) { return(SseIntrinsics.DotSU(a, b, indices)); } else { float result = 0; for (int i = 0; i < indices.Length; i++) { int index = indices[i]; result += a[index] * b[i]; } return(result); } }
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); } }
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); } }
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); } }