Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fixing 0 length issue with constructor - #107427

#107427
Merged
michaelgsharp merged 3 commits into
dotnet:maindotnet/runtime:mainfrom
michaelgsharp:tensor-span-monotonic-constructormichaelgsharp/runtime:tensor-span-monotonic-constructorCopy head branch name to clipboard
Sep 16, 2024
Merged

Fixing 0 length issue with constructor#107427
michaelgsharp merged 3 commits into
dotnet:maindotnet/runtime:mainfrom
michaelgsharp:tensor-span-monotonic-constructormichaelgsharp/runtime:tensor-span-monotonic-constructorCopy head branch name to clipboard

Conversation

@michaelgsharp

Copy link
Copy Markdown
Contributor

Fixes #106538 by modifying the check in the constructor to also throw an error if the base memory length is 0.

@rameel

rameel commented Sep 6, 2024

Copy link
Copy Markdown
Contributor

This expression can be simplified...

if ((maxElements >= span.Length && span.Length != 0) || (span.Length == 0 && maxElements > 0))
    ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

... to:

if (maxElements > 0 && maxElements >= span.Length)
    ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

Explanation:

For simplicity, let's represent maxElements as a and span.Length as b. Now, the condition looks like this:

if ((a >= b && b != 0) || (b == 0 && a > 0))
    error;

We can normalize the condition by switching the order of the expressions (b == 0 && a > 0), resulting in:

if ((a >= b && b != 0) || (a > 0 && b == 0))
    error;

This can be interpreted as:

if ((a >= b && b != 0)  // a > 0, b > 0, a >= b
 || (a > 0  && b == 0)) // a > 0, b = 0
    error;

Thus, we are only interested in cases where a > 0, which simplifies the second part of the expression.

In the end, we get:

if (a > 0 && a >= b)
    error;

The final expression will look like this:

if (maxElements > 0 && maxElements >= span.Length)
    ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

…ors/netcore/ReadOnlyTensorSpan.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>
@michaelgsharp

Copy link
Copy Markdown
Contributor Author

/backport to release/9.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/9.0: https://github.com/dotnet/runtime/actions/runs/10889241013

@michaelgsharp
michaelgsharp merged commit 9eef776 into dotnet:main Sep 16, 2024
@michaelgsharp
michaelgsharp deleted the tensor-span-monotonic-constructor branch September 16, 2024 19:18
jtschuster pushed a commit to jtschuster/runtime that referenced this pull request Sep 17, 2024
* fixing 0 length issue with constructor

* Update src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>

* comments from PR

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
sirntar pushed a commit to sirntar/runtime that referenced this pull request Sep 30, 2024
* fixing 0 length issue with constructor

* Update src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>

* comments from PR

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Numerics.Tensors Non-monotonic constructor errors

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.