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

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


================
@@ -0,0 +1,267 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 div_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
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{-1}, IntegerT{-1});
+    assert(quot == IntegerT{1});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{-1}, IntegerT{1});
+    assert(quot == IntegerT{-1});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{-1}, minVal);
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{-1}, maxVal);
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(maxVal, IntegerT{-1});
+    assert(quot == IntegerT{-1} * maxVal);
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{0}, IntegerT{-1});
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{0}, IntegerT{1});
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{0}, minVal);
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{1}, IntegerT{-1});
+    assert(quot == IntegerT{-1});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{1}, minVal);
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{1}, maxVal);
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(minVal, IntegerT{1});
+    assert(quot == minVal);
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(maxVal, IntegerT{1});
+    assert(quot == maxVal);
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{27}, IntegerT{28});
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{28}, IntegerT{27});
+    assert(quot == IntegerT{1});
+  }
+
+  {
+    // Large values
+    constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{-27};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
+
+    std::same_as<IntegerT> decltype(auto) sum = std::div_sat(x, y);
+    assert(sum == IntegerT{-1});
+  }
+
+  {
+    // Large values
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{28};
+    constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{-27};
+
+    std::same_as<IntegerT> decltype(auto) sum = std::div_sat(x, y);
+    assert(sum == IntegerT{-1});
+  }
+
+  {
+    // Large values
+    constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{-27};
+    constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{-28};
+
+    std::same_as<IntegerT> decltype(auto) sum = std::div_sat(x, y);
+    assert(sum == IntegerT{0});
+  }
+
+  {
+    // Large values
+    constexpr IntegerT x = minVal / IntegerT{2} + IntegerT{-28};
+    constexpr IntegerT y = minVal / IntegerT{2} + IntegerT{-27};
+
+    std::same_as<IntegerT> decltype(auto) sum = std::div_sat(x, y);
+    assert(sum == IntegerT{1});
+  }
+
+  {
+    // Large values
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
+
+    std::same_as<IntegerT> decltype(auto) sum = std::div_sat(x, y);
+    assert(sum == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(maxVal, minVal);
+    assert(quot == (maxVal / minVal));
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(minVal, maxVal);
+    assert(quot == (minVal / maxVal));
+  }
+
+  // Saturation - max only
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(minVal, IntegerT{-1});
+    assert(quot == maxVal);
+  }
+
+  return true;
+}
+
+template <typename IntegerT>
+constexpr bool test_unsigned() {
+  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
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{0}, IntegerT{1});
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{0}, maxVal);
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{1}, IntegerT{1});
+    assert(quot == IntegerT{1});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{1}, maxVal);
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{27}, IntegerT{28});
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(IntegerT{28}, IntegerT{27});
+    assert(quot == IntegerT{1});
+  }
+
+  {
+    // Large values
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{27};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{28};
+
+    std::same_as<IntegerT> decltype(auto) sum = std::div_sat(x, y);
+    assert(sum == IntegerT{0});
+  }
+
+  {
+    // Large values
+    constexpr IntegerT x = maxVal / IntegerT{2} + IntegerT{28};
+    constexpr IntegerT y = maxVal / IntegerT{2} + IntegerT{27};
+
+    std::same_as<IntegerT> decltype(auto) sum = std::div_sat(x, y);
+    assert(sum == IntegerT{1});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(minVal, maxVal);
+    assert(quot == IntegerT{0});
+  }
+
+  {
+    std::same_as<IntegerT> decltype(auto) quot = std::div_sat(maxVal, maxVal);
+    assert(quot == IntegerT{1});
+  }
+
+  // Unsigned integer devision never overflows
----------------
mordante wrote:

```suggestion
  // Unsigned integer division never overflows
```

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


More information about the cfe-commits mailing list