From 9c275d0a653b11f2a494321be2ab66678117db2d Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Tue, 12 Jun 2018 15:50:33 +0200 Subject: [PATCH] Add fast IdString <-> PortPin conversion Signed-off-by: Clifford Wolf --- common/nextpnr.cc | 10 +++++++++- common/nextpnr.h | 4 +++- dummy/chip.cc | 3 +++ ice40/chip.cc | 28 +++++++++++++--------------- ice40/chip.h | 1 + 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/common/nextpnr.cc b/common/nextpnr.cc index ce2cef63..d2b887de 100644 --- a/common/nextpnr.cc +++ b/common/nextpnr.cc @@ -28,7 +28,15 @@ void IdString::initialize() { database_str_to_idx = new std::unordered_map; database_idx_to_str = new std::vector; - auto insert_rc = database_str_to_idx->insert({std::string(), 0}); + initialize_add("", 0); + initialize_chip(); +} + +void IdString::initialize_add(const char *s, int idx) +{ + assert(database_str_to_idx->count(s) == 0); + assert(database_idx_to_str->size() == idx); + auto insert_rc = database_str_to_idx->insert({s, idx}); database_idx_to_str->push_back(&insert_rc.first->first); } diff --git a/common/nextpnr.h b/common/nextpnr.h index dad6239f..fc14299d 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -48,7 +48,9 @@ struct IdString static std::unordered_map *database_str_to_idx; static std::vector *database_idx_to_str; - void initialize(); + static void initialize(); + static void initialize_chip(); + static void initialize_add(const char *s, int idx); IdString() {} diff --git a/dummy/chip.cc b/dummy/chip.cc index 23aa0e9d..ae962508 100644 --- a/dummy/chip.cc +++ b/dummy/chip.cc @@ -24,6 +24,9 @@ NEXTPNR_NAMESPACE_BEGIN Chip::Chip(ChipArgs) {} std::string Chip::getChipName() { return "Dummy"; } + +void IdString::initialize_chip() {} + // --------------------------------------------------------------- BelId Chip::getBelByName(IdString name) const { return BelId(); } diff --git a/ice40/chip.cc b/ice40/chip.cc index 88fcc512..918a7fb4 100644 --- a/ice40/chip.cc +++ b/ice40/chip.cc @@ -52,27 +52,25 @@ BelType belTypeFromId(IdString id) // ----------------------------------------------------------------------- +void IdString::initialize_chip() +{ +#define X(t) initialize_add(#t, PIN_##t); +#include "portpins.inc" +#undef X +} + IdString portPinToId(PortPin type) { -#define X(t) \ - if (type == PIN_##t) \ - return #t; - -#include "portpins.inc" - -#undef X - return IdString(); + IdString ret; + if (type > 0 && type < PIN_MAXIDX) + ret.index = type; + return ret; } PortPin portPinFromId(IdString id) { -#define X(t) \ - if (id == #t) \ - return PIN_##t; - -#include "portpins.inc" - -#undef X + if (id.index > 0 && id.index < PIN_MAXIDX) + return PortPin(id.index); return PIN_NONE; } diff --git a/ice40/chip.h b/ice40/chip.h index 73e8d33b..f8946610 100644 --- a/ice40/chip.h +++ b/ice40/chip.h @@ -62,6 +62,7 @@ enum PortPin #define X(t) PIN_##t, #include "portpins.inc" #undef X + PIN_MAXIDX }; IdString portPinToId(PortPin type);