0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-05-29 00:03:45 +02:00

ircd::math: Start supplemental library; move log2 constexprs from util.

This commit is contained in:
Jason Volk 2021-02-17 09:51:10 -08:00
parent 44683b01f3
commit 014d0281d7
6 changed files with 74 additions and 21 deletions

View file

@ -76,6 +76,7 @@
#include "b58.h"
#include "iov.h"
#include "time.h"
#include "math/math.h"
#include "grammar.h"
#include "parse.h"
#include "fmt.h"

57
include/ircd/math/log2.h Normal file
View file

@ -0,0 +1,57 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2021 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_MATH_LOG2_H
namespace ircd::math
{
template<class Z> constexpr bool is_pow2(const Z);
template<class Z> constexpr Z next_pow2(const Z);
template<class Z> constexpr Z log2(const Z);
template<class Z> constexpr Z sqr(const Z);
}
template<class Z>
inline constexpr Z
ircd::math::sqr(const Z n)
{
return pow(n, 2);
}
template<class Z>
inline constexpr Z
ircd::math::log2(const Z n)
{
return n > Z{1}?
Z{1} + log2(n >> 1):
0;
}
template<class Z>
inline constexpr Z
ircd::math::next_pow2(Z v)
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v |= v >> 32;
return ++v;
}
template<class Z>
inline constexpr bool
ircd::math::is_pow2(const Z v)
{
return v && !(v & (v - Z{1}));
}

14
include/ircd/math/math.h Normal file
View file

@ -0,0 +1,14 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2021 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_MATH_MATH_H
#include "log2.h"

View file

@ -241,25 +241,6 @@ until(it_a a,
return true;
}
constexpr bool
is_powerof2(const long long v)
{
return v && !(v & (v - 1LL));
}
constexpr uint64_t
next_powerof2(uint64_t v)
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v |= v >> 32;
return ++v;
}
template<class T>
T
minmax(T ret,

View file

@ -462,7 +462,7 @@ ircd::db::database::allocator::allocator(database *const &d,
#endif
}
{
assert(is_powerof2(alignment));
assert(math::is_pow2(alignment));
}
ircd::db::database::allocator::~allocator()

View file

@ -285,7 +285,7 @@ try
+ 128 // algorithm
};
static_assert(is_powerof2(buffer_unit_size));
static_assert(math::is_pow2(buffer_unit_size));
const size_t buffer_size
{
8_KiB + // headers