21 #undef TRUE // MSVC defines TRUE
22 #undef FALSE // MSVC defines FALSE
67 #define UTILS_CHECK(cond, expr) \
69 std::cerr << expr << " in function: " << __FUNCTION__ \
70 << " in file: " __FILE__ << " at line: " << __LINE__; \
71 throw std::runtime_error("Error. Check log output"); \
74 #define UTILS_CHECK_BOUNDS(arg, n, bound, expr) \
75 for (size_t utils_check_idx = 0; utils_check_idx < n; ++utils_check_idx) { \
76 UTILS_CHECK((arg)[utils_check_idx] < bound, expr); \
79 #else // UTILS_DEBUG=OFF
81 #define UTILS_CHECK(cond, expr) \
83 #define UTILS_CHECK_BOUNDS(...) \
103 return static_cast<uint64_t
>(n % modulus);
113 return static_cast<uint64_t
>(q);
122 *prod_hi =
static_cast<uint64_t
>(prod >> 64);
123 *prod_lo =
static_cast<uint64_t
>(prod);
127 template <
int BitShift>
130 return static_cast<uint64_t
>(product >> BitShift);
134 inline uint64_t
MSB(uint64_t input) {
135 return static_cast<uint64_t
>(std::log2l(input));
173 inline bool IsPowerOfTwo(uint64_t num) {
return num && !(num & (num - 1)); }
176 inline uint64_t
Log2(uint64_t x) {
183 UTILS_CHECK(bits <= 64,
"MaximumValue requires bits <= 64; got " << bits);
185 return (std::numeric_limits<uint64_t>::max)();
187 return (1ULL << bits) - 1;
197 virtual void*
allocate(
size_t bytes_count) = 0;
198 virtual void deallocate(
void* p,
size_t n) = 0;
201 template <
class AllocatorImpl>
205 return static_cast<AllocatorImpl*
>(
this)->allocate_impl(bytes_count);
208 static_cast<AllocatorImpl*
>(
this)->deallocate_impl(p, n);
214 void* allocate_impl(
size_t bytes_count) {
219 void deallocate_impl(
void* p,
size_t n) {
226 template <
int error_code>
227 static constexpr
int fail_message() {
230 "Using 'AllocatorAdapter`as interface requires to implement "
231 "'::allocate_impl` method");
234 "Using 'AllocatorAdapter`as interface requires to implement "
235 "'::deallocate_impl` method");
248 return std::malloc(bytes_count);
261 throw std::runtime_error(
262 "Cannot create 'CustomAllocStrategy' without `impl`");
267 return p_impl->allocate(bytes_count);
273 std::shared_ptr<AllocatorBase> p_impl;
280 template <
typename T, u
int64_t Alignment>
283 template <
typename, u
int64_t>
289 : m_alloc_impl((strategy !=
nullptr) ? strategy :
mallocStrategy) {}
292 : m_alloc_impl(src.m_alloc_impl) {}
296 template <
typename U>
298 : m_alloc_impl(src.m_alloc_impl) {}
302 template <
typename U>
316 size_t buffer_size =
sizeof(T) * n + Alignment;
319 size_t alloc_size = buffer_size +
sizeof(
void*);
320 void* buffer = m_alloc_impl->allocate(alloc_size);
326 void* aligned_buffer =
static_cast<char*
>(buffer) +
sizeof(
void*);
328 if (!aligned_buffer) {
333 void* store_buffer_addr =
334 static_cast<char*
>(aligned_buffer) -
sizeof(
void*);
335 *(
static_cast<void**
>(store_buffer_addr)) = buffer;
337 return static_cast<T*
>(aligned_buffer);
344 void* store_buffer_addr = (
reinterpret_cast<char*
>(p) -
sizeof(
void*));
345 void* free_address = *(
static_cast<void**
>(store_buffer_addr));
346 m_alloc_impl->deallocate(free_address, n);
353 template <
typename T>
~AlignedAllocator()
Definition: utils-test.hpp:300
Definition: utils-test.hpp:202
uint64_t DivideUInt128UInt64Lo(uint64_t x1, uint64_t x0, uint64_t y)
Definition: utils-test.hpp:108
void * allocate_memory(size_t bytes_count)
Definition: utils-test.hpp:266
virtual void deallocate(void *p, size_t n)=0
uint64_t Log2(uint64_t x)
Definition: utils-test.hpp:176
bool Compare(CMPINT cmp, uint64_t lhs, uint64_t rhs)
Definition: utils-test.hpp:144
uint64_t BarrettReduce128(uint64_t input_hi, uint64_t input_lo, uint64_t modulus)
Definition: utils-test.hpp:97
uint128_t MultiplyUInt64(uint64_t x, uint64_t y)
Definition: utils-test.hpp:93
AlignedAllocator & operator=(const AlignedAllocator &src)=delete
void deallocate(void *p, size_t n) final
Definition: utils-test.hpp:251
void deallocate_memory(void *p, size_t n)
Definition: utils-test.hpp:270
CMPINT Not(CMPINT cmp)
Returns the logical negation of a binary operation.
Definition: utils-test.hpp:39
std::vector< T, AlignedAllocator< T, 64 > > AlignedVector64
Definition: utils-test.hpp:354
CustomAllocStrategy(std::shared_ptr< AllocatorBase > impl)
Definition: utils-test.hpp:258
uint64_t MSB(uint64_t input)
Definition: utils-test.hpp:134
CMPINT
Represents binary operations between two boolean values.
Definition: utils-test.hpp:26
AlignedAllocator(const AlignedAllocator< U, Alignment > &src)
Definition: utils-test.hpp:297
bool IsPowerOfTwo(uint64_t num)
Definition: utils-test.hpp:173
__extension__ typedef unsigned __int128 uint128_t
Definition: utils-test.hpp:16
AlignedAllocator(AllocatorStrategyPtr strategy=nullptr) noexcept
Definition: utils-test.hpp:288
uint64_t MultiplyUInt64Hi(uint64_t x, uint64_t y)
Definition: utils-test.hpp:128
Definition: utils-test.hpp:195
Definition: utils-test.hpp:303
#define UTILS_CHECK(cond, expr)
Definition: utils-test.hpp:81
void * allocate(size_t bytes_count) final
Definition: utils-test.hpp:247
__extension__ typedef __int128 int128_t
Definition: utils-test.hpp:15
uint64_t MaximumValue(uint64_t bits)
Definition: utils-test.hpp:182
AlignedAllocator(const AlignedAllocator &src)
Definition: utils-test.hpp:291
void deallocate(T *p, size_t n)
Definition: utils-test.hpp:340
bool operator==(const AlignedAllocator &)
Definition: utils-test.hpp:307
bool operator!=(const AlignedAllocator &)
Definition: utils-test.hpp:309
virtual ~AllocatorBase() noexcept
Definition: utils-test.hpp:196
void deallocate(void *p, size_t n) override
Definition: utils-test.hpp:207
Definition: utils-test.hpp:246
virtual void * allocate(size_t bytes_count)=0
void * allocate(size_t bytes_count) override
Definition: utils-test.hpp:204
Definition: utils-test.hpp:281
T value_type
Definition: utils-test.hpp:286
std::shared_ptr< AllocatorBase > AllocatorStrategyPtr
Definition: utils-test.hpp:277
Definition: utils-test.hpp:257
T * allocate(size_t n)
Definition: utils-test.hpp:311
AllocatorStrategyPtr mallocStrategy
Definition: ntt.cpp:255