-
Notifications
You must be signed in to change notification settings - Fork 319
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
Math: replaces the gcd algorithm #2381
Conversation
I wouldn't go that far without a real need. Looks like gcd is only used in 2 files and we don't have anyone complaining that gcd is slow. Current algorithm is easier to understand, is proved to work. So, NACK from me :). Please go for low hanging fruits now. |
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.
Not approving either, this introduces unneeded complexity. GCD is never called in any hot path so the additional performance (if it even is there) isn't worth the additional complexity. Code must be understandable by humans when there isn't a real benefit to the extra complexity.
If @singalsu is going to use GCD for some fancy processing we may keep it. Also @IulianOlaru249 would need to remove note about wikipedia from line # 9 |
The function gcd() is not used in execution time critical loops, only in initialization code. I'm not aware of such use for it. @IulianOlaru249 Does some of your development depend on faster version? |
@singalsu , not really. I was looking trough the code and thought of the algorithm. |
Closing this, thanks @IulianOlaru249 it was a really nice idea. We will revive it if at some point will see in tests that gcd is slowing us down. |
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 a win for me since it removes the % which is expensive on xtensa and other RISC architectures. We should also not assume gcd will only be used in cold paths. I would add extra comments with maybe a link to the algorithm.
Oh. Alright I see your point, then I guess I'll suggest reopening and once those extra comments for clarification are in there I will approve as well. |
@IulianOlaru249 Please also change |
475fcd4
to
6869c81
Compare
@singalsu can you review. |
src/math/numbers.c
Outdated
* If both parameters are 0, gcd(0, 0) returns 0 | ||
* If first parameters is 0 or second parameter is 0, gcd(0, b) returns b | ||
* and gcd(a, 0) returns a, because everything devides 0. | ||
* Link to algorithm: |
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 would add the link to algorithm at the beginning of the file. Like it was until now.
6869c81
to
e0d5159
Compare
@IulianOlaru249 How have you tested this? The test should include negative and positive numbers and specials like 0 and 1. |
@singalsu @IulianOlaru249 I think we can enhance the mocking tests right? test/cmocka/src/math/numbers/gcd.c |
@dbaluta That's a good idea. Can you @IulianOlaru249 please add a commit for more test cases. |
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.
Sorry @IulianOlaru249 ignore my previous approval, the last change is good, but these review fixes should be squashed into the top level feature patches.
3ce1f90
to
7f5ebfe
Compare
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 think we are good to merge once comments and commit message are updated. Code is fine.
This patch replaces the former gcd algorithm which was implemented with repetitive divisions with Stein's algorithm, whitch is a faster gcd algorithm. All operations are arithmetic shifts, comparisons and subtractions. Signed-off-by: Iulian Olaru <[email protected]>
This patch adds extra tests for the gcd of (0, 0), (a, 0), (0, b), (-a, -b), (-a, b), (a, -b) Signed-off-by: Iulian Olaru <[email protected]>
7f5ebfe
to
2b42951
Compare
@@ -5,6 +5,9 @@ | |||
// Author: Seppo Ingalsuo <[email protected]> | |||
// Liam Girdwood <[email protected]> | |||
// Keyon Jie <[email protected]> | |||
/* Binary GCD algorithm | |||
* https://en.wikipedia.org/wiki/Binary_GCD_algorithm | |||
*/ | |||
|
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 looks these lines above are added twice. They shouldn't be present in this patch.
2b42951
to
feaaa2e
Compare
This patch makes the binary gcd algorithm work for negative numbers by turning them into positive ones. Works since gcd(a, b) = gcd(-a, -b) = gcd(-a, b) = gcd(a, -b) Signed-off-by: Iulian Olaru <[email protected]>
feaaa2e
to
be5516a
Compare
@lgirdwood re-review pls |
CI jenkins known issues. |
This patch replaces the former gcd algorithm which was implemented with
repetitive divisions with Stein's algorithm, whitch is a faster gcd
algorithm. All operations are arithmetic shifts, comparisons and
subtractions.
Signed-off-by: Iulian Olaru [email protected]