-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[stdlib] [bugfix] Make memcmp function robust against overflow and provide correct SIMD implementation #2524
Conversation
Signed-off-by: Maxim Zaks <maxim.zaks@gmail.com>
Signed-off-by: Maxim Zaks <maxim.zaks@gmail.com>
Signed-off-by: Maxim Zaks <maxim.zaks@gmail.com>
stdlib/src/memory/memory.mojo
Outdated
var smaller = s1i < s2i | ||
var bigger = s1i > s2i | ||
if smaller or bigger: | ||
return -int(smaller) + int(bigger) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this work?
var smaller = s1i < s2i | |
var bigger = s1i > s2i | |
if smaller or bigger: | |
return -int(smaller) + int(bigger) | |
if s1i != s2i: | |
return 1 if s1i > s2i else -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that is even simpler, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do similar things to the SIMD part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually realised why I did it the way I did before I wanted to make a branchless implementation, but stoped half way :)
var result = 0
var i = 0
while i < count:
var s1i = s1[i]
var s2i = s2[i]
var smaller = s1i < s2i
var bigger = s1i > s2i
i += 1 + count * int(smaller or bigger)
result = -1 * int(smaller) + 1 * int(bigger)
return result
This would be branchless implementation, is a bit more complex but my guess it should be faster. Anyways let's go with the simple one for now. I will create a gist for myself. When we have benchmarks infrastructure setup we can do such micro optimisations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhat relevant reference. I still think we could these to a cmp
function in SIMD
so you can use as much trick as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@soraros you were right regarding simplifying SIMD part, I just pushed an update.
|
Co-authored-by: soraros <soraros@users.noreply.github.com> Signed-off-by: Maxim Zaks <maxim.zaks@gmail.com>
Signed-off-by: Maxim Zaks <maxim.zaks@gmail.com>
Signed-off-by: Maxim Zaks <maxim.zaks@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Thank you for the fixes! Always good to make the low-level functions robust to avoid lingering bugs here.
✅🟣 This contribution has been merged 🟣✅ Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours. We use Copybara to merge external contributions, click here to learn more. |
…flow and provide correct SIMD implementation (#39463) [External] [stdlib] [bugfix] Make memcmp function robust against overflow and provide correct SIMD implementation The bugs are reproduced in the unit tests. Co-authored-by: Maxim Zaks <maxim.zaks@gmail.com> Closes #2524 MODULAR_ORIG_COMMIT_REV_ID: 3a71a8adeca0ad82fbc127bf80d94e19b6f6aec0
…flow and provide correct SIMD implementation (#39463) [External] [stdlib] [bugfix] Make memcmp function robust against overflow and provide correct SIMD implementation The bugs are reproduced in the unit tests. Co-authored-by: Maxim Zaks <maxim.zaks@gmail.com> Closes modularml#2524 MODULAR_ORIG_COMMIT_REV_ID: 3a71a8adeca0ad82fbc127bf80d94e19b6f6aec0 Signed-off-by: Lukas Hermann <lukashermann28@gmail.com>
The bugs are reproduced in the unit tests.