PandA-2024.02
math_function.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 
45 #ifndef MATH_FUNCTION_HPP
46 #define MATH_FUNCTION_HPP
47 
48 #include <boost/version.hpp>
49 #if BOOST_VERSION >= 105800
50 #include <boost/integer/common_factor_rt.hpp>
51 #else
52 #include <boost/math/common_factor_rt.hpp>
53 #endif
54 
55 #include <limits>
56 #include <type_traits>
57 
64 template <typename Integer>
65 Integer GreatestCommonDivisor(const Integer first, const Integer second)
66 {
67 #if BOOST_VERSION >= 105800
68  return boost::integer::gcd<Integer>(first, second);
69 #else
70  return boost::math::gcd<Integer>(first, second);
71 #endif
72 }
73 
80 template <typename Integer>
81 Integer LeastCommonMultiple(const Integer first, const Integer second)
82 {
83 #if BOOST_VERSION >= 105800
84  return boost::integer::lcm<Integer>(first, second);
85 #else
86  return boost::math::lcm<Integer>(first, second);
87 #endif
88 }
89 
90 template <typename T>
91 inline T compute_n_bytes(T bitsize)
92 {
93  return bitsize / 8 + ((bitsize % 8) != 0);
94 }
95 
97 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x)-1)) == 0)
98 
101 inline T floor_log2(T x)
102 {
103  unsigned long long t = 0;
104 
105  if(x == 0)
106  {
107  return static_cast<T>(-1LL);
108  }
109 
110  if(x >= 1ULL << (t + 32))
111  {
112  t += 32;
113  }
114  if(x >= 1ULL << (t + 16))
115  {
116  t += 16;
117  }
118  if(x >= 1ULL << (t + 8))
119  {
120  t += 8;
121  }
122  if(x >= 1ULL << (t + 4))
123  {
124  t += 4;
125  }
126  if(x >= 1ULL << (t + 2))
127  {
128  t += 2;
129  }
130  if(x >= 1ULL << (t + 1))
131  {
132  t += 1;
133  }
134 
135  return static_cast<T>(t);
136 }
137 
141 inline T exact_log2(T x)
142 {
143  if(x != (x & -x))
144  {
145  return static_cast<T>(-1LL);
146  }
147  return floor_log2(x);
148 }
149 
152 inline T ceil_log2(T x)
153 {
154  if(x == 0)
155  {
156  return static_cast<T>(-1LL);
157  }
158  return static_cast<T>(floor_log2(static_cast<T>(x - 1)) + 1);
159 }
160 
162 template <
163  typename T, std::enable_if_t<std::is_unsigned<T>::value, bool> = true,
164  std::enable_if_t<std::numeric_limits<T>::digits <= std::numeric_limits<unsigned long long>::digits, bool> = true>
165 constexpr inline T ceil_pow2(T _x)
166 {
167  unsigned long long x = _x;
168  x--;
169  x |= x >> 1;
170  x |= x >> 2;
171  x |= x >> 4;
172  x |= x >> 8;
173  x |= x >> 16;
174  x |= x >> 32;
175  x++;
176  return static_cast<T>(x);
177 }
178 
180 constexpr inline T get_aligned_bitsize(T bitsize)
181 {
182  const auto rbw = std::max(T(8), ceil_pow2(bitsize));
183  if(rbw <= 128ULL)
184  {
185  return rbw;
186  }
187  return bitsize + ((32ULL - (bitsize % 32ULL)) & 31ULL);
188 }
189 
191 constexpr inline T get_aligned_bitsize(T bitsize, T align)
192 {
193  return std::max(align, ((bitsize / align) + (bitsize % align != 0)) * align);
194 }
195 
197 constexpr inline T get_aligned_ac_bitsize(T bitsize)
198 {
199  return bitsize + ((64ULL - (bitsize % 64ULL)) & 63ULL);
200 }
201 
204 {
205  if(value == T(1))
206  {
207  return T(1);
208  }
209  return std::max(T(8), ceil_pow2(value));
210 }
211 
212 #endif
T floor_log2(T x)
Given X, an unsigned number, return the largest int Y such that 2**Y <= X. If X is 0...
constexpr T get_aligned_ac_bitsize(T bitsize)
T resize_1_8_pow2(T value)
static unsigned long long int align(unsigned long long int address, unsigned long long int alignment)
STL includes.
Definition: memory.cpp:81
T exact_log2(T x)
Return the logarithm of X, base 2, considering X unsigned, if X is a power of 2.
#define max
Definition: backprop.h:17
T ceil_log2(T x)
Return the smallest n such that 2**n >= X.
return static_cast< T >(x)
constexpr T get_aligned_bitsize(T bitsize)
T compute_n_bytes(T bitsize)
Integer LeastCommonMultiple(const Integer first, const Integer second)
Return the least common multiple of two integers.
Integer GreatestCommonDivisor(const Integer first, const Integer second)
Return the greatest common divisor.
x
Return the smallest n such that 2^n >= _x.

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13