useful.h
Go to the documentation of this file.
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 
23 
24 #include <cstddef>
25 
28 namespace grpc_core {
29 
30 template <typename T>
31 T Clamp(T val, T min, T max) {
32  if (val < min) return min;
33  if (max < val) return max;
34  return val;
35 }
36 
38 template <typename T>
39 constexpr T RotateLeft(T x, T n) {
40  return ((x << n) | (x >> (sizeof(x) * 8 - n)));
41 }
42 template <typename T>
43 constexpr T RotateRight(T x, T n) {
44  return ((x >> n) | (x << (sizeof(x) * 8 - n)));
45 }
46 
47 // Set the n-th bit of i
48 template <typename T>
49 T SetBit(T* i, size_t n) {
50  return *i |= (T(1) << n);
51 }
52 
53 // Clear the n-th bit of i
54 template <typename T>
55 T ClearBit(T* i, size_t n) {
56  return *i &= ~(T(1) << n);
57 }
58 
59 // Get the n-th bit of i
60 template <typename T>
61 bool GetBit(T i, size_t n) {
62  return (i & (T(1) << n)) != 0;
63 }
64 
65 namespace useful_detail {
66 inline constexpr uint32_t HexdigitBitcount(uint32_t x) {
67  return (x - ((x >> 1) & 0x77777777) - ((x >> 2) & 0x33333333) -
68  ((x >> 3) & 0x11111111));
69 }
70 } // namespace useful_detail
71 
72 inline constexpr uint32_t BitCount(uint32_t i) {
75  0x0f0f0f0f) %
76  255);
77 }
78 
79 inline constexpr uint32_t BitCount(uint64_t i) {
80  return BitCount(uint32_t(i)) + BitCount(uint32_t(i >> 32));
81 }
82 
83 inline constexpr uint32_t BitCount(uint16_t i) { return BitCount(uint32_t(i)); }
84 inline constexpr uint32_t BitCount(uint8_t i) { return BitCount(uint32_t(i)); }
85 inline constexpr uint32_t BitCount(int64_t i) { return BitCount(uint64_t(i)); }
86 inline constexpr uint32_t BitCount(int32_t i) { return BitCount(uint32_t(i)); }
87 inline constexpr uint32_t BitCount(int16_t i) { return BitCount(uint16_t(i)); }
88 inline constexpr uint32_t BitCount(int8_t i) { return BitCount(uint8_t(i)); }
89 
90 // This function uses operator< to implement a qsort-style comparison, whereby:
91 // if a is smaller than b, a number smaller than 0 is returned.
92 // if a is bigger than b, a number greater than 0 is returned.
93 // if a is neither smaller nor bigger than b, 0 is returned.
94 template <typename T>
95 int QsortCompare(const T& a, const T& b) {
96  if (a < b) return -1;
97  if (b < a) return 1;
98  return 0;
99 }
100 
101 template <typename T>
102 constexpr size_t HashPointer(T* p, size_t range) {
103  return (((reinterpret_cast<size_t>(p)) >> 4) ^
104  ((reinterpret_cast<size_t>(p)) >> 9) ^
105  ((reinterpret_cast<size_t>(p)) >> 14)) %
106  range;
107 }
108 
109 // Compute a+b.
110 // If the result is greater than INT64_MAX, return INT64_MAX.
111 // If the result is less than INT64_MIN, return INT64_MIN.
113  if (a > 0) {
114  if (b > INT64_MAX - a) {
115  return INT64_MAX;
116  }
117  } else if (b < INT64_MIN - a) {
118  return INT64_MIN;
119  }
120  return a + b;
121 }
122 
124  return RotateLeft(a, 2u) ^ b;
125 }
126 
127 } // namespace grpc_core
128 
129 #define GPR_ARRAY_SIZE(array) (sizeof(array) / sizeof(*(array)))
130 
131 #endif /* GRPC_CORE_LIB_GPR_USEFUL_H */
grpc_core::MixHash32
uint32_t MixHash32(uint32_t a, uint32_t b)
Definition: useful.h:123
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
grpc_core
Definition: call_metric_recorder.h:31
INT64_MAX
#define INT64_MAX
Definition: stdint-msvc2008.h:139
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
T
#define T(upbtypeconst, upbtype, ctype, default_value)
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
int16_t
signed short int16_t
Definition: stdint-msvc2008.h:76
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc_core::GetBit
bool GetBit(T i, size_t n)
Definition: useful.h:61
grpc_core::RotateRight
constexpr T RotateRight(T x, T n)
Definition: useful.h:43
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
grpc_core::ClearBit
T ClearBit(T *i, size_t n)
Definition: useful.h:55
grpc_core::SaturatingAdd
int64_t SaturatingAdd(int64_t a, int64_t b)
Definition: useful.h:112
grpc_core::HashPointer
constexpr size_t HashPointer(T *p, size_t range)
Definition: useful.h:102
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
min
#define min(a, b)
Definition: qsort.h:83
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
INT64_MIN
#define INT64_MIN
Definition: stdint-msvc2008.h:138
grpc_core::Clamp
T Clamp(T val, T min, T max)
Definition: useful.h:31
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
grpc_core::BitCount
constexpr uint32_t BitCount(uint32_t i)
Definition: useful.h:72
grpc_core::SetBit
T SetBit(T *i, size_t n)
Definition: useful.h:49
int8_t
signed char int8_t
Definition: stdint-msvc2008.h:75
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_core::RotateLeft
constexpr T RotateLeft(T x, T n)
Definition: useful.h:39
grpc_core::useful_detail::HexdigitBitcount
constexpr uint32_t HexdigitBitcount(uint32_t x)
Definition: useful.h:66
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:48