8000 examples: add a proc version of gcd by MaxonT · Pull Request #5570 · google/xls · GitHub
[go: up one dir, main page]

Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions xls/examples/gcd.x
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(explicit_state_access)]
#![feature(generics)]

import std;

// https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclidean_algorithm
Expand Down Expand Up @@ -45,6 +48,34 @@ fn gcd_binary<N: u32, DN: u32 = {N * 2}>(a: uN[N], b: uN[N]) -> uN[N] {
(a << d)
}

// A proc that computes the Euclidean GCD one step per tick: each tick performs
// a single modulo, so the result takes a data-dependent number of ticks, in
// contrast to the fixed-latency `gcd_euclidean` function above. A new request
// is accepted only while the proc is idle.
proc gcd_euclidean_proc<N: u32> {
request_r: chan<(uN[N], uN[N])> in,
result_s: chan<uN[N]> out,
// The current (a, b) pair, and whether a computation is in progress.
state: (uN[N], uN[N], bool),
}

impl gcd_euclidean_proc<N> {
fn new(request_r: chan<(uN[N], uN[N])> in, result_s: chan<uN[N]> out) -> Self {
Self { request_r, result_s, state: (uN[N]:0, uN[N]:0, false) }
}

fn next(self) {
let (a, b, busy) = read(self.state);
let (tok, request) = recv_if(join(), self.request_r, !busy, (uN[N]:0, uN[N]:0));
let (a, b) = if busy { (a, b) } else { request };

// gcd(a, 0) == a; otherwise take one Euclidean step: (a, b) -> (b, a % b).
let done = b == uN[N]:0;
send_if(tok, self.result_s, done, a);
write(self.state, if done { (uN[N]:0, uN[N]:0, false) } else { (b, a % b, true) });
}
}

#[test]
fn gcd_euclidean_test() {
assert_eq(u8:6, gcd_euclidean(u8:48, u8:18));
Expand All @@ -59,3 +90,40 @@ fn gcd_binary_test() {

#[quickcheck(test_count=50000)]
fn prop_gcd_equal(a: u32, b: u32) -> bool { gcd_euclidean<32>(a, b) == gcd_binary<32>(a, b) }

#[test]
proc gcd_euclidean_proc_test {
request_s: chan<(u8, u8)> out,
result_r: chan<u8> in,
terminator: chan<bool> out,
}

impl gcd_euclidean_proc_test {
fn new(terminator: chan<bool> out) -> Self {
let (request_s, request_r) = chan<(u8, u8)>("request");
let (result_s, result_r) = chan<u8>("result");
gcd_euclidean_proc<u32:8>::new(request_r, result_s).spawn();
Self { request_s, result_r, terminator }
}

fn next(self) {
let tok = send(join(), self.request_s, (u8:48, u8:18));
let (tok, result) = recv(tok, self.result_r);
assert_eq(result, u8:6);

// Same inputs in the other order, and the edge cases b == 0 and a == 0.
let tok = send(tok, self.request_s, (u8:18, u8:48));
let (tok, result) = recv(tok, self.result_r);
assert_eq(result, u8:6);

let tok = send(tok, self.request_s, (u8:7, u8:0));
let (tok, result) = recv(tok, self.result_r);
assert_eq(result, u8:7);

let tok = send(tok, self.request_s, (u8:0, u8:9));
let (tok, result) = recv(tok, self.result_r);
assert_eq(result, u8:9);

let tok = send(tok, self.terminator, true);
}
}
0