diff options
Diffstat (limited to 'libc/src')
-rw-r--r-- | libc/src/__support/OSUtil/linux/auxv.h | 3 | ||||
-rw-r--r-- | libc/src/__support/OSUtil/linux/syscall.h | 14 | ||||
-rw-r--r-- | libc/src/__support/math/CMakeLists.txt | 44 | ||||
-rw-r--r-- | libc/src/__support/math/rsqrtf.h | 71 | ||||
-rw-r--r-- | libc/src/__support/threads/linux/thread.cpp | 2 | ||||
-rw-r--r-- | libc/src/math/CMakeLists.txt | 1 | ||||
-rw-r--r-- | libc/src/math/generic/CMakeLists.txt | 10 | ||||
-rw-r--r-- | libc/src/math/generic/rsqrtf.cpp | 16 | ||||
-rw-r--r-- | libc/src/math/rsqrtf.h | 20 | ||||
-rw-r--r-- | libc/src/sys/mman/linux/mmap.cpp | 2 |
10 files changed, 164 insertions, 19 deletions
diff --git a/libc/src/__support/OSUtil/linux/auxv.h b/libc/src/__support/OSUtil/linux/auxv.h index 894868a..9c07c54 100644 --- a/libc/src/__support/OSUtil/linux/auxv.h +++ b/libc/src/__support/OSUtil/linux/auxv.h @@ -16,6 +16,7 @@ #include <linux/auxvec.h> // For AT_ macros #include <linux/mman.h> // For mmap flags +#include <linux/param.h> // For EXEC_PAGESIZE #include <linux/prctl.h> // For prctl #include <sys/syscall.h> // For syscall numbers @@ -90,7 +91,7 @@ LIBC_INLINE void Vector::fallback_initialize_unsync() { PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); // We do not proceed if mmap fails. - if (mmap_ret <= 0) + if (!linux_utils::is_valid_mmap(mmap_ret)) return; // Initialize the auxv array with AT_NULL entries. diff --git a/libc/src/__support/OSUtil/linux/syscall.h b/libc/src/__support/OSUtil/linux/syscall.h index 24e0fca..0e25618 100644 --- a/libc/src/__support/OSUtil/linux/syscall.h +++ b/libc/src/__support/OSUtil/linux/syscall.h @@ -12,6 +12,7 @@ #include "src/__support/CPP/bit.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" +#include "src/__support/macros/optimization.h" #include "src/__support/macros/properties/architectures.h" #ifdef LIBC_TARGET_ARCH_IS_X86_32 @@ -34,6 +35,19 @@ LIBC_INLINE R syscall_impl(long __number, Ts... ts) { return cpp::bit_or_static_cast<R>(syscall_impl(__number, (long)ts...)); } +// Linux-specific function for checking +namespace linux_utils { +LIBC_INLINE_VAR constexpr unsigned long MAX_ERRNO = 4095; +// Ideally, this should be defined using PAGE_OFFSET +// However, that is a configurable parameter. We mimic kernel's behavior +// by checking against MAX_ERRNO. +template <typename PointerLike> +LIBC_INLINE constexpr bool is_valid_mmap(PointerLike ptr) { + long addr = cpp::bit_cast<long>(ptr); + return LIBC_LIKELY(addr > 0 || addr < -static_cast<long>(MAX_ERRNO)); +} +} // namespace linux_utils + } // namespace LIBC_NAMESPACE_DECL #endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_H diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index 203ebb4..61253de 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -109,22 +109,6 @@ add_header_library( libc.src.__support.macros.properties.types ) - -add_header_library( - rsqrtf16 - HDRS - rsqrtf16.h - DEPENDS - libc.src.__support.FPUtil.cast - libc.src.__support.FPUtil.fenv_impl - libc.src.__support.FPUtil.fp_bits - libc.src.__support.FPUtil.multiply_add - libc.src.__support.FPUtil.polyeval - libc.src.__support.FPUtil.manipulation_functions - libc.src.__support.macros.optimization - libc.src.__support.macros.properties.types -) - add_header_library( asin_utils HDRS @@ -867,6 +851,34 @@ add_header_library( ) add_header_library( + rsqrtf + HDRS + rsqrtf.h + DEPENDS + 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.sqrt + libc.src.__support.macros.optimization +) + +add_header_library( + rsqrtf16 + HDRS + rsqrtf16.h + DEPENDS + 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.sqrt + libc.src.__support.macros.optimization +) + +add_header_library( sincos_eval HDRS sincos_eval.h diff --git a/libc/src/__support/math/rsqrtf.h b/libc/src/__support/math/rsqrtf.h new file mode 100644 index 0000000..5da1e73 --- /dev/null +++ b/libc/src/__support/math/rsqrtf.h @@ -0,0 +1,71 @@ +//===-- Implementation header for rsqrtf ------------------------*- 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_MATH_RSQRTF_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF_H + +#include "hdr/errno_macros.h" +#include "hdr/fenv_macros.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/cast.h" +#include "src/__support/FPUtil/sqrt.h" +#include "src/__support/macros/optimization.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE static constexpr float rsqrtf(float x) { + using FPBits = fputil::FPBits<float>; + FPBits xbits(x); + + uint32_t x_u = xbits.uintval(); + uint32_t x_abs = x_u & 0x7fff'ffffU; + + constexpr uint32_t INF_BITS = FPBits::inf().uintval(); + + // x is 0, inf/nan, or negative. + if (LIBC_UNLIKELY(x_u == 0 || x_u >= INF_BITS)) { + // x is NaN + if (x_abs > INF_BITS) { + if (xbits.is_signaling_nan()) { + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + return x; + } + + // |x| = 0 + if (x_abs == 0) { + fputil::raise_except_if_required(FE_DIVBYZERO); + fputil::set_errno_if_required(ERANGE); + return FPBits::inf(xbits.sign()).get_val(); + } + + // -inf <= x < 0 + if (x_u > 0x7fff'ffffU) { + fputil::raise_except_if_required(FE_INVALID); + fputil::set_errno_if_required(EDOM); + return FPBits::quiet_nan().get_val(); + } + + // x = +inf => rsqrt(x) = 0 + return FPBits::zero(xbits.sign()).get_val(); + } + + // TODO: add float based approximation when + // LIBC_TARGET_CPU_HAS_FPU_DOUBLE is not defined + double result = 1.0 / fputil::sqrt<double>(fputil::cast<double>(x)); + + return fputil::cast<float>(result); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF_H diff --git a/libc/src/__support/threads/linux/thread.cpp b/libc/src/__support/threads/linux/thread.cpp index d9e479ee..4f1e911 100644 --- a/libc/src/__support/threads/linux/thread.cpp +++ b/libc/src/__support/threads/linux/thread.cpp @@ -100,7 +100,7 @@ LIBC_INLINE ErrorOr<void *> alloc_stack(size_t stacksize, size_t guardsize) { -1, // Not backed by any file 0 // No offset ); - if (mmap_result < 0 && (uintptr_t(mmap_result) >= UINTPTR_MAX - size)) + if (!linux_utils::is_valid_mmap(mmap_result)) return Error{int(-mmap_result)}; if (guardsize) { diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 3c7e99f..e37e22f 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -517,6 +517,7 @@ add_math_entrypoint_object(roundevenf16) add_math_entrypoint_object(roundevenf128) add_math_entrypoint_object(roundevenbf16) +add_math_entrypoint_object(rsqrtf) add_math_entrypoint_object(rsqrtf16) add_math_entrypoint_object(scalbln) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 5738fe8..55f4aaf 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -989,6 +989,16 @@ add_entrypoint_object( ) add_entrypoint_object( + rsqrtf + SRCS + rsqrtf.cpp + HDRS + ../rsqrtf.h + DEPENDS + libc.src.__support.math.rsqrtf +) + +add_entrypoint_object( rsqrtf16 SRCS rsqrtf16.cpp diff --git a/libc/src/math/generic/rsqrtf.cpp b/libc/src/math/generic/rsqrtf.cpp new file mode 100644 index 0000000..6a38fa7 --- /dev/null +++ b/libc/src/math/generic/rsqrtf.cpp @@ -0,0 +1,16 @@ +//===-- Single-precision rsqrt 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/rsqrtf.h" +#include "src/__support/math/rsqrtf.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(float, rsqrtf, (float x)) { return math::rsqrtf(x); } + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/rsqrtf.h b/libc/src/math/rsqrtf.h new file mode 100644 index 0000000..bc55904 --- /dev/null +++ b/libc/src/math/rsqrtf.h @@ -0,0 +1,20 @@ +//===-- Implementation header for rsqrtf ------------------------*- 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_RSQRTF_H +#define LLVM_LIBC_SRC_MATH_RSQRTF_H + +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +float rsqrtf(float x); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_MATH_RSQRTF_H diff --git a/libc/src/sys/mman/linux/mmap.cpp b/libc/src/sys/mman/linux/mmap.cpp index 33f9fe8..76a6611 100644 --- a/libc/src/sys/mman/linux/mmap.cpp +++ b/libc/src/sys/mman/linux/mmap.cpp @@ -56,7 +56,7 @@ LLVM_LIBC_FUNCTION(void *, mmap, // However, since a valid return address cannot be within the last page, a // return value corresponding to a location in the last page is an error // value. - if (ret < 0 && ret > -EXEC_PAGESIZE) { + if (!linux_utils::is_valid_mmap(ret)) { libc_errno = static_cast<int>(-ret); return MAP_FAILED; } |