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

Hristo Hristov via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 13 23:52:37 PST 2024


================
@@ -0,0 +1,119 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H
+#define _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H
+
+#include <__concepts/arithmetic.h>
+#include <__config>
+#include <__type_traits/decay.h>
+#include <__utility/cmp.h>
+#include <limits>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 26
+
+template <typename _Tp>
+concept __libcpp_standard_integer = __libcpp_unsigned_integer<decay_t<_Tp>> || __libcpp_signed_integer<decay_t<_Tp>>;
+
+template <__libcpp_standard_integer _Tp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
+  if (_Tp __sum; !__builtin_add_overflow(__x, __y, &__sum))
----------------
H-G-Hristov wrote:

> These builtins don't work with 128-bit integrals. @AaronBallman are you happy to receive patches for this in Clang? If so what is the proper name for `__builtin_uaddXX_overflow` and `____builtin_saddXX_overflow`? Obviously there should be patches for `sub` and `mul`.
> 
> @H-G-Hristov if acceptable for Clang do you want to do the work?

Yes, I could try to do the work. Please note I have no experience with Clang yet.

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


More information about the cfe-commits mailing list