diff options
author | Clifford Wolf <clifford@clifford.at> | 2016-02-13 16:52:16 +0100 |
---|---|---|
committer | Clifford Wolf <clifford@clifford.at> | 2016-02-13 16:52:16 +0100 |
commit | 0d7fd2585e8daec77870f19264644a204e0a8ed4 (patch) | |
tree | 680308c755ff95dde092e26d4e0e33eccc249f62 /kernel | |
parent | a75f94ec4ae411d98d9882e423e0ae02eda4bd37 (diff) | |
download | yosys-0d7fd2585e8daec77870f19264644a204e0a8ed4.tar.gz yosys-0d7fd2585e8daec77870f19264644a204e0a8ed4.tar.bz2 yosys-0d7fd2585e8daec77870f19264644a204e0a8ed4.zip |
Added "int ceil_log2(int)" function
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/yosys.cc | 25 | ||||
-rw-r--r-- | kernel/yosys.h | 1 |
2 files changed, 26 insertions, 0 deletions
diff --git a/kernel/yosys.cc b/kernel/yosys.cc index 109918816..4bfbe3614 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -124,6 +124,31 @@ void yosys_banner() log("\n"); } +int ceil_log2(int x) +{ + if (x <= 0) + return 0; + + int y = (x & (x - 1)); + y = (y | -y) >> 31; + + x |= (x >> 1); + x |= (x >> 2); + x |= (x >> 4); + x |= (x >> 8); + x |= (x >> 16); + + x >>= 1; + x -= ((x >> 1) & 0x55555555); + x = (((x >> 2) & 0x33333333) + (x & 0x33333333)); + x = (((x >> 4) + x) & 0x0f0f0f0f); + x += (x >> 8); + x += (x >> 16); + x = x & 0x0000003f; + + return x - y; +} + std::string stringf(const char *fmt, ...) { std::string string; diff --git a/kernel/yosys.h b/kernel/yosys.h index 92fa6ac19..c8bc46b65 100644 --- a/kernel/yosys.h +++ b/kernel/yosys.h @@ -222,6 +222,7 @@ extern bool memhasher_active; inline void memhasher() { if (memhasher_active) memhasher_do(); } void yosys_banner(); +int ceil_log2(int x); std::string stringf(const char *fmt, ...) YS_ATTRIBUTE(format(printf, 1, 2)); std::string vstringf(const char *fmt, va_list ap); int readsome(std::istream &f, char *s, int n); |