Skip to content

Commit

Permalink
Merge pull request #4779 from YosysHQ/emil/han-carlson-adder
Browse files Browse the repository at this point in the history
Add a Han-Carlson option for `$lcu` mapping
  • Loading branch information
widlarizer authored Nov 29, 2024
2 parents 6f3376c + 3ebc714 commit e436cc0
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 3 deletions.
1 change: 1 addition & 0 deletions techlibs/common/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ $(eval $(call add_share_file,share,techlibs/common/abc9_unmap.v))
$(eval $(call add_share_file,share,techlibs/common/cmp2lcu.v))
$(eval $(call add_share_file,share,techlibs/common/cmp2softlogic.v))
$(eval $(call add_share_file,share/choices,techlibs/common/choices/kogge-stone.v))
$(eval $(call add_share_file,share/choices,techlibs/common/choices/han-carlson.v))
57 changes: 57 additions & 0 deletions techlibs/common/choices/han-carlson.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
(* techmap_celltype = "$lcu" *)
module _80_lcu_han_carlson (P, G, CI, CO);
parameter WIDTH = 2;

(* force_downto *)
input [WIDTH-1:0] P, G;
input CI;

(* force_downto *)
output [WIDTH-1:0] CO;

integer i, j;
(* force_downto *)
reg [WIDTH-1:0] p, g;

always @* begin
i = 0;
p = P;
g = G;

// in almost all cases CI will be constant zero
g[0] = g[0] | (p[0] & CI);
if (i < $clog2(WIDTH)) begin

// First layer: BK
for (j = WIDTH - 1; j >= 0; j = j - 1) begin
if (j % 2 == 1) begin
g[j] = g[j] | p[j] & g[j - 1];
p[j] = p[j] & p[j - 1];
end
end

// Inner (log(WIDTH) - 1) layers: KS
for (i = 1; i < $clog2(WIDTH); i = i + 1) begin
for (j = WIDTH - 1; j >= 2**i; j = j - 1) begin
if (j % 2 == 1) begin
g[j] = g[j] | p[j] & g[j - 2**i];
p[j] = p[j] & p[j - 2**i];
end
end
end

// Last layer: BK
if (i < ($clog2(WIDTH) + 1)) begin
for (j = WIDTH - 1; j >= 0; j = j - 1) begin
if ((j % 2 == 0) && (j > 0)) begin
g[j] = g[j] | p[j] & g[j - 1];
p[j] = p[j] & p[j - 1];
end
end
end

end
end

assign CO = g;
endmodule
19 changes: 18 additions & 1 deletion tests/gen-tests-makefile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ generate_ys_test() {
generate_target "$ys_file" "\"$YOSYS_BASEDIR/yosys\" -ql ${ys_file%.*}.log $yosys_args_ $ys_file"
}

# $ generate_tcl_test tcl_file [yosys_args]
generate_tcl_test() {
tcl_file=$1
yosys_args_=${2:-}
generate_target "$tcl_file" "\"$YOSYS_BASEDIR/yosys\" -ql ${tcl_file%.*}.log $yosys_args_ $tcl_file"
}

# $ generate_bash_test bash_file
generate_bash_test() {
bash_file=$1
Expand All @@ -29,6 +36,7 @@ generate_bash_test() {
# $ generate_tests [-y|--yosys-scripts] [-s|--prove-sv] [-b|--bash] [-a|--yosys-args yosys_args]
generate_tests() {
do_ys=false
do_tcl=false
do_sv=false
do_sh=false
yosys_args=""
Expand All @@ -40,6 +48,10 @@ generate_tests() {
do_ys=true
shift
;;
-t|--tcl-scripts)
do_tcl=true
shift
;;
-s|--prove-sv)
do_sv=true
shift
Expand All @@ -59,7 +71,7 @@ generate_tests() {
esac
done

if [[ ! ( $do_ys = true || $do_sv = true || $do_sh = true ) ]]; then
if [[ ! ( $do_ys = true || $do_tcl = true || $do_sv = true || $do_sh = true ) ]]; then
echo >&2 "Error: No file types selected"
exit 1
fi
Expand All @@ -72,6 +84,11 @@ generate_tests() {
generate_ys_test "$x" "$yosys_args"
done
fi;
if [[ $do_tcl = true ]]; then
for x in *.tcl; do
generate_tcl_test "$x" "$yosys_args"
done
fi;
if [[ $do_sv = true ]]; then
for x in *.sv; do
if [ ! -f "${x%.sv}.ys" ]; then
Expand Down
15 changes: 15 additions & 0 deletions tests/techmap/han-carlson.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
yosys -import

read_verilog +/choices/han-carlson.v
read_verilog -icells lcu_refined.v
design -save init

for {set i 1} {$i <= 16} {incr i} {
design -load init
chparam -set WIDTH $i
yosys proc
opt_clean
equiv_make lcu _80_lcu_han_carlson equiv
equiv_simple equiv
equiv_status -assert equiv
}
15 changes: 15 additions & 0 deletions tests/techmap/kogge-stone.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
yosys -import

read_verilog +/choices/kogge-stone.v
read_verilog -icells lcu_refined.v
design -save init

for {set i 1} {$i <= 16} {incr i} {
design -load init
chparam -set WIDTH $i
yosys proc
opt_clean
equiv_make lcu _80_lcu_kogge_stone equiv
equiv_simple equiv
equiv_status -assert equiv
}
1 change: 0 additions & 1 deletion tests/techmap/kogge-stone.ys

This file was deleted.

13 changes: 13 additions & 0 deletions tests/techmap/lcu_refined.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module lcu (P, G, CI, CO);
parameter WIDTH = 2;

input [WIDTH-1:0] P, G;
input CI;

output [WIDTH-1:0] CO;

reg [WIDTH-1:0] p, g;

\$lcu #(.WIDTH(WIDTH)) impl (.P(P), .G(G), .CI(CI), .CO(CO));

endmodule
2 changes: 1 addition & 1 deletion tests/techmap/run-test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
set -eu
source ../gen-tests-makefile.sh
run_tests --yosys-scripts --bash --yosys-args "-e 'select out of bounds'"
run_tests --yosys-scripts --tcl-scripts --bash --yosys-args "-e 'select out of bounds'"

0 comments on commit e436cc0

Please sign in to comment.