Skip to content

Commit

Permalink
rtlil: Speeds up string decoding by 30%
Browse files Browse the repository at this point in the history
This change represents about a 2% speed up of Yosys as a
whole.

Written by @rmlarsen

Co-authored-by: Rasmus Larsen <[email protected]>
Signed-off-by: Ethan Mahintorabi <[email protected]>
  • Loading branch information
QuantamHD and rmlarsen committed Sep 19, 2023
1 parent 54be4ac commit 8f6a9f5
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions kernel/rtlil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <string.h>
#include <algorithm>
#include <array>

YOSYS_NAMESPACE_BEGIN

Expand Down Expand Up @@ -200,17 +201,30 @@ const pool<IdString> &RTLIL::builtin_ff_cell_types() {
return res;
}

RTLIL::Const::Const(const std::string &str)
{
flags = RTLIL::CONST_FLAG_STRING;
bits.reserve(str.size() * 8);
for (int i = str.size()-1; i >= 0; i--) {
unsigned char ch = str[i];
for (int j = 0; j < 8; j++) {
bits.push_back((ch & 1) != 0 ? State::S1 : State::S0);
ch = ch >> 1;
}
}
PRIVATE_NAMESPACE_BEGIN

using CharToBitsLUT = std::array<std::array<RTLIL::State, 8>, 256>;

const CharToBitsLUT build_CharToBitsLUT() {
CharToBitsLUT LUT;
for (int i = 0; i < 256; ++i) {
unsigned char ch = i;
for (int j = 0; j < 8; j++) {
LUT[i][j] = (ch & 1) != 0 ? State::S1 : State::S0;
ch = ch >> 1;
}
}
return LUT;
}
PRIVATE_NAMESPACE_END

RTLIL::Const::Const(const std::string &str) : flags(RTLIL::CONST_FLAG_STRING) {
static const CharToBitsLUT LUT = build_CharToBitsLUT();
bits.reserve(str.size() * 8);
for (int i = str.size() - 1; i >= 0; --i) {
const unsigned char ch = str[i];
bits.insert(bits.end(), LUT[ch].begin(), LUT[ch].end());
}
}

RTLIL::Const::Const(int val, int width)
Expand Down Expand Up @@ -4040,7 +4054,7 @@ void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec
}
}
}

other->check();
}

Expand Down

0 comments on commit 8f6a9f5

Please sign in to comment.