diff options
Diffstat (limited to 'contrib/compiler-rt/lib/xray/xray_utils.h')
-rw-r--r-- | contrib/compiler-rt/lib/xray/xray_utils.h | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/contrib/compiler-rt/lib/xray/xray_utils.h b/contrib/compiler-rt/lib/xray/xray_utils.h index 1ecc74a2dce8..eafa16e1a9d5 100644 --- a/contrib/compiler-rt/lib/xray/xray_utils.h +++ b/contrib/compiler-rt/lib/xray/xray_utils.h @@ -15,6 +15,8 @@ #ifndef XRAY_UTILS_H #define XRAY_UTILS_H +#include <cstddef> +#include <cstdint> #include <sys/types.h> #include <utility> @@ -24,7 +26,7 @@ namespace __xray { void printToStdErr(const char *Buffer); // EINTR-safe write routine, provided a file descriptor and a character range. -void retryingWriteAll(int Fd, char *Begin, char *End); +void retryingWriteAll(int Fd, const char *Begin, const char *End); // Reads a long long value from a provided file. bool readValueFromFile(const char *Filename, long long *Value); @@ -36,6 +38,32 @@ std::pair<ssize_t, bool> retryingReadSome(int Fd, char *Begin, char *End); // file. int getLogFD(); +constexpr size_t gcd(size_t a, size_t b) { + return (b == 0) ? a : gcd(b, a % b); +} + +constexpr size_t lcm(size_t a, size_t b) { return a * b / gcd(a, b); } + +constexpr size_t nearest_boundary(size_t number, size_t multiple) { + return multiple * ((number / multiple) + (number % multiple ? 1 : 0)); +} + +constexpr size_t next_pow2_helper(size_t num, size_t acc) { + return (1u << acc) >= num ? (1u << acc) : next_pow2_helper(num, acc + 1); +} + +constexpr size_t next_pow2(size_t number) { + return next_pow2_helper(number, 1); +} + +template <class T> constexpr T &max(T &A, T &B) { return A > B ? A : B; } + +template <class T> constexpr T &min(T &A, T &B) { return A <= B ? A : B; } + +constexpr ptrdiff_t diff(uintptr_t A, uintptr_t B) { + return max(A, B) - min(A, B); +} + } // namespace __xray #endif // XRAY_UTILS_H |