-
Notifications
You must be signed in to change notification settings - Fork 167
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
examples: add gcd #935
examples: add gcd #935
Conversation
rebased |
/cc @mdagois |
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); |
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.
should move this in the else case, so that we don't perform unnecessary division when the remainder is 0
.
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 sure I understand. How can this move into the else case if the result of the computation is the condition?
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 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.
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 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.
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 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.
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.
@meheff since we always pass
r
to the next iteration, I was thinking we couldsel
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
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 great!
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); |
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 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.
Filed #1444 |
PTAL |
gcd_euclidean
gcd_binary
quicktest