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

Math: replaces the gcd algorithm #2381

Merged
merged 3 commits into from
Mar 26, 2020

Conversation

IulianOlaru249
Copy link
Contributor

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]

@dbaluta
Copy link
Collaborator

dbaluta commented Feb 13, 2020

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.

Copy link
Collaborator

@paulstelian97 paulstelian97 left a 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.

@jajanusz
Copy link
Contributor

jajanusz commented Feb 13, 2020

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

@singalsu
Copy link
Collaborator

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?

@IulianOlaru249
Copy link
Contributor Author

@singalsu , not really. I was looking trough the code and thought of the algorithm.

@dbaluta dbaluta closed this Feb 13, 2020
@dbaluta
Copy link
Collaborator

dbaluta commented Feb 13, 2020

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.

Copy link
Member

@lgirdwood lgirdwood left a 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.

@paulstelian97
Copy link
Collaborator

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.

@paulstelian97 paulstelian97 reopened this Feb 14, 2020
@jajanusz
Copy link
Contributor

@IulianOlaru249 Please also change replaces to replace in commit title. We have imperative mood in our titles. Thanks

@lgirdwood
Copy link
Member

@singalsu can you review.

* 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:
Copy link
Collaborator

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.

src/math/numbers.c Outdated Show resolved Hide resolved
@singalsu
Copy link
Collaborator

@IulianOlaru249 How have you tested this? The test should include negative and positive numbers and specials like 0 and 1.

@dbaluta
Copy link
Collaborator

dbaluta commented Feb 20, 2020

@singalsu @IulianOlaru249 I think we can enhance the mocking tests right?

test/cmocka/src/math/numbers/gcd.c

@singalsu
Copy link
Collaborator

@dbaluta That's a good idea. Can you @IulianOlaru249 please add a commit for more test cases.

Copy link
Member

@lgirdwood lgirdwood left a 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.

Copy link
Member

@lgirdwood lgirdwood left a 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]>
@@ -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
*/

Copy link
Collaborator

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.

src/math/numbers.c Outdated Show resolved Hide resolved
src/math/numbers.c Outdated Show resolved Hide resolved
src/math/numbers.c Outdated Show resolved Hide resolved
src/math/numbers.c Outdated Show resolved Hide resolved
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]>
@lgirdwood
Copy link
Member

Jenkins CI known issues. @wwittbrx @lbetlej any clues on internal CI, I cant see the problem.

@jajanusz jajanusz requested a review from lgirdwood March 25, 2020 15:40
@jajanusz
Copy link
Contributor

@lgirdwood re-review pls

@lgirdwood
Copy link
Member

CI jenkins known issues.

@lgirdwood lgirdwood merged commit 0bd34f5 into thesofproject:master Mar 26, 2020
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

Successfully merging this pull request may close these issues.

6 participants