Skip to content

Commit

Permalink
Revert "[libc][NFC] Remove __support/bit.h and use __support/CPP/bit.…
Browse files Browse the repository at this point in the history
…h instead (#73939)"

This reverts commit b140948.
  • Loading branch information
gchatelet committed Dec 5, 2023
1 parent b140948 commit 4fdb933
Show file tree
Hide file tree
Showing 31 changed files with 393 additions and 107 deletions.
9 changes: 4 additions & 5 deletions libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ add_header_library(
.str_to_num_result
.uint128
libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.limits
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.dyadic_float
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.bit
libc.src.errno.errno
)

Expand Down Expand Up @@ -188,10 +188,10 @@ add_header_library(
HDRS
integer_utils.h
DEPENDS
.bit
.math_extras
.number_pair
libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
)

Expand All @@ -200,11 +200,11 @@ add_header_library(
HDRS
UInt.h
DEPENDS
.bit
.integer_utils
.math_extras
.number_pair
libc.src.__support.CPP.array
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
libc.src.__support.macros.optimization
)
Expand All @@ -231,9 +231,8 @@ add_header_library(
HDRS
hash.h
DEPENDS
.bit
.uint128
libc.src.__support.CPP.bit
libc.src.__support.CPP.limits
libc.src.__support.macros.attributes
)

Expand Down
2 changes: 2 additions & 0 deletions libc/src/__support/FPUtil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ add_header_library(
libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
libc.src.__support.bit
)

add_header_library(
Expand Down Expand Up @@ -145,6 +146,7 @@ add_header_library(
libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
libc.src.__support.bit
libc.src.__support.uint128
)

Expand Down
3 changes: 2 additions & 1 deletion libc/src/__support/FPUtil/FPBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/bit.h"
#include "src/__support/common.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE

Expand Down Expand Up @@ -221,7 +222,7 @@ template <typename T> struct FPBits {
LIBC_INLINE static constexpr FPBits<T> make_value(UIntType number, int ep) {
FPBits<T> result;
// offset: +1 for sign, but -1 for implicit first bit
int lz = cpp::countl_zero(number) - FloatProp::EXPONENT_WIDTH;
int lz = unsafe_clz(number) - FloatProp::EXPONENT_WIDTH;
number <<= lz;
ep -= lz;

Expand Down
11 changes: 4 additions & 7 deletions libc/src/__support/FPUtil/Hypot.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/UInt128.h"
#include "src/__support/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {
Expand All @@ -27,7 +28,7 @@ template <typename T>
LIBC_INLINE T find_leading_one(T mant, int &shift_length) {
shift_length = 0;
if (mant > 0) {
shift_length = (sizeof(mant) * 8) - 1 - cpp::countl_zero(mant);
shift_length = (sizeof(mant) * 8) - 1 - unsafe_clz(mant);
}
return T(1) << shift_length;
}
Expand All @@ -36,13 +37,9 @@ LIBC_INLINE T find_leading_one(T mant, int &shift_length) {

template <typename T> struct DoubleLength;

template <> struct DoubleLength<uint16_t> {
using Type = uint32_t;
};
template <> struct DoubleLength<uint16_t> { using Type = uint32_t; };

template <> struct DoubleLength<uint32_t> {
using Type = uint64_t;
};
template <> struct DoubleLength<uint32_t> { using Type = uint64_t; };

template <> struct DoubleLength<uint64_t> {
using Type = UInt128;
Expand Down
5 changes: 3 additions & 2 deletions libc/src/__support/FPUtil/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_header_library(
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.bit
libc.src.__support.uint128
)

Expand All @@ -20,13 +21,13 @@ add_header_library(
FMA.h
DEPENDS
libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.float_properties
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.src.__support.bit
libc.src.__support.uint128
)

Expand All @@ -36,12 +37,12 @@ add_header_library(
FMod.h
DEPENDS
libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.float_properties
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.src.__support.bit
libc.src.math.generic.math_utils
)
8 changes: 4 additions & 4 deletions libc/src/__support/FPUtil/generic/FMA.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_FMA_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_FMA_H

#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/FloatProperties.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/UInt128.h"
#include "src/__support/bit.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY

Expand Down Expand Up @@ -209,9 +209,9 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
// Normalize the result.
if (prod_mant != 0) {
uint64_t prod_hi = static_cast<uint64_t>(prod_mant >> 64);
int lead_zeros =
prod_hi ? cpp::countl_zero(prod_hi)
: 64 + cpp::countl_zero(static_cast<uint64_t>(prod_mant));
int lead_zeros = prod_hi
? unsafe_clz(prod_hi)
: 64 + unsafe_clz(static_cast<uint64_t>(prod_mant));
// Move the leading 1 to the most significant bit.
prod_mant <<= lead_zeros;
// The lower 64 bits are always sticky bits after moving the leading 1 to
Expand Down
8 changes: 4 additions & 4 deletions libc/src/__support/FPUtil/generic/FMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_FMOD_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_FMOD_H

#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/bit.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
#include "src/math/generic/math_utils.h"

Expand Down Expand Up @@ -229,7 +229,7 @@ class FMod {

if (LIBC_LIKELY(sx.uintval() <= sy.uintval())) {
if (sx.uintval() < sy.uintval())
return sx; // |x|<|y| return x
return sx; // |x|<|y| return x
return FPB(FPB::zero()); // |x|=|y| return 0.0
}

Expand Down Expand Up @@ -264,11 +264,11 @@ class FMod {
e_y--;
} else {
m_y = sy.get_mantissa();
lead_zeros_m_y = cpp::countl_zero(m_y);
lead_zeros_m_y = unsafe_clz(m_y);
}

// Assume hy != 0
int tail_zeros_m_y = cpp::countr_zero(m_y);
int tail_zeros_m_y = unsafe_ctz(m_y);
int sides_zeroes_count = lead_zeros_m_y + tail_zeros_m_y;
// n > 0 by conditions above
int exp_diff = e_x - e_y;
Expand Down
13 changes: 7 additions & 6 deletions libc/src/__support/FPUtil/generic/sqrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_SQRT_H

#include "sqrt_80_bit_long_double.h"
#include "src/__support/CPP/bit.h" // countl_zero
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/UInt128.h"
#include "src/__support/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {
Expand All @@ -36,7 +37,7 @@ template <> struct SpecialLongDouble<long double> {
template <typename T>
LIBC_INLINE void normalize(int &exponent,
typename FPBits<T>::UIntType &mantissa) {
const int shift = cpp::countl_zero(mantissa) -
const int shift = unsafe_clz(mantissa) -
(8 * sizeof(mantissa) - 1 - MantissaWidth<T>::VALUE);
exponent -= shift;
mantissa <<= shift;
Expand All @@ -51,9 +52,9 @@ LIBC_INLINE void normalize<long double>(int &exponent, uint64_t &mantissa) {
template <>
LIBC_INLINE void normalize<long double>(int &exponent, UInt128 &mantissa) {
const uint64_t hi_bits = static_cast<uint64_t>(mantissa >> 64);
const int shift =
hi_bits ? (cpp::countl_zero(hi_bits) - 15)
: (cpp::countl_zero(static_cast<uint64_t>(mantissa)) + 49);
const int shift = hi_bits
? (unsafe_clz(hi_bits) - 15)
: (unsafe_clz(static_cast<uint64_t>(mantissa)) + 49);
exponent -= shift;
mantissa <<= shift;
}
Expand Down Expand Up @@ -136,7 +137,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {

// We compute one more iteration in order to round correctly.
bool lsb = static_cast<bool>(y & 1); // Least significant bit
bool rb = false; // Round bit
bool rb = false; // Round bit
r <<= 2;
UIntType tmp = (y << 2) + 1;
if (r >= tmp) {
Expand Down
6 changes: 3 additions & 3 deletions libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_SQRT_80_BIT_LONG_DOUBLE_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_SQRT_80_BIT_LONG_DOUBLE_H

#include "src/__support/CPP/bit.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/UInt128.h"
#include "src/__support/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {
Expand All @@ -22,7 +22,7 @@ namespace x86 {

LIBC_INLINE void normalize(int &exponent, UInt128 &mantissa) {
const unsigned int shift = static_cast<unsigned int>(
cpp::countl_zero(static_cast<uint64_t>(mantissa)) -
unsafe_clz(static_cast<uint64_t>(mantissa)) -
(8 * sizeof(uint64_t) - 1 - MantissaWidth<long double>::VALUE));
exponent -= shift;
mantissa <<= shift;
Expand Down Expand Up @@ -101,7 +101,7 @@ LIBC_INLINE long double sqrt(long double x) {

// We compute one more iteration in order to round correctly.
bool lsb = static_cast<bool>(y & 1); // Least significant bit
bool rb = false; // Round bit
bool rb = false; // Round bit
r <<= 2;
UIntType tmp = (y << 2) + 1;
if (r >= tmp) {
Expand Down
12 changes: 6 additions & 6 deletions libc/src/__support/HashTable/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ add_header_library(
PREFER_GENERIC
DEPENDS
libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.bit
libc.src.__support.macros.properties.cpu_features
)

Expand All @@ -25,17 +25,17 @@ add_header_library(
table.h
DEPENDS
.bitmask
libc.include.llvm-libc-types.ENTRY
libc.src.__support.CPP.bit
libc.src.__support.CPP.new
libc.src.__support.memory_size
libc.src.__support.bit
libc.src.__support.CPP.type_traits
libc.src.__support.hash
libc.src.__support.CPP.new
libc.src.__support.macros.attributes
libc.src.__support.macros.optimization
libc.src.__support.memory_size
libc.src.__support.hash
libc.src.string.memset
libc.src.string.strcmp
libc.src.string.strlen
libc.include.llvm-libc-types.ENTRY
)

add_header_library(
Expand Down
4 changes: 2 additions & 2 deletions libc/src/__support/HashTable/bitmask.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H
#define LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H

#include "src/__support/CPP/bit.h"
#include "src/__support/bit.h"
#include "src/__support/macros/properties/cpu_features.h"
#include <stddef.h> // size_t
#include <stdint.h> // uint8_t, uint64_t
Expand Down Expand Up @@ -45,7 +45,7 @@ template <typename T, T WORD_MASK, size_t WORD_STRIDE> struct BitMaskAdaptor {
// Count trailing zeros with respect to stride. (Assume the bitmask is none
// zero.)
LIBC_INLINE constexpr size_t lowest_set_bit_nonzero() const {
return cpp::countr_zero<T>(word) / WORD_STRIDE;
return unsafe_ctz<T>(word) / WORD_STRIDE;
}
};

Expand Down
7 changes: 3 additions & 4 deletions libc/src/__support/HashTable/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#define LLVM_LIBC_SRC___SUPPORT_HASHTABLE_table_H

#include "include/llvm-libc-types/ENTRY.h"
#include "src/__support/CPP/bit.h" // bit_ceil
#include "src/__support/CPP/new.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/HashTable/bitmask.h"
#include "src/__support/bit.h"
#include "src/__support/hash.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/optimization.h"
Expand Down Expand Up @@ -70,7 +70,7 @@ LIBC_INLINE size_t capacity_to_entries(size_t cap) {
if (cap < sizeof(Group))
cap = sizeof(Group);
// overflow is always checked in allocate()
return cpp::bit_ceil(cap * 8 / 7);
return next_power_of_two(cap * 8 / 7);
}

// The heap memory layout for N buckets HashTable is as follows:
Expand Down Expand Up @@ -98,8 +98,7 @@ struct HashTable {

LIBC_INLINE size_t offset_from_entries() const {
size_t entries_size = num_of_entries() * sizeof(ENTRY);
return entries_size +
SafeMemSize::offset_to(entries_size, table_alignment());
return entries_size + offset_to(entries_size, table_alignment());
}

LIBC_INLINE constexpr static size_t table_alignment() {
Expand Down
4 changes: 2 additions & 2 deletions libc/src/__support/UInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#define LLVM_LIBC_SRC___SUPPORT_UINT_H

#include "src/__support/CPP/array.h"
#include "src/__support/CPP/bit.h" // countl_zero
#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/bit.h" // unsafe_clz
#include "src/__support/integer_utils.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
Expand Down Expand Up @@ -557,7 +557,7 @@ template <size_t Bits, bool Signed> struct BigInt {
if (val[i - 1] == 0) {
leading_zeroes += sizeof(uint64_t) * 8;
} else {
leading_zeroes += countl_zero(val[i - 1]);
leading_zeroes += unsafe_clz(val[i - 1]);
break;
}
}
Expand Down

0 comments on commit 4fdb933

Please sign in to comment.