diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 68d0501d3d358..e236ad62261d8 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -632,6 +632,7 @@ if(LIBC_TYPES_HAS_FLOAT16) libc.src.math.canonicalizef16 libc.src.math.ceilf16 libc.src.math.copysignf16 + libc.src.math.cosf16 libc.src.math.coshf16 libc.src.math.cospif16 libc.src.math.exp10f16 diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst index adc0afa080ec2..2808165ad539b 100644 --- a/libc/docs/headers/math/index.rst +++ b/libc/docs/headers/math/index.rst @@ -276,7 +276,7 @@ Higher Math Functions +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | compoundn | | | | | | 7.12.7.2 | F.10.4.2 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ -| cos | |check| | |check| | | | | 7.12.4.5 | F.10.1.5 | +| cos | |check| | |check| | | |check| | | 7.12.4.5 | F.10.1.5 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ | cosh | |check| | | | |check| | | 7.12.5.4 | F.10.2.4 | +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+ diff --git a/libc/hdrgen/yaml/math.yaml b/libc/hdrgen/yaml/math.yaml index 00efc34789667..3b8caec66bbfd 100644 --- a/libc/hdrgen/yaml/math.yaml +++ b/libc/hdrgen/yaml/math.yaml @@ -200,6 +200,13 @@ functions: return_type: float arguments: - type: float + - name: cosf16 + standards: + - stdc + return_type: _Float16 + arguments: + - type: _Float16 + guard: LIBC_TYPES_HAS_FLOAT16 - name: coshf standards: - stdc diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 390a59d07a28b..e4e2c49642f2d 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -89,6 +89,7 @@ add_math_entrypoint_object(copysignf128) add_math_entrypoint_object(cos) add_math_entrypoint_object(cosf) +add_math_entrypoint_object(cosf16) add_math_entrypoint_object(cosh) add_math_entrypoint_object(coshf) diff --git a/libc/src/math/cosf16.h b/libc/src/math/cosf16.h new file mode 100644 index 0000000000000..cc179a6e16c62 --- /dev/null +++ b/libc/src/math/cosf16.h @@ -0,0 +1,21 @@ +//===-- Implementation header for cosf16 ------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_MATH_COSF16_H +#define LLVM_LIBC_SRC_MATH_COSF16_H + +#include "src/__support/macros/config.h" +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE_DECL { + +float16 cosf16(float16 x); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_MATH_COSF16_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index aeb758d4a092d..b3d4612915197 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -418,6 +418,25 @@ add_entrypoint_object( ${libc_opt_high_flag} ) +add_entrypoint_object( + cosf16 + SRCS + cosf16.cpp + HDRS + ../cosf16.h + DEPENDS + .sincosf16_utils + libc.hdr.errno_macros + libc.hdr.fenv_macros + libc.src.__support.FPUtil.cast + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.except_value_utils + libc.src.__support.FPUtil.multiply_add + libc.src.__support.macros.optimization + libc.src.__support.macros.properties.types +) + add_entrypoint_object( cospif SRCS diff --git a/libc/src/math/generic/cosf16.cpp b/libc/src/math/generic/cosf16.cpp new file mode 100644 index 0000000000000..534e07854474e --- /dev/null +++ b/libc/src/math/generic/cosf16.cpp @@ -0,0 +1,84 @@ +//===-- Half-precision cos(x) function ------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/cosf16.h" +#include "hdr/errno_macros.h" +#include "hdr/fenv_macros.h" +#include "sincosf16_utils.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/cast.h" +#include "src/__support/FPUtil/except_value_utils.h" +#include "src/__support/FPUtil/multiply_add.h" +#include "src/__support/macros/optimization.h" + +namespace LIBC_NAMESPACE_DECL { + +constexpr size_t N_EXCEPTS = 4; + +constexpr fputil::ExceptValues COSF16_EXCEPTS{{ + // (input, RZ output, RU offset, RD offset, RN offset) + {0x2b7c, 0x3bfc, 1, 0, 1}, + {0x4ac1, 0x38b5, 1, 0, 0}, + {0x5c49, 0xb8c6, 0, 1, 0}, + {0x7acc, 0xa474, 0, 1, 0}, +}}; + +LLVM_LIBC_FUNCTION(float16, cosf16, (float16 x)) { + using FPBits = fputil::FPBits; + FPBits xbits(x); + + uint16_t x_u = xbits.uintval(); + uint16_t x_abs = x_u & 0x7fff; + float xf = x; + + // Range reduction: + // For |x| > pi/32, we perform range reduction as follows: + // Find k and y such that: + // x = (k + y) * pi/32 + // k is an integer, |y| < 0.5 + // + // This is done by performing: + // k = round(x * 32/pi) + // y = x * 32/pi - k + // + // Once k and y are computed, we then deduce the answer by the cosine of sum + // formula: + // cos(x) = cos((k + y) * pi/32) + // = cos(k * pi/32) * cos(y * pi/32) - + // sin(k * pi/32) * sin(y * pi/32) + + // Handle exceptional values + if (auto r = COSF16_EXCEPTS.lookup(x_abs); LIBC_UNLIKELY(r.has_value())) + return r.value(); + + // cos(+/-0) = 1 + if (LIBC_UNLIKELY(x_abs == 0U)) + return fputil::cast(1.0f); + + // cos(+/-inf) = NaN, and cos(NaN) = NaN + if (xbits.is_inf_or_nan()) { + if (xbits.is_inf()) { + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_INVALID); + } + + return x + FPBits::quiet_nan().get_val(); + } + + float sin_k, cos_k, sin_y, cosm1_y; + sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); + // Since, cosm1_y = cos_y - 1, therefore: + // cos(x) = cos_k * cos_y - sin_k * sin_y + // = cos_k * (cos_y - 1 + 1) - sin_k * sin_y + // = cos_k * cosm1_y - sin_k * sin_y + cos_k + return fputil::cast(fputil::multiply_add( + cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k))); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/cospif16.cpp b/libc/src/math/generic/cospif16.cpp index 384b39ff8e2c4..ee74bdb4a3693 100644 --- a/libc/src/math/generic/cospif16.cpp +++ b/libc/src/math/generic/cospif16.cpp @@ -73,7 +73,7 @@ LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) { return fputil::cast(0.0f); // Since, cosm1_y = cos_y - 1, therefore: - // cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y + // cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y return fputil::cast(fputil::multiply_add( cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k))); } diff --git a/libc/src/math/generic/sinf16.cpp b/libc/src/math/generic/sinf16.cpp index 86546348ba739..7c15951cccecf 100644 --- a/libc/src/math/generic/sinf16.cpp +++ b/libc/src/math/generic/sinf16.cpp @@ -54,13 +54,10 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) { // sin(y * pi/32) * cos(k * pi/32) // Handle exceptional values - if (LIBC_UNLIKELY(x_abs == 0x585c || x_abs == 0x5cb0 || x_abs == 0x51f5 || - x_abs == 0x2b45)) { - bool x_sign = x_u >> 15; - if (auto r = SINF16_EXCEPTS.lookup_odd(x_abs, x_sign); - LIBC_UNLIKELY(r.has_value())) - return r.value(); - } + bool x_sign = x_u >> 15; + if (auto r = SINF16_EXCEPTS.lookup_odd(x_abs, x_sign); + LIBC_UNLIKELY(r.has_value())) + return r.value(); int rounding = fputil::quick_get_round(); @@ -99,8 +96,8 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) { if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0)) return FPBits::zero(xbits.sign()).get_val(); - // Since, cosm1_y = cos_y - 1, therfore: - // sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k) + // Since, cosm1_y = cos_y - 1, therefore: + // sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k) return fputil::cast(fputil::multiply_add( sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k))); } diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index ea75720df4f43..44a29f0c48e40 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -28,6 +28,17 @@ add_fp_unittest( libc.src.__support.FPUtil.fp_bits ) +add_fp_unittest( + cosf16_test + NEED_MPFR + SUITE + libc-math-unittests + SRCS + cosf16_test.cpp + DEPENDS + libc.src.math.cosf16 +) + add_fp_unittest( cospif_test NEED_MPFR diff --git a/libc/test/src/math/cosf16_test.cpp b/libc/test/src/math/cosf16_test.cpp new file mode 100644 index 0000000000000..9e4687f0325c4 --- /dev/null +++ b/libc/test/src/math/cosf16_test.cpp @@ -0,0 +1,40 @@ +//===-- Exhaustive test for cosf16 ----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/math/cosf16.h" +#include "test/UnitTest/FPMatcher.h" +#include "test/UnitTest/Test.h" +#include "utils/MPFRWrapper/MPFRUtils.h" + +using LlvmLibcCosf16Test = 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(LlvmLibcCosf16Test, PositiveRange) { + for (uint16_t v = POS_START; v <= POS_STOP; ++v) { + float16 x = FPBits(v).get_val(); + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cos, x, + LIBC_NAMESPACE::cosf16(x), 0.5); + } +} + +TEST_F(LlvmLibcCosf16Test, NegativeRange) { + for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) { + float16 x = FPBits(v).get_val(); + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Cos, x, + LIBC_NAMESPACE::cosf16(x), 0.5); + } +} diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 2c1c4dba73846..31f85a3ecfd27 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -12,6 +12,17 @@ add_fp_unittest( libc.src.math.cosf ) +add_fp_unittest( + cosf16_test + SUITE + libc-math-smoke-tests + SRCS + cosf16_test.cpp + DEPENDS + libc.src.errno.errno + libc.src.math.cosf16 +) + add_fp_unittest( cospif_test SUITE diff --git a/libc/test/src/math/smoke/cosf16_test.cpp b/libc/test/src/math/smoke/cosf16_test.cpp new file mode 100644 index 0000000000000..9a51d1015da34 --- /dev/null +++ b/libc/test/src/math/smoke/cosf16_test.cpp @@ -0,0 +1,33 @@ +//===-- Unittests for cosf16 ----------------------------------------------===// +// +// 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/cosf16.h" +#include "test/UnitTest/FPMatcher.h" +#include "test/UnitTest/Test.h" + +using LlvmLibcCosf16Test = LIBC_NAMESPACE::testing::FPTest; + +TEST_F(LlvmLibcCosf16Test, SpecialNumbers) { + LIBC_NAMESPACE::libc_errno = 0; + + EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::cosf16(aNaN)); + EXPECT_MATH_ERRNO(0); + + EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::cosf16(zero)); + EXPECT_MATH_ERRNO(0); + + EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::cosf16(neg_zero)); + EXPECT_MATH_ERRNO(0); + + EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::cosf16(inf)); + EXPECT_MATH_ERRNO(EDOM); + + EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::cosf16(neg_inf)); + EXPECT_MATH_ERRNO(EDOM); +}