-
Notifications
You must be signed in to change notification settings - Fork 57
/
bitops.h
143 lines (126 loc) · 3.57 KB
/
bitops.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 2015-2021 Morwenn
* SPDX-License-Identifier: MIT
*/
#ifndef CPPSORT_DETAIL_BITOPS_H_
#define CPPSORT_DETAIL_BITOPS_H_
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <cstddef>
#include <limits>
#include <type_traits>
#include "../detail/attributes.h"
#include "../detail/config.h"
#include "../detail/type_traits.h"
namespace cppsort
{
namespace detail
{
// Cast signed value to unsigned one
template<typename Integer>
CPPSORT_ATTRIBUTE_NODISCARD
constexpr auto as_unsigned(Integer value)
-> std::make_unsigned_t<Integer>
{
return static_cast<std::make_unsigned_t<Integer>>(value);
}
// Returns 2^floor(log2(n)), assumes n > 0
template<typename Unsigned>
constexpr auto hyperfloor(Unsigned n)
-> Unsigned
{
constexpr auto bound = std::numeric_limits<Unsigned>::digits / 2;
for (std::size_t i = 1 ; i <= bound ; i <<= 1) {
n |= (n >> i);
}
return n & ~(n >> 1);
}
// Returns 2^ceil(log2(n)), assumes n > 0
template<typename Unsigned>
constexpr auto hyperceil(Unsigned n)
-> Unsigned
{
constexpr auto bound = std::numeric_limits<Unsigned>::digits / 2;
--n;
for (std::size_t i = 1 ; i <= bound ; i <<= 1) {
n |= (n >> i);
}
return ++n;
}
// Returns floor(log2(n)), assumes n > 0
#if defined(__GNUC__) || defined(__clang__)
constexpr auto log2(unsigned int n)
-> unsigned int
{
return std::numeric_limits<unsigned int>::digits - __builtin_clz(n) - 1u;
}
constexpr auto log2(unsigned long n)
-> unsigned long
{
return std::numeric_limits<unsigned long>::digits - __builtin_clzl(n) - 1ul;
}
constexpr auto log2(unsigned long long n)
-> unsigned long long
{
return std::numeric_limits<unsigned long long>::digits - __builtin_clzll(n) - 1ull;
}
#endif
template<typename Integer>
constexpr auto log2(Integer n)
-> Integer
{
Integer log = 0;
while (n >>= 1) {
++log;
}
return log;
}
// Returns ceil(log2(n)), assumes n > 0
template<typename Integer>
constexpr auto ceil_log2(Integer n)
-> Integer
{
Integer log = 0;
--n;
while (n > 0) {
++log;
n >>= 1;
}
return log;
}
// Halves a positive number, using unsigned division if possible
template<typename Integer>
constexpr auto half(Integer value)
-> detail::enable_if_t<std::is_integral<Integer>::value, Integer>
{
return static_cast<Integer>(as_unsigned(value) / 2);
}
template<typename T>
constexpr auto half(T value)
-> detail::enable_if_t<not std::is_integral<T>::value, T>
{
return value / 2;
}
// Returns whether an integer has a single bit set, generally
// used to check whether an integer is a power of 2,
// assumes n >= 0
template<typename Integer>
constexpr auto has_single_bit(Integer n) noexcept
-> bool
{
CPPSORT_ASSERT(n >= 0);
auto x = as_unsigned(n);
return x != 0 && (x & (x - 1)) == 0;
}
// Left bit rotation
template<typename Unsigned>
constexpr auto rotl(Unsigned x, int s)
-> Unsigned
{
CPPSORT_ASSERT(s > 0);
constexpr auto n = std::numeric_limits<Unsigned>::digits;
return (x << s) | (x >> (n - s));
}
}}
#endif // CPPSORT_DETAIL_BITOPS_H_