Path: blob/main/contrib/llvm-project/libc/src/__support/CPP/bit.h
213799 views
//===-- Implementation of the C++20 bit header -----------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// This is inspired by LLVM ADT/bit.h header.8// Some functions are missing, we can add them as needed (popcount, byteswap).910#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H11#define LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H1213#include "src/__support/CPP/limits.h" // numeric_limits14#include "src/__support/CPP/type_traits.h"15#include "src/__support/macros/attributes.h"16#include "src/__support/macros/config.h"17#include "src/__support/macros/sanitizer.h"1819#include <stdint.h>2021namespace LIBC_NAMESPACE_DECL {22namespace cpp {2324#if __has_builtin(__builtin_memcpy_inline)25#define LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE26#endif2728// This implementation of bit_cast requires trivially-constructible To, to avoid29// UB in the implementation.30template <typename To, typename From>31LIBC_INLINE constexpr cpp::enable_if_t<32(sizeof(To) == sizeof(From)) &&33cpp::is_trivially_constructible<To>::value &&34cpp::is_trivially_copyable<To>::value &&35cpp::is_trivially_copyable<From>::value,36To>37bit_cast(const From &from) {38MSAN_UNPOISON(&from, sizeof(From));39#if __has_builtin(__builtin_bit_cast)40return __builtin_bit_cast(To, from);41#else42To to;43char *dst = reinterpret_cast<char *>(&to);44const char *src = reinterpret_cast<const char *>(&from);45#if __has_builtin(__builtin_memcpy_inline)46__builtin_memcpy_inline(dst, src, sizeof(To));47#else48for (unsigned i = 0; i < sizeof(To); ++i)49dst[i] = src[i];50#endif // __has_builtin(__builtin_memcpy_inline)51return to;52#endif // __has_builtin(__builtin_bit_cast)53}5455template <typename T>56[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>,57bool>58has_single_bit(T value) {59return (value != 0) && ((value & (value - 1)) == 0);60}6162// A temporary macro to add template function specialization when compiler63// builtin is available.64#define ADD_SPECIALIZATION(NAME, TYPE, BUILTIN) \65template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \66static_assert(cpp::is_unsigned_v<TYPE>); \67return value == 0 ? cpp::numeric_limits<TYPE>::digits : BUILTIN(value); \68}6970/// Count number of 0's from the least significant bit to the most71/// stopping at the first 1.72///73/// Only unsigned integral types are allowed.74///75/// Returns cpp::numeric_limits<T>::digits on an input of 0.76// clang-19+, gcc-14+77#if __has_builtin(__builtin_ctzg)78template <typename T>79[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>80countr_zero(T value) {81return __builtin_ctzg(value, cpp::numeric_limits<T>::digits);82}83#else84template <typename T>85[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>86countr_zero(T value) {87if (!value)88return cpp::numeric_limits<T>::digits;89if (value & 0x1)90return 0;91// Bisection method.92unsigned zero_bits = 0;93unsigned shift = cpp::numeric_limits<T>::digits >> 1;94T mask = cpp::numeric_limits<T>::max() >> shift;95while (shift) {96if ((value & mask) == 0) {97value >>= shift;98zero_bits |= shift;99}100shift >>= 1;101mask >>= shift;102}103return static_cast<int>(zero_bits);104}105#if __has_builtin(__builtin_ctzs)106ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)107#endif108ADD_SPECIALIZATION(countr_zero, unsigned int, __builtin_ctz)109ADD_SPECIALIZATION(countr_zero, unsigned long, __builtin_ctzl)110ADD_SPECIALIZATION(countr_zero, unsigned long long, __builtin_ctzll)111#endif // __has_builtin(__builtin_ctzg)112113/// Count number of 0's from the most significant bit to the least114/// stopping at the first 1.115///116/// Only unsigned integral types are allowed.117///118/// Returns cpp::numeric_limits<T>::digits on an input of 0.119// clang-19+, gcc-14+120#if __has_builtin(__builtin_clzg)121template <typename T>122[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>123countl_zero(T value) {124return __builtin_clzg(value, cpp::numeric_limits<T>::digits);125}126#else127template <typename T>128[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>129countl_zero(T value) {130if (!value)131return cpp::numeric_limits<T>::digits;132// Bisection method.133unsigned zero_bits = 0;134for (unsigned shift = cpp::numeric_limits<T>::digits >> 1; shift;135shift >>= 1) {136T tmp = value >> shift;137if (tmp)138value = tmp;139else140zero_bits |= shift;141}142return static_cast<int>(zero_bits);143}144#if __has_builtin(__builtin_clzs)145ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)146#endif147ADD_SPECIALIZATION(countl_zero, unsigned int, __builtin_clz)148ADD_SPECIALIZATION(countl_zero, unsigned long, __builtin_clzl)149ADD_SPECIALIZATION(countl_zero, unsigned long long, __builtin_clzll)150#endif // __has_builtin(__builtin_clzg)151152#undef ADD_SPECIALIZATION153154/// Count the number of ones from the most significant bit to the first155/// zero bit.156///157/// Ex. countl_one(0xFF0FFF00) == 8.158/// Only unsigned integral types are allowed.159///160/// Returns cpp::numeric_limits<T>::digits on an input of all ones.161template <typename T>162[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>163countl_one(T value) {164return cpp::countl_zero<T>(static_cast<T>(~value));165}166167/// Count the number of ones from the least significant bit to the first168/// zero bit.169///170/// Ex. countr_one(0x00FF00FF) == 8.171/// Only unsigned integral types are allowed.172///173/// Returns cpp::numeric_limits<T>::digits on an input of all ones.174template <typename T>175[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>176countr_one(T value) {177return cpp::countr_zero<T>(static_cast<T>(~value));178}179180/// Returns the number of bits needed to represent value if value is nonzero.181/// Returns 0 otherwise.182///183/// Ex. bit_width(5) == 3.184template <typename T>185[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>186bit_width(T value) {187return cpp::numeric_limits<T>::digits - cpp::countl_zero(value);188}189190/// Returns the largest integral power of two no greater than value if value is191/// nonzero. Returns 0 otherwise.192///193/// Ex. bit_floor(5) == 4.194template <typename T>195[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>196bit_floor(T value) {197if (!value)198return 0;199return static_cast<T>(T(1) << (cpp::bit_width(value) - 1));200}201202/// Returns the smallest integral power of two no smaller than value if value is203/// nonzero. Returns 1 otherwise.204///205/// Ex. bit_ceil(5) == 8.206///207/// The return value is undefined if the input is larger than the largest power208/// of two representable in T.209template <typename T>210[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>211bit_ceil(T value) {212if (value < 2)213return 1;214return static_cast<T>(T(1) << cpp::bit_width(value - 1U));215}216217// Rotate algorithms make use of "Safe, Efficient, and Portable Rotate in C/C++"218// from https://blog.regehr.org/archives/1063.219220// Forward-declare rotr so that rotl can use it.221template <typename T>222[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>223rotr(T value, int rotate);224225template <typename T>226[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>227rotl(T value, int rotate) {228constexpr int N = cpp::numeric_limits<T>::digits;229rotate = rotate % N;230if (!rotate)231return value;232if (rotate < 0)233return cpp::rotr<T>(value, -rotate);234return static_cast<T>((value << rotate) | (value >> (N - rotate)));235}236237template <typename T>238[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T>239rotr(T value, int rotate) {240constexpr int N = cpp::numeric_limits<T>::digits;241rotate = rotate % N;242if (!rotate)243return value;244if (rotate < 0)245return cpp::rotl<T>(value, -rotate);246return static_cast<T>((value >> rotate) | (value << (N - rotate)));247}248249// TODO: Do we need this function at all? How is it different from250// 'static_cast'?251template <class To, class From>252LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {253if constexpr (sizeof(To) == sizeof(From)) {254return bit_cast<To>(from);255} else {256return static_cast<To>(from);257}258}259260/// Count number of 1's aka population count or Hamming weight.261///262/// Only unsigned integral types are allowed.263// clang-19+, gcc-14+264#if __has_builtin(__builtin_popcountg)265template <typename T>266[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>267popcount(T value) {268return __builtin_popcountg(value);269}270#else // !__has_builtin(__builtin_popcountg)271template <typename T>272[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>273popcount(T value) {274int count = 0;275while (value) {276value &= value - 1;277++count;278}279return count;280}281#define ADD_SPECIALIZATION(TYPE, BUILTIN) \282template <> \283[[nodiscard]] LIBC_INLINE constexpr int popcount<TYPE>(TYPE value) { \284return BUILTIN(value); \285}286ADD_SPECIALIZATION(unsigned char, __builtin_popcount)287ADD_SPECIALIZATION(unsigned short, __builtin_popcount)288ADD_SPECIALIZATION(unsigned, __builtin_popcount)289ADD_SPECIALIZATION(unsigned long, __builtin_popcountl)290ADD_SPECIALIZATION(unsigned long long, __builtin_popcountll)291#endif // __builtin_popcountg292#undef ADD_SPECIALIZATION293294} // namespace cpp295} // namespace LIBC_NAMESPACE_DECL296297#endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H298299300