summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorClifford Wolf <clifford@clifford.at>2016-02-13 16:52:16 +0100
committerClifford Wolf <clifford@clifford.at>2016-02-13 16:52:16 +0100
commit0d7fd2585e8daec77870f19264644a204e0a8ed4 (patch)
tree680308c755ff95dde092e26d4e0e33eccc249f62 /kernel
parenta75f94ec4ae411d98d9882e423e0ae02eda4bd37 (diff)
Added "int ceil_log2(int)" function
Diffstat (limited to 'kernel')
-rw-r--r--kernel/yosys.cc25
-rw-r--r--kernel/yosys.h1
2 files changed, 26 insertions, 0 deletions
diff --git a/kernel/yosys.cc b/kernel/yosys.cc
index 10991881..4bfbe361 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 92fa6ac1..c8bc46b6 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);