summaryrefslogtreecommitdiff
path: root/libc/src
diff options
context:
space:
mode:
authorFlorian Mayer <fmayer@google.com>2024-08-07 14:00:59 -0700
committerFlorian Mayer <fmayer@google.com>2024-08-07 14:00:59 -0700
commit890289dfb61757cc3f8fd5feb093131ebc3b7477 (patch)
treeb9b69f1881544d20a2c05f84c42a0a5805128f4e /libc/src
parentb58d0717d588624eae77aea330e94f52607448c9 (diff)
parent6a3604ef8592edf39fedd6af8100aefafd6d931d (diff)
[𝘀𝗽𝗿] changes introduced through rebaseusers/fmayer/spr/main.compiler-rt-tsan-leave-bufferedstacktrace-uninit
Created using spr 1.3.4 [skip ci]
Diffstat (limited to 'libc/src')
-rw-r--r--libc/src/__support/threads/spin_lock.h36
-rw-r--r--libc/src/math/CMakeLists.txt16
-rw-r--r--libc/src/math/exp10f16.h21
-rw-r--r--libc/src/math/exp2f16.h21
-rw-r--r--libc/src/math/fdiv.h20
-rw-r--r--libc/src/math/fdivf128.h21
-rw-r--r--libc/src/math/fdivl.h20
-rw-r--r--libc/src/math/ffma.h20
-rw-r--r--libc/src/math/ffmaf128.h21
-rw-r--r--libc/src/math/ffmal.h20
-rw-r--r--libc/src/math/fsub.h20
-rw-r--r--libc/src/math/fsubf128.h21
-rw-r--r--libc/src/math/fsubl.h20
-rw-r--r--libc/src/math/generic/CMakeLists.txt191
-rw-r--r--libc/src/math/generic/exp10f16.cpp170
-rw-r--r--libc/src/math/generic/exp2f16.cpp127
-rw-r--r--libc/src/math/generic/expxf16.h28
-rw-r--r--libc/src/math/generic/fdiv.cpp20
-rw-r--r--libc/src/math/generic/fdivf128.cpp20
-rw-r--r--libc/src/math/generic/fdivl.cpp20
-rw-r--r--libc/src/math/generic/ffma.cpp20
-rw-r--r--libc/src/math/generic/ffmaf128.cpp20
-rw-r--r--libc/src/math/generic/ffmal.cpp21
-rw-r--r--libc/src/math/generic/fsub.cpp20
-rw-r--r--libc/src/math/generic/fsubf128.cpp20
-rw-r--r--libc/src/math/generic/fsubl.cpp20
-rw-r--r--libc/src/math/generic/getpayloadl.cpp20
-rw-r--r--libc/src/math/generic/pow.cpp68
-rw-r--r--libc/src/math/generic/remainderf128.cpp21
-rw-r--r--libc/src/math/getpayloadl.h20
-rw-r--r--libc/src/math/remainderf128.h21
-rw-r--r--libc/src/pthread/CMakeLists.txt65
-rw-r--r--libc/src/pthread/pthread_spin_destroy.cpp47
-rw-r--r--libc/src/pthread/pthread_spin_destroy.h21
-rw-r--r--libc/src/pthread/pthread_spin_init.cpp37
-rw-r--r--libc/src/pthread/pthread_spin_init.h21
-rw-r--r--libc/src/pthread/pthread_spin_lock.cpp47
-rw-r--r--libc/src/pthread/pthread_spin_lock.h21
-rw-r--r--libc/src/pthread/pthread_spin_trylock.cpp41
-rw-r--r--libc/src/pthread/pthread_spin_trylock.h22
-rw-r--r--libc/src/pthread/pthread_spin_unlock.cpp44
-rw-r--r--libc/src/pthread/pthread_spin_unlock.h21
-rw-r--r--libc/src/time/mktime.cpp18
-rw-r--r--libc/src/time/time_utils.h5
44 files changed, 1479 insertions, 55 deletions
diff --git a/libc/src/__support/threads/spin_lock.h b/libc/src/__support/threads/spin_lock.h
index 8a3655056846..e176ad9eeac2 100644
--- a/libc/src/__support/threads/spin_lock.h
+++ b/libc/src/__support/threads/spin_lock.h
@@ -11,26 +11,17 @@
#include "src/__support/CPP/atomic.h"
#include "src/__support/macros/attributes.h"
-#include "src/__support/macros/properties/architectures.h"
#include "src/__support/threads/sleep.h"
namespace LIBC_NAMESPACE_DECL {
-namespace spinlock {
-template <typename LockWord, typename Return>
-using AtomicOp = Return (cpp::Atomic<LockWord>::*)(LockWord, cpp::MemoryOrder,
- cpp::MemoryScope);
-}
-
-template <typename LockWord, spinlock::AtomicOp<LockWord, LockWord> Acquire,
- spinlock::AtomicOp<LockWord, void> Release>
-class SpinLockAdaptor {
- cpp::Atomic<LockWord> flag;
+class SpinLock {
+ cpp::Atomic<unsigned char> flag;
public:
- LIBC_INLINE constexpr SpinLockAdaptor() : flag{false} {}
+ LIBC_INLINE constexpr SpinLock() : flag{0} {}
LIBC_INLINE bool try_lock() {
- return !flag.*Acquire(static_cast<LockWord>(1), cpp::MemoryOrder::ACQUIRE);
+ return !flag.exchange(1u, cpp::MemoryOrder::ACQUIRE);
}
LIBC_INLINE void lock() {
// clang-format off
@@ -60,22 +51,15 @@ public:
while (flag.load(cpp::MemoryOrder::RELAXED))
sleep_briefly();
}
- LIBC_INLINE void unlock() {
- flag.*Release(static_cast<LockWord>(0), cpp::MemoryOrder::RELEASE);
+ LIBC_INLINE void unlock() { flag.store(0u, cpp::MemoryOrder::RELEASE); }
+ LIBC_INLINE bool is_locked() { return flag.load(cpp::MemoryOrder::ACQUIRE); }
+ LIBC_INLINE bool is_invalid() {
+ return flag.load(cpp::MemoryOrder::ACQUIRE) > 1;
}
+ // poison the lock
+ LIBC_INLINE ~SpinLock() { flag.store(0xffu, cpp::MemoryOrder::RELEASE); }
};
-// It is reported that atomic operations with higher-order semantics
-// lead to better performance on GPUs.
-#ifdef LIBC_TARGET_ARCH_IS_GPU
-using SpinLock =
- SpinLockAdaptor<unsigned int, &cpp::Atomic<unsigned int>::fetch_or,
- &cpp::Atomic<unsigned int>::fetch_and>;
-#else
-using SpinLock = SpinLockAdaptor<bool, &cpp::Atomic<bool>::exchange,
- &cpp::Atomic<bool>::store>;
-#endif
-
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_SPIN_LOCK_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index aec94d41ad08..a0bd5269169f 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -110,11 +110,13 @@ add_math_entrypoint_object(expf16)
add_math_entrypoint_object(exp2)
add_math_entrypoint_object(exp2f)
+add_math_entrypoint_object(exp2f16)
add_math_entrypoint_object(exp2m1f)
add_math_entrypoint_object(exp10)
add_math_entrypoint_object(exp10f)
+add_math_entrypoint_object(exp10f16)
add_math_entrypoint_object(expm1)
add_math_entrypoint_object(expm1f)
@@ -166,6 +168,14 @@ add_math_entrypoint_object(fdiml)
add_math_entrypoint_object(fdimf16)
add_math_entrypoint_object(fdimf128)
+add_math_entrypoint_object(fdiv)
+add_math_entrypoint_object(fdivl)
+add_math_entrypoint_object(fdivf128)
+
+add_math_entrypoint_object(ffma)
+add_math_entrypoint_object(ffmal)
+add_math_entrypoint_object(ffmaf128)
+
add_math_entrypoint_object(floor)
add_math_entrypoint_object(floorf)
add_math_entrypoint_object(floorl)
@@ -263,8 +273,13 @@ add_math_entrypoint_object(fromfpxl)
add_math_entrypoint_object(fromfpxf16)
add_math_entrypoint_object(fromfpxf128)
+add_math_entrypoint_object(fsub)
+add_math_entrypoint_object(fsubl)
+add_math_entrypoint_object(fsubf128)
+
add_math_entrypoint_object(getpayload)
add_math_entrypoint_object(getpayloadf)
+add_math_entrypoint_object(getpayloadl)
add_math_entrypoint_object(getpayloadf16)
add_math_entrypoint_object(getpayloadf128)
@@ -385,6 +400,7 @@ add_math_entrypoint_object(remainder)
add_math_entrypoint_object(remainderf)
add_math_entrypoint_object(remainderl)
add_math_entrypoint_object(remainderf16)
+add_math_entrypoint_object(remainderf128)
add_math_entrypoint_object(remquo)
add_math_entrypoint_object(remquof)
diff --git a/libc/src/math/exp10f16.h b/libc/src/math/exp10f16.h
new file mode 100644
index 000000000000..62a62f7da59e
--- /dev/null
+++ b/libc/src/math/exp10f16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for exp10f16 ----------------------*- 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_EXP10F16_H
+#define LLVM_LIBC_SRC_MATH_EXP10F16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 exp10f16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_EXP10F16_H
diff --git a/libc/src/math/exp2f16.h b/libc/src/math/exp2f16.h
new file mode 100644
index 000000000000..71361b997ae8
--- /dev/null
+++ b/libc/src/math/exp2f16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for exp2f16 -----------------------*- 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_EXP2F16_H
+#define LLVM_LIBC_SRC_MATH_EXP2F16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 exp2f16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_EXP2F16_H
diff --git a/libc/src/math/fdiv.h b/libc/src/math/fdiv.h
new file mode 100644
index 000000000000..4d60afaf469f
--- /dev/null
+++ b/libc/src/math/fdiv.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for fdiv --------------------------*- 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_FDIV_H
+#define LLVM_LIBC_SRC_MATH_FDIV_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fdiv(double x, double y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FDIV_H
diff --git a/libc/src/math/fdivf128.h b/libc/src/math/fdivf128.h
new file mode 100644
index 000000000000..1a150389b685
--- /dev/null
+++ b/libc/src/math/fdivf128.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for fdivf128 ----------------------*- 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_FDIVF128_H
+#define LLVM_LIBC_SRC_MATH_FDIVF128_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fdivf128(float128 x, float128 y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FDIVF128_H
diff --git a/libc/src/math/fdivl.h b/libc/src/math/fdivl.h
new file mode 100644
index 000000000000..1943eaae7783
--- /dev/null
+++ b/libc/src/math/fdivl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for fdivl -------------------------*- 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_FDIVL_H
+#define LLVM_LIBC_SRC_MATH_FDIVL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fdivl(long double x, long double y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FDIVL_H
diff --git a/libc/src/math/ffma.h b/libc/src/math/ffma.h
new file mode 100644
index 000000000000..98d89f2eef98
--- /dev/null
+++ b/libc/src/math/ffma.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for ffma --------------------------*- 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_FFMA_H
+#define LLVM_LIBC_SRC_MATH_FFMA_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float ffma(double x, double y, double z);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FFMA_H
diff --git a/libc/src/math/ffmaf128.h b/libc/src/math/ffmaf128.h
new file mode 100644
index 000000000000..741099780b8d
--- /dev/null
+++ b/libc/src/math/ffmaf128.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for ffmaf128 ----------------------*- 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_FFMAF128_H
+#define LLVM_LIBC_SRC_MATH_FFMAF128_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float ffmaf128(float128 x, float128 y, float128 z);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FFMAF128_H
diff --git a/libc/src/math/ffmal.h b/libc/src/math/ffmal.h
new file mode 100644
index 000000000000..75fd40a0a92a
--- /dev/null
+++ b/libc/src/math/ffmal.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for ffmal -------------------------*- 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_FFMAL_H
+#define LLVM_LIBC_SRC_MATH_FFMAL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float ffmal(long double x, long double y, long double z);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FFMAL_H
diff --git a/libc/src/math/fsub.h b/libc/src/math/fsub.h
new file mode 100644
index 000000000000..f17f0fd3ec30
--- /dev/null
+++ b/libc/src/math/fsub.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for fsub --------------------------*- 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_FSUB_H
+#define LLVM_LIBC_SRC_MATH_FSUB_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fsub(double x, double y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FSUB_H
diff --git a/libc/src/math/fsubf128.h b/libc/src/math/fsubf128.h
new file mode 100644
index 000000000000..4f41c7d8bb04
--- /dev/null
+++ b/libc/src/math/fsubf128.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for fsubf128 ----------------------*- 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_FSUBF128_H
+#define LLVM_LIBC_SRC_MATH_FSUBF128_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fsubf128(float128 x, float128 y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FSUBF128_H
diff --git a/libc/src/math/fsubl.h b/libc/src/math/fsubl.h
new file mode 100644
index 000000000000..32570ca3dc0f
--- /dev/null
+++ b/libc/src/math/fsubl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for fsubl -------------------------*- 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_FSUBL_H
+#define LLVM_LIBC_SRC_MATH_FSUBL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float fsubl(long double x, long double y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_FSUBL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 2fe6cc4d39d9..be5cc2e02635 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1396,6 +1396,29 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ exp2f16
+ SRCS
+ exp2f16.cpp
+ HDRS
+ ../exp2f16.h
+ DEPENDS
+ .expxf16
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.CPP.array
+ libc.src.__support.FPUtil.except_value_utils
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.nearest_integer
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.optimization
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
exp2m1f
SRCS
exp2m1f.cpp
@@ -1476,6 +1499,30 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ exp10f16
+ SRCS
+ exp10f16.cpp
+ HDRS
+ ../exp10f16.h
+ DEPENDS
+ .expxf16
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.CPP.array
+ libc.src.__support.FPUtil.except_value_utils
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.nearest_integer
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.optimization
+ libc.src.__support.macros.properties.cpu_features
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
expm1
SRCS
expm1.cpp
@@ -2885,6 +2932,43 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ fsub
+ SRCS
+ fsub.cpp
+ HDRS
+ ../fsub.h
+ DEPENDS
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
+ fsubl
+ SRCS
+ fsubl.cpp
+ HDRS
+ ../fsubl.h
+ DEPENDS
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
+ fsubf128
+ SRCS
+ fsubf128.cpp
+ HDRS
+ ../fsubf128.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.add_sub
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
sqrt
SRCS
sqrt.cpp
@@ -3045,6 +3129,19 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ remainderf128
+ SRCS
+ remainderf128.cpp
+ HDRS
+ ../remainderf128.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.division_and_remainder_operations
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
hypotf
SRCS
hypotf.cpp
@@ -3124,6 +3221,80 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ fdiv
+ SRCS
+ fdiv.cpp
+ HDRS
+ ../fdiv.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.FPUtil.generic.div
+)
+
+add_entrypoint_object(
+ fdivl
+ SRCS
+ fdivl.cpp
+ HDRS
+ ../fdivl.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.FPUtil.generic.div
+)
+
+add_entrypoint_object(
+ fdivf128
+ SRCS
+ fdivf128.cpp
+ HDRS
+ ../fdivf128.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.generic.div
+)
+
+add_entrypoint_object(
+ ffma
+ SRCS
+ ffma.cpp
+ HDRS
+ ../ffma.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.FPUtil.fma
+)
+
+add_entrypoint_object(
+ ffmal
+ SRCS
+ ffmal.cpp
+ HDRS
+ ../ffmal.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.FPUtil.fma
+)
+
+add_entrypoint_object(
+ ffmaf128
+ SRCS
+ ffmaf128.cpp
+ HDRS
+ ../ffmaf128.h
+ COMPILE_OPTIONS
+ -O3
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.fma
+)
+
+add_entrypoint_object(
hypot
SRCS
hypot.cpp
@@ -4241,6 +4412,18 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ getpayloadl
+ SRCS
+ getpayloadl.cpp
+ HDRS
+ ../getpayloadl.h
+ DEPENDS
+ libc.src.__support.FPUtil.basic_operations
+ COMPILE_OPTIONS
+ -O3
+)
+
+add_entrypoint_object(
getpayloadf16
SRCS
getpayloadf16.cpp
@@ -4738,3 +4921,11 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O3
)
+
+add_header_library(
+ expxf16
+ HDRS
+ expxf16.h
+ DEPENDS
+ libc.src.__support.CPP.array
+)
diff --git a/libc/src/math/generic/exp10f16.cpp b/libc/src/math/generic/exp10f16.cpp
new file mode 100644
index 000000000000..9959f7450b59
--- /dev/null
+++ b/libc/src/math/generic/exp10f16.cpp
@@ -0,0 +1,170 @@
+//===-- Half-precision 10^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/exp10f16.h"
+#include "expxf16.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/CPP/array.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/cpu_features.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+#ifdef LIBC_TARGET_CPU_HAS_FMA
+static constexpr size_t N_EXP10F16_EXCEPTS = 5;
+#else
+static constexpr size_t N_EXP10F16_EXCEPTS = 8;
+#endif
+
+static constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS>
+ EXP10F16_EXCEPTS = {{
+ // x = 0x1.8f4p-2, exp10f16(x) = 0x1.3ap+1 (RZ)
+ {0x363dU, 0x40e8U, 1U, 0U, 1U},
+ // x = 0x1.95cp-2, exp10f16(x) = 0x1.3ecp+1 (RZ)
+ {0x3657U, 0x40fbU, 1U, 0U, 0U},
+ // x = -0x1.018p-4, exp10f16(x) = 0x1.bbp-1 (RZ)
+ {0xac06U, 0x3aecU, 1U, 0U, 0U},
+ // x = -0x1.c28p+0, exp10f16(x) = 0x1.1ccp-6 (RZ)
+ {0xbf0aU, 0x2473U, 1U, 0U, 0U},
+ // x = -0x1.e1cp+1, exp10f16(x) = 0x1.694p-13 (RZ)
+ {0xc387U, 0x09a5U, 1U, 0U, 0U},
+#ifndef LIBC_TARGET_CPU_HAS_FMA
+ // x = 0x1.0cp+1, exp10f16(x) = 0x1.f04p+6 (RZ)
+ {0x4030U, 0x57c1U, 1U, 0U, 1U},
+ // x = 0x1.1b8p+1, exp10f16(x) = 0x1.47cp+7 (RZ)
+ {0x406eU, 0x591fU, 1U, 0U, 1U},
+ // x = 0x1.1b8p+2, exp10f16(x) = 0x1.a4p+14 (RZ)
+ {0x446eU, 0x7690U, 1U, 0U, 1U},
+#endif
+ }};
+
+// Generated by Sollya with the following commands:
+// > display = hexadecimal;
+// > round(log2(10), SG, RN);
+static constexpr float LOG2F_10 = 0x1.a934fp+1f;
+
+// Generated by Sollya with the following commands:
+// > display = hexadecimal;
+// > round(log10(2), SG, RN);
+static constexpr float LOG10F_2 = 0x1.344136p-2f;
+
+LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) {
+ using FPBits = fputil::FPBits<float16>;
+ FPBits x_bits(x);
+
+ uint16_t x_u = x_bits.uintval();
+ uint16_t x_abs = x_u & 0x7fffU;
+
+ // When |x| >= 5, or x is NaN.
+ if (LIBC_UNLIKELY(x_abs >= 0x4500U)) {
+ // exp10(NaN) = NaN
+ if (x_bits.is_nan()) {
+ if (x_bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ return x;
+ }
+
+ // When x >= 5.
+ if (x_bits.is_pos()) {
+ // exp10(+inf) = +inf
+ if (x_bits.is_inf())
+ return FPBits::inf().get_val();
+
+ switch (fputil::quick_get_round()) {
+ case FE_TONEAREST:
+ case FE_UPWARD:
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_OVERFLOW);
+ return FPBits::inf().get_val();
+ default:
+ return FPBits::max_normal().get_val();
+ }
+ }
+
+ // When x <= -8.
+ if (x_u >= 0xc800U) {
+ // exp10(-inf) = +0
+ if (x_bits.is_inf())
+ return FPBits::zero().get_val();
+
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
+
+ if (fputil::fenv_is_round_up())
+ return FPBits::min_subnormal().get_val();
+ return FPBits::zero().get_val();
+ }
+ }
+
+ // When x is 1, 2, 3, or 4. These are hard-to-round cases with exact results.
+ if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) {
+ switch (x_u) {
+ case 0x3c00U: // x = 1.0f16
+ return static_cast<float16>(10.0);
+ case 0x4000U: // x = 2.0f16
+ return static_cast<float16>(100.0);
+ case 0x4200U: // x = 3.0f16
+ return static_cast<float16>(1'000.0);
+ case 0x4400U: // x = 4.0f16
+ return static_cast<float16>(10'000.0);
+ }
+ }
+
+ if (auto r = EXP10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+
+ // For -8 < x < 5, to compute 10^x, we perform the following range reduction:
+ // find hi, mid, lo, such that:
+ // x = (hi + mid) * log2(10) + lo, in which
+ // hi is an integer,
+ // mid * 2^3 is an integer,
+ // -2^(-4) <= lo < 2^(-4).
+ // In particular,
+ // hi + mid = round(x * 2^3) * 2^(-3).
+ // Then,
+ // 10^x = 10^(hi + mid + lo) = 2^((hi + mid) * log2(10)) + 10^lo
+ // We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid
+ // by adding hi to the exponent field of 2^mid. 10^lo is computed using a
+ // degree-4 minimax polynomial generated by Sollya.
+
+ float xf = x;
+ float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f));
+ int x_hi_mid = static_cast<int>(kf);
+ int x_hi = x_hi_mid >> 3;
+ int x_mid = x_hi_mid & 0x7;
+ // lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x
+ float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf);
+
+ uint32_t exp2_hi_mid_bits =
+ EXP2_MID_BITS[x_mid] +
+ static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
+ float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
+ // Degree-4 minimax polynomial generated by Sollya with the following
+ // commands:
+ // > display = hexadecimal;
+ // > P = fpminimax((10^x - 1)/x, 3, [|SG...|], [-2^-4, 2^-4]);
+ // > 1 + x * P;
+ float exp10_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.26bb14p+1f, 0x1.53526p+1f,
+ 0x1.04b434p+1f, 0x1.2bcf9ep+0f);
+ return static_cast<float16>(exp2_hi_mid * exp10_lo);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/exp2f16.cpp b/libc/src/math/generic/exp2f16.cpp
new file mode 100644
index 000000000000..66b795670400
--- /dev/null
+++ b/libc/src/math/generic/exp2f16.cpp
@@ -0,0 +1,127 @@
+//===-- Half-precision 2^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/exp2f16.h"
+#include "expxf16.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/CPP/array.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static constexpr fputil::ExceptValues<float16, 3> EXP2F16_EXCEPTS = {{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.714p-11, exp2f16(x) = 0x1p+0 (RZ)
+ {0x11c5U, 0x3c00U, 1U, 0U, 1U},
+ // x = -0x1.558p-4, exp2f16(x) = 0x1.e34p-1 (RZ)
+ {0xad56U, 0x3b8dU, 1U, 0U, 0U},
+ // x = -0x1.d5cp-4, exp2f16(x) = 0x1.d8cp-1 (RZ)
+ {0xaf57U, 0x3b63U, 1U, 0U, 0U},
+}};
+
+LLVM_LIBC_FUNCTION(float16, exp2f16, (float16 x)) {
+ using FPBits = fputil::FPBits<float16>;
+ FPBits x_bits(x);
+
+ uint16_t x_u = x_bits.uintval();
+ uint16_t x_abs = x_u & 0x7fffU;
+
+ // When |x| >= 16, or x is NaN.
+ if (LIBC_UNLIKELY(x_abs >= 0x4c00U)) {
+ // exp2(NaN) = NaN
+ if (x_bits.is_nan()) {
+ if (x_bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ return x;
+ }
+
+ // When x >= 16.
+ if (x_bits.is_pos()) {
+ // exp2(+inf) = +inf
+ if (x_bits.is_inf())
+ return FPBits::inf().get_val();
+
+ switch (fputil::quick_get_round()) {
+ case FE_TONEAREST:
+ case FE_UPWARD:
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_OVERFLOW);
+ return FPBits::inf().get_val();
+ default:
+ return FPBits::max_normal().get_val();
+ }
+ }
+
+ // When x <= -25.
+ if (x_u >= 0xce40U) {
+ // exp2(-inf) = +0
+ if (x_bits.is_inf())
+ return FPBits::zero().get_val();
+
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
+
+ if (fputil::fenv_is_round_up())
+ return FPBits::min_subnormal().get_val();
+ return FPBits::zero().get_val();
+ }
+ }
+
+ if (auto r = EXP2F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+
+ // For -25 < x < 16, to compute 2^x, we perform the following range reduction:
+ // find hi, mid, lo, such that:
+ // x = hi + mid + lo, in which
+ // hi is an integer,
+ // mid * 2^3 is an integer,
+ // -2^(-4) <= lo < 2^(-4).
+ // In particular,
+ // hi + mid = round(x * 2^3) * 2^(-3).
+ // Then,
+ // 2^x = 2^(hi + mid + lo) = 2^hi * 2^mid * 2^lo.
+ // We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid
+ // by adding hi to the exponent field of 2^mid. 2^lo is computed using a
+ // degree-3 minimax polynomial generated by Sollya.
+
+ float xf = x;
+ float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
+ int x_hi_mid = static_cast<int>(kf);
+ int x_hi = x_hi_mid >> 3;
+ int x_mid = x_hi_mid & 0x7;
+ // lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x
+ float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf);
+
+ uint32_t exp2_hi_mid_bits =
+ EXP2_MID_BITS[x_mid] +
+ static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
+ float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
+ // Degree-3 minimax polynomial generated by Sollya with the following
+ // commands:
+ // > display = hexadecimal;
+ // > P = fpminimax((2^x - 1)/x, 2, [|SG...|], [-2^-4, 2^-4]);
+ // > 1 + x * P;
+ float exp2_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.62e43p-1f, 0x1.ec0aa6p-3f,
+ 0x1.c6b4a6p-5f);
+ return static_cast<float16>(exp2_hi_mid * exp2_lo);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/expxf16.h b/libc/src/math/generic/expxf16.h
new file mode 100644
index 000000000000..c33aca337b98
--- /dev/null
+++ b/libc/src/math/generic/expxf16.h
@@ -0,0 +1,28 @@
+//===-- Common utilities for half-precision exponential functions ---------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H
+#define LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H
+
+#include "src/__support/CPP/array.h"
+#include "src/__support/macros/config.h"
+#include <stdint.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+// Generated by Sollya with the following commands:
+// > display = hexadecimal;
+// > for i from 0 to 7 do printsingle(round(2^(i * 2^-3), SG, RN));
+constexpr cpp::array<uint32_t, 8> EXP2_MID_BITS = {
+ 0x3f80'0000U, 0x3f8b'95c2U, 0x3f98'37f0U, 0x3fa5'fed7U,
+ 0x3fb5'04f3U, 0x3fc5'672aU, 0x3fd7'44fdU, 0x3fea'c0c7U,
+};
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H
diff --git a/libc/src/math/generic/fdiv.cpp b/libc/src/math/generic/fdiv.cpp
new file mode 100644
index 000000000000..1d97fade22a3
--- /dev/null
+++ b/libc/src/math/generic/fdiv.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of fdiv 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/fdiv.h"
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fdiv, (double x, double y)) {
+ return fputil::generic::div<float>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fdivf128.cpp b/libc/src/math/generic/fdivf128.cpp
new file mode 100644
index 000000000000..192f13f5de10
--- /dev/null
+++ b/libc/src/math/generic/fdivf128.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of fdivf128 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/fdivf128.h"
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fdivf128, (float128 x, float128 y)) {
+ return fputil::generic::div<float>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fdivl.cpp b/libc/src/math/generic/fdivl.cpp
new file mode 100644
index 000000000000..dcd5debc2acd
--- /dev/null
+++ b/libc/src/math/generic/fdivl.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of fdivl 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/fdivl.h"
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fdivl, (long double x, long double y)) {
+ return fputil::generic::div<float>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ffma.cpp b/libc/src/math/generic/ffma.cpp
new file mode 100644
index 000000000000..a4c834ddd798
--- /dev/null
+++ b/libc/src/math/generic/ffma.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of ffma 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/ffma.h"
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, ffma, (double x, double y, double z)) {
+ return fputil::fma<float>(x, y, z);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ffmaf128.cpp b/libc/src/math/generic/ffmaf128.cpp
new file mode 100644
index 000000000000..55da93020faf
--- /dev/null
+++ b/libc/src/math/generic/ffmaf128.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of ffmaf128 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/ffmaf128.h"
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, ffmaf128, (float128 x, float128 y, float128 z)) {
+ return fputil::fma<float>(x, y, z);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/ffmal.cpp b/libc/src/math/generic/ffmal.cpp
new file mode 100644
index 000000000000..d5cd4f763cbe
--- /dev/null
+++ b/libc/src/math/generic/ffmal.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of ffmal 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/ffmal.h"
+#include "src/__support/FPUtil/FMA.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, ffmal,
+ (long double x, long double y, long double z)) {
+ return fputil::fma<float>(x, y, z);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fsub.cpp b/libc/src/math/generic/fsub.cpp
new file mode 100644
index 000000000000..97e28015c048
--- /dev/null
+++ b/libc/src/math/generic/fsub.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of fsub 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/fsub.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fsub, (double x, double y)) {
+ return fputil::generic::sub<float>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fsubf128.cpp b/libc/src/math/generic/fsubf128.cpp
new file mode 100644
index 000000000000..3efb34992b74
--- /dev/null
+++ b/libc/src/math/generic/fsubf128.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of fsubf128 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/fsubf128.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fsubf128, (float128 x, float128 y)) {
+ return fputil::generic::sub<float>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fsubl.cpp b/libc/src/math/generic/fsubl.cpp
new file mode 100644
index 000000000000..cad5a2d5d452
--- /dev/null
+++ b/libc/src/math/generic/fsubl.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of fsubl 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/fsubl.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, fsubl, (long double x, long double y)) {
+ return fputil::generic::sub<float>(x, y);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/getpayloadl.cpp b/libc/src/math/generic/getpayloadl.cpp
new file mode 100644
index 000000000000..028dc1e2611c
--- /dev/null
+++ b/libc/src/math/generic/getpayloadl.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of getpayloadl 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/getpayloadl.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(long double, getpayloadl, (const long double *x)) {
+ return fputil::getpayload(*x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/pow.cpp b/libc/src/math/generic/pow.cpp
index d2c5f7535082..20f914430261 100644
--- a/libc/src/math/generic/pow.cpp
+++ b/libc/src/math/generic/pow.cpp
@@ -358,42 +358,64 @@ LLVM_LIBC_FUNCTION(double, pow, (double x, double y)) {
// Then m_x = (1 + dx) / r, and
// log2(m_x) = log2( (1 + dx) / r )
// = log2(1 + dx) - log2(r).
- // Perform exact range reduction
+
+ // In order for the overall computations x^y = 2^(y * log2(x)) to have the
+ // relative errors < 2^-52 (1ULP), we will need to evaluate the exponent part
+ // y * log2(x) with absolute errors < 2^-52 (or better, 2^-53). Since the
+ // whole exponent range for double precision is bounded by
+ // |y * log2(x)| < 1076 ~ 2^10, we need to evaluate log2(x) with absolute
+ // errors < 2^-53 * 2^-10 = 2^-63.
+
+ // With that requirement, we use the following degree-6 polynomial
+ // approximation:
+ // P(dx) ~ log2(1 + dx) / dx
+ // Generated by Sollya with:
+ // > P = fpminimax(log2(1 + x)/x, 6, [|D...|], [-2^-8, 2^-7]); P;
+ // > dirtyinfnorm(log2(1 + x) - x*P, [-2^-8, 2^-7]);
+ // 0x1.d03cc...p-66
+ constexpr double COEFFS[] = {0x1.71547652b82fep0, -0x1.71547652b82e7p-1,
+ 0x1.ec709dc3b1fd5p-2, -0x1.7154766124215p-2,
+ 0x1.2776bd90259d8p-2, -0x1.ec586c6f3d311p-3,
+ 0x1.9c4775eccf524p-3};
+ // Error: ulp(dx^2) <= (2^-7)^2 * 2^-52 = 2^-66
+ // Extra errors from various computations and rounding directions, the overall
+ // errors we can be bounded by 2^-65.
+
double dx;
+ DoubleDouble dx_c0;
+
+ // Perform exact range reduction and exact product dx * c0.
#ifdef LIBC_TARGET_CPU_HAS_FMA
dx = fputil::multiply_add(RD[idx_x], m_x.get_val(), -1.0); // Exact
+ dx_c0 = fputil::exact_mult(COEFFS[0], dx);
#else
double c = FPBits(m_x.uintval() & 0x3fff'e000'0000'0000).get_val();
dx = fputil::multiply_add(RD[idx_x], m_x.get_val() - c, CD[idx_x]); // Exact
+ dx_c0 = fputil::exact_mult<true>(COEFFS[0], dx);
#endif // LIBC_TARGET_CPU_HAS_FMA
- // Degree-5 polynomial approximation:
- // dx * P(dx) ~ log2(1 + dx)
- // Generated by Sollya with:
- // > P = fpminimax(log2(1 + x)/x, 5, [|D...|], [-2^-8, 2^-7]);
- // > dirtyinfnorm(log2(1 + x)/x - P, [-2^-8, 2^-7]);
- // 0x1.653...p-52
- constexpr double COEFFS[] = {0x1.71547652b82fep0, -0x1.71547652b7a07p-1,
- 0x1.ec709dc458db1p-2, -0x1.715479c2266c9p-2,
- 0x1.2776ae1ddf8fp-2, -0x1.e7b2178870157p-3};
-
- double dx2 = dx * dx; // Exact
- double c0 = fputil::multiply_add(dx, COEFFS[1], COEFFS[0]);
- double c1 = fputil::multiply_add(dx, COEFFS[3], COEFFS[2]);
- double c2 = fputil::multiply_add(dx, COEFFS[5], COEFFS[4]);
+ double dx2 = dx * dx;
+ double c0 = fputil::multiply_add(dx, COEFFS[2], COEFFS[1]);
+ double c1 = fputil::multiply_add(dx, COEFFS[4], COEFFS[3]);
+ double c2 = fputil::multiply_add(dx, COEFFS[6], COEFFS[5]);
double p = fputil::polyeval(dx2, c0, c1, c2);
// s = e_x - log2(r) + dx * P(dx)
// Absolute error bound:
- // |log2(x) - log2_x.hi - log2_x.lo| < 2^-58.
- // Relative error bound:
- // |(log2_x.hi + log2_x.lo)/log2(x) - 1| < 2^-51.
- double log2_x_hi = e_x + LOG2_R_DD[idx_x].hi; // Exact
- // Error
- double log2_x_lo = fputil::multiply_add(dx, p, LOG2_R_DD[idx_x].lo);
-
- DoubleDouble log2_x = fputil::exact_add(log2_x_hi, log2_x_lo);
+ // |log2(x) - log2_x.hi - log2_x.lo| < 2^-65.
+
+ // Notice that e_x - log2(r).hi is exact, so we perform an exact sum of
+ // e_x - log2(r).hi and the high part of the product dx * c0:
+ // log2_x_hi.hi + log2_x_hi.lo = e_x - log2(r).hi + (dx * c0).hi
+ DoubleDouble log2_x_hi =
+ fputil::exact_add(e_x + LOG2_R_DD[idx_x].hi, dx_c0.hi);
+ // The low part is dx^2 * p + low part of (dx * c0) + low part of -log2(r).
+ double log2_x_lo =
+ fputil::multiply_add(dx2, p, dx_c0.lo + LOG2_R_DD[idx_x].lo);
+ // Perform accurate sums.
+ DoubleDouble log2_x = fputil::exact_add(log2_x_hi.hi, log2_x_lo);
+ log2_x.lo += log2_x_hi.lo;
// To compute 2^(y * log2(x)), we break the exponent into 3 parts:
// y * log(2) = hi + mid + lo, where
diff --git a/libc/src/math/generic/remainderf128.cpp b/libc/src/math/generic/remainderf128.cpp
new file mode 100644
index 000000000000..52b6c5149fdc
--- /dev/null
+++ b/libc/src/math/generic/remainderf128.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of remainderf128 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/remainderf128.h"
+#include "src/__support/FPUtil/DivisionAndRemainderOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float128, remainderf128, (float128 x, float128 y)) {
+ int quotient;
+ return fputil::remquo(x, y, quotient);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/getpayloadl.h b/libc/src/math/getpayloadl.h
new file mode 100644
index 000000000000..1ae9f86c19e0
--- /dev/null
+++ b/libc/src/math/getpayloadl.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for getpayloadl -------------------*- 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_GETPAYLOADL_H
+#define LLVM_LIBC_SRC_MATH_GETPAYLOADL_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+long double getpayloadl(const long double *x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_GETPAYLOADL_H
diff --git a/libc/src/math/remainderf128.h b/libc/src/math/remainderf128.h
new file mode 100644
index 000000000000..57f770a853d6
--- /dev/null
+++ b/libc/src/math/remainderf128.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for remainderf128 -----------------*- 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_REMAINDERF128_H
+#define LLVM_LIBC_SRC_MATH_REMAINDERF128_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float128 remainderf128(float128 x, float128 y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_REMAINDERF128_H
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index 70d10e6c4e3f..e7e92e5b60dc 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -645,6 +645,71 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ pthread_spin_init
+ SRCS
+ pthread_spin_init.cpp
+ HDRS
+ pthread_spin_init.h
+ DEPENDS
+ libc.include.pthread
+ libc.src.__support.threads.spin_lock
+ libc.src.__support.threads.identifier
+ libc.hdr.errno_macros
+)
+
+add_entrypoint_object(
+ pthread_spin_destroy
+ SRCS
+ pthread_spin_destroy.cpp
+ HDRS
+ pthread_spin_destroy.h
+ DEPENDS
+ libc.include.pthread
+ libc.src.__support.threads.spin_lock
+ libc.src.__support.threads.identifier
+ libc.hdr.errno_macros
+)
+
+add_entrypoint_object(
+ pthread_spin_lock
+ SRCS
+ pthread_spin_lock.cpp
+ HDRS
+ pthread_spin_lock.h
+ DEPENDS
+ libc.include.pthread
+ libc.src.__support.threads.spin_lock
+ libc.src.__support.threads.identifier
+ libc.hdr.errno_macros
+)
+
+add_entrypoint_object(
+ pthread_spin_trylock
+ SRCS
+ pthread_spin_trylock.cpp
+ HDRS
+ pthread_spin_trylock.h
+ DEPENDS
+ libc.include.pthread
+ libc.src.__support.threads.spin_lock
+ libc.src.__support.threads.identifier
+ libc.hdr.errno_macros
+)
+
+add_entrypoint_object(
+ pthread_spin_unlock
+ SRCS
+ pthread_spin_unlock.cpp
+ HDRS
+ pthread_spin_unlock.h
+ DEPENDS
+ libc.include.pthread
+ libc.src.__support.threads.spin_lock
+ libc.src.__support.threads.identifier
+ libc.hdr.errno_macros
+)
+
+add_entrypoint_object(
pthread_once
SRCS
pthread_once.cpp
diff --git a/libc/src/pthread/pthread_spin_destroy.cpp b/libc/src/pthread/pthread_spin_destroy.cpp
new file mode 100644
index 000000000000..7d93dd967afe
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_destroy.cpp
@@ -0,0 +1,47 @@
+//===-- Implementation of pthread_spin_destroy 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/pthread/pthread_spin_destroy.h"
+#include "hdr/errno_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/spin_lock.h"
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) &&
+ alignof(decltype(pthread_spinlock_t::__lockword)) ==
+ alignof(SpinLock),
+ "pthread_spinlock_t::__lockword and SpinLock must be of the same "
+ "size and alignment");
+
+LLVM_LIBC_FUNCTION(int, pthread_spin_destroy,
+ ([[maybe_unused]] pthread_spinlock_t * lock)) {
+ // If an implementation detects that the value specified by the lock argument
+ // to pthread_spin_lock() or pthread_spin_trylock() does not refer to an
+ // initialized spin lock object, it is recommended that the function should
+ // fail and report an [EINVAL] error.
+ if (!lock)
+ return EINVAL;
+ auto spin_lock = reinterpret_cast<SpinLock *>(&lock->__lockword);
+ if (!spin_lock || spin_lock->is_invalid())
+ return EINVAL;
+
+ // If an implementation detects that the value specified by the lock argument
+ // to pthread_spin_destroy() or pthread_spin_init() refers to a locked spin
+ // lock object, or detects that the value specified by the lock argument to
+ // pthread_spin_init() refers to an already initialized spin lock object, it
+ // is recommended that the function should fail and report an [EBUSY] error.
+ if (spin_lock->is_locked())
+ return EBUSY;
+
+ // poison the lock
+ spin_lock->~SpinLock();
+ lock->__owner = 0;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/pthread/pthread_spin_destroy.h b/libc/src/pthread/pthread_spin_destroy.h
new file mode 100644
index 000000000000..2581e9e92f70
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_destroy.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for pthread_spin_destroy 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_PTHREAD_PTHREAD_SPIN_DESTROY_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_DESTROY_H
+
+#include "src/__support/macros/config.h"
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_spin_destroy(pthread_spinlock_t *lock);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_DESTROY_H
diff --git a/libc/src/pthread/pthread_spin_init.cpp b/libc/src/pthread/pthread_spin_init.cpp
new file mode 100644
index 000000000000..54972475288b
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_init.cpp
@@ -0,0 +1,37 @@
+//===-- Implementation of pthread_spin_init 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/pthread/pthread_spin_init.h"
+#include "hdr/errno_macros.h"
+#include "src/__support/CPP/new.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/spin_lock.h"
+#include <pthread.h> // for PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_PRIVATE
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) &&
+ alignof(decltype(pthread_spinlock_t::__lockword)) ==
+ alignof(SpinLock),
+ "pthread_spinlock_t::__lockword and SpinLock must be of the same "
+ "size and alignment");
+
+LLVM_LIBC_FUNCTION(int, pthread_spin_init,
+ (pthread_spinlock_t * lock, [[maybe_unused]] int pshared)) {
+ if (!lock)
+ return EINVAL;
+ if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE)
+ return EINVAL;
+ // The spin lock here is a simple atomic flag, so we don't need to do any
+ // special handling for pshared.
+ ::new (&lock->__lockword) SpinLock();
+ lock->__owner = 0;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/pthread/pthread_spin_init.h b/libc/src/pthread/pthread_spin_init.h
new file mode 100644
index 000000000000..89f7e3adc82e
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_init.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for pthread_spin_init 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_PTHREAD_PTHREAD_SPIN_INIT_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_INIT_H
+
+#include "src/__support/macros/config.h"
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_INIT_H
diff --git a/libc/src/pthread/pthread_spin_lock.cpp b/libc/src/pthread/pthread_spin_lock.cpp
new file mode 100644
index 000000000000..61c8db14e487
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_lock.cpp
@@ -0,0 +1,47 @@
+//===-- Implementation of pthread_spin_lock 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/pthread/pthread_spin_lock.h"
+#include "hdr/errno_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/identifier.h"
+#include "src/__support/threads/spin_lock.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) &&
+ alignof(decltype(pthread_spinlock_t::__lockword)) ==
+ alignof(SpinLock),
+ "pthread_spinlock_t::__lockword and SpinLock must be of the same "
+ "size and alignment");
+
+LLVM_LIBC_FUNCTION(int, pthread_spin_lock, (pthread_spinlock_t * lock)) {
+ // If an implementation detects that the value specified by the lock argument
+ // to pthread_spin_lock() or pthread_spin_trylock() does not refer to an
+ // initialized spin lock object, it is recommended that the function should
+ // fail and report an [EINVAL] error.
+ if (!lock)
+ return EINVAL;
+ auto spin_lock = reinterpret_cast<SpinLock *>(&lock->__lockword);
+ if (spin_lock->is_invalid())
+ return EINVAL;
+
+ pid_t self_tid = internal::gettid();
+ // If an implementation detects that the value specified by the lock argument
+ // to pthread_spin_lock() refers to a spin lock object for which the calling
+ // thread already holds the lock, it is recommended that the function should
+ // fail and report an [EDEADLK] error.
+ if (lock->__owner == self_tid)
+ return EDEADLK;
+
+ spin_lock->lock();
+ lock->__owner = self_tid;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/pthread/pthread_spin_lock.h b/libc/src/pthread/pthread_spin_lock.h
new file mode 100644
index 000000000000..835aa858b25f
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_lock.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for pthread_spin_lock 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_PTHREAD_PTHREAD_SPIN_LOCK_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_LOCK_H
+
+#include "src/__support/macros/config.h"
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_spin_lock(pthread_spinlock_t *lock);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_LOCK_H
diff --git a/libc/src/pthread/pthread_spin_trylock.cpp b/libc/src/pthread/pthread_spin_trylock.cpp
new file mode 100644
index 000000000000..99b0eac41355
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_trylock.cpp
@@ -0,0 +1,41 @@
+//===-- Implementation of pthread_spin_trylock 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/pthread/pthread_spin_trylock.h"
+#include "hdr/errno_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/identifier.h"
+#include "src/__support/threads/spin_lock.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) &&
+ alignof(decltype(pthread_spinlock_t::__lockword)) ==
+ alignof(SpinLock),
+ "pthread_spinlock_t::__lockword and SpinLock must be of the same "
+ "size and alignment");
+
+LLVM_LIBC_FUNCTION(int, pthread_spin_trylock, (pthread_spinlock_t * lock)) {
+ // If an implementation detects that the value specified by the lock argument
+ // to pthread_spin_lock() or pthread_spin_trylock() does not refer to an
+ // initialized spin lock object, it is recommended that the function should
+ // fail and report an [EINVAL] error.
+ if (!lock)
+ return EINVAL;
+ auto spin_lock = reinterpret_cast<SpinLock *>(&lock->__lockword);
+ if (!spin_lock || spin_lock->is_invalid())
+ return EINVAL;
+ // Try to acquire the lock without blocking.
+ if (!spin_lock->try_lock())
+ return EBUSY;
+ // We have acquired the lock. Update the owner field.
+ lock->__owner = internal::gettid();
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/pthread/pthread_spin_trylock.h b/libc/src/pthread/pthread_spin_trylock.h
new file mode 100644
index 000000000000..e175ab8d7cfb
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_trylock.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for pthread_spin_trylock 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_PTHREAD_PTHREAD_SPIN_TRYLOCK_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_TRYLOCK_H
+
+#include "src/__support/macros/config.h"
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_spin_trylock(pthread_spinlock_t *lock);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_TRYLOCK_H
diff --git a/libc/src/pthread/pthread_spin_unlock.cpp b/libc/src/pthread/pthread_spin_unlock.cpp
new file mode 100644
index 000000000000..a02f2b360458
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_unlock.cpp
@@ -0,0 +1,44 @@
+//===-- Implementation of pthread_spin_unlock 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/pthread/pthread_spin_unlock.h"
+#include "hdr/errno_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/identifier.h"
+#include "src/__support/threads/spin_lock.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(pthread_spinlock_t::__lockword) == sizeof(SpinLock) &&
+ alignof(decltype(pthread_spinlock_t::__lockword)) ==
+ alignof(SpinLock),
+ "pthread_spinlock_t::__lockword and SpinLock must be of the same "
+ "size and alignment");
+
+LLVM_LIBC_FUNCTION(int, pthread_spin_unlock, (pthread_spinlock_t * lock)) {
+ // If an implementation detects that the value specified by the lock argument
+ // to pthread_spin_lock() or pthread_spin_trylock() does not refer to an
+ // initialized spin lock object, it is recommended that the function should
+ // fail and report an [EINVAL] error.
+ if (!lock)
+ return EINVAL;
+ auto spin_lock = reinterpret_cast<SpinLock *>(&lock->__lockword);
+ if (!spin_lock || spin_lock->is_invalid())
+ return EINVAL;
+ // If an implementation detects that the value specified by the lock argument
+ // to pthread_spin_unlock() refers to a spin lock object for which the current
+ // thread does not hold the lock, it is recommended that the function should
+ // fail and report an [EPERM] error.
+ if (lock->__owner != internal::gettid())
+ return EPERM;
+ // Release the lock.
+ lock->__owner = 0;
+ spin_lock->unlock();
+ return 0;
+}
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/pthread/pthread_spin_unlock.h b/libc/src/pthread/pthread_spin_unlock.h
new file mode 100644
index 000000000000..4918613f2e6c
--- /dev/null
+++ b/libc/src/pthread/pthread_spin_unlock.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for pthread_spin_unlock 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_PTHREAD_PTHREAD_SPIN_UNLOCK_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_UNLOCK_H
+
+#include "src/__support/macros/config.h"
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_spin_unlock(pthread_spinlock_t *lock);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_SPIN_UNLOCK_H
diff --git a/libc/src/time/mktime.cpp b/libc/src/time/mktime.cpp
index 9ea316d504f5..72cd22912053 100644
--- a/libc/src/time/mktime.cpp
+++ b/libc/src/time/mktime.cpp
@@ -42,12 +42,18 @@ LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) {
return time_utils::out_of_range();
if (tm_out->tm_mday > 19)
return time_utils::out_of_range();
- if (tm_out->tm_hour > 3)
- return time_utils::out_of_range();
- if (tm_out->tm_min > 14)
- return time_utils::out_of_range();
- if (tm_out->tm_sec > 7)
- return time_utils::out_of_range();
+ else if (tm_out->tm_mday == 19) {
+ if (tm_out->tm_hour > 3)
+ return time_utils::out_of_range();
+ else if (tm_out->tm_hour == 3) {
+ if (tm_out->tm_min > 14)
+ return time_utils::out_of_range();
+ else if (tm_out->tm_min == 14) {
+ if (tm_out->tm_sec > 7)
+ return time_utils::out_of_range();
+ }
+ }
+ }
}
// Years are ints. A 32-bit year will fit into a 64-bit time_t.
diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h
index 106870af7299..47f55f7d3891 100644
--- a/libc/src/time/time_utils.h
+++ b/libc/src/time/time_utils.h
@@ -92,7 +92,12 @@ extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm);
// POSIX.1-2017 requires this.
LIBC_INLINE time_t out_of_range() {
+#ifdef EOVERFLOW
+ // For non-POSIX uses of the standard C time functions, where EOVERFLOW is
+ // not defined, it's OK not to set errno at all. The plain C standard doesn't
+ // require it.
libc_errno = EOVERFLOW;
+#endif
return TimeConstants::OUT_OF_RANGE_RETURN_VALUE;
}