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

Mark de Wever llvmlistbot at llvm.org
Sun Jan 21 06:10:25 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();
----------------
mordante wrote:

This feels a bit odd. The `climits` header has nice macros for these values https://en.cppreference.com/w/c/types/limits.

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


More information about the Mlir-commits mailing list