diff options
Diffstat (limited to 'libc')
57 files changed, 1456 insertions, 895 deletions
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index 2478fde..d85c393 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -108,6 +108,10 @@ function(_get_compile_options_from_config output_var) list(APPEND config_options "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}") endif() + if(LIBC_CONF_THREAD_MODE) + list(APPEND config_options "-DLIBC_THREAD_MODE=${LIBC_CONF_THREAD_MODE}") + endif() + set(${output_var} ${config_options} PARENT_SCOPE) endfunction(_get_compile_options_from_config) diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt index e766eb2..e24e2b9 100644 --- a/libc/config/baremetal/aarch64/entrypoints.txt +++ b/libc/config/baremetal/aarch64/entrypoints.txt @@ -218,6 +218,7 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.stdlib.abort libc.src.stdlib.abs libc.src.stdlib.aligned_alloc + libc.src.stdlib.atexit libc.src.stdlib.atof libc.src.stdlib.atoi libc.src.stdlib.atol diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt index 336b1e6..44e9c3e 100644 --- a/libc/config/baremetal/arm/entrypoints.txt +++ b/libc/config/baremetal/arm/entrypoints.txt @@ -218,6 +218,7 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.stdlib.abort libc.src.stdlib.abs libc.src.stdlib.aligned_alloc + libc.src.stdlib.atexit libc.src.stdlib.atof libc.src.stdlib.atoi libc.src.stdlib.atol diff --git a/libc/config/baremetal/config.json b/libc/config/baremetal/config.json index 105e417..f01e508 100644 --- a/libc/config/baremetal/config.json +++ b/libc/config/baremetal/config.json @@ -4,6 +4,11 @@ "value": "LIBC_ERRNO_MODE_EXTERNAL" } }, + "threads": { + "LIBC_CONF_THREAD_MODE": { + "value": "LIBC_THREAD_MODE_SINGLE" + } + }, "printf": { "LIBC_CONF_PRINTF_DISABLE_FIXED_POINT": { "value": true diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt index e92ec87..29cf322a 100644 --- a/libc/config/baremetal/riscv/entrypoints.txt +++ b/libc/config/baremetal/riscv/entrypoints.txt @@ -218,6 +218,7 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.stdlib.abort libc.src.stdlib.abs libc.src.stdlib.aligned_alloc + libc.src.stdlib.atexit libc.src.stdlib.atof libc.src.stdlib.atoi libc.src.stdlib.atol diff --git a/libc/config/config.json b/libc/config/config.json index d53b293..1b05469 100644 --- a/libc/config/config.json +++ b/libc/config/config.json @@ -5,6 +5,12 @@ "doc": "The implementation used for errno, acceptable values are LIBC_ERRNO_MODE_DEFAULT, LIBC_ERRNO_MODE_UNDEFINED, LIBC_ERRNO_MODE_THREAD_LOCAL, LIBC_ERRNO_MODE_SHARED, LIBC_ERRNO_MODE_EXTERNAL, LIBC_ERRNO_MODE_SYSTEM, and LIBC_ERRNO_MODE_SYSTEM_INLINE." } }, + "threads": { + "LIBC_CONF_THREAD_MODE": { + "value": "LIBC_THREAD_MODE_PLATFORM", + "doc": "The implementation used for Mutex, acceptable values are LIBC_THREAD_MODE_PLATFORM, LIBC_THREAD_MODE_SINGLE, and LIBC_THREAD_MODE_EXTERNAL." + } + }, "printf": { "LIBC_CONF_PRINTF_DISABLE_FLOAT": { "value": false, diff --git a/libc/config/gpu/amdgpu/config.json b/libc/config/gpu/amdgpu/config.json index 30ae10e..fa179b8 100644 --- a/libc/config/gpu/amdgpu/config.json +++ b/libc/config/gpu/amdgpu/config.json @@ -4,6 +4,11 @@ "value": "LIBC_ERRNO_MODE_SHARED" } }, + "threads": { + "LIBC_CONF_THREAD_MODE": { + "value": "LIBC_THREAD_MODE_SINGLE" + } + }, "printf": { "LIBC_CONF_PRINTF_DISABLE_FLOAT": { "value": true diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt index e39819d..291a2d0 100644 --- a/libc/config/gpu/amdgpu/entrypoints.txt +++ b/libc/config/gpu/amdgpu/entrypoints.txt @@ -489,6 +489,7 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.tan libc.src.math.tanf libc.src.math.tanhf + libc.src.math.tanpif libc.src.math.tgamma libc.src.math.tgammaf libc.src.math.totalorder diff --git a/libc/config/gpu/nvptx/config.json b/libc/config/gpu/nvptx/config.json index 30ae10e..fa179b8 100644 --- a/libc/config/gpu/nvptx/config.json +++ b/libc/config/gpu/nvptx/config.json @@ -4,6 +4,11 @@ "value": "LIBC_ERRNO_MODE_SHARED" } }, + "threads": { + "LIBC_CONF_THREAD_MODE": { + "value": "LIBC_THREAD_MODE_SINGLE" + } + }, "printf": { "LIBC_CONF_PRINTF_DISABLE_FLOAT": { "value": true diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt index 26e3b15..55b27e6 100644 --- a/libc/config/gpu/nvptx/entrypoints.txt +++ b/libc/config/gpu/nvptx/entrypoints.txt @@ -490,6 +490,7 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.tan libc.src.math.tanf libc.src.math.tanhf + libc.src.math.tanpif libc.src.math.tgamma libc.src.math.tgammaf libc.src.math.totalorder diff --git a/libc/docs/configure.rst b/libc/docs/configure.rst index 1094122..95c51b8 100644 --- a/libc/docs/configure.rst +++ b/libc/docs/configure.rst @@ -60,5 +60,7 @@ to learn about the defaults for your platform and target. * **"string" options** - ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled. - ``LIBC_CONF_STRING_UNSAFE_WIDE_READ``: Read more than a byte at a time to perform byte-string operations like strlen. +* **"threads" options** + - ``LIBC_CONF_THREAD_MODE``: The implementation used for Mutex, acceptable values are LIBC_THREAD_MODE_PLATFORM, LIBC_THREAD_MODE_SINGLE, and LIBC_THREAD_MODE_EXTERNAL. * **"time" options** - ``LIBC_CONF_TIME_64BIT``: Force the size of time_t to 64 bits, even on platforms where compatibility considerations would otherwise make it 32-bit. diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt index 1c1f242..02fe1ad 100644 --- a/libc/hdr/types/CMakeLists.txt +++ b/libc/hdr/types/CMakeLists.txt @@ -432,6 +432,7 @@ add_proxy_header_library( cpu_set_t.h FULL_BUILD_DEPENDS libc.include.llvm-libc-types.cpu_set_t + libc.include.sched ) add_proxy_header_library( @@ -451,3 +452,12 @@ add_proxy_header_library( libc.include.llvm-libc-types.ACTION libc.include.search ) + +add_proxy_header_library( + struct_sched_param + HDRS + struct_sched_param.h + FULL_BUILD_DEPENDS + libc.include.llvm-libc-types.struct_sched_param + libc.include.sched +) diff --git a/libc/hdr/types/struct_sched_param.h b/libc/hdr/types/struct_sched_param.h new file mode 100644 index 0000000..b1bdcf9 --- /dev/null +++ b/libc/hdr/types/struct_sched_param.h @@ -0,0 +1,22 @@ +//===-- Proxy for struct sched_param --------------------------------------===// +// +// 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_HDR_TYPES_STRUCT_SCHED_PARAM_H +#define LLVM_LIBC_HDR_TYPES_STRUCT_SCHED_PARAM_H + +#ifdef LIBC_FULL_BUILD + +#include "include/llvm-libc-types/struct_sched_param.h" + +#else // Overlay mode + +#include <sched.h> + +#endif // LLVM_LIBC_FULL_BUILD + +#endif // LLVM_LIBC_HDR_TYPES_STRUCT_SCHED_PARAM_H diff --git a/libc/include/math.yaml b/libc/include/math.yaml index 007be23..e8ac7ee 100644 --- a/libc/include/math.yaml +++ b/libc/include/math.yaml @@ -283,6 +283,12 @@ functions: return_type: float arguments: - type: float + - name: cospif + standards: + - stdc + return_type: float + arguments: + - type: float - name: cospif16 standards: - stdc @@ -2453,6 +2459,12 @@ functions: arguments: - type: _Float16 guard: LIBC_TYPES_HAS_FLOAT16 + - name: sinpif + standards: + - stdc + return_type: float + arguments: + - type: float - name: sinpif16 standards: - stdc diff --git a/libc/shared/math.h b/libc/shared/math.h index 0605d91..7fb736b 100644 --- a/libc/shared/math.h +++ b/libc/shared/math.h @@ -24,8 +24,12 @@ #include "math/asinhf16.h" #include "math/atan.h" #include "math/atan2.h" +#include "math/atan2f.h" +#include "math/atan2f128.h" #include "math/atanf.h" #include "math/atanf16.h" +#include "math/atanhf.h" +#include "math/atanhf16.h" #include "math/erff.h" #include "math/exp.h" #include "math/exp10.h" diff --git a/libc/shared/math/atan2f.h b/libc/shared/math/atan2f.h new file mode 100644 index 0000000..2de09d2 --- /dev/null +++ b/libc/shared/math/atan2f.h @@ -0,0 +1,23 @@ +//===-- Shared atan2f 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_SHARED_MATH_ATAN2F_H +#define LLVM_LIBC_SHARED_MATH_ATAN2F_H + +#include "shared/libc_common.h" +#include "src/__support/math/atan2f.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::atan2f; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_ATAN2F_H diff --git a/libc/shared/math/atan2f128.h b/libc/shared/math/atan2f128.h new file mode 100644 index 0000000..d7aee40c --- /dev/null +++ b/libc/shared/math/atan2f128.h @@ -0,0 +1,29 @@ +//===-- Shared atan2f128 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_SHARED_MATH_ATAN2F128_H +#define LLVM_LIBC_SHARED_MATH_ATAN2F128_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/math/atan2f128.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::atan2f128; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_MATH_ATAN2F128_H diff --git a/libc/shared/math/atanhf.h b/libc/shared/math/atanhf.h new file mode 100644 index 0000000..763fb3e --- /dev/null +++ b/libc/shared/math/atanhf.h @@ -0,0 +1,23 @@ +//===-- Shared atanhf 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_SHARED_MATH_ATANHF_H +#define LLVM_LIBC_SHARED_MATH_ATANHF_H + +#include "shared/libc_common.h" +#include "src/__support/math/atanhf.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::atanhf; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_ATANHF_H diff --git a/libc/shared/math/atanhf16.h b/libc/shared/math/atanhf16.h new file mode 100644 index 0000000..b7b5d77 --- /dev/null +++ b/libc/shared/math/atanhf16.h @@ -0,0 +1,28 @@ +//===-- Shared atanhf16 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_SHARED_MATH_ATANHF16_H +#define LLVM_LIBC_SHARED_MATH_ATANHF16_H + +#include "shared/libc_common.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "src/__support/math/atanhf16.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::atanhf16; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SHARED_MATH_ATANHF16_H diff --git a/libc/src/__support/GPU/allocator.cpp b/libc/src/__support/GPU/allocator.cpp index bd0a55c..250bebd 100644 --- a/libc/src/__support/GPU/allocator.cpp +++ b/libc/src/__support/GPU/allocator.cpp @@ -156,7 +156,7 @@ static inline constexpr uint32_t get_start_index(uint32_t chunk_size) { // Returns the id of the lane below this one that acts as its leader. static inline uint32_t get_leader_id(uint64_t ballot, uint32_t id) { - uint64_t mask = id < BITS_IN_DWORD ? ~0ull << (id + 1) : 0; + uint64_t mask = id < BITS_IN_DWORD - 1 ? ~0ull << (id + 1) : 0; return BITS_IN_DWORD - cpp::countl_zero(ballot & ~mask) - 1; } diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index bbb07b6..9631ab5 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -214,6 +214,38 @@ add_header_library( ) add_header_library( + atan2f + HDRS + atan2f_float.h + atan2f.h + DEPENDS + .inv_trigf_utils + libc.src.__support.FPUtil.double_double + 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.macros.config + libc.src.__support.macros.optimization +) + +add_header_library( + atan2f128 + HDRS + atan2f128.h + DEPENDS + .atan_utils + libc.src.__support.integer_literals + libc.src.__support.uint128 + libc.src.__support.FPUtil.dyadic_float + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.multiply_add + libc.src.__support.FPUtil.nearest_integer + libc.src.__support.macros.optimization +) + +add_header_library( atanf HDRS atanf.h @@ -244,6 +276,32 @@ add_header_library( ) add_header_library( + atanhf + HDRS + atanhf.h + DEPENDS + .acoshf_utils + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.macros.optimization +) + +add_header_library( + atanhf16 + HDRS + atanhf16.h + DEPENDS + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.polyeval + libc.src.__support.FPUtil.cast + libc.src.__support.FPUtil.except_value_utils + libc.src.__support.FPUtil.multiply_add + libc.src.__support.macros.config + libc.src.__support.macros.optimization +) + +add_header_library( asinf HDRS asinf.h diff --git a/libc/src/__support/math/asin_utils.h b/libc/src/__support/math/asin_utils.h index e0c9096..efe779c 100644 --- a/libc/src/__support/math/asin_utils.h +++ b/libc/src/__support/math/asin_utils.h @@ -45,7 +45,7 @@ static constexpr double ASIN_COEFFS[12] = { 0x1.2b5993bda1d9bp-6, -0x1.806aff270bf25p-7, 0x1.02614e5ed3936p-5, }; -LIBC_INLINE static constexpr double asin_eval(double u) { +LIBC_INLINE double asin_eval(double u) { double u2 = u * u; double c0 = fputil::multiply_add(u, ASIN_COEFFS[1], ASIN_COEFFS[0]); double c1 = fputil::multiply_add(u, ASIN_COEFFS[3], ASIN_COEFFS[2]); diff --git a/libc/src/__support/math/atan2f.h b/libc/src/__support/math/atan2f.h new file mode 100644 index 0000000..e3b1932 --- /dev/null +++ b/libc/src/__support/math/atan2f.h @@ -0,0 +1,351 @@ +//===-- Implementation header for atan2f ------------------------*- 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_ATAN2F_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F_H + +#include "inv_trigf_utils.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/PolyEval.h" +#include "src/__support/FPUtil/double_double.h" +#include "src/__support/FPUtil/multiply_add.h" +#include "src/__support/FPUtil/nearest_integer.h" +#include "src/__support/macros/config.h" +#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY + +#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) && \ + defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT) + +// We use float-float implementation to reduce size. +#include "atan2f_float.h" + +#else + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +namespace atan2f_internal { + +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +// Look up tables for accurate pass: + +// atan(i/16) with i = 0..16, generated by Sollya with: +// > for i from 0 to 16 do { +// a = round(atan(i/16), D, RN); +// b = round(atan(i/16) - a, D, RN); +// print("{", b, ",", a, "},"); +// }; +static constexpr fputil::DoubleDouble ATAN_I[17] = { + {0.0, 0.0}, + {-0x1.c934d86d23f1dp-60, 0x1.ff55bb72cfdeap-5}, + {-0x1.cd37686760c17p-59, 0x1.fd5ba9aac2f6ep-4}, + {0x1.347b0b4f881cap-58, 0x1.7b97b4bce5b02p-3}, + {0x1.8ab6e3cf7afbdp-57, 0x1.f5b75f92c80ddp-3}, + {-0x1.963a544b672d8p-57, 0x1.362773707ebccp-2}, + {-0x1.c63aae6f6e918p-56, 0x1.6f61941e4def1p-2}, + {-0x1.24dec1b50b7ffp-56, 0x1.a64eec3cc23fdp-2}, + {0x1.a2b7f222f65e2p-56, 0x1.dac670561bb4fp-2}, + {-0x1.d5b495f6349e6p-56, 0x1.0657e94db30dp-1}, + {-0x1.928df287a668fp-58, 0x1.1e00babdefeb4p-1}, + {0x1.1021137c71102p-55, 0x1.345f01cce37bbp-1}, + {0x1.2419a87f2a458p-56, 0x1.4978fa3269ee1p-1}, + {0x1.0028e4bc5e7cap-57, 0x1.5d58987169b18p-1}, + {-0x1.8c34d25aadef6p-56, 0x1.700a7c5784634p-1}, + {-0x1.bf76229d3b917p-56, 0x1.819d0b7158a4dp-1}, + {0x1.1a62633145c07p-55, 0x1.921fb54442d18p-1}, +}; + +// Taylor polynomial, generated by Sollya with: +// > for i from 0 to 8 do { +// j = (-1)^(i + 1)/(2*i + 1); +// a = round(j, D, RN); +// b = round(j - a, D, RN); +// print("{", b, ",", a, "},"); +// }; +static constexpr fputil::DoubleDouble COEFFS[9] = { + {0.0, 1.0}, // 1 + {-0x1.5555555555555p-56, -0x1.5555555555555p-2}, // -1/3 + {-0x1.999999999999ap-57, 0x1.999999999999ap-3}, // 1/5 + {-0x1.2492492492492p-57, -0x1.2492492492492p-3}, // -1/7 + {0x1.c71c71c71c71cp-58, 0x1.c71c71c71c71cp-4}, // 1/9 + {0x1.745d1745d1746p-59, -0x1.745d1745d1746p-4}, // -1/11 + {-0x1.3b13b13b13b14p-58, 0x1.3b13b13b13b14p-4}, // 1/13 + {-0x1.1111111111111p-60, -0x1.1111111111111p-4}, // -1/15 + {0x1.e1e1e1e1e1e1ep-61, 0x1.e1e1e1e1e1e1ep-5}, // 1/17 +}; + +// Veltkamp's splitting of a double precision into hi + lo, where the hi part is +// slightly smaller than an even split, so that the product of +// hi * (s1 * k + s2) is exact, +// where: +// s1, s2 are single precsion, +// 1/16 <= s1/s2 <= 1 +// 1/16 <= k <= 1 is an integer. +// So the maximal precision of (s1 * k + s2) is: +// prec(s1 * k + s2) = 2 + log2(msb(s2)) - log2(lsb(k_d * s1)) +// = 2 + log2(msb(s1)) + 4 - log2(lsb(k_d)) - log2(lsb(s1)) +// = 2 + log2(lsb(s1)) + 23 + 4 - (-4) - log2(lsb(s1)) +// = 33. +// Thus, the Veltkamp splitting constant is C = 2^33 + 1. +// This is used when FMA instruction is not available. +[[maybe_unused]] LIBC_INLINE static constexpr fputil::DoubleDouble +split_d(double a) { + fputil::DoubleDouble r{0.0, 0.0}; + constexpr double C = 0x1.0p33 + 1.0; + double t1 = C * a; + double t2 = a - t1; + r.hi = t1 + t2; + r.lo = a - r.hi; + return r; +} + +// Compute atan( num_d / den_d ) in double-double precision. +// num_d = min(|x|, |y|) +// den_d = max(|x|, |y|) +// q_d = num_d / den_d +// idx, k_d = round( 2^4 * num_d / den_d ) +// final_sign = sign of the final result +// const_term = the constant term in the final expression. +LIBC_INLINE static float +atan2f_double_double(double num_d, double den_d, double q_d, int idx, + double k_d, double final_sign, + const fputil::DoubleDouble &const_term) { + fputil::DoubleDouble q; + double num_r = 0, den_r = 0; + + if (idx != 0) { + // The following range reduction is accurate even without fma for + // 1/16 <= n/d <= 1. + // atan(n/d) - atan(idx/16) = atan((n/d - idx/16) / (1 + (n/d) * (idx/16))) + // = atan((n - d*(idx/16)) / (d + n*idx/16)) + k_d *= 0x1.0p-4; + num_r = fputil::multiply_add(k_d, -den_d, num_d); // Exact + den_r = fputil::multiply_add(k_d, num_d, den_d); // Exact + q.hi = num_r / den_r; + } else { + // For 0 < n/d < 1/16, we just need to calculate the lower part of their + // quotient. + q.hi = q_d; + num_r = num_d; + den_r = den_d; + } +#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE + q.lo = fputil::multiply_add(q.hi, -den_r, num_r) / den_r; +#else + // Compute `(num_r - q.hi * den_r) / den_r` accurately without FMA + // instructions. + fputil::DoubleDouble q_hi_dd = split_d(q.hi); + double t1 = fputil::multiply_add(q_hi_dd.hi, -den_r, num_r); // Exact + double t2 = fputil::multiply_add(q_hi_dd.lo, -den_r, t1); + q.lo = t2 / den_r; +#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE + + // Taylor polynomial, evaluating using Horner's scheme: + // P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9 - x^11/11 + x^13/13 - x^15/15 + // + x^17/17 + // = x*(1 + x^2*(-1/3 + x^2*(1/5 + x^2*(-1/7 + x^2*(1/9 + x^2* + // *(-1/11 + x^2*(1/13 + x^2*(-1/15 + x^2 * 1/17)))))))) + fputil::DoubleDouble q2 = fputil::quick_mult(q, q); + fputil::DoubleDouble p_dd = + fputil::polyeval(q2, COEFFS[0], COEFFS[1], COEFFS[2], COEFFS[3], + COEFFS[4], COEFFS[5], COEFFS[6], COEFFS[7], COEFFS[8]); + fputil::DoubleDouble r_dd = + fputil::add(const_term, fputil::multiply_add(q, p_dd, ATAN_I[idx])); + r_dd.hi *= final_sign; + r_dd.lo *= final_sign; + + // Make sure the sum is normalized: + fputil::DoubleDouble rr = fputil::exact_add(r_dd.hi, r_dd.lo); + // Round to odd. + uint64_t rr_bits = cpp::bit_cast<uint64_t>(rr.hi); + if (LIBC_UNLIKELY(((rr_bits & 0xfff'ffff) == 0) && (rr.lo != 0.0))) { + Sign hi_sign = fputil::FPBits<double>(rr.hi).sign(); + Sign lo_sign = fputil::FPBits<double>(rr.lo).sign(); + if (hi_sign == lo_sign) { + ++rr_bits; + } else if ((rr_bits & fputil::FPBits<double>::FRACTION_MASK) > 0) { + --rr_bits; + } + } + + return static_cast<float>(cpp::bit_cast<double>(rr_bits)); +} + +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +} // namespace atan2f_internal + +// There are several range reduction steps we can take for atan2(y, x) as +// follow: + +// * Range reduction 1: signness +// atan2(y, x) will return a number between -PI and PI representing the angle +// forming by the 0x axis and the vector (x, y) on the 0xy-plane. +// In particular, we have that: +// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant) +// = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant) +// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant) +// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant) +// Since atan function is odd, we can use the formula: +// atan(-u) = -atan(u) +// to adjust the above conditions a bit further: +// atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant) +// = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant) +// = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant) +// = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant) +// Which can be simplified to: +// atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0 +// = sign(y) * (pi - atan( |y|/|x| )) if x < 0 + +// * Range reduction 2: reciprocal +// Now that the argument inside atan is positive, we can use the formula: +// atan(1/x) = pi/2 - atan(x) +// to make the argument inside atan <= 1 as follow: +// atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x +// = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y| +// = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x +// = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y| + +// * Range reduction 3: look up table. +// After the previous two range reduction steps, we reduce the problem to +// compute atan(u) with 0 <= u <= 1, or to be precise: +// atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|). +// An accurate polynomial approximation for the whole [0, 1] input range will +// require a very large degree. To make it more efficient, we reduce the input +// range further by finding an integer idx such that: +// | n/d - idx/16 | <= 1/32. +// In particular, +// idx := 2^-4 * round(2^4 * n/d) +// Then for the fast pass, we find a polynomial approximation for: +// atan( n/d ) ~ atan( idx/16 ) + (n/d - idx/16) * Q(n/d - idx/16) +// For the accurate pass, we use the addition formula: +// atan( n/d ) - atan( idx/16 ) = atan( (n/d - idx/16)/(1 + (n*idx)/(16*d)) ) +// = atan( (n - d * idx/16)/(d + n * idx/16) ) +// And finally we use Taylor polynomial to compute the RHS in the accurate pass: +// atan(u) ~ P(u) = u - u^3/3 + u^5/5 - u^7/7 + u^9/9 - u^11/11 + u^13/13 - +// - u^15/15 + u^17/17 +// It's error in double-double precision is estimated in Sollya to be: +// > P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9 - x^11/11 + x^13/13 - x^15/15 +// + x^17/17; +// > dirtyinfnorm(atan(x) - P, [-2^-5, 2^-5]); +// 0x1.aec6f...p-100 +// which is about rounding errors of double-double (2^-104). + +LIBC_INLINE static constexpr float atan2f(float y, float x) { + using namespace atan2f_internal; + using namespace inv_trigf_utils_internal; + using FPBits = typename fputil::FPBits<float>; + constexpr double IS_NEG[2] = {1.0, -1.0}; + constexpr double PI = 0x1.921fb54442d18p1; + constexpr double PI_LO = 0x1.1a62633145c07p-53; + constexpr double PI_OVER_4 = 0x1.921fb54442d18p-1; + constexpr double PI_OVER_2 = 0x1.921fb54442d18p0; + constexpr double THREE_PI_OVER_4 = 0x1.2d97c7f3321d2p+1; + // Adjustment for constant term: + // CONST_ADJ[x_sign][y_sign][recip] + constexpr fputil::DoubleDouble CONST_ADJ[2][2][2] = { + {{{0.0, 0.0}, {-PI_LO / 2, -PI_OVER_2}}, + {{-0.0, -0.0}, {-PI_LO / 2, -PI_OVER_2}}}, + {{{-PI_LO, -PI}, {PI_LO / 2, PI_OVER_2}}, + {{-PI_LO, -PI}, {PI_LO / 2, PI_OVER_2}}}}; + + FPBits x_bits(x), y_bits(y); + bool x_sign = x_bits.sign().is_neg(); + bool y_sign = y_bits.sign().is_neg(); + x_bits.set_sign(Sign::POS); + y_bits.set_sign(Sign::POS); + uint32_t x_abs = x_bits.uintval(); + uint32_t y_abs = y_bits.uintval(); + uint32_t max_abs = x_abs > y_abs ? x_abs : y_abs; + uint32_t min_abs = x_abs <= y_abs ? x_abs : y_abs; + float num_f = FPBits(min_abs).get_val(); + float den_f = FPBits(max_abs).get_val(); + double num_d = static_cast<double>(num_f); + double den_d = static_cast<double>(den_f); + + if (LIBC_UNLIKELY(max_abs >= 0x7f80'0000U || num_d == 0.0)) { + if (x_bits.is_nan() || y_bits.is_nan()) { + if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan()) + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + double x_d = static_cast<double>(x); + double y_d = static_cast<double>(y); + size_t x_except = (x_d == 0.0) ? 0 : (x_abs == 0x7f80'0000 ? 2 : 1); + size_t y_except = (y_d == 0.0) ? 0 : (y_abs == 0x7f80'0000 ? 2 : 1); + + // Exceptional cases: + // EXCEPT[y_except][x_except][x_is_neg] + // with x_except & y_except: + // 0: zero + // 1: finite, non-zero + // 2: infinity + constexpr double EXCEPTS[3][3][2] = { + {{0.0, PI}, {0.0, PI}, {0.0, PI}}, + {{PI_OVER_2, PI_OVER_2}, {0.0, 0.0}, {0.0, PI}}, + {{PI_OVER_2, PI_OVER_2}, + {PI_OVER_2, PI_OVER_2}, + {PI_OVER_4, THREE_PI_OVER_4}}, + }; + + double r = IS_NEG[y_sign] * EXCEPTS[y_except][x_except][x_sign]; + + return static_cast<float>(r); + } + + bool recip = x_abs < y_abs; + double final_sign = IS_NEG[(x_sign != y_sign) != recip]; + fputil::DoubleDouble const_term = CONST_ADJ[x_sign][y_sign][recip]; + double q_d = num_d / den_d; + + double k_d = fputil::nearest_integer(q_d * 0x1.0p4); + int idx = static_cast<int>(k_d); + double r = 0.0; + +#ifdef LIBC_MATH_HAS_SMALL_TABLES + double p = atan_eval_no_table(num_d, den_d, k_d * 0x1.0p-4); + r = final_sign * (p + (const_term.hi + ATAN_K_OVER_16[idx])); +#else + q_d = fputil::multiply_add(k_d, -0x1.0p-4, q_d); + + double p = atan_eval(q_d, idx); + r = final_sign * + fputil::multiply_add(q_d, p, const_term.hi + ATAN_COEFFS[idx][0]); +#endif // LIBC_MATH_HAS_SMALL_TABLES + +#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS + return static_cast<float>(r); +#else + constexpr uint32_t LOWER_ERR = 4; + // Mask sticky bits in double precision before rounding to single precision. + constexpr uint32_t MASK = + mask_trailing_ones<uint32_t, fputil::FPBits<double>::SIG_LEN - + FPBits::SIG_LEN - 1>(); + constexpr uint32_t UPPER_ERR = MASK - LOWER_ERR; + + uint32_t r_bits = static_cast<uint32_t>(cpp::bit_cast<uint64_t>(r)) & MASK; + + // Ziv's rounding test. + if (LIBC_LIKELY(r_bits > LOWER_ERR && r_bits < UPPER_ERR)) + return static_cast<float>(r); + + return atan2f_double_double(num_d, den_d, q_d, idx, k_d, final_sign, + const_term); +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F_H diff --git a/libc/src/__support/math/atan2f128.h b/libc/src/__support/math/atan2f128.h new file mode 100644 index 0000000..89efaf1 --- /dev/null +++ b/libc/src/__support/math/atan2f128.h @@ -0,0 +1,212 @@ +//===-- Implementation header for atan2f128 ---------------------*- 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_ATAN2F128_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F128_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "atan_utils.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/dyadic_float.h" +#include "src/__support/FPUtil/nearest_integer.h" +#include "src/__support/integer_literals.h" +#include "src/__support/macros/config.h" +#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY +#include "src/__support/uint128.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +// There are several range reduction steps we can take for atan2(y, x) as +// follow: + +// * Range reduction 1: signness +// atan2(y, x) will return a number between -PI and PI representing the angle +// forming by the 0x axis and the vector (x, y) on the 0xy-plane. +// In particular, we have that: +// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant) +// = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant) +// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant) +// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant) +// Since atan function is odd, we can use the formula: +// atan(-u) = -atan(u) +// to adjust the above conditions a bit further: +// atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant) +// = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant) +// = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant) +// = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant) +// Which can be simplified to: +// atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0 +// = sign(y) * (pi - atan( |y|/|x| )) if x < 0 + +// * Range reduction 2: reciprocal +// Now that the argument inside atan is positive, we can use the formula: +// atan(1/x) = pi/2 - atan(x) +// to make the argument inside atan <= 1 as follow: +// atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x +// = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y| +// = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x +// = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y| + +// * Range reduction 3: look up table. +// After the previous two range reduction steps, we reduce the problem to +// compute atan(u) with 0 <= u <= 1, or to be precise: +// atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|). +// An accurate polynomial approximation for the whole [0, 1] input range will +// require a very large degree. To make it more efficient, we reduce the input +// range further by finding an integer idx such that: +// | n/d - idx/64 | <= 1/128. +// In particular, +// idx := round(2^6 * n/d) +// Then for the fast pass, we find a polynomial approximation for: +// atan( n/d ) ~ atan( idx/64 ) + (n/d - idx/64) * Q(n/d - idx/64) +// For the accurate pass, we use the addition formula: +// atan( n/d ) - atan( idx/64 ) = atan( (n/d - idx/64)/(1 + (n*idx)/(64*d)) ) +// = atan( (n - d*(idx/64))/(d + n*(idx/64)) ) +// And for the fast pass, we use degree-13 minimax polynomial to compute the +// RHS: +// atan(u) ~ P(u) = u - c_3 * u^3 + c_5 * u^5 - c_7 * u^7 + c_9 *u^9 - +// - c_11 * u^11 + c_13 * u^13 +// with absolute errors bounded by: +// |atan(u) - P(u)| < 2^-121 +// and relative errors bounded by: +// |(atan(u) - P(u)) / P(u)| < 2^-114. + +LIBC_INLINE static constexpr float128 atan2f128(float128 y, float128 x) { + using Float128 = fputil::DyadicFloat<128>; + + constexpr Float128 ZERO = {Sign::POS, 0, 0_u128}; + constexpr Float128 MZERO = {Sign::NEG, 0, 0_u128}; + constexpr Float128 PI = {Sign::POS, -126, + 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; + constexpr Float128 MPI = {Sign::NEG, -126, + 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; + constexpr Float128 PI_OVER_2 = {Sign::POS, -127, + 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; + constexpr Float128 MPI_OVER_2 = {Sign::NEG, -127, + 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; + constexpr Float128 PI_OVER_4 = {Sign::POS, -128, + 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; + constexpr Float128 THREE_PI_OVER_4 = { + Sign::POS, -128, 0x96cbe3f9'990e91a7'9394c9e8'a0a5159d_u128}; + + // Adjustment for constant term: + // CONST_ADJ[x_sign][y_sign][recip] + constexpr Float128 CONST_ADJ[2][2][2] = { + {{ZERO, MPI_OVER_2}, {MZERO, MPI_OVER_2}}, + {{MPI, PI_OVER_2}, {MPI, PI_OVER_2}}}; + + using namespace atan_internal; + using FPBits = fputil::FPBits<float128>; + using Float128 = fputil::DyadicFloat<128>; + + FPBits x_bits(x), y_bits(y); + bool x_sign = x_bits.sign().is_neg(); + bool y_sign = y_bits.sign().is_neg(); + x_bits = x_bits.abs(); + y_bits = y_bits.abs(); + UInt128 x_abs = x_bits.uintval(); + UInt128 y_abs = y_bits.uintval(); + bool recip = x_abs < y_abs; + UInt128 min_abs = recip ? x_abs : y_abs; + UInt128 max_abs = !recip ? x_abs : y_abs; + unsigned min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN); + unsigned max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN); + + Float128 num(FPBits(min_abs).get_val()); + Float128 den(FPBits(max_abs).get_val()); + + // Check for exceptional cases, whether inputs are 0, inf, nan, or close to + // overflow, or close to underflow. + if (LIBC_UNLIKELY(max_exp >= 0x7fffU || min_exp == 0U)) { + if (x_bits.is_nan() || y_bits.is_nan()) + return FPBits::quiet_nan().get_val(); + unsigned x_except = x == 0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1); + unsigned y_except = y == 0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1); + + // Exceptional cases: + // EXCEPT[y_except][x_except][x_is_neg] + // with x_except & y_except: + // 0: zero + // 1: finite, non-zero + // 2: infinity + constexpr Float128 EXCEPTS[3][3][2] = { + {{ZERO, PI}, {ZERO, PI}, {ZERO, PI}}, + {{PI_OVER_2, PI_OVER_2}, {ZERO, ZERO}, {ZERO, PI}}, + {{PI_OVER_2, PI_OVER_2}, + {PI_OVER_2, PI_OVER_2}, + {PI_OVER_4, THREE_PI_OVER_4}}, + }; + + if ((x_except != 1) || (y_except != 1)) { + Float128 r = EXCEPTS[y_except][x_except][x_sign]; + if (y_sign) + r.sign = r.sign.negate(); + return static_cast<float128>(r); + } + } + + bool final_sign = ((x_sign != y_sign) != recip); + Float128 const_term = CONST_ADJ[x_sign][y_sign][recip]; + int exp_diff = den.exponent - num.exponent; + // We have the following bound for normalized n and d: + // 2^(-exp_diff - 1) < n/d < 2^(-exp_diff + 1). + if (LIBC_UNLIKELY(exp_diff > FPBits::FRACTION_LEN + 2)) { + if (final_sign) + const_term.sign = const_term.sign.negate(); + return static_cast<float128>(const_term); + } + + // Take 24 leading bits of num and den to convert to float for fast division. + // We also multiply the numerator by 64 using integer addition directly to the + // exponent field. + float num_f = + cpp::bit_cast<float>(static_cast<uint32_t>(num.mantissa >> 104) + + (6U << fputil::FPBits<float>::FRACTION_LEN)); + float den_f = cpp::bit_cast<float>( + static_cast<uint32_t>(den.mantissa >> 104) + + (static_cast<uint32_t>(exp_diff) << fputil::FPBits<float>::FRACTION_LEN)); + + float k = fputil::nearest_integer(num_f / den_f); + unsigned idx = static_cast<unsigned>(k); + + // k_f128 = idx / 64 + Float128 k_f128(Sign::POS, -6, Float128::MantissaType(idx)); + + // Range reduction: + // atan(n/d) - atan(k) = atan((n/d - k/64) / (1 + (n/d) * (k/64))) + // = atan((n - d * k/64)) / (d + n * k/64)) + // num_f128 = n - d * k/64 + Float128 num_f128 = fputil::multiply_add(den, -k_f128, num); + // den_f128 = d + n * k/64 + Float128 den_f128 = fputil::multiply_add(num, k_f128, den); + + // q = (n - d * k) / (d + n * k) + Float128 q = fputil::quick_mul(num_f128, fputil::approx_reciprocal(den_f128)); + // p ~ atan(q) + Float128 p = atan_eval(q); + + Float128 r = + fputil::quick_add(const_term, fputil::quick_add(ATAN_I_F128[idx], p)); + if (final_sign) + r.sign = r.sign.negate(); + + return static_cast<float128>(r); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F128_H diff --git a/libc/src/math/generic/atan2f_float.h b/libc/src/__support/math/atan2f_float.h index 1fd853d..8bd7095 100644 --- a/libc/src/math/generic/atan2f_float.h +++ b/libc/src/__support/math/atan2f_float.h @@ -1,4 +1,4 @@ -//===-- Single-precision atan2f function ----------------------------------===// +//===-- Single-precision atan2f float function ----------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,18 +6,21 @@ // //===----------------------------------------------------------------------===// +#ifndef LIBC_SRC___SUPPORT_MATH_ATAN2F_FLOAT_H +#define LIBC_SRC___SUPPORT_MATH_ATAN2F_FLOAT_H + #include "src/__support/FPUtil/FPBits.h" #include "src/__support/FPUtil/double_double.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/macros/config.h" #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY -#include "src/math/atan2f.h" namespace LIBC_NAMESPACE_DECL { -namespace { +namespace math { + +namespace atan2f_internal { using FloatFloat = fputil::FloatFloat; @@ -27,7 +30,7 @@ using FloatFloat = fputil::FloatFloat; // b = round(atan(i/16) - a, SG, RN); // print("{", b, ",", a, "},"); // }; -constexpr FloatFloat ATAN_I[17] = { +static constexpr FloatFloat ATAN_I[17] = { {0.0f, 0.0f}, {-0x1.1a6042p-30f, 0x1.ff55bcp-5f}, {-0x1.54f424p-30f, 0x1.fd5baap-4f}, @@ -57,7 +60,7 @@ constexpr FloatFloat ATAN_I[17] = { // For x = x_hi + x_lo, fully expand the polynomial and drop any terms less than // ulp(x_hi^3 / 3) gives us: // P(x) ~ x_hi - x_hi^3/3 + x_lo * (1 - x_hi^2) -FloatFloat atan_eval(const FloatFloat &x) { +LIBC_INLINE static constexpr FloatFloat atan_eval(const FloatFloat &x) { FloatFloat p; p.hi = x.hi; float x_hi_sq = x.hi * x.hi; @@ -70,7 +73,7 @@ FloatFloat atan_eval(const FloatFloat &x) { return p; } -} // anonymous namespace +} // namespace atan2f_internal // There are several range reduction steps we can take for atan2(y, x) as // follow: @@ -121,7 +124,8 @@ FloatFloat atan_eval(const FloatFloat &x) { // > dirtyinfnorm(atan(x) - P, [-2^-5, 2^-5]); // 0x1.995...p-28. -LLVM_LIBC_FUNCTION(float, atan2f, (float y, float x)) { +LIBC_INLINE static constexpr float atan2f(float y, float x) { + using namespace atan2f_internal; using FPBits = typename fputil::FPBits<float>; constexpr float IS_NEG[2] = {1.0f, -1.0f}; constexpr FloatFloat ZERO = {0.0f, 0.0f}; @@ -234,4 +238,8 @@ LLVM_LIBC_FUNCTION(float, atan2f, (float y, float x)) { return final_sign * r.hi; } +} // namespace math + } // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_SRC___SUPPORT_MATH_ATAN2F_FLOAT_H diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h new file mode 100644 index 0000000..b3ee5bb --- /dev/null +++ b/libc/src/__support/math/atanhf.h @@ -0,0 +1,76 @@ +//===-- Implementation header for atanhf ------------------------*- 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_ATANHF_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H + +#include "acoshf_utils.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/macros/config.h" +#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +LIBC_INLINE static constexpr float atanhf(float x) { + using namespace acoshf_internal; + using FPBits = typename fputil::FPBits<float>; + + FPBits xbits(x); + Sign sign = xbits.sign(); + uint32_t x_abs = xbits.abs().uintval(); + + // |x| >= 1.0 + if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) { + if (xbits.is_nan()) { + if (xbits.is_signaling_nan()) { + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + return x; + } + // |x| == 1.0 + if (x_abs == 0x3F80'0000U) { + fputil::set_errno_if_required(ERANGE); + fputil::raise_except_if_required(FE_DIVBYZERO); + return FPBits::inf(sign).get_val(); + } else { + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + } + + // |x| < ~0.10 + if (LIBC_UNLIKELY(x_abs <= 0x3dcc'0000U)) { + // |x| <= 2^-26 + if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) { + return static_cast<float>(LIBC_UNLIKELY(x_abs == 0) + ? x + : (x + 0x1.5555555555555p-2 * x * x * x)); + } + + double xdbl = x; + double x2 = xdbl * xdbl; + // Pure Taylor series. + double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2, + 0x1.999999999999ap-3, 0x1.2492492492492p-3, + 0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4); + return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl)); + } + double xdbl = x; + return static_cast<float>(0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0))); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H diff --git a/libc/src/__support/math/atanhf16.h b/libc/src/__support/math/atanhf16.h new file mode 100644 index 0000000..80929dd --- /dev/null +++ b/libc/src/__support/math/atanhf16.h @@ -0,0 +1,234 @@ +//===-- Implementation header for atanhf16 ----------------------*- 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_ATANHF16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF16_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/PolyEval.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/config.h" +#include "src/__support/macros/optimization.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +namespace atanhf16_internal { + +// Lookup table for logf(f) = logf(1 + n*2^(-7)) where n = 0..127, +// computed and stored as float precision constants. +// Generated by Sollya with the following commands: +// display = hexadecimal; +// for n from 0 to 127 do { print(single(1 / (1 + n / 128.0))); }; +static constexpr float ONE_OVER_F_FLOAT[128] = { + 0x1p0f, 0x1.fc07fp-1f, 0x1.f81f82p-1f, 0x1.f4465ap-1f, + 0x1.f07c2p-1f, 0x1.ecc07cp-1f, 0x1.e9131ap-1f, 0x1.e573acp-1f, + 0x1.e1e1e2p-1f, 0x1.de5d6ep-1f, 0x1.dae608p-1f, 0x1.d77b66p-1f, + 0x1.d41d42p-1f, 0x1.d0cb58p-1f, 0x1.cd8568p-1f, 0x1.ca4b3p-1f, + 0x1.c71c72p-1f, 0x1.c3f8fp-1f, 0x1.c0e07p-1f, 0x1.bdd2b8p-1f, + 0x1.bacf92p-1f, 0x1.b7d6c4p-1f, 0x1.b4e81cp-1f, 0x1.b20364p-1f, + 0x1.af286cp-1f, 0x1.ac5702p-1f, 0x1.a98ef6p-1f, 0x1.a6d01ap-1f, + 0x1.a41a42p-1f, 0x1.a16d4p-1f, 0x1.9ec8eap-1f, 0x1.9c2d14p-1f, + 0x1.99999ap-1f, 0x1.970e5p-1f, 0x1.948b1p-1f, 0x1.920fb4p-1f, + 0x1.8f9c18p-1f, 0x1.8d3018p-1f, 0x1.8acb9p-1f, 0x1.886e6p-1f, + 0x1.861862p-1f, 0x1.83c978p-1f, 0x1.818182p-1f, 0x1.7f406p-1f, + 0x1.7d05f4p-1f, 0x1.7ad22p-1f, 0x1.78a4c8p-1f, 0x1.767dcep-1f, + 0x1.745d18p-1f, 0x1.724288p-1f, 0x1.702e06p-1f, 0x1.6e1f76p-1f, + 0x1.6c16c2p-1f, 0x1.6a13cep-1f, 0x1.681682p-1f, 0x1.661ec6p-1f, + 0x1.642c86p-1f, 0x1.623fa8p-1f, 0x1.605816p-1f, 0x1.5e75bcp-1f, + 0x1.5c9882p-1f, 0x1.5ac056p-1f, 0x1.58ed24p-1f, 0x1.571ed4p-1f, + 0x1.555556p-1f, 0x1.539094p-1f, 0x1.51d07ep-1f, 0x1.501502p-1f, + 0x1.4e5e0ap-1f, 0x1.4cab88p-1f, 0x1.4afd6ap-1f, 0x1.49539ep-1f, + 0x1.47ae14p-1f, 0x1.460cbcp-1f, 0x1.446f86p-1f, 0x1.42d662p-1f, + 0x1.414142p-1f, 0x1.3fb014p-1f, 0x1.3e22ccp-1f, 0x1.3c995ap-1f, + 0x1.3b13b2p-1f, 0x1.3991c2p-1f, 0x1.381382p-1f, 0x1.3698ep-1f, + 0x1.3521dp-1f, 0x1.33ae46p-1f, 0x1.323e34p-1f, 0x1.30d19p-1f, + 0x1.2f684cp-1f, 0x1.2e025cp-1f, 0x1.2c9fb4p-1f, 0x1.2b404ap-1f, + 0x1.29e412p-1f, 0x1.288b02p-1f, 0x1.27350cp-1f, 0x1.25e228p-1f, + 0x1.24924ap-1f, 0x1.234568p-1f, 0x1.21fb78p-1f, 0x1.20b47p-1f, + 0x1.1f7048p-1f, 0x1.1e2ef4p-1f, 0x1.1cf06ap-1f, 0x1.1bb4a4p-1f, + 0x1.1a7b96p-1f, 0x1.194538p-1f, 0x1.181182p-1f, 0x1.16e068p-1f, + 0x1.15b1e6p-1f, 0x1.1485fp-1f, 0x1.135c82p-1f, 0x1.12358ep-1f, + 0x1.111112p-1f, 0x1.0fef02p-1f, 0x1.0ecf56p-1f, 0x1.0db20ap-1f, + 0x1.0c9714p-1f, 0x1.0b7e6ep-1f, 0x1.0a681p-1f, 0x1.0953f4p-1f, + 0x1.08421p-1f, 0x1.07326p-1f, 0x1.0624dep-1f, 0x1.05198p-1f, + 0x1.041042p-1f, 0x1.03091cp-1f, 0x1.020408p-1f, 0x1.010102p-1f}; + +// Lookup table for log(f) = log(1 + n*2^(-7)) where n = 0..127, +// computed and stored as float precision constants. +// Generated by Sollya with the following commands: +// display = hexadecimal; +// for n from 0 to 127 do { print(single(log(1 + n / 128.0))); }; +static constexpr float LOG_F_FLOAT[128] = { + 0.0f, 0x1.fe02a6p-8f, 0x1.fc0a8cp-7f, 0x1.7b91bp-6f, + 0x1.f829bp-6f, 0x1.39e87cp-5f, 0x1.77459p-5f, 0x1.b42dd8p-5f, + 0x1.f0a30cp-5f, 0x1.16536ep-4f, 0x1.341d7ap-4f, 0x1.51b074p-4f, + 0x1.6f0d28p-4f, 0x1.8c345ep-4f, 0x1.a926d4p-4f, 0x1.c5e548p-4f, + 0x1.e27076p-4f, 0x1.fec914p-4f, 0x1.0d77e8p-3f, 0x1.1b72aep-3f, + 0x1.29553p-3f, 0x1.371fc2p-3f, 0x1.44d2b6p-3f, 0x1.526e5ep-3f, + 0x1.5ff308p-3f, 0x1.6d60fep-3f, 0x1.7ab89p-3f, 0x1.87fa06p-3f, + 0x1.9525aap-3f, 0x1.a23bc2p-3f, 0x1.af3c94p-3f, 0x1.bc2868p-3f, + 0x1.c8ff7cp-3f, 0x1.d5c216p-3f, 0x1.e27076p-3f, 0x1.ef0adcp-3f, + 0x1.fb9186p-3f, 0x1.04025ap-2f, 0x1.0a324ep-2f, 0x1.1058cp-2f, + 0x1.1675cap-2f, 0x1.1c898cp-2f, 0x1.22942p-2f, 0x1.2895a2p-2f, + 0x1.2e8e2cp-2f, 0x1.347ddap-2f, 0x1.3a64c6p-2f, 0x1.404308p-2f, + 0x1.4618bcp-2f, 0x1.4be5fap-2f, 0x1.51aad8p-2f, 0x1.576772p-2f, + 0x1.5d1bdcp-2f, 0x1.62c83p-2f, 0x1.686c82p-2f, 0x1.6e08eap-2f, + 0x1.739d8p-2f, 0x1.792a56p-2f, 0x1.7eaf84p-2f, 0x1.842d1ep-2f, + 0x1.89a338p-2f, 0x1.8f11e8p-2f, 0x1.947942p-2f, 0x1.99d958p-2f, + 0x1.9f323ep-2f, 0x1.a4840ap-2f, 0x1.a9cecap-2f, 0x1.af1294p-2f, + 0x1.b44f78p-2f, 0x1.b9858ap-2f, 0x1.beb4dap-2f, 0x1.c3dd7ap-2f, + 0x1.c8ff7cp-2f, 0x1.ce1afp-2f, 0x1.d32fe8p-2f, 0x1.d83e72p-2f, + 0x1.dd46ap-2f, 0x1.e24882p-2f, 0x1.e74426p-2f, 0x1.ec399ep-2f, + 0x1.f128f6p-2f, 0x1.f6124p-2f, 0x1.faf588p-2f, 0x1.ffd2ep-2f, + 0x1.02552ap-1f, 0x1.04bdfap-1f, 0x1.0723e6p-1f, 0x1.0986f4p-1f, + 0x1.0be72ep-1f, 0x1.0e4498p-1f, 0x1.109f3ap-1f, 0x1.12f71ap-1f, + 0x1.154c3ep-1f, 0x1.179eacp-1f, 0x1.19ee6cp-1f, 0x1.1c3b82p-1f, + 0x1.1e85f6p-1f, 0x1.20cdcep-1f, 0x1.23130ep-1f, 0x1.2555bcp-1f, + 0x1.2795e2p-1f, 0x1.29d38p-1f, 0x1.2c0e9ep-1f, 0x1.2e4744p-1f, + 0x1.307d74p-1f, 0x1.32b134p-1f, 0x1.34e28ap-1f, 0x1.37117cp-1f, + 0x1.393e0ep-1f, 0x1.3b6844p-1f, 0x1.3d9026p-1f, 0x1.3fb5b8p-1f, + 0x1.41d8fep-1f, 0x1.43f9fep-1f, 0x1.4618bcp-1f, 0x1.48353ep-1f, + 0x1.4a4f86p-1f, 0x1.4c679ap-1f, 0x1.4e7d82p-1f, 0x1.50913cp-1f, + 0x1.52a2d2p-1f, 0x1.54b246p-1f, 0x1.56bf9ep-1f, 0x1.58cadcp-1f, + 0x1.5ad404p-1f, 0x1.5cdb1ep-1f, 0x1.5ee02ap-1f, 0x1.60e33p-1f}; + +// x should be positive, normal finite value +// TODO: Simplify range reduction and polynomial degree for float16. +// See issue #137190. +LIBC_INLINE static float log_eval_f(float x) { + // For x = 2^ex * (1 + mx), logf(x) = ex * logf(2) + logf(1 + mx). + using FPBits = fputil::FPBits<float>; + FPBits xbits(x); + + float ex = static_cast<float>(xbits.get_exponent()); + // p1 is the leading 7 bits of mx, i.e. + // p1 * 2^(-7) <= m_x < (p1 + 1) * 2^(-7). + int p1 = static_cast<int>(xbits.get_mantissa() >> (FPBits::FRACTION_LEN - 7)); + + // Set bits to (1 + (mx - p1*2^(-7))) + xbits.set_uintval(xbits.uintval() & (FPBits::FRACTION_MASK >> 7)); + xbits.set_biased_exponent(FPBits::EXP_BIAS); + // dx = (mx - p1*2^(-7)) / (1 + p1*2^(-7)). + float dx = (xbits.get_val() - 1.0f) * ONE_OVER_F_FLOAT[p1]; + + // Minimax polynomial for log(1 + dx), generated using Sollya: + // > P = fpminimax(log(1 + x)/x, 6, [|SG...|], [0, 2^-7]); + // > Q = (P - 1) / x; + // > for i from 0 to degree(Q) do print(coeff(Q, i)); + constexpr float COEFFS[6] = {-0x1p-1f, 0x1.555556p-2f, -0x1.00022ep-2f, + 0x1.9ea056p-3f, -0x1.e50324p-2f, 0x1.c018fp3f}; + + float dx2 = dx * dx; + + float c1 = fputil::multiply_add(dx, COEFFS[1], COEFFS[0]); + float c2 = fputil::multiply_add(dx, COEFFS[3], COEFFS[2]); + float c3 = fputil::multiply_add(dx, COEFFS[5], COEFFS[4]); + + float p = fputil::polyeval(dx2, dx, c1, c2, c3); + + // Generated by Sollya with the following commands: + // > display = hexadecimal; + // > round(log(2), SG, RN); + constexpr float LOGF_2 = 0x1.62e43p-1f; + + float result = fputil::multiply_add(ex, LOGF_2, LOG_F_FLOAT[p1] + p); + return result; +} + +} // namespace atanhf16_internal + +LIBC_INLINE static constexpr float16 atanhf16(float16 x) { + constexpr size_t N_EXCEPTS = 1; + constexpr fputil::ExceptValues<float16, N_EXCEPTS> ATANHF16_EXCEPTS{{ + // (input, RZ output, RU offset, RD offset, RN offset) + // x = 0x1.a5cp-4, atanhf16(x) = 0x1.a74p-4 (RZ) + {0x2E97, 0x2E9D, 1, 0, 0}, + }}; + + using namespace atanhf16_internal; + using FPBits = fputil::FPBits<float16>; + + FPBits xbits(x); + Sign sign = xbits.sign(); + uint16_t x_abs = xbits.abs().uintval(); + + // |x| >= 1 + if (LIBC_UNLIKELY(x_abs >= 0x3c00U)) { + if (xbits.is_nan()) { + if (xbits.is_signaling_nan()) { + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + return x; + } + + // |x| == 1.0 + if (x_abs == 0x3c00U) { + fputil::set_errno_if_required(ERANGE); + fputil::raise_except_if_required(FE_DIVBYZERO); + return FPBits::inf(sign).get_val(); + } + // |x| > 1.0 + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + + if (auto r = ATANHF16_EXCEPTS.lookup(xbits.uintval()); + LIBC_UNLIKELY(r.has_value())) + return r.value(); + + // For |x| less than approximately 0.24 + if (LIBC_UNLIKELY(x_abs <= 0x33f3U)) { + // atanh(+/-0) = +/-0 + if (LIBC_UNLIKELY(x_abs == 0U)) + return x; + // The Taylor expansion of atanh(x) is: + // atanh(x) = x + x^3/3 + x^5/5 + x^7/7 + x^9/9 + x^11/11 + // = x * [1 + x^2/3 + x^4/5 + x^6/7 + x^8/9 + x^10/11] + // When |x| < 2^-5 (0x0800U), this can be approximated by: + // atanh(x) ≈ x + (1/3)*x^3 + if (LIBC_UNLIKELY(x_abs < 0x0800U)) { + float xf = x; + return fputil::cast<float16>(xf + 0x1.555556p-2f * xf * xf * xf); + } + + // For 2^-5 <= |x| <= 0x1.fccp-3 (~0.24): + // Let t = x^2. + // Define P(t) ≈ (1/3)*t + (1/5)*t^2 + (1/7)*t^3 + (1/9)*t^4 + (1/11)*t^5. + // Coefficients (from Sollya, RN, hexadecimal): + // 1/3 = 0x1.555556p-2, 1/5 = 0x1.99999ap-3, 1/7 = 0x1.24924ap-3, + // 1/9 = 0x1.c71c72p-4, 1/11 = 0x1.745d18p-4 + // Thus, atanh(x) ≈ x * (1 + P(x^2)). + float xf = x; + float x2 = xf * xf; + float pe = fputil::polyeval(x2, 0.0f, 0x1.555556p-2f, 0x1.99999ap-3f, + 0x1.24924ap-3f, 0x1.c71c72p-4f, 0x1.745d18p-4f); + return fputil::cast<float16>(fputil::multiply_add(xf, pe, xf)); + } + + float xf = x; + return fputil::cast<float16>(0.5 * log_eval_f((xf + 1.0f) / (xf - 1.0f))); +} + +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF16_H diff --git a/libc/src/__support/threads/CMakeLists.txt b/libc/src/__support/threads/CMakeLists.txt index b084346..f8a4493 100644 --- a/libc/src/__support/threads/CMakeLists.txt +++ b/libc/src/__support/threads/CMakeLists.txt @@ -42,6 +42,14 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.mutex) .mutex libc.src.__support.CPP.mutex ) +elseif(NOT (LIBC_CONF_THREAD_MODE STREQUAL LIBC_THREAD_MODE_PLATFORM)) + add_header_library( + mutex + HDRS + mutex.h + DEPENDS + .mutex_common + ) endif() add_header_library( diff --git a/libc/src/__support/threads/gpu/CMakeLists.txt b/libc/src/__support/threads/gpu/CMakeLists.txt deleted file mode 100644 index ea89feb..0000000 --- a/libc/src/__support/threads/gpu/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_header_library( - mutex - HDRS - mutex.h -) diff --git a/libc/src/__support/threads/gpu/mutex.h b/libc/src/__support/threads/gpu/mutex.h deleted file mode 100644 index c8c484e..0000000 --- a/libc/src/__support/threads/gpu/mutex.h +++ /dev/null @@ -1,32 +0,0 @@ -//===--- Implementation of a GPU mutex class --------------------*- 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_THREADS_GPU_MUTEX_H -#define LLVM_LIBC_SRC___SUPPORT_THREADS_GPU_MUTEX_H - -#include "src/__support/macros/attributes.h" -#include "src/__support/macros/config.h" -#include "src/__support/threads/mutex_common.h" - -namespace LIBC_NAMESPACE_DECL { - -/// Implementation of a simple passthrough mutex which guards nothing. A -/// complete Mutex locks in general cannot be implemented on the GPU. We simply -/// define the Mutex interface and require that only a single thread executes -/// code requiring a mutex lock. -struct Mutex { - LIBC_INLINE constexpr Mutex(bool, bool, bool, bool) {} - - LIBC_INLINE MutexError lock() { return MutexError::NONE; } - LIBC_INLINE MutexError unlock() { return MutexError::NONE; } - LIBC_INLINE MutexError reset() { return MutexError::NONE; } -}; - -} // namespace LIBC_NAMESPACE_DECL - -#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_GPU_MUTEX_H diff --git a/libc/src/__support/threads/mutex.h b/libc/src/__support/threads/mutex.h index 392b389..cbef0d0 100644 --- a/libc/src/__support/threads/mutex.h +++ b/libc/src/__support/threads/mutex.h @@ -9,10 +9,35 @@ #ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H #define LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H -#include "src/__support/macros/properties/architectures.h" +#include "src/__support/macros/attributes.h" +#include "src/__support/macros/config.h" + +// Uses the platform specific specialization +#define LIBC_THREAD_MODE_PLATFORM 0 + +// Mutex guards nothing, used in single-threaded implementations +#define LIBC_THREAD_MODE_SINGLE 1 + +// Vendor provides implementation +#define LIBC_THREAD_MODE_EXTERNAL 2 + +#if !defined(LIBC_THREAD_MODE) +#error LIBC_THREAD_MODE is undefined +#endif // LIBC_THREAD_MODE + +#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \ + LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \ + LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL +#error LIBC_THREAD_MODE must be one of the following values: \ +LIBC_THREAD_MODE_PLATFORM, \ +LIBC_THREAD_MODE_SINGLE, \ +LIBC_THREAD_MODE_EXTERNAL. +#endif + +#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_PLATFORM // Platform independent code will include this header file which pulls -// the platfrom specific specializations using platform macros. +// the platform specific specializations using platform macros. // // The platform specific specializations should define a class by name // Mutex with non-static methods having the following signature: @@ -39,8 +64,32 @@ #if defined(__linux__) #include "src/__support/threads/linux/mutex.h" -#elif defined(LIBC_TARGET_ARCH_IS_GPU) -#include "src/__support/threads/gpu/mutex.h" #endif // __linux__ +#elif LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE + +#include "src/__support/threads/mutex_common.h" + +namespace LIBC_NAMESPACE_DECL { + +/// Implementation of a simple passthrough mutex which guards nothing. A +/// complete Mutex locks in general cannot be implemented on the GPU, or on some +/// baremetal platforms. We simply define the Mutex interface and require that +/// only a single thread executes code requiring a mutex lock. +struct Mutex { + LIBC_INLINE constexpr Mutex(bool, bool, bool, bool) {} + + LIBC_INLINE MutexError lock() { return MutexError::NONE; } + LIBC_INLINE MutexError unlock() { return MutexError::NONE; } + LIBC_INLINE MutexError reset() { return MutexError::NONE; } +}; + +} // namespace LIBC_NAMESPACE_DECL + +#elif LIBC_THREAD_MODE == LIBC_THREAD_MODE_EXTERNAL + +// TODO: Implement the interfacing, if necessary, e.g. "extern struct Mutex;" + +#endif // LIBC_THREAD_MODE == LIBC_THREAD_MODE_PLATFORM + #endif // LLVM_LIBC_SRC___SUPPORT_THREADS_MUTEX_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 6bcb1e2..9df9973 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -3922,10 +3922,7 @@ add_entrypoint_object( HDRS ../atanhf.h DEPENDS - .explogxf - libc.src.__support.FPUtil.fp_bits - libc.src.__support.FPUtil.fenv_impl - libc.src.__support.macros.optimization + libc.src.__support.math.atanhf ) add_entrypoint_object( @@ -3935,17 +3932,7 @@ add_entrypoint_object( HDRS ../atanhf16.h DEPENDS - .explogxf - libc.hdr.errno_macros - libc.hdr.fenv_macros - libc.src.__support.FPUtil.cast - 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.polyeval - libc.src.__support.macros.optimization - libc.src.__support.macros.properties.types + libc.src.__support.math.atanhf16 ) add_entrypoint_object( @@ -4058,18 +4045,8 @@ add_entrypoint_object( atan2f.cpp HDRS ../atan2f.h - atan2f_float.h DEPENDS - libc.hdr.fenv_macros - libc.src.__support.FPUtil.double_double - 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.math.inv_trigf_utils + libc.src.__support.math.atan2f ) add_entrypoint_object( @@ -4099,15 +4076,7 @@ add_entrypoint_object( HDRS ../atan2f128.h DEPENDS - libc.src.__support.math.atan_utils - libc.src.__support.integer_literals - libc.src.__support.uint128 - libc.src.__support.FPUtil.dyadic_float - libc.src.__support.FPUtil.fp_bits - libc.src.__support.FPUtil.multiply_add - libc.src.__support.FPUtil.nearest_integer - libc.src.__support.macros.optimization - libc.src.__support.macros.properties.types + libc.src.__support.math.atan2f128 ) add_entrypoint_object( diff --git a/libc/src/math/generic/atan2f.cpp b/libc/src/math/generic/atan2f.cpp index 32b977f..7c56788 100644 --- a/libc/src/math/generic/atan2f.cpp +++ b/libc/src/math/generic/atan2f.cpp @@ -7,336 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/atan2f.h" -#include "hdr/fenv_macros.h" -#include "src/__support/FPUtil/FEnvImpl.h" -#include "src/__support/FPUtil/FPBits.h" -#include "src/__support/FPUtil/PolyEval.h" -#include "src/__support/FPUtil/double_double.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/macros/config.h" -#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY -#include "src/__support/math/inv_trigf_utils.h" - -#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) && \ - defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT) - -// We use float-float implementation to reduce size. -#include "src/math/generic/atan2f_float.h" - -#else +#include "src/__support/math/atan2f.h" namespace LIBC_NAMESPACE_DECL { -namespace { - -#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS - -// Look up tables for accurate pass: - -// atan(i/16) with i = 0..16, generated by Sollya with: -// > for i from 0 to 16 do { -// a = round(atan(i/16), D, RN); -// b = round(atan(i/16) - a, D, RN); -// print("{", b, ",", a, "},"); -// }; -constexpr fputil::DoubleDouble ATAN_I[17] = { - {0.0, 0.0}, - {-0x1.c934d86d23f1dp-60, 0x1.ff55bb72cfdeap-5}, - {-0x1.cd37686760c17p-59, 0x1.fd5ba9aac2f6ep-4}, - {0x1.347b0b4f881cap-58, 0x1.7b97b4bce5b02p-3}, - {0x1.8ab6e3cf7afbdp-57, 0x1.f5b75f92c80ddp-3}, - {-0x1.963a544b672d8p-57, 0x1.362773707ebccp-2}, - {-0x1.c63aae6f6e918p-56, 0x1.6f61941e4def1p-2}, - {-0x1.24dec1b50b7ffp-56, 0x1.a64eec3cc23fdp-2}, - {0x1.a2b7f222f65e2p-56, 0x1.dac670561bb4fp-2}, - {-0x1.d5b495f6349e6p-56, 0x1.0657e94db30dp-1}, - {-0x1.928df287a668fp-58, 0x1.1e00babdefeb4p-1}, - {0x1.1021137c71102p-55, 0x1.345f01cce37bbp-1}, - {0x1.2419a87f2a458p-56, 0x1.4978fa3269ee1p-1}, - {0x1.0028e4bc5e7cap-57, 0x1.5d58987169b18p-1}, - {-0x1.8c34d25aadef6p-56, 0x1.700a7c5784634p-1}, - {-0x1.bf76229d3b917p-56, 0x1.819d0b7158a4dp-1}, - {0x1.1a62633145c07p-55, 0x1.921fb54442d18p-1}, -}; - -// Taylor polynomial, generated by Sollya with: -// > for i from 0 to 8 do { -// j = (-1)^(i + 1)/(2*i + 1); -// a = round(j, D, RN); -// b = round(j - a, D, RN); -// print("{", b, ",", a, "},"); -// }; -constexpr fputil::DoubleDouble COEFFS[9] = { - {0.0, 1.0}, // 1 - {-0x1.5555555555555p-56, -0x1.5555555555555p-2}, // -1/3 - {-0x1.999999999999ap-57, 0x1.999999999999ap-3}, // 1/5 - {-0x1.2492492492492p-57, -0x1.2492492492492p-3}, // -1/7 - {0x1.c71c71c71c71cp-58, 0x1.c71c71c71c71cp-4}, // 1/9 - {0x1.745d1745d1746p-59, -0x1.745d1745d1746p-4}, // -1/11 - {-0x1.3b13b13b13b14p-58, 0x1.3b13b13b13b14p-4}, // 1/13 - {-0x1.1111111111111p-60, -0x1.1111111111111p-4}, // -1/15 - {0x1.e1e1e1e1e1e1ep-61, 0x1.e1e1e1e1e1e1ep-5}, // 1/17 -}; - -// Veltkamp's splitting of a double precision into hi + lo, where the hi part is -// slightly smaller than an even split, so that the product of -// hi * (s1 * k + s2) is exact, -// where: -// s1, s2 are single precsion, -// 1/16 <= s1/s2 <= 1 -// 1/16 <= k <= 1 is an integer. -// So the maximal precision of (s1 * k + s2) is: -// prec(s1 * k + s2) = 2 + log2(msb(s2)) - log2(lsb(k_d * s1)) -// = 2 + log2(msb(s1)) + 4 - log2(lsb(k_d)) - log2(lsb(s1)) -// = 2 + log2(lsb(s1)) + 23 + 4 - (-4) - log2(lsb(s1)) -// = 33. -// Thus, the Veltkamp splitting constant is C = 2^33 + 1. -// This is used when FMA instruction is not available. -[[maybe_unused]] constexpr fputil::DoubleDouble split_d(double a) { - fputil::DoubleDouble r{0.0, 0.0}; - constexpr double C = 0x1.0p33 + 1.0; - double t1 = C * a; - double t2 = a - t1; - r.hi = t1 + t2; - r.lo = a - r.hi; - return r; -} - -// Compute atan( num_d / den_d ) in double-double precision. -// num_d = min(|x|, |y|) -// den_d = max(|x|, |y|) -// q_d = num_d / den_d -// idx, k_d = round( 2^4 * num_d / den_d ) -// final_sign = sign of the final result -// const_term = the constant term in the final expression. -float atan2f_double_double(double num_d, double den_d, double q_d, int idx, - double k_d, double final_sign, - const fputil::DoubleDouble &const_term) { - fputil::DoubleDouble q; - double num_r, den_r; - - if (idx != 0) { - // The following range reduction is accurate even without fma for - // 1/16 <= n/d <= 1. - // atan(n/d) - atan(idx/16) = atan((n/d - idx/16) / (1 + (n/d) * (idx/16))) - // = atan((n - d*(idx/16)) / (d + n*idx/16)) - k_d *= 0x1.0p-4; - num_r = fputil::multiply_add(k_d, -den_d, num_d); // Exact - den_r = fputil::multiply_add(k_d, num_d, den_d); // Exact - q.hi = num_r / den_r; - } else { - // For 0 < n/d < 1/16, we just need to calculate the lower part of their - // quotient. - q.hi = q_d; - num_r = num_d; - den_r = den_d; - } -#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - q.lo = fputil::multiply_add(q.hi, -den_r, num_r) / den_r; -#else - // Compute `(num_r - q.hi * den_r) / den_r` accurately without FMA - // instructions. - fputil::DoubleDouble q_hi_dd = split_d(q.hi); - double t1 = fputil::multiply_add(q_hi_dd.hi, -den_r, num_r); // Exact - double t2 = fputil::multiply_add(q_hi_dd.lo, -den_r, t1); - q.lo = t2 / den_r; -#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE - - // Taylor polynomial, evaluating using Horner's scheme: - // P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9 - x^11/11 + x^13/13 - x^15/15 - // + x^17/17 - // = x*(1 + x^2*(-1/3 + x^2*(1/5 + x^2*(-1/7 + x^2*(1/9 + x^2* - // *(-1/11 + x^2*(1/13 + x^2*(-1/15 + x^2 * 1/17)))))))) - fputil::DoubleDouble q2 = fputil::quick_mult(q, q); - fputil::DoubleDouble p_dd = - fputil::polyeval(q2, COEFFS[0], COEFFS[1], COEFFS[2], COEFFS[3], - COEFFS[4], COEFFS[5], COEFFS[6], COEFFS[7], COEFFS[8]); - fputil::DoubleDouble r_dd = - fputil::add(const_term, fputil::multiply_add(q, p_dd, ATAN_I[idx])); - r_dd.hi *= final_sign; - r_dd.lo *= final_sign; - - // Make sure the sum is normalized: - fputil::DoubleDouble rr = fputil::exact_add(r_dd.hi, r_dd.lo); - // Round to odd. - uint64_t rr_bits = cpp::bit_cast<uint64_t>(rr.hi); - if (LIBC_UNLIKELY(((rr_bits & 0xfff'ffff) == 0) && (rr.lo != 0.0))) { - Sign hi_sign = fputil::FPBits<double>(rr.hi).sign(); - Sign lo_sign = fputil::FPBits<double>(rr.lo).sign(); - if (hi_sign == lo_sign) { - ++rr_bits; - } else if ((rr_bits & fputil::FPBits<double>::FRACTION_MASK) > 0) { - --rr_bits; - } - } - - return static_cast<float>(cpp::bit_cast<double>(rr_bits)); -} - -#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS - -} // anonymous namespace - -// There are several range reduction steps we can take for atan2(y, x) as -// follow: - -// * Range reduction 1: signness -// atan2(y, x) will return a number between -PI and PI representing the angle -// forming by the 0x axis and the vector (x, y) on the 0xy-plane. -// In particular, we have that: -// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant) -// = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant) -// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant) -// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant) -// Since atan function is odd, we can use the formula: -// atan(-u) = -atan(u) -// to adjust the above conditions a bit further: -// atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant) -// = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant) -// = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant) -// = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant) -// Which can be simplified to: -// atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0 -// = sign(y) * (pi - atan( |y|/|x| )) if x < 0 - -// * Range reduction 2: reciprocal -// Now that the argument inside atan is positive, we can use the formula: -// atan(1/x) = pi/2 - atan(x) -// to make the argument inside atan <= 1 as follow: -// atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x -// = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y| -// = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x -// = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y| - -// * Range reduction 3: look up table. -// After the previous two range reduction steps, we reduce the problem to -// compute atan(u) with 0 <= u <= 1, or to be precise: -// atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|). -// An accurate polynomial approximation for the whole [0, 1] input range will -// require a very large degree. To make it more efficient, we reduce the input -// range further by finding an integer idx such that: -// | n/d - idx/16 | <= 1/32. -// In particular, -// idx := 2^-4 * round(2^4 * n/d) -// Then for the fast pass, we find a polynomial approximation for: -// atan( n/d ) ~ atan( idx/16 ) + (n/d - idx/16) * Q(n/d - idx/16) -// For the accurate pass, we use the addition formula: -// atan( n/d ) - atan( idx/16 ) = atan( (n/d - idx/16)/(1 + (n*idx)/(16*d)) ) -// = atan( (n - d * idx/16)/(d + n * idx/16) ) -// And finally we use Taylor polynomial to compute the RHS in the accurate pass: -// atan(u) ~ P(u) = u - u^3/3 + u^5/5 - u^7/7 + u^9/9 - u^11/11 + u^13/13 - -// - u^15/15 + u^17/17 -// It's error in double-double precision is estimated in Sollya to be: -// > P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9 - x^11/11 + x^13/13 - x^15/15 -// + x^17/17; -// > dirtyinfnorm(atan(x) - P, [-2^-5, 2^-5]); -// 0x1.aec6f...p-100 -// which is about rounding errors of double-double (2^-104). - LLVM_LIBC_FUNCTION(float, atan2f, (float y, float x)) { - using namespace inv_trigf_utils_internal; - using FPBits = typename fputil::FPBits<float>; - constexpr double IS_NEG[2] = {1.0, -1.0}; - constexpr double PI = 0x1.921fb54442d18p1; - constexpr double PI_LO = 0x1.1a62633145c07p-53; - constexpr double PI_OVER_4 = 0x1.921fb54442d18p-1; - constexpr double PI_OVER_2 = 0x1.921fb54442d18p0; - constexpr double THREE_PI_OVER_4 = 0x1.2d97c7f3321d2p+1; - // Adjustment for constant term: - // CONST_ADJ[x_sign][y_sign][recip] - constexpr fputil::DoubleDouble CONST_ADJ[2][2][2] = { - {{{0.0, 0.0}, {-PI_LO / 2, -PI_OVER_2}}, - {{-0.0, -0.0}, {-PI_LO / 2, -PI_OVER_2}}}, - {{{-PI_LO, -PI}, {PI_LO / 2, PI_OVER_2}}, - {{-PI_LO, -PI}, {PI_LO / 2, PI_OVER_2}}}}; - - FPBits x_bits(x), y_bits(y); - bool x_sign = x_bits.sign().is_neg(); - bool y_sign = y_bits.sign().is_neg(); - x_bits.set_sign(Sign::POS); - y_bits.set_sign(Sign::POS); - uint32_t x_abs = x_bits.uintval(); - uint32_t y_abs = y_bits.uintval(); - uint32_t max_abs = x_abs > y_abs ? x_abs : y_abs; - uint32_t min_abs = x_abs <= y_abs ? x_abs : y_abs; - float num_f = FPBits(min_abs).get_val(); - float den_f = FPBits(max_abs).get_val(); - double num_d = static_cast<double>(num_f); - double den_d = static_cast<double>(den_f); - - if (LIBC_UNLIKELY(max_abs >= 0x7f80'0000U || num_d == 0.0)) { - if (x_bits.is_nan() || y_bits.is_nan()) { - if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan()) - fputil::raise_except_if_required(FE_INVALID); - return FPBits::quiet_nan().get_val(); - } - double x_d = static_cast<double>(x); - double y_d = static_cast<double>(y); - size_t x_except = (x_d == 0.0) ? 0 : (x_abs == 0x7f80'0000 ? 2 : 1); - size_t y_except = (y_d == 0.0) ? 0 : (y_abs == 0x7f80'0000 ? 2 : 1); - - // Exceptional cases: - // EXCEPT[y_except][x_except][x_is_neg] - // with x_except & y_except: - // 0: zero - // 1: finite, non-zero - // 2: infinity - constexpr double EXCEPTS[3][3][2] = { - {{0.0, PI}, {0.0, PI}, {0.0, PI}}, - {{PI_OVER_2, PI_OVER_2}, {0.0, 0.0}, {0.0, PI}}, - {{PI_OVER_2, PI_OVER_2}, - {PI_OVER_2, PI_OVER_2}, - {PI_OVER_4, THREE_PI_OVER_4}}, - }; - - double r = IS_NEG[y_sign] * EXCEPTS[y_except][x_except][x_sign]; - - return static_cast<float>(r); - } - - bool recip = x_abs < y_abs; - double final_sign = IS_NEG[(x_sign != y_sign) != recip]; - fputil::DoubleDouble const_term = CONST_ADJ[x_sign][y_sign][recip]; - double q_d = num_d / den_d; - - double k_d = fputil::nearest_integer(q_d * 0x1.0p4); - int idx = static_cast<int>(k_d); - double r; - -#ifdef LIBC_MATH_HAS_SMALL_TABLES - double p = atan_eval_no_table(num_d, den_d, k_d * 0x1.0p-4); - r = final_sign * (p + (const_term.hi + ATAN_K_OVER_16[idx])); -#else - q_d = fputil::multiply_add(k_d, -0x1.0p-4, q_d); - - double p = atan_eval(q_d, idx); - r = final_sign * - fputil::multiply_add(q_d, p, const_term.hi + ATAN_COEFFS[idx][0]); -#endif // LIBC_MATH_HAS_SMALL_TABLES - -#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS - return static_cast<float>(r); -#else - constexpr uint32_t LOWER_ERR = 4; - // Mask sticky bits in double precision before rounding to single precision. - constexpr uint32_t MASK = - mask_trailing_ones<uint32_t, fputil::FPBits<double>::SIG_LEN - - FPBits::SIG_LEN - 1>(); - constexpr uint32_t UPPER_ERR = MASK - LOWER_ERR; - - uint32_t r_bits = static_cast<uint32_t>(cpp::bit_cast<uint64_t>(r)) & MASK; - - // Ziv's rounding test. - if (LIBC_LIKELY(r_bits > LOWER_ERR && r_bits < UPPER_ERR)) - return static_cast<float>(r); - - return atan2f_double_double(num_d, den_d, q_d, idx, k_d, final_sign, - const_term); -#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS + return math::atan2f(y, x); } } // namespace LIBC_NAMESPACE_DECL - -#endif diff --git a/libc/src/math/generic/atan2f128.cpp b/libc/src/math/generic/atan2f128.cpp index 8838d94..ec051dd 100644 --- a/libc/src/math/generic/atan2f128.cpp +++ b/libc/src/math/generic/atan2f128.cpp @@ -7,198 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/atan2f128.h" -#include "src/__support/FPUtil/FPBits.h" -#include "src/__support/FPUtil/dyadic_float.h" -#include "src/__support/FPUtil/multiply_add.h" -#include "src/__support/FPUtil/nearest_integer.h" -#include "src/__support/integer_literals.h" -#include "src/__support/macros/config.h" -#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY -#include "src/__support/macros/properties/types.h" -#include "src/__support/math/atan_utils.h" -#include "src/__support/uint128.h" +#include "src/__support/math/atan2f128.h" namespace LIBC_NAMESPACE_DECL { -namespace { - -using Float128 = fputil::DyadicFloat<128>; - -static constexpr Float128 ZERO = {Sign::POS, 0, 0_u128}; -static constexpr Float128 MZERO = {Sign::NEG, 0, 0_u128}; -static constexpr Float128 PI = {Sign::POS, -126, - 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; -static constexpr Float128 MPI = {Sign::NEG, -126, - 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; -static constexpr Float128 PI_OVER_2 = { - Sign::POS, -127, 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; -static constexpr Float128 MPI_OVER_2 = { - Sign::NEG, -127, 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; -static constexpr Float128 PI_OVER_4 = { - Sign::POS, -128, 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128}; -static constexpr Float128 THREE_PI_OVER_4 = { - Sign::POS, -128, 0x96cbe3f9'990e91a7'9394c9e8'a0a5159d_u128}; - -// Adjustment for constant term: -// CONST_ADJ[x_sign][y_sign][recip] -static constexpr Float128 CONST_ADJ[2][2][2] = { - {{ZERO, MPI_OVER_2}, {MZERO, MPI_OVER_2}}, - {{MPI, PI_OVER_2}, {MPI, PI_OVER_2}}}; - -} // anonymous namespace - -// There are several range reduction steps we can take for atan2(y, x) as -// follow: - -// * Range reduction 1: signness -// atan2(y, x) will return a number between -PI and PI representing the angle -// forming by the 0x axis and the vector (x, y) on the 0xy-plane. -// In particular, we have that: -// atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant) -// = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant) -// = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant) -// = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant) -// Since atan function is odd, we can use the formula: -// atan(-u) = -atan(u) -// to adjust the above conditions a bit further: -// atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant) -// = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant) -// = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant) -// = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant) -// Which can be simplified to: -// atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0 -// = sign(y) * (pi - atan( |y|/|x| )) if x < 0 - -// * Range reduction 2: reciprocal -// Now that the argument inside atan is positive, we can use the formula: -// atan(1/x) = pi/2 - atan(x) -// to make the argument inside atan <= 1 as follow: -// atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x -// = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y| -// = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x -// = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y| - -// * Range reduction 3: look up table. -// After the previous two range reduction steps, we reduce the problem to -// compute atan(u) with 0 <= u <= 1, or to be precise: -// atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|). -// An accurate polynomial approximation for the whole [0, 1] input range will -// require a very large degree. To make it more efficient, we reduce the input -// range further by finding an integer idx such that: -// | n/d - idx/64 | <= 1/128. -// In particular, -// idx := round(2^6 * n/d) -// Then for the fast pass, we find a polynomial approximation for: -// atan( n/d ) ~ atan( idx/64 ) + (n/d - idx/64) * Q(n/d - idx/64) -// For the accurate pass, we use the addition formula: -// atan( n/d ) - atan( idx/64 ) = atan( (n/d - idx/64)/(1 + (n*idx)/(64*d)) ) -// = atan( (n - d*(idx/64))/(d + n*(idx/64)) ) -// And for the fast pass, we use degree-13 minimax polynomial to compute the -// RHS: -// atan(u) ~ P(u) = u - c_3 * u^3 + c_5 * u^5 - c_7 * u^7 + c_9 *u^9 - -// - c_11 * u^11 + c_13 * u^13 -// with absolute errors bounded by: -// |atan(u) - P(u)| < 2^-121 -// and relative errors bounded by: -// |(atan(u) - P(u)) / P(u)| < 2^-114. - LLVM_LIBC_FUNCTION(float128, atan2f128, (float128 y, float128 x)) { - using namespace atan_internal; - using FPBits = fputil::FPBits<float128>; - using Float128 = fputil::DyadicFloat<128>; - - FPBits x_bits(x), y_bits(y); - bool x_sign = x_bits.sign().is_neg(); - bool y_sign = y_bits.sign().is_neg(); - x_bits = x_bits.abs(); - y_bits = y_bits.abs(); - UInt128 x_abs = x_bits.uintval(); - UInt128 y_abs = y_bits.uintval(); - bool recip = x_abs < y_abs; - UInt128 min_abs = recip ? x_abs : y_abs; - UInt128 max_abs = !recip ? x_abs : y_abs; - unsigned min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN); - unsigned max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN); - - Float128 num(FPBits(min_abs).get_val()); - Float128 den(FPBits(max_abs).get_val()); - - // Check for exceptional cases, whether inputs are 0, inf, nan, or close to - // overflow, or close to underflow. - if (LIBC_UNLIKELY(max_exp >= 0x7fffU || min_exp == 0U)) { - if (x_bits.is_nan() || y_bits.is_nan()) - return FPBits::quiet_nan().get_val(); - unsigned x_except = x == 0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1); - unsigned y_except = y == 0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1); - - // Exceptional cases: - // EXCEPT[y_except][x_except][x_is_neg] - // with x_except & y_except: - // 0: zero - // 1: finite, non-zero - // 2: infinity - constexpr Float128 EXCEPTS[3][3][2] = { - {{ZERO, PI}, {ZERO, PI}, {ZERO, PI}}, - {{PI_OVER_2, PI_OVER_2}, {ZERO, ZERO}, {ZERO, PI}}, - {{PI_OVER_2, PI_OVER_2}, - {PI_OVER_2, PI_OVER_2}, - {PI_OVER_4, THREE_PI_OVER_4}}, - }; - - if ((x_except != 1) || (y_except != 1)) { - Float128 r = EXCEPTS[y_except][x_except][x_sign]; - if (y_sign) - r.sign = r.sign.negate(); - return static_cast<float128>(r); - } - } - - bool final_sign = ((x_sign != y_sign) != recip); - Float128 const_term = CONST_ADJ[x_sign][y_sign][recip]; - int exp_diff = den.exponent - num.exponent; - // We have the following bound for normalized n and d: - // 2^(-exp_diff - 1) < n/d < 2^(-exp_diff + 1). - if (LIBC_UNLIKELY(exp_diff > FPBits::FRACTION_LEN + 2)) { - if (final_sign) - const_term.sign = const_term.sign.negate(); - return static_cast<float128>(const_term); - } - - // Take 24 leading bits of num and den to convert to float for fast division. - // We also multiply the numerator by 64 using integer addition directly to the - // exponent field. - float num_f = - cpp::bit_cast<float>(static_cast<uint32_t>(num.mantissa >> 104) + - (6U << fputil::FPBits<float>::FRACTION_LEN)); - float den_f = cpp::bit_cast<float>( - static_cast<uint32_t>(den.mantissa >> 104) + - (static_cast<uint32_t>(exp_diff) << fputil::FPBits<float>::FRACTION_LEN)); - - float k = fputil::nearest_integer(num_f / den_f); - unsigned idx = static_cast<unsigned>(k); - - // k_f128 = idx / 64 - Float128 k_f128(Sign::POS, -6, Float128::MantissaType(idx)); - - // Range reduction: - // atan(n/d) - atan(k) = atan((n/d - k/64) / (1 + (n/d) * (k/64))) - // = atan((n - d * k/64)) / (d + n * k/64)) - // num_f128 = n - d * k/64 - Float128 num_f128 = fputil::multiply_add(den, -k_f128, num); - // den_f128 = d + n * k/64 - Float128 den_f128 = fputil::multiply_add(num, k_f128, den); - - // q = (n - d * k) / (d + n * k) - Float128 q = fputil::quick_mul(num_f128, fputil::approx_reciprocal(den_f128)); - // p ~ atan(q) - Float128 p = atan_eval(q); - - Float128 r = - fputil::quick_add(const_term, fputil::quick_add(ATAN_I_F128[idx], p)); - if (final_sign) - r.sign = r.sign.negate(); - - return static_cast<float128>(r); + return math::atan2f128(y, x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/atanhf.cpp b/libc/src/math/generic/atanhf.cpp index 602a8f0..81706190 100644 --- a/libc/src/math/generic/atanhf.cpp +++ b/libc/src/math/generic/atanhf.cpp @@ -7,62 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/math/atanhf.h" -#include "src/__support/FPUtil/FEnvImpl.h" -#include "src/__support/FPUtil/FPBits.h" -#include "src/__support/macros/config.h" -#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY -#include "src/math/generic/explogxf.h" +#include "src/__support/math/atanhf.h" namespace LIBC_NAMESPACE_DECL { -LLVM_LIBC_FUNCTION(float, atanhf, (float x)) { - using namespace acoshf_internal; - using FPBits = typename fputil::FPBits<float>; - - FPBits xbits(x); - Sign sign = xbits.sign(); - uint32_t x_abs = xbits.abs().uintval(); - - // |x| >= 1.0 - if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) { - if (xbits.is_nan()) { - if (xbits.is_signaling_nan()) { - fputil::raise_except_if_required(FE_INVALID); - return FPBits::quiet_nan().get_val(); - } - return x; - } - // |x| == 1.0 - if (x_abs == 0x3F80'0000U) { - fputil::set_errno_if_required(ERANGE); - fputil::raise_except_if_required(FE_DIVBYZERO); - return FPBits::inf(sign).get_val(); - } else { - fputil::set_errno_if_required(EDOM); - fputil::raise_except_if_required(FE_INVALID); - return FPBits::quiet_nan().get_val(); - } - } - - // |x| < ~0.10 - if (LIBC_UNLIKELY(x_abs <= 0x3dcc'0000U)) { - // |x| <= 2^-26 - if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) { - return static_cast<float>(LIBC_UNLIKELY(x_abs == 0) - ? x - : (x + 0x1.5555555555555p-2 * x * x * x)); - } - - double xdbl = x; - double x2 = xdbl * xdbl; - // Pure Taylor series. - double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2, - 0x1.999999999999ap-3, 0x1.2492492492492p-3, - 0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4); - return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl)); - } - double xdbl = x; - return static_cast<float>(0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0))); -} +LLVM_LIBC_FUNCTION(float, atanhf, (float x)) { return math::atanhf(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/atanhf16.cpp b/libc/src/math/generic/atanhf16.cpp index 57885ac..0539bac 100644 --- a/libc/src/math/generic/atanhf16.cpp +++ b/libc/src/math/generic/atanhf16.cpp @@ -7,92 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/math/atanhf16.h" -#include "explogxf.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/PolyEval.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/common.h" -#include "src/__support/macros/config.h" -#include "src/__support/macros/optimization.h" +#include "src/__support/math/atanhf16.h" namespace LIBC_NAMESPACE_DECL { -static constexpr size_t N_EXCEPTS = 1; -static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ATANHF16_EXCEPTS{{ - // (input, RZ output, RU offset, RD offset, RN offset) - // x = 0x1.a5cp-4, atanhf16(x) = 0x1.a74p-4 (RZ) - {0x2E97, 0x2E9D, 1, 0, 0}, -}}; - -LLVM_LIBC_FUNCTION(float16, atanhf16, (float16 x)) { - using FPBits = fputil::FPBits<float16>; - - FPBits xbits(x); - Sign sign = xbits.sign(); - uint16_t x_abs = xbits.abs().uintval(); - - // |x| >= 1 - if (LIBC_UNLIKELY(x_abs >= 0x3c00U)) { - if (xbits.is_nan()) { - if (xbits.is_signaling_nan()) { - fputil::raise_except_if_required(FE_INVALID); - return FPBits::quiet_nan().get_val(); - } - return x; - } - - // |x| == 1.0 - if (x_abs == 0x3c00U) { - fputil::set_errno_if_required(ERANGE); - fputil::raise_except_if_required(FE_DIVBYZERO); - return FPBits::inf(sign).get_val(); - } - // |x| > 1.0 - fputil::set_errno_if_required(EDOM); - fputil::raise_except_if_required(FE_INVALID); - return FPBits::quiet_nan().get_val(); - } - - if (auto r = ATANHF16_EXCEPTS.lookup(xbits.uintval()); - LIBC_UNLIKELY(r.has_value())) - return r.value(); - - // For |x| less than approximately 0.24 - if (LIBC_UNLIKELY(x_abs <= 0x33f3U)) { - // atanh(+/-0) = +/-0 - if (LIBC_UNLIKELY(x_abs == 0U)) - return x; - // The Taylor expansion of atanh(x) is: - // atanh(x) = x + x^3/3 + x^5/5 + x^7/7 + x^9/9 + x^11/11 - // = x * [1 + x^2/3 + x^4/5 + x^6/7 + x^8/9 + x^10/11] - // When |x| < 2^-5 (0x0800U), this can be approximated by: - // atanh(x) ≈ x + (1/3)*x^3 - if (LIBC_UNLIKELY(x_abs < 0x0800U)) { - float xf = x; - return fputil::cast<float16>(xf + 0x1.555556p-2f * xf * xf * xf); - } - - // For 2^-5 <= |x| <= 0x1.fccp-3 (~0.24): - // Let t = x^2. - // Define P(t) ≈ (1/3)*t + (1/5)*t^2 + (1/7)*t^3 + (1/9)*t^4 + (1/11)*t^5. - // Coefficients (from Sollya, RN, hexadecimal): - // 1/3 = 0x1.555556p-2, 1/5 = 0x1.99999ap-3, 1/7 = 0x1.24924ap-3, - // 1/9 = 0x1.c71c72p-4, 1/11 = 0x1.745d18p-4 - // Thus, atanh(x) ≈ x * (1 + P(x^2)). - float xf = x; - float x2 = xf * xf; - float pe = fputil::polyeval(x2, 0.0f, 0x1.555556p-2f, 0x1.99999ap-3f, - 0x1.24924ap-3f, 0x1.c71c72p-4f, 0x1.745d18p-4f); - return fputil::cast<float16>(fputil::multiply_add(xf, pe, xf)); - } - - float xf = x; - return fputil::cast<float16>(0.5 * log_eval_f((xf + 1.0f) / (xf - 1.0f))); -} +LLVM_LIBC_FUNCTION(float16, atanhf16, (float16 x)) { return math::atanhf16(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/common_constants.cpp b/libc/src/math/generic/common_constants.cpp index 42e3ff0..2a15df2 100644 --- a/libc/src/math/generic/common_constants.cpp +++ b/libc/src/math/generic/common_constants.cpp @@ -12,84 +12,6 @@ namespace LIBC_NAMESPACE_DECL { -// Lookup table for logf(f) = logf(1 + n*2^(-7)) where n = 0..127, -// computed and stored as float precision constants. -// Generated by Sollya with the following commands: -// display = hexadecimal; -// for n from 0 to 127 do { print(single(1 / (1 + n / 128.0))); }; -const float ONE_OVER_F_FLOAT[128] = { - 0x1p0f, 0x1.fc07fp-1f, 0x1.f81f82p-1f, 0x1.f4465ap-1f, - 0x1.f07c2p-1f, 0x1.ecc07cp-1f, 0x1.e9131ap-1f, 0x1.e573acp-1f, - 0x1.e1e1e2p-1f, 0x1.de5d6ep-1f, 0x1.dae608p-1f, 0x1.d77b66p-1f, - 0x1.d41d42p-1f, 0x1.d0cb58p-1f, 0x1.cd8568p-1f, 0x1.ca4b3p-1f, - 0x1.c71c72p-1f, 0x1.c3f8fp-1f, 0x1.c0e07p-1f, 0x1.bdd2b8p-1f, - 0x1.bacf92p-1f, 0x1.b7d6c4p-1f, 0x1.b4e81cp-1f, 0x1.b20364p-1f, - 0x1.af286cp-1f, 0x1.ac5702p-1f, 0x1.a98ef6p-1f, 0x1.a6d01ap-1f, - 0x1.a41a42p-1f, 0x1.a16d4p-1f, 0x1.9ec8eap-1f, 0x1.9c2d14p-1f, - 0x1.99999ap-1f, 0x1.970e5p-1f, 0x1.948b1p-1f, 0x1.920fb4p-1f, - 0x1.8f9c18p-1f, 0x1.8d3018p-1f, 0x1.8acb9p-1f, 0x1.886e6p-1f, - 0x1.861862p-1f, 0x1.83c978p-1f, 0x1.818182p-1f, 0x1.7f406p-1f, - 0x1.7d05f4p-1f, 0x1.7ad22p-1f, 0x1.78a4c8p-1f, 0x1.767dcep-1f, - 0x1.745d18p-1f, 0x1.724288p-1f, 0x1.702e06p-1f, 0x1.6e1f76p-1f, - 0x1.6c16c2p-1f, 0x1.6a13cep-1f, 0x1.681682p-1f, 0x1.661ec6p-1f, - 0x1.642c86p-1f, 0x1.623fa8p-1f, 0x1.605816p-1f, 0x1.5e75bcp-1f, - 0x1.5c9882p-1f, 0x1.5ac056p-1f, 0x1.58ed24p-1f, 0x1.571ed4p-1f, - 0x1.555556p-1f, 0x1.539094p-1f, 0x1.51d07ep-1f, 0x1.501502p-1f, - 0x1.4e5e0ap-1f, 0x1.4cab88p-1f, 0x1.4afd6ap-1f, 0x1.49539ep-1f, - 0x1.47ae14p-1f, 0x1.460cbcp-1f, 0x1.446f86p-1f, 0x1.42d662p-1f, - 0x1.414142p-1f, 0x1.3fb014p-1f, 0x1.3e22ccp-1f, 0x1.3c995ap-1f, - 0x1.3b13b2p-1f, 0x1.3991c2p-1f, 0x1.381382p-1f, 0x1.3698ep-1f, - 0x1.3521dp-1f, 0x1.33ae46p-1f, 0x1.323e34p-1f, 0x1.30d19p-1f, - 0x1.2f684cp-1f, 0x1.2e025cp-1f, 0x1.2c9fb4p-1f, 0x1.2b404ap-1f, - 0x1.29e412p-1f, 0x1.288b02p-1f, 0x1.27350cp-1f, 0x1.25e228p-1f, - 0x1.24924ap-1f, 0x1.234568p-1f, 0x1.21fb78p-1f, 0x1.20b47p-1f, - 0x1.1f7048p-1f, 0x1.1e2ef4p-1f, 0x1.1cf06ap-1f, 0x1.1bb4a4p-1f, - 0x1.1a7b96p-1f, 0x1.194538p-1f, 0x1.181182p-1f, 0x1.16e068p-1f, - 0x1.15b1e6p-1f, 0x1.1485fp-1f, 0x1.135c82p-1f, 0x1.12358ep-1f, - 0x1.111112p-1f, 0x1.0fef02p-1f, 0x1.0ecf56p-1f, 0x1.0db20ap-1f, - 0x1.0c9714p-1f, 0x1.0b7e6ep-1f, 0x1.0a681p-1f, 0x1.0953f4p-1f, - 0x1.08421p-1f, 0x1.07326p-1f, 0x1.0624dep-1f, 0x1.05198p-1f, - 0x1.041042p-1f, 0x1.03091cp-1f, 0x1.020408p-1f, 0x1.010102p-1f}; - -// Lookup table for log(f) = log(1 + n*2^(-7)) where n = 0..127, -// computed and stored as float precision constants. -// Generated by Sollya with the following commands: -// display = hexadecimal; -// for n from 0 to 127 do { print(single(log(1 + n / 128.0))); }; -const float LOG_F_FLOAT[128] = { - 0.0f, 0x1.fe02a6p-8f, 0x1.fc0a8cp-7f, 0x1.7b91bp-6f, - 0x1.f829bp-6f, 0x1.39e87cp-5f, 0x1.77459p-5f, 0x1.b42dd8p-5f, - 0x1.f0a30cp-5f, 0x1.16536ep-4f, 0x1.341d7ap-4f, 0x1.51b074p-4f, - 0x1.6f0d28p-4f, 0x1.8c345ep-4f, 0x1.a926d4p-4f, 0x1.c5e548p-4f, - 0x1.e27076p-4f, 0x1.fec914p-4f, 0x1.0d77e8p-3f, 0x1.1b72aep-3f, - 0x1.29553p-3f, 0x1.371fc2p-3f, 0x1.44d2b6p-3f, 0x1.526e5ep-3f, - 0x1.5ff308p-3f, 0x1.6d60fep-3f, 0x1.7ab89p-3f, 0x1.87fa06p-3f, - 0x1.9525aap-3f, 0x1.a23bc2p-3f, 0x1.af3c94p-3f, 0x1.bc2868p-3f, - 0x1.c8ff7cp-3f, 0x1.d5c216p-3f, 0x1.e27076p-3f, 0x1.ef0adcp-3f, - 0x1.fb9186p-3f, 0x1.04025ap-2f, 0x1.0a324ep-2f, 0x1.1058cp-2f, - 0x1.1675cap-2f, 0x1.1c898cp-2f, 0x1.22942p-2f, 0x1.2895a2p-2f, - 0x1.2e8e2cp-2f, 0x1.347ddap-2f, 0x1.3a64c6p-2f, 0x1.404308p-2f, - 0x1.4618bcp-2f, 0x1.4be5fap-2f, 0x1.51aad8p-2f, 0x1.576772p-2f, - 0x1.5d1bdcp-2f, 0x1.62c83p-2f, 0x1.686c82p-2f, 0x1.6e08eap-2f, - 0x1.739d8p-2f, 0x1.792a56p-2f, 0x1.7eaf84p-2f, 0x1.842d1ep-2f, - 0x1.89a338p-2f, 0x1.8f11e8p-2f, 0x1.947942p-2f, 0x1.99d958p-2f, - 0x1.9f323ep-2f, 0x1.a4840ap-2f, 0x1.a9cecap-2f, 0x1.af1294p-2f, - 0x1.b44f78p-2f, 0x1.b9858ap-2f, 0x1.beb4dap-2f, 0x1.c3dd7ap-2f, - 0x1.c8ff7cp-2f, 0x1.ce1afp-2f, 0x1.d32fe8p-2f, 0x1.d83e72p-2f, - 0x1.dd46ap-2f, 0x1.e24882p-2f, 0x1.e74426p-2f, 0x1.ec399ep-2f, - 0x1.f128f6p-2f, 0x1.f6124p-2f, 0x1.faf588p-2f, 0x1.ffd2ep-2f, - 0x1.02552ap-1f, 0x1.04bdfap-1f, 0x1.0723e6p-1f, 0x1.0986f4p-1f, - 0x1.0be72ep-1f, 0x1.0e4498p-1f, 0x1.109f3ap-1f, 0x1.12f71ap-1f, - 0x1.154c3ep-1f, 0x1.179eacp-1f, 0x1.19ee6cp-1f, 0x1.1c3b82p-1f, - 0x1.1e85f6p-1f, 0x1.20cdcep-1f, 0x1.23130ep-1f, 0x1.2555bcp-1f, - 0x1.2795e2p-1f, 0x1.29d38p-1f, 0x1.2c0e9ep-1f, 0x1.2e4744p-1f, - 0x1.307d74p-1f, 0x1.32b134p-1f, 0x1.34e28ap-1f, 0x1.37117cp-1f, - 0x1.393e0ep-1f, 0x1.3b6844p-1f, 0x1.3d9026p-1f, 0x1.3fb5b8p-1f, - 0x1.41d8fep-1f, 0x1.43f9fep-1f, 0x1.4618bcp-1f, 0x1.48353ep-1f, - 0x1.4a4f86p-1f, 0x1.4c679ap-1f, 0x1.4e7d82p-1f, 0x1.50913cp-1f, - 0x1.52a2d2p-1f, 0x1.54b246p-1f, 0x1.56bf9ep-1f, 0x1.58cadcp-1f, - 0x1.5ad404p-1f, 0x1.5cdb1ep-1f, 0x1.5ee02ap-1f, 0x1.60e33p-1f}; - // Range reduction constants for logarithms. // r(0) = 1, r(127) = 0.5 // r(k) = 2^-8 * ceil(2^8 * (1 - 2^-8) / (1 + k*2^-7)) diff --git a/libc/src/math/generic/common_constants.h b/libc/src/math/generic/common_constants.h index 72b1d564..9ee31f0 100644 --- a/libc/src/math/generic/common_constants.h +++ b/libc/src/math/generic/common_constants.h @@ -17,14 +17,6 @@ namespace LIBC_NAMESPACE_DECL { -// Lookup table for (1/f) where f = 1 + n*2^(-7), n = 0..127, -// computed and stored as float precision constants. -extern const float ONE_OVER_F_FLOAT[128]; - -// Lookup table for log(f) = log(1 + n*2^(-7)) where n = 0..127, -// computed and stored as float precision constants. -extern const float LOG_F_FLOAT[128]; - // Lookup table for range reduction constants r for logarithms. extern const float R[128]; diff --git a/libc/src/math/generic/explogxf.h b/libc/src/math/generic/explogxf.h index a2a6d60..72f8da8 100644 --- a/libc/src/math/generic/explogxf.h +++ b/libc/src/math/generic/explogxf.h @@ -121,49 +121,6 @@ template <bool is_sinh> LIBC_INLINE double exp_pm_eval(float x) { return r; } -// x should be positive, normal finite value -// TODO: Simplify range reduction and polynomial degree for float16. -// See issue #137190. -LIBC_INLINE static float log_eval_f(float x) { - // For x = 2^ex * (1 + mx), logf(x) = ex * logf(2) + logf(1 + mx). - using FPBits = fputil::FPBits<float>; - FPBits xbits(x); - - float ex = static_cast<float>(xbits.get_exponent()); - // p1 is the leading 7 bits of mx, i.e. - // p1 * 2^(-7) <= m_x < (p1 + 1) * 2^(-7). - int p1 = static_cast<int>(xbits.get_mantissa() >> (FPBits::FRACTION_LEN - 7)); - - // Set bits to (1 + (mx - p1*2^(-7))) - xbits.set_uintval(xbits.uintval() & (FPBits::FRACTION_MASK >> 7)); - xbits.set_biased_exponent(FPBits::EXP_BIAS); - // dx = (mx - p1*2^(-7)) / (1 + p1*2^(-7)). - float dx = (xbits.get_val() - 1.0f) * ONE_OVER_F_FLOAT[p1]; - - // Minimax polynomial for log(1 + dx), generated using Sollya: - // > P = fpminimax(log(1 + x)/x, 6, [|SG...|], [0, 2^-7]); - // > Q = (P - 1) / x; - // > for i from 0 to degree(Q) do print(coeff(Q, i)); - constexpr float COEFFS[6] = {-0x1p-1f, 0x1.555556p-2f, -0x1.00022ep-2f, - 0x1.9ea056p-3f, -0x1.e50324p-2f, 0x1.c018fp3f}; - - float dx2 = dx * dx; - - float c1 = fputil::multiply_add(dx, COEFFS[1], COEFFS[0]); - float c2 = fputil::multiply_add(dx, COEFFS[3], COEFFS[2]); - float c3 = fputil::multiply_add(dx, COEFFS[5], COEFFS[4]); - - float p = fputil::polyeval(dx2, dx, c1, c2, c3); - - // Generated by Sollya with the following commands: - // > display = hexadecimal; - // > round(log(2), SG, RN); - constexpr float LOGF_2 = 0x1.62e43p-1f; - - float result = fputil::multiply_add(ex, LOGF_2, LOG_F_FLOAT[p1] + p); - return result; -} - } // namespace LIBC_NAMESPACE_DECL #endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt index bb50002..ceb755f 100644 --- a/libc/src/sched/linux/CMakeLists.txt +++ b/libc/src/sched/linux/CMakeLists.txt @@ -5,7 +5,6 @@ add_entrypoint_object( HDRS ../getcpu.h DEPENDS - libc.include.sched libc.src.__support.OSUtil.osutil libc.src.errno.errno ) @@ -69,9 +68,10 @@ add_entrypoint_object( HDRS ../sched_setparam.h DEPENDS + libc.hdr.types.pid_t + libc.hdr.types.struct_sched_param libc.include.sys_syscall libc.include.time - libc.include.sched libc.src.__support.OSUtil.osutil libc.src.errno.errno ) @@ -83,9 +83,10 @@ add_entrypoint_object( HDRS ../sched_getparam.h DEPENDS + libc.hdr.types.pid_t + libc.hdr.types.struct_sched_param libc.include.sys_syscall libc.include.time - libc.include.sched libc.src.__support.OSUtil.osutil libc.src.errno.errno ) @@ -97,9 +98,10 @@ add_entrypoint_object( HDRS ../sched_setscheduler.h DEPENDS + libc.hdr.types.pid_t + libc.hdr.types.struct_sched_param libc.include.sys_syscall libc.include.time - libc.include.sched libc.src.__support.OSUtil.osutil libc.src.errno.errno ) diff --git a/libc/src/sched/sched_getparam.h b/libc/src/sched/sched_getparam.h index e1b2365..00defdf 100644 --- a/libc/src/sched/sched_getparam.h +++ b/libc/src/sched/sched_getparam.h @@ -10,7 +10,9 @@ #define LLVM_LIBC_SRC_SCHED_SCHED_GETPARAM_H #include "src/__support/macros/config.h" -#include <sched.h> + +#include "hdr/types/pid_t.h" +#include "hdr/types/struct_sched_param.h" namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/sched/sched_setparam.h b/libc/src/sched/sched_setparam.h index e4691a7..5a69b09 100644 --- a/libc/src/sched/sched_setparam.h +++ b/libc/src/sched/sched_setparam.h @@ -10,7 +10,9 @@ #define LLVM_LIBC_SRC_SCHED_SCHED_SETPARAM_H #include "src/__support/macros/config.h" -#include <sched.h> + +#include "hdr/types/pid_t.h" +#include "hdr/types/struct_sched_param.h" namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/sched/sched_setscheduler.h b/libc/src/sched/sched_setscheduler.h index e745002..c5cb148 100644 --- a/libc/src/sched/sched_setscheduler.h +++ b/libc/src/sched/sched_setscheduler.h @@ -10,7 +10,9 @@ #define LLVM_LIBC_SRC_SCHED_SCHED_SETSCHEDULER_H #include "src/__support/macros/config.h" -#include <sched.h> + +#include "hdr/types/pid_t.h" +#include "hdr/types/struct_sched_param.h" namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/wchar/wcstok.cpp b/libc/src/wchar/wcstok.cpp index ed4f0aa..85513a6 100644 --- a/libc/src/wchar/wcstok.cpp +++ b/libc/src/wchar/wcstok.cpp @@ -27,17 +27,22 @@ LLVM_LIBC_FUNCTION(wchar_t *, wcstok, wchar_t *tok_start = str; while (*tok_start != L'\0' && internal::wcschr(delims, *tok_start)) ++tok_start; + if (*tok_start == L'\0') { + *context = nullptr; + return nullptr; + } wchar_t *tok_end = tok_start; while (*tok_end != L'\0' && !internal::wcschr(delims, *tok_end)) ++tok_end; - if (*tok_end != L'\0') { + if (*tok_end == L'\0') { + *context = nullptr; + } else { *tok_end = L'\0'; - ++tok_end; + *context = tok_end + 1; } - *context = tok_end; - return *tok_start == L'\0' ? nullptr : tok_start; + return tok_start; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/startup/baremetal/CMakeLists.txt b/libc/startup/baremetal/CMakeLists.txt index 276fe33..e361000 100644 --- a/libc/startup/baremetal/CMakeLists.txt +++ b/libc/startup/baremetal/CMakeLists.txt @@ -1,3 +1,34 @@ +# TODO: Use generic "add_startup_object" https://github.com/llvm/llvm-project/issues/133156 +function(add_startup_object name) + cmake_parse_arguments( + "ADD_STARTUP_OBJECT" + "ALIAS" # Option argument + "SRC" # Single value arguments + "DEPENDS;COMPILE_OPTIONS" # Multi value arguments + ${ARGN} + ) + + get_fq_target_name(${name} fq_target_name) + if(ADD_STARTUP_OBJECT_ALIAS) + get_fq_deps_list(fq_dep_list ${ADD_STARTUP_OBJECT_DEPENDS}) + add_library(${fq_target_name} ALIAS ${fq_dep_list}) + return() + endif() + + add_object_library( + ${name} + SRCS ${ADD_STARTUP_OBJECT_SRC} + COMPILE_OPTIONS ${ADD_STARTUP_OBJECT_COMPILE_OPTIONS} + ${ADD_STARTUP_OBJECT_UNPARSED_ARGUMENTS} + DEPENDS ${ADD_STARTUP_OBJECT_DEPENDS} + ) + set_target_properties( + ${fq_target_name} + PROPERTIES + OUTPUT_NAME ${name}.o + ) +endfunction() + add_entrypoint_object( init SRCS @@ -5,6 +36,8 @@ add_entrypoint_object( DEPENDS libc.hdr.stdint_proxy libc.src.__support.common + HDRS + init.h ) add_entrypoint_object( @@ -14,4 +47,29 @@ add_entrypoint_object( DEPENDS libc.hdr.stdint_proxy libc.src.__support.common + HDRS + fini.h ) + +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE}) + add_subdirectory(${LIBC_TARGET_ARCHITECTURE}) +else() + message(WARNING "Cannot build 'crt1.o' for ${LIBC_TARGET_ARCHITECTURE} yet.") + return() +endif() + +add_startup_object( + crt1 + ALIAS + DEPENDS + .${LIBC_TARGET_ARCHITECTURE}.crt1 +) + +add_custom_target(libc-startup) + +set(fq_target_name libc.startup.baremetal.${LIBC_TARGET_ARCHITECTURE}.crt1) +add_dependencies(libc-startup ${fq_target_name}) +install(FILES $<TARGET_OBJECTS:${fq_target_name}> + DESTINATION ${LIBC_INSTALL_LIBRARY_DIR} + RENAME $<TARGET_PROPERTY:${fq_target_name},OUTPUT_NAME> + COMPONENT libc) diff --git a/libc/startup/baremetal/arm/CMakeLists.txt b/libc/startup/baremetal/arm/CMakeLists.txt new file mode 100644 index 0000000..f75bd89 --- /dev/null +++ b/libc/startup/baremetal/arm/CMakeLists.txt @@ -0,0 +1,16 @@ +add_startup_object( + crt1 + SRC + start.cpp + DEPENDS + libc.src.stdlib.atexit + libc.src.stdlib.exit + libc.src.string.memcpy + libc.src.string.memset + libc.startup.baremetal.init + libc.startup.baremetal.fini + COMPILE_OPTIONS + -ffreestanding # To avoid compiler warnings about calling the main function. + -fno-builtin + -Wno-global-constructors # To allow vector table initialization +) diff --git a/libc/startup/baremetal/arm/start.cpp b/libc/startup/baremetal/arm/start.cpp new file mode 100644 index 0000000..123efc4 --- /dev/null +++ b/libc/startup/baremetal/arm/start.cpp @@ -0,0 +1,92 @@ +//===-- Implementation of crt for arm -------------------------------------===// +// +// 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/macros/config.h" +#include "src/stdlib/atexit.h" +#include "src/stdlib/exit.h" +#include "src/string/memcpy.h" +#include "src/string/memset.h" +#include "startup/baremetal/fini.h" +#include "startup/baremetal/init.h" + +#include <stdint.h> + +extern "C" { +int main(int argc, char **argv); +void _start(); + +// Semihosting library initialisation if applicable. Required for printf, etc. +[[gnu::weak]] void _platform_init() {} + +// These symbols are provided by the linker. The exact names are not defined by +// a standard. +extern uintptr_t __stack; +extern uintptr_t __data_source[]; +extern uintptr_t __data_start[]; +extern uintptr_t __data_size[]; +extern uintptr_t __bss_start[]; +extern uintptr_t __bss_size[]; + +// Based on +// https://developer.arm.com/documentation/107565/0101/Use-case-examples/Generic-Information/What-is-inside-a-program-image-/Vector-table +void NMI_Handler() {} +void HardFault_Handler() { LIBC_NAMESPACE::exit(1); } +void MemManage_Handler() { LIBC_NAMESPACE::exit(1); } +void BusFault_Handler() { LIBC_NAMESPACE::exit(1); } +void UsageFault_Handler() { LIBC_NAMESPACE::exit(1); } +void SVC_Handler() {} +void DebugMon_Handler() {} +void PendSV_Handler() {} +void SysTick_Handler() {} + +// Architecturally the bottom 7 bits of VTOR are zero, meaning the vector table +// has to be 128-byte aligned, however an implementation can require more bits +// to be zero and Cortex-M23 can require up to 10, so 1024-byte align the vector +// table. +using HandlerType = void (*)(void); +const HandlerType vector_table[] + __attribute__((section(".vectors"), aligned(1024), used)) = { + (HandlerType)&__stack, // SP + _start, // Reset + NMI_Handler, // NMI Handler + HardFault_Handler, // Hard Fault Handlerß + MemManage_Handler, // MPU Fault Han`dler + BusFault_Handler, // Bus Fault Handler + UsageFault_Handler, // Usage Fault Handler + 0, // Reserved + 0, // Reserved + 0, // Reserved + 0, // Reserved + SVC_Handler, // SVC Handler + DebugMon_Handler, // Debug Monitor Handler + 0, // Reserved + PendSV_Handler, // PendSV Handler + SysTick_Handler, // SysTick Handler + // Unused +}; +} // extern "C" + +namespace LIBC_NAMESPACE_DECL { +[[noreturn]] void do_start() { + // FIXME: set up the QEMU test environment + + // Perform the equivalent of scatterloading + LIBC_NAMESPACE::memcpy(__data_start, __data_source, (uintptr_t)__data_size); + LIBC_NAMESPACE::memset(__bss_start, '\0', (uintptr_t)__bss_size); + __libc_init_array(); + + _platform_init(); + LIBC_NAMESPACE::atexit(&__libc_fini_array); + LIBC_NAMESPACE::exit(main(0, 0)); +} +} // namespace LIBC_NAMESPACE_DECL + +extern "C" void _start() { + asm volatile("mov sp, %0" : : "r"(&__stack)); + asm volatile("bl %0" : : "X"(LIBC_NAMESPACE::do_start)); +} diff --git a/libc/startup/baremetal/fini.cpp b/libc/startup/baremetal/fini.cpp index 64af842..66714e2 100644 --- a/libc/startup/baremetal/fini.cpp +++ b/libc/startup/baremetal/fini.cpp @@ -6,17 +6,13 @@ // //===----------------------------------------------------------------------===// -#include "hdr/stdint_proxy.h" +#include "startup/baremetal/fini.h" + #include "src/__support/macros/config.h" #include <stddef.h> namespace LIBC_NAMESPACE_DECL { -extern "C" { -extern uintptr_t __fini_array_start[]; -extern uintptr_t __fini_array_end[]; -} - using FiniCallback = void(void); extern "C" void __libc_fini_array(void) { diff --git a/libc/startup/baremetal/fini.h b/libc/startup/baremetal/fini.h new file mode 100644 index 0000000..74e9601 --- /dev/null +++ b/libc/startup/baremetal/fini.h @@ -0,0 +1,16 @@ +//===-- Implementation header of __libc_fini_array ------------------------===// +// +// 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/stdint_proxy.h" + +extern "C" { +extern uintptr_t __fini_array_start[]; +extern uintptr_t __fini_array_end[]; + +void __libc_fini_array(void); +} // extern "C" diff --git a/libc/startup/baremetal/init.cpp b/libc/startup/baremetal/init.cpp index 995609c..89065fd 100644 --- a/libc/startup/baremetal/init.cpp +++ b/libc/startup/baremetal/init.cpp @@ -6,19 +6,13 @@ // //===----------------------------------------------------------------------===// -#include "hdr/stdint_proxy.h" +#include "startup/baremetal/init.h" + #include "src/__support/macros/config.h" #include <stddef.h> namespace LIBC_NAMESPACE_DECL { -extern "C" { -extern uintptr_t __preinit_array_start[]; -extern uintptr_t __preinit_array_end[]; -extern uintptr_t __init_array_start[]; -extern uintptr_t __init_array_end[]; -} - using InitCallback = void(void); extern "C" void __libc_init_array(void) { diff --git a/libc/startup/baremetal/init.h b/libc/startup/baremetal/init.h new file mode 100644 index 0000000..6b545db3 --- /dev/null +++ b/libc/startup/baremetal/init.h @@ -0,0 +1,18 @@ +//===-- Implementation header of __libc_init_array ------------------------===// +// +// 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/stdint_proxy.h" + +extern "C" { +extern uintptr_t __preinit_array_start[]; +extern uintptr_t __preinit_array_end[]; +extern uintptr_t __init_array_start[]; +extern uintptr_t __init_array_end[]; + +void __libc_init_array(void); +} // extern "C" diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt index 4ed32d4..3d739c2 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -20,8 +20,12 @@ add_fp_unittest( libc.src.__support.math.asinhf16 libc.src.__support.math.atan libc.src.__support.math.atan2 + libc.src.__support.math.atan2f + libc.src.__support.math.atan2f128 libc.src.__support.math.atanf libc.src.__support.math.atanf16 + libc.src.__support.math.atanhf + libc.src.__support.math.atanhf16 libc.src.__support.math.erff libc.src.__support.math.exp libc.src.__support.math.exp10 diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp index cd72df4..6d3cf7c 100644 --- a/libc/test/shared/shared_math_test.cpp +++ b/libc/test/shared/shared_math_test.cpp @@ -20,6 +20,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) { EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinf16(0.0f16)); EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinhf16(0.0f16)); EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::atanf16(0.0f16)); + EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::atanhf16(0.0f16)); EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::exp10f16(0.0f16)); @@ -45,7 +46,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat) { EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::acoshf(1.0f)); EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::asinf(0.0f)); EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::asinhf(0.0f)); + EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atan2f(0.0f, 0.0f)); EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atanf(0.0f)); + EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atanhf(0.0f)); EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::erff(0.0f)); EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp10f(0.0f)); EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f)); @@ -72,6 +75,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) { TEST(LlvmLibcSharedMathTest, AllFloat128) { int exponent; + EXPECT_FP_EQ(float128(0x0p+0), + LIBC_NAMESPACE::shared::atan2f128(float128(0.0), float128(0.0))); EXPECT_FP_EQ_ALL_ROUNDING(float128(0.75), LIBC_NAMESPACE::shared::frexpf128( float128(24), &exponent)); EXPECT_EQ(exponent, 5); diff --git a/libc/test/src/sched/CMakeLists.txt b/libc/test/src/sched/CMakeLists.txt index f6151d0..362c526 100644 --- a/libc/test/src/sched/CMakeLists.txt +++ b/libc/test/src/sched/CMakeLists.txt @@ -48,7 +48,6 @@ add_libc_unittest( SRCS getcpu_test.cpp DEPENDS - libc.include.sched libc.src.errno.errno libc.src.sched.getcpu libc.test.UnitTest.ErrnoCheckingTest @@ -61,7 +60,8 @@ add_libc_unittest( SRCS param_and_scheduler_test.cpp DEPENDS - libc.include.sched + libc.hdr.sched_macros + libc.hdr.types.struct_sched_param libc.src.errno.errno libc.src.sched.sched_getscheduler libc.src.sched.sched_setscheduler @@ -79,6 +79,7 @@ add_libc_unittest( SRCS sched_rr_get_interval_test.cpp DEPENDS + libc.hdr.sched_macros libc.hdr.types.struct_timespec libc.src.errno.errno libc.src.sched.sched_getscheduler diff --git a/libc/test/src/sched/param_and_scheduler_test.cpp b/libc/test/src/sched/param_and_scheduler_test.cpp index 4f2b6e4..b8ee123 100644 --- a/libc/test/src/sched/param_and_scheduler_test.cpp +++ b/libc/test/src/sched/param_and_scheduler_test.cpp @@ -16,7 +16,8 @@ #include "src/unistd/getuid.h" #include "test/UnitTest/Test.h" -#include <sched.h> +#include "hdr/sched_macros.h" +#include "hdr/types/struct_sched_param.h" // We Test: // SCHED_OTHER, SCHED_FIFO, SCHED_RR diff --git a/libc/test/src/sched/sched_rr_get_interval_test.cpp b/libc/test/src/sched/sched_rr_get_interval_test.cpp index 272cf86..e5dc4e3 100644 --- a/libc/test/src/sched/sched_rr_get_interval_test.cpp +++ b/libc/test/src/sched/sched_rr_get_interval_test.cpp @@ -14,6 +14,7 @@ #include "src/unistd/getuid.h" #include "test/UnitTest/Test.h" +#include "hdr/sched_macros.h" #include "hdr/types/struct_timespec.h" TEST(LlvmLibcSchedRRGetIntervalTest, SmokeTest) { diff --git a/libc/test/src/wchar/wcstok_test.cpp b/libc/test/src/wchar/wcstok_test.cpp index 7106e9f..3bb1014 100644 --- a/libc/test/src/wchar/wcstok_test.cpp +++ b/libc/test/src/wchar/wcstok_test.cpp @@ -19,6 +19,8 @@ TEST(LlvmLibcWCSTokReentrantTest, NoTokenFound) { // Another call to ensure that 'reserve' is not in a bad state. ASSERT_EQ(LIBC_NAMESPACE::wcstok(empty, L"", &reserve), nullptr); ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"", &reserve), nullptr); + // Subsequent searches still return nullptr. + ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"", &reserve), nullptr); } { // Empty source and single character delimiter string. wchar_t empty[] = L""; @@ -27,6 +29,8 @@ TEST(LlvmLibcWCSTokReentrantTest, NoTokenFound) { // Another call to ensure that 'reserve' is not in a bad state. ASSERT_EQ(LIBC_NAMESPACE::wcstok(empty, L"_", &reserve), nullptr); ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"_", &reserve), nullptr); + // Subsequent searches still return nullptr. + ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"_", &reserve), nullptr); } { // Same character source and delimiter string. wchar_t single[] = L"_"; @@ -35,6 +39,8 @@ TEST(LlvmLibcWCSTokReentrantTest, NoTokenFound) { // Another call to ensure that 'reserve' is not in a bad state. ASSERT_EQ(LIBC_NAMESPACE::wcstok(single, L"_", &reserve), nullptr); ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"_", &reserve), nullptr); + // Subsequent searches still return nullptr. + ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"_", &reserve), nullptr); } { // Multiple character source and single character delimiter string. wchar_t multiple[] = L"1,2"; @@ -51,6 +57,8 @@ TEST(LlvmLibcWCSTokReentrantTest, NoTokenFound) { ASSERT_TRUE(tok[2] == L'2'); ASSERT_TRUE(tok[3] == L'\0'); ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L":", &reserve), nullptr); + // Subsequent searches still return nullptr. + ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L":", &reserve), nullptr); } } |