From 0d7fd2585e8daec77870f19264644a204e0a8ed4 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Sat, 13 Feb 2016 16:52:16 +0100 Subject: Added "int ceil_log2(int)" function --- kernel/yosys.cc | 25 +++++++++++++++++++++++++ kernel/yosys.h | 1 + 2 files changed, 26 insertions(+) (limited to 'kernel') 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); -- cgit v1.2.3