General Purpose library for Freestanding C++ and POSIX systems
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 

78 рядки
1.7 KiB

#pragma once
#include "gp/math/details/math_definitions.hpp"
#include <stdint.h>
#include <stddef.h>
namespace gp {
namespace math {
/**
Sean Eron Anderson
seander@cs.stanford.edu
**/
template<>
constexpr uint32_t log2<uint32_t>(uint32_t v)
{
constexpr int MultiplyDeBruijnBitPosition[32] =
{
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return MultiplyDeBruijnBitPosition[(v * 0x07C4ACDDUL) / 134217728UL];
}
template<>
constexpr uint64_t log2<uint64_t>(uint64_t v)
{
constexpr int MultiplyDeBruijnBitPosition[64] =
{
0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61,
51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62,
57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56,
45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5, 63
};
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v |= v >> 32;
return MultiplyDeBruijnBitPosition[(v * 0x03f6eaf2cd271461ULL) / 288230376151711744ULL];
}
static_assert(log2<uint32_t>(7) == 2, "bad log2");
static_assert(log2<uint32_t>(8) == 3, "bad log2");
constexpr uint64_t pow2(uint64_t v) {
if(v) return pow2(v-1)*2;
return 1;
}
template<>
constexpr uint32_t msb<uint32_t>(uint32_t v)
{
auto l = log2(v);
return l + ((pow2(l) ^ v) != 0);
}
template<>
constexpr uint64_t msb<uint64_t>(uint64_t v)
{
auto l = log2(v);
return l + ((pow2(l) ^ v) != 0);
}
static_assert(msb<uint32_t>(7) == 3, "bad msb");
static_assert(msb<uint32_t>(8) == 3, "bad msb");
}
}