Debian Codegraph @

src/core/lib/gpr/useful.h

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPC_CORE_LIB_GPR_USEFUL_H
20 #define GRPC_CORE_LIB_GPR_USEFUL_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <cstddef>
25
26 #include "absl/strings/string_view.h"
27 #include "absl/types/variant.h"
28
29 /** useful utilities that don't belong anywhere else */
30
31 namespace grpc_core {
32
33 template <typename T>
34 T Clamp(T val, T min, T max) {
35 if (val < min) return min;
36 if (max < val) return max;
37 return val;
38 }
39
40 /** rotl, rotr assume x is unsigned */
41 template <typename T>
42 constexpr T RotateLeft(T x, T n) {
43 return ((x << n) | (x >> (sizeof(x) * 8 - n)));
44 }
45 template <typename T>
46 constexpr T RotateRight(T x, T n) {
47 return ((x >> n) | (x << (sizeof(x) * 8 - n)));
48 }
49
50 // Set the n-th bit of i
51 template <typename T>
52 T SetBit(T* i, size_t n) {
53 return *i |= (T(1) << n);
54 }
55
56 // Clear the n-th bit of i
57 template <typename T>
58 T ClearBit(T* i, size_t n) {
59 return *i &= ~(T(1) << n);
60 }
61
62 // Get the n-th bit of i
63 template <typename T>
64 bool GetBit(T i, size_t n) {
65 return (i & (T(1) << n)) != 0;
66 }
67
68 namespace useful_detail {
69 inline constexpr uint32_t HexdigitBitcount(uint32_t x) {
70 return (x - ((x >> 1) & 0x77777777) - ((x >> 2) & 0x33333333) -
71 ((x >> 3) & 0x11111111));
72 }
73 } // namespace useful_detail
74
75 inline constexpr uint32_t BitCount(uint32_t i) {
76 return (((useful_detail::HexdigitBitcount(i) +
77 (useful_detail::HexdigitBitcount(i) >> 4)) &
78 0x0f0f0f0f) %
79 255);
80 }
81
82 inline constexpr uint32_t BitCount(uint64_t i) {
83 return BitCount(uint32_t(i)) + BitCount(uint32_t(i >> 32));
84 }
85
86 inline constexpr uint32_t BitCount(uint16_t i) { return BitCount(uint32_t(i)); }
87 inline constexpr uint32_t BitCount(uint8_t i) { return BitCount(uint32_t(i)); }
88 inline constexpr uint32_t BitCount(int64_t i) { return BitCount(uint64_t(i)); }
89 inline constexpr uint32_t BitCount(int32_t i) { return BitCount(uint32_t(i)); }
90 inline constexpr uint32_t BitCount(int16_t i) { return BitCount(uint16_t(i)); }
91 inline constexpr uint32_t BitCount(int8_t i) { return BitCount(uint8_t(i)); }
92
93 // This function uses operator< to implement a qsort-style comparison, whereby:
94 // if a is smaller than b, a number smaller than 0 is returned.
95 // if a is bigger than b, a number greater than 0 is returned.
96 // if a is neither smaller nor bigger than b, 0 is returned.
97 template <typename T>
98 int QsortCompare(const T& a, const T& b) {
99 if (a < b) return -1;
100 if (b < a) return 1;
101 return 0;
102 }
103
104 template <typename... X>
105 int QsortCompare(const absl::variant<X...>& a, const absl::variant<X...>& b) {
106 const int index = QsortCompare(a.index(), b.index());
107 if (index != 0) return index;
108 return absl::visit(
109 [&](const auto& x) {
110 return QsortCompare(x, absl::get<absl::remove_cvref_t<decltype(x)>>(b));
111 },
112 a);
113 }
114
115 inline int QsortCompare(absl::string_view a, absl::string_view b) {
116 return a.compare(b);
117 }
118
119 inline int QsortCompare(const std::string& a, const std::string& b) {
120 return a.compare(b);
121 }
122
123 template <typename A, typename B>
124 int QsortCompare(const std::pair<A, B>& a, const std::pair<A, B>& b) {
125 const int first = QsortCompare(a.first, b.first);
126 if (first != 0) return first;
127 return QsortCompare(a.second, b.second);
128 }
129
130 template <typename T>
131 constexpr size_t HashPointer(T* p, size_t range) {
132 return (((reinterpret_cast<size_t>(p)) >> 4) ^
133 ((reinterpret_cast<size_t>(p)) >> 9) ^
134 ((reinterpret_cast<size_t>(p)) >> 14)) %
135 range;
136 }
137
138 // Compute a+b.
139 // If the result is greater than INT64_MAX, return INT64_MAX.
140 // If the result is less than INT64_MIN, return INT64_MIN.
141 inline int64_t SaturatingAdd(int64_t a, int64_t b) {
142 if (a > 0) {
143 if (b > INT64_MAX - a) {
144 return INT64_MAX;
145 }
146 } else if (b < INT64_MIN - a) {
147 return INT64_MIN;
148 }
149 return a + b;
150 }
151
152 inline uint32_t MixHash32(uint32_t a, uint32_t b) {
153 return RotateLeft(a, 2u) ^ b;
154 }
155
156 inline uint32_t RoundUpToPowerOf2(uint32_t v) {
157 v--;
158 v |= v >> 1;
159 v |= v >> 2;
160 v |= v >> 4;
161 v |= v >> 8;
162 v |= v >> 16;
163 v++;
164 return v;
165 }
166
167 } // namespace grpc_core
168
169 #define GPR_ARRAY_SIZE(array) (sizeof(array) / sizeof(*(array)))
170
171 #endif /* GRPC_CORE_LIB_GPR_USEFUL_H */