[libc-commits] [libc] 91eb0b6 - [libc][NFC] Use STL case for limits
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Mon Aug 1 02:18:35 PDT 2022
Author: Guillaume Chatelet
Date: 2022-08-01T09:18:25Z
New Revision: 91eb0b6584e06f898ad1994962a0bc73937c7f49
URL: https://github.com/llvm/llvm-project/commit/91eb0b6584e06f898ad1994962a0bc73937c7f49
DIFF: https://github.com/llvm/llvm-project/commit/91eb0b6584e06f898ad1994962a0bc73937c7f49.diff
LOG: [libc][NFC] Use STL case for limits
Migrating all private STL code to the standard STL case but keeping it under the CPP namespace to avoid confusion.
Differential Revision: https://reviews.llvm.org/D130762
Added:
libc/src/__support/CPP/limits.h
Modified:
libc/src/__support/CPP/CMakeLists.txt
libc/src/__support/FPUtil/generic/FMod.h
libc/src/__support/str_to_float.h
libc/src/__support/str_to_integer.h
libc/src/stdio/printf_core/converter_utils.h
libc/src/stdio/printf_core/write_int_converter.h
libc/test/src/__support/CPP/limits_test.cpp
utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Removed:
libc/src/__support/CPP/Limits.h
################################################################################
diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index 7172dae2615dd..489715400395b 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -47,7 +47,7 @@ add_header_library(
add_header_library(
limits
HDRS
- Limits.h
+ limits.h
DEPENDS
.uint
)
diff --git a/libc/src/__support/CPP/Limits.h b/libc/src/__support/CPP/limits.h
similarity index 79%
rename from libc/src/__support/CPP/Limits.h
rename to libc/src/__support/CPP/limits.h
index 8f27d075eae2a..1a6f3a0a83fb8 100644
--- a/libc/src/__support/CPP/Limits.h
+++ b/libc/src/__support/CPP/limits.h
@@ -16,60 +16,60 @@
namespace __llvm_libc {
namespace cpp {
-template <class T> class NumericLimits {
+template <class T> class numeric_limits {
public:
static constexpr T max();
static constexpr T min();
};
-// TODO: Add NumericLimits specializations as needed for new types.
+// TODO: Add numeric_limits specializations as needed for new types.
-template <> class NumericLimits<int> {
+template <> class numeric_limits<int> {
public:
static constexpr int max() { return INT_MAX; }
static constexpr int min() { return INT_MIN; }
};
-template <> class NumericLimits<unsigned int> {
+template <> class numeric_limits<unsigned int> {
public:
static constexpr unsigned int max() { return UINT_MAX; }
static constexpr unsigned int min() { return 0; }
};
-template <> class NumericLimits<long> {
+template <> class numeric_limits<long> {
public:
static constexpr long max() { return LONG_MAX; }
static constexpr long min() { return LONG_MIN; }
};
-template <> class NumericLimits<unsigned long> {
+template <> class numeric_limits<unsigned long> {
public:
static constexpr unsigned long max() { return ULONG_MAX; }
static constexpr unsigned long min() { return 0; }
};
-template <> class NumericLimits<long long> {
+template <> class numeric_limits<long long> {
public:
static constexpr long long max() { return LLONG_MAX; }
static constexpr long long min() { return LLONG_MIN; }
};
-template <> class NumericLimits<unsigned long long> {
+template <> class numeric_limits<unsigned long long> {
public:
static constexpr unsigned long long max() { return ULLONG_MAX; }
static constexpr unsigned long long min() { return 0; }
};
-template <> class NumericLimits<short> {
+template <> class numeric_limits<short> {
public:
static constexpr short max() { return SHRT_MAX; }
static constexpr short min() { return SHRT_MIN; }
};
-template <> class NumericLimits<unsigned short> {
+template <> class numeric_limits<unsigned short> {
public:
static constexpr unsigned short max() { return USHRT_MAX; }
static constexpr unsigned short min() { return 0; }
};
-template <> class NumericLimits<char> {
+template <> class numeric_limits<char> {
public:
static constexpr char max() { return CHAR_MAX; }
static constexpr char min() { return CHAR_MIN; }
};
-template <> class NumericLimits<unsigned char> {
+template <> class numeric_limits<unsigned char> {
public:
static constexpr unsigned char max() { return UCHAR_MAX; }
static constexpr unsigned char min() { return 0; }
@@ -79,7 +79,7 @@ template <> class NumericLimits<unsigned char> {
// provides limits of UInt128.
// 2. On platforms where UInt128 resolves to __uint128_t, this specialization
// allows us to unittest UInt<128>.
-template <> class NumericLimits<UInt<128>> {
+template <> class numeric_limits<UInt<128>> {
public:
static constexpr UInt<128> max() { return ~UInt<128>(0); }
static constexpr UInt<128> min() { return 0; }
@@ -87,7 +87,7 @@ template <> class NumericLimits<UInt<128>> {
#ifdef __SIZEOF_INT128__
// On platform where UInt128 resolves to __uint128_t, this specialization
// provides the limits of UInt128.
-template <> class NumericLimits<__uint128_t> {
+template <> class numeric_limits<__uint128_t> {
public:
static constexpr __uint128_t max() { return ~__uint128_t(0); }
static constexpr __uint128_t min() { return 0; }
diff --git a/libc/src/__support/FPUtil/generic/FMod.h b/libc/src/__support/FPUtil/generic/FMod.h
index ea52020153819..a922253e46b04 100644
--- a/libc/src/__support/FPUtil/generic/FMod.h
+++ b/libc/src/__support/FPUtil/generic/FMod.h
@@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_GENERIC_FMOD_H
#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_GENERIC_FMOD_H
-#include "src/__support/CPP/Limits.h"
+#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
@@ -190,7 +190,7 @@ template <typename T> class FModDivisionInvMultHelper {
inline constexpr static intU_t execute(int exp_
diff , int sides_zeroes_count,
intU_t m_x, intU_t m_y) {
if (exp_
diff > sides_zeroes_count) {
- intU_t inv_hy = (cpp::NumericLimits<intU_t>::max() / m_y);
+ intU_t inv_hy = (cpp::numeric_limits<intU_t>::max() / m_y);
while (exp_
diff > sides_zeroes_count) {
exp_
diff -= sides_zeroes_count;
intU_t hd =
diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h
index 2348603bd6e21..3b043ec1222b3 100644
--- a/libc/src/__support/str_to_float.h
+++ b/libc/src/__support/str_to_float.h
@@ -9,8 +9,8 @@
#ifndef LIBC_SRC_SUPPORT_STR_TO_FLOAT_H
#define LIBC_SRC_SUPPORT_STR_TO_FLOAT_H
-#include "src/__support/CPP/Limits.h"
#include "src/__support/CPP/UInt128.h"
+#include "src/__support/CPP/limits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/builtin_wrappers.h"
#include "src/__support/ctype_utils.h"
@@ -738,7 +738,7 @@ decimal_string_to_float(const char *__restrict src, const char DECIMAL_POINT,
// The loop fills the mantissa with as many digits as it can hold
const BitsType bitstype_max_div_by_base =
- __llvm_libc::cpp::NumericLimits<BitsType>::max() / BASE;
+ cpp::numeric_limits<BitsType>::max() / BASE;
while (true) {
if (isdigit(*src)) {
uint32_t digit = *src - '0';
@@ -828,7 +828,7 @@ static inline bool hexadecimal_string_to_float(
// The loop fills the mantissa with as many digits as it can hold
const BitsType bitstype_max_div_by_base =
- __llvm_libc::cpp::NumericLimits<BitsType>::max() / BASE;
+ cpp::numeric_limits<BitsType>::max() / BASE;
while (true) {
if (isalnum(*src)) {
uint32_t digit = b36_char_to_int(*src);
diff --git a/libc/src/__support/str_to_integer.h b/libc/src/__support/str_to_integer.h
index c3505c3713fa6..3f3349d65de74 100644
--- a/libc/src/__support/str_to_integer.h
+++ b/libc/src/__support/str_to_integer.h
@@ -9,7 +9,7 @@
#ifndef LIBC_SRC_SUPPORT_STR_TO_INTEGER_H
#define LIBC_SRC_SUPPORT_STR_TO_INTEGER_H
-#include "src/__support/CPP/Limits.h"
+#include "src/__support/CPP/limits.h"
#include "src/__support/ctype_utils.h"
#include <errno.h>
#include <limits.h>
@@ -92,15 +92,14 @@ static inline T strtointeger(const char *__restrict src,
src = src + 2;
}
- constexpr bool IS_UNSIGNED = (__llvm_libc::cpp::NumericLimits<T>::min() == 0);
+ constexpr bool IS_UNSIGNED = (cpp::numeric_limits<T>::min() == 0);
const bool is_positive = (result_sign == '+');
unsigned long long constexpr NEGATIVE_MAX =
- !IS_UNSIGNED ? static_cast<unsigned long long>(
- __llvm_libc::cpp::NumericLimits<T>::max()) +
- 1
- : __llvm_libc::cpp::NumericLimits<T>::max();
+ !IS_UNSIGNED
+ ? static_cast<unsigned long long>(cpp::numeric_limits<T>::max()) + 1
+ : cpp::numeric_limits<T>::max();
unsigned long long const abs_max =
- (is_positive ? __llvm_libc::cpp::NumericLimits<T>::max() : NEGATIVE_MAX);
+ (is_positive ? cpp::numeric_limits<T>::max() : NEGATIVE_MAX);
unsigned long long const abs_max_div_by_base = abs_max / base;
while (isalnum(*src)) {
int cur_digit = b36_char_to_int(*src);
@@ -137,9 +136,9 @@ static inline T strtointeger(const char *__restrict src,
if (result == abs_max) {
if (is_positive || IS_UNSIGNED)
- return __llvm_libc::cpp::NumericLimits<T>::max();
+ return cpp::numeric_limits<T>::max();
else // T is signed and there is a negative overflow
- return __llvm_libc::cpp::NumericLimits<T>::min();
+ return cpp::numeric_limits<T>::min();
}
return is_positive ? static_cast<T>(result) : -static_cast<T>(result);
diff --git a/libc/src/stdio/printf_core/converter_utils.h b/libc/src/stdio/printf_core/converter_utils.h
index 85895f1ac5b8c..ec605abf7deb8 100644
--- a/libc/src/stdio/printf_core/converter_utils.h
+++ b/libc/src/stdio/printf_core/converter_utils.h
@@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_UTILS_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_CONVERTER_UTILS_H
-#include "src/__support/CPP/Limits.h"
+#include "src/__support/CPP/limits.h"
#include "src/stdio/printf_core/core_structs.h"
#include <inttypes.h>
@@ -21,23 +21,23 @@ namespace printf_core {
inline uintmax_t apply_length_modifier(uintmax_t num, LengthModifier lm) {
switch (lm) {
case LengthModifier::none:
- return num & cpp::NumericLimits<unsigned int>::max();
+ return num & cpp::numeric_limits<unsigned int>::max();
case LengthModifier::l:
- return num & cpp::NumericLimits<unsigned long>::max();
+ return num & cpp::numeric_limits<unsigned long>::max();
case LengthModifier::ll:
case LengthModifier::L:
- return num & cpp::NumericLimits<unsigned long long>::max();
+ return num & cpp::numeric_limits<unsigned long long>::max();
case LengthModifier::h:
- return num & cpp::NumericLimits<unsigned short>::max();
+ return num & cpp::numeric_limits<unsigned short>::max();
case LengthModifier::hh:
- return num & cpp::NumericLimits<unsigned char>::max();
+ return num & cpp::numeric_limits<unsigned char>::max();
case LengthModifier::z:
- return num & cpp::NumericLimits<size_t>::max();
+ return num & cpp::numeric_limits<size_t>::max();
case LengthModifier::t:
// We don't have unsigned ptr
diff so uintptr_t is used, since we need an
// unsigned type and ptr
diff is usually the same size as a pointer.
static_assert(sizeof(ptr
diff _t) == sizeof(uintptr_t));
- return num & cpp::NumericLimits<uintptr_t>::max();
+ return num & cpp::numeric_limits<uintptr_t>::max();
case LengthModifier::j:
return num; // j is intmax, so no mask is necessary.
}
diff --git a/libc/src/stdio/printf_core/write_int_converter.h b/libc/src/stdio/printf_core/write_int_converter.h
index 5baebd96b79c1..b270127afbd5d 100644
--- a/libc/src/stdio/printf_core/write_int_converter.h
+++ b/libc/src/stdio/printf_core/write_int_converter.h
@@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H
-#include "src/__support/CPP/Limits.h"
+#include "src/__support/CPP/limits.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/writer.h"
diff --git a/libc/test/src/__support/CPP/limits_test.cpp b/libc/test/src/__support/CPP/limits_test.cpp
index b55823f5527db..82ce0b812a40d 100644
--- a/libc/test/src/__support/CPP/limits_test.cpp
+++ b/libc/test/src/__support/CPP/limits_test.cpp
@@ -6,39 +6,40 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/CPP/Limits.h"
#include "src/__support/CPP/UInt.h"
+#include "src/__support/CPP/limits.h"
#include "utils/UnitTest/Test.h"
+namespace __llvm_libc {
+
// This just checks against the C spec, almost all implementations will surpass
// this.
TEST(LlvmLibcLimitsTest, LimitsFollowSpec) {
- ASSERT_EQ(__llvm_libc::cpp::NumericLimits<int>::max(), INT_MAX);
- ASSERT_EQ(__llvm_libc::cpp::NumericLimits<int>::min(), INT_MIN);
+ ASSERT_EQ(cpp::numeric_limits<int>::max(), INT_MAX);
+ ASSERT_EQ(cpp::numeric_limits<int>::min(), INT_MIN);
- ASSERT_EQ(__llvm_libc::cpp::NumericLimits<unsigned int>::max(), UINT_MAX);
+ ASSERT_EQ(cpp::numeric_limits<unsigned int>::max(), UINT_MAX);
- ASSERT_EQ(__llvm_libc::cpp::NumericLimits<long>::max(), LONG_MAX);
- ASSERT_EQ(__llvm_libc::cpp::NumericLimits<long>::min(), LONG_MIN);
+ ASSERT_EQ(cpp::numeric_limits<long>::max(), LONG_MAX);
+ ASSERT_EQ(cpp::numeric_limits<long>::min(), LONG_MIN);
- ASSERT_EQ(__llvm_libc::cpp::NumericLimits<unsigned long>::max(), ULONG_MAX);
+ ASSERT_EQ(cpp::numeric_limits<unsigned long>::max(), ULONG_MAX);
- ASSERT_EQ(__llvm_libc::cpp::NumericLimits<long long>::max(), LLONG_MAX);
- ASSERT_EQ(__llvm_libc::cpp::NumericLimits<long long>::min(), LLONG_MIN);
+ ASSERT_EQ(cpp::numeric_limits<long long>::max(), LLONG_MAX);
+ ASSERT_EQ(cpp::numeric_limits<long long>::min(), LLONG_MIN);
- ASSERT_EQ(__llvm_libc::cpp::NumericLimits<unsigned long long>::max(),
- ULLONG_MAX);
+ ASSERT_EQ(cpp::numeric_limits<unsigned long long>::max(), ULLONG_MAX);
}
TEST(LlvmLibcLimitsTest, UInt128Limits) {
- auto umax128 =
- __llvm_libc::cpp::NumericLimits<__llvm_libc::cpp::UInt<128>>::max();
- auto umax64 = __llvm_libc::cpp::UInt<128>(
- __llvm_libc::cpp::NumericLimits<uint64_t>::max());
+ auto umax128 = cpp::numeric_limits<__llvm_libc::cpp::UInt<128>>::max();
+ auto umax64 =
+ __llvm_libc::cpp::UInt<128>(cpp::numeric_limits<uint64_t>::max());
EXPECT_GT(umax128, umax64);
ASSERT_EQ(~__llvm_libc::cpp::UInt<128>(0), umax128);
#ifdef __SIZEOF_INT128__
- ASSERT_EQ(~__uint128_t(0),
- __llvm_libc::cpp::NumericLimits<__uint128_t>::max());
+ ASSERT_EQ(~__uint128_t(0), cpp::numeric_limits<__uint128_t>::max());
#endif
}
+
+} // namespace __llvm_libc
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 6ee055f9ca01a..7cf8e33d364c5 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -71,7 +71,7 @@ cc_library(
cc_library(
name = "__support_cpp_limits",
- hdrs = ["src/__support/CPP/Limits.h"],
+ hdrs = ["src/__support/CPP/limits.h"],
deps = [":libc_root", "__support_cpp_uint"],
)
More information about the libc-commits
mailing list