[libc-commits] [PATCH] D138182: [libc] Add sub_with_borrow to builtin_wrapper.h
Tue Ly via Phabricator via libc-commits
libc-commits at lists.llvm.org
Wed Nov 16 21:59:30 PST 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 sub_with_borrow to builtin_wrapper.h to be used in UInt.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D138182
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
@@ -120,6 +120,61 @@
}
#endif // __has_builtin(__builtin_addc)
+// Subtract with borrow
+template <typename T>
+inline constexpr cpp::enable_if_t<
+ cpp::is_integral_v<T> && cpp::is_unsigned_v<T>, T>
+sub_with_borrow(T a, T b, T borrow_in, T &borrow_out) {
+ T tmp = a - b;
+ T diff = tmp - borrow_in;
+ borrow_out = (diff > tmp) || (tmp > a);
+ return diff;
+}
+
+#if __has_builtin(__builtin_subc)
+// https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins
+
+template <>
+inline unsigned char sub_with_borrow<unsigned char>(unsigned char a,
+ unsigned char b,
+ unsigned char borrow_in,
+ unsigned char &borrow_out) {
+ return __builtin_subcb(a, b, borrow_in, &borrow_out);
+}
+
+template <>
+inline unsigned short
+sub_with_borrow<unsigned short>(unsigned short a, unsigned short b,
+ unsigned short borrow_in,
+ unsigned short &borrow_out) {
+ return __builtin_subcs(a, b, borrow_in, &borrow_out);
+}
+
+template <>
+inline unsigned int sub_with_borrow<unsigned int>(unsigned int a,
+ unsigned int b,
+ unsigned int borrow_in,
+ unsigned int &borrow_out) {
+ return __builtin_subc(a, b, borrow_in, &borrow_out);
+}
+
+template <>
+inline unsigned long sub_with_borrow<unsigned long>(unsigned long a,
+ unsigned long b,
+ unsigned long borrow_in,
+ unsigned long &borrow_out) {
+ return __builtin_subcl(a, b, borrow_in, &borrow_out);
+}
+
+template <>
+inline unsigned long long
+sub_with_borrow<unsigned long long>(unsigned long long a, unsigned long long b,
+ unsigned long long borrow_in,
+ unsigned long long &borrow_out) {
+ return __builtin_subcll(a, b, borrow_in, &borrow_out);
+}
+#endif // __has_builtin(__builtin_subc)
+
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_SUPPORT_BUILTIN_WRAPPERS_H
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138182.476007.patch
Type: text/x-patch
Size: 2546 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221117/aa48352b/attachment.bin>
More information about the libc-commits
mailing list