1//===-- Mimics llvm/Support/MathExtras.h ------------------------*- C++ -*-===//
2// Provides useful math functions.
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
11#define LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
12
13#include "src/__support/CPP/bit.h" // countl_one, countr_zero
14#include "src/__support/CPP/limits.h" // CHAR_BIT, numeric_limits
15#include "src/__support/CPP/type_traits.h" // is_unsigned_v, is_constant_evaluated
16#include "src/__support/macros/attributes.h" // LIBC_INLINE
17
18namespace LIBC_NAMESPACE {
19
20// Create a bitmask with the count right-most bits set to 1, and all other bits
21// set to 0. Only unsigned types are allowed.
22template <typename T, size_t count>
23LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
24mask_trailing_ones() {
25 constexpr unsigned T_BITS = CHAR_BIT * sizeof(T);
26 static_assert(count <= T_BITS && "Invalid bit index");
27 return count == 0 ? 0 : (T(-1) >> (T_BITS - count));
28}
29
30// Create a bitmask with the count left-most bits set to 1, and all other bits
31// set to 0. Only unsigned types are allowed.
32template <typename T, size_t count>
33LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
34mask_leading_ones() {
35 return T(~mask_trailing_ones<T, CHAR_BIT * sizeof(T) - count>());
36}
37
38// Create a bitmask with the count right-most bits set to 0, and all other bits
39// set to 1. Only unsigned types are allowed.
40template <typename T, size_t count>
41LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
42mask_trailing_zeros() {
43 return mask_leading_ones<T, CHAR_BIT * sizeof(T) - count>();
44}
45
46// Create a bitmask with the count left-most bits set to 0, and all other bits
47// set to 1. Only unsigned types are allowed.
48template <typename T, size_t count>
49LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
50mask_leading_zeros() {
51 return mask_trailing_ones<T, CHAR_BIT * sizeof(T) - count>();
52}
53
54// Returns whether 'a + b' overflows, the result is stored in 'res'.
55template <typename T>
56[[nodiscard]] LIBC_INLINE constexpr bool add_overflow(T a, T b, T &res) {
57 return __builtin_add_overflow(a, b, &res);
58}
59
60// Returns whether 'a - b' overflows, the result is stored in 'res'.
61template <typename T>
62[[nodiscard]] LIBC_INLINE constexpr bool sub_overflow(T a, T b, T &res) {
63 return __builtin_sub_overflow(a, b, &res);
64}
65
66#define RETURN_IF(TYPE, BUILTIN) \
67 if constexpr (cpp::is_same_v<T, TYPE>) \
68 return BUILTIN(a, b, carry_in, carry_out);
69
70// Returns the result of 'a + b' taking into account 'carry_in'.
71// The carry out is stored in 'carry_out' it not 'nullptr', dropped otherwise.
72// We keep the pass by pointer interface for consistency with the intrinsic.
73template <typename T>
74[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
75add_with_carry(T a, T b, T carry_in, T &carry_out) {
76 if constexpr (!cpp::is_constant_evaluated()) {
77#if __has_builtin(__builtin_addcb)
78 RETURN_IF(unsigned char, __builtin_addcb)
79#elif __has_builtin(__builtin_addcs)
80 RETURN_IF(unsigned short, __builtin_addcs)
81#elif __has_builtin(__builtin_addc)
82 RETURN_IF(unsigned int, __builtin_addc)
83#elif __has_builtin(__builtin_addcl)
84 RETURN_IF(unsigned long, __builtin_addcl)
85#elif __has_builtin(__builtin_addcll)
86 RETURN_IF(unsigned long long, __builtin_addcll)
87#endif
88 }
89 T sum = {};
90 T carry1 = add_overflow(a, b, sum);
91 T carry2 = add_overflow(sum, carry_in, sum);
92 carry_out = carry1 | carry2;
93 return sum;
94}
95
96// Returns the result of 'a - b' taking into account 'carry_in'.
97// The carry out is stored in 'carry_out' it not 'nullptr', dropped otherwise.
98// We keep the pass by pointer interface for consistency with the intrinsic.
99template <typename T>
100[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
101sub_with_borrow(T a, T b, T carry_in, T &carry_out) {
102 if constexpr (!cpp::is_constant_evaluated()) {
103#if __has_builtin(__builtin_subcb)
104 RETURN_IF(unsigned char, __builtin_subcb)
105#elif __has_builtin(__builtin_subcs)
106 RETURN_IF(unsigned short, __builtin_subcs)
107#elif __has_builtin(__builtin_subc)
108 RETURN_IF(unsigned int, __builtin_subc)
109#elif __has_builtin(__builtin_subcl)
110 RETURN_IF(unsigned long, __builtin_subcl)
111#elif __has_builtin(__builtin_subcll)
112 RETURN_IF(unsigned long long, __builtin_subcll)
113#endif
114 }
115 T sub = {};
116 T carry1 = sub_overflow(a, b, sub);
117 T carry2 = sub_overflow(sub, carry_in, sub);
118 carry_out = carry1 | carry2;
119 return sub;
120}
121
122#undef RETURN_IF
123
124template <typename T>
125[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
126first_leading_zero(T value) {
127 return value == cpp::numeric_limits<T>::max() ? 0
128 : cpp::countl_one(value) + 1;
129}
130
131template <typename T>
132[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
133first_leading_one(T value) {
134 return first_leading_zero(static_cast<T>(~value));
135}
136
137template <typename T>
138[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
139first_trailing_zero(T value) {
140 return value == cpp::numeric_limits<T>::max()
141 ? 0
142 : cpp::countr_zero(static_cast<T>(~value)) + 1;
143}
144
145template <typename T>
146[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
147first_trailing_one(T value) {
148 return value == cpp::numeric_limits<T>::max() ? 0
149 : cpp::countr_zero(value) + 1;
150}
151
152template <typename T>
153[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
154count_zeros(T value) {
155 return cpp::popcount<T>(static_cast<T>(~value));
156}
157
158} // namespace LIBC_NAMESPACE
159
160#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
161

source code of libc/src/__support/math_extras.h