Skip to content

Commit

Permalink
[libc][NFC] Use STL case for limits
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gchatelet committed Aug 1, 2022
1 parent 984d1bf commit 91eb0b6
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ add_header_library(
add_header_library(
limits
HDRS
Limits.h
limits.h
DEPENDS
.uint
)
Expand Down
28 changes: 14 additions & 14 deletions libc/src/__support/CPP/Limits.h → libc/src/__support/CPP/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -79,15 +79,15 @@ 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; }
};
#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; }
Expand Down
4 changes: 2 additions & 2 deletions libc/src/__support/FPUtil/generic/FMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 =
Expand Down
6 changes: 3 additions & 3 deletions libc/src/__support/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 8 additions & 9 deletions libc/src/__support/str_to_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions libc/src/stdio/printf_core/converter_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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 ptrdiff so uintptr_t is used, since we need an
// unsigned type and ptrdiff is usually the same size as a pointer.
static_assert(sizeof(ptrdiff_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.
}
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdio/printf_core/write_int_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
35 changes: 18 additions & 17 deletions libc/test/src/__support/CPP/limits_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)

Expand Down

0 comments on commit 91eb0b6

Please sign in to comment.