8000 dslx/stdlib: add iterative_div_mod by proppy · Pull Request #932 · google/xls · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
85 changes: 61 additions & 24 deletions xls/dslx/stdlib/std.x
Original file line number Diff line number Diff line change
Expand Up @@ -199,35 +199,69 @@ fn smul_test() {
assert_eq(s4:0b1111, smul(s2:0b11, s2:0b01));
}

// Calculate x / y one bit at a time. This is an alternative to using
// the division operator '/' which may not synthesize nicely.
pub fn iterative_div<N: u32, DN: u32 = {N * u32:2}>(x: uN[N], y: uN[N]) -> uN[N] {
let init_shift_amount = ((N as uN[N]) - uN[N]:1);
let x = x as uN[DN];

let (_, _, _, div_result) = for
(idx, (shifted_y, shifted_index_bit, running_product, running_result)) in range(u32:0, N) {
// Increment running_result by current power of 2
// if the prodcut running_result * y < x.
let inc_running_result = running_result | shifted_index_bit;
let inc_running_product = running_product + shifted_y;
let (running_result, running_product) = if inc_running_product <= x {
(inc_running_result, inc_running_product)
// Returns unsigned division of `n` (N bits) and `d` (M bits) as quotient (N bits) and remainder (M
// bits).
/ 10000 / If dividing by `0`, returns alls `1`s for quotient and `n` for remainder.
Comment thread
ericastor marked this conversation as resolved.
Resolved
// Based on the implementation in //xls/ir/abstract_evaluator:UDivMod;
Comment thread
ericastor marked this conversation as resolved.
Resolved
// should be expected to use a large number of gates and have a slow
// critical path when using combinational codegen.
fn iterative_div_mod<N: u32, M: u32>(n: uN[N], d: uN[M]) -> (uN[N], uN[M]) {
// Zero extend divisor by 1 bit.
let divisor = d as uN[M + u32:1];

for (i, (q, r)): (u32, (uN[N], uN[M])) in range(u32:0, N) {
// Shift the next bit of n into r.
let r = r ++ n[(N - u32:1 - i)+:u1];
let (q, r) = if r >= divisor {
(q as uN[N - u32:1] ++ u1:1, r - divisor)
} else {
(running_result, running_product)
(q as uN[N - u32:1] ++ u1:0, r)
};
// Remove the MSB of r; guaranteed to be 0 because r < d.
(q, r[0:M as s32])
}((uN[N]:0, uN[M]:0))
}

// Returns unsigned division of `n` (N bits) and `d` (M bits) as quotient (N bits).
// If dividing by `0`, returns alls `1`s for quotient.
fn iterative_div<N: u32, M: u32>(n: uN[N], d: uN[M]) -> uN[N] {
let (q, r) = iterative_div_mod(n, d);
q
}

#[test]
fn iterative_div_mod_test() {
// Power of 2.
assert_eq((u4:0, u4:8), iterative_div_mod(u4:8, u4:15));
assert_eq((u4:1, u4:0), iterative_div_mod(u4:8, u4:8));
assert_eq((u4:2, u4:0), iterative_div_mod(u4:8, u4:4));
assert_eq((u4:4, u4:0), iterative_div_mod(u4:8, u4:2));
assert_eq((u4:8, u4:0), iterative_div_mod(u4:8, u4:1));
assert_eq((u4:8 / u4:0, u4:8), iterative_div_mod(u4:8, u4:0));
assert_eq((u4:15, u4:8), iterative_div_mod(u4:8, u4:0));

// Shift to next (lower) power of 2.
let shifted_y = shifted_y >> uN[N]:1;
let shifted_index_bit = shifted_index_bit >> uN[N]:1;
// Non-powers-of-2.
assert_eq((u32:6, u32:0), iterative_div_mod(u32:18, u32:3));
assert_eq((u32:6, u32:0), iterative_div_mod(u32:36, u32:6));
assert_eq((u32:6, u32:0), iterative_div_mod(u32:48, u32:8));
assert_eq((u32:20, u32:0), iterative_div_mod(u32:900, u32:45));

(shifted_y, shifted_index_bit, running_product, running_result)
}((
(y as uN[DN]) << (init_shift_amount as uN[DN]), uN[N]:1 << init_shift_amount, uN[DN]:0,
uN[N]:0,
));
// Results w/ remainder.
assert_eq((u32:6, u32:2), iterative_div_mod(u32:20, u32:3));
assert_eq((u32:6, u32:5), iterative_div_mod(u32:41, u32:6));
assert_eq((u32:6, u32:7), iterative_div_mod(u32:55, u32:8));
assert_eq((u32:20, u32:44), iterative_div_mod(u32:944, u32:45));

div_result
// Arbitrary width.
assert_eq((u5:6, u3:2), iterative_div_mod(u5:20, u3:3));
assert_eq((u6:6, u4:5), iterative_div_mod(u6:41, u4:6));
assert_eq((u6:6, u4:7), iterative_div_mod(u6:55, u4:8));
assert_eq((u10:20, u6:44), iterative_div_mod(u10:944, u6:45));

// Divide by 0.
assert_eq((u4:0xf, u4:8), iterative_div_mod(u4:8, u4:0));
assert_eq((u8:0xff, u8:64), iterative_div_mod(u8:64, u8:0));
assert_eq((u1:1, u1:0), iterative_div_mod(u1:0, u1:0));
}

#[test]
Expand All @@ -252,6 +286,9 @@ fn iterative_div_test() {
assert_eq(u32:6, iterative_div(u32:41, u32:6));
assert_eq(u32:6, iterative_div(u32:55, u32:8));
assert_eq(u32:20, iterative_div(u32:944, u32:45));

// Divide by 0.
assert_eq(u8:0xff, iterative_div(u8:64, u8:0));
}

// Returns the value of x-1 with saturation at 0.
Expand Down
0