[libcxx-commits] [libcxx] 03c19e9 - [libc++][numeric] P0543R3: Saturation arithmetic (#77967)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jan 21 20:57:50 PST 2024


Author: Hristo Hristov
Date: 2024-01-22T06:57:45+02:00
New Revision: 03c19e91e8d8cb706b58e02d69f80caeaf7eb0f4

URL: https://github.com/llvm/llvm-project/commit/03c19e91e8d8cb706b58e02d69f80caeaf7eb0f4
DIFF: https://github.com/llvm/llvm-project/commit/03c19e91e8d8cb706b58e02d69f80caeaf7eb0f4.diff

LOG: [libc++][numeric] P0543R3: Saturation arithmetic (#77967)

Implements: https://wg21.link/P0543R3
- https://eel.is/c++draft/numeric.sat

Additional references:
- Division: https://eel.is/c++draft/expr.mul#4
- Arithmetic conversions: https://eel.is/c++draft/expr.arith.conv#1
- Clang builtins:
https://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions

Depends on: https://github.com/llvm/llvm-project/pull/78086

---------

Co-authored-by: Zingam <zingam at outlook.com>
Co-authored-by: Mark de Wever <zar-rpg at xs4all.nl>

Added: 
    libcxx/include/__numeric/saturation_arithmetic.h
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.assert.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.compile.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.compile.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.compile.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.compile.pass.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.pass.cpp

Modified: 
    libcxx/docs/FeatureTestMacroTable.rst
    libcxx/docs/ReleaseNotes/18.rst
    libcxx/docs/Status/Cxx2cPapers.csv
    libcxx/include/CMakeLists.txt
    libcxx/include/libcxx.imp
    libcxx/include/module.modulemap.in
    libcxx/include/numeric
    libcxx/include/version
    libcxx/modules/std/numeric.inc
    libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp
    libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
    libcxx/utils/generate_feature_test_macro_components.py

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index 82e51d4e7eb53d..d0d057e6bbaf0e 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -432,7 +432,7 @@ Status
     --------------------------------------------------- -----------------
     ``__cpp_lib_rcu``                                   *unimplemented*
     --------------------------------------------------- -----------------
-    ``__cpp_lib_saturation_arithmetic``                 *unimplemented*
+    ``__cpp_lib_saturation_arithmetic``                 ``202311L``
     --------------------------------------------------- -----------------
     ``__cpp_lib_smart_ptr_owner_equality``              *unimplemented*
     --------------------------------------------------- -----------------

diff  --git a/libcxx/docs/ReleaseNotes/18.rst b/libcxx/docs/ReleaseNotes/18.rst
index 0b43d76650a9d3..b868b37790f77a 100644
--- a/libcxx/docs/ReleaseNotes/18.rst
+++ b/libcxx/docs/ReleaseNotes/18.rst
@@ -75,6 +75,7 @@ Implemented Papers
 - P2909R4 - Fix formatting of code units as integers (Dude, where’s my ``char``?)
 - P2821R5 - ``span.at()``
 - P0521R0 - Proposed Resolution for CA 14 (``shared_ptr`` ``use_count/unique``)
+- P0543R3 - Saturation arithmetic
 - P1759R6 - Native handles and file streams
 - P2868R3 - Remove Deprecated ``std::allocator`` Typedef From C++26
 - P2517R1 - Add a conditional ``noexcept`` specification to ``std::apply``

diff  --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv
index e49290b45589a9..c45aa3c510072e 100644
--- a/libcxx/docs/Status/Cxx2cPapers.csv
+++ b/libcxx/docs/Status/Cxx2cPapers.csv
@@ -27,7 +27,7 @@
 "`P2714R1 <https://wg21.link/P2714R1>`__","LWG","Bind front and back to NTTP callables","Varna June 2023","","",""
 "`P2630R4 <https://wg21.link/P2630R4>`__","LWG","``submdspan``","Varna June 2023","","",""
 "","","","","","",""
-"`P0543R3 <https://wg21.link/P0543R3>`__","LWG","Saturation arithmetic","Kona November 2023","","",""
+"`P0543R3 <https://wg21.link/P0543R3>`__","LWG","Saturation arithmetic","Kona November 2023","|Complete|","18.0",""
 "`P2407R5 <https://wg21.link/P2407R5>`__","LWG","Freestanding Library: Partial Classes","Kona November 2023","","",""
 "`P2546R5 <https://wg21.link/P2546R5>`__","LWG","Debugging Support","Kona November 2023","","",""
 "`P2905R2 <https://wg21.link/P2905R2>`__","LWG","Runtime format strings","Kona November 2023","|Complete|","18.0","|format| |DR|"

diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 9fdf978a89d7ed..ed721d467e94f4 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -569,6 +569,7 @@ set(files
   __numeric/pstl_reduce.h
   __numeric/pstl_transform_reduce.h
   __numeric/reduce.h
+  __numeric/saturation_arithmetic.h
   __numeric/transform_exclusive_scan.h
   __numeric/transform_inclusive_scan.h
   __numeric/transform_reduce.h

diff  --git a/libcxx/include/__numeric/saturation_arithmetic.h b/libcxx/include/__numeric/saturation_arithmetic.h
new file mode 100644
index 00000000000000..50274c6bbd9f3a
--- /dev/null
+++ b/libcxx/include/__numeric/saturation_arithmetic.h
@@ -0,0 +1,110 @@
+// -*- 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 _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H
+#define _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H
+
+#include <__concepts/arithmetic.h>
+#include <__config>
+#include <__utility/cmp.h>
+#include <limits>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 26
+
+template <__libcpp_integer _Tp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
+  if (_Tp __sum; !__builtin_add_overflow(__x, __y, &__sum))
+    return __sum;
+  // Handle overflow
+  if constexpr (__libcpp_unsigned_integer<_Tp>) {
+    return std::numeric_limits<_Tp>::max();
+  } else {
+    // Signed addition overflow
+    if (__x > 0)
+      // Overflows if (x > 0 && y > 0)
+      return std::numeric_limits<_Tp>::max();
+    else
+      // Overflows if  (x < 0 && y < 0)
+      return std::numeric_limits<_Tp>::min();
+  }
+}
+
+template <__libcpp_integer _Tp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {
+  if (_Tp __sub; !__builtin_sub_overflow(__x, __y, &__sub))
+    return __sub;
+  // Handle overflow
+  if constexpr (__libcpp_unsigned_integer<_Tp>) {
+    // Overflows if (x < y)
+    return std::numeric_limits<_Tp>::min();
+  } else {
+    // Signed subtration overflow
+    if (__x >= 0)
+      // Overflows if (x >= 0 && y < 0)
+      return std::numeric_limits<_Tp>::max();
+    else
+      // Overflows if (x < 0 && y > 0)
+      return std::numeric_limits<_Tp>::min();
+  }
+}
+
+template <__libcpp_integer _Tp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {
+  if (_Tp __mul; !__builtin_mul_overflow(__x, __y, &__mul))
+    return __mul;
+  // Handle overflow
+  if constexpr (__libcpp_unsigned_integer<_Tp>) {
+    return std::numeric_limits<_Tp>::max();
+  } else {
+    // Signed multiplication overflow
+    if ((__x > 0 && __y > 0) || (__x < 0 && __y < 0))
+      return std::numeric_limits<_Tp>::max();
+    // Overflows if (x < 0 && y > 0) || (x > 0 && y < 0)
+    return std::numeric_limits<_Tp>::min();
+  }
+}
+
+template <__libcpp_integer _Tp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {
+  _LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined");
+  if constexpr (__libcpp_unsigned_integer<_Tp>) {
+    return __x / __y;
+  } else {
+    // Handle signed division overflow
+    if (__x == std::numeric_limits<_Tp>::min() && __y == _Tp{-1})
+      return std::numeric_limits<_Tp>::max();
+    return __x / __y;
+  }
+}
+
+template <__libcpp_integer _Rp, __libcpp_integer _Tp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {
+  // Saturation is impossible edge case when ((min _Rp) < (min _Tp) && (max _Rp) > (max _Tp)) and it is expected to be
+  // optimized out by the compiler.
+
+  // Handle overflow
+  if (std::cmp_less(__x, std::numeric_limits<_Rp>::min()))
+    return std::numeric_limits<_Rp>::min();
+  if (std::cmp_greater(__x, std::numeric_limits<_Rp>::max()))
+    return std::numeric_limits<_Rp>::max();
+  // No overflow
+  return static_cast<_Rp>(__x);
+}
+
+#endif // _LIBCPP_STD_VER >= 26
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H

diff  --git a/libcxx/include/libcxx.imp b/libcxx/include/libcxx.imp
index 8616f9639f4d90..45fa4a9541917f 100644
--- a/libcxx/include/libcxx.imp
+++ b/libcxx/include/libcxx.imp
@@ -559,6 +559,7 @@
   { include: [ "<__numeric/pstl_reduce.h>", "private", "<numeric>", "public" ] },
   { include: [ "<__numeric/pstl_transform_reduce.h>", "private", "<numeric>", "public" ] },
   { include: [ "<__numeric/reduce.h>", "private", "<numeric>", "public" ] },
+  { include: [ "<__numeric/saturation_arithmetic.h>", "private", "<numeric>", "public" ] },
   { include: [ "<__numeric/transform_exclusive_scan.h>", "private", "<numeric>", "public" ] },
   { include: [ "<__numeric/transform_inclusive_scan.h>", "private", "<numeric>", "public" ] },
   { include: [ "<__numeric/transform_reduce.h>", "private", "<numeric>", "public" ] },

diff  --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index d10670d4faaffc..194a74a1e07b14 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1580,6 +1580,7 @@ module std_private_numeric_pstl_transform_reduce    [system] {
   export *
 }
 module std_private_numeric_reduce                   [system] { header "__numeric/reduce.h" }
+module std_private_numeric_saturation_arithmetic    [system] { header "__numeric/saturation_arithmetic.h" }
 module std_private_numeric_transform_exclusive_scan [system] { header "__numeric/transform_exclusive_scan.h" }
 module std_private_numeric_transform_inclusive_scan [system] { header "__numeric/transform_inclusive_scan.h" }
 module std_private_numeric_transform_reduce         [system] { header "__numeric/transform_reduce.h" }

diff  --git a/libcxx/include/numeric b/libcxx/include/numeric
index d09d0a81fcc03a..0fe7115f1c666e 100644
--- a/libcxx/include/numeric
+++ b/libcxx/include/numeric
@@ -140,6 +140,18 @@ template<class T>
 template<class T>
     constexpr T* midpoint(T* a, T* b);        // C++20
 
+// [numeric.sat], saturation arithmetic
+template<class T>
+constexpr T add_sat(T x, T y) noexcept;                     // freestanding, Since C++26
+template<class T>
+constexpr T sub_sat(T x, T y) noexcept;                     // freestanding, Since C++26
+template<class T>
+constexpr T mul_sat(T x, T y) noexcept;                     // freestanding, Since C++26
+template<class T>
+constexpr T div_sat(T x, T y) noexcept;                     // freestanding, Since C++26
+template<class T, class U>
+constexpr T saturate_cast(U x) noexcept;                    // freestanding, Since C++26
+
 }  // std
 
 */
@@ -160,6 +172,7 @@ template<class T>
 #include <__numeric/pstl_reduce.h>
 #include <__numeric/pstl_transform_reduce.h>
 #include <__numeric/reduce.h>
+#include <__numeric/saturation_arithmetic.h>
 #include <__numeric/transform_exclusive_scan.h>
 #include <__numeric/transform_inclusive_scan.h>
 #include <__numeric/transform_reduce.h>

diff  --git a/libcxx/include/version b/libcxx/include/version
index 7b2f487fe70205..9e26da8c1b2425 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -504,7 +504,7 @@ __cpp_lib_within_lifetime                               202306L <type_traits>
 // # define __cpp_lib_out_ptr                              202311L
 # define __cpp_lib_ratio                                202306L
 // # define __cpp_lib_rcu                                  202306L
-// # define __cpp_lib_saturation_arithmetic                202311L
+# define __cpp_lib_saturation_arithmetic                202311L
 // # define __cpp_lib_smart_ptr_owner_equality             202306L
 # define __cpp_lib_span_at                              202311L
 # define __cpp_lib_span_initializer_list                202311L

diff  --git a/libcxx/modules/std/numeric.inc b/libcxx/modules/std/numeric.inc
index d2b7688d4e5f10..3bc7b231681584 100644
--- a/libcxx/modules/std/numeric.inc
+++ b/libcxx/modules/std/numeric.inc
@@ -54,4 +54,14 @@ export namespace std {
 
   // [numeric.ops.midpoint], midpoint
   using std::midpoint;
+
+#if _LIBCPP_STD_VER >= 26
+  // [numeric.sat], saturation arithmetic
+  using std::add_sat;
+  using std::div_sat;
+  using std::mul_sat;
+  using std::saturate_cast;
+  using std::sub_sat;
+#endif
+
 } // namespace std

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp
index b510eefc69a5d3..d132b7c7b9c4f5 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/numeric.version.compile.pass.cpp
@@ -263,17 +263,11 @@
 #   endif
 # endif
 
-# if !defined(_LIBCPP_VERSION)
-#   ifndef __cpp_lib_saturation_arithmetic
-#     error "__cpp_lib_saturation_arithmetic should be defined in c++26"
-#   endif
-#   if __cpp_lib_saturation_arithmetic != 202311L
-#     error "__cpp_lib_saturation_arithmetic should have the value 202311L in c++26"
-#   endif
-# else // _LIBCPP_VERSION
-#   ifdef __cpp_lib_saturation_arithmetic
-#     error "__cpp_lib_saturation_arithmetic should not be defined because it is unimplemented in libc++!"
-#   endif
+# ifndef __cpp_lib_saturation_arithmetic
+#   error "__cpp_lib_saturation_arithmetic should be defined in c++26"
+# endif
+# if __cpp_lib_saturation_arithmetic != 202311L
+#   error "__cpp_lib_saturation_arithmetic should have the value 202311L in c++26"
 # endif
 
 #endif // TEST_STD_VER > 23

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
index fa188533446b47..c319940fe6e49c 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
@@ -7167,17 +7167,11 @@
 #   error "__cpp_lib_sample should have the value 201603L in c++26"
 # endif
 
-# if !defined(_LIBCPP_VERSION)
-#   ifndef __cpp_lib_saturation_arithmetic
-#     error "__cpp_lib_saturation_arithmetic should be defined in c++26"
-#   endif
-#   if __cpp_lib_saturation_arithmetic != 202311L
-#     error "__cpp_lib_saturation_arithmetic should have the value 202311L in c++26"
-#   endif
-# else // _LIBCPP_VERSION
-#   ifdef __cpp_lib_saturation_arithmetic
-#     error "__cpp_lib_saturation_arithmetic should not be defined because it is unimplemented in libc++!"
-#   endif
+# ifndef __cpp_lib_saturation_arithmetic
+#   error "__cpp_lib_saturation_arithmetic should be defined in c++26"
+# endif
+# if __cpp_lib_saturation_arithmetic != 202311L
+#   error "__cpp_lib_saturation_arithmetic should have the value 202311L in c++26"
 # endif
 
 # ifndef __cpp_lib_scoped_lock

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp
new file mode 100644
index 00000000000000..38b6d627869a04
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.compile.pass.cpp
@@ -0,0 +1,77 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <numeric>
+
+// template<class T>
+// constexpr T add_sat(T x, T y) noexcept;                     // freestanding
+
+#include <concepts>
+#include <numeric>
+
+#include "test_macros.h"
+
+template <typename T, typename U>
+concept CanDo = requires(T x, U y) {
+  { std::add_sat(x, y) } -> std::same_as<T>;
+};
+
+template <typename T, typename U>
+constexpr void test_constraint_success() {
+  static_assert(CanDo<T, T>);
+  static_assert(!CanDo<U, T>);
+  static_assert(!CanDo<T, U>);
+}
+
+template <typename T>
+constexpr void test_constraint_fail() {
+  using I = int;
+  static_assert(!CanDo<T, T>);
+  static_assert(!CanDo<I, T>);
+  static_assert(!CanDo<T, I>);
+}
+
+constexpr void test() {
+  // Contraint success - Signed
+  using SI = long long int;
+  test_constraint_success<signed char, SI>();
+  test_constraint_success<short int, SI>();
+  test_constraint_success<signed char, SI>();
+  test_constraint_success<short int, SI>();
+  test_constraint_success<int, SI>();
+  test_constraint_success<long int, SI>();
+  test_constraint_success<long long int, int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<__int128_t, SI>();
+#endif
+  // Contraint success - Unsigned
+  using UI = unsigned long long int;
+  test_constraint_success<unsigned char, UI>();
+  test_constraint_success<unsigned short int, UI>();
+  test_constraint_success<unsigned int, UI>();
+  test_constraint_success<unsigned long int, UI>();
+  test_constraint_success<unsigned long long int, unsigned int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<__uint128_t, UI>();
+#endif
+
+  // Contraint failure
+  test_constraint_fail<bool>();
+  test_constraint_fail<char>();
+#ifndef TEST_HAS_NO_WIDE_CHARACTERS
+  test_constraint_fail<wchar_t>();
+#endif
+  test_constraint_fail<char8_t>();
+  test_constraint_fail<char16_t>();
+  test_constraint_fail<char32_t>();
+  test_constraint_fail<float>();
+  test_constraint_fail<double>();
+  test_constraint_fail<long double>();
+}

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.pass.cpp
new file mode 100644
index 00000000000000..036bf53e36dcd4
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/add_sat.pass.cpp
@@ -0,0 +1,173 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <numeric>
+
+// template<class T>
+// constexpr T add_sat(T x, T y) noexcept;                     // freestanding
+
+#include <cassert>
+#include <concepts>
+#include <limits>
+#include <numeric>
+
+#include "test_macros.h"
+
+template <typename IntegerT>
+constexpr bool test_signed() {
+  constexpr auto minVal = std::numeric_limits<IntegerT>::min();
+  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
+
+  // TODO(LLVM-20) remove [[maybe_unused]]  since all supported compilers support "Placeholder variables with no name"
+  [[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::add_sat(minVal, maxVal);
+
+  static_assert(noexcept(std::add_sat(minVal, maxVal)));
+
+  // clang-format off
+
+  // Limit values (-1, 0, 1, min, max)
+
+  assert(std::add_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{-2});
+  assert(std::add_sat(IntegerT{-1}, IntegerT{ 0}) == IntegerT{-1});
+  assert(std::add_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{ 0});
+  assert(std::add_sat(IntegerT{-1},       minVal) == minVal); // saturated
+  assert(std::add_sat(IntegerT{-1},       maxVal) == IntegerT{-1} + maxVal);
+
+  assert(std::add_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{-1});
+  assert(std::add_sat(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});
+  assert(std::add_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 1});
+  assert(std::add_sat(IntegerT{ 0},       minVal) == minVal);
+  assert(std::add_sat(IntegerT{ 0},       maxVal) == maxVal);
+
+  assert(std::add_sat(IntegerT{ 1}, IntegerT{-1}) == IntegerT{ 0});
+  assert(std::add_sat(IntegerT{ 1}, IntegerT{ 0}) == IntegerT{ 1});
+  assert(std::add_sat(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 2});
+  assert(std::add_sat(IntegerT{ 1},       minVal) == IntegerT{ 1} + minVal);
+  assert(std::add_sat(IntegerT{ 1},       maxVal) == maxVal); // saturated
+
+  assert(std::add_sat(      minVal, IntegerT{-1}) == minVal); // saturated
+  assert(std::add_sat(      minVal, IntegerT{ 0}) == minVal);
+  assert(std::add_sat(      minVal, IntegerT{ 1}) == minVal + IntegerT{ 1});
+  assert(std::add_sat(      minVal,       minVal) == minVal); // saturated
+  assert(std::add_sat(      minVal,       maxVal) == IntegerT{-1});
+
+  assert(std::add_sat(      maxVal, IntegerT{-1}) == maxVal + IntegerT{-1});
+  assert(std::add_sat(      maxVal, IntegerT{ 0}) == maxVal);
+  assert(std::add_sat(      maxVal, IntegerT{ 1}) == maxVal); // saturated
+  assert(std::add_sat(      maxVal,       minVal) == IntegerT{-1});
+  assert(std::add_sat(      maxVal,       maxVal) == maxVal); // saturated
+
+  // No saturation (no limit values)
+
+  assert(std::add_sat(IntegerT{-27}, IntegerT{28})== IntegerT{ 1});
+  assert(std::add_sat(IntegerT{ 27}, IntegerT{28})== IntegerT{55});
+  {
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::add_sat(x, y) == maxVal);
+  }
+
+  // Saturation (no limit values)
+
+  {
+    constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{-27};
+    constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{-28};
+    assert(std::add_sat(x, y) == minVal); // saturated
+  }
+  {
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::add_sat(x, y) == maxVal); // saturated
+  }
+
+  // clang-format on
+
+  return true;
+}
+
+template <typename IntegerT>
+constexpr bool test_unsigned() {
+  constexpr auto minVal = std::numeric_limits<IntegerT>::min();
+  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
+
+  // TODO(LLVM-20) remove [[maybe_unused]]  since all supported compilers support "Placeholder variables with no name"
+  [[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::add_sat(minVal, maxVal);
+
+  static_assert(noexcept(std::add_sat(minVal, maxVal)));
+
+  // clang-format off
+
+  // Litmit values (0, 1, min, max)
+
+  assert(std::add_sat(IntegerT{0}, IntegerT{0}) == IntegerT{0});
+  assert(std::add_sat(IntegerT{0}, IntegerT{1}) == IntegerT{1});
+  assert(std::add_sat(IntegerT{0},      minVal) == IntegerT{0});
+  assert(std::add_sat(IntegerT{0},      maxVal) == maxVal);
+  assert(std::add_sat(IntegerT{1}, IntegerT{0}) == IntegerT{1});
+  assert(std::add_sat(IntegerT{1}, IntegerT{1}) == IntegerT{2});
+  assert(std::add_sat(IntegerT{1},      minVal) == IntegerT{1});
+  assert(std::add_sat(IntegerT{1},      maxVal) == maxVal); // saturated
+  assert(std::add_sat(     minVal, IntegerT{0}) == IntegerT{0});
+  assert(std::add_sat(     minVal, IntegerT{1}) == IntegerT{1});
+  assert(std::add_sat(     minVal,      minVal) == minVal);
+  assert(std::add_sat(     minVal,      maxVal) == maxVal);
+  assert(std::add_sat(     maxVal, IntegerT{0}) == maxVal);
+  assert(std::add_sat(     maxVal, IntegerT{1}) == maxVal); // saturated
+  assert(std::add_sat(     maxVal,      minVal) == maxVal);
+  assert(std::add_sat(     maxVal,      maxVal) == maxVal); // saturated
+
+  // No saturation (no limit values)
+
+  assert(std::add_sat(IntegerT{27}, IntegerT{28}) == IntegerT{55});
+
+  // Saturation (no limit values)
+
+  {
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::add_sat(     x,           y) == maxVal); // saturated
+    assert(std::add_sat(     x,      maxVal) == maxVal); // saturated
+    assert(std::add_sat(maxVal,           y) == maxVal); // saturated
+  }
+
+  // clang-format on
+
+  return true;
+}
+
+constexpr bool test() {
+  // Signed
+  test_signed<signed char>();
+  test_signed<short int>();
+  test_signed<int>();
+  test_signed<long int>();
+  test_signed<long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_signed<__int128_t>();
+#endif
+  // Unsigned
+  test_unsigned<unsigned char>();
+  test_unsigned<unsigned short int>();
+  test_unsigned<unsigned int>();
+  test_unsigned<unsigned long int>();
+  test_unsigned<unsigned long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_unsigned<__uint128_t>();
+#endif
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.assert.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.assert.pass.cpp
new file mode 100644
index 00000000000000..b1ef8345d51e61
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.assert.pass.cpp
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// REQUIRES: has-unix-headers
+// REQUIRES: libcpp-hardening-mode={{extensive|debug}}
+// XFAIL: availability-verbose_abort-missing
+
+// <numeric>
+
+// template<class T>
+// constexpr T div_sat(T x, T y) noexcept;                     // freestanding
+
+#include <cassert>
+#include <numeric>
+
+#include "check_assertion.h"
+#include "test_macros.h"
+
+template <typename IntegerT>
+void test_runtime_assertion() {
+  TEST_LIBCPP_ASSERT_FAILURE((void)std::div_sat(IntegerT{27}, IntegerT{0}), "Division by 0 is undefined");
+}
+
+bool test() {
+  // Signed
+  test_runtime_assertion<signed char>();
+  test_runtime_assertion<short int>();
+  test_runtime_assertion<int>();
+  test_runtime_assertion<long int>();
+  test_runtime_assertion<long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_runtime_assertion<__int128_t>();
+#endif
+  // Unsigned
+  test_runtime_assertion<unsigned char>();
+  test_runtime_assertion<unsigned short int>();
+  test_runtime_assertion<unsigned int>();
+  test_runtime_assertion<unsigned long int>();
+  test_runtime_assertion<unsigned long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_runtime_assertion<__uint128_t>();
+#endif
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.compile.pass.cpp
new file mode 100644
index 00000000000000..32d77f710bd701
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.compile.pass.cpp
@@ -0,0 +1,108 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <numeric>
+
+// template<class T>
+// constexpr T div_sat(T x, T y) noexcept;                     // freestanding
+
+#include <concepts>
+#include <numeric>
+#include <type_traits>
+
+#include "test_macros.h"
+
+// Constraints
+
+template <typename T, typename U>
+concept CanDo = requires(T x, U y) {
+  { std::div_sat(x, y) } -> std::same_as<T>;
+};
+
+template <typename T, typename U>
+constexpr void test_constraint_success() {
+  static_assert(CanDo<T, T>);
+  static_assert(!CanDo<U, T>);
+  static_assert(!CanDo<T, U>);
+}
+
+template <typename T>
+constexpr void test_constraint_fail() {
+  using I = int;
+  static_assert(!CanDo<T, T>);
+  static_assert(!CanDo<I, T>);
+  static_assert(!CanDo<T, I>);
+}
+
+constexpr void test() {
+  // Contraint success - Signed
+  using SI = long long int;
+  test_constraint_success<signed char, SI>();
+  test_constraint_success<short int, SI>();
+  test_constraint_success<signed char, SI>();
+  test_constraint_success<short int, SI>();
+  test_constraint_success<int, SI>();
+  test_constraint_success<long int, SI>();
+  test_constraint_success<long long int, int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<__int128_t, SI>();
+#endif
+  // Contraint success - Unsigned
+  using UI = unsigned long long int;
+  test_constraint_success<unsigned char, UI>();
+  test_constraint_success<unsigned short int, UI>();
+  test_constraint_success<unsigned int, UI>();
+  test_constraint_success<unsigned long int, UI>();
+  test_constraint_success<unsigned long long int, unsigned int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<__uint128_t, UI>();
+#endif
+
+  // Contraint failure
+  test_constraint_fail<bool>();
+  test_constraint_fail<char>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_fail<wchar_t>();
+#endif
+  test_constraint_fail<char8_t>();
+  test_constraint_fail<char16_t>();
+  test_constraint_fail<char32_t>();
+  test_constraint_fail<float>();
+  test_constraint_fail<double>();
+  test_constraint_fail<long double>();
+}
+
+//  A function call expression that violates the precondition in the Preconditions: element is not a core constant expression (7.7 [expr.const]).
+
+template <auto N>
+using QuotT = std::integral_constant<decltype(N), std::div_sat(N, N)>;
+
+template <auto N>
+QuotT<N> div_by_zero();
+
+template <auto N>
+concept CanDivByZero = requires { div_by_zero<N>(); };
+
+static_assert(!CanDivByZero<static_cast<signed char>(0)>);
+static_assert(!CanDivByZero<static_cast<short int>(0)>);
+static_assert(!CanDivByZero<0>);
+static_assert(!CanDivByZero<0L>);
+static_assert(!CanDivByZero<0LL>);
+#ifndef TEST_HAS_NO_INT128
+static_assert(!CanDivByZero<static_cast<__int128_t>(0)>);
+#endif
+static_assert(!CanDivByZero<static_cast<unsigned char>(0)>);
+static_assert(!CanDivByZero<static_cast<unsigned short int>(0)>);
+static_assert(!CanDivByZero<0U>);
+static_assert(!CanDivByZero<0UL>);
+static_assert(!CanDivByZero<0ULL>);
+#ifndef TEST_HAS_NO_INT128
+static_assert(!CanDivByZero<static_cast<__uint128_t>(0)>);
+#endif

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.pass.cpp
new file mode 100644
index 00000000000000..b1cace74d88288
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/div_sat.pass.cpp
@@ -0,0 +1,158 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// The test uses "Placeholder variables with no name"
+
+// <numeric>
+
+// template<class T>
+// constexpr T div_sat(T x, T y) noexcept;                     // freestanding
+
+#include <cassert>
+#include <concepts>
+#include <limits>
+#include <numeric>
+
+#include "test_macros.h"
+
+template <typename IntegerT>
+constexpr bool test_signed() {
+  constexpr auto minVal = std::numeric_limits<IntegerT>::min();
+  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
+
+  // TODO(LLVM-20) remove [[maybe_unused]] and `{}` scope since all supported compilers support "Placeholder variables with no name"
+  [[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::div_sat(minVal, maxVal);
+
+  static_assert(noexcept(std::div_sat(minVal, maxVal)));
+
+  // clang-format off
+
+  // Limit values (-1, 0, 1, min, max)
+
+  assert(std::div_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 1});
+  assert(std::div_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-1});
+  assert(std::div_sat(IntegerT{-1},       minVal) == IntegerT{ 0});
+  assert(std::div_sat(IntegerT{-1},       maxVal) == IntegerT{ 0});
+  assert(std::div_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 0});
+  assert(std::div_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 0});
+  assert(std::div_sat(IntegerT{ 0},       minVal) == IntegerT{ 0});
+  assert(std::div_sat(IntegerT{ 0},       maxVal) == IntegerT{ 0});
+  assert(std::div_sat(IntegerT{ 1}, IntegerT{-1}) == IntegerT{-1});
+  assert(std::div_sat(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 1});
+  assert(std::div_sat(IntegerT{ 1},       minVal) == IntegerT{ 0});
+  assert(std::div_sat(IntegerT{ 1},       maxVal) == IntegerT{ 0});
+  assert(std::div_sat(      minVal, IntegerT{ 1}) == minVal);
+  assert(std::div_sat(      minVal, IntegerT{-1}) == maxVal); // saturated
+  assert(std::div_sat(      minVal,       minVal) == IntegerT{ 1});
+  assert(std::div_sat(      minVal,       maxVal) == (minVal / maxVal));
+  assert(std::div_sat(      maxVal, IntegerT{-1}) == -maxVal);
+  assert(std::div_sat(      maxVal, IntegerT{ 1}) == maxVal);
+  assert(std::div_sat(      maxVal,       minVal) == IntegerT{ 0});
+  assert(std::div_sat(      maxVal,       maxVal) == IntegerT{ 1});
+
+  // No saturation (no limit values)
+
+  assert(std::div_sat(IntegerT{27}, IntegerT{28}) == IntegerT{0});
+  assert(std::div_sat(IntegerT{28}, IntegerT{27}) == IntegerT{1});
+  {
+    constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{-28};
+    constexpr IntegerT biggerVal = minVal / IntegerT{2} + IntegerT{-27};
+    assert(std::div_sat(lesserVal, biggerVal) == IntegerT{1});
+    assert(std::div_sat(biggerVal, lesserVal) == IntegerT{0});
+  }
+  {
+    constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{-27};
+    constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::div_sat(lesserVal, biggerVal) == IntegerT{-1});
+    assert(std::div_sat(biggerVal, lesserVal) == IntegerT{-1});
+  }
+  {
+    constexpr IntegerT lesserVal = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::div_sat(lesserVal, biggerVal) == IntegerT{0});
+    assert(std::div_sat(biggerVal, lesserVal) == IntegerT{1});
+  }
+
+  // clang-format on
+
+  return true;
+}
+
+template <typename IntegerT>
+constexpr bool test_unsigned() {
+  constexpr auto minVal = std::numeric_limits<IntegerT>::min();
+  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
+
+  // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name"
+  [[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::div_sat(minVal, maxVal);
+  static_assert(noexcept(std::div_sat(minVal, maxVal)));
+
+  // clang-format off
+
+  // No limit values (0, 1, min, max)
+
+  assert(std::div_sat(IntegerT{0}, IntegerT{1}) == IntegerT{0});
+  assert(std::div_sat(IntegerT{0},      maxVal) == IntegerT{0});
+
+  assert(std::div_sat(IntegerT{1}, IntegerT{1}) == IntegerT{1});
+  assert(std::div_sat(IntegerT{1},      maxVal) == IntegerT{0});
+
+  assert(std::div_sat(     minVal, IntegerT{1}) == minVal);
+  assert(std::div_sat(     minVal,      maxVal) == IntegerT{0});
+
+  assert(std::div_sat(     maxVal, IntegerT{1}) == maxVal);
+  assert(std::div_sat(     maxVal,      maxVal) == IntegerT{1});
+
+  // No saturation (no limit values)
+
+  assert(std::div_sat(IntegerT{27}, IntegerT{28}) == IntegerT{0});
+  assert(std::div_sat(IntegerT{28}, IntegerT{27}) == IntegerT{1});
+  {
+    constexpr IntegerT lesserVal = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::div_sat(lesserVal, biggerVal) == IntegerT{0});
+    assert(std::div_sat(biggerVal, lesserVal) == IntegerT{1});
+  }
+
+  // Unsigned integer division never overflows
+
+  // clang-format on
+
+  return true;
+}
+
+constexpr bool test() {
+  // Signed
+  test_signed<signed char>();
+  test_signed<short int>();
+  test_signed<int>();
+  test_signed<long int>();
+  test_signed<long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_signed<__int128_t>();
+#endif
+  // Unsigned
+  test_unsigned<unsigned char>();
+  test_unsigned<unsigned short int>();
+  test_unsigned<unsigned int>();
+  test_unsigned<unsigned long int>();
+  test_unsigned<unsigned long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_unsigned<__uint128_t>();
+#endif
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.compile.pass.cpp
new file mode 100644
index 00000000000000..f1762d25911995
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.compile.pass.cpp
@@ -0,0 +1,77 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <numeric>
+
+// template<class T>
+// constexpr T mul_sat(T x, T y) noexcept;                     // freestanding
+
+#include <concepts>
+#include <numeric>
+
+#include "test_macros.h"
+
+template <typename T, typename U>
+concept CanDo = requires(T x, U y) {
+  { std::mul_sat(x, y) } -> std::same_as<T>;
+};
+
+template <typename T, typename U>
+constexpr void test_constraint_success() {
+  static_assert(CanDo<T, T>);
+  static_assert(!CanDo<U, T>);
+  static_assert(!CanDo<T, U>);
+}
+
+template <typename T>
+constexpr void test_constraint_fail() {
+  using I = int;
+  static_assert(!CanDo<T, T>);
+  static_assert(!CanDo<I, T>);
+  static_assert(!CanDo<T, I>);
+}
+
+constexpr void test() {
+  // Contraint success - Signed
+  using SI = long long int;
+  test_constraint_success<signed char, SI>();
+  test_constraint_success<short int, SI>();
+  test_constraint_success<signed char, SI>();
+  test_constraint_success<short int, SI>();
+  test_constraint_success<int, SI>();
+  test_constraint_success<long int, SI>();
+  test_constraint_success<long long int, int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<__int128_t, SI>();
+#endif
+  // Contraint success - Unsigned
+  using UI = unsigned long long int;
+  test_constraint_success<unsigned char, UI>();
+  test_constraint_success<unsigned short int, UI>();
+  test_constraint_success<unsigned int, UI>();
+  test_constraint_success<unsigned long int, UI>();
+  test_constraint_success<unsigned long long int, unsigned int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<__uint128_t, UI>();
+#endif
+
+  // Contraint failure
+  test_constraint_fail<bool>();
+  test_constraint_fail<char>();
+#ifndef TEST_HAS_NO_WIDE_CHARACTERS
+  test_constraint_fail<wchar_t>();
+#endif
+  test_constraint_fail<char8_t>();
+  test_constraint_fail<char16_t>();
+  test_constraint_fail<char32_t>();
+  test_constraint_fail<float>();
+  test_constraint_fail<double>();
+  test_constraint_fail<long double>();
+}

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.pass.cpp
new file mode 100644
index 00000000000000..2c8eec57e12042
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/mul_sat.pass.cpp
@@ -0,0 +1,180 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// The test uses "Placeholder variables with no name"
+
+// <numeric>
+
+// template<class T>
+// constexpr T mul_sat(T x, T y) noexcept;                     // freestanding
+
+#include <cassert>
+#include <concepts>
+#include <limits>
+#include <numeric>
+
+#include "test_macros.h"
+
+template <typename IntegerT>
+constexpr bool test_signed() {
+  constexpr auto minVal = std::numeric_limits<IntegerT>::min();
+  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
+
+  // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name"
+  [[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::mul_sat(minVal, maxVal);
+
+  static_assert(noexcept(std::mul_sat(minVal, maxVal)));
+
+  // clang-format off
+
+  // Limit values (-1, 0, 1, min, max)
+
+  assert(std::mul_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 1});
+  assert(std::mul_sat(IntegerT{-1}, IntegerT{ 0}) == IntegerT{ 0});
+  assert(std::mul_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-1});
+  assert(std::mul_sat(IntegerT{-1},       minVal) == maxVal); // saturated
+  assert(std::mul_sat(IntegerT{-1},       maxVal) == -maxVal);
+
+  assert(std::mul_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 0});
+  assert(std::mul_sat(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});
+  assert(std::mul_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{ 0});
+  assert(std::mul_sat(IntegerT{ 0},       minVal) == IntegerT{ 0});
+  assert(std::mul_sat(IntegerT{ 0},       maxVal) == IntegerT{ 0});
+
+  assert(std::mul_sat(IntegerT{ 1}, IntegerT{-1}) == IntegerT{-1});
+  assert(std::mul_sat(IntegerT{ 1}, IntegerT{ 0}) == IntegerT{ 0});
+  assert(std::mul_sat(IntegerT{ 1}, IntegerT{ 1}) == IntegerT{ 1});
+  assert(std::mul_sat(IntegerT{ 1},       minVal) == minVal);
+  assert(std::mul_sat(IntegerT{ 1},       maxVal) == maxVal);
+
+  assert(std::mul_sat(      minVal, IntegerT{-1}) == maxVal); // saturated
+  assert(std::mul_sat(      minVal, IntegerT{ 0}) == IntegerT{ 0});
+  assert(std::mul_sat(      minVal, IntegerT{ 1}) == minVal);
+  assert(std::mul_sat(      minVal,       minVal) == maxVal); // saturated
+  assert(std::mul_sat(      minVal,       maxVal) == minVal); // saturated
+
+  assert(std::mul_sat(      maxVal, IntegerT{-1}) == -maxVal);
+  assert(std::mul_sat(      maxVal, IntegerT{ 0}) == IntegerT{ 0});
+  assert(std::mul_sat(      maxVal, IntegerT{ 1}) == maxVal); // saturated
+  assert(std::mul_sat(      maxVal,       minVal) == minVal); // saturated
+  assert(std::mul_sat(      maxVal,       maxVal) == maxVal); // saturated
+
+  // No saturation (no limit values)
+
+  assert(std::mul_sat(IntegerT{27}, IntegerT{ 2}) == IntegerT{54});
+  assert(std::mul_sat(IntegerT{ 2}, IntegerT{28}) == IntegerT{56});
+
+  // Saturation (no limit values)
+
+  {
+    constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{28};
+    assert(std::mul_sat(x, y) == maxVal); // saturated
+  }
+  {
+    constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::mul_sat(x, y) == minVal); // saturated
+  }
+  {
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{28};
+    assert(std::mul_sat(x, y) == minVal); // saturated
+  }
+  {
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::mul_sat(x, y) == maxVal); // saturated
+  }
+
+  // clang-format on
+
+  return true;
+}
+
+template <typename IntegerT>
+constexpr bool test_unsigned() {
+  constexpr auto minVal = std::numeric_limits<IntegerT>::min();
+  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
+
+  // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name"
+  [[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::mul_sat(minVal, maxVal);
+
+  static_assert(noexcept(std::mul_sat(minVal, maxVal)));
+
+  // clang-format off
+
+  // No saturation (0, 1)
+
+  assert(std::mul_sat(IntegerT{0}, IntegerT{0}) == IntegerT{0});
+  assert(std::mul_sat(IntegerT{0}, IntegerT{1}) == IntegerT{0});
+  assert(std::mul_sat(IntegerT{0},      minVal) == IntegerT{0});
+  assert(std::mul_sat(IntegerT{0},      maxVal) == IntegerT{0});
+
+  assert(std::mul_sat(IntegerT{1}, IntegerT{0}) == IntegerT{0});
+  assert(std::mul_sat(IntegerT{1}, IntegerT{1}) == IntegerT{1});
+  assert(std::mul_sat(IntegerT{1},      minVal) == minVal);
+  assert(std::mul_sat(IntegerT{1},      maxVal) == maxVal);
+
+  assert(std::mul_sat(     minVal, IntegerT{0}) == IntegerT{0});
+  assert(std::mul_sat(     minVal, IntegerT{1}) == minVal);
+  assert(std::mul_sat(     minVal,      maxVal) == minVal);
+  assert(std::mul_sat(     minVal,      maxVal) == minVal);
+
+  assert(std::mul_sat(     maxVal, IntegerT{0}) == IntegerT{0});
+  assert(std::mul_sat(     maxVal, IntegerT{1}) == maxVal);
+  assert(std::mul_sat(     maxVal,      minVal) == IntegerT{0});
+  assert(std::mul_sat(     maxVal,      maxVal) == maxVal); // saturated
+
+  // No saturation (no limit values)
+
+  assert(std::mul_sat(IntegerT{28}, IntegerT{2}) == IntegerT{56});
+
+  // Saturation (no limit values
+
+  {
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::mul_sat(x, y) == maxVal); // saturated
+  }
+
+  // clang-format on
+
+  return true;
+}
+
+constexpr bool test() {
+  // Signed
+  test_signed<signed char>();
+  test_signed<short int>();
+  test_signed<int>();
+  test_signed<long int>();
+  test_signed<long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_signed<__int128_t>();
+#endif
+  // Unsigned
+  test_unsigned<unsigned char>();
+  test_unsigned<unsigned short int>();
+  test_unsigned<unsigned int>();
+  test_unsigned<unsigned long int>();
+  test_unsigned<unsigned long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_unsigned<__uint128_t>();
+#endif
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.compile.pass.cpp
new file mode 100644
index 00000000000000..9b035c1e3c4e5e
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.compile.pass.cpp
@@ -0,0 +1,79 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <numeric>
+
+// template<class R, class T>
+//   constexpr R saturate_cast(T x) noexcept;                    // freestanding
+
+#include <concepts>
+#include <numeric>
+
+#include "test_macros.h"
+
+template <typename R, typename T>
+concept CanDo = requires(T x) {
+  { std::saturate_cast<R>(x) } -> std::same_as<R>;
+};
+
+template <typename R, typename T>
+constexpr void test_constraint_success() {
+  static_assert(CanDo<R, T>);
+  static_assert(CanDo<T, T>);
+  static_assert(CanDo<T, R>);
+}
+
+template <typename T>
+constexpr void test_constraint_fail() {
+  using I = int;
+  using R = T;
+  static_assert(!CanDo<R, T>);
+  static_assert(!CanDo<T, R>);
+  static_assert(!CanDo<I, T>);
+  static_assert(!CanDo<T, I>);
+}
+
+constexpr void test() {
+  // Contraint success - Signed
+  using SI = long long int;
+  test_constraint_success<SI, signed char>();
+  test_constraint_success<SI, short int>();
+  test_constraint_success<SI, signed char>();
+  test_constraint_success<SI, short int>();
+  test_constraint_success<SI, int>();
+  test_constraint_success<SI, long int>();
+  test_constraint_success<int, long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<__int128_t, SI>();
+#endif
+  // Contraint success - Unsigned
+  using UI = unsigned long long int;
+  test_constraint_success<UI, unsigned char>();
+  test_constraint_success<UI, unsigned short int>();
+  test_constraint_success<UI, unsigned int>();
+  test_constraint_success<UI, unsigned long int>();
+  test_constraint_success<unsigned int, unsigned long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<UI, __uint128_t>();
+#endif
+
+  // Contraint failure
+  test_constraint_fail<bool>();
+  test_constraint_fail<char>();
+#ifndef TEST_HAS_NO_WIDE_CHARACTERS
+  test_constraint_fail<wchar_t>();
+#endif
+  test_constraint_fail<char8_t>();
+  test_constraint_fail<char16_t>();
+  test_constraint_fail<char32_t>();
+  test_constraint_fail<float>();
+  test_constraint_fail<double>();
+  test_constraint_fail<long double>();
+}

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp
new file mode 100644
index 00000000000000..c06a9ed2d5cb42
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp
@@ -0,0 +1,397 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <numeric>
+
+// template<class R, class T>
+//   constexpr R saturate_cast(T x) noexcept;                     // freestanding
+
+#include <cassert>
+#include <climits>
+#include <concepts>
+#include <limits>
+#include <numeric>
+
+#include "test_macros.h"
+#include <print>
+
+// Smaller to larger
+static_assert(noexcept(std::saturate_cast<signed int>(std::numeric_limits<signed char>::max())));
+static_assert(noexcept(std::saturate_cast<signed int>(std::numeric_limits<unsigned char>::max())));
+
+static_assert(noexcept(std::saturate_cast<unsigned int>(std::numeric_limits<signed char>::max())));
+static_assert(noexcept(std::saturate_cast<unsigned int>(std::numeric_limits<unsigned char>::max())));
+
+// Same type
+static_assert(noexcept(std::saturate_cast<signed long int>(std::numeric_limits<signed long int>::max())));
+static_assert(noexcept(std::saturate_cast<unsigned long int>(std::numeric_limits<unsigned long int>::max())));
+
+// Larger to smaller
+static_assert(noexcept(std::saturate_cast<signed char>(std::numeric_limits<signed int>::max())));
+static_assert(noexcept(std::saturate_cast<signed char>(std::numeric_limits<unsigned int>::max())));
+
+static_assert(noexcept(std::saturate_cast<unsigned char>(std::numeric_limits<signed int>::max())));
+static_assert(noexcept(std::saturate_cast<unsigned char>(std::numeric_limits<unsigned int>::max())));
+
+// Tests
+
+constexpr bool test() {
+  // clang-format off
+
+#ifndef TEST_HAS_NO_INT128
+  using SIntT = __int128_t;
+  using UIntT = __uint128_t;
+#else
+  using SIntT = long long int;
+  using UIntT = unsigned long long int;
+#endif
+
+  // Constants the values of which depend on the context (platform)
+
+  constexpr auto sBigMin = std::numeric_limits<SIntT>::min();
+  constexpr auto sZero   = SIntT{0};
+  constexpr auto sBigMax = std::numeric_limits<SIntT>::max();
+
+  constexpr auto uZero   = UIntT{0};
+  constexpr auto uBigMax = std::numeric_limits<UIntT>::max();
+
+  // Constants to avoid casting in place
+
+  constexpr auto O_C  = static_cast<signed char>(0);
+  constexpr auto O_UC = static_cast<unsigned char>(0);
+
+  constexpr auto O_S  = static_cast<signed short int>(0);
+  constexpr auto O_US = static_cast<unsigned short int>(0);
+
+  // signed char
+
+  // TODO(LLVM-20) remove [[maybe_unused]] and `{}` scope since all supported compilers support "Placeholder variables with no name",
+  // here and below...
+  { [[maybe_unused]] std::same_as<signed char> decltype(auto) _ = std::saturate_cast<signed char>(SCHAR_MAX); }
+  assert(std::saturate_cast<signed char>(SCHAR_MIN)  == SCHAR_MIN);
+  assert(std::saturate_cast<signed char>(      O_C)  ==       O_C);
+  assert(std::saturate_cast<signed char>(SCHAR_MAX)  == SCHAR_MAX);
+
+  { [[maybe_unused]] std::same_as<signed char> decltype(auto) _ = std::saturate_cast<signed char>(UCHAR_MAX); }
+  assert(std::saturate_cast<signed char>(     O_UC)  ==       O_C);
+  assert(std::saturate_cast<signed char>(UCHAR_MAX)  == SCHAR_MAX);
+
+  { [[maybe_unused]] std::same_as<signed char> decltype(auto) _ = std::saturate_cast<signed char>(sBigMax); }
+  assert(std::saturate_cast<signed char>(sBigMin)    == SCHAR_MIN); // saturated
+  assert(std::saturate_cast<signed char>(  sZero)    ==       O_C);
+  assert(std::saturate_cast<signed char>(sBigMax)    == SCHAR_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<signed char> decltype(auto) _ = std::saturate_cast<signed char>(uBigMax); }
+  assert(std::saturate_cast<signed char>(  uZero)    ==       O_C);
+  assert(std::saturate_cast<signed char>(uBigMax)    == SCHAR_MAX); // saturated
+
+  // short
+
+  { [[maybe_unused]] std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(SCHAR_MAX); }
+  assert(std::saturate_cast<signed short int>(SCHAR_MIN) == static_cast<signed short int>(SCHAR_MIN));
+  assert(std::saturate_cast<signed short int>(      O_C) == O_S);
+  assert(std::saturate_cast<signed short int>(SCHAR_MAX) == static_cast<signed short int>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(UCHAR_MAX); }
+  assert(std::saturate_cast<signed short int>(     O_UC) == O_S);
+  assert(std::saturate_cast<signed short int>(UCHAR_MAX) == static_cast<signed short int>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(SHRT_MAX); }
+  assert(std::saturate_cast<signed short int>( SHRT_MIN) == SHRT_MIN);
+  assert(std::saturate_cast<signed short int>(      O_S) == O_S);
+  assert(std::saturate_cast<signed short int>( SHRT_MAX) == SHRT_MAX);
+
+  { [[maybe_unused]] std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(USHRT_MAX); }
+  assert(std::saturate_cast<signed short int>(     O_US) == O_S);
+  assert(std::saturate_cast<signed short int>(USHRT_MAX) == SHRT_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(sBigMax); }
+  assert(std::saturate_cast<signed short int>( sBigMin)   == SHRT_MIN); // saturated
+  assert(std::saturate_cast<signed short int>(   sZero)   == O_S);
+  assert(std::saturate_cast<signed short int>( sBigMax)   == SHRT_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(uBigMax); }
+  assert(std::saturate_cast<signed short int>(   uZero)   == O_S);
+  assert(std::saturate_cast<signed short int>( uBigMax)   == SHRT_MAX); // saturated
+
+  // int
+
+  { [[maybe_unused]] std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(SCHAR_MAX); }
+  assert(std::saturate_cast<signed int>(SCHAR_MIN) == static_cast<signed int>(SCHAR_MIN));
+  assert(std::saturate_cast<signed int>(      O_C) == 0);
+  assert(std::saturate_cast<signed int>(SCHAR_MAX) == static_cast<signed int>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(UCHAR_MAX); }
+  assert(std::saturate_cast<signed int>(     O_UC) == 0);
+  assert(std::saturate_cast<signed int>(UCHAR_MAX) == static_cast<signed int>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(INT_MAX); }
+  assert(std::saturate_cast<signed int>(  INT_MIN) == INT_MIN);
+  assert(std::saturate_cast<signed int>(        0) == 0);
+  assert(std::saturate_cast<signed int>(  INT_MAX) == INT_MAX);
+
+  { [[maybe_unused]] std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(UINT_MAX); }
+  assert(std::saturate_cast<signed int>(       0)  == 0);
+  assert(std::saturate_cast<signed int>(UINT_MAX)  == INT_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(sBigMax); }
+  assert(std::saturate_cast<signed int>( sBigMin)  == INT_MIN); // saturated
+  assert(std::saturate_cast<signed int>(   sZero)  == 0);
+  assert(std::saturate_cast<signed int>( sBigMax)  == INT_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<signed int> decltype(auto) _ = std::saturate_cast<signed int>(uBigMax); }
+  assert(std::saturate_cast<signed int>( uZero)    == 0);
+  assert(std::saturate_cast<signed int>( uBigMax)  == INT_MAX); // saturated
+
+  // long
+
+  { [[maybe_unused]] std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(SCHAR_MAX); }
+  assert(std::saturate_cast<signed long int>(SCHAR_MIN) == static_cast<signed long int>(SCHAR_MIN));
+  assert(std::saturate_cast<signed long int>(      O_C) == 0L);
+  assert(std::saturate_cast<signed long int>(SCHAR_MAX) == static_cast<signed long int>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(UCHAR_MAX); }
+  assert(std::saturate_cast<signed long int>(     O_UC) == 0L);
+  assert(std::saturate_cast<signed long int>(UCHAR_MAX) == static_cast<signed long int>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(LONG_MAX); }
+  assert(std::saturate_cast<signed long int>( LONG_MIN) == LONG_MIN);
+  assert(std::saturate_cast<signed long int>(       0L) == 0L);
+  assert(std::saturate_cast<signed long int>( LONG_MAX) == LONG_MAX);
+
+  { [[maybe_unused]] std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(ULONG_MAX); }
+  assert(std::saturate_cast<signed long int>(      0UL) == 0L);
+  assert(std::saturate_cast<signed long int>(ULONG_MAX) == LONG_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(sBigMax); }
+  assert(std::saturate_cast<signed long int>(  sBigMin) == LONG_MIN); // saturated
+  assert(std::saturate_cast<signed long int>(    sZero) == 0L);
+  assert(std::saturate_cast<signed long int>(  sBigMax) == LONG_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<signed long int> decltype(auto) _ = std::saturate_cast<signed long int>(uBigMax); }
+  assert(std::saturate_cast<signed long int>(    uZero) == 0L);
+  assert(std::saturate_cast<signed long int>(  uBigMax) == LONG_MAX); // saturated
+
+  // long long
+
+  { [[maybe_unused]] std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(SCHAR_MAX); }
+  assert(std::saturate_cast<signed long long int>(SCHAR_MIN) == static_cast<signed long long int>(SCHAR_MIN));
+  assert(std::saturate_cast<signed long long int>(      0LL) == 0LL);
+  assert(std::saturate_cast<signed long long int>(SCHAR_MAX) == static_cast<signed long long int>(SCHAR_MAX));
+
+  { [[maybe_unused]]   std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(UCHAR_MAX); }
+  assert(std::saturate_cast<signed long long int>(     O_UC) == 0LL);
+  assert(std::saturate_cast<signed long long int>(UCHAR_MAX) == static_cast<signed long long int>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(LLONG_MIN); }
+  assert(std::saturate_cast<signed long long int>(LLONG_MIN) == LLONG_MIN);
+  assert(std::saturate_cast<signed long long int>(      0LL) == 0LL);
+  assert(std::saturate_cast<signed long long int>(LLONG_MAX) == LLONG_MAX);
+
+  { [[maybe_unused]] std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(ULLONG_MAX); }
+  assert(std::saturate_cast<signed long long int>(      0ULL) == 0LL);
+  assert(std::saturate_cast<signed long long int>(ULLONG_MAX) == LLONG_MAX); // saturated
+
+#ifndef TEST_HAS_NO_INT128
+  { [[maybe_unused]] std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(sBigMax); }
+  assert(std::saturate_cast<signed long long int>(   sBigMin) == LLONG_MIN); // (128-bit) saturated
+  assert(std::saturate_cast<signed long long int>(     sZero) == 0LL);
+  assert(std::saturate_cast<signed long long int>(   sBigMax) == LLONG_MAX); // (128-bit) saturated
+
+  { [[maybe_unused]] std::same_as<signed long long int> decltype(auto) _ = std::saturate_cast<signed long long int>(uBigMax); }
+  assert(std::saturate_cast<signed long long int>(     uZero) == 0LL);
+  assert(std::saturate_cast<signed long long int>(   uBigMax) == LLONG_MAX); // (128-bit) saturated
+
+  { [[maybe_unused]] std::same_as<__int128_t> decltype(auto) _ = std::saturate_cast<__int128_t>(SCHAR_MAX); }
+  assert(std::saturate_cast<__int128_t>(SCHAR_MIN) == static_cast<__int128_t>(SCHAR_MIN));
+  assert(std::saturate_cast<__int128_t>(      O_C) == sZero);
+  assert(std::saturate_cast<__int128_t>(SCHAR_MAX) == static_cast<__int128_t>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<__int128_t> decltype(auto) _ = std::saturate_cast<__int128_t>(UCHAR_MAX); }
+  assert(std::saturate_cast<__int128_t>(     O_UC) == sZero);
+  assert(std::saturate_cast<__int128_t>(UCHAR_MAX) == static_cast<__int128_t>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<__int128_t> decltype(auto) _ = std::saturate_cast<__int128_t>(sBigMax); }
+  assert(std::saturate_cast<__int128_t>(  sBigMin) == sBigMin);
+  assert(std::saturate_cast<__int128_t>(    sZero) == sZero);
+  assert(std::saturate_cast<__int128_t>(  sBigMax) == sBigMax);
+
+  { [[maybe_unused]] std::same_as<__int128_t> decltype(auto) _ = std::saturate_cast<__int128_t>(uBigMax); }
+  assert(std::saturate_cast<__int128_t>(    uZero) == sZero);
+  assert(std::saturate_cast<__int128_t>(  uBigMax) == sBigMax); // saturated
+#endif
+
+  // unsigned char
+
+  { [[maybe_unused]] std::same_as<unsigned char> decltype(auto) _ = std::saturate_cast<unsigned char>(SCHAR_MAX); }
+  assert(std::saturate_cast<unsigned char>(SCHAR_MIN) == O_UC);
+  assert(std::saturate_cast<unsigned char>(      O_C) == O_UC);
+  assert(std::saturate_cast<unsigned char>(SCHAR_MAX) == static_cast<unsigned char>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned char> decltype(auto) _ = std::saturate_cast<unsigned char>(UCHAR_MAX); }
+  assert(std::saturate_cast<unsigned char>(     O_UC) == O_UC);
+  assert(std::saturate_cast<unsigned char>(UCHAR_MAX) == UCHAR_MAX);
+
+  { [[maybe_unused]] std::same_as<unsigned char> decltype(auto) _ = std::saturate_cast<unsigned char>(sBigMax); }
+  assert(std::saturate_cast<unsigned char>(  sBigMin) == O_UC);      // saturated
+  assert(std::saturate_cast<unsigned char>(    sZero) == O_UC);
+  assert(std::saturate_cast<unsigned char>(  sBigMax) == UCHAR_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<unsigned char> decltype(auto) _ = std::saturate_cast<unsigned char>(uBigMax); }
+  assert(std::saturate_cast<unsigned char>(    uZero) == O_UC);
+  assert(std::saturate_cast<unsigned char>(  uBigMax) == UCHAR_MAX); // saturated
+
+  // unsigned short
+
+  { [[maybe_unused]] std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(SCHAR_MAX); }
+  assert(std::saturate_cast<unsigned short int>(SCHAR_MIN) == O_US);
+  assert(std::saturate_cast<unsigned short int>(      O_C) == O_US);
+  assert(std::saturate_cast<unsigned short int>(SCHAR_MAX) == static_cast<unsigned short int>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(UCHAR_MAX); }
+  assert(std::saturate_cast<unsigned short int>(     O_UC) == O_US);
+  assert(std::saturate_cast<unsigned short int>(UCHAR_MAX) == static_cast<unsigned short int>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(SCHAR_MIN); }
+  assert(std::saturate_cast<unsigned short int>( SHRT_MIN) == O_US);
+  assert(std::saturate_cast<unsigned short int>(      O_S) == O_US);
+  assert(std::saturate_cast<unsigned short int>( SHRT_MAX) == static_cast<unsigned short int>(SHRT_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(UCHAR_MAX); }
+  assert(std::saturate_cast<unsigned short int>(     O_US) == O_US);
+  assert(std::saturate_cast<unsigned short int>(USHRT_MAX) == USHRT_MAX);
+
+  { [[maybe_unused]] std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(sBigMax); }
+  assert(std::saturate_cast<unsigned short int>(  sBigMin) == O_US);      // saturated
+  assert(std::saturate_cast<unsigned short int>(    sZero) == O_US);
+  assert(std::saturate_cast<unsigned short int>(  sBigMax) == USHRT_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<unsigned short int> decltype(auto) _ = std::saturate_cast<unsigned short int>(uBigMax); }
+  assert(std::saturate_cast<unsigned short int>(    uZero) == O_US);
+  assert(std::saturate_cast<unsigned short int>(  uBigMax) == USHRT_MAX); // saturated
+
+  // unsigned int
+
+  { [[maybe_unused]] std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(SCHAR_MAX); }
+  assert(std::saturate_cast<unsigned int>(SCHAR_MIN) == O_US);
+  assert(std::saturate_cast<unsigned int>(     O_UC) == 0U);
+  assert(std::saturate_cast<unsigned int>(SCHAR_MAX) == static_cast<unsigned int>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(UCHAR_MAX); }
+  assert(std::saturate_cast<unsigned int>(     O_UC) == 0U);
+  assert(std::saturate_cast<unsigned int>(UCHAR_MAX) == static_cast<unsigned int>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(INT_MAX); }
+  assert(std::saturate_cast<unsigned int>(  INT_MIN) == 0U);
+  assert(std::saturate_cast<unsigned int>(        0) == 0U);
+  assert(std::saturate_cast<unsigned int>(  INT_MAX) == static_cast<unsigned int>(INT_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(UINT_MAX); }
+  assert(std::saturate_cast<unsigned int>(       0U) == 0U);
+  assert(std::saturate_cast<unsigned int>( UINT_MAX) == UINT_MAX);
+
+  { [[maybe_unused]] std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(sBigMax); }
+  assert(std::saturate_cast<unsigned int>(  sBigMin) == 0U);       // saturated
+  assert(std::saturate_cast<unsigned int>(    sZero) == 0U);
+  assert(std::saturate_cast<unsigned int>(  sBigMax) == UINT_MAX); // saturated
+  
+  { [[maybe_unused]] std::same_as<unsigned int> decltype(auto) _ = std::saturate_cast<unsigned int>(uBigMax); }
+  assert(std::saturate_cast<unsigned int>(    uZero) == 0U);
+  assert(std::saturate_cast<unsigned int>(  uBigMax) == UINT_MAX);  // saturated
+
+  // unsigned long
+
+  { [[maybe_unused]] std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(SCHAR_MAX); }
+  assert(std::saturate_cast<unsigned long int>(SCHAR_MIN) == 0UL);
+  assert(std::saturate_cast<unsigned long int>(      O_C) == 0UL);
+  assert(std::saturate_cast<unsigned long int>(SCHAR_MAX) == static_cast<unsigned long int>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(UCHAR_MAX); }
+  assert(std::saturate_cast<unsigned long int>(     O_UC) == 0UL);
+  assert(std::saturate_cast<unsigned long int>(UCHAR_MAX) == static_cast<unsigned long int>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(LONG_MAX); }
+  assert(std::saturate_cast<unsigned long int>( LONG_MIN) == 0UL);
+  assert(std::saturate_cast<unsigned long int>(       0L) == 0UL);
+  assert(std::saturate_cast<unsigned long int>( LONG_MAX) == static_cast<unsigned long int>(LONG_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(ULONG_MAX); }
+  assert(std::saturate_cast<unsigned long int>(      0UL) == 0UL);
+  assert(std::saturate_cast<unsigned long int>(ULONG_MAX) == ULONG_MAX);
+
+  { [[maybe_unused]] std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(sBigMax); }
+  assert(std::saturate_cast<unsigned long int>(  sBigMin) == 0UL);       // saturated
+  assert(std::saturate_cast<unsigned long int>(    sZero) == 0UL);
+  assert(std::saturate_cast<unsigned long int>(  sBigMax) == ULONG_MAX); // saturated
+
+  { [[maybe_unused]] std::same_as<unsigned long int> decltype(auto) _ = std::saturate_cast<unsigned long int>(uBigMax); }
+  assert(std::saturate_cast<unsigned long int>(    uZero) == 0UL);
+  assert(std::saturate_cast<unsigned long int>(  uBigMax) == ULONG_MAX); // saturated
+
+  // unsigned long long
+
+  { [[maybe_unused]] std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(SCHAR_MAX); }
+  assert(std::saturate_cast<unsigned long long int>( SCHAR_MIN) == 0ULL);
+  assert(std::saturate_cast<unsigned long long int>(       O_C) == 0ULL);
+  assert(std::saturate_cast<unsigned long long int>( SCHAR_MAX) == static_cast<unsigned long long int>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned long long  int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(UCHAR_MAX); }
+  assert(std::saturate_cast<unsigned long long int>(      O_UC) == 0ULL);
+  assert(std::saturate_cast<unsigned long long int>( UCHAR_MAX) == static_cast<unsigned long long int>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(LLONG_MAX); }
+  assert(std::saturate_cast<unsigned long long int>( LLONG_MIN) == 0ULL);
+  assert(std::saturate_cast<unsigned long long int>(       0LL) == 0ULL);
+  assert(std::saturate_cast<unsigned long long int>( LLONG_MAX) == static_cast<unsigned long long int>(LLONG_MAX));
+
+  { [[maybe_unused]] std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(ULLONG_MAX); }
+  assert(std::saturate_cast<unsigned long long int>(      0ULL) == 0ULL);
+  assert(std::saturate_cast<unsigned long long int>(ULLONG_MAX) == ULLONG_MAX);
+
+#ifndef TEST_HAS_NO_INT128
+  { [[maybe_unused]] std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(sBigMax); }
+  assert(std::saturate_cast<unsigned long long int>(   sBigMin) == 0ULL);       // (128-bit) saturated
+  assert(std::saturate_cast<unsigned long long int>(     sZero) == 0ULL);
+  assert(std::saturate_cast<unsigned long long int>(   sBigMax) == ULLONG_MAX); // (128-bit) saturated
+
+  { [[maybe_unused]] std::same_as<unsigned long long int> decltype(auto) _ = std::saturate_cast<unsigned long long int>(uBigMax); }
+  assert(std::saturate_cast<unsigned long long int>(     uZero) == 0ULL);
+  assert(std::saturate_cast<unsigned long long int>(   uBigMax) == ULLONG_MAX); // (128-bit) saturated
+
+  { [[maybe_unused]] std::same_as<__uint128_t> decltype(auto) _ = std::saturate_cast<__uint128_t>(SCHAR_MIN); }
+  assert(std::saturate_cast<__uint128_t>(SCHAR_MIN) == uZero);
+  assert(std::saturate_cast<__uint128_t>(      O_C) == uZero);
+  assert(std::saturate_cast<__uint128_t>(SCHAR_MAX) == static_cast<__uint128_t>(SCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<__uint128_t> decltype(auto) _ = std::saturate_cast<__uint128_t>(UCHAR_MAX); }
+  assert(std::saturate_cast<__uint128_t>(     O_UC) == uZero);
+  assert(std::saturate_cast<__uint128_t>(UCHAR_MAX) == static_cast<__uint128_t>(UCHAR_MAX));
+
+  { [[maybe_unused]] std::same_as<__uint128_t> decltype(auto) _ = std::saturate_cast<__uint128_t>(sBigMax); }
+  assert(std::saturate_cast<__uint128_t>(  sBigMin) == uZero); // saturated
+  assert(std::saturate_cast<__uint128_t>(    sZero) == uZero);
+  assert(std::saturate_cast<__uint128_t>(  sBigMax) == static_cast<__uint128_t>(sBigMax));
+
+  { [[maybe_unused]] std::same_as<__uint128_t> decltype(auto) _ = std::saturate_cast<__uint128_t>(uBigMax); }
+  assert(std::saturate_cast<__uint128_t>(    uZero) == uZero);
+  assert(std::saturate_cast<__uint128_t>(  uBigMax) == uBigMax);
+#endif
+
+  // clang-format on
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.compile.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.compile.pass.cpp
new file mode 100644
index 00000000000000..9234819d9ec241
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.compile.pass.cpp
@@ -0,0 +1,77 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <numeric>
+
+// template<class T>
+// constexpr T sub_sat(T x, T y) noexcept;                     // freestanding
+
+#include <concepts>
+#include <numeric>
+
+#include "test_macros.h"
+
+template <typename T, typename U>
+concept CanDo = requires(T x, U y) {
+  { std::sub_sat(x, y) } -> std::same_as<T>;
+};
+
+template <typename T, typename U>
+constexpr void test_constraint_success() {
+  static_assert(CanDo<T, T>);
+  static_assert(!CanDo<U, T>);
+  static_assert(!CanDo<T, U>);
+}
+
+template <typename T>
+constexpr void test_constraint_fail() {
+  using I = int;
+  static_assert(!CanDo<T, T>);
+  static_assert(!CanDo<I, T>);
+  static_assert(!CanDo<T, I>);
+}
+
+constexpr void test() {
+  // Contraint success - Signed
+  using SI = long long int;
+  test_constraint_success<signed char, SI>();
+  test_constraint_success<short int, SI>();
+  test_constraint_success<signed char, SI>();
+  test_constraint_success<short int, SI>();
+  test_constraint_success<int, SI>();
+  test_constraint_success<long int, SI>();
+  test_constraint_success<long long int, int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<__int128_t, SI>();
+#endif
+  // Contraint success - Unsigned
+  using UI = unsigned long long int;
+  test_constraint_success<unsigned char, UI>();
+  test_constraint_success<unsigned short int, UI>();
+  test_constraint_success<unsigned int, UI>();
+  test_constraint_success<unsigned long int, UI>();
+  test_constraint_success<unsigned long long int, unsigned int>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_success<__uint128_t, UI>();
+#endif
+
+  // Contraint failure
+  test_constraint_fail<bool>();
+  test_constraint_fail<char>();
+#ifndef TEST_HAS_NO_INT128
+  test_constraint_fail<wchar_t>();
+#endif
+  test_constraint_fail<char8_t>();
+  test_constraint_fail<char16_t>();
+  test_constraint_fail<char32_t>();
+  test_constraint_fail<float>();
+  test_constraint_fail<double>();
+  test_constraint_fail<long double>();
+}

diff  --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.pass.cpp
new file mode 100644
index 00000000000000..d7bdf2c0271acf
--- /dev/null
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.sat/sub_sat.pass.cpp
@@ -0,0 +1,162 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// The test uses "Placeholder variables with no name"
+
+// <numeric>
+
+// template<class T>
+// constexpr T sub_sat(T x, T y) noexcept;                     // freestanding
+
+#include <cassert>
+#include <concepts>
+#include <limits>
+#include <numeric>
+
+#include "test_macros.h"
+
+template <typename IntegerT>
+constexpr bool test_signed() {
+  constexpr auto minVal = std::numeric_limits<IntegerT>::min();
+  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
+
+  // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name"
+  [[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::sub_sat(minVal, maxVal);
+
+  static_assert(noexcept(std::sub_sat(minVal, maxVal)));
+
+  // clang-format off
+
+  // Limit values (-1, 0, 1, min, max)
+
+  assert(std::sub_sat(IntegerT{-1}, IntegerT{-1}) == IntegerT{ 0});
+  assert(std::sub_sat(IntegerT{-1}, IntegerT{ 0}) == IntegerT{-1});
+  assert(std::sub_sat(IntegerT{-1}, IntegerT{ 1}) == IntegerT{-2});
+  assert(std::sub_sat(IntegerT{-1},       minVal) == IntegerT{-1} - minVal);
+  assert(std::sub_sat(IntegerT{-1},       maxVal) == IntegerT{-1} - maxVal);
+
+  assert(std::sub_sat(IntegerT{ 0}, IntegerT{-1}) == IntegerT{ 1});
+  assert(std::sub_sat(IntegerT{ 0}, IntegerT{ 0}) == IntegerT{ 0});
+  assert(std::sub_sat(IntegerT{ 0}, IntegerT{ 1}) == IntegerT{-1});
+  assert(std::sub_sat(IntegerT{ 0},       minVal) == maxVal); // saturated
+  assert(std::sub_sat(IntegerT{ 0},       maxVal) == -maxVal);
+
+  assert(std::sub_sat(      minVal, IntegerT{-1}) == minVal - IntegerT{-1});
+  assert(std::sub_sat(      minVal, IntegerT{ 0}) == minVal);
+  assert(std::sub_sat(      minVal, IntegerT{ 1}) == minVal); // saturated
+  assert(std::sub_sat(      minVal,       minVal) == IntegerT{0});
+  assert(std::sub_sat(      minVal,       maxVal) == minVal); // saturated
+
+  assert(std::sub_sat(      maxVal, IntegerT{-1}) == maxVal); // saturated
+  assert(std::sub_sat(      maxVal, IntegerT{ 0}) == maxVal);
+  assert(std::sub_sat(      maxVal, IntegerT{ 1}) == maxVal - IntegerT{ 1});
+  assert(std::sub_sat(      maxVal,       minVal) == maxVal); // saturated
+  assert(std::sub_sat(      maxVal,       maxVal) == IntegerT{0});
+
+  // No saturation (no limit values)
+
+  assert(std::sub_sat(IntegerT{ 27}, IntegerT{-28}) ==  55);
+  assert(std::sub_sat(IntegerT{ 27}, IntegerT{ 28}) ==  -1);
+  assert(std::sub_sat(IntegerT{-27}, IntegerT{ 28}) == -55);
+  assert(std::sub_sat(IntegerT{-27}, IntegerT{-28}) ==   1);
+
+  // Saturation (no limit values)
+
+  {
+    constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::sub_sat(lesserVal, biggerVal) == minVal); // saturated
+  }
+  {
+    constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
+    constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};
+    assert(std::sub_sat(biggerVal, lesserVal) == maxVal); // saturated
+  }
+
+  // clang-format on
+
+  return true;
+}
+
+template <typename IntegerT>
+constexpr bool test_unsigned() {
+  constexpr auto minVal = std::numeric_limits<IntegerT>::min();
+  constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
+
+  // TODO(LLVM-20) remove [[maybe_unused]] since all supported compilers support "Placeholder variables with no name"
+  [[maybe_unused]] std::same_as<IntegerT> decltype(auto) _ = std::sub_sat(minVal, maxVal);
+
+  static_assert(noexcept(std::sub_sat(minVal, maxVal)));
+
+  // clang-format off
+
+  // Limit values (0, 1, min, max)
+
+  assert(std::sub_sat(IntegerT{0}, IntegerT{0}) == IntegerT{0});
+  assert(std::sub_sat(IntegerT{0}, IntegerT{1}) == minVal); // saturated
+  assert(std::sub_sat(IntegerT{0},      minVal) == minVal);
+  assert(std::sub_sat(IntegerT{0},      maxVal) == minVal); // saturated
+
+  assert(std::sub_sat(IntegerT{1}, IntegerT{0}) == IntegerT{1});
+  assert(std::sub_sat(IntegerT{1}, IntegerT{1}) == IntegerT{0});
+  assert(std::sub_sat(IntegerT{1},      minVal) == IntegerT{1});
+  assert(std::sub_sat(IntegerT{1},      maxVal) == minVal); // saturated
+
+  assert(std::sub_sat(     minVal, IntegerT{0}) == IntegerT{0});
+  assert(std::sub_sat(     minVal, IntegerT{1}) == minVal);
+  assert(std::sub_sat(     minVal,      maxVal) == minVal);
+  assert(std::sub_sat(     minVal,      maxVal) == minVal);
+
+  assert(std::sub_sat(     maxVal, IntegerT{0}) == maxVal);
+  assert(std::sub_sat(     maxVal, IntegerT{1}) == maxVal - IntegerT{1});
+  assert(std::sub_sat(     maxVal,      minVal) == maxVal);
+  assert(std::sub_sat(     maxVal,      maxVal) == IntegerT{0});
+
+  // Saturation (no limit values)
+
+  {
+    constexpr IntegerT lesserVal = minVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT biggerVal = maxVal / IntegerT{2} + IntegerT{28};
+    assert(std::sub_sat(lesserVal, biggerVal) == minVal); // saturated
+  }
+
+  // clang-format on
+
+  return true;
+}
+
+constexpr bool test() {
+  // Signed
+  test_signed<signed char>();
+  test_signed<short int>();
+  test_signed<int>();
+  test_signed<long int>();
+  test_signed<long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_signed<__int128_t>();
+#endif
+  // Unsigned
+  test_unsigned<unsigned char>();
+  test_unsigned<unsigned short int>();
+  test_unsigned<unsigned int>();
+  test_unsigned<unsigned long int>();
+  test_unsigned<unsigned long long int>();
+#ifndef TEST_HAS_NO_INT128
+  test_unsigned<__uint128_t>();
+#endif
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 667dd75278c8b2..065b70620cd170 100755
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -1011,10 +1011,7 @@ def add_version_header(tc):
         {
             "name": "__cpp_lib_saturation_arithmetic",
             "values": {"c++26": 202311},  # P0543R3 Saturation arithmetic
-            "headers": [
-                "numeric"  # TODO verify this entry since the paper was underspecified.
-            ],
-            "unimplemented": True,
+            "headers": ["numeric"],
         },
         {
             "name": "__cpp_lib_scoped_lock",


        


More information about the libcxx-commits mailing list