[libcxx-commits] [libcxx] [libc++][math] Add constexpr for std::abs() (PR #146633)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 1 23:12:13 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- libcxx/include/__math/abs.h libcxx/test/libcxx/numerics/c.math/constexpr-cxx23-clang.pass.cpp libcxx/test/std/numerics/c.math/abs.pass.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libcxx/include/__math/abs.h b/libcxx/include/__math/abs.h
index a1d5854ac..cf047df68 100644
--- a/libcxx/include/__math/abs.h
+++ b/libcxx/include/__math/abs.h
@@ -41,10 +41,15 @@ template <class _A1, __enable_if_t<is_integral<_A1>::value, int> = 0>
 
 // abs
 
-[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline float abs(float __x) _NOEXCEPT { return __builtin_fabsf(__x); }
-[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline double abs(double __x) _NOEXCEPT { return __builtin_fabs(__x); }
+[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline float abs(float __x) _NOEXCEPT {
+  return __builtin_fabsf(__x);
+}
+[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline double abs(double __x) _NOEXCEPT {
+  return __builtin_fabs(__x);
+}
 
-[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline long double abs(long double __x) _NOEXCEPT {
+[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline long double
+abs(long double __x) _NOEXCEPT {
   return __builtin_fabsl(__x);
 }
 
diff --git a/libcxx/test/std/numerics/c.math/abs.pass.cpp b/libcxx/test/std/numerics/c.math/abs.pass.cpp
index 908620b2a..6c9456f0d 100644
--- a/libcxx/test/std/numerics/c.math/abs.pass.cpp
+++ b/libcxx/test/std/numerics/c.math/abs.pass.cpp
@@ -26,23 +26,24 @@ TEST_CONSTEXPR_CXX23 void test_abs() {
   TEST_CONSTEXPR_CXX23 Result res     = 5;
 
   ASSERT_SAME_TYPE(decltype(std::abs(neg_val)), Result);
-  #if TEST_STD_VER >= 23
-    static_assert(std::abs(neg_val) == res);
-    static_assert(std::abs(pos_val) == res);
-  #else 
-    assert(std::abs(neg_val) == res);
-    assert(std::abs(pos_val) == res);
-  #endif
+#if TEST_STD_VER >= 23
+  static_assert(std::abs(neg_val) == res);
+  static_assert(std::abs(pos_val) == res);
+#else
+  assert(std::abs(neg_val) == res);
+  assert(std::abs(pos_val) == res);
+#endif
 }
 
 TEST_CONSTEXPR_CXX23 void test_big() {
-  TEST_CONSTEXPR_CXX23 long long int big_value          = std::numeric_limits<long long int>::max(); // a value too big for ints to store
+  TEST_CONSTEXPR_CXX23 long long int big_value =
+      std::numeric_limits<long long int>::max(); // a value too big for ints to store
   TEST_CONSTEXPR_CXX23 long long int negative_big_value = -big_value;
-  #if TEST_STD_VER >= 23
-    static_assert(std::abs(negative_big_value) == big_value); // make sure it doesn't get casted to a smaller type
-  #else
-    assert(std::abs(negative_big_value) == big_value); // make sure it doesn't get casted to a smaller type
-  #endif
+#if TEST_STD_VER >= 23
+  static_assert(std::abs(negative_big_value) == big_value); // make sure it doesn't get casted to a smaller type
+#else
+  assert(std::abs(negative_big_value) == big_value); // make sure it doesn't get casted to a smaller type
+#endif
 }
 
 // The following is helpful to keep in mind:

``````````

</details>


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


More information about the libcxx-commits mailing list