[clang] [lldb] [mlir] [clang-tools-extra] [openmp] [lld] [libc] [libcxxabi] [flang] [libunwind] [polly] [compiler-rt] [llvm] [libcxx] [libc++][numeric] P0543R3: Saturation arithmetic (PR #77967)

Hristo Hristov via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 21 09:36:20 PST 2024


================
@@ -0,0 +1,458 @@
+//===----------------------------------------------------------------------===//
+//
+// 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"
+// UNSUPPORTED: clang-17
+// XFAIL: apple-clang
+
+// <numeric>
+
+// template<class R, class T>
+//   constexpr R saturate_cast(T x) noexcept;                     // freestanding
+
+#include <cassert>
+#include <concepts>
+#include <limits>
+#include <numeric>
+
+#include "test_macros.h"
+
+// 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())));
+
+template <typename IntegerT>
+constexpr auto zero() {
+  return IntegerT{0};
+}
+
+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: biggest numbers
+
+  constexpr auto big_sintMin  = std::numeric_limits<SIntT>::min();
+  constexpr auto big_sintZero =                zero<SIntT>();
+  constexpr auto big_sintMax  = std::numeric_limits<SIntT>::max();
----------------
H-G-Hristov wrote:

These are needed specifically as these values can vary but I used the C integer constants wherever applicable.

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


More information about the cfe-commits mailing list