From 5ce3fd5f43d20c0d2178700046fcc531b0b02536 Mon Sep 17 00:00:00 2001 From: Andi Drebes Date: Mon, 27 Sep 2021 16:23:14 +0200 Subject: [PATCH] refactor(compiler): Move ceilLog2 to separate header file --- compiler/include/zamalang/Support/math.h | 20 ++++++++++++++++++++ compiler/lib/Dialect/HLFHE/Analysis/MANP.cpp | 17 +---------------- 2 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 compiler/include/zamalang/Support/math.h diff --git a/compiler/include/zamalang/Support/math.h b/compiler/include/zamalang/Support/math.h new file mode 100644 index 000000000..4d4de1e89 --- /dev/null +++ b/compiler/include/zamalang/Support/math.h @@ -0,0 +1,20 @@ +#ifndef ZAMALANG_SUPPORT_MATH_H_ +#define ZAMALANG_SUPPORT_MATH_H_ + +// Calculates (T)ceil(log2f(v)) +// TODO: Replace with some fancy bit twiddling hack +template static T ceilLog2(const T v) { + T tmp = v; + T log2 = 0; + + while (tmp >>= 1) + log2++; + + // If more than MSB set, round to next highest power of 2 + if (v & ~((T)1 << log2)) + log2 += 1; + + return log2; +} + +#endif diff --git a/compiler/lib/Dialect/HLFHE/Analysis/MANP.cpp b/compiler/lib/Dialect/HLFHE/Analysis/MANP.cpp index 4eac5be2e..0a677cb43 100644 --- a/compiler/lib/Dialect/HLFHE/Analysis/MANP.cpp +++ b/compiler/lib/Dialect/HLFHE/Analysis/MANP.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -183,22 +184,6 @@ static llvm::APInt denseCstTensorNorm2Sq(mlir::ConstantOp cstOp) { return accu; } -// Calculates (T)ceil(log2f(v)) -// TODO: Replace with some fancy bit twiddling hack -template static T ceilLog2(const T v) { - T tmp = v; - T log2 = 0; - - while (tmp >>= 1) - log2++; - - // If more than MSB set, round to next highest power of 2 - if (v & ~((T)1 << log2)) - log2 += 1; - - return log2; -} - // Calculates the square of the 2-norm of a 1D tensor of signless // integers by conservatively assuming that the dynamic values are the // maximum for the integer width. Aborts if the tensor type `tTy` is