Skip to content

Commit

Permalink
Consolidate a few exception throws in TensorPrimitives (dotnet#93168)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored and michaelgsharp committed Oct 20, 2023
1 parent 8db0a9b commit cd02aa5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@ public static float Distance(ReadOnlySpan<float> x, ReadOnlySpan<float> y)
ThrowHelper.ThrowArgument_SpansMustBeNonEmpty();
}

if (x.Length != y.Length)
{
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
}

return MathF.Sqrt(Aggregate<SubtractSquaredOperator, AddOperator>(x, y));
}

Expand Down Expand Up @@ -282,15 +277,8 @@ public static void Divide(ReadOnlySpan<float> x, float y, Span<float> destinatio
/// operating systems or architectures.
/// </para>
/// </remarks>
public static float Dot(ReadOnlySpan<float> x, ReadOnlySpan<float> y)
{
if (x.Length != y.Length)
{
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
}

return Aggregate<MultiplyOperator, AddOperator>(x, y);
}
public static float Dot(ReadOnlySpan<float> x, ReadOnlySpan<float> y) =>
Aggregate<MultiplyOperator, AddOperator>(x, y);

/// <summary>Computes the element-wise result of raising <c>e</c> to the single-precision floating-point number powers in the specified tensor.</summary>
/// <param name="x">The tensor, represented as a span.</param>
Expand Down Expand Up @@ -910,11 +898,6 @@ public static float ProductOfDifferences(ReadOnlySpan<float> x, ReadOnlySpan<flo
ThrowHelper.ThrowArgument_SpansMustBeNonEmpty();
}

if (x.Length != y.Length)
{
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
}

return Aggregate<SubtractOperator, MultiplyOperator>(x, y);
}

Expand Down Expand Up @@ -946,11 +929,6 @@ public static float ProductOfSums(ReadOnlySpan<float> x, ReadOnlySpan<float> y)
ThrowHelper.ThrowArgument_SpansMustBeNonEmpty();
}

if (x.Length != y.Length)
{
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
}

return Aggregate<AddOperator, MultiplyOperator>(x, y);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,10 @@ private static float Aggregate<TBinaryOperator, TAggregationOperator>(
where TBinaryOperator : struct, IBinaryOperator
where TAggregationOperator : struct, IAggregationOperator
{
Debug.Assert(x.Length == y.Length);
if (x.Length != y.Length)
{
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
}

if (x.IsEmpty)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ private static float Aggregate<TBinaryOperator, TAggregationOperator>(
where TBinaryOperator : struct, IBinaryOperator
where TAggregationOperator : struct, IAggregationOperator
{
Debug.Assert(x.Length == y.Length);
if (x.Length != y.Length)
{
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
}

if (x.Length == 0)
{
Expand Down

0 comments on commit cd02aa5

Please sign in to comment.