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

Failing test #1

Closed
Veedrac opened this issue Sep 27, 2016 · 0 comments
Closed

Failing test #1

Veedrac opened this issue Sep 27, 2016 · 0 comments

Comments

@Veedrac
Copy link
Collaborator

Veedrac commented Sep 27, 2016

This test fails

let haystack = vec![0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let needle = 2;
assert_eq!(count(&haystack[0..], needle), naive_count(&haystack[0..], needle));

giving a count of 2 rather than 1. The errors are with

(((x ^ needles).wrapping_sub(LO)) & !x & HI) >> 7

Firstly, this should be

let x = x ^ needles;
((x.wrapping_sub(LO)) & !x & HI) >> 7

Secondly, this doesn't actually work for counting - the x.wrapping_sub(LO) sets the high bit correctly but can overflow into the next value if the next byte is 1 - wrapping_sub will decrement that to zero and the subtraction will continue past.

It's fairly simple to deal with by using

!((((v & !HI) + !HI) | v) >> 7) & LO

instead, but it'd be interesting to see if you don't have to spend the extra operations to get that. It doesn't seem to affect speed all too much, though.

@llogiq llogiq closed this as completed in 52b177d Sep 27, 2016
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

1 participant