From c7c10613d77fcac34909b9f1042fc35cf19c1383 Mon Sep 17 00:00:00 2001 From: Stephen Williams Date: Sun, 28 Jul 2013 17:48:03 -0700 Subject: [PATCH] blif support for unsigned magnitude comparison. --- tgt-blif/Makefile.in | 3 +- tgt-blif/lpm.cc | 4 ++ tgt-blif/lpm_cmp_gt.cc | 154 +++++++++++++++++++++++++++++++++++++++++ tgt-blif/priv.h | 1 + 4 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 tgt-blif/lpm_cmp_gt.cc diff --git a/tgt-blif/Makefile.in b/tgt-blif/Makefile.in index fd4273ca7..79f8811e5 100644 --- a/tgt-blif/Makefile.in +++ b/tgt-blif/Makefile.in @@ -44,7 +44,8 @@ CPPFLAGS = $(INCLUDE_PATH) @CPPFLAGS@ @DEFS@ @PICFLAG@ CXXFLAGS = @WARNING_FLAGS@ @CXXFLAGS@ LDFLAGS = @LDFLAGS@ -O = blif.o constants.o logic_gate.o lpm.o lpm_add.o lpm_cmp_eq.o nex_data.o +O = blif.o constants.o logic_gate.o lpm.o lpm_add.o lpm_cmp_eq.o lpm_cmp_gt.o \ + nex_data.o all: dep blif.tgt diff --git a/tgt-blif/lpm.cc b/tgt-blif/lpm.cc index 4631ec824..470fd4e85 100644 --- a/tgt-blif/lpm.cc +++ b/tgt-blif/lpm.cc @@ -107,6 +107,10 @@ int print_lpm(FILE*fd, ivl_lpm_t net) case IVL_LPM_CMP_EEQ: rc += print_lpm_cmp_eq(fd, net); break; + case IVL_LPM_CMP_GE: + case IVL_LPM_CMP_GT: + rc += print_lpm_cmp_gt(fd, net); + break; case IVL_LPM_CONCAT: case IVL_LPM_CONCATZ: rc += print_concat(fd, net); diff --git a/tgt-blif/lpm_cmp_gt.cc b/tgt-blif/lpm_cmp_gt.cc new file mode 100644 index 000000000..331aab0db --- /dev/null +++ b/tgt-blif/lpm_cmp_gt.cc @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2013 Stephen Williams (steve@icarus.com) + * + * This source code is free software; you can redistribute it + * and/or modify it in source code form under the terms of the GNU + * General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +# include "priv.h" +# include "nex_data.h" +# include + +static int print_lpm_cmp_gt_s(FILE*fd, ivl_lpm_t net) +{ + ivl_nexus_t q_nex = ivl_lpm_q(net); + ivl_nexus_t a_nex = ivl_lpm_data(net,0); + ivl_nexus_t b_nex = ivl_lpm_data(net,1); + + blif_nex_data_t*q_ned = blif_nex_data_t::get_nex_data(q_nex); + blif_nex_data_t*a_ned = blif_nex_data_t::get_nex_data(a_nex); + blif_nex_data_t*b_ned = blif_nex_data_t::get_nex_data(b_nex); + + assert(1 == q_ned->get_width()); + assert(ivl_lpm_width(net) == a_ned->get_width()); + assert(ivl_lpm_width(net) == b_ned->get_width()); + + // This is true if the operator is GE instead of just GT. + bool ge_flag = ivl_lpm_type(net)==IVL_LPM_CMP_GE; + + // Special case: if the input vector is a single bit, then the + // operation is trivial. Note that since this is signed, the + // "1" bit represents -1 and is <0. + if (ivl_lpm_width(net) == 1) { + fprintf(fd, ".names %s%s %s%s %s%s\n" + "01 1\n", + a_ned->get_name(), a_ned->get_name_index(0), + b_ned->get_name(), b_ned->get_name_index(0), + q_ned->get_name(), q_ned->get_name_index(0)); + if (ge_flag) fprintf(fd, "00 1\n11 1\n"); + return 0; + } + + fprintf(stderr, "%s:%u: sorry: blif: Signed agnitude compare not implemented yet\n", + ivl_lpm_file(net), ivl_lpm_lineno(net)); + + return 1; +} + +static int print_lpm_cmp_gt_u(FILE*fd, ivl_lpm_t net) +{ + ivl_nexus_t q_nex = ivl_lpm_q(net); + ivl_nexus_t a_nex = ivl_lpm_data(net,0); + ivl_nexus_t b_nex = ivl_lpm_data(net,1); + + blif_nex_data_t*q_ned = blif_nex_data_t::get_nex_data(q_nex); + blif_nex_data_t*a_ned = blif_nex_data_t::get_nex_data(a_nex); + blif_nex_data_t*b_ned = blif_nex_data_t::get_nex_data(b_nex); + + assert(1 == q_ned->get_width()); + assert(ivl_lpm_width(net) == a_ned->get_width()); + assert(ivl_lpm_width(net) == b_ned->get_width()); + + // This is true if the operator is GE instead of just GT. + bool ge_flag = ivl_lpm_type(net)==IVL_LPM_CMP_GE; + + // Special case: if the input vector is a single bit, then the + // operation is trivial. + if (ivl_lpm_width(net) == 1) { + fprintf(fd, ".names %s%s %s%s %s%s\n" + "10 1\n", + a_ned->get_name(), a_ned->get_name_index(0), + b_ned->get_name(), b_ned->get_name_index(0), + q_ned->get_name(), q_ned->get_name_index(0)); + if (ge_flag) fprintf(fd, "00 1\n11 1\n"); + return 0; + } + + // Calculate GT and EQ for each bit. + for (unsigned idx = 0 ; idx < ivl_lpm_width(net) ; idx += 1) { + fprintf(fd, ".names %s%s %s%s %s%s/G%u\n" + "10 1\n", + a_ned->get_name(), a_ned->get_name_index(idx), + b_ned->get_name(), b_ned->get_name_index(idx), + q_ned->get_name(), q_ned->get_name_index(0), idx); + } + for (unsigned idx = ge_flag?0 : 1 ; idx < ivl_lpm_width(net) ; idx += 1) { + fprintf(fd, ".names %s%s %s%s %s%s/E%u\n" + "11 1\n" + "00 1\n", + a_ned->get_name(), a_ned->get_name_index(idx), + b_ned->get_name(), b_ned->get_name_index(idx), + q_ned->get_name(), q_ned->get_name_index(0), idx); + } + + // Generate a wide function to blend the per-bit comparisons. + fprintf(fd, ".names"); + for (unsigned idx = 0 ; idx < ivl_lpm_width(net) ; idx += 1) { + fprintf(fd, " %s%s/G%u", + q_ned->get_name(), q_ned->get_name_index(0), idx); + } + for (unsigned idx = ge_flag?0:1 ; idx < ivl_lpm_width(net) ; idx += 1) { + fprintf(fd, " %s%s/E%u", + q_ned->get_name(), q_ned->get_name_index(0), idx); + } + fprintf(fd, " %s%s\n", q_ned->get_name(), q_ned->get_name_index(0)); + + for (unsigned idx = 0 ; idx < ivl_lpm_width(net) ; idx += 1) { + for (unsigned jdx = 0 ; jdx < ivl_lpm_width(net) ; jdx += 1) { + if (jdx != idx) + fprintf(fd, "-"); + else + fprintf(fd, "1"); + } + for (unsigned jdx = ge_flag?0:1 ; jdx < ivl_lpm_width(net) ; jdx += 1) { + if (jdx <= idx) + fprintf(fd, "-"); + else + fprintf(fd, "1"); + } + fprintf(fd, " 1\n"); + } + + if (ge_flag) { + for (unsigned idx = 0 ; idx < ivl_lpm_width(net) ; idx += 1) + fputc('-', fd); + for (unsigned idx = 0 ; idx < ivl_lpm_width(net) ; idx += 1) + fputc('1', fd); + fprintf(fd, " 1\n"); + } + + return 0; +} + +int print_lpm_cmp_gt(FILE*fd, ivl_lpm_t net) +{ + fprintf(fd, "# %s:%u: LPM_LPM_CMP_GT: width=%u\n", + ivl_lpm_file(net), ivl_lpm_lineno(net), ivl_lpm_width(net)); + + if (ivl_lpm_signed(net)) + return print_lpm_cmp_gt_s(fd, net); + else + return print_lpm_cmp_gt_u(fd, net); +} diff --git a/tgt-blif/priv.h b/tgt-blif/priv.h index 43afe4180..071333afd 100644 --- a/tgt-blif/priv.h +++ b/tgt-blif/priv.h @@ -34,6 +34,7 @@ extern int print_lpm(FILE*fd, ivl_lpm_t net); extern int print_lpm_add(FILE*fd, ivl_lpm_t net); extern int print_lpm_sub(FILE*fd, ivl_lpm_t net); extern int print_lpm_cmp_eq(FILE*fd, ivl_lpm_t net); +extern int print_lpm_cmp_gt(FILE*fd, ivl_lpm_t net); extern int print_lpm_cmp_ne(FILE*fd, ivl_lpm_t net); /*