8000 examples: add gcd by proppy · Pull Request #935 · google/xls · GitHub
[go: up one dir, main page]

Skip to content

examples: add gcd - #935

Merged
copybara-service[bot] merged 2 commits into
google:mainfrom
proppy:gcd
May 31, 2024
Merged

copybara-service[bot] merged 2 commits into
google:mainfrom
proppy:gcd

Conversation

@proppy
@proppy proppy commented Apr 20, 2023
Copy link
Copy Markdown
Member

@proppy
proppy commented Apr 20, 2023
Copy link
Copy Markdown
Member Author

rebased

@proppy
proppy commented Apr 20, 2023
Copy link
Copy Markdown
Member Author

/cc @mdagois

Comment thread xls/examples/gcd.x Outdated
// https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclidean_algorithm
fn gcd_euclidean<N: u32, DN: u32 = {N * u32:2}>(a: uN[N], b: uN[N]) -> uN[N] {
let (_, gcd) = for (i, (a, b)) in range(u32:0, DN) {
let (d, r) = std::iterative_binary_div(a, b);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should move this in the else case, so that we don't perform unnecessary division when the remainder is 0.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand. How can this move into the else case if the result of the computation is the condition?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think gcd is a good candidate for proc implementations, which would let you do the "early return" I think you're describing. Each proc tick would do one division (or gcd_binary_match), and you send the output when you reach the stop condition.

Fine to leave as a TODO, and these function implementations are nice because they work well w/ quickcheck.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand. How can this move into the else case if the result of the computation is the condition?

@meheff since we always pass r to the next iteration, I was thinking we could sel based on the result of the previous division, rather than always performing the same division again and again until the end of the loop.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think gcd is a good candidate for proc implementations, which would let you do the "early return" I think you're describing.

It could be interesting to compare a proc implementation to the automatically pipelined version.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@meheff since we always pass r to the next iteration, I was thinking we could sel based on the result of the previous division, rather than always performing the same division again and again until the end of the loop.

As discussed simplified the implementation to only trigger div_mod in for recurrence w/ a non-zero modulo.

This apparently result in a reduced critical path and bom, see https://colab.research.google.com/gist/proppy/01142e33a2be8b71d6b317ee4ce7fde4/xls-playground-sandbox.ipynb

@grebe grebe left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/dslx/stdlib/std.x Resolved
Comment thread xls/examples/gcd.x Outdated
// https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclidean_algorithm
fn gcd_euclidean<N: u32, DN: u32 = {N * u32:2}>(a: uN[N], b: uN[N]) -> uN[N] {
let (_, gcd) = for (i, (a, b)) in range(u32:0, DN) {
let (d, r) = std::iterative_binary_div(a, b);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think gcd is a good candidate for proc implementations, which would let you do the "early return" I think you're describing. Each proc tick would do one division (or gcd_binary_match), and you send the output when you reach the stop condition.

Fine to leave as a TODO, and these function implementations are nice because they work well w/ quickcheck.

@cdleary cdleary added the app Application level functionality (examples, uses of XLS stack) label Mar 27, 2024
@proppy
proppy commented May 31, 2024
Copy link
Copy Markdown
Member Author

I think gcd is a good candidate for proc implementations

Filed #1444

@proppy
proppy commented May 31, 2024
Copy link
Copy Markdown
Member Author

PTAL

@copybara-service
copybara-service Bot merged commit d2da104 into google:main May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app Application level functionality (examples, uses of XLS stack) Reviewing Internally

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

0