-
-
Notifications
You must be signed in to change notification settings - Fork 458
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
Use int16 min/max for _mm_set_epi16() calls #395
Conversation
This fixes a bug that causes g0 overflows when compiled on Intel processors and run on AMD Ryzen processors. According to Intel, the `_mm_set_epi16()` function takes signed shorts (int16): https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=mm_set_epi16&ig_expand=6355,6355 But 0-65535 is the range of uint16, not int16; causing an overflow to -1. This is warned of by the compiler: `implict conversion from int to short changes value from 65535 to -1` For whatever reason, compiling and running on AMD Ryzen chip succeeded fine. Compiling and runing on an Intel chip also worked fine. But compiling on Intel and running on AMD caused an exception. Using the int16 range fixes the problem. However, this is not my skill set and I am not sure if this is the right fix. Please verify. Thank you!
Hopefully. Still need to test and verify it. See upstream PR: strukturag/libde265#395
@farindk Just wondering what you think of this patch. Is it on the right track? It does resolve the crashes for me. |
libde265/x86/sse-motion.cc
Outdated
@@ -3527,9 +3527,9 @@ void ff_hevc_put_hevc_qpel_h_1_v_1_sse(int16_t *dst, ptrdiff_t dststride, | |||
r0 = _mm_srli_epi32(r0, 6); | |||
|
|||
r1 = _mm_and_si128(r1, | |||
_mm_set_epi16(0, 65535, 0, 65535, 0, 65535, 0, 65535)); | |||
_mm_set_epi16(-32766, 32767, -32766, 32767, -32766, 32767, -32766, 32767)); |
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.
@mholt I'm wondering why you have chosen these int16 values. The equivalent to (0, 65535, 0, 65535, ...)
would be (0, -1, 0, -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.
It was a best guess to preserve the range of ~65535, but if I recall correctly it also required some tinkering to prevent the exception.
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.
It is used as a simple bit-mask that is AND
ed with the input pixel vector.
Do you want to change it to (0, -1) so that I can take your PR or should I just make the type fix on my side?
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'll change it 👍 One sec.
I can understand that the compiler may issue a warning, but I don't see how that can lead to a crash. |
Thanks for the review!
I don't either. I wish I had preserved the logs until this day, but I think by now I've lost them. I do recall the crash happened when compiling on Intel and running on AMD (both amd64 architectures). (I don't have easy access to an Intel CPU anymore to replicate though.) That said, do you think this line should be fixed? I know for sure it resolved a warning and an exception in that edge case. But I admit I don't totally understand what it does. 😅 |
Okay, should be updated now. I appreciate the second opinion. I believe the crash may have to do with implementation differences between Intel and AMD for undefined behavior that the compiler warning was calling out. |
Thanks and sorry that the PR was unnoticed for so long. I'm still sceptical that this can lead to any crash. The worst case should be wrong decoded pixel values, but even that is unlikely. However, if you find any file that gives a crash, please send it to me and I'll have a look. |
Thanks, I'll keep an eye out and update if I have more information! Appreciate the merge. |
This fixes a bug that causes g0 overflows when compiled on Intel processors and run on AMD Ryzen processors.
According to Intel, the
_mm_set_epi16()
function takes signed shorts (int16): https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=mm_set_epi16&ig_expand=6355,6355But 0-65535 is the range of uint16, not int16; causing an overflow to -1. This is warned of by the compiler:
implict conversion from int to short changes value from 65535 to -1
For whatever reason, compiling and running on AMD Ryzen chip succeeded fine. Compiling and runing on an Intel chip also worked fine. But compiling on Intel and running on AMD caused an exception.
Using the int16 range fixes the problem. However, this is not my skill set and I am not sure if this is the right fix. Please verify. Thank you!