Path: blob/main/contrib/llvm-project/libc/src/__support/math_extras.h
213766 views
//===-- Mimics llvm/Support/MathExtras.h ------------------------*- C++ -*-===//1// Provides useful math functions.2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H1112#include "src/__support/CPP/bit.h" // countl_one, countr_zero13#include "src/__support/CPP/limits.h" // CHAR_BIT, numeric_limits14#include "src/__support/CPP/type_traits.h" // is_unsigned_v, is_constant_evaluated15#include "src/__support/macros/attributes.h" // LIBC_INLINE16#include "src/__support/macros/config.h"1718namespace LIBC_NAMESPACE_DECL {1920// Create a bitmask with the count right-most bits set to 1, and all other bits21// 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() {25constexpr unsigned T_BITS = CHAR_BIT * sizeof(T);26static_assert(count <= T_BITS && "Invalid bit index");27return count == 0 ? 0 : (T(-1) >> (T_BITS - count));28}2930// Create a bitmask with the count left-most bits set to 1, and all other bits31// 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() {35return T(~mask_trailing_ones<T, CHAR_BIT * sizeof(T) - count>());36}3738// Create a bitmask with the count right-most bits set to 0, and all other bits39// 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() {43return mask_leading_ones<T, CHAR_BIT * sizeof(T) - count>();44}4546// Create a bitmask with the count left-most bits set to 0, and all other bits47// 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() {51return mask_trailing_ones<T, CHAR_BIT * sizeof(T) - count>();52}5354// 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) {57return __builtin_add_overflow(a, b, &res);58}5960// 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) {63return __builtin_sub_overflow(a, b, &res);64}6566#define RETURN_IF(TYPE, BUILTIN) \67if constexpr (cpp::is_same_v<T, TYPE>) \68return BUILTIN(a, b, carry_in, carry_out);6970// 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) {76if constexpr (!cpp::is_constant_evaluated()) {77#if __has_builtin(__builtin_addcb)78RETURN_IF(unsigned char, __builtin_addcb)79#elif __has_builtin(__builtin_addcs)80RETURN_IF(unsigned short, __builtin_addcs)81#elif __has_builtin(__builtin_addc)82RETURN_IF(unsigned int, __builtin_addc)83#elif __has_builtin(__builtin_addcl)84RETURN_IF(unsigned long, __builtin_addcl)85#elif __has_builtin(__builtin_addcll)86RETURN_IF(unsigned long long, __builtin_addcll)87#endif88}89T sum = {};90T carry1 = add_overflow(a, b, sum);91T carry2 = add_overflow(sum, carry_in, sum);92carry_out = carry1 | carry2;93return sum;94}9596// 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) {102if constexpr (!cpp::is_constant_evaluated()) {103#if __has_builtin(__builtin_subcb)104RETURN_IF(unsigned char, __builtin_subcb)105#elif __has_builtin(__builtin_subcs)106RETURN_IF(unsigned short, __builtin_subcs)107#elif __has_builtin(__builtin_subc)108RETURN_IF(unsigned int, __builtin_subc)109#elif __has_builtin(__builtin_subcl)110RETURN_IF(unsigned long, __builtin_subcl)111#elif __has_builtin(__builtin_subcll)112RETURN_IF(unsigned long long, __builtin_subcll)113#endif114}115T sub = {};116T carry1 = sub_overflow(a, b, sub);117T carry2 = sub_overflow(sub, carry_in, sub);118carry_out = carry1 | carry2;119return sub;120}121122#undef RETURN_IF123124template <typename T>125[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>126first_leading_zero(T value) {127return value == cpp::numeric_limits<T>::max() ? 0128: cpp::countl_one(value) + 1;129}130131template <typename T>132[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>133first_leading_one(T value) {134return first_leading_zero(static_cast<T>(~value));135}136137template <typename T>138[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>139first_trailing_zero(T value) {140return value == cpp::numeric_limits<T>::max()141? 0142: cpp::countr_zero(static_cast<T>(~value)) + 1;143}144145template <typename T>146[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>147first_trailing_one(T value) {148return value == 0 ? 0 : cpp::countr_zero(value) + 1;149}150151template <typename T>152[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>153count_zeros(T value) {154return cpp::popcount<T>(static_cast<T>(~value));155}156157} // namespace LIBC_NAMESPACE_DECL158159#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H160161162