[libc] Add sub_with_borrow to builtin_wrapper.h

Add sub_with_borrow to builtin_wrapper.h to be used in UInt.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D138182
This commit is contained in:
Tue Ly 2022-11-17 00:56:56 -05:00
parent 420d0d3aa6
commit e205fc1836
7 changed files with 149 additions and 46 deletions

View File

@ -6,11 +6,18 @@ add_header_library(
blockstore.h
)
add_header_library(
named_pair
HDRS
named_pair.h
)
add_header_library(
builtin_wrappers
HDRS
builtin_wrappers.h
DEPENDS
.named_pair
libc.src.__support.CPP.type_traits
)
@ -111,6 +118,7 @@ add_header_library(
HDRS
number_pair.h
DEPENDS
.named_pair
libc.src.__support.CPP.type_traits
)

View File

@ -101,22 +101,20 @@ template <size_t Bits> struct UInt {
// property of unsigned integers:
// x + (~x) = 2^(sizeof(x)) - 1.
constexpr uint64_t add(const UInt<Bits> &x) {
uint64_t carry_in = 0;
uint64_t carry_out = 0;
SumCarry<uint64_t> s{0, 0};
for (size_t i = 0; i < WordCount; ++i) {
val[i] = add_with_carry(val[i], x.val[i], carry_in, carry_out);
carry_in = carry_out;
s = add_with_carry(val[i], x.val[i], s.carry);
val[i] = s.sum;
}
return carry_out;
return s.carry;
}
constexpr UInt<Bits> operator+(const UInt<Bits> &other) const {
UInt<Bits> result;
uint64_t carry_in = 0;
uint64_t carry_out = 0;
SumCarry<uint64_t> s{0, 0};
for (size_t i = 0; i < WordCount; ++i) {
result.val[i] = add_with_carry(val[i], other.val[i], carry_in, carry_out);
carry_in = carry_out;
s = add_with_carry(val[i], other.val[i], s.carry);
result.val[i] = s.sum;
}
return result;
}

View File

@ -10,6 +10,7 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_BUILTIN_WRAPPERS_H
#define LLVM_LIBC_SRC_SUPPORT_BUILTIN_WRAPPERS_H
#include "named_pair.h"
#include "src/__support/CPP/type_traits.h"
namespace __llvm_libc {
@ -67,59 +68,131 @@ template <typename T> static inline int unsafe_clz(T val) {
}
// Add with carry
DEFINE_NAMED_PAIR_TEMPLATE(SumCarry, sum, 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) {
cpp::is_integral_v<T> && cpp::is_unsigned_v<T>, SumCarry<T>>
add_with_carry(T a, T b, T carry_in) {
T tmp = a + carry_in;
T sum = b + tmp;
carry_out = (sum < b) || (tmp < a);
return sum;
T carry_out = (sum < b) || (tmp < a);
return {sum, carry_out};
}
#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);
inline SumCarry<unsigned char>
add_with_carry<unsigned char>(unsigned char a, unsigned char b,
unsigned char carry_in) {
SumCarry<unsigned char> result{0, 0};
result.sum = __builtin_addcb(a, b, carry_in, &result.carry);
return result;
}
template <>
inline unsigned short
inline SumCarry<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);
unsigned short carry_in) {
SumCarry<unsigned short> result{0, 0};
result.sum = __builtin_addcs(a, b, carry_in, &result.carry);
return result;
}
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);
inline SumCarry<unsigned int>
add_with_carry<unsigned int>(unsigned int a, unsigned int b,
unsigned int carry_in) {
SumCarry<unsigned int> result{0, 0};
result.sum = __builtin_addc(a, b, carry_in, &result.carry);
return result;
}
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);
inline SumCarry<unsigned long>
add_with_carry<unsigned long>(unsigned long a, unsigned long b,
unsigned long carry_in) {
SumCarry<unsigned long> result{0, 0};
result.sum = __builtin_addcl(a, b, carry_in, &result.carry);
return result;
}
template <>
inline unsigned long long
inline SumCarry<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);
unsigned long long carry_in) {
SumCarry<unsigned long long> result{0, 0};
result.sum = __builtin_addcll(a, b, carry_in, &result.carry);
return result;
}
#endif // __has_builtin(__builtin_addc)
// Subtract with borrow
DEFINE_NAMED_PAIR_TEMPLATE(DiffBorrow, diff, borrow);
template <typename T>
inline constexpr cpp::enable_if_t<
cpp::is_integral_v<T> && cpp::is_unsigned_v<T>, DiffBorrow<T>>
sub_with_borrow(T a, T b, T borrow_in) {
T tmp = a - b;
T diff = tmp - borrow_in;
T borrow_out = (diff > tmp) || (tmp > a);
return {diff, borrow_out};
}
#if __has_builtin(__builtin_subc)
// https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins
template <>
inline DiffBorrow<unsigned char>
sub_with_borrow<unsigned char>(unsigned char a, unsigned char b,
unsigned char borrow_in) {
DiffBorrow<unsigned char> result{0, 0};
result.diff = __builtin_subcb(a, b, borrow_in, &result.borrow);
return result;
}
template <>
inline DiffBorrow<unsigned short>
sub_with_borrow<unsigned short>(unsigned short a, unsigned short b,
unsigned short borrow_in) {
DiffBorrow<unsigned short> result{0, 0};
result.diff = __builtin_subcs(a, b, borrow_in, &result.borrow);
return result;
}
template <>
inline DiffBorrow<unsigned int>
sub_with_borrow<unsigned int>(unsigned int a, unsigned int b,
unsigned int borrow_in) {
DiffBorrow<unsigned int> result{0, 0};
result.diff = __builtin_subc(a, b, borrow_in, &result.borrow);
return result;
}
template <>
inline DiffBorrow<unsigned long>
sub_with_borrow<unsigned long>(unsigned long a, unsigned long b,
unsigned long borrow_in) {
DiffBorrow<unsigned long> result{0, 0};
result.diff = __builtin_subcl(a, b, borrow_in, &result.borrow);
return result;
}
template <>
inline DiffBorrow<unsigned long long>
sub_with_borrow<unsigned long long>(unsigned long long a, unsigned long long b,
unsigned long long borrow_in) {
DiffBorrow<unsigned long long> result{0, 0};
result.diff = __builtin_subcll(a, b, borrow_in, &result.borrow);
return result;
}
#endif // __has_builtin(__builtin_subc)
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_SUPPORT_BUILTIN_WRAPPERS_H

