[libc-commits] [libc] [llvm] [libc][NFC] Split 'builtin_wrapper' into 'bit' and 'math_extras' (PR #72998)
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Wed Nov 22 02:29:49 PST 2023
https://github.com/gchatelet updated https://github.com/llvm/llvm-project/pull/72998
>From b4d0428276bcedc88d13357546c07274e323efa0 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Wed, 22 Nov 2023 10:22:35 +0000
Subject: [PATCH 1/3] [libc][NFC] Split builtin_wrapper into bit and
math_extras
---
libc/src/__support/CMakeLists.txt | 31 +++++---
libc/src/__support/FPUtil/CMakeLists.txt | 10 +--
libc/src/__support/FPUtil/FPBits.h | 2 +-
libc/src/__support/FPUtil/Hypot.h | 2 +-
.../__support/FPUtil/generic/CMakeLists.txt | 10 +--
libc/src/__support/FPUtil/generic/FMA.h | 2 +-
libc/src/__support/FPUtil/generic/FMod.h | 2 +-
libc/src/__support/FPUtil/generic/sqrt.h | 2 +-
.../FPUtil/generic/sqrt_80_bit_long_double.h | 2 +-
libc/src/__support/UInt.h | 50 ++++++++----
libc/src/__support/bit.h | 77 +++++++++++++++++++
libc/src/__support/integer_utils.h | 2 +-
.../{builtin_wrappers.h => math_extras.h} | 68 ++--------------
libc/src/__support/str_to_float.h | 2 +-
libc/src/math/generic/CMakeLists.txt | 10 +--
libc/src/math/generic/powf.cpp | 2 +-
libc/test/src/__support/CMakeLists.txt | 23 ++++++
libc/test/src/__support/bit_test.cpp | 14 ++++
libc/test/src/__support/math_extras_test.cpp | 16 ++++
.../llvm-project-overlay/libc/BUILD.bazel | 29 ++++---
.../libc/libc_build_rules.bzl | 2 +-
.../libc/test/src/__support/BUILD.bazel | 23 ++++++
.../test/src/math/libc_math_test_rules.bzl | 3 +-
23 files changed, 256 insertions(+), 128 deletions(-)
create mode 100644 libc/src/__support/bit.h
rename libc/src/__support/{builtin_wrappers.h => math_extras.h} (72%)
create mode 100644 libc/test/src/__support/bit_test.cpp
create mode 100644 libc/test/src/__support/math_extras_test.cpp
create mode 100644 utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 35b724b3e3df9a4..8f66209ab54c550 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -27,9 +27,17 @@ add_header_library(
)
add_header_library(
- builtin_wrappers
+ bit
HDRS
- builtin_wrappers.h
+ bit.h
+ DEPENDS
+ libc.src.__support.macros.attributes
+)
+
+add_header_library(
+ math_extras
+ HDRS
+ math_extras.h
DEPENDS
.named_pair
libc.src.__support.CPP.type_traits
@@ -98,7 +106,6 @@ add_header_library(
libc.src.__support.CPP.type_traits
)
-
add_header_library(
float_to_string
HDRS
@@ -131,18 +138,17 @@ add_header_library(
.str_to_integer
.str_to_num_result
.uint128
- libc.src.__support.CPP.optional
+ libc.src.__support.common
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.FPUtil.dyadic_float
- libc.src.__support.builtin_wrappers
- libc.src.__support.common
+ libc.src.__support.bit
libc.src.errno.errno
)
-
add_header_library(
integer_operations
HDRS
@@ -187,10 +193,10 @@ add_header_library(
HDRS
integer_utils.h
DEPENDS
- .builtin_wrappers
+ .bit
.number_pair
- libc.src.__support.CPP.type_traits
libc.src.__support.common
+ libc.src.__support.CPP.type_traits
)
add_header_library(
@@ -198,9 +204,10 @@ add_header_library(
HDRS
UInt.h
DEPENDS
- .builtin_wrappers
- .number_pair
+ .bit
.integer_utils
+ .math_extras
+ .number_pair
libc.src.__support.CPP.array
libc.src.__support.CPP.type_traits
libc.src.__support.macros.optimization
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index 4025c2a5d19a53f..bf6e39f436c8c49 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -45,12 +45,12 @@ add_header_library(
HDRS
FPBits.h
DEPENDS
- .platform_defs
.float_properties
- libc.src.__support.builtin_wrappers
+ .platform_defs
+ libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
- libc.src.__support.common
+ libc.src.__support.bit
)
add_header_library(
@@ -153,10 +153,10 @@ add_header_library(
.fenv_impl
.fp_bits
.rounding_mode
- libc.src.__support.builtin_wrappers
+ libc.src.__support.common
libc.src.__support.CPP.bit
libc.src.__support.CPP.type_traits
- libc.src.__support.common
+ libc.src.__support.bit
libc.src.__support.uint128
)
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 37e9bc9cfc84c3a..0db90d1e36df0c2 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -13,7 +13,7 @@
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/type_traits.h"
-#include "src/__support/builtin_wrappers.h"
+#include "src/__support/bit.h"
#include "src/__support/common.h"
#include "FloatProperties.h"
diff --git a/libc/src/__support/FPUtil/Hypot.h b/libc/src/__support/FPUtil/Hypot.h
index 357d9a6e99c7457..db2a62fbdf2a107 100644
--- a/libc/src/__support/FPUtil/Hypot.h
+++ b/libc/src/__support/FPUtil/Hypot.h
@@ -16,7 +16,7 @@
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/UInt128.h"
-#include "src/__support/builtin_wrappers.h"
+#include "src/__support/bit.h"
#include "src/__support/common.h"
namespace LIBC_NAMESPACE {
diff --git a/libc/src/__support/FPUtil/generic/CMakeLists.txt b/libc/src/__support/FPUtil/generic/CMakeLists.txt
index 7f986d05adedf92..cf603e7987e1d68 100644
--- a/libc/src/__support/FPUtil/generic/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/generic/CMakeLists.txt
@@ -4,16 +4,16 @@ add_header_library(
sqrt.h
sqrt_80_bit_long_double.h
DEPENDS
+ libc.include.fenv
+ 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.fp_bits
libc.src.__support.FPUtil.platform_defs
libc.src.__support.FPUtil.rounding_mode
- libc.src.__support.builtin_wrappers
- libc.src.__support.common
+ libc.src.__support.bit
libc.src.__support.uint128
- libc.include.fenv
)
add_header_library(
@@ -27,8 +27,8 @@ add_header_library(
libc.src.__support.FPUtil.float_properties
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
- libc.src.__support.builtin_wrappers
libc.src.__support.macros.optimization
+ libc.src.__support.bit
libc.src.__support.uint128
)
@@ -43,7 +43,7 @@ add_header_library(
libc.src.__support.FPUtil.float_properties
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
- libc.src.__support.builtin_wrappers
libc.src.__support.macros.optimization
+ libc.src.__support.bit
libc.src.math.generic.math_utils
)
diff --git a/libc/src/__support/FPUtil/generic/FMA.h b/libc/src/__support/FPUtil/generic/FMA.h
index b90b134926bb649..5a856d144733041 100644
--- a/libc/src/__support/FPUtil/generic/FMA.h
+++ b/libc/src/__support/FPUtil/generic/FMA.h
@@ -15,7 +15,7 @@
#include "src/__support/FPUtil/FloatProperties.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/UInt128.h"
-#include "src/__support/builtin_wrappers.h"
+#include "src/__support/bit.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
diff --git a/libc/src/__support/FPUtil/generic/FMod.h b/libc/src/__support/FPUtil/generic/FMod.h
index ff320f36ee2277b..6f934cd4a87bd4a 100644
--- a/libc/src/__support/FPUtil/generic/FMod.h
+++ b/libc/src/__support/FPUtil/generic/FMod.h
@@ -13,7 +13,7 @@
#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/builtin_wrappers.h"
+#include "src/__support/bit.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
#include "src/math/generic/math_utils.h"
diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h
index 7d446d3a5ffb135..db9f4b11eb17c59 100644
--- a/libc/src/__support/FPUtil/generic/sqrt.h
+++ b/libc/src/__support/FPUtil/generic/sqrt.h
@@ -17,7 +17,7 @@
#include "src/__support/FPUtil/PlatformDefs.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/UInt128.h"
-#include "src/__support/builtin_wrappers.h"
+#include "src/__support/bit.h"
#include "src/__support/common.h"
namespace LIBC_NAMESPACE {
diff --git a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h
index 685a90dba7c704b..dbbb238ca24d1b8 100644
--- a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h
+++ b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h
@@ -14,7 +14,7 @@
#include "src/__support/FPUtil/PlatformDefs.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/UInt128.h"
-#include "src/__support/builtin_wrappers.h"
+#include "src/__support/bit.h"
#include "src/__support/common.h"
namespace LIBC_NAMESPACE {
diff --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index 9aeb239b8328bea..f46e31ae03db291 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -13,9 +13,10 @@
#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/type_traits.h"
-#include "src/__support/builtin_wrappers.h"
+#include "src/__support/bit.h" // unsafe_clz
#include "src/__support/integer_utils.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/math_extras.h" // SumCarry, DiffBorrow
#include "src/__support/number_pair.h"
#include <stddef.h> // For size_t
@@ -33,7 +34,9 @@ template <size_t Bits, bool Signed> struct BigInt {
static LIBC_INLINE_VAR constexpr uint64_t MASK32 = 0xFFFFFFFFu;
static LIBC_INLINE constexpr uint64_t low(uint64_t v) { return v & MASK32; }
- static LIBC_INLINE constexpr uint64_t high(uint64_t v) { return (v >> 32) & MASK32; }
+ static LIBC_INLINE constexpr uint64_t high(uint64_t v) {
+ return (v >> 32) & MASK32;
+ }
LIBC_INLINE constexpr BigInt() = default;
@@ -93,7 +96,8 @@ template <size_t Bits, bool Signed> struct BigInt {
}
}
- LIBC_INLINE constexpr explicit BigInt(const cpp::array<uint64_t, WORDCOUNT> &words) {
+ LIBC_INLINE constexpr explicit BigInt(
+ const cpp::array<uint64_t, WORDCOUNT> &words) {
for (size_t i = 0; i < WORDCOUNT; ++i)
val[i] = words[i];
}
@@ -143,7 +147,8 @@ template <size_t Bits, bool Signed> struct BigInt {
return s.carry;
}
- LIBC_INLINE constexpr BigInt<Bits, Signed> operator+(const BigInt<Bits, Signed> &other) const {
+ LIBC_INLINE constexpr BigInt<Bits, Signed>
+ operator+(const BigInt<Bits, Signed> &other) const {
BigInt<Bits, Signed> result;
SumCarry<uint64_t> s{0, 0};
for (size_t i = 0; i < WORDCOUNT; ++i) {
@@ -155,7 +160,8 @@ template <size_t Bits, bool Signed> struct BigInt {
// This will only apply when initializing a variable from constant values, so
// it will always use the constexpr version of add_with_carry.
- LIBC_INLINE constexpr BigInt<Bits, Signed> operator+(BigInt<Bits, Signed> &&other) const {
+ LIBC_INLINE constexpr BigInt<Bits, Signed>
+ operator+(BigInt<Bits, Signed> &&other) const {
BigInt<Bits, Signed> result;
SumCarry<uint64_t> s{0, 0};
for (size_t i = 0; i < WORDCOUNT; ++i) {
@@ -182,7 +188,8 @@ template <size_t Bits, bool Signed> struct BigInt {
return d.borrow;
}
- LIBC_INLINE BigInt<Bits, Signed> operator-(const BigInt<Bits, Signed> &other) const {
+ LIBC_INLINE BigInt<Bits, Signed>
+ operator-(const BigInt<Bits, Signed> &other) const {
BigInt<Bits, Signed> result;
DiffBorrow<uint64_t> d{0, 0};
for (size_t i = 0; i < WORDCOUNT; ++i) {
@@ -192,7 +199,8 @@ template <size_t Bits, bool Signed> struct BigInt {
return result;
}
- LIBC_INLINE constexpr BigInt<Bits, Signed> operator-(BigInt<Bits, Signed> &&other) const {
+ LIBC_INLINE constexpr BigInt<Bits, Signed>
+ operator-(BigInt<Bits, Signed> &&other) const {
BigInt<Bits, Signed> result;
DiffBorrow<uint64_t> d{0, 0};
for (size_t i = 0; i < WORDCOUNT; ++i) {
@@ -317,7 +325,7 @@ template <size_t Bits, bool Signed> struct BigInt {
// 256 4 16 10 3
// 512 8 64 36 7
constexpr BigInt<Bits, Signed>
- LIBC_INLINE quick_mul_hi(const BigInt<Bits, Signed> &other) const {
+ LIBC_INLINE quick_mul_hi(const BigInt<Bits, Signed> &other) const {
BigInt<Bits, Signed> result(0);
BigInt<128, Signed> partial_sum(0);
uint64_t carry = 0;
@@ -407,8 +415,8 @@ template <size_t Bits, bool Signed> struct BigInt {
// Since the remainder of each division step < x < 2^32, the computation of
// each step is now properly contained within uint64_t.
// And finally we perform some extra alignment steps for the remaining bits.
- LIBC_INLINE constexpr optional<BigInt<Bits, Signed>> div_uint32_times_pow_2(uint32_t x,
- size_t e) {
+ LIBC_INLINE constexpr optional<BigInt<Bits, Signed>>
+ div_uint32_times_pow_2(uint32_t x, size_t e) {
BigInt<Bits, Signed> remainder(0);
if (x == 0) {
@@ -729,7 +737,8 @@ template <size_t Bits, bool Signed> struct BigInt {
return result;
}
- LIBC_INLINE constexpr bool operator==(const BigInt<Bits, Signed> &other) const {
+ LIBC_INLINE constexpr bool
+ operator==(const BigInt<Bits, Signed> &other) const {
for (size_t i = 0; i < WORDCOUNT; ++i) {
if (val[i] != other.val[i])
return false;
@@ -737,7 +746,8 @@ template <size_t Bits, bool Signed> struct BigInt {
return true;
}
- LIBC_INLINE constexpr bool operator!=(const BigInt<Bits, Signed> &other) const {
+ LIBC_INLINE constexpr bool
+ operator!=(const BigInt<Bits, Signed> &other) const {
for (size_t i = 0; i < WORDCOUNT; ++i) {
if (val[i] != other.val[i])
return true;
@@ -745,7 +755,8 @@ template <size_t Bits, bool Signed> struct BigInt {
return false;
}
- LIBC_INLINE constexpr bool operator>(const BigInt<Bits, Signed> &other) const {
+ LIBC_INLINE constexpr bool
+ operator>(const BigInt<Bits, Signed> &other) const {
if constexpr (Signed) {
// Check for different signs;
bool a_sign = val[WORDCOUNT - 1] >> 63;
@@ -766,7 +777,8 @@ template <size_t Bits, bool Signed> struct BigInt {
return false;
}
- LIBC_INLINE constexpr bool operator>=(const BigInt<Bits, Signed> &other) const {
+ LIBC_INLINE constexpr bool
+ operator>=(const BigInt<Bits, Signed> &other) const {
if constexpr (Signed) {
// Check for different signs;
bool a_sign = val[WORDCOUNT - 1] >> 63;
@@ -787,7 +799,8 @@ template <size_t Bits, bool Signed> struct BigInt {
return true;
}
- LIBC_INLINE constexpr bool operator<(const BigInt<Bits, Signed> &other) const {
+ LIBC_INLINE constexpr bool
+ operator<(const BigInt<Bits, Signed> &other) const {
if constexpr (Signed) {
// Check for different signs;
bool a_sign = val[WORDCOUNT - 1] >> 63;
@@ -809,7 +822,8 @@ template <size_t Bits, bool Signed> struct BigInt {
return false;
}
- LIBC_INLINE constexpr bool operator<=(const BigInt<Bits, Signed> &other) const {
+ LIBC_INLINE constexpr bool
+ operator<=(const BigInt<Bits, Signed> &other) const {
if constexpr (Signed) {
// Check for different signs;
bool a_sign = val[WORDCOUNT - 1] >> 63;
@@ -857,7 +871,9 @@ template <size_t Bits, bool Signed> struct BigInt {
}
// Return the i-th 64-bit word of the number.
- LIBC_INLINE constexpr const uint64_t &operator[](size_t i) const { return val[i]; }
+ LIBC_INLINE constexpr const uint64_t &operator[](size_t i) const {
+ return val[i];
+ }
// Return the i-th 64-bit word of the number.
LIBC_INLINE constexpr uint64_t &operator[](size_t i) { return val[i]; }
diff --git a/libc/src/__support/bit.h b/libc/src/__support/bit.h
new file mode 100644
index 000000000000000..d0a15c89b7b45e7
--- /dev/null
+++ b/libc/src/__support/bit.h
@@ -0,0 +1,77 @@
+//===-- Mimics llvm/ADT/Bit.h -----------------------------------*- C++ -*-===//
+// Provides useful bit functions.
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_BIT_H
+#define LLVM_LIBC_SRC___SUPPORT_BIT_H
+
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
+
+namespace LIBC_NAMESPACE {
+
+// The following overloads are matched based on what is accepted by
+// __builtin_clz/ctz* rather than using the exactly-sized aliases from stdint.h.
+// This way, we can avoid making any assumptions about integer sizes and let the
+// compiler match for us.
+namespace __internal {
+
+template <typename T> LIBC_INLINE int constexpr correct_zero(T val, int bits) {
+ if (val == T(0))
+ return sizeof(T(0)) * 8;
+ else
+ return bits;
+}
+
+template <typename T> LIBC_INLINE constexpr int clz(T val);
+template <> LIBC_INLINE int clz<unsigned int>(unsigned int val) {
+ return __builtin_clz(val);
+}
+template <>
+LIBC_INLINE constexpr int clz<unsigned long int>(unsigned long int val) {
+ return __builtin_clzl(val);
+}
+template <>
+LIBC_INLINE constexpr int
+clz<unsigned long long int>(unsigned long long int val) {
+ return __builtin_clzll(val);
+}
+
+template <typename T> LIBC_INLINE constexpr int ctz(T val);
+template <> LIBC_INLINE int ctz<unsigned int>(unsigned int val) {
+ return __builtin_ctz(val);
+}
+template <>
+LIBC_INLINE constexpr int ctz<unsigned long int>(unsigned long int val) {
+ return __builtin_ctzl(val);
+}
+template <>
+LIBC_INLINE constexpr int
+ctz<unsigned long long int>(unsigned long long int val) {
+ return __builtin_ctzll(val);
+}
+} // namespace __internal
+
+template <typename T> LIBC_INLINE constexpr int safe_ctz(T val) {
+ return __internal::correct_zero(val, __internal::ctz(val));
+}
+
+template <typename T> LIBC_INLINE constexpr int unsafe_ctz(T val) {
+ return __internal::ctz(val);
+}
+
+template <typename T> LIBC_INLINE constexpr int safe_clz(T val) {
+ return __internal::correct_zero(val, __internal::clz(val));
+}
+
+template <typename T> LIBC_INLINE constexpr int unsafe_clz(T val) {
+ return __internal::clz(val);
+}
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC___SUPPORT_BIT_H
diff --git a/libc/src/__support/integer_utils.h b/libc/src/__support/integer_utils.h
index 7b62cb0d9f50593..433e99227bcfd4a 100644
--- a/libc/src/__support/integer_utils.h
+++ b/libc/src/__support/integer_utils.h
@@ -12,7 +12,7 @@
#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"
-#include "builtin_wrappers.h"
+#include "bit.h"
#include "number_pair.h"
#include <stdint.h>
diff --git a/libc/src/__support/builtin_wrappers.h b/libc/src/__support/math_extras.h
similarity index 72%
rename from libc/src/__support/builtin_wrappers.h
rename to libc/src/__support/math_extras.h
index bd307a3544cd930..cc22aa49d02601b 100644
--- a/libc/src/__support/builtin_wrappers.h
+++ b/libc/src/__support/math_extras.h
@@ -1,5 +1,5 @@
-//===--Convenient template for builtins -------------------------*- C++ -*-===//
-// (Count Lead Zeroes) and (Count Trailing Zeros)
+//===-- Mimics llvm/Support/MathExtras.h ------------------------*- C++ -*-===//
+// Provides useful math functions.
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -7,8 +7,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC___SUPPORT_BUILTIN_WRAPPERS_H
-#define LLVM_LIBC_SRC___SUPPORT_BUILTIN_WRAPPERS_H
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
#include "named_pair.h"
#include "src/__support/CPP/type_traits.h"
@@ -17,64 +17,6 @@
namespace LIBC_NAMESPACE {
-// The following overloads are matched based on what is accepted by
-// __builtin_clz/ctz* rather than using the exactly-sized aliases from stdint.h.
-// This way, we can avoid making any assumptions about integer sizes and let the
-// compiler match for us.
-namespace __internal {
-
-template <typename T> LIBC_INLINE int constexpr correct_zero(T val, int bits) {
- if (val == T(0))
- return sizeof(T(0)) * 8;
- else
- return bits;
-}
-
-template <typename T> LIBC_INLINE constexpr int clz(T val);
-template <> LIBC_INLINE int clz<unsigned int>(unsigned int val) {
- return __builtin_clz(val);
-}
-template <>
-LIBC_INLINE constexpr int clz<unsigned long int>(unsigned long int val) {
- return __builtin_clzl(val);
-}
-template <>
-LIBC_INLINE constexpr int
-clz<unsigned long long int>(unsigned long long int val) {
- return __builtin_clzll(val);
-}
-
-template <typename T> LIBC_INLINE constexpr int ctz(T val);
-template <> LIBC_INLINE int ctz<unsigned int>(unsigned int val) {
- return __builtin_ctz(val);
-}
-template <>
-LIBC_INLINE constexpr int ctz<unsigned long int>(unsigned long int val) {
- return __builtin_ctzl(val);
-}
-template <>
-LIBC_INLINE constexpr int
-ctz<unsigned long long int>(unsigned long long int val) {
- return __builtin_ctzll(val);
-}
-} // namespace __internal
-
-template <typename T> LIBC_INLINE constexpr int safe_ctz(T val) {
- return __internal::correct_zero(val, __internal::ctz(val));
-}
-
-template <typename T> LIBC_INLINE constexpr int unsafe_ctz(T val) {
- return __internal::ctz(val);
-}
-
-template <typename T> LIBC_INLINE constexpr int safe_clz(T val) {
- return __internal::correct_zero(val, __internal::clz(val));
-}
-
-template <typename T> LIBC_INLINE constexpr int unsafe_clz(T val) {
- return __internal::clz(val);
-}
-
// Add with carry
DEFINE_NAMED_PAIR_TEMPLATE(SumCarry, sum, carry);
@@ -223,4 +165,4 @@ sub_with_borrow<unsigned long long>(unsigned long long a, unsigned long long b,
} // namespace LIBC_NAMESPACE
-#endif // LLVM_LIBC_SRC___SUPPORT_BUILTIN_WRAPPERS_H
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h
index e827e3322fac11f..81ab36dbf9471fb 100644
--- a/libc/src/__support/str_to_float.h
+++ b/libc/src/__support/str_to_float.h
@@ -16,7 +16,7 @@
#include "src/__support/FPUtil/dyadic_float.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/UInt128.h"
-#include "src/__support/builtin_wrappers.h"
+#include "src/__support/bit.h"
#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/detailed_powers_of_ten.h"
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 413ac049a22b840..3536efa5e4fd5e0 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -775,10 +775,11 @@ add_entrypoint_object(
../powf.h
DEPENDS
.common_constants
- .explogxf
- .exp2f_impl
.exp10f_impl
- libc.src.__support.builtin_wrappers
+ .exp2f_impl
+ .explogxf
+ libc.include.errno
+ libc.include.math
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.fenv_impl
@@ -790,9 +791,8 @@ add_entrypoint_object(
libc.src.__support.FPUtil.sqrt
libc.src.__support.FPUtil.triple_double
libc.src.__support.macros.optimization
- libc.include.errno
+ libc.src.__support.bit
libc.src.errno.errno
- libc.include.math
COMPILE_OPTIONS
-O3
)
diff --git a/libc/src/math/generic/powf.cpp b/libc/src/math/generic/powf.cpp
index 891b09c69baed6d..2de8e76b04b5ae0 100644
--- a/libc/src/math/generic/powf.cpp
+++ b/libc/src/math/generic/powf.cpp
@@ -18,7 +18,7 @@
#include "src/__support/FPUtil/nearest_integer.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/FPUtil/sqrt.h" // Speedup for powf(x, 1/2) = sqrtf(x)
-#include "src/__support/builtin_wrappers.h"
+#include "src/__support/bit.h"
#include "src/__support/common.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index c899d9bd1fcd069..2920535fbaa56e7 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -23,6 +23,29 @@ add_libc_test(
libc.src.__support.common
)
+
+add_libc_test(
+ bit_test
+ SUITE
+ libc-support-tests
+ SRCS
+ bit_test.cpp
+ DEPENDS
+ libc.src.__support.bit
+)
+
+
+add_libc_test(
+ math_extras_test
+ SUITE
+ libc-support-tests
+ SRCS
+ math_extras_test.cpp
+ DEPENDS
+ libc.src.__support.math_extras
+)
+
+
add_libc_test(
high_precision_decimal_test
SUITE
diff --git a/libc/test/src/__support/bit_test.cpp b/libc/test/src/__support/bit_test.cpp
new file mode 100644
index 000000000000000..c8c12f4d68357d5
--- /dev/null
+++ b/libc/test/src/__support/bit_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for BlockStore ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/bit.h"
+#include "test/UnitTest/Test.h"
+
+namespace LIBC_NAMESPACE {
+TEST(LlvmLibcBlockBitTest, TODO) {}
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/__support/math_extras_test.cpp b/libc/test/src/__support/math_extras_test.cpp
new file mode 100644
index 000000000000000..7d3f6be524f7ee3
--- /dev/null
+++ b/libc/test/src/__support/math_extras_test.cpp
@@ -0,0 +1,16 @@
+//===-- Unittests for math_extras -----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/math_extras.h"
+#include "test/UnitTest/Test.h"
+
+namespace LIBC_NAMESPACE {
+
+TEST(LlvmLibcBlockMathExtrasTest, TODO) {}
+
+} // namespace LIBC_NAMESPACE
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c89c1f7950b974e..98e980cf3cb09bb 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -394,7 +394,7 @@ libc_support_library(
name = "__support_integer_utils",
hdrs = ["src/__support/integer_utils.h"],
deps = [
- ":__support_builtin_wrappers",
+ ":__support_bit",
":__support_common",
":__support_cpp_type_traits",
":__support_number_pair",
@@ -405,7 +405,7 @@ libc_support_library(
name = "__support_uint",
hdrs = ["src/__support/UInt.h"],
deps = [
- ":__support_builtin_wrappers",
+ ":__support_bit",
":__support_cpp_array",
":__support_cpp_limits",
":__support_cpp_optional",
@@ -413,6 +413,7 @@ libc_support_library(
":__support_integer_utils",
":__support_macros_attributes",
":__support_macros_optimization",
+ ":__support_math_extras",
":__support_number_pair",
],
)
@@ -491,7 +492,7 @@ libc_support_library(
"src/__support/str_to_float.h",
],
deps = [
- ":__support_builtin_wrappers",
+ ":__support_bit",
":__support_common",
":__support_cpp_limits",
":__support_cpp_optional",
@@ -547,8 +548,16 @@ libc_support_library(
)
libc_support_library(
- name = "__support_builtin_wrappers",
- hdrs = ["src/__support/builtin_wrappers.h"],
+ name = "__support_bit",
+ hdrs = ["src/__support/bit.h"],
+ deps = [
+ ":__support_macros_attributes",
+ ],
+)
+
+libc_support_library(
+ name = "__support_math_extras",
+ hdrs = ["src/__support/math_extras.h"],
deps = [
":__support_cpp_type_traits",
":__support_macros_attributes",
@@ -561,7 +570,7 @@ libc_support_library(
name = "__support_fputil_generic_fmod",
hdrs = ["src/__support/FPUtil/generic/FMod.h"],
deps = [
- ":__support_builtin_wrappers",
+ ":__support_bit",
":__support_common",
":__support_cpp_limits",
":__support_cpp_type_traits",
@@ -633,7 +642,7 @@ libc_support_library(
hdrs = ["src/__support/FPUtil/FPBits.h"],
textual_hdrs = ["src/__support/FPUtil/x86_64/LongDoubleBits.h"],
deps = [
- ":__support_builtin_wrappers",
+ ":__support_bit",
":__support_common",
":__support_cpp_bit",
":__support_cpp_type_traits",
@@ -662,7 +671,7 @@ libc_support_library(
name = "__support_fputil_hypot",
hdrs = ["src/__support/FPUtil/Hypot.h"],
deps = [
- ":__support_builtin_wrappers",
+ ":__support_bit",
":__support_common",
":__support_cpp_bit",
":__support_cpp_type_traits",
@@ -741,7 +750,7 @@ libc_support_library(
name = "__support_fputil_sqrt",
hdrs = sqrt_hdrs,
deps = [
- ":__support_builtin_wrappers",
+ ":__support_bit",
":__support_common",
":__support_cpp_bit",
":__support_cpp_type_traits",
@@ -770,7 +779,7 @@ libc_support_library(
# doesn't support FMA, so they can't be compiled on their own.
textual_hdrs = fma_platform_hdrs,
deps = [
- ":__support_builtin_wrappers",
+ ":__support_bit",
":__support_cpp_bit",
":__support_cpp_type_traits",
":__support_fputil_fenv_impl",
diff --git a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
index 1abda0deb7d6461..d93f089ac9ed357 100644
--- a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
@@ -145,7 +145,7 @@ def libc_math_function(
":__support_fputil_nearest_integer_operations",
":__support_fputil_normal_float",
":__support_fputil_platform_defs",
- ":__support_builtin_wrappers",
+ ":__support_math_extras",
":__support_fputil_except_value_utils",
]
libc_function(
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel
new file mode 100644
index 000000000000000..92fc9604e029783
--- /dev/null
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel
@@ -0,0 +1,23 @@
+# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+# Tests for LLVM libc __support functions.
+
+load("//libc/test:libc_test_rules.bzl", "libc_test")
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])
+
+libc_test(
+ name = "bit_test",
+ srcs = ["bit_test.cpp"],
+ deps = ["//libc:__support_bit"],
+)
+
+libc_test(
+ name = "math_extras_test",
+ srcs = ["math_extras_test.cpp"],
+ deps = ["//libc:__support_math_extras"],
+)
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/libc_math_test_rules.bzl b/utils/bazel/llvm-project-overlay/libc/test/src/math/libc_math_test_rules.bzl
index 2d759b5ec0f7c59..2843270d414a7d2 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/libc_math_test_rules.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/libc_math_test_rules.bzl
@@ -25,7 +25,7 @@ def math_test(name, hdrs = [], deps = [], **kwargs):
srcs = [test_name + ".cpp"] + hdrs,
libc_function_deps = ["//libc:func_name".replace("func_name", name)],
deps = [
- "//libc:__support_builtin_wrappers",
+ "//libc:__support_bit",
"//libc:__support_fputil_basic_operations",
"//libc:__support_fputil_fenv_impl",
"//libc:__support_fputil_float_properties",
@@ -33,6 +33,7 @@ def math_test(name, hdrs = [], deps = [], **kwargs):
"//libc:__support_fputil_manipulation_functions",
"//libc:__support_fputil_nearest_integer_operations",
"//libc:__support_fputil_normal_float",
+ "//libc:__support_math_extras",
"//libc:__support_uint128",
"//libc/test/UnitTest:fp_test_helpers",
] + deps,
>From 3b1bf61a39fcb540c721b189526de1bf17e19fe5 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Wed, 22 Nov 2023 10:25:56 +0000
Subject: [PATCH 2/3] formatting nits
---
libc/src/math/generic/CMakeLists.txt | 2 +-
libc/test/src/__support/bit_test.cpp | 6 +++++-
libc/test/src/__support/math_extras_test.cpp | 4 +++-
3 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3536efa5e4fd5e0..538050221082817 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -780,6 +780,7 @@ add_entrypoint_object(
.explogxf
libc.include.errno
libc.include.math
+ libc.src.__support.bit
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.fenv_impl
@@ -791,7 +792,6 @@ add_entrypoint_object(
libc.src.__support.FPUtil.sqrt
libc.src.__support.FPUtil.triple_double
libc.src.__support.macros.optimization
- libc.src.__support.bit
libc.src.errno.errno
COMPILE_OPTIONS
-O3
diff --git a/libc/test/src/__support/bit_test.cpp b/libc/test/src/__support/bit_test.cpp
index c8c12f4d68357d5..196e5b15b112e70 100644
--- a/libc/test/src/__support/bit_test.cpp
+++ b/libc/test/src/__support/bit_test.cpp
@@ -10,5 +10,9 @@
#include "test/UnitTest/Test.h"
namespace LIBC_NAMESPACE {
-TEST(LlvmLibcBlockBitTest, TODO) {}
+
+TEST(LlvmLibcBlockBitTest, TODO) {
+ // TODO Implement me.
+}
+
} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/__support/math_extras_test.cpp b/libc/test/src/__support/math_extras_test.cpp
index 7d3f6be524f7ee3..48c8fe95c689a1e 100644
--- a/libc/test/src/__support/math_extras_test.cpp
+++ b/libc/test/src/__support/math_extras_test.cpp
@@ -11,6 +11,8 @@
namespace LIBC_NAMESPACE {
-TEST(LlvmLibcBlockMathExtrasTest, TODO) {}
+TEST(LlvmLibcBlockMathExtrasTest, TODO) {
+ // TODO Implement me.
+}
} // namespace LIBC_NAMESPACE
>From 0da2365d971990af97d6c49c3a2711071208e0a6 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Wed, 22 Nov 2023 10:29:31 +0000
Subject: [PATCH 3/3] Fix indentation
---
libc/test/src/__support/bit_test.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/__support/bit_test.cpp b/libc/test/src/__support/bit_test.cpp
index 196e5b15b112e70..09d9c2f0a4ed88a 100644
--- a/libc/test/src/__support/bit_test.cpp
+++ b/libc/test/src/__support/bit_test.cpp
@@ -12,7 +12,7 @@
namespace LIBC_NAMESPACE {
TEST(LlvmLibcBlockBitTest, TODO) {
- // TODO Implement me.
+ // TODO Implement me.
}
} // namespace LIBC_NAMESPACE
More information about the libc-commits
mailing list