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#include "src/__support/macros/config.h"
18
19namespace LIBC_NAMESPACE_DECL {
20
21// Create a bitmask with the count right-most bits set to 1, and all other bits
22// set to 0. Only unsigned types are allowed.
23template <typename T, size_t count>
24LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
25mask_trailing_ones() {
26 constexpr unsigned T_BITS = CHAR_BIT * sizeof(T);
27 static_assert(count <= T_BITS && "Invalid bit index");
28 // MSVC complains about out of range shifts.
29 if constexpr (count == 0)
30 return 0;
31 else if constexpr (count >= T_BITS)
32 return T(-1);
33 else
34 return T(-1) >> (T_BITS - count);
35}
36
37// Create a bitmask with the count left-most bits set to 1, and all other bits
38// set to 0. Only unsigned types are allowed.
39template <typename T, size_t count>
40LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
41mask_leading_ones() {
42 return T(~mask_trailing_ones<T, CHAR_BIT * sizeof(T) - count>());
43}
44
45// Create a bitmask with the count right-most bits set to 0, and all other bits
46// set to 1. Only unsigned types are allowed.
47template <typename T, size_t count>
48LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
49mask_trailing_zeros() {
50 return mask_leading_ones<T, CHAR_BIT * sizeof(T) - count>();
51}
52
53// Create a bitmask with the count left-most bits set to 0, and all other bits
54// set to 1. Only unsigned types are allowed.
55template <typename T, size_t count>
56LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
57mask_leading_zeros() {
58 return mask_trailing_ones<T, CHAR_BIT * sizeof(T) - count>();
59}
60
61// Returns whether 'a + b' overflows, the result is stored in 'res'.
62template <typename T>
63[[nodiscard]] LIBC_INLINE constexpr bool add_overflow(T a, T b, T &res) {
64#if __has_builtin(__builtin_add_overflow)
65 return __builtin_add_overflow(a, b, &res);
66#else
67 res = a + b;
68 return (res < a) || (res < b);
69#endif // __builtin_add_overflow
70}
71
72// Returns whether 'a - b' overflows, the result is stored in 'res'.
73template <typename T>
74[[nodiscard]] LIBC_INLINE constexpr bool sub_overflow(T a, T b, T &res) {
75#if __has_builtin(__builtin_sub_overflow)
76 return __builtin_sub_overflow(a, b, &res);
77#else
78 res = a - b;
79 return (res > a);
80#endif // __builtin_sub_overflow
81}
82
83template <typename T>
84[[nodiscard]] LIBC_INLINE constexpr bool mul_overflow(T a, T b, T &res) {
85#if __has_builtin(__builtin_mul_overflow)
86 return __builtin_mul_overflow(a, b, &res);
87#else
88 T max = cpp::numeric_limits<T>::max();
89 T min = cpp::numeric_limits<T>::min();
90 bool overflow = (b > 0 && (a > max / b || a < min / b)) ||
91 (b < 0 && (a < max / b || a > min / b));
92 if (!overflow)
93 res = a * b;
94 return overflow;
95#endif
96}
97
98#define RETURN_IF(TYPE, BUILTIN) \
99 if constexpr (cpp::is_same_v<T, TYPE>) \
100 return BUILTIN(a, b, carry_in, &carry_out);
101
102// Returns the result of 'a + b' taking into account 'carry_in'.
103// The carry out is stored in 'carry_out' it not 'nullptr', dropped otherwise.
104// We keep the pass by pointer interface for consistency with the intrinsic.
105template <typename T>
106[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
107add_with_carry(T a, T b, T carry_in, T &carry_out) {
108 if (!cpp::is_constant_evaluated()) {
109#if __has_builtin(__builtin_addcb)
110 RETURN_IF(unsigned char, __builtin_addcb)
111#elif __has_builtin(__builtin_addcs)
112 RETURN_IF(unsigned short, __builtin_addcs)
113#elif __has_builtin(__builtin_addc)
114 RETURN_IF(unsigned int, __builtin_addc)
115#elif __has_builtin(__builtin_addcl)
116 RETURN_IF(unsigned long, __builtin_addcl)
117#elif __has_builtin(__builtin_addcll)
118 RETURN_IF(unsigned long long, __builtin_addcll)
119#endif
120 }
121 T sum = {};
122 T carry1 = add_overflow(a, b, sum);
123 T carry2 = add_overflow(sum, carry_in, sum);
124 carry_out = carry1 | carry2;
125 return sum;
126}
127
128// Returns the result of 'a - b' taking into account 'carry_in'.
129// The carry out is stored in 'carry_out' it not 'nullptr', dropped otherwise.
130// We keep the pass by pointer interface for consistency with the intrinsic.
131template <typename T>
132[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>
133sub_with_borrow(T a, T b, T carry_in, T &carry_out) {
134 if (!cpp::is_constant_evaluated()) {
135#if __has_builtin(__builtin_subcb)
136 RETURN_IF(unsigned char, __builtin_subcb)
137#elif __has_builtin(__builtin_subcs)
138 RETURN_IF(unsigned short, __builtin_subcs)
139#elif __has_builtin(__builtin_subc)
140 RETURN_IF(unsigned int, __builtin_subc)
141#elif __has_builtin(__builtin_subcl)
142 RETURN_IF(unsigned long, __builtin_subcl)
143#elif __has_builtin(__builtin_subcll)
144 RETURN_IF(unsigned long long, __builtin_subcll)
145#endif
146 }
147 T sub = {};
148 T carry1 = sub_overflow(a, b, sub);
149 T carry2 = sub_overflow(sub, carry_in, sub);
150 carry_out = carry1 | carry2;
151 return sub;
152}
153
154#undef RETURN_IF
155
156template <typename T>
157[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
158first_leading_zero(T value) {
159 return value == cpp::numeric_limits<T>::max() ? 0
160 : cpp::countl_one(value) + 1;
161}
162
163template <typename T>
164[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
165first_leading_one(T value) {
166 return first_leading_zero(static_cast<T>(~value));
167}
168
169template <typename T>
170[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
171first_trailing_zero(T value) {
172 return value == cpp::numeric_limits<T>::max()
173 ? 0
174 : cpp::countr_zero(static_cast<T>(~value)) + 1;
175}
176
177template <typename T>
178[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
179first_trailing_one(T value) {
180 return value == 0 ? 0 : cpp::countr_zero(value) + 1;
181}
182
183template <typename T>
184[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
185count_zeros(T value) {
186 return cpp::popcount<T>(static_cast<T>(~value));
187}
188
189} // namespace LIBC_NAMESPACE_DECL
190
191#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
192