View File

@ -46,13 +46,13 @@ inline NumberPair<uint64_t> full_mul<uint64_t>(uint64_t a, uint64_t b) {
NumberPair<uint64_t> lo_hi = split(pa.lo * pb.hi); // exact
NumberPair<uint64_t> hi_lo = split(pa.hi * pb.lo); // exact
uint64_t carry_in = 0;
uint64_t carry_out = 0;
uint64_t carry_unused = 0;
prod.lo = add_with_carry(prod.lo, lo_hi.lo << 32, carry_in, carry_out);
prod.hi = add_with_carry(prod.hi, lo_hi.hi, carry_out, carry_unused);
prod.lo = add_with_carry(prod.lo, hi_lo.lo << 32, carry_in, carry_out);
prod.hi = add_with_carry(prod.hi, hi_lo.hi, carry_out, carry_unused);
auto r1 = add_with_carry(prod.lo, lo_hi.lo << 32, uint64_t(0));
prod.lo = r1.sum;
prod.hi = add_with_carry(prod.hi, lo_hi.hi, r1.carry).sum;
auto r2 = add_with_carry(prod.lo, hi_lo.lo << 32, uint64_t(0));
prod.lo = r2.sum;
prod.hi = add_with_carry(prod.hi, hi_lo.hi, r2.carry).sum;
return prod;
#endif // __SIZEOF_INT128__

View File

@ -0,0 +1,18 @@
//===-- Utilities for pairs of numbers. -------------------------*- 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 LLVM_LIBC_SRC_SUPPORT_NAMED_PAIR_H
#define LLVM_LIBC_SRC_SUPPORT_NAMED_PAIR_H
#define DEFINE_NAMED_PAIR_TEMPLATE(Name, FirstField, SecondField) \
template <typename T1, typename T2 = T1> struct Name { \
T1 FirstField; \
T2 SecondField; \
}
#endif // LLVM_LIBC_SRC_SUPPORT_NAMED_PAIR_H

View File

@ -10,15 +10,13 @@
#define LLVM_LIBC_SRC_SUPPORT_NUMBER_PAIR_H
#include "CPP/type_traits.h"
#include "named_pair.h"
#include <stddef.h>
namespace __llvm_libc {
template <typename T> struct NumberPair {
T lo;
T hi;
};
DEFINE_NAMED_PAIR_TEMPLATE(NumberPair, lo, hi);
using DoubleDouble = NumberPair<double>;

View File

@ -161,6 +161,7 @@ cc_library(
hdrs = ["src/__support/number_pair.h"],
deps = [
"__support_cpp_type_traits",
"__support_named_pair",
":libc_root",
],
)
@ -230,11 +231,18 @@ cc_library(
],
)
cc_library(
name = "__support_named_pair",
hdrs = ["src/__support/named_pair.h"],
deps = [":libc_root"],
)
cc_library(
name = "__support_builtin_wrappers",
hdrs = ["src/__support/builtin_wrappers.h"],
deps = [
":__support_cpp_type_traits",
":__support_named_pair",
":libc_root",
],
)