summaryrefslogtreecommitdiff
path: root/kernel/yosys.cc
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/yosys.cc
parenta75f94ec4ae411d98d9882e423e0ae02eda4bd37 (diff)
Added "int ceil_log2(int)" function
Diffstat (limited to 'kernel/yosys.cc')
-rw-r--r--kernel/yosys.cc25
1 files changed, 25 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;