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

Mark de Wever via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 21 11:49:26 PST 2024


================
@@ -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 bellow...
----------------
mordante wrote:

```suggestion
  // here and below...
```

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


More information about the cfe-commits mailing list