Skip to content

[libc][math][c23] Add cosf16 function #118785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/headers/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
7 changes: 7 additions & 0 deletions libc/hdrgen/yaml/math.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions libc/src/math/cosf16.h
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions libc/src/math/generic/cosf16.cpp
Original file line number Diff line number Diff line change
@@ -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<float16, N_EXCEPTS> 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<float16>;
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<float16>(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<float16>(fputil::multiply_add(
cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
}

} // namespace LIBC_NAMESPACE_DECL
2 changes: 1 addition & 1 deletion libc/src/math/generic/cospif16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) {
return fputil::cast<float16>(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<float16>(fputil::multiply_add(
cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
}
Expand Down
15 changes: 6 additions & 9 deletions libc/src/math/generic/sinf16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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<float16>(fputil::multiply_add(
sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
}
Expand Down
11 changes: 11 additions & 0 deletions libc/test/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions libc/test/src/math/cosf16_test.cpp
Original file line number Diff line number Diff line change
@@ -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<float16>;

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);
}
}
11 changes: 11 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions libc/test/src/math/smoke/cosf16_test.cpp
Original file line number Diff line number Diff line change
@@ -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<float16>;

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);
}
Loading