[libcxx-commits] [libcxx] [libc++] Refactor __next_prime to be const (PR #157421)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Sep 9 05:18:49 PDT 2025


================
@@ -72,7 +73,28 @@ struct __is_hash_value_type : false_type {};
 template <class _One>
 struct __is_hash_value_type<_One> : __is_hash_value_type_imp<__remove_cvref_t<_One> > {};
 
-_LIBCPP_EXPORTED_FROM_ABI size_t __next_prime(size_t __n);
+_LIBCPP_HIDE_FROM_ABI inline void __check_for_overflow(size_t __n) {
+  if _LIBCPP_CONSTEXPR (sizeof(size_t) == 4) {
+    if (__n > 0xFFFFFFFB)
+      std::__throw_overflow_error("__next_prime overflow");
+  } else {
+    if (__n > 0xFFFFFFFFFFFFFFC5ull)
+      std::__throw_overflow_error("__next_prime overflow");
+  }
+}
+
+#if _LIBCPP_AVAILABILITY_HAS_NEXT_PRIME_IMPL
+[[__gnu__::__const__]] _LIBCPP_EXPORTED_FROM_ABI size_t __next_prime_impl(size_t) _NOEXCEPT;
----------------
ldionne wrote:

I feel like a short comment explaining what this does and why there's also `__next_prime` would be helpful. This seems confusing for someone not familiar with this patch. Maybe just explain that a new `__next_prime_impl` was introduce to allow inlining the `__check_overflow` call and this dance deals with backwards compatibility.

https://github.com/llvm/llvm-project/pull/157421


More information about the libcxx-commits mailing list