From 2a64451d6c82c9804a364df18c52562eda16753d Mon Sep 17 00:00:00 2001 From: wldfngrs Date: Wed, 6 Nov 2024 00:02:46 +0100 Subject: [PATCH 1/8] add tanpif16 function --- libc/config/linux/x86_64/entrypoints.txt | 1 + libc/newhdrgen/yaml/math.yaml | 7 ++ libc/src/math/CMakeLists.txt | 1 + libc/src/math/cospif16.h | 2 +- libc/src/math/generic/CMakeLists.txt | 20 ++++ libc/src/math/generic/cospif16.cpp | 2 +- libc/src/math/generic/tanpif16.cpp | 112 +++++++++++++++++++++ libc/src/math/tanpif16.h | 21 ++++ libc/test/src/math/CMakeLists.txt | 11 ++ libc/test/src/math/smoke/CMakeLists.txt | 11 ++ libc/test/src/math/smoke/tanpif16_test.cpp | 33 ++++++ libc/test/src/math/tanpif16_test.cpp | 41 ++++++++ libc/utils/MPFRWrapper/MPFRUtils.cpp | 24 +++++ libc/utils/MPFRWrapper/MPFRUtils.h | 1 + 14 files changed, 285 insertions(+), 2 deletions(-) create mode 100644 libc/src/math/generic/tanpif16.cpp create mode 100644 libc/src/math/tanpif16.h create mode 100644 libc/test/src/math/smoke/tanpif16_test.cpp create mode 100644 libc/test/src/math/tanpif16_test.cpp diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index a2fb97d04584d..1b850a6df6084 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -687,6 +687,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.sinpif16 libc.src.math.sqrtf16 libc.src.math.tanhf16 + libc.src.math.tanpif16 libc.src.math.totalorderf16 libc.src.math.totalordermagf16 libc.src.math.truncf16 diff --git a/libc/newhdrgen/yaml/math.yaml b/libc/newhdrgen/yaml/math.yaml index 3cc4b599c777b..07a0b80d6e4cc 100644 --- a/libc/newhdrgen/yaml/math.yaml +++ b/libc/newhdrgen/yaml/math.yaml @@ -2410,6 +2410,13 @@ functions: arguments: - type: _Float16 guard: LIBC_TYPES_HAS_FLOAT16 + - name: tanpif16 + standards: + - stdc + return_type: _Float16 + arguments: + - type: _Float16 + guard: LIBC_TYPES_HAS_FLOAT16 - name: totalorder standards: - stdc diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 80c1867d2116f..9f376193e0e74 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -502,6 +502,7 @@ add_math_entrypoint_object(tanf) add_math_entrypoint_object(tanh) add_math_entrypoint_object(tanhf) add_math_entrypoint_object(tanhf16) +add_math_entrypoint_object(tanpif16) add_math_entrypoint_object(tgamma) add_math_entrypoint_object(tgammaf) diff --git a/libc/src/math/cospif16.h b/libc/src/math/cospif16.h index ef9625dfed45f..122b548dfb098 100644 --- a/libc/src/math/cospif16.h +++ b/libc/src/math/cospif16.h @@ -18,4 +18,4 @@ float16 cospif16(float16 x); } // namespace LIBC_NAMESPACE_DECL -#endif // LLVM_LIBC_SRC_MATH_SINPIF16_H +#endif // LLVM_LIBC_SRC_MATH_COSPIF16_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index ca27759d3212f..ceb8f0df00129 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -620,6 +620,26 @@ add_entrypoint_object( -O3 ) +add_entrypoint_object( + tanpif16 + SRCS + tanpif16.cpp + HDRS + ../tanpif16.h + DEPENDS + .sincosf16_utils + libc.hdr.errno_macros + libc.hdr.fenv_macros + libc.src.__support.FPUtil.cast + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.except_value_utils + libc.src.__support.FPUtil.multiply_add + libc.src.__support.macros.optimization + COMPILE_OPTIONS + -O3 +) + add_entrypoint_object( fabs SRCS diff --git a/libc/src/math/generic/cospif16.cpp b/libc/src/math/generic/cospif16.cpp index dd8c7ab6afa3d..384b39ff8e2c4 100644 --- a/libc/src/math/generic/cospif16.cpp +++ b/libc/src/math/generic/cospif16.cpp @@ -37,7 +37,7 @@ LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) { // k = round(x * 32) // y = x * 32 - k // - // Once k and y are computed, we then deduce the answer by the sine of sum + // Once k and y are computed, we then deduce the answer by the cosine of sum // formula: // cos(x * pi) = cos((k + y) * pi/32) // = cos(k * pi/32) * cos(y * pi/32) + diff --git a/libc/src/math/generic/tanpif16.cpp b/libc/src/math/generic/tanpif16.cpp new file mode 100644 index 0000000000000..f4fe265f6f762 --- /dev/null +++ b/libc/src/math/generic/tanpif16.cpp @@ -0,0 +1,112 @@ +//===-- Half-precision tanpif function ------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/tanpif16.h" +#include "hdr/errno_macros.h" +#include "hdr/fenv_macros.h" +#include "sincosf16_utils.h" +#include "src/__support/FPUtil/except_value_utils.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/cast.h" +#include "src/__support/FPUtil/multiply_add.h" +#include "src/__support/macros/optimization.h" + +namespace LIBC_NAMESPACE_DECL { + +constexpr size_t N_EXCEPTS = 21; + +constexpr fputil::ExceptValues TANF16_EXCEPTS{{ + // (input, RZ output, RU offset, RD offset, RN offset) + {0x07f2, 0x0e3d, 1, 0, 0}, + {0x086a, 0x0eee, 1, 0, 1}, + {0x08db, 0x0fa0, 1, 0, 0}, + {0x094c, 0x1029, 1, 0, 0}, + {0x0b10, 0x118c, 1, 0, 0}, + {0x1ce0, 0x23a8, 1, 0, 1}, + {0x1235, 0x18e0, 1, 0, 0}, + {0x2579, 0x2c4e, 1, 0, 0}, + {0x28b2, 0x2f68, 1, 0, 1}, + {0x2a43, 0x30f4, 1, 0, 1}, + {0x31b7, 0x3907, 1, 0, 0}, + {0x329d, 0x3a12, 1, 0, 1}, + {0x34f1, 0x3dd7, 1, 0, 0}, + {0x3658, 0x41ee, 1, 0, 0}, + {0x38d4, 0xc1ee, 0, 1, 0}, + {0x3d96, 0x41ee, 1, 0, 0}, + {0x3e6a, 0xc1ee, 0, 1, 0}, + {0x40cb, 0x41ee, 1, 0, 0}, + {0x4135, 0xc1ee, 0, 1, 0}, + {0x42cb, 0x41ee, 1, 0, 0}, + {0x4335, 0xc1ee, 0, 1, 0}, +}}; + +LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) { + using FPBits = typename fputil::FPBits; + FPBits xbits(x); + + uint16_t x_u = xbits.uintval(); + bool x_sign = x_u >> 15; + uint16_t x_abs = x_u & 0x7fff; + + if (LIBC_UNLIKELY(x_abs == 0U)) return x; + + // Handle exceptional values + if (LIBC_UNLIKELY(x_abs <= 0x4335)) { + if (auto r = TANF16_EXCEPTS.lookup_odd(x_abs, x_sign); + LIBC_UNLIKELY(r.has_value())) + return r.value(); + } + + // Numbers greater or equal to 2^10 are integers, or infinity, or NaN + if (LIBC_UNLIKELY(x_abs >= 0x6400)) { + // Check for NaN or infinity values + if (LIBC_UNLIKELY(x_abs >= 0x7c00)) { + if (x_abs == 0x7c00) { + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_INVALID); + } + + return x + FPBits::quiet_nan().get_val(); + } + + return FPBits::zero(xbits.sign()).get_val(); + } + // Range reduction: + // For |x| > 1/32, we perform range reduction as follows: + // Find k and y such that: + // x = (k + y) * 1/32 + // k is an integer + // |y| < 0.5 + // + // This is done by performing: + // k = round(x * 32) + // y = x * 32 - k + // + // Once k and y are computed, we then deduce the answer by tthe formula: + // tan(x) = sin(x) / cos(x) + // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k) + float xf = x; + float sin_k, cos_k, sin_y, cosm1_y; + sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); + + if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) { + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_DIVBYZERO); + + int16_t x_mp5_u = static_cast(x - 0.5); + return ((x_mp5_u & 0x1) ? -1 : 1) * FPBits::inf().get_val(); + } + + using fputil::multiply_add; + return fputil::cast( + multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) / + multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k))); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/tanpif16.h b/libc/src/math/tanpif16.h new file mode 100644 index 0000000000000..c07a955564de3 --- /dev/null +++ b/libc/src/math/tanpif16.h @@ -0,0 +1,21 @@ +//===-- Implementation header for tanpif16 ----------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_MATH_TANPIF16_H +#define LLVM_LIBC_SRC_MATH_TANPIF16_H + +#include "src/__support/macros/config.h" +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE_DECL { + +float16 tanpif16(float16 x); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_MATH_TANPIF16_H diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index b46ef4028915b..ebe98d346b832 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -168,6 +168,17 @@ add_fp_unittest( libc.src.__support.FPUtil.fp_bits ) +add_fp_unittest( + tanpif16_test + NEED_MPFR + SUITE + libc-math-unittests + SRCS + tanpif16_test.cpp + DEPENDS + libc.src.math.tanpif16 +) + add_fp_unittest( fabs_test NEED_MPFR diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 269e92c590062..8c75d1577081d 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -99,6 +99,17 @@ add_fp_unittest( libc.src.__support.FPUtil.fp_bits ) +add_fp_unittest( + tanpif16_test + SUITE + libc-math-smoke-tests + SRCS + tanpif16_test.cpp + DEPENDS + libc.src.errno.errno + libc.src.math.tanpif16 +) + add_fp_unittest( fabs_test SUITE diff --git a/libc/test/src/math/smoke/tanpif16_test.cpp b/libc/test/src/math/smoke/tanpif16_test.cpp new file mode 100644 index 0000000000000..a378cfb0a62e1 --- /dev/null +++ b/libc/test/src/math/smoke/tanpif16_test.cpp @@ -0,0 +1,33 @@ +//===-- Unittests for tanpif16 --------------------------------------------===// +// +// 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/errno/libc_errno.h" +#include "src/math/tanpif16.h" +#include "test/UnitTest/FPMatcher.h" +#include "test/UnitTest/Test.h" + +using LlvmLibcTanpif16Test = LIBC_NAMESPACE::testing::FPTest; + +TEST_F(LlvmLibcTanpif16Test, SpecialNumbers) { + LIBC_NAMESPACE::libc_errno = 0; + + EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::tanpif16(aNaN)); + EXPECT_MATH_ERRNO(0); + + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::tanpif16(zero)); + EXPECT_MATH_ERRNO(0); + + EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::tanpif16(neg_zero)); + EXPECT_MATH_ERRNO(0); + + EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::tanpif16(inf)); + EXPECT_MATH_ERRNO(EDOM); + + EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::tanpif16(neg_inf)); + EXPECT_MATH_ERRNO(EDOM); +} diff --git a/libc/test/src/math/tanpif16_test.cpp b/libc/test/src/math/tanpif16_test.cpp new file mode 100644 index 0000000000000..6360c93bb0935 --- /dev/null +++ b/libc/test/src/math/tanpif16_test.cpp @@ -0,0 +1,41 @@ +//===-- Exhaustive test for cospif16 --------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/tanpif16.h" +#include "test/UnitTest/FPMatcher.h" +#include "test/UnitTest/Test.h" +#include "utils/MPFRWrapper/MPFRUtils.h" + +using LlvmLibcTanpif16Test = LIBC_NAMESPACE::testing::FPTest; + +namespace mpfr = LIBC_NAMESPACE::testing::mpfr; + +// Range: [0, Inf] +static constexpr uint16_t POS_START = 0x0000U; +static constexpr uint16_t POS_STOP = 0x7c00U; + +// Range: [-Inf, 0] +static constexpr uint16_t NEG_START = 0x8000U; +static constexpr uint16_t NEG_STOP = 0xfc00U; + +TEST_F(LlvmLibcTanpif16Test, PositiveRange) { + for (uint16_t v = POS_START; v <= POS_STOP; ++v) { + float16 x = FPBits(v).get_val(); + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x, + LIBC_NAMESPACE::tanpif16(x), 0.5); + } +} + + +TEST_F(LlvmLibcTanpif16Test, NegativeRange) { + for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) { + float16 x = FPBits(v).get_val(); + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x, + LIBC_NAMESPACE::tanpif16(x), 0.5); + } +} diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index 5afc3d007d4d7..1eb4c9ebc3ea8 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -560,6 +560,28 @@ class MPFRNumber { return result; } + MPFRNumber tanpi() const { + MPFRNumber result(*this); + +#if MPFR_VERSION_MAJOR > 4 || \ + (MPFR_VERSION_MAJOR == 4 && MPFR_VERSION_MINOR >= 2) + + mpfr_tanpi(result.value, value, mpfr_rounding); + return result; +#else + if (mpfr_integer_p(value)) { + mpfr_set_si(result.value, 0, mpfr_rounding); + return result; + } + + MPFRNumber value_pi(0.0, 1280); + mpfr_const_pi(value_pi.value, MPFR_RNDN); + mpfr_mul(value_pi.value, value_pi.value, value, MPFR_RNDN); + mpfr_tan(result.value, value_pi.value, mpfr_rounding); + return result; +#endif + } + MPFRNumber trunc() const { MPFRNumber result(*this); mpfr_trunc(result.value, value); @@ -798,6 +820,8 @@ unary_operation(Operation op, InputType input, unsigned int precision, return mpfrInput.tan(); case Operation::Tanh: return mpfrInput.tanh(); + case Operation::Tanpi: + return mpfrInput.tanpi(); case Operation::Trunc: return mpfrInput.trunc(); default: diff --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h index 9fc12a6adefb5..c7a57819f68b7 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.h +++ b/libc/utils/MPFRWrapper/MPFRUtils.h @@ -60,6 +60,7 @@ enum class Operation : int { Sqrt, Tan, Tanh, + Tanpi, Trunc, EndUnaryOperationsSingleOutput, From 0a27bd5c9eb64600d6111bf87ce19aea40ac2bbb Mon Sep 17 00:00:00 2001 From: wldfngrs Date: Wed, 6 Nov 2024 00:04:45 +0100 Subject: [PATCH 2/8] mend --- libc/src/math/generic/tanpif16.cpp | 59 ++++++++++++---------------- libc/test/src/math/tanpif16_test.cpp | 3 +- libc/utils/MPFRWrapper/MPFRUtils.cpp | 2 +- 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/libc/src/math/generic/tanpif16.cpp b/libc/src/math/generic/tanpif16.cpp index f4fe265f6f762..450022f599234 100644 --- a/libc/src/math/generic/tanpif16.cpp +++ b/libc/src/math/generic/tanpif16.cpp @@ -10,10 +10,10 @@ #include "hdr/errno_macros.h" #include "hdr/fenv_macros.h" #include "sincosf16_utils.h" -#include "src/__support/FPUtil/except_value_utils.h" #include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FPBits.h" #include "src/__support/FPUtil/cast.h" +#include "src/__support/FPUtil/except_value_utils.h" #include "src/__support/FPUtil/multiply_add.h" #include "src/__support/macros/optimization.h" @@ -22,54 +22,45 @@ namespace LIBC_NAMESPACE_DECL { constexpr size_t N_EXCEPTS = 21; constexpr fputil::ExceptValues TANF16_EXCEPTS{{ - // (input, RZ output, RU offset, RD offset, RN offset) - {0x07f2, 0x0e3d, 1, 0, 0}, - {0x086a, 0x0eee, 1, 0, 1}, - {0x08db, 0x0fa0, 1, 0, 0}, - {0x094c, 0x1029, 1, 0, 0}, - {0x0b10, 0x118c, 1, 0, 0}, - {0x1ce0, 0x23a8, 1, 0, 1}, - {0x1235, 0x18e0, 1, 0, 0}, - {0x2579, 0x2c4e, 1, 0, 0}, - {0x28b2, 0x2f68, 1, 0, 1}, - {0x2a43, 0x30f4, 1, 0, 1}, - {0x31b7, 0x3907, 1, 0, 0}, - {0x329d, 0x3a12, 1, 0, 1}, - {0x34f1, 0x3dd7, 1, 0, 0}, - {0x3658, 0x41ee, 1, 0, 0}, - {0x38d4, 0xc1ee, 0, 1, 0}, - {0x3d96, 0x41ee, 1, 0, 0}, - {0x3e6a, 0xc1ee, 0, 1, 0}, - {0x40cb, 0x41ee, 1, 0, 0}, - {0x4135, 0xc1ee, 0, 1, 0}, - {0x42cb, 0x41ee, 1, 0, 0}, - {0x4335, 0xc1ee, 0, 1, 0}, + // (input, RZ output, RU offset, RD offset, RN offset) + {0x07f2, 0x0e3d, 1, 0, 0}, {0x086a, 0x0eee, 1, 0, 1}, + {0x08db, 0x0fa0, 1, 0, 0}, {0x094c, 0x1029, 1, 0, 0}, + {0x0b10, 0x118c, 1, 0, 0}, {0x1ce0, 0x23a8, 1, 0, 1}, + {0x1235, 0x18e0, 1, 0, 0}, {0x2579, 0x2c4e, 1, 0, 0}, + {0x28b2, 0x2f68, 1, 0, 1}, {0x2a43, 0x30f4, 1, 0, 1}, + {0x31b7, 0x3907, 1, 0, 0}, {0x329d, 0x3a12, 1, 0, 1}, + {0x34f1, 0x3dd7, 1, 0, 0}, {0x3658, 0x41ee, 1, 0, 0}, + {0x38d4, 0xc1ee, 0, 1, 0}, {0x3d96, 0x41ee, 1, 0, 0}, + {0x3e6a, 0xc1ee, 0, 1, 0}, {0x40cb, 0x41ee, 1, 0, 0}, + {0x4135, 0xc1ee, 0, 1, 0}, {0x42cb, 0x41ee, 1, 0, 0}, + {0x4335, 0xc1ee, 0, 1, 0}, }}; LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) { using FPBits = typename fputil::FPBits; FPBits xbits(x); - uint16_t x_u = xbits.uintval(); + uint16_t x_u = xbits.uintval(); bool x_sign = x_u >> 15; uint16_t x_abs = x_u & 0x7fff; - if (LIBC_UNLIKELY(x_abs == 0U)) return x; - - // Handle exceptional values + if (LIBC_UNLIKELY(x_abs == 0U)) + return x; + + // Handle exceptional values if (LIBC_UNLIKELY(x_abs <= 0x4335)) { if (auto r = TANF16_EXCEPTS.lookup_odd(x_abs, x_sign); - LIBC_UNLIKELY(r.has_value())) - return r.value(); + LIBC_UNLIKELY(r.has_value())) + return r.value(); } - + // Numbers greater or equal to 2^10 are integers, or infinity, or NaN if (LIBC_UNLIKELY(x_abs >= 0x6400)) { // Check for NaN or infinity values if (LIBC_UNLIKELY(x_abs >= 0x7c00)) { if (x_abs == 0x7c00) { fputil::set_errno_if_required(EDOM); - fputil::raise_except_if_required(FE_INVALID); + fputil::raise_except_if_required(FE_INVALID); } return x + FPBits::quiet_nan().get_val(); @@ -90,7 +81,7 @@ LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) { // // Once k and y are computed, we then deduce the answer by tthe formula: // tan(x) = sin(x) / cos(x) - // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k) + // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k) float xf = x; float sin_k, cos_k, sin_y, cosm1_y; sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); @@ -101,8 +92,8 @@ LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) { int16_t x_mp5_u = static_cast(x - 0.5); return ((x_mp5_u & 0x1) ? -1 : 1) * FPBits::inf().get_val(); - } - + } + using fputil::multiply_add; return fputil::cast( multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) / diff --git a/libc/test/src/math/tanpif16_test.cpp b/libc/test/src/math/tanpif16_test.cpp index 6360c93bb0935..629ba963135c0 100644 --- a/libc/test/src/math/tanpif16_test.cpp +++ b/libc/test/src/math/tanpif16_test.cpp @@ -31,11 +31,10 @@ TEST_F(LlvmLibcTanpif16Test, PositiveRange) { } } - TEST_F(LlvmLibcTanpif16Test, NegativeRange) { for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) { float16 x = FPBits(v).get_val(); EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x, - LIBC_NAMESPACE::tanpif16(x), 0.5); + LIBC_NAMESPACE::tanpif16(x), 0.5); } } diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index 1eb4c9ebc3ea8..eadc82502c526 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -581,7 +581,7 @@ class MPFRNumber { return result; #endif } - + MPFRNumber trunc() const { MPFRNumber result(*this); mpfr_trunc(result.value, value); From 4edcddd60e03069ddfd8db3efff5db74e0ac937c Mon Sep 17 00:00:00 2001 From: wldfngrs Date: Wed, 6 Nov 2024 18:07:17 +0100 Subject: [PATCH 3/8] Add implementation of tanpi for MPFR < 4.2 --- libc/test/src/math/tanpif16_test.cpp | 4 +++- libc/utils/MPFRWrapper/MPFRUtils.cpp | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/libc/test/src/math/tanpif16_test.cpp b/libc/test/src/math/tanpif16_test.cpp index 629ba963135c0..67834dac0f569 100644 --- a/libc/test/src/math/tanpif16_test.cpp +++ b/libc/test/src/math/tanpif16_test.cpp @@ -23,6 +23,7 @@ static constexpr uint16_t POS_STOP = 0x7c00U; static constexpr uint16_t NEG_START = 0x8000U; static constexpr uint16_t NEG_STOP = 0xfc00U; + TEST_F(LlvmLibcTanpif16Test, PositiveRange) { for (uint16_t v = POS_START; v <= POS_STOP; ++v) { float16 x = FPBits(v).get_val(); @@ -36,5 +37,6 @@ TEST_F(LlvmLibcTanpif16Test, NegativeRange) { float16 x = FPBits(v).get_val(); EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x, LIBC_NAMESPACE::tanpif16(x), 0.5); - } + } } + diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index eadc82502c526..ebe362c7466d2 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -569,8 +569,32 @@ class MPFRNumber { mpfr_tanpi(result.value, value, mpfr_rounding); return result; #else - if (mpfr_integer_p(value)) { - mpfr_set_si(result.value, 0, mpfr_rounding); + MPFRNumber value_ret_exact(*this); + mpfr_fmod_ui(value_ret_exact.value, value, 1, mpfr_rounding); + mpfr_mul_si(value_ret_exact.value, value_ret_exact.value, 4, MPFR_RNDN); + + if (mpfr_integer_p(value_ret_exact.value)) { + int mod = mpfr_get_si(value_ret_exact.value, MPFR_RNDN); + mod = (mod < 0 ? -1*mod : mod); + + switch(mod) { + case 0: + mpfr_set_si(result.value, 0, mpfr_rounding); + break; + case 1: + mpfr_set_si(result.value, (mpfr_signbit(value) ? -1 : 1), mpfr_rounding); + break; + case 2: { + auto d = mpfr_get_si(value, MPFR_RNDZ); + d += mpfr_sgn(value) > 0 ? 0 : 1; + mpfr_set_inf(result.value, (d & 1) ? -1 : 1); + break; + } + case 3: + mpfr_set_si(result.value, (mpfr_signbit(value) ? 1 : -1), mpfr_rounding); + break; + } + return result; } From d89dbaf5c10b99a86b4820fd1fbdcbdffc5137ca Mon Sep 17 00:00:00 2001 From: wldfngrs Date: Wed, 6 Nov 2024 18:08:17 +0100 Subject: [PATCH 4/8] mend --- libc/test/src/math/tanpif16_test.cpp | 4 +-- libc/utils/MPFRWrapper/MPFRUtils.cpp | 38 +++++++++++++++------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/libc/test/src/math/tanpif16_test.cpp b/libc/test/src/math/tanpif16_test.cpp index 67834dac0f569..629ba963135c0 100644 --- a/libc/test/src/math/tanpif16_test.cpp +++ b/libc/test/src/math/tanpif16_test.cpp @@ -23,7 +23,6 @@ static constexpr uint16_t POS_STOP = 0x7c00U; static constexpr uint16_t NEG_START = 0x8000U; static constexpr uint16_t NEG_STOP = 0xfc00U; - TEST_F(LlvmLibcTanpif16Test, PositiveRange) { for (uint16_t v = POS_START; v <= POS_STOP; ++v) { float16 x = FPBits(v).get_val(); @@ -37,6 +36,5 @@ TEST_F(LlvmLibcTanpif16Test, NegativeRange) { float16 x = FPBits(v).get_val(); EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x, LIBC_NAMESPACE::tanpif16(x), 0.5); - } + } } - diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index ebe362c7466d2..00ac8a8ba271a 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -575,24 +575,26 @@ class MPFRNumber { if (mpfr_integer_p(value_ret_exact.value)) { int mod = mpfr_get_si(value_ret_exact.value, MPFR_RNDN); - mod = (mod < 0 ? -1*mod : mod); - - switch(mod) { - case 0: - mpfr_set_si(result.value, 0, mpfr_rounding); - break; - case 1: - mpfr_set_si(result.value, (mpfr_signbit(value) ? -1 : 1), mpfr_rounding); - break; - case 2: { - auto d = mpfr_get_si(value, MPFR_RNDZ); - d += mpfr_sgn(value) > 0 ? 0 : 1; - mpfr_set_inf(result.value, (d & 1) ? -1 : 1); - break; - } - case 3: - mpfr_set_si(result.value, (mpfr_signbit(value) ? 1 : -1), mpfr_rounding); - break; + mod = (mod < 0 ? -1 * mod : mod); + + switch (mod) { + case 0: + mpfr_set_si(result.value, 0, mpfr_rounding); + break; + case 1: + mpfr_set_si(result.value, (mpfr_signbit(value) ? -1 : 1), + mpfr_rounding); + break; + case 2: { + auto d = mpfr_get_si(value, MPFR_RNDZ); + d += mpfr_sgn(value) > 0 ? 0 : 1; + mpfr_set_inf(result.value, (d & 1) ? -1 : 1); + break; + } + case 3: + mpfr_set_si(result.value, (mpfr_signbit(value) ? 1 : -1), + mpfr_rounding); + break; } return result; From 0810591d55559fe3d15fc9de33cb0fd82cc1b442 Mon Sep 17 00:00:00 2001 From: wldfngrs Date: Wed, 6 Nov 2024 18:18:26 +0100 Subject: [PATCH 5/8] update documentation --- libc/docs/math/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst index a50e054622e1a..4b426a0b83a21 100644 --- a/libc/docs/math/index.rst +++ b/libc/docs/math/index.rst @@ -350,7 +350,7 @@ Higher Math Functions +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | tanh | |check| | | | |check| | | 7.12.5.6 | F.10.2.6 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| tanpi | | | | | | 7.12.4.14 | F.10.1.14 | +| tanpi | | | | |check| | | 7.12.4.14 | F.10.1.14 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | tgamma | | | | | | 7.12.8.4 | F.10.5.4 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ From c1b926be1c64f5a81cae410dcd6ed7eb2b27f748 Mon Sep 17 00:00:00 2001 From: wldfngrs Date: Thu, 7 Nov 2024 05:29:22 +0100 Subject: [PATCH 6/8] Fixed failed remote build (changed 'mpfr_fmod_ui()' to 'mpfr_fmod()') --- libc/utils/MPFRWrapper/MPFRUtils.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index 00ac8a8ba271a..2d1f7a7174dda 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -563,14 +563,16 @@ class MPFRNumber { MPFRNumber tanpi() const { MPFRNumber result(*this); -#if MPFR_VERSION_MAJOR > 4 || \ + //#if MPFR_VERSION_MAJOR > 4 || \ (MPFR_VERSION_MAJOR == 4 && MPFR_VERSION_MINOR >= 2) - mpfr_tanpi(result.value, value, mpfr_rounding); - return result; -#else + // mpfr_tanpi(result.value, value, mpfr_rounding); + // return result; + // #else MPFRNumber value_ret_exact(*this); - mpfr_fmod_ui(value_ret_exact.value, value, 1, mpfr_rounding); + MPFRNumber value_one(*this); + mpfr_set_si(value_one.value, 1, MPFR_RNDN); + mpfr_fmod(value_ret_exact.value, value, value_one.value, mpfr_rounding); mpfr_mul_si(value_ret_exact.value, value_ret_exact.value, 4, MPFR_RNDN); if (mpfr_integer_p(value_ret_exact.value)) { @@ -605,7 +607,7 @@ class MPFRNumber { mpfr_mul(value_pi.value, value_pi.value, value, MPFR_RNDN); mpfr_tan(result.value, value_pi.value, mpfr_rounding); return result; -#endif + // #endif } MPFRNumber trunc() const { From 7d71a09b2971dab96b9b42db14af8f7568a56d2d Mon Sep 17 00:00:00 2001 From: wldfngrs Date: Thu, 7 Nov 2024 19:06:04 +0100 Subject: [PATCH 7/8] minor fix --- libc/src/math/generic/tanpif16.cpp | 2 +- libc/utils/MPFRWrapper/MPFRUtils.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libc/src/math/generic/tanpif16.cpp b/libc/src/math/generic/tanpif16.cpp index 450022f599234..a23e8041fd5f2 100644 --- a/libc/src/math/generic/tanpif16.cpp +++ b/libc/src/math/generic/tanpif16.cpp @@ -41,7 +41,6 @@ LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) { FPBits xbits(x); uint16_t x_u = xbits.uintval(); - bool x_sign = x_u >> 15; uint16_t x_abs = x_u & 0x7fff; if (LIBC_UNLIKELY(x_abs == 0U)) @@ -49,6 +48,7 @@ LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) { // Handle exceptional values if (LIBC_UNLIKELY(x_abs <= 0x4335)) { + bool x_sign = x_u >> 15; if (auto r = TANF16_EXCEPTS.lookup_odd(x_abs, x_sign); LIBC_UNLIKELY(r.has_value())) return r.value(); diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index 2d1f7a7174dda..00592c5cb15f3 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -563,12 +563,12 @@ class MPFRNumber { MPFRNumber tanpi() const { MPFRNumber result(*this); - //#if MPFR_VERSION_MAJOR > 4 || \ +#if MPFR_VERSION_MAJOR > 4 || \ (MPFR_VERSION_MAJOR == 4 && MPFR_VERSION_MINOR >= 2) - // mpfr_tanpi(result.value, value, mpfr_rounding); - // return result; - // #else + mpfr_tanpi(result.value, value, mpfr_rounding); + return result; +#else MPFRNumber value_ret_exact(*this); MPFRNumber value_one(*this); mpfr_set_si(value_one.value, 1, MPFR_RNDN); @@ -607,7 +607,7 @@ class MPFRNumber { mpfr_mul(value_pi.value, value_pi.value, value, MPFR_RNDN); mpfr_tan(result.value, value_pi.value, mpfr_rounding); return result; - // #endif +#endif } MPFRNumber trunc() const { From f81198bb0624c3cf05cd63b61c59b8e7ba0ab2b2 Mon Sep 17 00:00:00 2001 From: wldfngrs Date: Thu, 7 Nov 2024 19:29:34 +0100 Subject: [PATCH 8/8] more fix --- libc/src/math/generic/tanpif16.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libc/src/math/generic/tanpif16.cpp b/libc/src/math/generic/tanpif16.cpp index a23e8041fd5f2..ab3c9cb2122ba 100644 --- a/libc/src/math/generic/tanpif16.cpp +++ b/libc/src/math/generic/tanpif16.cpp @@ -43,11 +43,11 @@ LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) { uint16_t x_u = xbits.uintval(); uint16_t x_abs = x_u & 0x7fff; - if (LIBC_UNLIKELY(x_abs == 0U)) - return x; - // Handle exceptional values if (LIBC_UNLIKELY(x_abs <= 0x4335)) { + if (LIBC_UNLIKELY(x_abs == 0U)) + return x; + bool x_sign = x_u >> 15; if (auto r = TANF16_EXCEPTS.lookup_odd(x_abs, x_sign); LIBC_UNLIKELY(r.has_value()))