[libcxx-commits] [libunwind] [flang] [mlir] [openmp] [clang-tools-extra] [lldb] [polly] [compiler-rt] [clang] [lld] [llvm] [libcxx] [libc] [libcxxabi] [libc++][numeric] P0543R3: Saturation arithmetic (PR #77967)
Mark de Wever via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jan 18 09:32:05 PST 2024
================
@@ -0,0 +1,281 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+template <typename IntegerT>
+constexpr bool test_signed() {
+ constexpr auto minVal = std::numeric_limits<IntegerT>::min();
+ constexpr auto maxVal = std::numeric_limits<IntegerT>::max();
+
+ static_assert(noexcept(std::div_sat(minVal, maxVal)));
+
+ // No saturation (-1, 0, 1)
+ {
+ std::same_as<IntegerT> decltype(auto) sum = std::add_sat(IntegerT{0}, IntegerT{0});
----------------
mordante wrote:
How about testing the result type once and then directly assert the result. Something like
```
std::same_as<IntegerT> decltype(auto) sum = std::add_sat(IntegerT{0}, IntegerT{0});
assert(sum == IntegerT{0});
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}, IntegerT{0}) == IntegerT{-1});
assert(std::add_sat(IntegerT{1}, IntegerT{-1}) == IntegerT{0});
assert(std::add_sat(IntegerT{1}, IntegerT{1}) == IntegerT{1});
...
```
Currently every test entry is 5 lines, which makes the test quite long. (I'm not against long tests in general, but here the test density is a bit low.)
https://github.com/llvm/llvm-project/pull/77967
More information about the libcxx-commits
mailing list