refactor(compiler): Move ceilLog2 to separate header file

This commit is contained in:
Andi Drebes
2021-09-27 16:23:14 +02:00
committed by Quentin Bourgerie
parent 1200a46e49
commit 5ce3fd5f43
2 changed files with 21 additions and 16 deletions

View File

@@ -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 <typename T> 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

View File

@@ -3,6 +3,7 @@
#include <zamalang/Dialect/HLFHE/IR/HLFHEDialect.h>
#include <zamalang/Dialect/HLFHE/IR/HLFHEOps.h>
#include <zamalang/Dialect/HLFHE/IR/HLFHETypes.h>
#include <zamalang/Support/math.h>
#include <limits>
#include <llvm/ADT/APInt.h>
@@ -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 <typename T> 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