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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
//
//
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#ifndef GRPC_SRC_CORE_LIB_GPR_USEFUL_H
#define GRPC_SRC_CORE_LIB_GPR_USEFUL_H
#include <grpc/support/port_platform.h>
#include <cstddef>
#include "y_absl/strings/string_view.h"
#include "y_absl/types/variant.h"
/// useful utilities that don't belong anywhere else
namespace grpc_core {
template <typename T>
T Clamp(T val, T min, T max) {
if (val < min) return min;
if (max < val) return max;
return val;
}
/// rotl, rotr assume x is unsigned
template <typename T>
constexpr T RotateLeft(T x, T n) {
return ((x << n) | (x >> (sizeof(x) * 8 - n)));
}
template <typename T>
constexpr T RotateRight(T x, T n) {
return ((x >> n) | (x << (sizeof(x) * 8 - n)));
}
// Set the n-th bit of i
template <typename T>
T SetBit(T* i, size_t n) {
return *i |= (T(1) << n);
}
// Clear the n-th bit of i
template <typename T>
T ClearBit(T* i, size_t n) {
return *i &= ~(T(1) << n);
}
// Get the n-th bit of i
template <typename T>
bool GetBit(T i, size_t n) {
return (i & (T(1) << n)) != 0;
}
namespace useful_detail {
inline constexpr uint32_t HexdigitBitcount(uint32_t x) {
return (x - ((x >> 1) & 0x77777777) - ((x >> 2) & 0x33333333) -
((x >> 3) & 0x11111111));
}
} // namespace useful_detail
inline constexpr uint32_t BitCount(uint32_t i) {
return (((useful_detail::HexdigitBitcount(i) +
(useful_detail::HexdigitBitcount(i) >> 4)) &
0x0f0f0f0f) %
255);
}
inline constexpr uint32_t BitCount(uint64_t i) {
return BitCount(static_cast<uint32_t>(i)) +
BitCount(static_cast<uint32_t>(i >> 32));
}
inline constexpr uint32_t BitCount(uint16_t i) {
return BitCount(static_cast<uint32_t>(i));
}
inline constexpr uint32_t BitCount(uint8_t i) {
return BitCount(static_cast<uint32_t>(i));
}
inline constexpr uint32_t BitCount(int64_t i) {
return BitCount(static_cast<uint64_t>(i));
}
inline constexpr uint32_t BitCount(int32_t i) {
return BitCount(static_cast<uint32_t>(i));
}
inline constexpr uint32_t BitCount(int16_t i) {
return BitCount(static_cast<uint16_t>(i));
}
inline constexpr uint32_t BitCount(int8_t i) {
return BitCount(static_cast<uint8_t>(i));
}
// This function uses operator< to implement a qsort-style comparison, whereby:
// if a is smaller than b, a number smaller than 0 is returned.
// if a is bigger than b, a number greater than 0 is returned.
// if a is neither smaller nor bigger than b, 0 is returned.
template <typename T>
int QsortCompare(const T& a, const T& b) {
if (a < b) return -1;
if (b < a) return 1;
return 0;
}
template <typename... X>
int QsortCompare(const y_absl::variant<X...>& a, const y_absl::variant<X...>& b) {
const int index = QsortCompare(a.index(), b.index());
if (index != 0) return index;
return y_absl::visit(
[&](const auto& x) {
return QsortCompare(x, y_absl::get<y_absl::remove_cvref_t<decltype(x)>>(b));
},
a);
}
inline int QsortCompare(y_absl::string_view a, y_absl::string_view b) {
return a.compare(b);
}
inline int QsortCompare(const TString& a, const TString& b) {
return a.compare(b);
}
template <typename A, typename B>
int QsortCompare(const std::pair<A, B>& a, const std::pair<A, B>& b) {
const int first = QsortCompare(a.first, b.first);
if (first != 0) return first;
return QsortCompare(a.second, b.second);
}
template <typename T>
constexpr size_t HashPointer(T* p, size_t range) {
return (((reinterpret_cast<size_t>(p)) >> 4) ^
((reinterpret_cast<size_t>(p)) >> 9) ^
((reinterpret_cast<size_t>(p)) >> 14)) %
range;
}
// Compute a+b.
// If the result is greater than INT64_MAX, return INT64_MAX.
// If the result is less than INT64_MIN, return INT64_MIN.
inline int64_t SaturatingAdd(int64_t a, int64_t b) {
if (a > 0) {
if (b > INT64_MAX - a) {
return INT64_MAX;
}
} else if (b < INT64_MIN - a) {
return INT64_MIN;
}
return a + b;
}
inline uint32_t MixHash32(uint32_t a, uint32_t b) {
return RotateLeft(a, 2u) ^ b;
}
inline uint32_t RoundUpToPowerOf2(uint32_t v) {
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
} // namespace grpc_core
#define GPR_ARRAY_SIZE(array) (sizeof(array) / sizeof(*(array)))
#endif // GRPC_SRC_CORE_LIB_GPR_USEFUL_H
|