[libc-commits] [PATCH] D110643: [libc] Add support for 128 bit ints in limits.h

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Sep 28 11:27:32 PDT 2021


michaelrj created this revision.
michaelrj added a reviewer: sivachandra.
Herald added subscribers: libc-commits, ecnelises, tschuett, mgorny.
Herald added a project: libc-project.
michaelrj requested review of this revision.

Also, this adds unit tests to check that limits.h complies with the C
standard.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D110643

Files:
  libc/test/utils/CPP/CMakeLists.txt
  libc/test/utils/CPP/limits_test.cpp
  libc/utils/CPP/Limits.h


Index: libc/utils/CPP/Limits.h
===================================================================
--- libc/utils/CPP/Limits.h
+++ libc/utils/CPP/Limits.h
@@ -52,6 +52,13 @@
   static constexpr unsigned long long max() { return ULLONG_MAX; }
   static constexpr unsigned long long min() { return 0; }
 };
+template <> class NumericLimits<__uint128_t> {
+public:
+  static constexpr __uint128_t max() {
+    return (((__uint128_t(1) << 127) - 1) << 1) + 1;
+  }
+  static constexpr __uint128_t min() { return 0; }
+};
 
 } // namespace cpp
 } // namespace __llvm_libc
Index: libc/test/utils/CPP/limits_test.cpp
===================================================================
--- /dev/null
+++ libc/test/utils/CPP/limits_test.cpp
@@ -0,0 +1,42 @@
+//===-- Unittests for Limits ----------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "utils/CPP/Limits.h"
+#include "utils/UnitTest/Test.h"
+
+// This just checks against the C spec, almost all implementations will surpass
+// this.
+TEST(LlvmLibcLimitsTest, LimitsFollowSpec) {
+  ASSERT_GE(__llvm_libc::cpp::NumericLimits<int>::max(), 32767);
+  ASSERT_LE(__llvm_libc::cpp::NumericLimits<int>::min(), -32767);
+
+  ASSERT_GE(__llvm_libc::cpp::NumericLimits<unsigned int>::max(), 65535u);
+
+  ASSERT_GE(__llvm_libc::cpp::NumericLimits<long>::max(), 2147483647l);
+  ASSERT_LE(__llvm_libc::cpp::NumericLimits<long>::min(), -2147483647l);
+
+  ASSERT_GE(__llvm_libc::cpp::NumericLimits<unsigned long>::max(),
+            4294967295ul);
+
+  ASSERT_GE(__llvm_libc::cpp::NumericLimits<long long>::max(),
+            9223372036854775807ll);
+  ASSERT_LE(__llvm_libc::cpp::NumericLimits<long long>::min(),
+            -9223372036854775807ll);
+
+  ASSERT_GE(__llvm_libc::cpp::NumericLimits<unsigned long long>::max(),
+            18446744073709551615ull);
+}
+
+// This checks that the current environment supports 128 bit integers.
+TEST(LlvmLibcLimitsTest, Uint128Works) {
+  __uint128_t max128 = (((__uint128_t(1) << 127) - 1) << 1) + 1;
+  EXPECT_GT(
+      __llvm_libc::cpp::NumericLimits<__uint128_t>::max(),
+      __uint128_t(__llvm_libc::cpp::NumericLimits<unsigned long long>::max()));
+  ASSERT_EQ(__llvm_libc::cpp::NumericLimits<__uint128_t>::max(), max128);
+}
Index: libc/test/utils/CPP/CMakeLists.txt
===================================================================
--- libc/test/utils/CPP/CMakeLists.txt
+++ libc/test/utils/CPP/CMakeLists.txt
@@ -20,6 +20,16 @@
     libc.utils.CPP.standalone_cpp
 )
 
+add_libc_unittest(
+  limits_test
+  SUITE
+    libc_cpp_utils_unittests
+  SRCS
+    limits_test.cpp
+  DEPENDS
+    libc.utils.CPP.standalone_cpp
+)
+
 add_libc_unittest(
   arrayref_test
   SUITE


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110643.375649.patch
Type: text/x-patch
Size: 2973 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20210928/3c177cd3/attachment-0001.bin>


More information about the libc-commits mailing list