Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] ContainsS returns different result to Contains #31

Open
Lukazoid opened this issue Apr 27, 2022 · 4 comments
Open

[BUG] ContainsS returns different result to Contains #31

Lukazoid opened this issue Apr 27, 2022 · 4 comments

Comments

@Lukazoid
Copy link

Lukazoid commented Apr 27, 2022

void Main()
{
    var max = 999999;

    var nums = Enumerable.Range(0, max + 1).ToArray();

    Console.WriteLine(nums.ContainsS(max));
    Console.WriteLine(nums.Contains(max));
}

Outputs

False
True

Or another more simple example:

Console.WriteLine(new[]{1,2,3,4,5,6,7,8}.ContainsS(5));

It appears that .ContainsS fails for any of the last Vector<T>.Count elements when (haystack.Length % Vector<T>.Count) == 0

@yenmoc
Copy link

yenmoc commented May 12, 2022

I tried your example on Unity but the same result is true

@Lukazoid
Copy link
Author

Lukazoid commented May 13, 2022

@yenmoc Do the .NET SIMD types work under Unity? Are you sure it isn't just running without hardware acceleration (what's the result of Vector.IsHardwareAccelerated?)

Interesting if that is so as this definitely fails under .NET 6

@Lukazoid
Copy link
Author

Lukazoid commented May 13, 2022

And this fails in particular when (nums.Length % Vector<T>.Count) == 0 so with Vector<int>.Count == 8 the following values of max all fail: 7, 15, 23 etc

And the needle to .ContainsS can be any of the last 8 values, e.g. Console.WriteLine(new[]{1,2,3,4,5,6,7,8}.ContainsS(5)); also outputs False.

@yenmoc
Copy link

yenmoc commented May 13, 2022

Sorry my previous answer was because i didn't read your question carefully

In ContainsSIMD.cs

for (int i = 0; i < source.Length - count; i += count)
 {
                var v = new Vector<T>(source, i);
                if (Vector.EqualsAny(v, vectorValue))
                {
                    return true;
                }
  }

Change to

for (int i = 0; i <= source.Length - count; i += count)
 {
                var v = new Vector<T>(source, i);
                if (Vector.EqualsAny(v, vectorValue))
                {
                    return true;
                }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants