From c38201e15d457388b26d712df4aa10266015c22d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Mon, 25 Mar 2024 14:56:17 +0100 Subject: [PATCH 1/5] techmap: Add a Kogge-Stone option for `$lcu` mapping --- techlibs/common/techmap.v | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/techlibs/common/techmap.v b/techlibs/common/techmap.v index 7fb8173b069..38ca33cb947 100644 --- a/techlibs/common/techmap.v +++ b/techlibs/common/techmap.v @@ -230,6 +230,7 @@ module _90_lcu (P, G, CI, CO); // in almost all cases CI will be constant zero g[0] = g[0] | (p[0] & CI); +`ifndef KOGGE_STONE // [[CITE]] Brent Kung Adder // R. P. Brent and H. T. Kung, "A Regular Layout for Parallel Adders", // IEEE Transaction on Computers, Vol. C-31, No. 3, p. 260-264, March, 1982 @@ -249,6 +250,14 @@ module _90_lcu (P, G, CI, CO); p[j] = p[j] & p[j - 2**(i-1)]; end end +`else + for (i = 0; i < $clog2(WIDTH); i = i + 1) begin + for (j = 2**i; j < WIDTH; j = j + 1) begin + g[j] = g[j] | p[j] & g[j - 2**i]; + p[j] = p[j] & p[j - 2**i]; + end + end +`endif end assign CO = g; From 4570d064e5bac128f4057874457e306d4e275eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Mon, 25 Mar 2024 18:39:55 +0100 Subject: [PATCH 2/5] techmap: Split out Kogge-Stone into a separate file --- techlibs/common/Makefile.inc | 1 + techlibs/common/choices/kogge-stone.v | 53 +++++++++++++++++++++++++++ techlibs/common/techmap.v | 11 +----- 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 techlibs/common/choices/kogge-stone.v diff --git a/techlibs/common/Makefile.inc b/techlibs/common/Makefile.inc index 6b377855e3e..fa5c2a825de 100644 --- a/techlibs/common/Makefile.inc +++ b/techlibs/common/Makefile.inc @@ -35,3 +35,4 @@ $(eval $(call add_share_file,share,techlibs/common/abc9_map.v)) $(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)) diff --git a/techlibs/common/choices/kogge-stone.v b/techlibs/common/choices/kogge-stone.v new file mode 100644 index 00000000000..eb97a75d831 --- /dev/null +++ b/techlibs/common/choices/kogge-stone.v @@ -0,0 +1,53 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2024 Martin PoviĊĦer + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +(* techmap_celltype = "$lcu" *) +module _80_lcu_kogge_stone (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; + + wire [1023:0] _TECHMAP_DO_ = "proc; opt -fast"; + + always @* begin + p = P; + g = G; + + // in almost all cases CI will be constant zero + g[0] = g[0] | (p[0] & CI); + + for (i = 0; i < $clog2(WIDTH); i = i + 1) begin + for (j = 2**i; j < WIDTH; j = j + 1) begin + g[j] = g[j] | p[j] & g[j - 2**i]; + p[j] = p[j] & p[j - 2**i]; + end + end + end + + assign CO = g; +endmodule diff --git a/techlibs/common/techmap.v b/techlibs/common/techmap.v index 38ca33cb947..68b276588b5 100644 --- a/techlibs/common/techmap.v +++ b/techlibs/common/techmap.v @@ -207,7 +207,7 @@ module _90_fa (A, B, C, X, Y); endmodule (* techmap_celltype = "$lcu" *) -module _90_lcu (P, G, CI, CO); +module _90_lcu_brent_kung (P, G, CI, CO); parameter WIDTH = 2; (* force_downto *) @@ -230,7 +230,6 @@ module _90_lcu (P, G, CI, CO); // in almost all cases CI will be constant zero g[0] = g[0] | (p[0] & CI); -`ifndef KOGGE_STONE // [[CITE]] Brent Kung Adder // R. P. Brent and H. T. Kung, "A Regular Layout for Parallel Adders", // IEEE Transaction on Computers, Vol. C-31, No. 3, p. 260-264, March, 1982 @@ -250,14 +249,6 @@ module _90_lcu (P, G, CI, CO); p[j] = p[j] & p[j - 2**(i-1)]; end end -`else - for (i = 0; i < $clog2(WIDTH); i = i + 1) begin - for (j = 2**i; j < WIDTH; j = j + 1) begin - g[j] = g[j] | p[j] & g[j - 2**i]; - p[j] = p[j] & p[j - 2**i]; - end - end -`endif end assign CO = g; From c49d6e78743fb51335c0640af9d4dc8b6c9407cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Wed, 27 Mar 2024 11:08:26 +0100 Subject: [PATCH 3/5] techmap: Add Kogge-Stone test --- tests/techmap/kogge-stone.ys | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/techmap/kogge-stone.ys diff --git a/tests/techmap/kogge-stone.ys b/tests/techmap/kogge-stone.ys new file mode 100644 index 00000000000..fc3637f10b0 --- /dev/null +++ b/tests/techmap/kogge-stone.ys @@ -0,0 +1 @@ +test_cell -s 1711533949 -n 10 -map +/techmap.v -map +/choices/kogge-stone.v $lcu From bc087f91ed2779fec32c94d99f56cebca3c6a4b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Wed, 27 Mar 2024 18:32:25 +0100 Subject: [PATCH 4/5] techmap: Fix using overwritten results in Kogge-Stone --- techlibs/common/choices/kogge-stone.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/techlibs/common/choices/kogge-stone.v b/techlibs/common/choices/kogge-stone.v index eb97a75d831..0e25fe86073 100644 --- a/techlibs/common/choices/kogge-stone.v +++ b/techlibs/common/choices/kogge-stone.v @@ -42,7 +42,7 @@ module _80_lcu_kogge_stone (P, G, CI, CO); g[0] = g[0] | (p[0] & CI); for (i = 0; i < $clog2(WIDTH); i = i + 1) begin - for (j = 2**i; j < WIDTH; j = j + 1) begin + for (j = WIDTH - 1; j >= 2**i; j = j - 1) begin g[j] = g[j] | p[j] & g[j - 2**i]; p[j] = p[j] & p[j - 2**i]; end From 5f4d13ee3fdf49d371fe29712b95c07eba9f7f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Mon, 8 Apr 2024 16:44:43 +0200 Subject: [PATCH 5/5] techmap: Note down iteration in Kogge-Stone --- techlibs/common/choices/kogge-stone.v | 1 + 1 file changed, 1 insertion(+) diff --git a/techlibs/common/choices/kogge-stone.v b/techlibs/common/choices/kogge-stone.v index 0e25fe86073..ad2e2b16954 100644 --- a/techlibs/common/choices/kogge-stone.v +++ b/techlibs/common/choices/kogge-stone.v @@ -42,6 +42,7 @@ module _80_lcu_kogge_stone (P, G, CI, CO); g[0] = g[0] | (p[0] & CI); for (i = 0; i < $clog2(WIDTH); i = i + 1) begin + // iterate in reverse so we don't confuse a result from this stage and the previous for (j = WIDTH - 1; j >= 2**i; j = j - 1) begin g[j] = g[j] | p[j] & g[j - 2**i]; p[j] = p[j] & p[j - 2**i];