[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 11:31:11 PDT 2022
lntue created this revision.
lntue added reviewers: michaelrj, sivachandra, orex.
Herald added subscribers: ecnelises, tschuett.
Herald added projects: libc-project, All.
lntue requested review of this revision.
Add add_with_carry to builtin wrapper to be used by UInt class.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D137453
Files:
libc/src/__support/builtin_wrappers.h
Index: libc/src/__support/builtin_wrappers.h
===================================================================
--- libc/src/__support/builtin_wrappers.h
+++ libc/src/__support/builtin_wrappers.h
@@ -64,6 +64,58 @@
return __internal::clz(val);
}
+// Add with carry
+template <typename T>
+inline constexpr 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137453.473301.patch
Type: text/x-patch
Size: 2357 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221104/2b045a34/attachment-0001.bin>
More information about the libc-commits
mailing list