[libc-commits] [flang] [clang-tools-extra] [compiler-rt] [libunwind] [openmp] [lldb] [llvm] [libc] [libcxx] [mlir] [clang] [polly] [libcxxabi] [lld] [libc++][numeric] P0543R3: Saturation arithmetic (PR #77967)
Mark de Wever via libc-commits
libc-commits at lists.llvm.org
Sun Jan 21 06:10:24 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();
+
+ constexpr auto big_uintMin = std::numeric_limits<UIntT>::min();
+ constexpr auto big_uintZero = zero<UIntT>();
+ constexpr auto big_uintMax = std::numeric_limits<UIntT>::max();
+
+ // Constants: numeric limits
+
+ constexpr auto std_scharMin = std::numeric_limits<signed char>::min();
+ constexpr auto std_scharZero = zero<signed char>();
+ constexpr auto std_scharMax = std::numeric_limits<signed char>::max();
+
+ constexpr auto std_ucharMin = std::numeric_limits<unsigned char>::min();
+ constexpr auto std_ucharZero = zero<unsigned char>();
+ constexpr auto std_ucharMax = std::numeric_limits<unsigned char>::max();
+
+ constexpr auto std_ssintMin = std::numeric_limits<signed short int>::min();
+ constexpr auto std_ssintZero = zero<signed short int>();
+ constexpr auto std_ssintMax = std::numeric_limits<signed short int>::max();
+
+ constexpr auto std_usintMin = std::numeric_limits<unsigned short int>::min();
+ constexpr auto std_usintZero = zero<unsigned short int>();
+ constexpr auto std_usintMax = std::numeric_limits<unsigned short int>::max();
+
+ constexpr auto std_sintMin = std::numeric_limits<signed int>::min();
+ constexpr auto std_sintZero = zero<signed int>();
+ constexpr auto std_sintMax = std::numeric_limits<signed int>::max();
+
+ constexpr auto std_uintMin = std::numeric_limits<unsigned int>::min();
+ constexpr auto std_uintZero = zero<unsigned int>();
+ constexpr auto std_uintMax = std::numeric_limits<unsigned int>::max();
+
+ constexpr auto std_slMin = std::numeric_limits<signed long int>::min();
+ constexpr auto std_slZero = zero<signed long int>();
+ constexpr auto std_slMax = std::numeric_limits<signed long int>::max();
+
+ constexpr auto std_ulMin = std::numeric_limits<unsigned long int>::min();
+ constexpr auto std_ulZero = zero<unsigned long int>();
+ constexpr auto std_ulMax = std::numeric_limits<unsigned long int>::max();
+
+ constexpr auto std_sllMin = std::numeric_limits<signed long long int>::min();
+ constexpr auto std_sllZero = zero<signed long long int>();
+ constexpr auto std_sllMax = std::numeric_limits<signed long long int>::max();
+
+ constexpr auto std_ullMin = std::numeric_limits<unsigned long long int>::min();
+ constexpr auto std_ullZero = zero<unsigned long long int>();
+ constexpr auto std_ullMax = std::numeric_limits<unsigned long long int>::max();
+
+ // signed char
+
+ assert(std::saturate_cast<signed char>(std_scharMin) == std_scharMin);
+ assert(std::saturate_cast<signed char>(std_scharZero) == std_scharZero);
+ assert(std::saturate_cast<signed char>(std_scharMax) == std_scharMax);
+
+ assert(std::saturate_cast<signed char>(std_ucharMin) == std_scharZero);
+ assert(std::saturate_cast<signed char>(std_ucharZero) == std_scharZero);
+ assert(std::saturate_cast<signed char>(std_ucharMax) == std_scharMax);
+
+ assert(std::saturate_cast<signed char>(big_sintMin) == std_scharMin); // saturated
+ assert(std::saturate_cast<signed char>(big_sintZero) == std_scharZero);
+ assert(std::saturate_cast<signed char>(big_sintMax) == std_scharMax); // saturated
+
+ assert(std::saturate_cast<signed char>(big_uintMin) == std_scharZero);
+ assert(std::saturate_cast<signed char>(big_uintZero) == std_scharZero);
+ assert(std::saturate_cast<signed char>(big_uintMax) == std_scharMax); // saturated
+
+ // short
+
+ std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(std_scharMin);
+ assert(std::saturate_cast<signed short int>(std_scharMin) == static_cast<signed short int>(std_scharMin));
+ assert(std::saturate_cast<signed short int>(std_scharZero) == std_ssintZero);
+ assert(std::saturate_cast<signed short int>(std_scharMax) == static_cast<signed short int>(std_scharMax));
+
+ std::same_as<signed short int> decltype(auto) _ = std::saturate_cast<signed short int>(std_ucharMin);
+ assert(std::saturate_cast<signed short int>(std_ucharMin) == std_ssintZero);
+ assert(std::saturate_cast<signed short int>(std_ucharZero) == std_ssintZero);
+ assert(std::saturate_cast<signed short int>(std_ucharMax) == static_cast<signed short int>(std_ucharMax));
----------------
mordante wrote:
```suggestion
assert(std::saturate_cast<signed short int>(UCHAR_MAX) == SHRT_MIN);
assert(std::saturate_cast<signed short int>((unsigned char)(0)) == 0); // or a static cast
assert(std::saturate_cast<signed short int>(UCHAR_MAX) == static_cast<signed short int>(UCHAR_MAX));
```
for the 0 values you can use, 0, 0u, 0l, 0ul, etc.
This removes a lot of boilerplate and uses well known names instead of your own less familiar names.
https://github.com/llvm/llvm-project/pull/77967
More information about the libc-commits
mailing list