[libc-commits] [PATCH] D107987: [libc][nfc] add CPP Limits.h for numeric_limits

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu Aug 12 13:42:36 PDT 2021


michaelrj updated this revision to Diff 366092.
michaelrj marked 3 inline comments as done.
michaelrj added a comment.

Rename numeric_limits to NumericLimits and make all the functions constexpr.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D107987/new/

https://reviews.llvm.org/D107987

Files:
  libc/utils/CPP/CMakeLists.txt
  libc/utils/CPP/Limits.h


Index: libc/utils/CPP/Limits.h
===================================================================
--- /dev/null
+++ libc/utils/CPP/Limits.h
@@ -0,0 +1,57 @@
+//===-- 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();
+};
+
+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
Index: libc/utils/CPP/CMakeLists.txt
===================================================================
--- libc/utils/CPP/CMakeLists.txt
+++ libc/utils/CPP/CMakeLists.txt
@@ -7,4 +7,5 @@
     Functional.h
     StringView.h
     TypeTraits.h
+    Limits.h
 )


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107987.366092.patch
Type: text/x-patch
Size: 2137 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20210812/14d74222/attachment.bin>


More information about the libc-commits mailing list