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

Mark de Wever via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 18 09:32:05 PST 2024


================
@@ -0,0 +1,157 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <concepts>
+#include <limits>
+#include <numeric>
+
+template <typename IntegerResultT, typename IntegerT>
----------------
mordante wrote:

I'm not fond of this test. IMO it's a bit too smart and it makes it hard to understand what is tested.
What I often do for these kind of tests is something like
```
assert(std::saturate_cast<signed char, int>(-255) == -128);
assert(std::saturate_cast<signed char, int>(-128) == -128);
assert(std::saturate_cast<signed char, int>(0) == 0);
assert(std::saturate_cast<signed char, int>(127) == 127);
assert(std::saturate_cast<signed char, int>(255) == 127);
```

and similar for other conversions. I think that is easier to follow. (Note I like the add, sub, mul, and div test.)


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


More information about the llvm-commits mailing list