Repository navigation
examples: add gcd - #935
Conversation
|
rebased |
|
/cc @mdagois |
| // 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.
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.
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.
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.
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.
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.
@meheff since we always pass
rto the next iteration, I was thinking we couldselbased 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
| // 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.
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_euclideangcd_binaryquicktest