[libc-commits] [libc] 472fa04 - [libc][nfc] add CPP Limits.h for numeric_limits

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Aug 12 14:31:21 PDT 2021


Author: Michael Jones
Date: 2021-08-12T21:31:16Z
New Revision: 472fa04de8f3c0e0381d2209ed01d3c18b4ecf4a

URL: https://github.com/llvm/llvm-project/commit/472fa04de8f3c0e0381d2209ed01d3c18b4ecf4a
DIFF: https://github.com/llvm/llvm-project/commit/472fa04de8f3c0e0381d2209ed01d3c18b4ecf4a.diff

LOG: [libc][nfc] add CPP Limits.h for numeric_limits

Add an implementation of numeric_limits for use in str_conv_utils.
It currently only supports the basic integer types, with more types
coming as needed.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D107987

Added: 
    libc/utils/CPP/Limits.h

Modified: 
    libc/utils/CPP/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/utils/CPP/CMakeLists.txt b/libc/utils/CPP/CMakeLists.txt
index 67483ab97d264..72b3e786808a9 100644
--- a/libc/utils/CPP/CMakeLists.txt
+++ b/libc/utils/CPP/CMakeLists.txt
@@ -7,4 +7,5 @@ add_header_library(
     Functional.h
     StringView.h
     TypeTraits.h
+    Limits.h
 )

diff  --git a/libc/utils/CPP/Limits.h b/libc/utils/CPP/Limits.h
new file mode 100644
index 0000000000000..1fb57d1f82cec
--- /dev/null
+++ b/libc/utils/CPP/Limits.h
@@ -0,0 +1,59 @@
+//===-- A self contained equivalent of std::limits --------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_UTILS_CPP_LIMITS_H
+#define LLVM_LIBC_UTILS_CPP_LIMITS_H
+
+#include <limits.h>
+
+namespace __llvm_libc {
+namespace cpp {
+
+template <class T> class NumericLimits {
+public:
+  static constexpr T max();
+  static constexpr T min();
+};
+
+// TODO: Add NumericLimits specializations as needed for new types.
+
+template <> class NumericLimits<int> {
+public:
+  static constexpr int max() { return INT_MAX; }
+  static constexpr int min() { return INT_MIN; }
+};
+template <> class NumericLimits<unsigned int> {
+public:
+  static constexpr unsigned int max() { return UINT_MAX; }
+  static constexpr unsigned int min() { return 0; }
+};
+template <> class NumericLimits<long> {
+public:
+  static constexpr long max() { return LONG_MAX; }
+  static constexpr long min() { return LONG_MIN; }
+};
+template <> class NumericLimits<unsigned long> {
+public:
+  static constexpr long max() { return ULONG_MAX; }
+  static constexpr long min() { return 0; }
+};
+template <> class NumericLimits<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> {
+public:
+  static constexpr long max() { return ULLONG_MAX; }
+  static constexpr long min() { return 0; }
+};
+
+} // namespace cpp
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_UTILS_CPP_LIMITS_H


        


More information about the libc-commits mailing list