[libc-commits] [PATCH] D137453: [libc] Add add_with_carry to builtin wrapper.
Tue Ly via Phabricator via libc-commits
libc-commits at lists.llvm.org
Fri Nov 4 12:42:43 PDT 2022
lntue updated this revision to Diff 473317.
lntue added a comment.
Address comments.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D137453/new/
https://reviews.llvm.org/D137453
Files:
libc/src/__support/CMakeLists.txt
libc/src/__support/builtin_wrappers.h
utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Index: utils/bazel/llvm-project-overlay/libc/BUILD.bazel
===================================================================
--- utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -183,6 +183,7 @@
name = "__support_builtin_wrappers",
hdrs = ["src/__support/builtin_wrappers.h"],
deps = [
+ ":__support_cpp_type_traits",
":libc_root",
],
)
Index: libc/src/__support/builtin_wrappers.h
===================================================================
--- libc/src/__support/builtin_wrappers.h
+++ libc/src/__support/builtin_wrappers.h
@@ -10,6 +10,8 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_BUILTIN_WRAPPERS_H
#define LLVM_LIBC_SRC_SUPPORT_BUILTIN_WRAPPERS_H
+#include "src/__support/CPP/type_traits.h"
+
namespace __llvm_libc {
// The following overloads are matched based on what is accepted by
@@ -64,6 +66,60 @@
return __internal::clz(val);
}
+// Add with carry
+template <typename T>
+inline constexpr cpp::enable_if_t<
+ cpp::is_integral_v<T> && cpp::is_unsigned_v<T>, T>
+add_with_carry(T a, T b, T carry_in, T &carry_out) {
+ T tmp = a + carry_in;
+ T sum = b + tmp;
+ carry_out = (sum < b) || (tmp < a);
+ return sum;
+}
+
+#if __has_builtin(__builtin_addc)
+// https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins
+
+template <>
+inline unsigned char add_with_carry<unsigned char>(unsigned char a,
+ unsigned char b,
+ unsigned char carry_in,
+ unsigned char &carry_out) {
+ return __builtin_addcb(a, b, carry_in, &carry_out);
+}
+
+template <>
+inline unsigned short
+add_with_carry<unsigned short>(unsigned short a, unsigned short b,
+ unsigned short carry_in,
+ unsigned short &carry_out) {
+ return __builtin_addcs(a, b, carry_in, &carry_out);
+}
+
+template <>
+inline unsigned int add_with_carry<unsigned int>(unsigned int a, unsigned int b,
+ unsigned int carry_in,
+ unsigned int &carry_out) {
+ return __builtin_addc(a, b, carry_in, &carry_out);
+}
+
+template <>
+inline unsigned long add_with_carry<unsigned long>(unsigned long a,
+ unsigned long b,
+ unsigned long carry_in,
+ unsigned long &carry_out) {
+ return __builtin_addcl(a, b, carry_in, &carry_out);
+}
+
+template <>
+inline unsigned long long
+add_with_carry<unsigned long long>(unsigned long long a, unsigned long long b,
+ unsigned long long carry_in,
+ unsigned long long &carry_out) {
+ return __builtin_addcll(a, b, carry_in, &carry_out);
+}
+#endif // __has_builtin(__builtin_addc)
+
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_SUPPORT_BUILTIN_WRAPPERS_H
Index: libc/src/__support/CMakeLists.txt
===================================================================
--- libc/src/__support/CMakeLists.txt
+++ libc/src/__support/CMakeLists.txt
@@ -10,6 +10,8 @@
builtin_wrappers
HDRS
builtin_wrappers.h
+ DEPENDS
+ libc.src.__support.CPP.type_traits
)
add_header_library(
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137453.473317.patch
Type: text/x-patch
Size: 3457 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221104/dea725eb/attachment.bin>
More information about the libc-commits
mailing list