diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt --- a/libc/src/__support/CMakeLists.txt +++ b/libc/src/__support/CMakeLists.txt @@ -91,6 +91,7 @@ integer_to_string.h DEPENDS libc.src.__support.common + libc.src.__support.CPP.algorithm libc.src.__support.CPP.limits libc.src.__support.CPP.span libc.src.__support.CPP.string_view diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt --- a/libc/src/__support/CPP/CMakeLists.txt +++ b/libc/src/__support/CPP/CMakeLists.txt @@ -1,3 +1,9 @@ +add_header_library( + algorithm + HDRS + algorithm.h +) + add_header_library( array HDRS diff --git a/libc/src/__support/CPP/algorithm.h b/libc/src/__support/CPP/algorithm.h new file mode 100644 --- /dev/null +++ b/libc/src/__support/CPP/algorithm.h @@ -0,0 +1,31 @@ +//===-- A self contained equivalent of --------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// This file is minimalist on purpose but can receive a few more function if +// they prove useful. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_ALGORITHM_H +#define LLVM_LIBC_SRC_SUPPORT_CPP_ALGORITHM_H + +#include "src/__support/macros/attributes.h" // LIBC_INLINE + +namespace __llvm_libc { +namespace cpp { + +template LIBC_INLINE constexpr const T &max(const T &a, const T &b) { + return (a < b) ? b : a; +} + +template LIBC_INLINE constexpr const T &min(const T &a, const T &b) { + return (a < b) ? a : b; +} + +} // namespace cpp +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SUPPORT_CPP_ALGORITHM_H diff --git a/libc/src/__support/RPC/CMakeLists.txt b/libc/src/__support/RPC/CMakeLists.txt --- a/libc/src/__support/RPC/CMakeLists.txt +++ b/libc/src/__support/RPC/CMakeLists.txt @@ -9,9 +9,10 @@ rpc_util.h DEPENDS libc.src.__support.common + libc.src.__support.CPP.algorithm libc.src.__support.CPP.atomic - libc.src.__support.CPP.optional libc.src.__support.CPP.functional + libc.src.__support.CPP.optional libc.src.__support.GPU.utils ) diff --git a/libc/src/__support/RPC/rpc.h b/libc/src/__support/RPC/rpc.h --- a/libc/src/__support/RPC/rpc.h +++ b/libc/src/__support/RPC/rpc.h @@ -19,6 +19,7 @@ #define LLVM_LIBC_SRC_SUPPORT_RPC_RPC_H #include "rpc_util.h" +#include "src/__support/CPP/algorithm.h" // max #include "src/__support/CPP/atomic.h" #include "src/__support/CPP/functional.h" #include "src/__support/CPP/optional.h" @@ -462,7 +463,7 @@ send([&](Buffer *buffer, uint32_t id) { reinterpret_cast(buffer->data)[0] = lane_value(size, id); num_sends = is_process_gpu() ? lane_value(size, id) - : max(lane_value(size, id), num_sends); + : cpp::max(lane_value(size, id), num_sends); uint64_t len = lane_value(size, id) > sizeof(Buffer::data) - sizeof(uint64_t) ? sizeof(Buffer::data) - sizeof(uint64_t) @@ -495,7 +496,7 @@ lane_value(dst, id) = reinterpret_cast(alloc(lane_value(size, id))); num_recvs = is_process_gpu() ? lane_value(size, id) - : max(lane_value(size, id), num_recvs); + : cpp::max(lane_value(size, id), num_recvs); uint64_t len = lane_value(size, id) > sizeof(Buffer::data) - sizeof(uint64_t) ? sizeof(Buffer::data) - sizeof(uint64_t) diff --git a/libc/src/__support/RPC/rpc_util.h b/libc/src/__support/RPC/rpc_util.h --- a/libc/src/__support/RPC/rpc_util.h +++ b/libc/src/__support/RPC/rpc_util.h @@ -11,7 +11,7 @@ #include "src/__support/CPP/type_traits.h" #include "src/__support/GPU/utils.h" -#include "src/__support/macros/attributes.h" +#include "src/__support/macros/attributes.h" // LIBC_INLINE #include "src/__support/macros/properties/architectures.h" namespace __llvm_libc { @@ -68,11 +68,6 @@ return val[id]; } -/// Helper to get the maximum value. -template LIBC_INLINE const T &max(const T &x, const T &y) { - return x < y ? y : x; -} - /// Advance the \p p by \p bytes. template LIBC_INLINE T *advance(T *ptr, U bytes) { if constexpr (cpp::is_const_v) diff --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h --- a/libc/src/__support/integer_to_string.h +++ b/libc/src/__support/integer_to_string.h @@ -59,6 +59,7 @@ #include +#include "src/__support/CPP/algorithm.h" // max #include "src/__support/CPP/array.h" #include "src/__support/CPP/bit.h" #include "src/__support/CPP/limits.h" @@ -196,10 +197,7 @@ constexpr size_t BITS_PER_DIGIT = floor_log_2(Fmt::BASE); return ((sizeof(T) * 8 + (BITS_PER_DIGIT - 1)) / BITS_PER_DIGIT); }; - constexpr auto max = [](size_t a, size_t b) -> size_t { - return a > b ? a : b; - }; - constexpr size_t digit_size = max(max_digits(), Fmt::MIN_DIGITS); + constexpr size_t digit_size = cpp::max(max_digits(), Fmt::MIN_DIGITS); constexpr size_t sign_size = Fmt::BASE == 10 ? 1 : 0; constexpr size_t prefix_size = Fmt::PREFIX ? 2 : 0; return digit_size + sign_size + prefix_size; diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -143,6 +143,14 @@ ], ) +libc_support_library( + name = "__support_cpp_algorithm", + hdrs = ["src/__support/CPP/algorithm.h"], + deps = [ + ":libc_root", + ], +) + libc_support_library( name = "__support_cpp_array", hdrs = ["src/__support/CPP/array.h"], @@ -413,6 +421,7 @@ hdrs = ["src/__support/integer_to_string.h"], deps = [ ":__support_common", + ":__support_cpp_algorithm", ":__support_cpp_bit", ":__support_cpp_limits", ":__support_cpp_optional",