128 changes: 64 additions & 64 deletions libc/src/__support/UInt.h

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions libc/src/__support/threads/linux/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ LIBC_INLINE ErrorOr<void *> alloc_stack(size_t stacksize, size_t guardsize) {
// This must always be inlined as we may be freeing the calling threads stack in
// which case a normal return from the top the stack would cause an invalid
// memory read.
static __attribute__((always_inline)) inline void
[[gnu::always_inline]] LIBC_INLINE void
free_stack(void *stack, size_t stacksize, size_t guardsize) {
uintptr_t stackaddr = reinterpret_cast<uintptr_t>(stack);
stackaddr -= guardsize;
Expand All @@ -144,7 +144,7 @@ struct alignas(STACK_ALIGNMENT) StartArgs {
// This must always be inlined as we may be freeing the calling threads stack in
// which case a normal return from the top the stack would cause an invalid
// memory read.
static __attribute__((always_inline)) inline void
[[gnu::always_inline]] LIBC_INLINE void
cleanup_thread_resources(ThreadAttributes *attrib) {
// Cleanup the TLS before the stack as the TLS information is stored on
// the stack.
Expand All @@ -153,7 +153,7 @@ cleanup_thread_resources(ThreadAttributes *attrib) {
free_stack(attrib->stack, attrib->stacksize, attrib->guardsize);
}

__attribute__((always_inline)) inline uintptr_t get_start_args_addr() {
[[gnu::always_inline]] LIBC_INLINE uintptr_t get_start_args_addr() {
// NOTE: For __builtin_frame_address to work reliably across compilers,
// architectures and various optimization levels, the TU including this file
// should be compiled with -fno-omit-frame-pointer.
Expand All @@ -176,7 +176,7 @@ __attribute__((always_inline)) inline uintptr_t get_start_args_addr() {
#endif
}

__attribute__((noinline)) static void start_thread() {
[[gnu::noinline]] LIBC_INLINE void start_thread() {
auto *start_args = reinterpret_cast<StartArgs *>(get_start_args_addr());
auto *attrib = start_args->thread_attrib;
self.attrib = attrib;
Expand Down
3 changes: 2 additions & 1 deletion libc/src/stdio/printf_core/write_int_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
namespace __llvm_libc {
namespace printf_core {

int inline convert_write_int(Writer *writer, const FormatSection &to_conv) {
LIBC_INLINE int convert_write_int(Writer *writer,
const FormatSection &to_conv) {

// This is an additional check added by LLVM-libc. The reason it returns -3 is
// because printf uses negative return values for errors, and -1 and -2 are
Expand Down
4 changes: 2 additions & 2 deletions libc/src/string/memory_utils/op_aarch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace __llvm_libc::aarch64 {

static inline constexpr bool kNeon = LLVM_LIBC_IS_DEFINED(__ARM_NEON);
LIBC_INLINE_VAR constexpr bool kNeon = LLVM_LIBC_IS_DEFINED(__ARM_NEON);

namespace neon {

Expand All @@ -52,7 +52,7 @@ struct BzeroCacheLine {
}
};

LIBC_INLINE static bool hasZva() {
LIBC_INLINE bool hasZva() {
uint64_t zva_val;
asm("mrs %[zva_val], dczid_el0" : [zva_val] "=r"(zva_val));
// DC ZVA is permitted if DZP, bit [4] is zero.
Expand Down
13 changes: 7 additions & 6 deletions libc/src/string/memory_utils/op_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,24 +318,24 @@ template <typename T> struct Memmove {
// Same as load above but with an offset to the pointer.
// Making the offset explicit hints the compiler to use relevant addressing mode
// consistently.
template <typename T> LIBC_INLINE static T load(CPtr ptr, size_t offset) {
template <typename T> LIBC_INLINE T load(CPtr ptr, size_t offset) {
return ::__llvm_libc::load<T>(ptr + offset);
}

// Same as above but also makes sure the loaded value is in big endian format.
// This is useful when implementing lexicograhic comparisons as big endian
// scalar comparison directly maps to lexicographic byte comparisons.
template <typename T> LIBC_INLINE static T load_be(CPtr ptr, size_t offset) {
template <typename T> LIBC_INLINE T load_be(CPtr ptr, size_t offset) {
return Endian::to_big_endian(load<T>(ptr, offset));
}

// Equality: returns true iff values at locations (p1 + offset) and (p2 +
// offset) compare equal.
template <typename T> static bool eq(CPtr p1, CPtr p2, size_t offset);
template <typename T> LIBC_INLINE bool eq(CPtr p1, CPtr p2, size_t offset);

// Not equals: returns non-zero iff values at locations (p1 + offset) and (p2 +
// offset) differ.
template <typename T> static uint32_t neq(CPtr p1, CPtr p2, size_t offset);
template <typename T> LIBC_INLINE uint32_t neq(CPtr p1, CPtr p2, size_t offset);

// Lexicographic comparison:
// - returns 0 iff values at locations (p1 + offset) and (p2 + offset) compare
Expand All @@ -345,15 +345,15 @@ template <typename T> static uint32_t neq(CPtr p1, CPtr p2, size_t offset);
// - returns a positive value if value at location (p1 + offset) is
// lexicographically greater than value at (p2 + offset).
template <typename T>
static MemcmpReturnType cmp(CPtr p1, CPtr p2, size_t offset);
LIBC_INLINE MemcmpReturnType cmp(CPtr p1, CPtr p2, size_t offset);

// Lexicographic comparison of non-equal values:
// - returns a negative value if value at location (p1 + offset) is
// lexicographically less than value at (p2 + offset).
// - returns a positive value if value at location (p1 + offset) is
// lexicographically greater than value at (p2 + offset).
template <typename T>
static MemcmpReturnType cmp_neq(CPtr p1, CPtr p2, size_t offset);
LIBC_INLINE MemcmpReturnType cmp_neq(CPtr p1, CPtr p2, size_t offset);

///////////////////////////////////////////////////////////////////////////////
// Memcmp implementation
Expand Down Expand Up @@ -563,6 +563,7 @@ LIBC_INLINE MemcmpReturnType cmp<uint8_t>(CPtr p1, CPtr p2, size_t offset) {
}
template <>
LIBC_INLINE MemcmpReturnType cmp_neq<uint8_t>(CPtr p1, CPtr p2, size_t offset);

} // namespace __llvm_libc::generic

#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_OP_GENERIC_H
2 changes: 1 addition & 1 deletion libc/src/string/memory_utils/op_x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ LIBC_INLINE_VAR constexpr bool kAvx512BW = LLVM_LIBC_IS_DEFINED(__AVX512BW__);
///////////////////////////////////////////////////////////////////////////////
// Memcpy repmovsb implementation
struct Memcpy {
static void repmovsb(void *dst, const void *src, size_t count) {
LIBC_INLINE static void repmovsb(void *dst, const void *src, size_t count) {
asm volatile("rep movsb" : "+D"(dst), "+S"(src), "+c"(count) : : "memory");
}
};
Expand Down
9 changes: 5 additions & 4 deletions libc/src/string/memory_utils/strcmp_implementations.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace __llvm_libc {

template <typename Comp>
constexpr static int strcmp_implementation(const char *left, const char *right,
Comp &&comp) {
LIBC_INLINE constexpr int
strcmp_implementation(const char *left, const char *right, Comp &&comp) {
// TODO: Look at benefits for comparing words at a time.
for (; *left && !comp(*left, *right); ++left, ++right)
;
Expand All @@ -24,8 +24,9 @@ constexpr static int strcmp_implementation(const char *left, const char *right,
}

template <typename Comp>
constexpr static int strncmp_implementation(const char *left, const char *right,
size_t n, Comp &&comp) {
LIBC_INLINE constexpr int strncmp_implementation(const char *left,
const char *right, size_t n,
Comp &&comp) {
if (n == 0)
return 0;

Expand Down
4 changes: 2 additions & 2 deletions libc/src/string/memory_utils/strstr_implementations.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
namespace __llvm_libc {

template <typename Comp>
constexpr static char *strstr_implementation(const char *haystack,
const char *needle, Comp &&comp) {
LIBC_INLINE constexpr char *
strstr_implementation(const char *haystack, const char *needle, Comp &&comp) {
void *result = memmem_implementation(
static_cast<const void *>(haystack), internal::string_length(haystack),
static_cast<const void *>(needle), internal::string_length(needle), comp);
Expand Down
23 changes: 13 additions & 10 deletions libc/src/string/memory_utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,50 @@ namespace __llvm_libc {

// Allows compile time error reporting in `if constexpr` branches.
template <bool flag = false>
static void deferred_static_assert(const char *msg) {
LIBC_INLINE void deferred_static_assert(const char *msg) {
static_assert(flag, "compilation error");
(void)msg;
}

// Return whether `value` is zero or a power of two.
static constexpr bool is_power2_or_zero(size_t value) {
LIBC_INLINE constexpr bool is_power2_or_zero(size_t value) {
return (value & (value - 1U)) == 0;
}

// Return whether `value` is a power of two.
static constexpr bool is_power2(size_t value) {
LIBC_INLINE constexpr bool is_power2(size_t value) {
return value && is_power2_or_zero(value);
}

// Compile time version of log2 that handles 0.
static constexpr size_t log2s(size_t value) {
LIBC_INLINE constexpr size_t log2s(size_t value) {
return (value == 0 || value == 1) ? 0 : 1 + log2s(value / 2);
}

// Returns the first power of two preceding value or value if it is already a
// power of two (or 0 when value is 0).
static constexpr size_t le_power2(size_t value) {
LIBC_INLINE constexpr size_t le_power2(size_t value) {
return value == 0 ? value : 1ULL << log2s(value);
}

// Returns the first power of two following value or value if it is already a
// power of two (or 0 when value is 0).
static constexpr size_t ge_power2(size_t value) {
LIBC_INLINE constexpr size_t ge_power2(size_t value) {
return is_power2_or_zero(value) ? value : 1ULL << (log2s(value) + 1);
}

// Returns the number of bytes to substract from ptr to get to the previous
// multiple of alignment. If ptr is already aligned returns 0.
template <size_t alignment> uintptr_t distance_to_align_down(const void *ptr) {
template <size_t alignment>
LIBC_INLINE uintptr_t distance_to_align_down(const void *ptr) {
static_assert(is_power2(alignment), "alignment must be a power of 2");
return reinterpret_cast<uintptr_t>(ptr) & (alignment - 1U);
}

// Returns the number of bytes to add to ptr to get to the next multiple of
// alignment. If ptr is already aligned returns 0.
template <size_t alignment> uintptr_t distance_to_align_up(const void *ptr) {
template <size_t alignment>
LIBC_INLINE uintptr_t distance_to_align_up(const void *ptr) {
static_assert(is_power2(alignment), "alignment must be a power of 2");
// The logic is not straightforward and involves unsigned modulo arithmetic
// but the generated code is as fast as it can be.
Expand All @@ -75,12 +77,13 @@ template <size_t alignment> uintptr_t distance_to_align_up(const void *ptr) {
// Returns the number of bytes to add to ptr to get to the next multiple of
// alignment. If ptr is already aligned returns alignment.
template <size_t alignment>
uintptr_t distance_to_next_aligned(const void *ptr) {
LIBC_INLINE uintptr_t distance_to_next_aligned(const void *ptr) {
return alignment - distance_to_align_down<alignment>(ptr);
}

// Returns the same pointer but notifies the compiler that it is aligned.
template <size_t alignment, typename T> static T *assume_aligned(T *ptr) {
template <size_t alignment, typename T>
LIBC_INLINE T *assume_aligned(T *ptr) {
return reinterpret_cast<T *>(__builtin_assume_aligned(ptr, alignment));
}

Expand Down
6 changes: 4 additions & 2 deletions libc/src/string/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,17 @@ LIBC_INLINE size_t strlcpy(char *__restrict dst, const char *__restrict src,
}

template <bool ReturnNull = true>
constexpr static char *strchr_implementation(const char *src, int c) {
LIBC_INLINE constexpr static char *strchr_implementation(const char *src,
int c) {
char ch = static_cast<char>(c);
for (; *src && *src != ch; ++src)
;
char *ret = ReturnNull ? nullptr : const_cast<char *>(src);
return *src == ch ? const_cast<char *>(src) : ret;
}

constexpr static char *strrchr_implementation(const char *src, int c) {
LIBC_INLINE constexpr static char *strrchr_implementation(const char *src,
int c) {
char ch = static_cast<char>(c);
char *last_occurrence = nullptr;
for (; *src; ++src) {
Expand Down