diff --git a/libc/docs/code_style.rst b/libc/docs/code_style.rst index 9efacc4bbece3..f7852291c5303 100644 --- a/libc/docs/code_style.rst +++ b/libc/docs/code_style.rst @@ -4,6 +4,9 @@ The libc code style =================== +Naming style +============ + For the large part, the libc project follows the general `coding standards of the LLVM project `_. The libc project differs from that standard with respect to the naming style. The @@ -17,6 +20,27 @@ differences are as follows: #. **Function and methods** - They use the ``snake_case`` style like the non-const variables. #. **Internal type names** - These are types which are interal to the libc - implementation. They use the `CaptilizedCamelCase` style. + implementation. They use the ``CaptilizedCamelCase`` style. #. **Public names** - These are the names as prescribed by the standards and will follow the style as prescribed by the standards. + +Inline functions defined in header files +======================================== + +When defining functions inline in header files, we follow certain rules: + +#. The functions should not be given file-static linkage. There can be class + static methods defined inline however. +#. Instead of using the ``inline`` keyword, they should be tagged with the + ``LIBC_INLINE`` macro defined in ``src/__support/common.h``. For example: + + .. code-block:: c++ + + LIBC_INLINE ReturnType function_defined_inline(ArgType arg) { + ... + } + +#. The ``LIBC_INLINE`` tag should also be added to functions which have + definitions that are implicitly inline. Examples of such functions are + class methods (static and non-static) defined inline and ``constexpr`` + functions. diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt index 83a5a5b3bd4ba..e242d4e424fb0 100644 --- a/libc/src/__support/CPP/CMakeLists.txt +++ b/libc/src/__support/CPP/CMakeLists.txt @@ -51,6 +51,8 @@ add_header_library( string_view HDRS string_view.h + DEPENDS + libc.src.__support.common ) add_header_library( @@ -101,4 +103,5 @@ add_object_library( new.h DEPENDS libc.include.stdlib + libc.src.__support.common ) diff --git a/libc/src/__support/CPP/new.h b/libc/src/__support/CPP/new.h index 7b23f0877c149..4c0fb9c5100c8 100644 --- a/libc/src/__support/CPP/new.h +++ b/libc/src/__support/CPP/new.h @@ -9,6 +9,8 @@ #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_NEW_H #define LLVM_LIBC_SRC_SUPPORT_CPP_NEW_H +#include "src/__support/common.h" + #include // For size_t #include // For malloc, free etc. @@ -34,14 +36,14 @@ class AllocChecker { AllocChecker() = default; operator bool() const { return success; } - static void *alloc(size_t s, AllocChecker &ac) { + LIBC_INLINE static void *alloc(size_t s, AllocChecker &ac) { void *mem = ::malloc(s); ac = (mem != nullptr); return mem; } - static void *aligned_alloc(size_t s, std::align_val_t align, - AllocChecker &ac) { + LIBC_INLINE static void *aligned_alloc(size_t s, std::align_val_t align, + AllocChecker &ac) { void *mem = ::aligned_alloc(static_cast(align), s); ac = (mem != nullptr); return mem; @@ -50,27 +52,28 @@ class AllocChecker { } // namespace __llvm_libc -inline void *operator new(size_t size, __llvm_libc::AllocChecker &ac) noexcept { +LIBC_INLINE void *operator new(size_t size, + __llvm_libc::AllocChecker &ac) noexcept { return __llvm_libc::AllocChecker::alloc(size, ac); } -inline void *operator new(size_t size, std::align_val_t align, - __llvm_libc::AllocChecker &ac) noexcept { +LIBC_INLINE void *operator new(size_t size, std::align_val_t align, + __llvm_libc::AllocChecker &ac) noexcept { return __llvm_libc::AllocChecker::aligned_alloc(size, align, ac); } -inline void *operator new[](size_t size, - __llvm_libc::AllocChecker &ac) noexcept { +LIBC_INLINE void *operator new[](size_t size, + __llvm_libc::AllocChecker &ac) noexcept { return __llvm_libc::AllocChecker::alloc(size, ac); } -inline void *operator new[](size_t size, std::align_val_t align, - __llvm_libc::AllocChecker &ac) noexcept { +LIBC_INLINE void *operator new[](size_t size, std::align_val_t align, + __llvm_libc::AllocChecker &ac) noexcept { return __llvm_libc::AllocChecker::aligned_alloc(size, align, ac); } // The ideal situation would be to define the various flavors of operator delete -// inline like we do with operator new above. However, since we need operator +// inlinelike we do with operator new above. However, since we need operator // delete prototypes to match those specified by the C++ standard, we cannot // define them inline as the C++ standard does not allow inline definitions of // replacement operator delete implementations. Note also that we assign a diff --git a/libc/src/__support/CPP/string_view.h b/libc/src/__support/CPP/string_view.h index a1aae8dff401e..aa31f15d97c66 100644 --- a/libc/src/__support/CPP/string_view.h +++ b/libc/src/__support/CPP/string_view.h @@ -9,6 +9,8 @@ #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_STRINGVIEW_H #define LLVM_LIBC_SRC_SUPPORT_CPP_STRINGVIEW_H +#include "src/__support/common.h" + #include namespace __llvm_libc { @@ -26,20 +28,21 @@ class string_view { static size_t min(size_t A, size_t B) { return A <= B ? A : B; } - static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) { + LIBC_INLINE static int compareMemory(const char *Lhs, const char *Rhs, + size_t Length) { for (size_t I = 0; I < Length; ++I) if (int Diff = (int)Lhs[I] - (int)Rhs[I]) return Diff; return 0; } - static constexpr size_t length(const char *Str) { + LIBC_INLINE static constexpr size_t length(const char *Str) { for (const char *End = Str;; ++End) if (*End == '\0') return End - Str; } - bool equals(string_view Other) const { + LIBC_INLINE bool equals(string_view Other) const { return (Len == Other.Len && compareMemory(Data, Other.Data, Other.Len) == 0); } @@ -59,39 +62,43 @@ class string_view { // size_type. inline static constexpr size_t npos = -1; - constexpr string_view() : Data(nullptr), Len(0) {} + LIBC_INLINE constexpr string_view() : Data(nullptr), Len(0) {} // Assumes Str is a null-terminated string. The length of the string does // not include the terminating null character. // Preconditions: [Str, Str + ​length(Str)) is a valid range. - constexpr string_view(const char *Str) : Data(Str), Len(length(Str)) {} + LIBC_INLINE constexpr string_view(const char *Str) + : Data(Str), Len(length(Str)) {} // Preconditions: [Str, Str + N) is a valid range. - constexpr string_view(const char *Str, size_t N) : Data(Str), Len(N) {} + LIBC_INLINE constexpr string_view(const char *Str, size_t N) + : Data(Str), Len(N) {} - constexpr const char *data() const { return Data; } + LIBC_INLINE constexpr const char *data() const { return Data; } // Returns the size of the string_view. - constexpr size_t size() const { return Len; } + LIBC_INLINE constexpr size_t size() const { return Len; } // Returns whether the string_view is empty. - constexpr bool empty() const { return Len == 0; } + LIBC_INLINE constexpr bool empty() const { return Len == 0; } // Returns an iterator to the first character of the view. - const char *begin() const { return Data; } + LIBC_INLINE const char *begin() const { return Data; } // Returns an iterator to the character following the last character of the // view. - const char *end() const { return Data + Len; } + LIBC_INLINE const char *end() const { return Data + Len; } // Returns a const reference to the character at specified location pos. // No bounds checking is performed: the behavior is undefined if pos >= // size(). - constexpr const char &operator[](size_t Index) const { return Data[Index]; } + LIBC_INLINE constexpr const char &operator[](size_t Index) const { + return Data[Index]; + } /// compare - Compare two strings; the result is -1, 0, or 1 if this string /// is lexicographically less than, equal to, or greater than the \p Other. - int compare(string_view Other) const { + LIBC_INLINE int compare(string_view Other) const { // Check the prefix for a mismatch. if (int Res = compareMemory(Data, Other.Data, min(Len, Other.Len))) return Res < 0 ? -1 : 1; @@ -101,48 +108,52 @@ class string_view { return Len < Other.Len ? -1 : 1; } - inline bool operator==(string_view Other) const { return equals(Other); } - inline bool operator!=(string_view Other) const { return !(*this == Other); } - inline bool operator<(string_view Other) const { + LIBC_INLINE bool operator==(string_view Other) const { return equals(Other); } + LIBC_INLINE bool operator!=(string_view Other) const { + return !(*this == Other); + } + LIBC_INLINE bool operator<(string_view Other) const { return compare(Other) == -1; } - inline bool operator<=(string_view Other) const { + LIBC_INLINE bool operator<=(string_view Other) const { return compare(Other) != 1; } - inline bool operator>(string_view Other) const { return compare(Other) == 1; } - inline bool operator>=(string_view Other) const { + LIBC_INLINE bool operator>(string_view Other) const { + return compare(Other) == 1; + } + LIBC_INLINE bool operator>=(string_view Other) const { return compare(Other) != -1; } // Moves the start of the view forward by n characters. // The behavior is undefined if n > size(). - void remove_prefix(size_t N) { + LIBC_INLINE void remove_prefix(size_t N) { Len -= N; Data += N; } // Moves the end of the view back by n characters. // The behavior is undefined if n > size(). - void remove_suffix(size_t N) { Len -= N; } + LIBC_INLINE void remove_suffix(size_t N) { Len -= N; } // Check if this string starts with the given Prefix. - bool starts_with(string_view Prefix) const { + LIBC_INLINE bool starts_with(string_view Prefix) const { return Len >= Prefix.Len && compareMemory(Data, Prefix.Data, Prefix.Len) == 0; } // Check if this string starts with the given Prefix. - bool starts_with(const char Prefix) const { + LIBC_INLINE bool starts_with(const char Prefix) const { return !empty() && front() == Prefix; } // Check if this string ends with the given Prefix. - bool ends_with(const char Suffix) const { + LIBC_INLINE bool ends_with(const char Suffix) const { return !empty() && back() == Suffix; } // Check if this string ends with the given Suffix. - bool ends_with(string_view Suffix) const { + LIBC_INLINE bool ends_with(string_view Suffix) const { return Len >= Suffix.Len && compareMemory(end() - Suffix.Len, Suffix.Data, Suffix.Len) == 0; } @@ -156,19 +167,19 @@ class string_view { // N The number of characters to included in the substring. If N exceeds the // number of characters remaining in the string, the string suffix (starting // with Start) will be returned. - string_view substr(size_t Start, size_t N = npos) const { + LIBC_INLINE string_view substr(size_t Start, size_t N = npos) const { Start = min(Start, Len); return string_view(Data + Start, min(N, Len - Start)); } // front - Get the first character in the string. - char front() const { return Data[0]; } + LIBC_INLINE char front() const { return Data[0]; } // back - Get the last character in the string. - char back() const { return Data[Len - 1]; } + LIBC_INLINE char back() const { return Data[Len - 1]; } // Finds the first occurence of c in this view, starting at position From. - size_t find_first_of(const char c, size_t From = 0) const { + LIBC_INLINE size_t find_first_of(const char c, size_t From = 0) const { for (size_t Pos = From; Pos < size(); ++Pos) if ((*this)[Pos] == c) return Pos; @@ -176,7 +187,7 @@ class string_view { } // Finds the last occurence of c in this view, ending at position End. - size_t find_last_of(const char c, size_t End = npos) const { + LIBC_INLINE size_t find_last_of(const char c, size_t End = npos) const { End = End >= size() ? size() : End + 1; for (; End > 0; --End) if ((*this)[End - 1] == c) diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt index 84e5346a175ae..09d90f89ce1d1 100644 --- a/libc/src/__support/FPUtil/CMakeLists.txt +++ b/libc/src/__support/FPUtil/CMakeLists.txt @@ -59,6 +59,7 @@ add_header_library( DEPENDS .fp_bits libc.src.__support.CPP.type_traits + libc.src.__support.common ) add_header_library( diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h index 9f21e733498b5..edeed0c65932c 100644 --- a/libc/src/__support/FPUtil/NormalFloat.h +++ b/libc/src/__support/FPUtil/NormalFloat.h @@ -12,6 +12,7 @@ #include "FPBits.h" #include "src/__support/CPP/type_traits.h" +#include "src/__support/common.h" #include @@ -44,7 +45,7 @@ template struct NormalFloat { bool sign; - NormalFloat(int32_t e, UIntType m, bool s) + LIBC_INLINE NormalFloat(int32_t e, UIntType m, bool s) : exponent(e), mantissa(m), sign(s) { if (mantissa >= ONE) return; @@ -54,14 +55,14 @@ template struct NormalFloat { exponent -= normalization_shift; } - explicit NormalFloat(T x) { init_from_bits(FPBits(x)); } + LIBC_INLINE explicit NormalFloat(T x) { init_from_bits(FPBits(x)); } - explicit NormalFloat(FPBits bits) { init_from_bits(bits); } + LIBC_INLINE explicit NormalFloat(FPBits bits) { init_from_bits(bits); } // Compares this normalized number with another normalized number. // Returns -1 is this number is less than |other|, 0 if this number is equal // to |other|, and 1 if this number is greater than |other|. - int cmp(const NormalFloat &other) const { + LIBC_INLINE int cmp(const NormalFloat &other) const { if (sign != other.sign) return sign ? -1 : 1; @@ -82,13 +83,13 @@ template struct NormalFloat { // Returns a new normalized floating point number which is equal in value // to this number multiplied by 2^e. That is: // new = this * 2^e - NormalFloat mul2(int e) const { + LIBC_INLINE NormalFloat mul2(int e) const { NormalFloat result = *this; result.exponent += e; return result; } - operator T() const { + LIBC_INLINE operator T() const { int biased_exponent = exponent + FPBits::EXPONENT_BIAS; // Max exponent is of the form 0xFF...E. That is why -2 and not -1. constexpr int MAX_EXPONENT_VALUE = (1 << ExponentWidth::VALUE) - 2; @@ -138,7 +139,7 @@ template struct NormalFloat { } private: - void init_from_bits(FPBits bits) { + LIBC_INLINE void init_from_bits(FPBits bits) { sign = bits.get_sign(); if (bits.is_inf_or_nan() || bits.is_zero()) { @@ -160,7 +161,7 @@ template struct NormalFloat { } } - unsigned evaluate_normalization_shift(UIntType m) { + LIBC_INLINE unsigned evaluate_normalization_shift(UIntType m) { unsigned shift = 0; for (; (ONE & m) == 0 && (shift < MantissaWidth::VALUE); m <<= 1, ++shift) @@ -171,7 +172,8 @@ template struct NormalFloat { #ifdef SPECIAL_X86_LONG_DOUBLE template <> -inline void NormalFloat::init_from_bits(FPBits bits) { +LIBC_INLINE void +NormalFloat::init_from_bits(FPBits bits) { sign = bits.get_sign(); if (bits.is_inf_or_nan() || bits.is_zero()) { @@ -205,7 +207,7 @@ inline void NormalFloat::init_from_bits(FPBits bits) { } } -template <> inline NormalFloat::operator long double() const { +template <> LIBC_INLINE NormalFloat::operator long double() const { int biased_exponent = exponent + FPBits::EXPONENT_BIAS; // Max exponent is of the form 0xFF...E. That is why -2 and not -1. constexpr int MAX_EXPONENT_VALUE = diff --git a/libc/src/__support/FPUtil/double_double.h b/libc/src/__support/FPUtil/double_double.h index 35e2bbc2677f0..37dace9feeb20 100644 --- a/libc/src/__support/FPUtil/double_double.h +++ b/libc/src/__support/FPUtil/double_double.h @@ -10,6 +10,7 @@ #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_DOUBLEDOUBLE_H #include "multiply_add.h" +#include "src/__support/common.h" #include "src/__support/number_pair.h" namespace __llvm_libc::fputil { @@ -17,7 +18,7 @@ namespace __llvm_libc::fputil { using DoubleDouble = __llvm_libc::NumberPair; // Assumption: |a| >= |b| -constexpr inline DoubleDouble exact_add(double a, double b) { +LIBC_INLINE constexpr DoubleDouble exact_add(double a, double b) { DoubleDouble r{0.0, 0.0}; r.hi = a + b; double t = r.hi - a; @@ -26,21 +27,21 @@ constexpr inline DoubleDouble exact_add(double a, double b) { } // Assumption: |a.hi| >= |b.hi| -constexpr inline DoubleDouble add(DoubleDouble a, DoubleDouble b) { +LIBC_INLINE constexpr DoubleDouble add(DoubleDouble a, DoubleDouble b) { DoubleDouble r = exact_add(a.hi, b.hi); double lo = a.lo + b.lo; return exact_add(r.hi, r.lo + lo); } // Assumption: |a.hi| >= |b| -constexpr inline DoubleDouble add(DoubleDouble a, double b) { +LIBC_INLINE constexpr DoubleDouble add(DoubleDouble a, double b) { DoubleDouble r = exact_add(a.hi, b); return exact_add(r.hi, r.lo + a.lo); } // TODO(lntue): add a correct multiplication when FMA instructions are not // available. -inline DoubleDouble exact_mult(double a, double b) { +LIBC_INLINE DoubleDouble exact_mult(double a, double b) { DoubleDouble r{0.0, 0.0}; r.hi = a * b; r.lo = fputil::multiply_add(a, b, -r.hi); diff --git a/libc/src/__support/OSUtil/linux/aarch64/syscall.h b/libc/src/__support/OSUtil/linux/aarch64/syscall.h index 2d1f0669e8f76..350e39f3bd5f9 100644 --- a/libc/src/__support/OSUtil/linux/aarch64/syscall.h +++ b/libc/src/__support/OSUtil/linux/aarch64/syscall.h @@ -44,52 +44,46 @@ namespace __llvm_libc { -__attribute__((always_inline)) inline long syscall_impl(long number) { +LIBC_INLINE long syscall_impl(long number) { REGISTER_DECL_0; SYSCALL_INSTR(REGISTER_CONSTRAINT_0); return x0; } -__attribute__((always_inline)) inline long syscall_impl(long number, - long arg1) { +LIBC_INLINE long syscall_impl(long number, long arg1) { REGISTER_DECL_1; SYSCALL_INSTR(REGISTER_CONSTRAINT_1); return x0; } -__attribute__((always_inline)) inline long syscall_impl(long number, long arg1, - long arg2) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2) { REGISTER_DECL_2; SYSCALL_INSTR(REGISTER_CONSTRAINT_2); return x0; } -__attribute__((always_inline)) inline long syscall_impl(long number, long arg1, - long arg2, long arg3) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3) { REGISTER_DECL_3; SYSCALL_INSTR(REGISTER_CONSTRAINT_3); return x0; } -__attribute__((always_inline)) inline long -syscall_impl(long number, long arg1, long arg2, long arg3, long arg4) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3, + long arg4) { REGISTER_DECL_4; SYSCALL_INSTR(REGISTER_CONSTRAINT_4); return x0; } -__attribute__((always_inline)) inline long syscall_impl(long number, long arg1, - long arg2, long arg3, - long arg4, long arg5) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3, + long arg4, long arg5) { REGISTER_DECL_5; SYSCALL_INSTR(REGISTER_CONSTRAINT_5); return x0; } -__attribute__((always_inline)) inline long syscall_impl(long number, long arg1, - long arg2, long arg3, - long arg4, long arg5, - long arg6) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3, + long arg4, long arg5, long arg6) { REGISTER_DECL_6; SYSCALL_INSTR(REGISTER_CONSTRAINT_6); return x0; diff --git a/libc/src/__support/OSUtil/linux/arm/syscall.h b/libc/src/__support/OSUtil/linux/arm/syscall.h index 268d4093cec2d..325600e6d424a 100644 --- a/libc/src/__support/OSUtil/linux/arm/syscall.h +++ b/libc/src/__support/OSUtil/linux/arm/syscall.h @@ -50,52 +50,46 @@ namespace __llvm_libc { -__attribute__((always_inline)) inline long syscall_impl(long number) { +LIBC_INLINE long syscall_impl(long number) { REGISTER_DECL_0; SYSCALL_INSTR(REGISTER_CONSTRAINT_0); return r0; } -__attribute__((always_inline)) inline long syscall_impl(long number, - long arg1) { +LIBC_INLINE long syscall_impl(long number, long arg1) { REGISTER_DECL_1; SYSCALL_INSTR(REGISTER_CONSTRAINT_1); return r0; } -__attribute__((always_inline)) inline long syscall_impl(long number, long arg1, - long arg2) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2) { REGISTER_DECL_2; SYSCALL_INSTR(REGISTER_CONSTRAINT_2); return r0; } -__attribute__((always_inline)) inline long syscall_impl(long number, long arg1, - long arg2, long arg3) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3) { REGISTER_DECL_3; SYSCALL_INSTR(REGISTER_CONSTRAINT_3); return r0; } -__attribute__((always_inline)) inline long -syscall_impl(long number, long arg1, long arg2, long arg3, long arg4) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3, + long arg4) { REGISTER_DECL_4; SYSCALL_INSTR(REGISTER_CONSTRAINT_4); return r0; } -__attribute__((always_inline)) inline long syscall_impl(long number, long arg1, - long arg2, long arg3, - long arg4, long arg5) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3, + long arg4, long arg5) { REGISTER_DECL_5; SYSCALL_INSTR(REGISTER_CONSTRAINT_5); return r0; } -__attribute__((always_inline)) inline long syscall_impl(long number, long arg1, - long arg2, long arg3, - long arg4, long arg5, - long arg6) { +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3, + long arg4, long arg5, long arg6) { REGISTER_DECL_6; SYSCALL_INSTR(REGISTER_CONSTRAINT_6); return r0; diff --git a/libc/src/__support/OSUtil/linux/x86_64/syscall.h b/libc/src/__support/OSUtil/linux/x86_64/syscall.h index 1bb401abd2f28..1a5cb0a628a81 100644 --- a/libc/src/__support/OSUtil/linux/x86_64/syscall.h +++ b/libc/src/__support/OSUtil/linux/x86_64/syscall.h @@ -15,7 +15,7 @@ namespace __llvm_libc { -__attribute__((always_inline)) inline long syscall_impl(long __number) { +LIBC_INLINE long syscall_impl(long __number) { long retcode; LIBC_INLINE_ASM("syscall" : "=a"(retcode) @@ -24,8 +24,7 @@ __attribute__((always_inline)) inline long syscall_impl(long __number) { return retcode; } -__attribute__((always_inline)) inline long syscall_impl(long __number, - long __arg1) { +LIBC_INLINE long syscall_impl(long __number, long __arg1) { long retcode; LIBC_INLINE_ASM("syscall" : "=a"(retcode) @@ -34,8 +33,7 @@ __attribute__((always_inline)) inline long syscall_impl(long __number, return retcode; } -__attribute__((always_inline)) inline long -syscall_impl(long __number, long __arg1, long __arg2) { +LIBC_INLINE long syscall_impl(long __number, long __arg1, long __arg2) { long retcode; LIBC_INLINE_ASM("syscall" : "=a"(retcode) @@ -44,8 +42,8 @@ syscall_impl(long __number, long __arg1, long __arg2) { return retcode; } -__attribute__((always_inline)) inline long -syscall_impl(long __number, long __arg1, long __arg2, long __arg3) { +LIBC_INLINE long syscall_impl(long __number, long __arg1, long __arg2, + long __arg3) { long retcode; LIBC_INLINE_ASM("syscall" : "=a"(retcode) @@ -54,9 +52,8 @@ syscall_impl(long __number, long __arg1, long __arg2, long __arg3) { return retcode; } -__attribute__((always_inline)) inline long -syscall_impl(long __number, long __arg1, long __arg2, long __arg3, - long __arg4) { +LIBC_INLINE long syscall_impl(long __number, long __arg1, long __arg2, + long __arg3, long __arg4) { long retcode; register long r10 __asm__("r10") = __arg4; LIBC_INLINE_ASM("syscall" @@ -67,9 +64,8 @@ syscall_impl(long __number, long __arg1, long __arg2, long __arg3, return retcode; } -__attribute__((always_inline)) inline long -syscall_impl(long __number, long __arg1, long __arg2, long __arg3, long __arg4, - long __arg5) { +LIBC_INLINE long syscall_impl(long __number, long __arg1, long __arg2, + long __arg3, long __arg4, long __arg5) { long retcode; register long r10 __asm__("r10") = __arg4; register long r8 __asm__("r8") = __arg5; @@ -81,9 +77,9 @@ syscall_impl(long __number, long __arg1, long __arg2, long __arg3, long __arg4, return retcode; } -__attribute__((always_inline)) inline long -syscall_impl(long __number, long __arg1, long __arg2, long __arg3, long __arg4, - long __arg5, long __arg6) { +LIBC_INLINE long syscall_impl(long __number, long __arg1, long __arg2, + long __arg3, long __arg4, long __arg5, + long __arg6) { long retcode; register long r10 __asm__("r10") = __arg4; register long r8 __asm__("r8") = __arg5; diff --git a/libc/src/__support/builtin_wrappers.h b/libc/src/__support/builtin_wrappers.h index 6e3f4274f7533..e41808e1b94df 100644 --- a/libc/src/__support/builtin_wrappers.h +++ b/libc/src/__support/builtin_wrappers.h @@ -31,24 +31,26 @@ template LIBC_INLINE int correct_zero(T val, int bits) { } template LIBC_INLINE int clz(T val); -template <> inline int clz(unsigned int val) { +template <> LIBC_INLINE int clz(unsigned int val) { return __builtin_clz(val); } -template <> inline int clz(unsigned long int val) { +template <> LIBC_INLINE int clz(unsigned long int val) { return __builtin_clzl(val); } -template <> inline int clz(unsigned long long int val) { +template <> +LIBC_INLINE int clz(unsigned long long int val) { return __builtin_clzll(val); } template LIBC_INLINE int ctz(T val); -template <> inline int ctz(unsigned int val) { +template <> LIBC_INLINE int ctz(unsigned int val) { return __builtin_ctz(val); } -template <> inline int ctz(unsigned long int val) { +template <> LIBC_INLINE int ctz(unsigned long int val) { return __builtin_ctzl(val); } -template <> inline int ctz(unsigned long long int val) { +template <> +LIBC_INLINE int ctz(unsigned long long int val) { return __builtin_ctzll(val); } } // namespace __internal @@ -73,7 +75,7 @@ template LIBC_INLINE int unsafe_clz(T val) { DEFINE_NAMED_PAIR_TEMPLATE(SumCarry, sum, carry); template -inline constexpr cpp::enable_if_t< +LIBC_INLINE constexpr cpp::enable_if_t< cpp::is_integral_v && cpp::is_unsigned_v, SumCarry> add_with_carry(T a, T b, T carry_in) { T tmp = a + carry_in; @@ -86,7 +88,7 @@ add_with_carry(T a, T b, T carry_in) { // https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins template <> -inline SumCarry +LIBC_INLINE SumCarry add_with_carry(unsigned char a, unsigned char b, unsigned char carry_in) { SumCarry result{0, 0}; @@ -95,7 +97,7 @@ add_with_carry(unsigned char a, unsigned char b, } template <> -inline SumCarry +LIBC_INLINE SumCarry add_with_carry(unsigned short a, unsigned short b, unsigned short carry_in) { SumCarry result{0, 0}; @@ -104,7 +106,7 @@ add_with_carry(unsigned short a, unsigned short b, } template <> -inline SumCarry +LIBC_INLINE SumCarry add_with_carry(unsigned int a, unsigned int b, unsigned int carry_in) { SumCarry result{0, 0}; @@ -113,7 +115,7 @@ add_with_carry(unsigned int a, unsigned int b, } template <> -inline SumCarry +LIBC_INLINE SumCarry add_with_carry(unsigned long a, unsigned long b, unsigned long carry_in) { SumCarry result{0, 0}; @@ -122,7 +124,7 @@ add_with_carry(unsigned long a, unsigned long b, } template <> -inline SumCarry +LIBC_INLINE SumCarry add_with_carry(unsigned long long a, unsigned long long b, unsigned long long carry_in) { SumCarry result{0, 0}; @@ -136,7 +138,7 @@ add_with_carry(unsigned long long a, unsigned long long b, DEFINE_NAMED_PAIR_TEMPLATE(DiffBorrow, diff, borrow); template -inline constexpr cpp::enable_if_t< +LIBC_INLINE constexpr cpp::enable_if_t< cpp::is_integral_v && cpp::is_unsigned_v, DiffBorrow> sub_with_borrow(T a, T b, T borrow_in) { T tmp = a - b; @@ -149,7 +151,7 @@ sub_with_borrow(T a, T b, T borrow_in) { // https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins template <> -inline DiffBorrow +LIBC_INLINE DiffBorrow sub_with_borrow(unsigned char a, unsigned char b, unsigned char borrow_in) { DiffBorrow result{0, 0}; @@ -158,7 +160,7 @@ sub_with_borrow(unsigned char a, unsigned char b, } template <> -inline DiffBorrow +LIBC_INLINE DiffBorrow sub_with_borrow(unsigned short a, unsigned short b, unsigned short borrow_in) { DiffBorrow result{0, 0}; @@ -167,7 +169,7 @@ sub_with_borrow(unsigned short a, unsigned short b, } template <> -inline DiffBorrow +LIBC_INLINE DiffBorrow sub_with_borrow(unsigned int a, unsigned int b, unsigned int borrow_in) { DiffBorrow result{0, 0}; @@ -176,7 +178,7 @@ sub_with_borrow(unsigned int a, unsigned int b, } template <> -inline DiffBorrow +LIBC_INLINE DiffBorrow sub_with_borrow(unsigned long a, unsigned long b, unsigned long borrow_in) { DiffBorrow result{0, 0}; @@ -185,7 +187,7 @@ sub_with_borrow(unsigned long a, unsigned long b, } template <> -inline DiffBorrow +LIBC_INLINE DiffBorrow sub_with_borrow(unsigned long long a, unsigned long long b, unsigned long long borrow_in) { DiffBorrow result{0, 0}; diff --git a/libc/src/__support/endian.h b/libc/src/__support/endian.h index 8108d56c2d07b..1bdccd855a487 100644 --- a/libc/src/__support/endian.h +++ b/libc/src/__support/endian.h @@ -9,6 +9,8 @@ #ifndef LLVM_LIBC_SRC_SUPPORT_ENDIAN_H #define LLVM_LIBC_SRC_SUPPORT_ENDIAN_H +#include "common.h" + #include namespace __llvm_libc { @@ -37,49 +39,49 @@ template struct Endian { // Little Endian specializations template <> template <> -inline uint8_t +LIBC_INLINE uint8_t Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian(uint8_t v) { return v; } template <> template <> -inline uint8_t +LIBC_INLINE uint8_t Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian(uint8_t v) { return v; } template <> template <> -inline uint16_t +LIBC_INLINE uint16_t Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian(uint16_t v) { return __builtin_bswap16(v); } template <> template <> -inline uint16_t +LIBC_INLINE uint16_t Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian(uint16_t v) { return v; } template <> template <> -inline uint32_t +LIBC_INLINE uint32_t Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian(uint32_t v) { return __builtin_bswap32(v); } template <> template <> -inline uint32_t +LIBC_INLINE uint32_t Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian(uint32_t v) { return v; } template <> template <> -inline uint64_t +LIBC_INLINE uint64_t Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian(uint64_t v) { return __builtin_bswap64(v); } template <> template <> -inline uint64_t +LIBC_INLINE uint64_t Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian(uint64_t v) { return v; } @@ -87,48 +89,49 @@ Endian<__ORDER_LITTLE_ENDIAN__>::to_little_endian(uint64_t v) { // Big Endian specializations template <> template <> -inline uint8_t Endian<__ORDER_BIG_ENDIAN__>::to_big_endian(uint8_t v) { +LIBC_INLINE uint8_t +Endian<__ORDER_BIG_ENDIAN__>::to_big_endian(uint8_t v) { return v; } template <> template <> -inline uint8_t +LIBC_INLINE uint8_t Endian<__ORDER_BIG_ENDIAN__>::to_little_endian(uint8_t v) { return v; } template <> template <> -inline uint16_t +LIBC_INLINE uint16_t Endian<__ORDER_BIG_ENDIAN__>::to_big_endian(uint16_t v) { return v; } template <> template <> -inline uint16_t +LIBC_INLINE uint16_t Endian<__ORDER_BIG_ENDIAN__>::to_little_endian(uint16_t v) { return __builtin_bswap16(v); } template <> template <> -inline uint32_t +LIBC_INLINE uint32_t Endian<__ORDER_BIG_ENDIAN__>::to_big_endian(uint32_t v) { return v; } template <> template <> -inline uint32_t +LIBC_INLINE uint32_t Endian<__ORDER_BIG_ENDIAN__>::to_little_endian(uint32_t v) { return __builtin_bswap32(v); } template <> template <> -inline uint64_t +LIBC_INLINE uint64_t Endian<__ORDER_BIG_ENDIAN__>::to_big_endian(uint64_t v) { return v; } template <> template <> -inline uint64_t +LIBC_INLINE uint64_t Endian<__ORDER_BIG_ENDIAN__>::to_little_endian(uint64_t v) { return __builtin_bswap64(v); } diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt index 6d7a3be52bf28..518f9d7b50959 100644 --- a/libc/src/stdio/printf_core/CMakeLists.txt +++ b/libc/src/stdio/printf_core/CMakeLists.txt @@ -69,6 +69,7 @@ add_object_library( libc.src.__support.CPP.string_view libc.src.__support.FPUtil.fp_bits libc.src.__support.FPUtil.fenv_impl + libc.src.__support.common libc.src.__support.uint libc.src.__support.uint128 libc.src.__support.integer_to_string diff --git a/libc/src/stdio/printf_core/float_dec_converter.h b/libc/src/stdio/printf_core/float_dec_converter.h index e0f7b06d03061..0fc1d1144d706 100644 --- a/libc/src/stdio/printf_core/float_dec_converter.h +++ b/libc/src/stdio/printf_core/float_dec_converter.h @@ -15,6 +15,7 @@ #include "src/__support/FPUtil/FloatProperties.h" #include "src/__support/UInt.h" #include "src/__support/UInt128.h" +#include "src/__support/common.h" #include "src/__support/float_to_string.h" #include "src/__support/integer_to_string.h" #include "src/stdio/printf_core/converter_utils.h" @@ -31,8 +32,8 @@ namespace printf_core { using MantissaInt = fputil::FPBits::UIntType; // Returns true if value is divisible by 2^p. -constexpr inline bool multiple_of_power_of_2(const uint64_t value, - const uint32_t p) { +LIBC_INLINE constexpr bool multiple_of_power_of_2(const uint64_t value, + const uint32_t p) { return (value & ((uint64_t(1) << p) - 1)) == 0; } @@ -491,9 +492,9 @@ class FloatWriter { }; template , int> = 0> -int inline convert_float_decimal_typed(Writer *writer, - const FormatSection &to_conv, - fputil::FPBits float_bits) { +LIBC_INLINE int convert_float_decimal_typed(Writer *writer, + const FormatSection &to_conv, + fputil::FPBits float_bits) { // signed because later we use -MANT_WIDTH constexpr int32_t MANT_WIDTH = fputil::MantissaWidth::VALUE; bool is_negative = float_bits.get_sign(); @@ -634,9 +635,9 @@ int inline convert_float_decimal_typed(Writer *writer, } template , int> = 0> -int inline convert_float_dec_exp_typed(Writer *writer, - const FormatSection &to_conv, - fputil::FPBits float_bits) { +LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer, + const FormatSection &to_conv, + fputil::FPBits float_bits) { // signed because later we use -MANT_WIDTH constexpr int32_t MANT_WIDTH = fputil::MantissaWidth::VALUE; bool is_negative = float_bits.get_sign(); @@ -793,7 +794,8 @@ int inline convert_float_dec_exp_typed(Writer *writer, return WRITE_OK; } -int inline convert_float_decimal(Writer *writer, const FormatSection &to_conv) { +LIBC_INLINE int convert_float_decimal(Writer *writer, + const FormatSection &to_conv) { if (to_conv.length_modifier == LengthModifier::L) { fputil::FPBits::UIntType float_raw = to_conv.conv_val_raw; fputil::FPBits float_bits(float_raw); @@ -812,7 +814,8 @@ int inline convert_float_decimal(Writer *writer, const FormatSection &to_conv) { return convert_inf_nan(writer, to_conv); } -int inline convert_float_dec_exp(Writer *writer, const FormatSection &to_conv) { +LIBC_INLINE int convert_float_dec_exp(Writer *writer, + const FormatSection &to_conv) { if (to_conv.length_modifier == LengthModifier::L) { fputil::FPBits::UIntType float_raw = to_conv.conv_val_raw; fputil::FPBits float_bits(float_raw); diff --git a/libc/src/stdio/printf_core/float_hex_converter.h b/libc/src/stdio/printf_core/float_hex_converter.h index 9cc3b14b5ae4a..faf97b5b5dd9e 100644 --- a/libc/src/stdio/printf_core/float_hex_converter.h +++ b/libc/src/stdio/printf_core/float_hex_converter.h @@ -12,6 +12,7 @@ #include "src/__support/CPP/string_view.h" #include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FPBits.h" +#include "src/__support/common.h" #include "src/stdio/printf_core/converter_utils.h" #include "src/stdio/printf_core/core_structs.h" #include "src/stdio/printf_core/float_inf_nan_converter.h" @@ -25,7 +26,8 @@ namespace printf_core { using MantissaInt = fputil::FPBits::UIntType; -int inline convert_float_hex_exp(Writer *writer, const FormatSection &to_conv) { +LIBC_INLINE int convert_float_hex_exp(Writer *writer, + const FormatSection &to_conv) { // All of the letters will be defined relative to variable a, which will be // the appropriate case based on the name of the conversion. // Since the name of the conversion is also 'a', we can just use it directly. diff --git a/libc/src/stdio/printf_core/float_inf_nan_converter.h b/libc/src/stdio/printf_core/float_inf_nan_converter.h index 35c5f44ba8125..b7dcf8692e975 100644 --- a/libc/src/stdio/printf_core/float_inf_nan_converter.h +++ b/libc/src/stdio/printf_core/float_inf_nan_converter.h @@ -10,6 +10,7 @@ #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FLOAT_INF_NAN_CONVERTER_H #include "src/__support/FPUtil/FPBits.h" +#include "src/__support/common.h" #include "src/stdio/printf_core/converter_utils.h" #include "src/stdio/printf_core/core_structs.h" #include "src/stdio/printf_core/writer.h" @@ -22,7 +23,7 @@ namespace printf_core { using MantissaInt = fputil::FPBits::UIntType; -int inline convert_inf_nan(Writer *writer, const FormatSection &to_conv) { +LIBC_INLINE int convert_inf_nan(Writer *writer, const FormatSection &to_conv) { // All of the letters will be defined relative to variable a, which will be // the appropriate case based on the case of the conversion. const char a = (to_conv.conv_name & 32) | 'A'; diff --git a/libc/src/stdio/printf_core/int_converter.h b/libc/src/stdio/printf_core/int_converter.h index 4479ec6e88e42..f5c849c4ef929 100644 --- a/libc/src/stdio/printf_core/int_converter.h +++ b/libc/src/stdio/printf_core/int_converter.h @@ -11,6 +11,7 @@ #include "src/__support/CPP/span.h" #include "src/__support/CPP/string_view.h" +#include "src/__support/common.h" #include "src/__support/integer_to_string.h" #include "src/stdio/printf_core/converter_utils.h" #include "src/stdio/printf_core/core_structs.h" @@ -24,12 +25,11 @@ namespace printf_core { // These functions only work on characters that are already known to be in the // alphabet. Their behavior is undefined otherwise. -constexpr char inline to_lower(char a) { return a | 32; } -constexpr bool inline is_lower(char a) { return (a & 32) > 0; } +LIBC_INLINE constexpr char to_lower(char a) { return a | 32; } +LIBC_INLINE constexpr bool is_lower(char a) { return (a & 32) > 0; } -cpp::optional inline num_to_strview(uintmax_t num, - cpp::span bufref, - char conv_name) { +LIBC_INLINE cpp::optional +num_to_strview(uintmax_t num, cpp::span bufref, char conv_name) { if (to_lower(conv_name) == 'x') { return IntegerToString::hex(num, bufref, is_lower(conv_name)); } else if (conv_name == 'o') { @@ -39,7 +39,7 @@ cpp::optional inline num_to_strview(uintmax_t num, } } -int inline convert_int(Writer *writer, const FormatSection &to_conv) { +LIBC_INLINE int convert_int(Writer *writer, const FormatSection &to_conv) { static constexpr size_t BITS_IN_BYTE = 8; static constexpr size_t BITS_IN_NUM = sizeof(uintmax_t) * BITS_IN_BYTE; diff --git a/libc/src/stdio/printf_core/ptr_converter.h b/libc/src/stdio/printf_core/ptr_converter.h index a2738447c0dc4..e76e0f2613b82 100644 --- a/libc/src/stdio/printf_core/ptr_converter.h +++ b/libc/src/stdio/printf_core/ptr_converter.h @@ -10,6 +10,7 @@ #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_PTR_CONVERTER_H #include "src/__support/CPP/string_view.h" +#include "src/__support/common.h" #include "src/stdio/printf_core/converter_utils.h" #include "src/stdio/printf_core/core_structs.h" #include "src/stdio/printf_core/int_converter.h" @@ -18,7 +19,7 @@ namespace __llvm_libc { namespace printf_core { -int inline convert_pointer(Writer *writer, const FormatSection &to_conv) { +LIBC_INLINE int convert_pointer(Writer *writer, const FormatSection &to_conv) { if (to_conv.conv_val_ptr == (void *)(nullptr)) { RET_IF_RESULT_NEGATIVE(writer->write("(nullptr)")); } else { diff --git a/libc/src/sys/stat/linux/CMakeLists.txt b/libc/src/sys/stat/linux/CMakeLists.txt index 6c8b1b6a8e599..20deb795fde30 100644 --- a/libc/src/sys/stat/linux/CMakeLists.txt +++ b/libc/src/sys/stat/linux/CMakeLists.txt @@ -73,6 +73,7 @@ add_header_library( libc.include.sys_stat libc.include.sys_syscall libc.src.__support.OSUtil.osutil + libc.src.__support.common libc.src.errno.errno ) diff --git a/libc/src/sys/stat/linux/kernel_statx.h b/libc/src/sys/stat/linux/kernel_statx.h index 7260f155f512b..c3d5777eb9781 100644 --- a/libc/src/sys/stat/linux/kernel_statx.h +++ b/libc/src/sys/stat/linux/kernel_statx.h @@ -10,6 +10,7 @@ #define LLVM_LIBC_SRC_SYS_STAT_LINUX_STATX_H #include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/common.h" #include #include @@ -69,8 +70,8 @@ constexpr unsigned int STATX_BASIC_STATS_MASK = 0x7FF; namespace __llvm_libc { -inline int statx(int dirfd, const char *__restrict path, int flags, - struct stat *__restrict statbuf) { +LIBC_INLINE int statx(int dirfd, const char *__restrict path, int flags, + struct stat *__restrict statbuf) { // We make a statx syscall and copy out the result into the |statbuf|. ::statx_buf xbuf; long ret = __llvm_libc::syscall_impl(SYS_statx, dirfd, path, flags, diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index be4dafdc3b8b5..7f65a93e9a7c2 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -144,7 +144,10 @@ libc_support_library( libc_support_library( name = "__support_cpp_string_view", hdrs = ["src/__support/CPP/string_view.h"], - deps = [":libc_root"], + deps = [ + ":__support_common", + ":libc_root", + ], ) libc_support_library( @@ -393,6 +396,7 @@ libc_support_library( name = "__support_fputil_normal_float", hdrs = ["src/__support/FPUtil/NormalFloat.h"], deps = [ + ":__support_common", ":__support_cpp_type_traits", ":__support_fputil_fp_bits", ":libc_root",