summaryrefslogtreecommitdiff
path: root/libc/src
diff options
context:
space:
mode:
Diffstat (limited to 'libc/src')
-rw-r--r--libc/src/CMakeLists.txt7
-rw-r--r--libc/src/__support/CMakeLists.txt13
-rw-r--r--libc/src/__support/CPP/atomic.h184
-rw-r--r--libc/src/__support/CPP/type_traits.h5
-rw-r--r--libc/src/__support/CPP/type_traits/has_unique_object_representations.h30
-rw-r--r--libc/src/__support/CPP/type_traits/is_copy_assignable.h32
-rw-r--r--libc/src/__support/CPP/type_traits/is_copy_constructible.h31
-rw-r--r--libc/src/__support/CPP/type_traits/is_move_assignable.h33
-rw-r--r--libc/src/__support/CPP/type_traits/is_move_constructible.h31
-rw-r--r--libc/src/__support/CPP/type_traits/is_trivially_copyable.h4
-rw-r--r--libc/src/__support/FPUtil/aarch64/FEnvImpl.h52
-rw-r--r--libc/src/__support/RPC/CMakeLists.txt1
-rw-r--r--libc/src/__support/complex_type.h74
-rw-r--r--libc/src/__support/freelist_heap.cpp19
-rw-r--r--libc/src/__support/macros/properties/complex_types.h5
-rw-r--r--libc/src/__support/macros/properties/os.h12
-rw-r--r--libc/src/__support/macros/properties/types.h5
-rw-r--r--libc/src/__support/time/CMakeLists.txt17
-rw-r--r--libc/src/__support/time/clock_gettime.h (renamed from libc/src/__support/time/linux/clock_gettime.h)10
-rw-r--r--libc/src/__support/time/gpu/CMakeLists.txt22
-rw-r--r--libc/src/__support/time/gpu/clock_gettime.cpp33
-rw-r--r--libc/src/__support/time/gpu/time_utils.cpp (renamed from libc/src/time/gpu/time_utils.cpp)0
-rw-r--r--libc/src/__support/time/gpu/time_utils.h (renamed from libc/src/time/gpu/time_utils.h)2
-rw-r--r--libc/src/__support/time/linux/CMakeLists.txt2
-rw-r--r--libc/src/__support/time/linux/clock_conversion.h2
-rw-r--r--libc/src/__support/time/linux/clock_gettime.cpp2
-rw-r--r--libc/src/__support/time/windows/CMakeLists.txt26
-rw-r--r--libc/src/__support/time/windows/clock_gettime.cpp134
-rw-r--r--libc/src/__support/time/windows/performance_counter.h35
-rw-r--r--libc/src/arpa/CMakeLists.txt1
-rw-r--r--libc/src/arpa/inet/CMakeLists.txt (renamed from libc/src/network/CMakeLists.txt)0
-rw-r--r--libc/src/arpa/inet/htonl.cpp (renamed from libc/src/network/htonl.cpp)2
-rw-r--r--libc/src/arpa/inet/htonl.h (renamed from libc/src/network/htonl.h)6
-rw-r--r--libc/src/arpa/inet/htons.cpp (renamed from libc/src/network/htons.cpp)2
-rw-r--r--libc/src/arpa/inet/htons.h (renamed from libc/src/network/htons.h)6
-rw-r--r--libc/src/arpa/inet/ntohl.cpp (renamed from libc/src/network/ntohl.cpp)2
-rw-r--r--libc/src/arpa/inet/ntohl.h (renamed from libc/src/network/ntohl.h)6
-rw-r--r--libc/src/arpa/inet/ntohs.cpp (renamed from libc/src/network/ntohs.cpp)2
-rw-r--r--libc/src/arpa/inet/ntohs.h (renamed from libc/src/network/ntohs.h)6
-rw-r--r--libc/src/compiler/generic/__stack_chk_fail.cpp3
-rw-r--r--libc/src/complex/CMakeLists.txt12
-rw-r--r--libc/src/complex/cimagf128.h9
-rw-r--r--libc/src/complex/cimagf16.h9
-rw-r--r--libc/src/complex/conj.h20
-rw-r--r--libc/src/complex/conjf.h20
-rw-r--r--libc/src/complex/conjf128.h21
-rw-r--r--libc/src/complex/conjf16.h21
-rw-r--r--libc/src/complex/conjl.h20
-rw-r--r--libc/src/complex/cproj.h20
-rw-r--r--libc/src/complex/cprojf.h20
-rw-r--r--libc/src/complex/cprojf128.h21
-rw-r--r--libc/src/complex/cprojf16.h21
-rw-r--r--libc/src/complex/cprojl.h20
-rw-r--r--libc/src/complex/crealf128.h9
-rw-r--r--libc/src/complex/crealf16.h9
-rw-r--r--libc/src/complex/generic/CMakeLists.txt128
-rw-r--r--libc/src/complex/generic/cimagf128.cpp4
-rw-r--r--libc/src/complex/generic/cimagf16.cpp4
-rw-r--r--libc/src/complex/generic/conj.cpp19
-rw-r--r--libc/src/complex/generic/conjf.cpp19
-rw-r--r--libc/src/complex/generic/conjf128.cpp19
-rw-r--r--libc/src/complex/generic/conjf16.cpp19
-rw-r--r--libc/src/complex/generic/conjl.cpp19
-rw-r--r--libc/src/complex/generic/cproj.cpp19
-rw-r--r--libc/src/complex/generic/cprojf.cpp19
-rw-r--r--libc/src/complex/generic/cprojf128.cpp19
-rw-r--r--libc/src/complex/generic/cprojf16.cpp19
-rw-r--r--libc/src/complex/generic/cprojl.cpp19
-rw-r--r--libc/src/complex/generic/crealf128.cpp4
-rw-r--r--libc/src/complex/generic/crealf16.cpp4
-rw-r--r--libc/src/math/CMakeLists.txt1
-rw-r--r--libc/src/math/cosf16.h21
-rw-r--r--libc/src/math/docs/add_math_function.md2
-rw-r--r--libc/src/math/generic/CMakeLists.txt19
-rw-r--r--libc/src/math/generic/cosf16.cpp84
-rw-r--r--libc/src/math/generic/cospif16.cpp2
-rw-r--r--libc/src/math/generic/sinf16.cpp15
-rw-r--r--libc/src/stdlib/CMakeLists.txt38
-rw-r--r--libc/src/stdlib/baremetal/CMakeLists.txt50
-rw-r--r--libc/src/stdlib/baremetal/aligned_alloc.cpp21
-rw-r--r--libc/src/stdlib/baremetal/calloc.cpp21
-rw-r--r--libc/src/stdlib/baremetal/free.cpp19
-rw-r--r--libc/src/stdlib/baremetal/malloc.cpp21
-rw-r--r--libc/src/stdlib/baremetal/realloc.cpp21
-rw-r--r--libc/src/stdlib/freelist_malloc.cpp42
-rw-r--r--libc/src/string/CMakeLists.txt146
-rw-r--r--libc/src/strings/CMakeLists.txt97
-rw-r--r--libc/src/strings/bcmp.cpp (renamed from libc/src/string/bcmp.cpp)2
-rw-r--r--libc/src/strings/bcmp.h (renamed from libc/src/string/bcmp.h)6
-rw-r--r--libc/src/strings/bcopy.cpp (renamed from libc/src/string/bcopy.cpp)2
-rw-r--r--libc/src/strings/bcopy.h (renamed from libc/src/string/bcopy.h)6
-rw-r--r--libc/src/strings/bzero.cpp (renamed from libc/src/string/bzero.cpp)2
-rw-r--r--libc/src/strings/bzero.h (renamed from libc/src/string/bzero.h)6
-rw-r--r--libc/src/strings/index.cpp (renamed from libc/src/string/index.cpp)2
-rw-r--r--libc/src/strings/index.h (renamed from libc/src/string/index.h)6
-rw-r--r--libc/src/strings/rindex.cpp (renamed from libc/src/string/rindex.cpp)2
-rw-r--r--libc/src/strings/rindex.h (renamed from libc/src/string/rindex.h)6
-rw-r--r--libc/src/strings/strcasecmp.cpp (renamed from libc/src/string/strcasecmp.cpp)2
-rw-r--r--libc/src/strings/strcasecmp.h (renamed from libc/src/string/strcasecmp.h)6
-rw-r--r--libc/src/strings/strncasecmp.cpp (renamed from libc/src/string/strncasecmp.cpp)2
-rw-r--r--libc/src/strings/strncasecmp.h (renamed from libc/src/string/strncasecmp.h)6
-rw-r--r--libc/src/sys/mman/CMakeLists.txt6
-rw-r--r--libc/src/sys/mman/linux/CMakeLists.txt12
-rw-r--r--libc/src/sys/mman/linux/process_mrelease.cpp41
-rw-r--r--libc/src/sys/mman/process_mrelease.h21
-rw-r--r--libc/src/time/CMakeLists.txt17
-rw-r--r--libc/src/time/clock_getres.h21
-rw-r--r--libc/src/time/gpu/CMakeLists.txt36
-rw-r--r--libc/src/time/gpu/clock.cpp2
-rw-r--r--libc/src/time/gpu/clock_gettime.cpp19
-rw-r--r--libc/src/time/gpu/nanosleep.cpp4
-rw-r--r--libc/src/time/gpu/timespec_get.cpp1
-rw-r--r--libc/src/time/linux/CMakeLists.txt21
-rw-r--r--libc/src/time/linux/clock.cpp2
-rw-r--r--libc/src/time/linux/clock_gettime.cpp2
-rw-r--r--libc/src/time/linux/gettimeofday.cpp2
-rw-r--r--libc/src/time/linux/timespec_get.cpp2
-rw-r--r--libc/src/time/time.cpp (renamed from libc/src/time/linux/time.cpp)8
-rw-r--r--libc/src/time/windows/CMakeLists.txt13
-rw-r--r--libc/src/time/windows/clock_getres.cpp110
-rw-r--r--libc/src/unistd/dup.h2
-rw-r--r--libc/src/unistd/dup2.h2
-rw-r--r--libc/src/unistd/dup3.h2
-rw-r--r--libc/src/unistd/fork.h3
-rw-r--r--libc/src/unistd/ftruncate.h3
-rw-r--r--libc/src/unistd/getcwd.h3
-rw-r--r--libc/src/unistd/geteuid.h3
-rw-r--r--libc/src/unistd/getopt.h2
-rw-r--r--libc/src/unistd/getpid.h3
-rw-r--r--libc/src/unistd/getppid.h3
-rw-r--r--libc/src/unistd/getuid.h3
-rw-r--r--libc/src/unistd/isatty.h2
-rw-r--r--libc/src/unistd/link.h2
-rw-r--r--libc/src/unistd/linux/CMakeLists.txt39
-rw-r--r--libc/src/unistd/linux/ftruncate.cpp2
-rw-r--r--libc/src/unistd/linux/lseek.cpp2
-rw-r--r--libc/src/unistd/linux/sysconf.cpp2
-rw-r--r--libc/src/unistd/linux/truncate.cpp2
-rw-r--r--libc/src/unistd/lseek.h3
-rw-r--r--libc/src/unistd/pread.h5
-rw-r--r--libc/src/unistd/pwrite.h5
-rw-r--r--libc/src/unistd/read.h4
-rw-r--r--libc/src/unistd/readlink.h4
-rw-r--r--libc/src/unistd/readlinkat.h4
-rw-r--r--libc/src/unistd/swab.h2
-rw-r--r--libc/src/unistd/symlink.h2
-rw-r--r--libc/src/unistd/symlinkat.h2
-rw-r--r--libc/src/unistd/syscall.h2
-rw-r--r--libc/src/unistd/sysconf.h2
-rw-r--r--libc/src/unistd/truncate.h3
-rw-r--r--libc/src/unistd/write.h4
151 files changed, 1965 insertions, 618 deletions
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 02c193e63536..9fc331ad18a3 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -12,7 +12,9 @@ add_subdirectory(stdfix)
add_subdirectory(stdio)
add_subdirectory(stdlib)
add_subdirectory(string)
+add_subdirectory(strings)
add_subdirectory(wchar)
+add_subdirectory(time)
if(${LIBC_TARGET_OS} STREQUAL "linux")
add_subdirectory(dirent)
@@ -32,13 +34,12 @@ if(NOT LLVM_LIBC_FULL_BUILD)
return()
endif()
+add_subdirectory(arpa)
add_subdirectory(assert)
add_subdirectory(compiler)
-add_subdirectory(network)
+add_subdirectory(locale)
add_subdirectory(search)
add_subdirectory(setjmp)
add_subdirectory(signal)
add_subdirectory(spawn)
add_subdirectory(threads)
-add_subdirectory(time)
-add_subdirectory(locale)
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 8f85740f70a0..4e90aad9a45b 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -48,13 +48,19 @@ add_header_library(
.freetrie
)
-add_header_library(
+add_object_library(
freelist_heap
+ SRCS
+ freelist_heap.cpp
HDRS
freelist_heap.h
+ COMPILE_OPTIONS
+ -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
DEPENDS
.block
+ .freelist
.freestore
+ .freetrie
libc.src.__support.CPP.cstddef
libc.src.__support.CPP.array
libc.src.__support.CPP.optional
@@ -235,6 +241,11 @@ add_header_library(
complex_type
HDRS
complex_type.h
+ DEPENDS
+ libc.src.__support.CPP.bit
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.macros.properties.types
+ libc.src.__support.macros.properties.complex_types
)
add_header_library(
diff --git a/libc/src/__support/CPP/atomic.h b/libc/src/__support/CPP/atomic.h
index 72e7f2adde6a..287dcac98fbb 100644
--- a/libc/src/__support/CPP/atomic.h
+++ b/libc/src/__support/CPP/atomic.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ATOMIC_H
#define LLVM_LIBC_SRC___SUPPORT_CPP_ATOMIC_H
+#include "src/__support/CPP/type_traits/has_unique_object_representations.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
@@ -40,15 +41,37 @@ enum class MemoryScope : int {
};
template <typename T> struct Atomic {
- // For now, we will restrict to only arithmetic types.
- static_assert(is_arithmetic_v<T>, "Only arithmetic types can be atomic.");
+ static_assert(is_trivially_copyable_v<T> && is_copy_constructible_v<T> &&
+ is_move_constructible_v<T> && is_copy_assignable_v<T> &&
+ is_move_assignable_v<T>,
+ "atomic<T> requires T to be trivially copyable, copy "
+ "constructible, move constructible, copy assignable, "
+ "and move assignable.");
+
+ static_assert(cpp::has_unique_object_representations_v<T>,
+ "atomic<T> in libc only support types whose values has unique "
+ "object representations.");
private:
- // The value stored should be appropriately aligned so that
- // hardware instructions used to perform atomic operations work
- // correctly.
- static constexpr int ALIGNMENT = sizeof(T) > alignof(T) ? sizeof(T)
- : alignof(T);
+ // type conversion helper to avoid long c++ style casts
+ LIBC_INLINE static int order(MemoryOrder mem_ord) {
+ return static_cast<int>(mem_ord);
+ }
+
+ LIBC_INLINE static int scope(MemoryScope mem_scope) {
+ return static_cast<int>(mem_scope);
+ }
+
+ LIBC_INLINE static T *addressof(T &ref) { return __builtin_addressof(ref); }
+
+ // Require types that are 1, 2, 4, 8, or 16 bytes in length to be aligned to
+ // at least their size to be potentially used lock-free.
+ LIBC_INLINE_VAR static constexpr size_t MIN_ALIGNMENT =
+ (sizeof(T) & (sizeof(T) - 1)) || (sizeof(T) > 16) ? 0 : sizeof(T);
+
+ LIBC_INLINE_VAR static constexpr size_t ALIGNMENT = alignof(T) > MIN_ALIGNMENT
+ ? alignof(T)
+ : MIN_ALIGNMENT;
public:
using value_type = T;
@@ -59,139 +82,160 @@ public:
// operations should be performed using the atomic methods however.
alignas(ALIGNMENT) value_type val;
- constexpr Atomic() = default;
+ LIBC_INLINE constexpr Atomic() = default;
// Intializes the value without using atomic operations.
- constexpr Atomic(value_type v) : val(v) {}
+ LIBC_INLINE constexpr Atomic(value_type v) : val(v) {}
- Atomic(const Atomic &) = delete;
- Atomic &operator=(const Atomic &) = delete;
+ LIBC_INLINE Atomic(const Atomic &) = delete;
+ LIBC_INLINE Atomic &operator=(const Atomic &) = delete;
// Atomic load.
- operator T() { return __atomic_load_n(&val, int(MemoryOrder::SEQ_CST)); }
+ LIBC_INLINE operator T() { return load(); }
- T load(MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
- [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
-#if __has_builtin(__scoped_atomic_load_n)
- return __scoped_atomic_load_n(&val, int(mem_ord), (int)(mem_scope));
+ LIBC_INLINE T
+ load(MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
+ [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ T res;
+#if __has_builtin(__scoped_atomic_load)
+ __scoped_atomic_load(addressof(val), addressof(res), order(mem_ord),
+ scope(mem_scope));
#else
- return __atomic_load_n(&val, int(mem_ord));
+ __atomic_load(addressof(val), addressof(res), order(mem_ord));
#endif
+ return res;
}
// Atomic store.
- T operator=(T rhs) {
- __atomic_store_n(&val, rhs, int(MemoryOrder::SEQ_CST));
+ LIBC_INLINE T operator=(T rhs) {
+ store(rhs);
return rhs;
}
- void store(T rhs, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
- [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
-#if __has_builtin(__scoped_atomic_store_n)
- __scoped_atomic_store_n(&val, rhs, int(mem_ord), (int)(mem_scope));
+ LIBC_INLINE void
+ store(T rhs, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
+ [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+#if __has_builtin(__scoped_atomic_store)
+ __scoped_atomic_store(addressof(val), addressof(rhs), order(mem_ord),
+ scope(mem_scope));
#else
- __atomic_store_n(&val, rhs, int(mem_ord));
+ __atomic_store(addressof(val), addressof(rhs), order(mem_ord));
#endif
}
// Atomic compare exchange
- bool compare_exchange_strong(
+ LIBC_INLINE bool compare_exchange_strong(
T &expected, T desired, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
- return __atomic_compare_exchange_n(&val, &expected, desired, false,
- int(mem_ord), int(mem_ord));
+ return __atomic_compare_exchange(addressof(val), addressof(expected),
+ addressof(desired), false, order(mem_ord),
+ order(mem_ord));
}
// Atomic compare exchange (separate success and failure memory orders)
- bool compare_exchange_strong(
+ LIBC_INLINE bool compare_exchange_strong(
T &expected, T desired, MemoryOrder success_order,
MemoryOrder failure_order,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
- return __atomic_compare_exchange_n(&val, &expected, desired, false,
- static_cast<int>(success_order),
- static_cast<int>(failure_order));
+ return __atomic_compare_exchange(
+ addressof(val), addressof(expected), addressof(desired), false,
+ order(success_order), order(failure_order));
}
// Atomic compare exchange (weak version)
- bool compare_exchange_weak(
+ LIBC_INLINE bool compare_exchange_weak(
T &expected, T desired, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
- return __atomic_compare_exchange_n(&val, &expected, desired, true,
- static_cast<int>(mem_ord),
- static_cast<int>(mem_ord));
+ return __atomic_compare_exchange(addressof(val), addressof(expected),
+ addressof(desired), true, order(mem_ord),
+ order(mem_ord));
}
// Atomic compare exchange (weak version with separate success and failure
// memory orders)
- bool compare_exchange_weak(
+ LIBC_INLINE bool compare_exchange_weak(
T &expected, T desired, MemoryOrder success_order,
MemoryOrder failure_order,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
- return __atomic_compare_exchange_n(&val, &expected, desired, true,
- static_cast<int>(success_order),
- static_cast<int>(failure_order));
+ return __atomic_compare_exchange(
+ addressof(val), addressof(expected), addressof(desired), true,
+ order(success_order), order(failure_order));
}
- T exchange(T desired, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
- [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
-#if __has_builtin(__scoped_atomic_exchange_n)
- return __scoped_atomic_exchange_n(&val, desired, int(mem_ord),
- (int)(mem_scope));
+ LIBC_INLINE T
+ exchange(T desired, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
+ [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ T ret;
+#if __has_builtin(__scoped_atomic_exchange)
+ __scoped_atomic_exchange(addressof(val), addressof(desired), addressof(ret),
+ order(mem_ord), scope(mem_scope));
#else
- return __atomic_exchange_n(&val, desired, int(mem_ord));
+ __atomic_exchange(addressof(val), addressof(desired), addressof(ret),
+ order(mem_ord));
#endif
+ return ret;
}
- T fetch_add(T increment, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
- [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ LIBC_INLINE T
+ fetch_add(T increment, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
+ [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ static_assert(cpp::is_integral_v<T>, "T must be an integral type.");
#if __has_builtin(__scoped_atomic_fetch_add)
- return __scoped_atomic_fetch_add(&val, increment, int(mem_ord),
- (int)(mem_scope));
+ return __scoped_atomic_fetch_add(addressof(val), increment, order(mem_ord),
+ scope(mem_scope));
#else
- return __atomic_fetch_add(&val, increment, int(mem_ord));
+ return __atomic_fetch_add(addressof(val), increment, order(mem_ord));
#endif
}
- T fetch_or(T mask, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
- [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ LIBC_INLINE T
+ fetch_or(T mask, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
+ [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ static_assert(cpp::is_integral_v<T>, "T must be an integral type.");
#if __has_builtin(__scoped_atomic_fetch_or)
- return __scoped_atomic_fetch_or(&val, mask, int(mem_ord), (int)(mem_scope));
+ return __scoped_atomic_fetch_or(addressof(val), mask, order(mem_ord),
+ scope(mem_scope));
#else
- return __atomic_fetch_or(&val, mask, int(mem_ord));
+ return __atomic_fetch_or(addressof(val), mask, order(mem_ord));
#endif
}
- T fetch_and(T mask, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
- [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ LIBC_INLINE T
+ fetch_and(T mask, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
+ [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ static_assert(cpp::is_integral_v<T>, "T must be an integral type.");
#if __has_builtin(__scoped_atomic_fetch_and)
- return __scoped_atomic_fetch_and(&val, mask, int(mem_ord),
- (int)(mem_scope));
+ return __scoped_atomic_fetch_and(addressof(val), mask, order(mem_ord),
+ scope(mem_scope));
#else
- return __atomic_fetch_and(&val, mask, int(mem_ord));
+ return __atomic_fetch_and(addressof(val), mask, order(mem_ord));
#endif
}
- T fetch_sub(T decrement, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
- [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ LIBC_INLINE T
+ fetch_sub(T decrement, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
+ [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+ static_assert(cpp::is_integral_v<T>, "T must be an integral type.");
#if __has_builtin(__scoped_atomic_fetch_sub)
- return __scoped_atomic_fetch_sub(&val, decrement, int(mem_ord),
- (int)(mem_scope));
+ return __scoped_atomic_fetch_sub(addressof(val), decrement, order(mem_ord),
+ scope(mem_scope));
#else
- return __atomic_fetch_sub(&val, decrement, int(mem_ord));
+ return __atomic_fetch_sub(addressof(val), decrement, order(mem_ord));
#endif
}
// Set the value without using an atomic operation. This is useful
// in initializing atomic values without a constructor.
- void set(T rhs) { val = rhs; }
+ LIBC_INLINE void set(T rhs) { val = rhs; }
};
// Issue a thread fence with the given memory ordering.
-LIBC_INLINE void atomic_thread_fence([[maybe_unused]] MemoryOrder mem_ord) {
-// The NVPTX backend currently does not support atomic thread fences so we use a
-// full system fence instead.
-#ifdef LIBC_TARGET_ARCH_IS_NVPTX
- __nvvm_membar_sys();
+LIBC_INLINE void atomic_thread_fence(
+ MemoryOrder mem_ord,
+ [[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
+#if __has_builtin(__scoped_atomic_thread_fence)
+ __scoped_atomic_thread_fence(static_cast<int>(mem_ord),
+ static_cast<int>(mem_scope));
#else
__atomic_thread_fence(static_cast<int>(mem_ord));
#endif
diff --git a/libc/src/__support/CPP/type_traits.h b/libc/src/__support/CPP/type_traits.h
index d50b6612656d..910cebbb8d05 100644
--- a/libc/src/__support/CPP/type_traits.h
+++ b/libc/src/__support/CPP/type_traits.h
@@ -18,6 +18,7 @@
#include "src/__support/CPP/type_traits/decay.h"
#include "src/__support/CPP/type_traits/enable_if.h"
#include "src/__support/CPP/type_traits/false_type.h"
+#include "src/__support/CPP/type_traits/has_unique_object_representations.h"
#include "src/__support/CPP/type_traits/integral_constant.h"
#include "src/__support/CPP/type_traits/invoke.h"
#include "src/__support/CPP/type_traits/invoke_result.h"
@@ -28,6 +29,8 @@
#include "src/__support/CPP/type_traits/is_const.h"
#include "src/__support/CPP/type_traits/is_constant_evaluated.h"
#include "src/__support/CPP/type_traits/is_convertible.h"
+#include "src/__support/CPP/type_traits/is_copy_assignable.h"
+#include "src/__support/CPP/type_traits/is_copy_constructible.h"
#include "src/__support/CPP/type_traits/is_destructible.h"
#include "src/__support/CPP/type_traits/is_enum.h"
#include "src/__support/CPP/type_traits/is_fixed_point.h"
@@ -36,6 +39,8 @@
#include "src/__support/CPP/type_traits/is_integral.h"
#include "src/__support/CPP/type_traits/is_lvalue_reference.h"
#include "src/__support/CPP/type_traits/is_member_pointer.h"
+#include "src/__support/CPP/type_traits/is_move_assignable.h"
+#include "src/__support/CPP/type_traits/is_move_constructible.h"
#include "src/__support/CPP/type_traits/is_null_pointer.h"
#include "src/__support/CPP/type_traits/is_object.h"
#include "src/__support/CPP/type_traits/is_pointer.h"
diff --git a/libc/src/__support/CPP/type_traits/has_unique_object_representations.h b/libc/src/__support/CPP/type_traits/has_unique_object_representations.h
new file mode 100644
index 000000000000..639fb69d2720
--- /dev/null
+++ b/libc/src/__support/CPP/type_traits/has_unique_object_representations.h
@@ -0,0 +1,30 @@
+//===-- has_unique_object_representations type_traits ------------*- C++-*-===//
+//
+// 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_CPP_TYPE_TRAITS_HAS_UNIQUE_OBJECT_REPRESENTATIONS_H
+#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_HAS_UNIQUE_OBJECT_REPRESENTATIONS_H
+
+#include "src/__support/CPP/type_traits/integral_constant.h"
+#include "src/__support/CPP/type_traits/remove_all_extents.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace cpp {
+
+template <class T>
+struct has_unique_object_representations
+ : public integral_constant<bool, __has_unique_object_representations(
+ remove_all_extents_t<T>)> {};
+
+template <class T>
+LIBC_INLINE_VAR constexpr bool has_unique_object_representations_v =
+ has_unique_object_representations<T>::value;
+
+} // namespace cpp
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_HAS_UNIQUE_OBJECT_REPRESENTATIONS_H
diff --git a/libc/src/__support/CPP/type_traits/is_copy_assignable.h b/libc/src/__support/CPP/type_traits/is_copy_assignable.h
new file mode 100644
index 000000000000..9beb93d14668
--- /dev/null
+++ b/libc/src/__support/CPP/type_traits/is_copy_assignable.h
@@ -0,0 +1,32 @@
+//===-- is_copy_assignable type_traits --------------------------*- C++ -*-===//
+//
+// 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_CPP_TYPE_TRAITS_IS_COPY_ASSIGNABLE_H
+#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_COPY_ASSIGNABLE_H
+
+#include "src/__support/CPP/type_traits/add_lvalue_reference.h"
+#include "src/__support/CPP/type_traits/integral_constant.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace cpp {
+
+// is copy assignable
+template <class T>
+struct is_copy_assignable
+ : public integral_constant<
+ bool, __is_assignable(cpp::add_lvalue_reference_t<T>,
+ cpp::add_lvalue_reference_t<const T>)> {};
+
+template <class T>
+LIBC_INLINE_VAR constexpr bool is_copy_assignable_v =
+ is_copy_assignable<T>::value;
+
+} // namespace cpp
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_COPY_ASSIGNABLE_H
diff --git a/libc/src/__support/CPP/type_traits/is_copy_constructible.h b/libc/src/__support/CPP/type_traits/is_copy_constructible.h
new file mode 100644
index 000000000000..d8eb9ad3507e
--- /dev/null
+++ b/libc/src/__support/CPP/type_traits/is_copy_constructible.h
@@ -0,0 +1,31 @@
+//===-- is_copy_constructible type_traits -----------------------*- C++ -*-===//
+//
+// 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_CPP_TYPE_TRAITS_IS_COPY_CONSTRUCTIBLE_H
+#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_COPY_CONSTRUCTIBLE_H
+
+#include "src/__support/CPP/type_traits/add_lvalue_reference.h"
+#include "src/__support/CPP/type_traits/integral_constant.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace cpp {
+
+// is copy constructible
+template <class T>
+struct is_copy_constructible
+ : public integral_constant<
+ bool, __is_constructible(T, cpp::add_lvalue_reference_t<const T>)> {};
+
+template <class T>
+LIBC_INLINE_VAR constexpr bool is_copy_constructible_v =
+ is_copy_constructible<T>::value;
+
+} // namespace cpp
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_COPY_CONSTRUCTIBLE_H
diff --git a/libc/src/__support/CPP/type_traits/is_move_assignable.h b/libc/src/__support/CPP/type_traits/is_move_assignable.h
new file mode 100644
index 000000000000..a788bd9074e3
--- /dev/null
+++ b/libc/src/__support/CPP/type_traits/is_move_assignable.h
@@ -0,0 +1,33 @@
+//===-- is_move_assignable type_traits --------------------------*- C++ -*-===//
+//
+// 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_CPP_TYPE_TRAITS_IS_MOVE_ASSIGNABLE_H
+#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_MOVE_ASSIGNABLE_H
+
+#include "src/__support/CPP/type_traits/add_lvalue_reference.h"
+#include "src/__support/CPP/type_traits/add_rvalue_reference.h"
+#include "src/__support/CPP/type_traits/integral_constant.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace cpp {
+
+// is move assignable
+template <class T>
+struct is_move_assignable
+ : public integral_constant<bool, __is_assignable(
+ cpp::add_lvalue_reference_t<T>,
+ cpp::add_rvalue_reference_t<T>)> {};
+
+template <class T>
+LIBC_INLINE_VAR constexpr bool is_move_assignable_v =
+ is_move_assignable<T>::value;
+
+} // namespace cpp
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_MOVE_ASSIGNABLE_H
diff --git a/libc/src/__support/CPP/type_traits/is_move_constructible.h b/libc/src/__support/CPP/type_traits/is_move_constructible.h
new file mode 100644
index 000000000000..c89896054625
--- /dev/null
+++ b/libc/src/__support/CPP/type_traits/is_move_constructible.h
@@ -0,0 +1,31 @@
+//===-- is_move_constructible type_traits ------------------------*- C++-*-===//
+//
+// 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_CPP_TYPE_TRAITS_IS_MOVE_CONSTRUCTIBLE_H
+#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_MOVE_CONSTRUCTIBLE_H
+
+#include "src/__support/CPP/type_traits/add_rvalue_reference.h"
+#include "src/__support/CPP/type_traits/integral_constant.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace cpp {
+
+// is move constructible
+template <class T>
+struct is_move_constructible
+ : public integral_constant<bool, __is_constructible(
+ T, cpp::add_rvalue_reference_t<T>)> {};
+
+template <class T>
+LIBC_INLINE_VAR constexpr bool is_move_constructible_v =
+ is_move_constructible<T>::value;
+
+} // namespace cpp
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_MOVE_CONSTRUCTIBLE_H
diff --git a/libc/src/__support/CPP/type_traits/is_trivially_copyable.h b/libc/src/__support/CPP/type_traits/is_trivially_copyable.h
index 68e56c854783..a3e786fe1d14 100644
--- a/libc/src/__support/CPP/type_traits/is_trivially_copyable.h
+++ b/libc/src/__support/CPP/type_traits/is_trivially_copyable.h
@@ -19,6 +19,10 @@ template <class T>
struct is_trivially_copyable
: public integral_constant<bool, __is_trivially_copyable(T)> {};
+template <class T>
+LIBC_INLINE_VAR constexpr bool is_trivially_copyable_v =
+ is_trivially_copyable<T>::value;
+
} // namespace cpp
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/FPUtil/aarch64/FEnvImpl.h b/libc/src/__support/FPUtil/aarch64/FEnvImpl.h
index 3cea9772154f..18b0631324f8 100644
--- a/libc/src/__support/FPUtil/aarch64/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/aarch64/FEnvImpl.h
@@ -26,7 +26,6 @@
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
-
struct FEnv {
struct FPState {
uint32_t ControlWord;
@@ -42,11 +41,11 @@ struct FEnv {
static constexpr uint32_t DOWNWARD = 0x2;
static constexpr uint32_t TOWARDZERO = 0x3;
- static constexpr uint32_t INVALID = 0x1;
- static constexpr uint32_t DIVBYZERO = 0x2;
- static constexpr uint32_t OVERFLOW = 0x4;
- static constexpr uint32_t UNDERFLOW = 0x8;
- static constexpr uint32_t INEXACT = 0x10;
+ static constexpr uint32_t INVALID_F = 0x1;
+ static constexpr uint32_t DIVBYZERO_F = 0x2;
+ static constexpr uint32_t OVERFLOW_F = 0x4;
+ static constexpr uint32_t UNDERFLOW_F = 0x8;
+ static constexpr uint32_t INEXACT_F = 0x10;
// Zero-th bit is the first bit.
static constexpr uint32_t RoundingControlBitPosition = 22;
@@ -54,19 +53,19 @@ struct FEnv {
static constexpr uint32_t ExceptionControlFlagsBitPosition = 8;
LIBC_INLINE static uint32_t getStatusValueForExcept(int excepts) {
- return ((excepts & FE_INVALID) ? INVALID : 0) |
- ((excepts & FE_DIVBYZERO) ? DIVBYZERO : 0) |
- ((excepts & FE_OVERFLOW) ? OVERFLOW : 0) |
- ((excepts & FE_UNDERFLOW) ? UNDERFLOW : 0) |
- ((excepts & FE_INEXACT) ? INEXACT : 0);
+ return ((excepts & FE_INVALID) ? INVALID_F : 0) |
+ ((excepts & FE_DIVBYZERO) ? DIVBYZERO_F : 0) |
+ ((excepts & FE_OVERFLOW) ? OVERFLOW_F : 0) |
+ ((excepts & FE_UNDERFLOW) ? UNDERFLOW_F : 0) |
+ ((excepts & FE_INEXACT) ? INEXACT_F : 0);
}
LIBC_INLINE static int exceptionStatusToMacro(uint32_t status) {
- return ((status & INVALID) ? FE_INVALID : 0) |
- ((status & DIVBYZERO) ? FE_DIVBYZERO : 0) |
- ((status & OVERFLOW) ? FE_OVERFLOW : 0) |
- ((status & UNDERFLOW) ? FE_UNDERFLOW : 0) |
- ((status & INEXACT) ? FE_INEXACT : 0);
+ return ((status & INVALID_F) ? FE_INVALID : 0) |
+ ((status & DIVBYZERO_F) ? FE_DIVBYZERO : 0) |
+ ((status & OVERFLOW_F) ? FE_OVERFLOW : 0) |
+ ((status & UNDERFLOW_F) ? FE_UNDERFLOW : 0) |
+ ((status & INEXACT_F) ? FE_INEXACT : 0);
}
static uint32_t getControlWord() {
@@ -171,36 +170,36 @@ LIBC_INLINE int raise_except(int excepts) {
uint32_t toRaise = FEnv::getStatusValueForExcept(excepts);
int result = 0;
- if (toRaise & FEnv::INVALID) {
+ if (toRaise & FEnv::INVALID_F) {
divfunc(zero, zero);
uint32_t statusWord = FEnv::getStatusWord();
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
- FEnv::INVALID))
+ FEnv::INVALID_F))
result = -1;
}
- if (toRaise & FEnv::DIVBYZERO) {
+ if (toRaise & FEnv::DIVBYZERO_F) {
divfunc(one, zero);
uint32_t statusWord = FEnv::getStatusWord();
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
- FEnv::DIVBYZERO))
+ FEnv::DIVBYZERO_F))
result = -1;
}
- if (toRaise & FEnv::OVERFLOW) {
+ if (toRaise & FEnv::OVERFLOW_F) {
divfunc(largeValue, smallValue);
uint32_t statusWord = FEnv::getStatusWord();
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
- FEnv::OVERFLOW))
+ FEnv::OVERFLOW_F))
result = -1;
}
- if (toRaise & FEnv::UNDERFLOW) {
+ if (toRaise & FEnv::UNDERFLOW_F) {
divfunc(smallValue, largeValue);
uint32_t statusWord = FEnv::getStatusWord();
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
- FEnv::UNDERFLOW))
+ FEnv::UNDERFLOW_F))
result = -1;
}
- if (toRaise & FEnv::INEXACT) {
+ if (toRaise & FEnv::INEXACT_F) {
float two = 2.0f;
float three = 3.0f;
// 2.0 / 3.0 cannot be represented exactly in any radix 2 floating point
@@ -208,7 +207,7 @@ LIBC_INLINE int raise_except(int excepts) {
divfunc(two, three);
uint32_t statusWord = FEnv::getStatusWord();
if (!((statusWord >> FEnv::ExceptionStatusFlagsBitPosition) &
- FEnv::INEXACT))
+ FEnv::INEXACT_F))
result = -1;
}
return result;
@@ -278,7 +277,6 @@ LIBC_INLINE int set_env(const fenv_t *envp) {
FEnv::writeStatusWord(state->StatusWord);
return 0;
}
-
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/RPC/CMakeLists.txt b/libc/src/__support/RPC/CMakeLists.txt
index 0a7141fb60bf..cac9c4e05e36 100644
--- a/libc/src/__support/RPC/CMakeLists.txt
+++ b/libc/src/__support/RPC/CMakeLists.txt
@@ -9,6 +9,5 @@ add_object_library(
HDRS
rpc_client.h
DEPENDS
- libc.include.gpu_rpc
libc.src.__support.GPU.utils
)
diff --git a/libc/src/__support/complex_type.h b/libc/src/__support/complex_type.h
index d6b5eec8fd6b..f72ce8a4efd1 100644
--- a/libc/src/__support/complex_type.h
+++ b/libc/src/__support/complex_type.h
@@ -9,12 +9,86 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
#define LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
+#include "src/__support/CPP/bit.h"
+#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/complex_types.h"
+#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
template <typename T> struct Complex {
T real;
T imag;
};
+
+template <typename T> struct make_complex;
+
+template <> struct make_complex<float> {
+ using type = _Complex float;
+};
+template <> struct make_complex<double> {
+ using type = _Complex double;
+};
+template <> struct make_complex<long double> {
+ using type = _Complex long double;
+};
+
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+template <> struct make_complex<float16> {
+ using type = cfloat16;
+};
+#endif
+#ifdef LIBC_TYPES_CFLOAT128_IS_NOT_COMPLEX_LONG_DOUBLE
+template <> struct make_complex<float128> {
+ using type = cfloat128;
+};
+#endif
+
+template <typename T> using make_complex_t = typename make_complex<T>::type;
+
+template <typename T> struct make_real;
+
+template <> struct make_real<_Complex float> {
+ using type = float;
+};
+template <> struct make_real<_Complex double> {
+ using type = double;
+};
+template <> struct make_real<_Complex long double> {
+ using type = long double;
+};
+
+#if defined(LIBC_TYPES_HAS_CFLOAT16)
+template <> struct make_real<cfloat16> {
+ using type = float16;
+};
+#endif
+#ifdef LIBC_TYPES_CFLOAT128_IS_NOT_COMPLEX_LONG_DOUBLE
+template <> struct make_real<cfloat128> {
+ using type = float128;
+};
+#endif
+
+template <typename T> using make_real_t = typename make_real<T>::type;
+
+template <typename T> LIBC_INLINE constexpr T conjugate(T c) {
+ Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
+ c_c.imag = -c_c.imag;
+ return cpp::bit_cast<T>(c_c);
+}
+
+template <typename T> LIBC_INLINE constexpr T project(T c) {
+ using real_t = make_real_t<T>;
+ Complex<real_t> c_c = cpp::bit_cast<Complex<real_t>>(c);
+ if (fputil::FPBits<real_t>(c_c.real).is_inf() ||
+ fputil::FPBits<real_t>(c_c.imag).is_inf()) {
+ return cpp::bit_cast<T>(
+ Complex<real_t>{(fputil::FPBits<real_t>::inf(Sign::POS).get_val()),
+ static_cast<real_t>(c_c.imag > 0 ? 0.0 : -0.0)});
+ } else {
+ return c;
+ }
+}
+
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
diff --git a/libc/src/__support/freelist_heap.cpp b/libc/src/__support/freelist_heap.cpp
new file mode 100644
index 000000000000..4deb0e0f09e2
--- /dev/null
+++ b/libc/src/__support/freelist_heap.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation for freelist_heap ----------------------------------===//
+//
+// 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/freelist_heap.h"
+#include "src/__support/macros/config.h"
+
+#include <stddef.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+static LIBC_CONSTINIT FreeListHeap freelist_heap_symbols;
+FreeListHeap *freelist_heap = &freelist_heap_symbols;
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/macros/properties/complex_types.h b/libc/src/__support/macros/properties/complex_types.h
index 3f4a7646649c..ede4d6b7c7d9 100644
--- a/libc/src/__support/macros/properties/complex_types.h
+++ b/libc/src/__support/macros/properties/complex_types.h
@@ -22,4 +22,9 @@
// LIBC_TYPES_HAS_CFLOAT128 and 'cfloat128' type are provided by
// "include/llvm-libc-types/cfloat128.h"
+#if defined(LIBC_TYPES_HAS_CFLOAT128) && \
+ !defined(LIBC_TYPES_CFLOAT128_IS_COMPLEX_LONG_DOUBLE)
+#define LIBC_TYPES_CFLOAT128_IS_NOT_COMPLEX_LONG_DOUBLE
+#endif
+
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_CTYPES_H
diff --git a/libc/src/__support/macros/properties/os.h b/libc/src/__support/macros/properties/os.h
index 1c8fd5721ce6..807ce1812735 100644
--- a/libc/src/__support/macros/properties/os.h
+++ b/libc/src/__support/macros/properties/os.h
@@ -25,18 +25,6 @@
#define LIBC_TARGET_OS_IS_WINDOWS
#endif
-#if (defined(__apple__) || defined(__APPLE__) || defined(__MACH__))
-// From https://stackoverflow.com/a/49560690
-#include "TargetConditionals.h"
-#if defined(TARGET_OS_OSX)
-#define LIBC_TARGET_OS_IS_MACOS
-#endif
-#if defined(TARGET_OS_IPHONE)
-// This is set for any non-Mac Apple products (IOS, TV, WATCH)
-#define LIBC_TARGET_OS_IS_IPHONE
-#endif
-#endif
-
#if defined(__Fuchsia__)
#define LIBC_TARGET_OS_IS_FUCHSIA
#endif
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index 30c742c007ca..6293b9d4d292 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -31,6 +31,11 @@
#define LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
#endif
+#if defined(LIBC_TYPES_HAS_FLOAT128) && \
+ !defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT128)
+#define LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
+#endif
+
// int64 / uint64 support
#if defined(UINT64_MAX)
#define LIBC_TYPES_HAS_INT64
diff --git a/libc/src/__support/time/CMakeLists.txt b/libc/src/__support/time/CMakeLists.txt
index 89ddffb09938..8247e792e841 100644
--- a/libc/src/__support/time/CMakeLists.txt
+++ b/libc/src/__support/time/CMakeLists.txt
@@ -1,7 +1,3 @@
-if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
- add_subdirectory(${LIBC_TARGET_OS})
-endif()
-
add_header_library(
units
HDRS
@@ -10,3 +6,16 @@ add_header_library(
libc.src.__support.common
libc.hdr.types.time_t
)
+
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+else()
+ return()
+endif()
+
+add_object_library(
+ clock_gettime
+ ALIAS
+ DEPENDS
+ libc.src.__support.time.${LIBC_TARGET_OS}.clock_gettime
+)
diff --git a/libc/src/__support/time/linux/clock_gettime.h b/libc/src/__support/time/clock_gettime.h
index f7f996ce7c19..584bf546cd60 100644
--- a/libc/src/__support/time/linux/clock_gettime.h
+++ b/libc/src/__support/time/clock_gettime.h
@@ -6,21 +6,17 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
-#define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
+#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
#include "hdr/types/clockid_t.h"
#include "hdr/types/struct_timespec.h"
#include "src/__support/error_or.h"
-#if defined(SYS_clock_gettime64)
-#include <linux/time_types.h>
-#endif
-
namespace LIBC_NAMESPACE_DECL {
namespace internal {
ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts);
} // namespace internal
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_GETTIME_H
+#endif // LLVM_LIBC_SRC___SUPPORT_TIME_CLOCK_GETTIME_H
diff --git a/libc/src/__support/time/gpu/CMakeLists.txt b/libc/src/__support/time/gpu/CMakeLists.txt
new file mode 100644
index 000000000000..fc465e0cea25
--- /dev/null
+++ b/libc/src/__support/time/gpu/CMakeLists.txt
@@ -0,0 +1,22 @@
+add_object_library(
+ time_utils
+ SRCS
+ time_utils.cpp
+ HDRS
+ time_utils.h
+ DEPENDS
+ libc.hdr.types.clock_t
+ libc.hdr.time_macros
+)
+
+add_object_library(
+ clock_gettime
+ SRCS
+ clock_gettime.cpp
+ HDRS
+ ../clock_gettime.h
+ DEPENDS
+ libc.hdr.types.clockid_t
+ libc.hdr.types.struct_timespec
+ .time_utils
+)
diff --git a/libc/src/__support/time/gpu/clock_gettime.cpp b/libc/src/__support/time/gpu/clock_gettime.cpp
new file mode 100644
index 000000000000..cede72a1f35d
--- /dev/null
+++ b/libc/src/__support/time/gpu/clock_gettime.cpp
@@ -0,0 +1,33 @@
+//===---------- GPU implementation of the clock_gettime function ----------===//
+//
+// 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/time/clock_gettime.h"
+
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/__support/time/gpu/time_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
+
+ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
+ if (clockid != CLOCK_MONOTONIC || !ts)
+ return cpp::unexpected(-1);
+
+ uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
+ uint64_t ticks = gpu::fixed_frequency_clock();
+
+ ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
+ ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;
+
+ return 0;
+}
+} // namespace internal
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/gpu/time_utils.cpp b/libc/src/__support/time/gpu/time_utils.cpp
index 38e09f600f36..38e09f600f36 100644
--- a/libc/src/time/gpu/time_utils.cpp
+++ b/libc/src/__support/time/gpu/time_utils.cpp
diff --git a/libc/src/time/gpu/time_utils.h b/libc/src/__support/time/gpu/time_utils.h
index c631a38d91ba..315506c897dc 100644
--- a/libc/src/time/gpu/time_utils.h
+++ b/libc/src/__support/time/gpu/time_utils.h
@@ -38,6 +38,8 @@ extern gpu::Constant<uint64_t> __llvm_libc_clock_freq;
#error "Unsupported target"
#endif
+constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
+
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_TIME_GPU_TIME_UTILS_H
diff --git a/libc/src/__support/time/linux/CMakeLists.txt b/libc/src/__support/time/linux/CMakeLists.txt
index 94ed09e65215..6fec7eeba99a 100644
--- a/libc/src/__support/time/linux/CMakeLists.txt
+++ b/libc/src/__support/time/linux/CMakeLists.txt
@@ -1,7 +1,7 @@
add_object_library(
clock_gettime
HDRS
- clock_gettime.h
+ ../clock_gettime.h
SRCS
clock_gettime.cpp
DEPENDS
diff --git a/libc/src/__support/time/linux/clock_conversion.h b/libc/src/__support/time/linux/clock_conversion.h
index 7a52873263a1..ac5357d308d7 100644
--- a/libc/src/__support/time/linux/clock_conversion.h
+++ b/libc/src/__support/time/linux/clock_conversion.h
@@ -10,7 +10,7 @@
#define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CLOCK_CONVERSION_H
#include "src/__support/macros/config.h"
-#include "src/__support/time/linux/clock_gettime.h"
+#include "src/__support/time/clock_gettime.h"
#include "src/__support/time/units.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/__support/time/linux/clock_gettime.cpp b/libc/src/__support/time/linux/clock_gettime.cpp
index 3a0eca417724..944fc0a2b80f 100644
--- a/libc/src/__support/time/linux/clock_gettime.cpp
+++ b/libc/src/__support/time/linux/clock_gettime.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/time/linux/clock_gettime.h"
+#include "src/__support/time/clock_gettime.h"
#include "hdr/types/clockid_t.h"
#include "hdr/types/struct_timespec.h"
#include "src/__support/OSUtil/linux/vdso.h"
diff --git a/libc/src/__support/time/windows/CMakeLists.txt b/libc/src/__support/time/windows/CMakeLists.txt
new file mode 100644
index 000000000000..dd0ac2f2f79a
--- /dev/null
+++ b/libc/src/__support/time/windows/CMakeLists.txt
@@ -0,0 +1,26 @@
+add_header_library(
+ performance_counter
+ HDRS
+ performance_counter.h
+ DEPENDS
+ libc.src.__support.CPP.atomic
+ libc.src.__support.common
+)
+
+add_object_library(
+ clock_gettime
+ HDRS
+ ../clock_gettime.h
+ SRCS
+ clock_gettime.cpp
+ DEPENDS
+ .performance_counter
+ libc.hdr.types.struct_timespec
+ libc.hdr.types.clockid_t
+ libc.hdr.errno_macros
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.CPP.atomic
+ libc.src.__support.CPP.limits
+)
diff --git a/libc/src/__support/time/windows/clock_gettime.cpp b/libc/src/__support/time/windows/clock_gettime.cpp
new file mode 100644
index 000000000000..813eae0df970
--- /dev/null
+++ b/libc/src/__support/time/windows/clock_gettime.cpp
@@ -0,0 +1,134 @@
+//===--- clock_gettime windows implementation -------------------*- C++ -*-===//
+//
+// 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 "hdr/time_macros.h"
+
+#include "src/__support/CPP/atomic.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/CPP/limits.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/__support/time/units.h"
+#include "src/__support/time/windows/performance_counter.h"
+
+#define WIN32_LEAN_AND_MEAN
+#define NOMINMAX
+#include <Windows.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
+ using namespace time_units;
+ constexpr unsigned long long HNS_PER_SEC = 1_s_ns / 100ULL;
+ constexpr long long SEC_LIMIT =
+ cpp::numeric_limits<decltype(ts->tv_sec)>::max();
+ ErrorOr<int> ret = 0;
+ switch (clockid) {
+ default:
+ ret = cpp::unexpected(EINVAL);
+ break;
+
+ case CLOCK_MONOTONIC: {
+ // see
+ // https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
+ // Is the performance counter monotonic (non-decreasing)?
+ // Yes. performance_counter does not go backward.
+ [[clang::uninitialized]] LARGE_INTEGER buffer;
+ // On systems that run Windows XP or later, the function will always
+ // succeed and will thus never return zero.
+ ::QueryPerformanceCounter(&buffer);
+ long long freq = performance_counter::get_ticks_per_second();
+ long long ticks = buffer.QuadPart;
+ long long tv_sec = ticks / freq;
+ long long tv_nsec = (ticks % freq) * 1_s_ns / freq;
+ if (LIBC_UNLIKELY(tv_sec > SEC_LIMIT)) {
+ ret = cpp::unexpected(EOVERFLOW);
+ break;
+ }
+ ts->tv_sec = static_cast<decltype(ts->tv_sec)>(tv_sec);
+ ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(tv_nsec);
+ break;
+ }
+ case CLOCK_REALTIME: {
+ // https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
+ // GetSystemTimePreciseAsFileTime
+ // This function is best suited for high-resolution time-of-day
+ // measurements, or time stamps that are synchronized to UTC
+ [[clang::uninitialized]] FILETIME file_time;
+ [[clang::uninitialized]] ULARGE_INTEGER time;
+ ::GetSystemTimePreciseAsFileTime(&file_time);
+ time.LowPart = file_time.dwLowDateTime;
+ time.HighPart = file_time.dwHighDateTime;
+
+ // adjust to POSIX epoch (from Jan 1, 1601 to Jan 1, 1970)
+ constexpr unsigned long long POSIX_TIME_SHIFT =
+ (11644473600ULL * HNS_PER_SEC);
+ if (LIBC_UNLIKELY(POSIX_TIME_SHIFT > time.QuadPart)) {
+ ret = cpp::unexpected(EOVERFLOW);
+ break;
+ }
+ time.QuadPart -= (11644473600ULL * HNS_PER_SEC);
+ unsigned long long tv_sec = time.QuadPart / HNS_PER_SEC;
+ unsigned long long tv_nsec = (time.QuadPart % HNS_PER_SEC) * 100ULL;
+ if (LIBC_UNLIKELY(tv_sec > SEC_LIMIT)) {
+ ret = cpp::unexpected(EOVERFLOW);
+ break;
+ }
+ ts->tv_sec = static_cast<decltype(ts->tv_sec)>(tv_sec);
+ ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(tv_nsec);
+ break;
+ }
+ case CLOCK_PROCESS_CPUTIME_ID:
+ case CLOCK_THREAD_CPUTIME_ID: {
+ [[clang::uninitialized]] FILETIME creation_time;
+ [[clang::uninitialized]] FILETIME exit_time;
+ [[clang::uninitialized]] FILETIME kernel_time;
+ [[clang::uninitialized]] FILETIME user_time;
+ bool success;
+ if (clockid == CLOCK_PROCESS_CPUTIME_ID) {
+ // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesstimes
+ success = ::GetProcessTimes(::GetCurrentProcess(), &creation_time,
+ &exit_time, &kernel_time, &user_time);
+ } else {
+ // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes
+ success = ::GetThreadTimes(::GetCurrentThread(), &creation_time,
+ &exit_time, &kernel_time, &user_time);
+ }
+ if (!success) {
+ ret = cpp::unexpected(EINVAL);
+ break;
+ }
+ // https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
+ // It is not recommended that you add and subtract values from the FILETIME
+ // structure to obtain relative times. Instead, you should copy the low- and
+ // high-order parts of the file time to a ULARGE_INTEGER structure, perform
+ // 64-bit arithmetic on the QuadPart member, and copy the LowPart and
+ // HighPart members into the FILETIME structure.
+ auto kernel_time_hns = cpp::bit_cast<ULARGE_INTEGER>(kernel_time);
+ auto user_time_hns = cpp::bit_cast<ULARGE_INTEGER>(user_time);
+ unsigned long long total_time_hns =
+ kernel_time_hns.QuadPart + user_time_hns.QuadPart;
+
+ unsigned long long tv_sec = total_time_hns / HNS_PER_SEC;
+ unsigned long long tv_nsec = (total_time_hns % HNS_PER_SEC) * 100ULL;
+
+ if (LIBC_UNLIKELY(tv_sec > SEC_LIMIT)) {
+ ret = cpp::unexpected(EOVERFLOW);
+ break;
+ }
+
+ ts->tv_sec = static_cast<decltype(ts->tv_sec)>(tv_sec);
+ ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(tv_nsec);
+
+ break;
+ }
+ }
+ return ret;
+}
+} // namespace internal
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/time/windows/performance_counter.h b/libc/src/__support/time/windows/performance_counter.h
new file mode 100644
index 000000000000..3df81b3efff4
--- /dev/null
+++ b/libc/src/__support/time/windows/performance_counter.h
@@ -0,0 +1,35 @@
+//===--- Cached Performance Counter Frequency ----------------------------===//
+//
+// 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/CPP/atomic.h"
+#include "src/__support/common.h"
+
+#define WIN32_LEAN_AND_MEAN
+#define NOMINMAX
+#include <Windows.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace performance_counter {
+LIBC_INLINE long long get_ticks_per_second() {
+ static cpp::Atomic<long long> frequency = 0;
+ // Relaxed ordering is enough. It is okay to record the frequency multiple
+ // times. The store operation itself is atomic and the value must propagate
+ // as required by cache coherence.
+ auto freq = frequency.load(cpp::MemoryOrder::RELAXED);
+ if (!freq) {
+ [[clang::uninitialized]] LARGE_INTEGER buffer;
+ // On systems that run Windows XP or later, the function will always
+ // succeed and will thus never return zero.
+ ::QueryPerformanceFrequency(&buffer);
+ frequency.store(buffer.QuadPart, cpp::MemoryOrder::RELAXED);
+ return buffer.QuadPart;
+ }
+ return freq;
+}
+} // namespace performance_counter
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/arpa/CMakeLists.txt b/libc/src/arpa/CMakeLists.txt
new file mode 100644
index 000000000000..5c89828860ff
--- /dev/null
+++ b/libc/src/arpa/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(inet)
diff --git a/libc/src/network/CMakeLists.txt b/libc/src/arpa/inet/CMakeLists.txt
index cdd53e915034..cdd53e915034 100644
--- a/libc/src/network/CMakeLists.txt
+++ b/libc/src/arpa/inet/CMakeLists.txt
diff --git a/libc/src/network/htonl.cpp b/libc/src/arpa/inet/htonl.cpp
index 681786adea68..fb0404a26a24 100644
--- a/libc/src/network/htonl.cpp
+++ b/libc/src/arpa/inet/htonl.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/network/htonl.h"
+#include "src/arpa/inet/htonl.h"
#include "src/__support/common.h"
#include "src/__support/endian_internal.h"
#include "src/__support/macros/config.h"
diff --git a/libc/src/network/htonl.h b/libc/src/arpa/inet/htonl.h
index 14217310a227..e444972c771e 100644
--- a/libc/src/network/htonl.h
+++ b/libc/src/arpa/inet/htonl.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_NETWORK_HTONL_H
-#define LLVM_LIBC_SRC_NETWORK_HTONL_H
+#ifndef LLVM_LIBC_SRC_ARPA_INET_HTONL_H
+#define LLVM_LIBC_SRC_ARPA_INET_HTONL_H
#include "src/__support/macros/config.h"
#include <stdint.h>
@@ -18,4 +18,4 @@ uint32_t htonl(uint32_t hostlong);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_NETWORK_HTONL_H
+#endif // LLVM_LIBC_SRC_ARPA_INET_HTONL_H
diff --git a/libc/src/network/htons.cpp b/libc/src/arpa/inet/htons.cpp
index 675f53cf4a0a..1fcbbdf67deb 100644
--- a/libc/src/network/htons.cpp
+++ b/libc/src/arpa/inet/htons.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/network/htons.h"
+#include "src/arpa/inet/htons.h"
#include "src/__support/common.h"
#include "src/__support/endian_internal.h"
#include "src/__support/macros/config.h"
diff --git a/libc/src/network/htons.h b/libc/src/arpa/inet/htons.h
index 8f5c68a0a60a..35c2acdcfae4 100644
--- a/libc/src/network/htons.h
+++ b/libc/src/arpa/inet/htons.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_NETWORK_HTONS_H
-#define LLVM_LIBC_SRC_NETWORK_HTONS_H
+#ifndef LLVM_LIBC_SRC_ARPA_INET_HTONS_H
+#define LLVM_LIBC_SRC_ARPA_INET_HTONS_H
#include "src/__support/macros/config.h"
#include <stdint.h>
@@ -18,4 +18,4 @@ uint16_t htons(uint16_t hostshort);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_NETWORK_HTONS_H
+#endif // LLVM_LIBC_SRC_ARPA_INET_HTONS_H
diff --git a/libc/src/network/ntohl.cpp b/libc/src/arpa/inet/ntohl.cpp
index 6a309e97fca7..d472107a1988 100644
--- a/libc/src/network/ntohl.cpp
+++ b/libc/src/arpa/inet/ntohl.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/network/ntohl.h"
+#include "src/arpa/inet/ntohl.h"
#include "src/__support/common.h"
#include "src/__support/endian_internal.h"
#include "src/__support/macros/config.h"
diff --git a/libc/src/network/ntohl.h b/libc/src/arpa/inet/ntohl.h
index c32595194806..40079650d6fd 100644
--- a/libc/src/network/ntohl.h
+++ b/libc/src/arpa/inet/ntohl.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_NETWORK_NTOHL_H
-#define LLVM_LIBC_SRC_NETWORK_NTOHL_H
+#ifndef LLVM_LIBC_SRC_ARPA_INET_NTOHL_H
+#define LLVM_LIBC_SRC_ARPA_INET_NTOHL_H
#include "src/__support/macros/config.h"
#include <stdint.h>
@@ -18,4 +18,4 @@ uint32_t ntohl(uint32_t netlong);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_NETWORK_NTOHL_H
+#endif // LLVM_LIBC_SRC_ARPA_INET_NTOHL_H
diff --git a/libc/src/network/ntohs.cpp b/libc/src/arpa/inet/ntohs.cpp
index b51ecb93241c..9082ed928778 100644
--- a/libc/src/network/ntohs.cpp
+++ b/libc/src/arpa/inet/ntohs.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/network/ntohs.h"
+#include "src/arpa/inet/ntohs.h"
#include "src/__support/common.h"
#include "src/__support/endian_internal.h"
#include "src/__support/macros/config.h"
diff --git a/libc/src/network/ntohs.h b/libc/src/arpa/inet/ntohs.h
index f55591415f76..5fe3ebcb6252 100644
--- a/libc/src/network/ntohs.h
+++ b/libc/src/arpa/inet/ntohs.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_NETWORK_NTOHS_H
-#define LLVM_LIBC_SRC_NETWORK_NTOHS_H
+#ifndef LLVM_LIBC_SRC_ARPA_INET_NTOHS_H
+#define LLVM_LIBC_SRC_ARPA_INET_NTOHS_H
#include "src/__support/macros/config.h"
#include <stdint.h>
@@ -18,4 +18,4 @@ uint16_t ntohs(uint16_t netshort);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_NETWORK_NTOHS_H
+#endif // LLVM_LIBC_SRC_ARPA_INET_NTOHS_H
diff --git a/libc/src/compiler/generic/__stack_chk_fail.cpp b/libc/src/compiler/generic/__stack_chk_fail.cpp
index c76ec1407ad3..00e976ad8bc2 100644
--- a/libc/src/compiler/generic/__stack_chk_fail.cpp
+++ b/libc/src/compiler/generic/__stack_chk_fail.cpp
@@ -9,9 +9,12 @@
#include "src/compiler/__stack_chk_fail.h"
#include "src/__support/OSUtil/io.h"
#include "src/stdlib/abort.h"
+#include <stdint.h> // For uintptr_t
extern "C" {
+uintptr_t __stack_chk_guard = static_cast<uintptr_t>(0xa9fff01234);
+
void __stack_chk_fail(void) {
LIBC_NAMESPACE::write_to_stderr("stack smashing detected\n");
LIBC_NAMESPACE::abort();
diff --git a/libc/src/complex/CMakeLists.txt b/libc/src/complex/CMakeLists.txt
index 289cce5455af..bc66a5445d72 100644
--- a/libc/src/complex/CMakeLists.txt
+++ b/libc/src/complex/CMakeLists.txt
@@ -24,3 +24,15 @@ add_complex_entrypoint_object(cimagf)
add_complex_entrypoint_object(cimagl)
add_complex_entrypoint_object(cimagf16)
add_complex_entrypoint_object(cimagf128)
+
+add_complex_entrypoint_object(conj)
+add_complex_entrypoint_object(conjf)
+add_complex_entrypoint_object(conjl)
+add_complex_entrypoint_object(conjf16)
+add_complex_entrypoint_object(conjf128)
+
+add_complex_entrypoint_object(cproj)
+add_complex_entrypoint_object(cprojf)
+add_complex_entrypoint_object(cprojl)
+add_complex_entrypoint_object(cprojf16)
+add_complex_entrypoint_object(cprojf128)
diff --git a/libc/src/complex/cimagf128.h b/libc/src/complex/cimagf128.h
index ab8f9ac7da58..aaf52cfc54ef 100644
--- a/libc/src/complex/cimagf128.h
+++ b/libc/src/complex/cimagf128.h
@@ -6,15 +6,12 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/macros/properties/complex_types.h"
-#include "src/__support/macros/properties/types.h"
-
-#if defined(LIBC_TYPES_HAS_CFLOAT128)
-
#ifndef LLVM_LIBC_SRC_COMPLEX_CIMAGF128_H
#define LLVM_LIBC_SRC_COMPLEX_CIMAGF128_H
#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/complex_types.h"
+#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
@@ -23,5 +20,3 @@ float128 cimagf128(cfloat128 x);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_COMPLEX_CIMAGF128_H
-
-#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/cimagf16.h b/libc/src/complex/cimagf16.h
index 5c5de2eb1bcf..81ed4d2ce567 100644
--- a/libc/src/complex/cimagf16.h
+++ b/libc/src/complex/cimagf16.h
@@ -6,15 +6,12 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/macros/properties/complex_types.h"
-#include "src/__support/macros/properties/types.h"
-
-#if defined(LIBC_TYPES_HAS_CFLOAT16)
-
#ifndef LLVM_LIBC_SRC_COMPLEX_CIMAGF16_H
#define LLVM_LIBC_SRC_COMPLEX_CIMAGF16_H
#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/complex_types.h"
+#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
@@ -23,5 +20,3 @@ float16 cimagf16(cfloat16 x);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_COMPLEX_CIMAGF16_H
-
-#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/conj.h b/libc/src/complex/conj.h
new file mode 100644
index 000000000000..2ff82d275862
--- /dev/null
+++ b/libc/src/complex/conj.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for conj --------------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CONJ_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJ_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex double conj(_Complex double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJ_H
diff --git a/libc/src/complex/conjf.h b/libc/src/complex/conjf.h
new file mode 100644
index 000000000000..6b3bd612e5bf
--- /dev/null
+++ b/libc/src/complex/conjf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for conjf -------------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CONJF_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJF_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex float conjf(_Complex float x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJF_H
diff --git a/libc/src/complex/conjf128.h b/libc/src/complex/conjf128.h
new file mode 100644
index 000000000000..cae01d3f0069
--- /dev/null
+++ b/libc/src/complex/conjf128.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for conjf128 ----------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CONJF128_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJF128_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/complex_types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+cfloat128 conjf128(cfloat128 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJF128_H
diff --git a/libc/src/complex/conjf16.h b/libc/src/complex/conjf16.h
new file mode 100644
index 000000000000..dde1221473e4
--- /dev/null
+++ b/libc/src/complex/conjf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for conjf16 -----------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CONJF16_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/complex_types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+cfloat16 conjf16(cfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJF16_H
diff --git a/libc/src/complex/conjl.h b/libc/src/complex/conjl.h
new file mode 100644
index 000000000000..aec640f9433a
--- /dev/null
+++ b/libc/src/complex/conjl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for conjl -------------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CONJL_H
+#define LLVM_LIBC_SRC_COMPLEX_CONJL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex long double conjl(_Complex long double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CONJL_H
diff --git a/libc/src/complex/cproj.h b/libc/src/complex/cproj.h
new file mode 100644
index 000000000000..62d41bceec3e
--- /dev/null
+++ b/libc/src/complex/cproj.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cproj -------------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CPROJ_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJ_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex double cproj(_Complex double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJ_H
diff --git a/libc/src/complex/cprojf.h b/libc/src/complex/cprojf.h
new file mode 100644
index 000000000000..76124f911777
--- /dev/null
+++ b/libc/src/complex/cprojf.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cprojf ------------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CPROJF_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJF_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex float cprojf(_Complex float x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJF_H
diff --git a/libc/src/complex/cprojf128.h b/libc/src/complex/cprojf128.h
new file mode 100644
index 000000000000..71c1bbec2218
--- /dev/null
+++ b/libc/src/complex/cprojf128.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for cprojf128 ---------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CPROJF128_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJF128_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/complex_types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+cfloat128 cprojf128(cfloat128 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJF128_H
diff --git a/libc/src/complex/cprojf16.h b/libc/src/complex/cprojf16.h
new file mode 100644
index 000000000000..f12a46df9e17
--- /dev/null
+++ b/libc/src/complex/cprojf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for cprojf16 ----------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CPROJF16_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/complex_types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+cfloat16 cprojf16(cfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJF16_H
diff --git a/libc/src/complex/cprojl.h b/libc/src/complex/cprojl.h
new file mode 100644
index 000000000000..ecc8dce8f853
--- /dev/null
+++ b/libc/src/complex/cprojl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for cprojl ------------------------*- C++ -*-===//
+//
+// 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_COMPLEX_CPROJL_H
+#define LLVM_LIBC_SRC_COMPLEX_CPROJL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+_Complex long double cprojl(_Complex long double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_COMPLEX_CPROJL_H
diff --git a/libc/src/complex/crealf128.h b/libc/src/complex/crealf128.h
index 4922ae78cb23..b90c3e7c8548 100644
--- a/libc/src/complex/crealf128.h
+++ b/libc/src/complex/crealf128.h
@@ -6,15 +6,12 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/macros/properties/complex_types.h"
-#include "src/__support/macros/properties/types.h"
-
-#if defined(LIBC_TYPES_HAS_CFLOAT128)
-
#ifndef LLVM_LIBC_SRC_COMPLEX_CREALF128_H
#define LLVM_LIBC_SRC_COMPLEX_CREALF128_H
#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/complex_types.h"
+#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
@@ -23,5 +20,3 @@ float128 crealf128(cfloat128 x);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_COMPLEX_CREALF128_H
-
-#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/crealf16.h b/libc/src/complex/crealf16.h
index e6098a218d09..09d66649fa27 100644
--- a/libc/src/complex/crealf16.h
+++ b/libc/src/complex/crealf16.h
@@ -6,15 +6,12 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/macros/properties/complex_types.h"
-#include "src/__support/macros/properties/types.h"
-
-#if defined(LIBC_TYPES_HAS_CFLOAT16)
-
#ifndef LLVM_LIBC_SRC_COMPLEX_CREALF16_H
#define LLVM_LIBC_SRC_COMPLEX_CREALF16_H
#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/complex_types.h"
+#include "src/__support/macros/properties/types.h"
namespace LIBC_NAMESPACE_DECL {
@@ -23,5 +20,3 @@ float16 crealf16(cfloat16 x);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_COMPLEX_CREALF16_H
-
-#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/generic/CMakeLists.txt b/libc/src/complex/generic/CMakeLists.txt
index a3da781c6023..3dae6f8a6149 100644
--- a/libc/src/complex/generic/CMakeLists.txt
+++ b/libc/src/complex/generic/CMakeLists.txt
@@ -1,4 +1,132 @@
add_entrypoint_object(
+ cproj
+ SRCS
+ cproj.cpp
+ HDRS
+ ../cproj.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+)
+
+add_entrypoint_object(
+ cprojf
+ SRCS
+ cprojf.cpp
+ HDRS
+ ../cprojf.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+)
+
+add_entrypoint_object(
+ cprojl
+ SRCS
+ cprojl.cpp
+ HDRS
+ ../cprojl.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+)
+
+add_entrypoint_object(
+ cprojf16
+ SRCS
+ cprojf16.cpp
+ HDRS
+ ../cprojf16.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+ libc.src.__support.macros.properties.types
+ libc.src.__support.macros.properties.complex_types
+)
+
+add_entrypoint_object(
+ cprojf128
+ SRCS
+ cprojf128.cpp
+ HDRS
+ ../cprojf128.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+ libc.src.__support.macros.properties.types
+ libc.src.__support.macros.properties.complex_types
+)
+
+add_entrypoint_object(
+ conj
+ SRCS
+ conj.cpp
+ HDRS
+ ../conj.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+)
+
+add_entrypoint_object(
+ conjf
+ SRCS
+ conjf.cpp
+ HDRS
+ ../conjf.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+)
+
+add_entrypoint_object(
+ conjl
+ SRCS
+ conjl.cpp
+ HDRS
+ ../conjl.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+)
+
+add_entrypoint_object(
+ conjf16
+ SRCS
+ conjf16.cpp
+ HDRS
+ ../conjf16.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+ libc.src.__support.macros.properties.types
+ libc.src.__support.macros.properties.complex_types
+)
+
+add_entrypoint_object(
+ conjf128
+ SRCS
+ conjf128.cpp
+ HDRS
+ ../conjf128.h
+ COMPILE_OPTIONS
+ ${libc_opt_high_flag}
+ DEPENDS
+ libc.src.__support.complex_type
+ libc.src.__support.macros.properties.types
+ libc.src.__support.macros.properties.complex_types
+)
+
+add_entrypoint_object(
creal
SRCS
creal.cpp
diff --git a/libc/src/complex/generic/cimagf128.cpp b/libc/src/complex/generic/cimagf128.cpp
index c21bd7f4602c..78dbb8eddd3e 100644
--- a/libc/src/complex/generic/cimagf128.cpp
+++ b/libc/src/complex/generic/cimagf128.cpp
@@ -7,8 +7,6 @@
//===----------------------------------------------------------------------===//
#include "src/complex/cimagf128.h"
-#if defined(LIBC_TYPES_HAS_CFLOAT128)
-
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
#include "src/__support/complex_type.h"
@@ -21,5 +19,3 @@ LLVM_LIBC_FUNCTION(float128, cimagf128, (cfloat128 x)) {
}
} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/generic/cimagf16.cpp b/libc/src/complex/generic/cimagf16.cpp
index 361687984067..25d9b3ddf3b6 100644
--- a/libc/src/complex/generic/cimagf16.cpp
+++ b/libc/src/complex/generic/cimagf16.cpp
@@ -7,8 +7,6 @@
//===----------------------------------------------------------------------===//
#include "src/complex/cimagf16.h"
-#if defined(LIBC_TYPES_HAS_CFLOAT16)
-
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
#include "src/__support/complex_type.h"
@@ -21,5 +19,3 @@ LLVM_LIBC_FUNCTION(float16, cimagf16, (cfloat16 x)) {
}
} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/complex/generic/conj.cpp b/libc/src/complex/generic/conj.cpp
new file mode 100644
index 000000000000..cbcd480d6efa
--- /dev/null
+++ b/libc/src/complex/generic/conj.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of conj function -----------------------------------===//
+//
+// 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/complex/conj.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex double, conj, (_Complex double x)) {
+ return conjugate<_Complex double>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjf.cpp b/libc/src/complex/generic/conjf.cpp
new file mode 100644
index 000000000000..a1af3d78ebc6
--- /dev/null
+++ b/libc/src/complex/generic/conjf.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of conjf function ----------------------------------===//
+//
+// 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/complex/conjf.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex float, conjf, (_Complex float x)) {
+ return conjugate<_Complex float>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjf128.cpp b/libc/src/complex/generic/conjf128.cpp
new file mode 100644
index 000000000000..a63809a66e25
--- /dev/null
+++ b/libc/src/complex/generic/conjf128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of conjf128 function -------------------------------===//
+//
+// 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/complex/conjf128.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(cfloat128, conjf128, (cfloat128 x)) {
+ return conjugate<cfloat128>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjf16.cpp b/libc/src/complex/generic/conjf16.cpp
new file mode 100644
index 000000000000..cd1ab67ed1cd
--- /dev/null
+++ b/libc/src/complex/generic/conjf16.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of conjf16 function --------------------------------===//
+//
+// 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/complex/conjf16.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(cfloat16, conjf16, (cfloat16 x)) {
+ return conjugate<cfloat16>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/conjl.cpp b/libc/src/complex/generic/conjl.cpp
new file mode 100644
index 000000000000..8298ede6fa38
--- /dev/null
+++ b/libc/src/complex/generic/conjl.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of conjl function ----------------------------------===//
+//
+// 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/complex/conjl.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex long double, conjl, (_Complex long double x)) {
+ return conjugate<_Complex long double>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cproj.cpp b/libc/src/complex/generic/cproj.cpp
new file mode 100644
index 000000000000..d5e8c3ff3d9e
--- /dev/null
+++ b/libc/src/complex/generic/cproj.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of cproj function ----------------------------------===//
+//
+// 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/complex/cproj.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex double, cproj, (_Complex double x)) {
+ return project<_Complex double>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojf.cpp b/libc/src/complex/generic/cprojf.cpp
new file mode 100644
index 000000000000..d0235f6bfef7
--- /dev/null
+++ b/libc/src/complex/generic/cprojf.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of cprojf function ---------------------------------===//
+//
+// 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/complex/cprojf.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex float, cprojf, (_Complex float x)) {
+ return project<_Complex float>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojf128.cpp b/libc/src/complex/generic/cprojf128.cpp
new file mode 100644
index 000000000000..eb2cd08dfc11
--- /dev/null
+++ b/libc/src/complex/generic/cprojf128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of cprojf128 function ------------------------------===//
+//
+// 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/complex/cprojf128.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(cfloat128, cprojf128, (cfloat128 x)) {
+ return project<cfloat128>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojf16.cpp b/libc/src/complex/generic/cprojf16.cpp
new file mode 100644
index 000000000000..8d2d64a439e0
--- /dev/null
+++ b/libc/src/complex/generic/cprojf16.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of cprojf16 function -------------------------------===//
+//
+// 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/complex/cprojf16.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(cfloat16, cprojf16, (cfloat16 x)) {
+ return project<cfloat16>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/cprojl.cpp b/libc/src/complex/generic/cprojl.cpp
new file mode 100644
index 000000000000..34deeb63b16d
--- /dev/null
+++ b/libc/src/complex/generic/cprojl.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of cprojl function ---------------------------------===//
+//
+// 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/complex/cprojl.h"
+#include "src/__support/common.h"
+#include "src/__support/complex_type.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(_Complex long double, cprojl, (_Complex long double x)) {
+ return project<_Complex long double>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/complex/generic/crealf128.cpp b/libc/src/complex/generic/crealf128.cpp
index e72a77821601..e7554989e14a 100644
--- a/libc/src/complex/generic/crealf128.cpp
+++ b/libc/src/complex/generic/crealf128.cpp
@@ -7,8 +7,6 @@
//===----------------------------------------------------------------------===//
#include "src/complex/crealf128.h"
-#if defined(LIBC_TYPES_HAS_CFLOAT128)
-
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
#include "src/__support/complex_type.h"
@@ -21,5 +19,3 @@ LLVM_LIBC_FUNCTION(float128, crealf128, (cfloat128 x)) {
}
} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LIBC_TYPES_HAS_CFLOAT128
diff --git a/libc/src/complex/generic/crealf16.cpp b/libc/src/complex/generic/crealf16.cpp
index 35142071f053..c9e8626bfda9 100644
--- a/libc/src/complex/generic/crealf16.cpp
+++ b/libc/src/complex/generic/crealf16.cpp
@@ -7,8 +7,6 @@
//===----------------------------------------------------------------------===//
#include "src/complex/crealf16.h"
-#if defined(LIBC_TYPES_HAS_CFLOAT16)
-
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
#include "src/__support/complex_type.h"
@@ -21,5 +19,3 @@ LLVM_LIBC_FUNCTION(float16, crealf16, (cfloat16 x)) {
}
} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LIBC_TYPES_HAS_CFLOAT16
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 390a59d07a28..e4e2c49642f2 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -89,6 +89,7 @@ add_math_entrypoint_object(copysignf128)
add_math_entrypoint_object(cos)
add_math_entrypoint_object(cosf)
+add_math_entrypoint_object(cosf16)
add_math_entrypoint_object(cosh)
add_math_entrypoint_object(coshf)
diff --git a/libc/src/math/cosf16.h b/libc/src/math/cosf16.h
new file mode 100644
index 000000000000..cc179a6e16c6
--- /dev/null
+++ b/libc/src/math/cosf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for cosf16 ------------------------*- C++ -*-===//
+//
+// 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_MATH_COSF16_H
+#define LLVM_LIBC_SRC_MATH_COSF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 cosf16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_COSF16_H
diff --git a/libc/src/math/docs/add_math_function.md b/libc/src/math/docs/add_math_function.md
index f02d502399e2..daaf1a3ec563 100644
--- a/libc/src/math/docs/add_math_function.md
+++ b/libc/src/math/docs/add_math_function.md
@@ -18,7 +18,7 @@ together with its specifications:
```
- Add function specs to the file:
```
- libc/hdrgen/yaml/math.yaml
+ libc/include/math.yaml
```
## Implementation
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index aeb758d4a092..b3d461291519 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -419,6 +419,25 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ cosf16
+ SRCS
+ cosf16.cpp
+ HDRS
+ ../cosf16.h
+ DEPENDS
+ .sincosf16_utils
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.except_value_utils
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.macros.optimization
+ libc.src.__support.macros.properties.types
+)
+
+add_entrypoint_object(
cospif
SRCS
cospif.cpp
diff --git a/libc/src/math/generic/cosf16.cpp b/libc/src/math/generic/cosf16.cpp
new file mode 100644
index 000000000000..534e07854474
--- /dev/null
+++ b/libc/src/math/generic/cosf16.cpp
@@ -0,0 +1,84 @@
+//===-- Half-precision cos(x) function ------------------------------------===//
+//
+// 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/math/cosf16.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "sincosf16_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+constexpr size_t N_EXCEPTS = 4;
+
+constexpr fputil::ExceptValues<float16, N_EXCEPTS> COSF16_EXCEPTS{{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ {0x2b7c, 0x3bfc, 1, 0, 1},
+ {0x4ac1, 0x38b5, 1, 0, 0},
+ {0x5c49, 0xb8c6, 0, 1, 0},
+ {0x7acc, 0xa474, 0, 1, 0},
+}};
+
+LLVM_LIBC_FUNCTION(float16, cosf16, (float16 x)) {
+ using FPBits = fputil::FPBits<float16>;
+ FPBits xbits(x);
+
+ uint16_t x_u = xbits.uintval();
+ uint16_t x_abs = x_u & 0x7fff;
+ float xf = x;
+
+ // Range reduction:
+ // For |x| > pi/32, we perform range reduction as follows:
+ // Find k and y such that:
+ // x = (k + y) * pi/32
+ // k is an integer, |y| < 0.5
+ //
+ // This is done by performing:
+ // k = round(x * 32/pi)
+ // y = x * 32/pi - k
+ //
+ // Once k and y are computed, we then deduce the answer by the cosine of sum
+ // formula:
+ // cos(x) = cos((k + y) * pi/32)
+ // = cos(k * pi/32) * cos(y * pi/32) -
+ // sin(k * pi/32) * sin(y * pi/32)
+
+ // Handle exceptional values
+ if (auto r = COSF16_EXCEPTS.lookup(x_abs); LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+
+ // cos(+/-0) = 1
+ if (LIBC_UNLIKELY(x_abs == 0U))
+ return fputil::cast<float16>(1.0f);
+
+ // cos(+/-inf) = NaN, and cos(NaN) = NaN
+ if (xbits.is_inf_or_nan()) {
+ if (xbits.is_inf()) {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ }
+
+ return x + FPBits::quiet_nan().get_val();
+ }
+
+ float sin_k, cos_k, sin_y, cosm1_y;
+ sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
+ // Since, cosm1_y = cos_y - 1, therefore:
+ // cos(x) = cos_k * cos_y - sin_k * sin_y
+ // = cos_k * (cos_y - 1 + 1) - sin_k * sin_y
+ // = cos_k * cosm1_y - sin_k * sin_y + cos_k
+ return fputil::cast<float16>(fputil::multiply_add(
+ cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/cospif16.cpp b/libc/src/math/generic/cospif16.cpp
index 384b39ff8e2c..ee74bdb4a369 100644
--- a/libc/src/math/generic/cospif16.cpp
+++ b/libc/src/math/generic/cospif16.cpp
@@ -73,7 +73,7 @@ LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) {
return fputil::cast<float16>(0.0f);
// Since, cosm1_y = cos_y - 1, therefore:
- // cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
+ // cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
return fputil::cast<float16>(fputil::multiply_add(
cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
}
diff --git a/libc/src/math/generic/sinf16.cpp b/libc/src/math/generic/sinf16.cpp
index 86546348ba73..7c15951cccec 100644
--- a/libc/src/math/generic/sinf16.cpp
+++ b/libc/src/math/generic/sinf16.cpp
@@ -54,13 +54,10 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) {
// sin(y * pi/32) * cos(k * pi/32)
// Handle exceptional values
- if (LIBC_UNLIKELY(x_abs == 0x585c || x_abs == 0x5cb0 || x_abs == 0x51f5 ||
- x_abs == 0x2b45)) {
- bool x_sign = x_u >> 15;
- if (auto r = SINF16_EXCEPTS.lookup_odd(x_abs, x_sign);
- LIBC_UNLIKELY(r.has_value()))
- return r.value();
- }
+ bool x_sign = x_u >> 15;
+ if (auto r = SINF16_EXCEPTS.lookup_odd(x_abs, x_sign);
+ LIBC_UNLIKELY(r.has_value()))
+ return r.value();
int rounding = fputil::quick_get_round();
@@ -99,8 +96,8 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) {
if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
return FPBits::zero(xbits.sign()).get_val();
- // Since, cosm1_y = cos_y - 1, therfore:
- // sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k)
+ // Since, cosm1_y = cos_y - 1, therefore:
+ // sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k)
return fputil::cast<float16>(fputil::multiply_add(
sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
}
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 14d06534a604..40ba9ead9a7a 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -323,7 +323,7 @@ add_entrypoint_object(
.rand_util
)
-if(NOT LIBC_TARGET_OS_IS_GPU)
+if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
if(LLVM_LIBC_INCLUDE_SCUDO)
set(SCUDO_DEPS "")
@@ -349,7 +349,7 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
-
+
if (COMPILER_RT_BUILD_GWP_ASAN)
list(APPEND SCUDO_DEPS
RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
@@ -389,32 +389,8 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
${SCUDO_DEPS}
)
else()
- # Only use freelist malloc for baremetal targets.
- add_entrypoint_object(
- freelist_malloc
- SRCS
- freelist_malloc.cpp
- HDRS
- malloc.h
- DEPENDS
- libc.src.__support.freelist_heap
- )
- get_target_property(freelist_malloc_is_skipped libc.src.stdlib.freelist_malloc "SKIPPED")
- if(LIBC_TARGET_OS_IS_BAREMETAL AND NOT freelist_malloc_is_skipped)
- add_entrypoint_object(
- malloc
- ALIAS
- DEPENDS
- .freelist_malloc
- )
- else()
- add_entrypoint_external(
- malloc
- )
- endif()
-
add_entrypoint_external(
- free
+ malloc
)
add_entrypoint_external(
calloc
@@ -425,6 +401,12 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
add_entrypoint_external(
aligned_alloc
)
+ add_entrypoint_external(
+ free
+ )
+ add_entrypoint_external(
+ mallopt
+ )
endif()
endif()
@@ -513,7 +495,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()
-if(LIBC_TARGET_OS_IS_GPU)
+if(LIBC_TARGET_OS_IS_BAREMETAL OR LIBC_TARGET_OS_IS_GPU)
add_entrypoint_object(
malloc
ALIAS
diff --git a/libc/src/stdlib/baremetal/CMakeLists.txt b/libc/src/stdlib/baremetal/CMakeLists.txt
index 551a83a36b20..67ab1979e4d1 100644
--- a/libc/src/stdlib/baremetal/CMakeLists.txt
+++ b/libc/src/stdlib/baremetal/CMakeLists.txt
@@ -5,3 +5,53 @@ add_entrypoint_object(
HDRS
../abort.h
)
+
+add_entrypoint_object(
+ malloc
+ SRCS
+ malloc.cpp
+ HDRS
+ ../malloc.h
+ DEPENDS
+ libc.src.__support.freelist_heap
+)
+
+add_entrypoint_object(
+ free
+ SRCS
+ free.cpp
+ HDRS
+ ../free.h
+ DEPENDS
+ libc.src.__support.freelist_heap
+)
+
+add_entrypoint_object(
+ calloc
+ SRCS
+ calloc.cpp
+ HDRS
+ ../calloc.h
+ DEPENDS
+ libc.src.__support.freelist_heap
+)
+
+add_entrypoint_object(
+ realloc
+ SRCS
+ realloc.cpp
+ HDRS
+ ../realloc.h
+ DEPENDS
+ libc.src.__support.freelist_heap
+)
+
+add_entrypoint_object(
+ aligned_alloc
+ SRCS
+ aligned_alloc.cpp
+ HDRS
+ ../aligned_alloc.h
+ DEPENDS
+ libc.src.__support.freelist_heap
+)
diff --git a/libc/src/stdlib/baremetal/aligned_alloc.cpp b/libc/src/stdlib/baremetal/aligned_alloc.cpp
new file mode 100644
index 000000000000..e9548719c3a6
--- /dev/null
+++ b/libc/src/stdlib/baremetal/aligned_alloc.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation for freelist_malloc --------------------------------===//
+//
+// 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/stdlib/aligned_alloc.h"
+#include "src/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+
+#include <stddef.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
+ return freelist_heap->aligned_allocate(alignment, size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/baremetal/calloc.cpp b/libc/src/stdlib/baremetal/calloc.cpp
new file mode 100644
index 000000000000..2b3b83cebc8a
--- /dev/null
+++ b/libc/src/stdlib/baremetal/calloc.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation for freelist_malloc --------------------------------===//
+//
+// 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/stdlib/calloc.h"
+#include "src/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+
+#include <stddef.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
+ return freelist_heap->calloc(num, size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/baremetal/free.cpp b/libc/src/stdlib/baremetal/free.cpp
new file mode 100644
index 000000000000..1e25fe5f2dcf
--- /dev/null
+++ b/libc/src/stdlib/baremetal/free.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation for freelist_malloc --------------------------------===//
+//
+// 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/stdlib/free.h"
+#include "src/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+
+#include <stddef.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { return freelist_heap->free(ptr); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/baremetal/malloc.cpp b/libc/src/stdlib/baremetal/malloc.cpp
new file mode 100644
index 000000000000..a299282667fc
--- /dev/null
+++ b/libc/src/stdlib/baremetal/malloc.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation for freelist_malloc --------------------------------===//
+//
+// 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/stdlib/malloc.h"
+#include "src/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+
+#include <stddef.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
+ return freelist_heap->allocate(size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/baremetal/realloc.cpp b/libc/src/stdlib/baremetal/realloc.cpp
new file mode 100644
index 000000000000..fb25c68ec429
--- /dev/null
+++ b/libc/src/stdlib/baremetal/realloc.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation for freelist_malloc --------------------------------===//
+//
+// 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/stdlib/realloc.h"
+#include "src/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+
+#include <stddef.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
+ return freelist_heap->realloc(ptr, size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/freelist_malloc.cpp b/libc/src/stdlib/freelist_malloc.cpp
deleted file mode 100644
index fe56fad76937..000000000000
--- a/libc/src/stdlib/freelist_malloc.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-//===-- Implementation for freelist_malloc --------------------------------===//
-//
-// 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/freelist_heap.h"
-#include "src/__support/macros/config.h"
-#include "src/stdlib/aligned_alloc.h"
-#include "src/stdlib/calloc.h"
-#include "src/stdlib/free.h"
-#include "src/stdlib/malloc.h"
-#include "src/stdlib/realloc.h"
-
-#include <stddef.h>
-
-namespace LIBC_NAMESPACE_DECL {
-
-static LIBC_CONSTINIT FreeListHeap freelist_heap_symbols;
-FreeListHeap *freelist_heap = &freelist_heap_symbols;
-
-LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
- return freelist_heap->allocate(size);
-}
-
-LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { return freelist_heap->free(ptr); }
-
-LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
- return freelist_heap->calloc(num, size);
-}
-
-LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
- return freelist_heap->realloc(ptr, size);
-}
-
-LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
- return freelist_heap->aligned_allocate(alignment, size);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 8fe1226dd6a7..e3faa543e630 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -35,24 +35,6 @@ add_header_library(
)
add_entrypoint_object(
- bcopy
- SRCS
- bcopy.cpp
- HDRS
- bcopy.h
-)
-
-add_entrypoint_object(
- index
- SRCS
- index.cpp
- HDRS
- index.h
- DEPENDS
- .string_utils
-)
-
-add_entrypoint_object(
memccpy
SRCS
memccpy.cpp
@@ -99,16 +81,6 @@ add_entrypoint_object(
)
add_entrypoint_object(
- rindex
- SRCS
- rindex.cpp
- HDRS
- rindex.h
- DEPENDS
- .string_utils
-)
-
-add_entrypoint_object(
stpcpy
SRCS
stpcpy.cpp
@@ -172,17 +144,6 @@ add_entrypoint_object(
)
add_entrypoint_object(
- strcasecmp
- SRCS
- strcasecmp.cpp
- HDRS
- strcasecmp.h
- DEPENDS
- .memory_utils.inline_strcmp
- libc.src.__support.ctype_utils
-)
-
-add_entrypoint_object(
strcasestr
SRCS
strcasestr.cpp
@@ -320,17 +281,6 @@ add_entrypoint_object(
)
add_entrypoint_object(
- strncasecmp
- SRCS
- strncasecmp.cpp
- HDRS
- strncasecmp.h
- DEPENDS
- .memory_utils.inline_strcmp
- libc.src.__support.ctype_utils
-)
-
-add_entrypoint_object(
strncpy
SRCS
strncpy.cpp
@@ -475,102 +425,6 @@ add_entrypoint_object(
.memory_utils.inline_memset
)
-# Helper to define a function with multiple implementations
-# - Computes flags to satisfy required/rejected features and arch,
-# - Declares an entry point,
-# - Attach the REQUIRE_CPU_FEATURES property to the target,
-# - Add the fully qualified target to `${name}_implementations` global property for tests.
-function(add_implementation name impl_name)
- cmake_parse_arguments(
- "ADD_IMPL"
- "" # Optional arguments
- "" # Single value arguments
- "REQUIRE;SRCS;HDRS;DEPENDS;COMPILE_OPTIONS;MLLVM_COMPILE_OPTIONS" # Multi value arguments
- ${ARGN})
-
- if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
- # Note that '-mllvm' needs to be prefixed with 'SHELL:' to prevent CMake flag deduplication.
- foreach(opt IN LISTS ADD_IMPL_MLLVM_COMPILE_OPTIONS)
- list(APPEND ADD_IMPL_COMPILE_OPTIONS "SHELL:-mllvm ${opt}")
- endforeach()
- endif()
-
- if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
- # Prevent warning when passing x86 SIMD types as template arguments.
- # e.g. "warning: ignoring attributes on template argument ‘__m128i’ [-Wignored-attributes]"
- list(APPEND ADD_IMPL_COMPILE_OPTIONS "-Wno-ignored-attributes")
- endif()
-
- add_entrypoint_object(${impl_name}
- NAME ${name}
- SRCS ${ADD_IMPL_SRCS}
- HDRS ${ADD_IMPL_HDRS}
- DEPENDS ${ADD_IMPL_DEPENDS}
- COMPILE_OPTIONS ${libc_opt_high_flag} ${ADD_IMPL_COMPILE_OPTIONS}
- )
- get_fq_target_name(${impl_name} fq_target_name)
- set_target_properties(${fq_target_name} PROPERTIES REQUIRE_CPU_FEATURES "${ADD_IMPL_REQUIRE}")
- set_property(GLOBAL APPEND PROPERTY "${name}_implementations" "${fq_target_name}")
-endfunction()
-
-# ------------------------------------------------------------------------------
-# bcmp
-# ------------------------------------------------------------------------------
-
-function(add_bcmp bcmp_name)
- add_implementation(bcmp ${bcmp_name}
- SRCS ${LIBC_SOURCE_DIR}/src/string/bcmp.cpp
- HDRS ${LIBC_SOURCE_DIR}/src/string/bcmp.h
- DEPENDS
- .memory_utils.memory_utils
- libc.include.string
- ${ARGN}
- )
-endfunction()
-
-if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
- add_bcmp(bcmp_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
- add_bcmp(bcmp_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
- add_bcmp(bcmp_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
- add_bcmp(bcmp_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512BW)
- add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
- add_bcmp(bcmp)
-elseif(LIBC_TARGET_OS_IS_GPU)
- add_bcmp(bcmp)
-else()
- add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
- add_bcmp(bcmp)
-endif()
-
-# ------------------------------------------------------------------------------
-# bzero
-# ------------------------------------------------------------------------------
-
-function(add_bzero bzero_name)
- add_implementation(bzero ${bzero_name}
- SRCS ${LIBC_SOURCE_DIR}/src/string/bzero.cpp
- HDRS ${LIBC_SOURCE_DIR}/src/string/bzero.h
- DEPENDS
- .memory_utils.inline_memset
- libc.include.string
- ${ARGN}
- )
-endfunction()
-
-if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
- add_bzero(bzero_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
- add_bzero(bzero_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
- add_bzero(bzero_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
- add_bzero(bzero_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
- add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
- add_bzero(bzero)
-elseif(LIBC_TARGET_OS_IS_GPU)
- add_bzero(bzero)
-else()
- add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
- add_bzero(bzero)
-endif()
-
# ------------------------------------------------------------------------------
# memcmp
# ------------------------------------------------------------------------------
diff --git a/libc/src/strings/CMakeLists.txt b/libc/src/strings/CMakeLists.txt
new file mode 100644
index 000000000000..5e84c7be1f7d
--- /dev/null
+++ b/libc/src/strings/CMakeLists.txt
@@ -0,0 +1,97 @@
+function(add_bcmp bcmp_name)
+ add_implementation(bcmp ${bcmp_name}
+ SRCS ${LIBC_SOURCE_DIR}/src/strings/bcmp.cpp
+ HDRS ${LIBC_SOURCE_DIR}/src/strings/bcmp.h
+ DEPENDS
+ libc.src.string.memory_utils.memory_utils
+ ${ARGN}
+ )
+endfunction()
+
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
+ add_bcmp(bcmp_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
+ add_bcmp(bcmp_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
+ add_bcmp(bcmp_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
+ add_bcmp(bcmp_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512BW)
+ add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
+ add_bcmp(bcmp)
+elseif(LIBC_TARGET_OS_IS_GPU)
+ add_bcmp(bcmp)
+else()
+ add_bcmp(bcmp_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
+ add_bcmp(bcmp)
+endif()
+
+function(add_bzero bzero_name)
+ add_implementation(bzero ${bzero_name}
+ SRCS ${LIBC_SOURCE_DIR}/src/strings/bzero.cpp
+ HDRS ${LIBC_SOURCE_DIR}/src/strings/bzero.h
+ DEPENDS
+ libc.src.string.memory_utils.inline_memset
+ ${ARGN}
+ )
+endfunction()
+
+if(${LIBC_TARGET_ARCHITECTURE_IS_X86})
+ add_bzero(bzero_x86_64_opt_sse2 COMPILE_OPTIONS -march=k8 REQUIRE SSE2)
+ add_bzero(bzero_x86_64_opt_sse4 COMPILE_OPTIONS -march=nehalem REQUIRE SSE4_2)
+ add_bzero(bzero_x86_64_opt_avx2 COMPILE_OPTIONS -march=haswell REQUIRE AVX2)
+ add_bzero(bzero_x86_64_opt_avx512 COMPILE_OPTIONS -march=skylake-avx512 REQUIRE AVX512F)
+ add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
+ add_bzero(bzero)
+elseif(LIBC_TARGET_OS_IS_GPU)
+ add_bzero(bzero)
+else()
+ add_bzero(bzero_opt_host COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_NATIVE})
+ add_bzero(bzero)
+endif()
+
+add_entrypoint_object(
+ bcopy
+ SRCS
+ bcopy.cpp
+ HDRS
+ bcopy.h
+)
+
+add_entrypoint_object(
+ index
+ SRCS
+ index.cpp
+ HDRS
+ index.h
+ DEPENDS
+ libc.src.string.string_utils
+)
+
+add_entrypoint_object(
+ rindex
+ SRCS
+ rindex.cpp
+ HDRS
+ rindex.h
+ DEPENDS
+ libc.src.string.string_utils
+)
+
+add_entrypoint_object(
+ strcasecmp
+ SRCS
+ strcasecmp.cpp
+ HDRS
+ strcasecmp.h
+ DEPENDS
+ libc.src.__support.ctype_utils
+ libc.src.string.memory_utils.inline_strcmp
+)
+
+add_entrypoint_object(
+ strncasecmp
+ SRCS
+ strncasecmp.cpp
+ HDRS
+ strncasecmp.h
+ DEPENDS
+ libc.src.__support.ctype_utils
+ libc.src.string.memory_utils.inline_strcmp
+)
diff --git a/libc/src/string/bcmp.cpp b/libc/src/strings/bcmp.cpp
index 6e9c9ae3b7a3..083f13d3f95d 100644
--- a/libc/src/string/bcmp.cpp
+++ b/libc/src/strings/bcmp.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/string/bcmp.h"
+#include "src/strings/bcmp.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_bcmp.h"
diff --git a/libc/src/string/bcmp.h b/libc/src/strings/bcmp.h
index a82b529164d9..46b4d7163b52 100644
--- a/libc/src/string/bcmp.h
+++ b/libc/src/strings/bcmp.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_STRING_BCMP_H
-#define LLVM_LIBC_SRC_STRING_BCMP_H
+#ifndef LLVM_LIBC_SRC_STRINGS_BCMP_H
+#define LLVM_LIBC_SRC_STRINGS_BCMP_H
#include "src/__support/macros/config.h"
#include <stddef.h> // size_t
@@ -18,4 +18,4 @@ int bcmp(const void *lhs, const void *rhs, size_t count);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_STRING_BCMP_H
+#endif // LLVM_LIBC_SRC_STRINGS_BCMP_H
diff --git a/libc/src/string/bcopy.cpp b/libc/src/strings/bcopy.cpp
index 89aad71e354c..eb0358c18747 100644
--- a/libc/src/string/bcopy.cpp
+++ b/libc/src/strings/bcopy.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/string/bcopy.h"
+#include "src/strings/bcopy.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_memmove.h"
diff --git a/libc/src/string/bcopy.h b/libc/src/strings/bcopy.h
index 4cf0263a7d63..d8ee77e83c6d 100644
--- a/libc/src/string/bcopy.h
+++ b/libc/src/strings/bcopy.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_STRING_BCOPY_H
-#define LLVM_LIBC_SRC_STRING_BCOPY_H
+#ifndef LLVM_LIBC_SRC_STRINGS_BCOPY_H
+#define LLVM_LIBC_SRC_STRINGS_BCOPY_H
#include "src/__support/macros/config.h"
#include <stddef.h> // size_t
@@ -18,4 +18,4 @@ void bcopy(const void *src, void *dest, size_t count);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_STRING_BCOPY_H
+#endif // LLVM_LIBC_SRC_STRINGS_BCOPY_H
diff --git a/libc/src/string/bzero.cpp b/libc/src/strings/bzero.cpp
index 7bcbee3547b9..9c2e9700442a 100644
--- a/libc/src/string/bzero.cpp
+++ b/libc/src/strings/bzero.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/string/bzero.h"
+#include "src/strings/bzero.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_bzero.h"
diff --git a/libc/src/string/bzero.h b/libc/src/strings/bzero.h
index d9722197e8e8..3e270fe41f6c 100644
--- a/libc/src/string/bzero.h
+++ b/libc/src/strings/bzero.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_STRING_BZERO_H
-#define LLVM_LIBC_SRC_STRING_BZERO_H
+#ifndef LLVM_LIBC_SRC_STRINGS_BZERO_H
+#define LLVM_LIBC_SRC_STRINGS_BZERO_H
#include "src/__support/macros/config.h"
#include <stddef.h> // size_t
@@ -18,4 +18,4 @@ void bzero(void *ptr, size_t count);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_STRING_BZERO_H
+#endif // LLVM_LIBC_SRC_STRINGS_BZERO_H
diff --git a/libc/src/string/index.cpp b/libc/src/strings/index.cpp
index 46cf54825f6c..33b2be9f33e0 100644
--- a/libc/src/string/index.cpp
+++ b/libc/src/strings/index.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/string/index.h"
+#include "src/strings/index.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
diff --git a/libc/src/string/index.h b/libc/src/strings/index.h
index 9843bcdf9a57..d382cdd249aa 100644
--- a/libc/src/string/index.h
+++ b/libc/src/strings/index.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_STRING_INDEX_H
-#define LLVM_LIBC_SRC_STRING_INDEX_H
+#ifndef LLVM_LIBC_SRC_STRINGS_INDEX_H
+#define LLVM_LIBC_SRC_STRINGS_INDEX_H
#include "src/__support/macros/config.h"
@@ -17,4 +17,4 @@ char *index(const char *src, int c);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_STRING_INDEX_H
+#endif // LLVM_LIBC_SRC_STRINGS_INDEX_H
diff --git a/libc/src/string/rindex.cpp b/libc/src/strings/rindex.cpp
index 25879ddb07f6..1242e0faf85f 100644
--- a/libc/src/string/rindex.cpp
+++ b/libc/src/strings/rindex.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/string/rindex.h"
+#include "src/strings/rindex.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
diff --git a/libc/src/string/rindex.h b/libc/src/strings/rindex.h
index cfc35daa1f4d..f8aa7b9be28f 100644
--- a/libc/src/string/rindex.h
+++ b/libc/src/strings/rindex.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_STRING_RINDEX_H
-#define LLVM_LIBC_SRC_STRING_RINDEX_H
+#ifndef LLVM_LIBC_SRC_STRINGS_RINDEX_H
+#define LLVM_LIBC_SRC_STRINGS_RINDEX_H
#include "src/__support/macros/config.h"
@@ -17,4 +17,4 @@ char *rindex(const char *src, int c);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_STRING_RINDEX_H
+#endif // LLVM_LIBC_SRC_STRINGS_RINDEX_H
diff --git a/libc/src/string/strcasecmp.cpp b/libc/src/strings/strcasecmp.cpp
index 1274c047fc28..4bbe2909df1e 100644
--- a/libc/src/string/strcasecmp.cpp
+++ b/libc/src/strings/strcasecmp.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/string/strcasecmp.h"
+#include "src/strings/strcasecmp.h"
#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
diff --git a/libc/src/string/strcasecmp.h b/libc/src/strings/strcasecmp.h
index 2916b8d41707..b02fb3161587 100644
--- a/libc/src/string/strcasecmp.h
+++ b/libc/src/strings/strcasecmp.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_STRING_STRCASECMP_H
-#define LLVM_LIBC_SRC_STRING_STRCASECMP_H
+#ifndef LLVM_LIBC_SRC_STRINGS_STRCASECMP_H
+#define LLVM_LIBC_SRC_STRINGS_STRCASECMP_H
#include "src/__support/macros/config.h"
@@ -17,4 +17,4 @@ int strcasecmp(const char *left, const char *right);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_STRING_STRCASECMP_H
+#endif // LLVM_LIBC_SRC_STRINGS_STRCASECMP_H
diff --git a/libc/src/string/strncasecmp.cpp b/libc/src/strings/strncasecmp.cpp
index 45f82c98069b..9c2f0ab13126 100644
--- a/libc/src/string/strncasecmp.cpp
+++ b/libc/src/strings/strncasecmp.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "src/string/strncasecmp.h"
+#include "src/strings/strncasecmp.h"
#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
diff --git a/libc/src/string/strncasecmp.h b/libc/src/strings/strncasecmp.h
index 15f74994ca56..59df51705891 100644
--- a/libc/src/string/strncasecmp.h
+++ b/libc/src/strings/strncasecmp.h
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_SRC_STRING_STRNCASECMP_H
-#define LLVM_LIBC_SRC_STRING_STRNCASECMP_H
+#ifndef LLVM_LIBC_SRC_STRINGS_STRNCASECMP_H
+#define LLVM_LIBC_SRC_STRINGS_STRNCASECMP_H
#include "src/__support/macros/config.h"
#include <stddef.h>
@@ -18,4 +18,4 @@ int strncasecmp(const char *left, const char *right, size_t n);
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_STRING_STRNCASECMP_H
+#endif // LLVM_LIBC_SRC_STRINGS_STRNCASECMP_H
diff --git a/libc/src/sys/mman/CMakeLists.txt b/libc/src/sys/mman/CMakeLists.txt
index 281efc0ffcdf..4d4c2ad37605 100644
--- a/libc/src/sys/mman/CMakeLists.txt
+++ b/libc/src/sys/mman/CMakeLists.txt
@@ -113,9 +113,3 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.mremap
)
-
-add_entrypoint_object(
- process_mrelease
- ALIAS
- DEPENDS
- .${LIBC_TARGET_OS}.process_mrelease)
diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt
index aa2ca4b16018..89a0ad1527a0 100644
--- a/libc/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/src/sys/mman/linux/CMakeLists.txt
@@ -36,6 +36,7 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
+
add_entrypoint_object(
munmap
SRCS
@@ -213,14 +214,3 @@ add_entrypoint_object(
libc.src.unistd.unlink
.shm_common
)
-
-add_entrypoint_object(
- process_mrelease
- SRCS
- process_mrelease.cpp
- HDRS
- ../process_mrelease.h
- DEPENDS
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
- libc.src.errno.errno)
diff --git a/libc/src/sys/mman/linux/process_mrelease.cpp b/libc/src/sys/mman/linux/process_mrelease.cpp
deleted file mode 100644
index 7660f1e23ece..000000000000
--- a/libc/src/sys/mman/linux/process_mrelease.cpp
+++ /dev/null
@@ -1,41 +0,0 @@
-//===---------- Linux implementation of the mrelease function -----------===//
-//
-// 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/sys/mman/process_mrelease.h"
-
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
-#include "src/__support/common.h"
-
-#include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h"
-#include <linux/param.h> // For EXEC_PAGESIZE.
-#include <sys/syscall.h> // For syscall numbers.
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(int, process_mrelease, (int pidfd, unsigned int flags)) {
-#ifdef SYS_process_mrelease
- long ret =
- LIBC_NAMESPACE::syscall_impl<int>(SYS_process_mrelease, pidfd, flags);
-
- if (ret < 0) {
- libc_errno = static_cast<int>(-ret);
- return -1;
- }
-
- return 0;
-#else
- // The system call is not available.
- (void)pidfd;
- (void)flags;
- libc_errno = ENOSYS;
- return -1;
-#endif
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/mman/process_mrelease.h b/libc/src/sys/mman/process_mrelease.h
deleted file mode 100644
index 6c800f2d0eab..000000000000
--- a/libc/src/sys/mman/process_mrelease.h
+++ /dev/null
@@ -1,21 +0,0 @@
-//===-- Implementation header for process_mrelease function -*- C++ -*-===//
-//
-// 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_SYS_MMAN_PROCESS_MRELEASE_H
-#define LLVM_LIBC_SRC_SYS_MMAN_PROCESS_MRELEASE_H
-
-#include "src/__support/macros/config.h"
-#include <sys/mman.h> // For size_t and off_t
-
-namespace LIBC_NAMESPACE_DECL {
-
-int process_mrelease(int pidfd, unsigned int flags);
-
-} // namespace LIBC_NAMESPACE_DECL
-
-#endif // LLVM_LIBC_SRC_SYS_MMAN_PROCESS_MRELEASE_H
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index f18e74a15e6f..ae835dcc7427 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -106,9 +106,15 @@ add_entrypoint_object(
add_entrypoint_object(
time
- ALIAS
+ SRCS
+ time.cpp
+ HDRS
+ time_func.h
DEPENDS
- .${LIBC_TARGET_OS}.time
+ libc.hdr.time_macros
+ libc.hdr.types.time_t
+ libc.src.__support.time.clock_gettime
+ libc.src.errno.errno
)
add_entrypoint_object(
@@ -145,3 +151,10 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.gettimeofday
)
+
+add_entrypoint_object(
+ clock_getres
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.clock_getres
+)
diff --git a/libc/src/time/clock_getres.h b/libc/src/time/clock_getres.h
new file mode 100644
index 000000000000..c1c581c53599
--- /dev/null
+++ b/libc/src/time/clock_getres.h
@@ -0,0 +1,21 @@
+//===-- Implementation header of clock_getres -------------------*- C++ -*-===//
+//
+// 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_TIME_CLOCK_GETRES_H
+#define LLVM_LIBC_SRC_TIME_CLOCK_GETRES_H
+
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int clock_getres(clockid_t clockid, timespec *tp);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_CLOCK_GETRES_H
diff --git a/libc/src/time/gpu/CMakeLists.txt b/libc/src/time/gpu/CMakeLists.txt
index 8da5d3a22f5a..31a60595d68f 100644
--- a/libc/src/time/gpu/CMakeLists.txt
+++ b/libc/src/time/gpu/CMakeLists.txt
@@ -1,14 +1,3 @@
-add_object_library(
- time_utils
- SRCS
- time_utils.cpp
- HDRS
- time_utils.h
- DEPENDS
- libc.hdr.types.clock_t
- libc.hdr.time_macros
-)
-
add_entrypoint_object(
clock
SRCS
@@ -18,7 +7,8 @@ add_entrypoint_object(
DEPENDS
libc.include.time
libc.src.__support.GPU.utils
- .time_utils
+ libc.src.__support.time.clock_gettime
+ libc.src.__support.time.gpu.time_utils
)
add_entrypoint_object(
@@ -30,29 +20,31 @@ add_entrypoint_object(
DEPENDS
libc.include.time
libc.src.__support.GPU.utils
- .time_utils
+ libc.src.__support.time.gpu.time_utils
)
add_entrypoint_object(
- clock_gettime
+ timespec_get
SRCS
- clock_gettime.cpp
+ timespec_get.cpp
HDRS
- ../clock_gettime.h
+ ../timespec_get.h
DEPENDS
- libc.hdr.types.clockid_t
+ libc.hdr.time_macros
libc.hdr.types.struct_timespec
- .time_utils
+ libc.src.__support.time.gpu.time_utils
)
add_entrypoint_object(
- timespec_get
+ clock_gettime
SRCS
- timespec_get.cpp
+ clock_gettime.cpp
HDRS
- ../timespec_get.h
+ ../clock_gettime.h
DEPENDS
libc.hdr.time_macros
+ libc.hdr.types.clockid_t
libc.hdr.types.struct_timespec
- .time_utils
+ libc.src.__support.time.gpu.time_utils
+ libc.src.__support.time.clock_gettime
)
diff --git a/libc/src/time/gpu/clock.cpp b/libc/src/time/gpu/clock.cpp
index 4cdb1d505aed..add5b2725ef8 100644
--- a/libc/src/time/gpu/clock.cpp
+++ b/libc/src/time/gpu/clock.cpp
@@ -8,7 +8,7 @@
#include "src/time/clock.h"
#include "src/__support/macros/config.h"
-#include "src/time/gpu/time_utils.h"
+#include "src/__support/time/gpu/time_utils.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/time/gpu/clock_gettime.cpp b/libc/src/time/gpu/clock_gettime.cpp
index de7899a2a17c..81547ef7f1ca 100644
--- a/libc/src/time/gpu/clock_gettime.cpp
+++ b/libc/src/time/gpu/clock_gettime.cpp
@@ -10,23 +10,16 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "time_utils.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/__support/time/gpu/time_utils.h"
namespace LIBC_NAMESPACE_DECL {
-constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
-
LLVM_LIBC_FUNCTION(int, clock_gettime, (clockid_t clockid, timespec *ts)) {
- if (clockid != CLOCK_MONOTONIC || !ts)
- return -1;
-
- uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
- uint64_t ticks = gpu::fixed_frequency_clock();
-
- ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
- ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;
-
- return 0;
+ ErrorOr<int> result = internal::clock_gettime(clockid, ts);
+ if (result)
+ return result.value();
+ return result.error();
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/gpu/nanosleep.cpp b/libc/src/time/gpu/nanosleep.cpp
index 3f4a609dd40e..a92f660f225c 100644
--- a/libc/src/time/gpu/nanosleep.cpp
+++ b/libc/src/time/gpu/nanosleep.cpp
@@ -9,12 +9,10 @@
#include "src/time/nanosleep.h"
#include "src/__support/macros/config.h"
-#include "time_utils.h"
+#include "src/__support/time/gpu/time_utils.h"
namespace LIBC_NAMESPACE_DECL {
-constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
-
LLVM_LIBC_FUNCTION(int, nanosleep,
(const struct timespec *req, struct timespec *rem)) {
if (!GPU_CLOCKS_PER_SEC || !req)
diff --git a/libc/src/time/gpu/timespec_get.cpp b/libc/src/time/gpu/timespec_get.cpp
index f4ef328a8312..0dd128444aa8 100644
--- a/libc/src/time/gpu/timespec_get.cpp
+++ b/libc/src/time/gpu/timespec_get.cpp
@@ -10,6 +10,7 @@
#include "hdr/time_macros.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
+#include "src/__support/time/gpu/time_utils.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index 31fd7d1e64c8..314623f9f425 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -1,17 +1,4 @@
add_entrypoint_object(
- time
- SRCS
- time.cpp
- HDRS
- ../time_func.h
- DEPENDS
- libc.hdr.time_macros
- libc.hdr.types.time_t
- libc.src.__support.time.linux.clock_gettime
- libc.src.errno.errno
-)
-
-add_entrypoint_object(
timespec_get
SRCS
timespec_get.cpp
@@ -20,7 +7,7 @@ add_entrypoint_object(
DEPENDS
libc.hdr.time_macros
libc.hdr.types.struct_timespec
- libc.src.__support.time.linux.clock_gettime
+ libc.src.__support.time.clock_gettime
libc.src.errno.errno
)
@@ -34,7 +21,7 @@ add_entrypoint_object(
libc.hdr.time_macros
libc.hdr.types.clock_t
libc.src.__support.time.units
- libc.src.__support.time.linux.clock_gettime
+ libc.src.__support.time.clock_gettime
libc.src.__support.CPP.limits
libc.src.errno.errno
)
@@ -62,7 +49,7 @@ add_entrypoint_object(
DEPENDS
libc.hdr.types.clockid_t
libc.hdr.types.struct_timespec
- libc.src.__support.time.linux.clock_gettime
+ libc.src.__support.time.clock_gettime
libc.src.errno.errno
)
@@ -75,7 +62,7 @@ add_entrypoint_object(
DEPENDS
libc.hdr.time_macros
libc.hdr.types.suseconds_t
- libc.src.__support.time.linux.clock_gettime
+ libc.src.__support.time.clock_gettime
libc.src.__support.time.units
libc.src.errno.errno
)
diff --git a/libc/src/time/linux/clock.cpp b/libc/src/time/linux/clock.cpp
index f43e1bcad6a3..ee4fa82b4f89 100644
--- a/libc/src/time/linux/clock.cpp
+++ b/libc/src/time/linux/clock.cpp
@@ -11,7 +11,7 @@
#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/time/linux/clock_gettime.h"
+#include "src/__support/time/clock_gettime.h"
#include "src/__support/time/units.h"
#include "src/errno/libc_errno.h"
diff --git a/libc/src/time/linux/clock_gettime.cpp b/libc/src/time/linux/clock_gettime.cpp
index a2b20a6dbc98..743c644d65d0 100644
--- a/libc/src/time/linux/clock_gettime.cpp
+++ b/libc/src/time/linux/clock_gettime.cpp
@@ -9,7 +9,7 @@
#include "src/time/clock_gettime.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/time/linux/clock_gettime.h"
+#include "src/__support/time/clock_gettime.h"
#include "src/errno/libc_errno.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/time/linux/gettimeofday.cpp b/libc/src/time/linux/gettimeofday.cpp
index 19d9988ae73a..e8ddf482fc98 100644
--- a/libc/src/time/linux/gettimeofday.cpp
+++ b/libc/src/time/linux/gettimeofday.cpp
@@ -11,7 +11,7 @@
#include "hdr/types/suseconds_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/time/linux/clock_gettime.h"
+#include "src/__support/time/clock_gettime.h"
#include "src/__support/time/units.h"
#include "src/errno/libc_errno.h"
diff --git a/libc/src/time/linux/timespec_get.cpp b/libc/src/time/linux/timespec_get.cpp
index ba9f8eb2e442..cf5174523aa4 100644
--- a/libc/src/time/linux/timespec_get.cpp
+++ b/libc/src/time/linux/timespec_get.cpp
@@ -10,7 +10,7 @@
#include "hdr/time_macros.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/time/linux/clock_gettime.h"
+#include "src/__support/time/clock_gettime.h"
#include "src/errno/libc_errno.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/time/linux/time.cpp b/libc/src/time/time.cpp
index 20fb86e8e29d..4a0b614a68ef 100644
--- a/libc/src/time/linux/time.cpp
+++ b/libc/src/time/time.cpp
@@ -9,14 +9,14 @@
#include "hdr/time_macros.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/time/linux/clock_gettime.h"
+#include "src/__support/time/clock_gettime.h"
#include "src/errno/libc_errno.h"
#include "src/time/time_func.h"
namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(time_t, time, (time_t * tp)) {
- // TODO: Use the Linux VDSO to fetch the time and avoid the syscall.
+// avoid inconsitent clang-format behavior
+using time_ptr_t = time_t *;
+LLVM_LIBC_FUNCTION(time_t, time, (time_ptr_t tp)) {
struct timespec ts;
auto result = internal::clock_gettime(CLOCK_REALTIME, &ts);
if (!result.has_value()) {
diff --git a/libc/src/time/windows/CMakeLists.txt b/libc/src/time/windows/CMakeLists.txt
new file mode 100644
index 000000000000..6f242cd168f5
--- /dev/null
+++ b/libc/src/time/windows/CMakeLists.txt
@@ -0,0 +1,13 @@
+add_entrypoint_object(
+ clock_getres
+ SRCS
+ clock_getres.cpp
+ DEPENDS
+ libc.src.__support.time.windows.performance_counter
+ libc.src.__support.time.units
+ libc.src.__support.common
+ libc.src.__support.macros.optimization
+ libc.hdr.time_macros
+ libc.hdr.types.time_t
+ libc.hdr.types.struct_timespec
+)
diff --git a/libc/src/time/windows/clock_getres.cpp b/libc/src/time/windows/clock_getres.cpp
new file mode 100644
index 000000000000..b8c0c82aa641
--- /dev/null
+++ b/libc/src/time/windows/clock_getres.cpp
@@ -0,0 +1,110 @@
+//===-- Windows implementation of clock_getres ------------------*- C++ -*-===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/time_macros.h"
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
+
+#include "src/__support/CPP/limits.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/time/units.h"
+#include "src/__support/time/windows/performance_counter.h"
+#include "src/errno/libc_errno.h"
+#include "src/time/clock_getres.h"
+
+#define WIN32_LEAN_AND_MEAN
+#define NOMINMAX
+#include <Windows.h>
+
+// add in dependencies for GetSystemTimeAdjustmentPrecise
+#pragma comment(lib, "mincore.lib")
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(int, clock_getres, (clockid_t id, struct timespec *res)) {
+ using namespace time_units;
+ // POSIX allows nullptr to be passed as res, in which case the function should
+ // do nothing.
+ if (res == nullptr)
+ return 0;
+ constexpr unsigned long long HNS_PER_SEC = 1_s_ns / 100ULL;
+ constexpr unsigned long long SEC_LIMIT =
+ cpp::numeric_limits<decltype(res->tv_sec)>::max();
+ // For CLOCK_MONOTONIC, we are using performance counter
+ // https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
+ // Hence, the resolution is given by the performance counter frequency.
+ // For CLOCK_REALTIME, the precision is given by
+ // GetSystemTimeAdjustmentPrecise
+ // (https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeadjustmentprecise)
+ // For CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID, the precision is
+ // given by GetSystemTimeAdjustment
+ // (https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeadjustment)
+ switch (id) {
+ default:
+ libc_errno = EINVAL;
+ return -1;
+
+ case CLOCK_MONOTONIC: {
+ long long freq = performance_counter::get_ticks_per_second();
+ __builtin_assume(freq != 0);
+ // division of 1 second by frequency, rounded up.
+ long long tv_sec = static_cast<long long>(freq == 1);
+ long long tv_nsec =
+ LIBC_LIKELY(freq != 1) ? 1ll + ((1_s_ns - 1ll) / freq) : 0ll;
+ // not possible to overflow tv_sec, tv_nsec
+ res->tv_sec = static_cast<decltype(res->tv_sec)>(tv_sec);
+ res->tv_nsec = static_cast<decltype(res->tv_nsec)>(tv_nsec);
+ break;
+ }
+
+ case CLOCK_REALTIME: {
+ [[clang::uninitialized]] DWORD64 time_adjustment;
+ [[clang::uninitialized]] DWORD64 time_increment;
+ [[clang::uninitialized]] BOOL time_adjustment_disabled;
+ if (!::GetSystemTimeAdjustmentPrecise(&time_adjustment, &time_increment,
+ &time_adjustment_disabled)) {
+ libc_errno = EINVAL;
+ return -1;
+ }
+ DWORD64 tv_sec = time_increment / HNS_PER_SEC;
+ DWORD64 tv_nsec = (time_increment % HNS_PER_SEC) * 100ULL;
+ if (LIBC_UNLIKELY(tv_sec > SEC_LIMIT)) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+ res->tv_sec = static_cast<decltype(res->tv_sec)>(tv_sec);
+ res->tv_nsec = static_cast<decltype(res->tv_nsec)>(tv_nsec);
+ break;
+ }
+ case CLOCK_PROCESS_CPUTIME_ID:
+ case CLOCK_THREAD_CPUTIME_ID: {
+ [[clang::uninitialized]] DWORD time_adjustment;
+ [[clang::uninitialized]] DWORD time_increment;
+ [[clang::uninitialized]] BOOL time_adjustment_disabled;
+ if (!::GetSystemTimeAdjustment(&time_adjustment, &time_increment,
+ &time_adjustment_disabled)) {
+ libc_errno = EINVAL;
+ return -1;
+ }
+ DWORD hns_per_sec = static_cast<DWORD>(HNS_PER_SEC);
+ DWORD sec_limit = static_cast<DWORD>(SEC_LIMIT);
+ DWORD tv_sec = time_increment / hns_per_sec;
+ DWORD tv_nsec = (time_increment % hns_per_sec) * 100UL;
+ if (LIBC_UNLIKELY(tv_sec > sec_limit)) {
+ libc_errno = EOVERFLOW;
+ return -1;
+ }
+ res->tv_sec = static_cast<decltype(res->tv_sec)>(tv_sec);
+ res->tv_nsec = static_cast<decltype(res->tv_nsec)>(tv_nsec);
+ break;
+ }
+ }
+ return 0;
+}
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/dup.h b/libc/src/unistd/dup.h
index 63f093c0ee43..57601455acc6 100644
--- a/libc/src/unistd/dup.h
+++ b/libc/src/unistd/dup.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_UNISTD_DUP_H
#define LLVM_LIBC_SRC_UNISTD_DUP_H
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/dup2.h b/libc/src/unistd/dup2.h
index 060c112daf08..e2cf62389bca 100644
--- a/libc/src/unistd/dup2.h
+++ b/libc/src/unistd/dup2.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_UNISTD_DUP2_H
#define LLVM_LIBC_SRC_UNISTD_DUP2_H
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/dup3.h b/libc/src/unistd/dup3.h
index f3868867123b..06d9b23dbd20 100644
--- a/libc/src/unistd/dup3.h
+++ b/libc/src/unistd/dup3.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_UNISTD_DUP3_H
#define LLVM_LIBC_SRC_UNISTD_DUP3_H
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/fork.h b/libc/src/unistd/fork.h
index b6fd5763b3a5..a9f8a9795d3a 100644
--- a/libc/src/unistd/fork.h
+++ b/libc/src/unistd/fork.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_FORK_H
#define LLVM_LIBC_SRC_UNISTD_FORK_H
+#include "hdr/types/pid_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/ftruncate.h b/libc/src/unistd/ftruncate.h
index cd8d363727c4..95901c8b7003 100644
--- a/libc/src/unistd/ftruncate.h
+++ b/libc/src/unistd/ftruncate.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_FTRUNCATE_H
#define LLVM_LIBC_SRC_UNISTD_FTRUNCATE_H
+#include "hdr/types/off_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/getcwd.h b/libc/src/unistd/getcwd.h
index 8b63a91c26b5..3943c0217ec1 100644
--- a/libc/src/unistd/getcwd.h
+++ b/libc/src/unistd/getcwd.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_GETCWD_H
#define LLVM_LIBC_SRC_UNISTD_GETCWD_H
+#include "hdr/types/size_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/geteuid.h b/libc/src/unistd/geteuid.h
index 9469797bd3d4..6827266ee81d 100644
--- a/libc/src/unistd/geteuid.h
+++ b/libc/src/unistd/geteuid.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_GETEUID_H
#define LLVM_LIBC_SRC_UNISTD_GETEUID_H
+#include "hdr/types/uid_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/getopt.h b/libc/src/unistd/getopt.h
index 1be3331dcd98..0be639d87119 100644
--- a/libc/src/unistd/getopt.h
+++ b/libc/src/unistd/getopt.h
@@ -10,8 +10,8 @@
#define LLVM_LIBC_SRC_UNISTD_GETOPT_H
#include "hdr/types/FILE.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/getpid.h b/libc/src/unistd/getpid.h
index c3c55b0c06b1..9e2f156266b9 100644
--- a/libc/src/unistd/getpid.h
+++ b/libc/src/unistd/getpid.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_GETPID_H
#define LLVM_LIBC_SRC_UNISTD_GETPID_H
+#include "hdr/types/pid_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/getppid.h b/libc/src/unistd/getppid.h
index d820791bc06f..8243fa93eddd 100644
--- a/libc/src/unistd/getppid.h
+++ b/libc/src/unistd/getppid.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_GETPPID_H
#define LLVM_LIBC_SRC_UNISTD_GETPPID_H
+#include "hdr/types/pid_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/getuid.h b/libc/src/unistd/getuid.h
index dd82c7119d40..f8b3731a9c06 100644
--- a/libc/src/unistd/getuid.h
+++ b/libc/src/unistd/getuid.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_GETUID_H
#define LLVM_LIBC_SRC_UNISTD_GETUID_H
+#include "hdr/types/uid_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/isatty.h b/libc/src/unistd/isatty.h
index 6dd1b7b81717..5c8be6541c99 100644
--- a/libc/src/unistd/isatty.h
+++ b/libc/src/unistd/isatty.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_UNISTD_ISATTY_H
#define LLVM_LIBC_SRC_UNISTD_ISATTY_H
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/link.h b/libc/src/unistd/link.h
index 9b27aa1accf4..c1c26c5e0d49 100644
--- a/libc/src/unistd/link.h
+++ b/libc/src/unistd/link.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_UNISTD_LINK_H
#define LLVM_LIBC_SRC_UNISTD_LINK_H
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 8a4487314141..ed360c73354a 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -45,6 +45,7 @@ add_entrypoint_object(
HDRS
../dup.h
DEPENDS
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -72,6 +73,7 @@ add_entrypoint_object(
HDRS
../dup3.h
DEPENDS
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -98,6 +100,8 @@ add_entrypoint_object(
HDRS
../fork.h
DEPENDS
+ libc.hdr.types.pid_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.threads.fork_callbacks
@@ -166,6 +170,8 @@ add_entrypoint_object(
HDRS
../ftruncate.h
DEPENDS
+ libc.hdr.types.off_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -179,6 +185,8 @@ add_entrypoint_object(
HDRS
../getcwd.h
DEPENDS
+ libc.hdr.types.size_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -192,6 +200,8 @@ add_entrypoint_object(
HDRS
../geteuid.h
DEPENDS
+ libc.hdr.types.uid_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -204,6 +214,8 @@ add_entrypoint_object(
HDRS
../getpid.h
DEPENDS
+ libc.hdr.types.pid_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -216,6 +228,8 @@ add_entrypoint_object(
HDRS
../getppid.h
DEPENDS
+ libc.hdr.types.pid_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -228,6 +242,8 @@ add_entrypoint_object(
HDRS
../getuid.h
DEPENDS
+ libc.hdr.types.uid_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -240,6 +256,7 @@ add_entrypoint_object(
HDRS
../isatty.h
DEPENDS
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_ioctl
libc.include.sys_syscall
@@ -282,6 +299,8 @@ add_entrypoint_object(
HDRS
../lseek.h
DEPENDS
+ libc.hdr.types.off_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -349,6 +368,10 @@ add_entrypoint_object(
HDRS
../pread.h
DEPENDS
+ libc.hdr.types.off_t
+ libc.hdr.types.size_t
+ libc.hdr.types.ssize_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -363,6 +386,10 @@ add_entrypoint_object(
HDRS
../pwrite.h
DEPENDS
+ libc.hdr.types.off_t
+ libc.hdr.types.size_t
+ libc.hdr.types.ssize_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -376,6 +403,9 @@ add_entrypoint_object(
HDRS
../read.h
DEPENDS
+ libc.hdr.types.size_t
+ libc.hdr.types.ssize_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -404,6 +434,8 @@ add_entrypoint_object(
HDRS
../readlink.h
DEPENDS
+ libc.hdr.types.size_t
+ libc.hdr.types.ssize_t
libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
@@ -418,6 +450,8 @@ add_entrypoint_object(
HDRS
../readlinkat.h
DEPENDS
+ libc.hdr.types.size_t
+ libc.hdr.types.ssize_t
libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
@@ -485,6 +519,8 @@ add_entrypoint_object(
HDRS
../truncate.h
DEPENDS
+ libc.hdr.types.off_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
@@ -526,6 +562,9 @@ add_entrypoint_object(
HDRS
../write.h
DEPENDS
+ libc.hdr.types.size_t
+ libc.hdr.types.ssize_t
+ libc.hdr.fcntl_macros
libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
diff --git a/libc/src/unistd/linux/ftruncate.cpp b/libc/src/unistd/linux/ftruncate.cpp
index 39cb3b5778fa..ccbb0634664a 100644
--- a/libc/src/unistd/linux/ftruncate.cpp
+++ b/libc/src/unistd/linux/ftruncate.cpp
@@ -11,11 +11,11 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"
#include <stdint.h> // For uint64_t.
#include <sys/syscall.h> // For syscall numbers.
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/linux/lseek.cpp b/libc/src/unistd/linux/lseek.cpp
index 9486cecf3b12..0e957498da74 100644
--- a/libc/src/unistd/linux/lseek.cpp
+++ b/libc/src/unistd/linux/lseek.cpp
@@ -14,8 +14,8 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "hdr/types/off_t.h"
#include <sys/syscall.h> // For syscall numbers.
-#include <unistd.h> // For off_t.
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
index 1540eb499ec1..f785ff321c7d 100644
--- a/libc/src/unistd/linux/sysconf.cpp
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -10,11 +10,11 @@
#include "src/__support/common.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"
#include "src/sys/auxv/getauxval.h"
#include <sys/auxv.h>
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/linux/truncate.cpp b/libc/src/unistd/linux/truncate.cpp
index 283cf4098cf4..8236edb480d1 100644
--- a/libc/src/unistd/linux/truncate.cpp
+++ b/libc/src/unistd/linux/truncate.cpp
@@ -13,9 +13,9 @@
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"
+#include "hdr/unistd_macros.h"
#include <stdint.h> // For uint64_t.
#include <sys/syscall.h> // For syscall numbers.
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/lseek.h b/libc/src/unistd/lseek.h
index a8704ec7058d..e8442738dfbd 100644
--- a/libc/src/unistd/lseek.h
+++ b/libc/src/unistd/lseek.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_LSEEK_H
#define LLVM_LIBC_SRC_UNISTD_LSEEK_H
+#include "hdr/types/off_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/pread.h b/libc/src/unistd/pread.h
index 4723675e82a2..f8b66c548868 100644
--- a/libc/src/unistd/pread.h
+++ b/libc/src/unistd/pread.h
@@ -9,8 +9,11 @@
#ifndef LLVM_LIBC_SRC_UNISTD_PREAD_H
#define LLVM_LIBC_SRC_UNISTD_PREAD_H
+#include "hdr/types/off_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/pwrite.h b/libc/src/unistd/pwrite.h
index baffbe48b643..08ebb8934721 100644
--- a/libc/src/unistd/pwrite.h
+++ b/libc/src/unistd/pwrite.h
@@ -9,8 +9,11 @@
#ifndef LLVM_LIBC_SRC_UNISTD_PWRITE_H
#define LLVM_LIBC_SRC_UNISTD_PWRITE_H
+#include "hdr/types/off_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/read.h b/libc/src/unistd/read.h
index 01231cb82e35..5d3527372558 100644
--- a/libc/src/unistd/read.h
+++ b/libc/src/unistd/read.h
@@ -9,8 +9,10 @@
#ifndef LLVM_LIBC_SRC_UNISTD_READ_H
#define LLVM_LIBC_SRC_UNISTD_READ_H
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/readlink.h b/libc/src/unistd/readlink.h
index a73e9740c746..b63643e2a701 100644
--- a/libc/src/unistd/readlink.h
+++ b/libc/src/unistd/readlink.h
@@ -9,8 +9,10 @@
#ifndef LLVM_LIBC_SRC_UNISTD_READLINK_H
#define LLVM_LIBC_SRC_UNISTD_READLINK_H
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/readlinkat.h b/libc/src/unistd/readlinkat.h
index 6bdd48b537fc..0f5657e45a25 100644
--- a/libc/src/unistd/readlinkat.h
+++ b/libc/src/unistd/readlinkat.h
@@ -9,8 +9,10 @@
#ifndef LLVM_LIBC_SRC_UNISTD_READLINKAT_H
#define LLVM_LIBC_SRC_UNISTD_READLINKAT_H
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/swab.h b/libc/src/unistd/swab.h
index caa9c7100109..f6fa3414c43f 100644
--- a/libc/src/unistd/swab.h
+++ b/libc/src/unistd/swab.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_UNISTD_SWAB_H
#define LLVM_LIBC_SRC_UNISTD_SWAB_H
+#include "hdr/types/ssize_t.h"
#include "src/__support/macros/config.h"
-#include <unistd.h> // For ssize_t
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/symlink.h b/libc/src/unistd/symlink.h
index 47f04f8845b4..c743a32a8930 100644
--- a/libc/src/unistd/symlink.h
+++ b/libc/src/unistd/symlink.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_UNISTD_SYMLINK_H
#define LLVM_LIBC_SRC_UNISTD_SYMLINK_H
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/symlinkat.h b/libc/src/unistd/symlinkat.h
index 9f8ad517af5a..6697ce4d537e 100644
--- a/libc/src/unistd/symlinkat.h
+++ b/libc/src/unistd/symlinkat.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_UNISTD_SYMLINKAT_H
#define LLVM_LIBC_SRC_UNISTD_SYMLINKAT_H
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/syscall.h b/libc/src/unistd/syscall.h
index db70745719cf..7f82bd8a452f 100644
--- a/libc/src/unistd/syscall.h
+++ b/libc/src/unistd/syscall.h
@@ -9,9 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_SYSCALL_H
#define LLVM_LIBC_SRC_UNISTD_SYSCALL_H
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
#include <stdarg.h>
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/sysconf.h b/libc/src/unistd/sysconf.h
index 1b3f39e41350..470c4d846568 100644
--- a/libc/src/unistd/sysconf.h
+++ b/libc/src/unistd/sysconf.h
@@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_UNISTD_SYSCONF_H
#define LLVM_LIBC_SRC_UNISTD_SYSCONF_H
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/truncate.h b/libc/src/unistd/truncate.h
index 9ba5cf831752..1e1066351953 100644
--- a/libc/src/unistd/truncate.h
+++ b/libc/src/unistd/truncate.h
@@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_UNISTD_TRUNCATE_H
#define LLVM_LIBC_SRC_UNISTD_TRUNCATE_H
+#include "hdr/types/off_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/unistd/write.h b/libc/src/unistd/write.h
index e40ce19e2176..c5ba6bf719aa 100644
--- a/libc/src/unistd/write.h
+++ b/libc/src/unistd/write.h
@@ -9,8 +9,10 @@
#ifndef LLVM_LIBC_SRC_UNISTD_WRITE_H
#define LLVM_LIBC_SRC_UNISTD_WRITE_H
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <unistd.h>
namespace LIBC_NAMESPACE_DECL {