-
Notifications
You must be signed in to change notification settings - Fork 349
Audio: Use generic saturation logic for improved efficiency #9170
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
Closed
ShriramShastry
wants to merge
1
commit into
thesofproject:main
from
ShriramShastry:optimize_saturation_fix
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When
xis big enough then(x - INT32_MIN)overflows, which is undefined behavior because it's signed.Same overflow issue above when
xis negative enough.Signed overflow is undefined behavior:
https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow
Code like this is being banned from the Linux kernel right now for security reasons:
https://lwn.net/Articles/979747/
There are compiler flags to define it but that would introduce a very subtle and hard to track dependency. For what benefit?
https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Signed-Overflow.html
Even ignoring undefined behavior, this code is much, much harder to follow than the previous one.
Can branch prediction ever matter for code that small?
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.
Yes- Please take a look at the low-level impacts through assembly comparision
The bitwise approach uses fewer branching instructions, making it more efficient as branch prediction may fail and cause pipeline stalls.
Bitwise Approach: Uses fewer branches, reducing the risk of pipeline stalls and improving efficiency.
Complexity: Despite appearing complex, bitwise operations provide stability and avoid undefined behavior from signed overflows.
Branch Prediction Impact: Even small code sections can significantly benefit from reduced branching
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.
@ShriramShastry agree this should improve branch prediction and instruction pipeline throughput, but I think its best to show a real world test i.e. take a piece of FW code that is a heavy user of these APIs and place some timestamps around usage. The timestamps can then be measured before/after the changes with a real workload to show improvement.
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.
Sure
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 don't think you understand what "undefined behavior" means. This is not a problem that shows up in assembly code.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thank you for your feedback. Marc.
I thank your attention to the complexity and stability provided by bitwise operations. However, I believe there might be a misunderstanding regarding the definition and implications of "undefined behavior" (UB) in the context of signed integer overflow.
Explanation:
Undefined Behavior (UB):
I went over the links shared above, In C and C++, "undefined behavior" refers to a situation where the language specification does not define what should happen. This can lead to unpredictable results as the compiler is free to generate any code for UB constructs.
A classic case of UB in C is signed integer overflow. For example, adding two large
int32_tvalues that exceed the range ofint32_tcan result in UB because the C standard does not define the outcome of overflow for signed integers. https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow#NoncompliantCodeExampleBitwise Operations:
The bitwise operations used in the provided code ensure that values are clamped/bounded within their respective ranges without causing overflow, thereby ensuring predictable behavior.
For example, the code for sat_int32:
Here, bitwise operations are used to safely bound
int64_tvalues to theint32_trange, avoiding the issues of signed overflow.Understanding in Assembly:
While undefined behavior often has unpredictable and compiler-dependent outcomes, bitwise operations tend to translate directly to assembly without causing UB.
Ensuring signed overflows are handled in C code avoids reliance on compiler-specific behavior and promotes consistent, predictable execution across different platforms.
Summary:
Bitwise operations provide a robust way to handle out-of-range values, preventing undefined behavior stemming from signed integer overflow in C. This ensures predictable, stable, and portable code execution.
If there is an aspect I might have overlooked regarding the specific implications on assembly code or if you meant something different by undefined behavior, please let me know, and I can address it further.
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.
This is not true because the shift happens after the undefined behavior.
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.
Assembly differences are interesting but they're not answering the question. The question was "Does this matter? (and how much)". For this sort of optimization to matter you need:
Most performance optimizations tend to be very "cruel" because they're correct in theory and they don't matter in practice. That's what Donald Knuth meant when he wrote "premature optimization is the root of all evil" https://en.wikiquote.org/wiki/Donald_Knuth