Skip to content

Commit f7bb129

Browse files
authoredNov 8, 2024··
[libc][math][c23] Add tanpif16 function (#115183)
- Implementation of `tan` for 16-bit floating point inputs scaled by pi. i.e,. `tanpif16()` - Implementation of Tanpi in MPFRWrapper for MPFR versions < 4.2 - Exhaustive tests for `tanpif16()`
1 parent 77bec78 commit f7bb129

File tree

15 files changed

+304
-3
lines changed

15 files changed

+304
-3
lines changed
 

‎libc/config/linux/x86_64/entrypoints.txt

+1
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
702702
libc.src.math.sinpif16
703703
libc.src.math.sqrtf16
704704
libc.src.math.tanhf16
705+
libc.src.math.tanpif16
705706
libc.src.math.totalorderf16
706707
libc.src.math.totalordermagf16
707708
libc.src.math.truncf16

‎libc/docs/math/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ Higher Math Functions
350350
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
351351
| tanh | |check| | | | |check| | | 7.12.5.6 | F.10.2.6 |
352352
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
353-
| tanpi | | | | | | 7.12.4.14 | F.10.1.14 |
353+
| tanpi | | | | |check| | | 7.12.4.14 | F.10.1.14 |
354354
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
355355
| tgamma | | | | | | 7.12.8.4 | F.10.5.4 |
356356
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

‎libc/newhdrgen/yaml/math.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,13 @@ functions:
24162416
arguments:
24172417
- type: _Float16
24182418
guard: LIBC_TYPES_HAS_FLOAT16
2419+
- name: tanpif16
2420+
standards:
2421+
- stdc
2422+
return_type: _Float16
2423+
arguments:
2424+
- type: _Float16
2425+
guard: LIBC_TYPES_HAS_FLOAT16
24192426
- name: totalorder
24202427
standards:
24212428
- stdc

‎libc/src/math/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ add_math_entrypoint_object(tanf)
503503
add_math_entrypoint_object(tanh)
504504
add_math_entrypoint_object(tanhf)
505505
add_math_entrypoint_object(tanhf16)
506+
add_math_entrypoint_object(tanpif16)
506507

507508
add_math_entrypoint_object(tgamma)
508509
add_math_entrypoint_object(tgammaf)

‎libc/src/math/cospif16.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ float16 cospif16(float16 x);
1818

1919
} // namespace LIBC_NAMESPACE_DECL
2020

21-
#endif // LLVM_LIBC_SRC_MATH_SINPIF16_H
21+
#endif // LLVM_LIBC_SRC_MATH_COSPIF16_H

‎libc/src/math/generic/CMakeLists.txt

+20
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,26 @@ add_entrypoint_object(
620620
-O3
621621
)
622622

623+
add_entrypoint_object(
624+
tanpif16
625+
SRCS
626+
tanpif16.cpp
627+
HDRS
628+
../tanpif16.h
629+
DEPENDS
630+
.sincosf16_utils
631+
libc.hdr.errno_macros
632+
libc.hdr.fenv_macros
633+
libc.src.__support.FPUtil.cast
634+
libc.src.__support.FPUtil.fenv_impl
635+
libc.src.__support.FPUtil.fp_bits
636+
libc.src.__support.FPUtil.except_value_utils
637+
libc.src.__support.FPUtil.multiply_add
638+
libc.src.__support.macros.optimization
639+
COMPILE_OPTIONS
640+
-O3
641+
)
642+
623643
add_entrypoint_object(
624644
fabs
625645
SRCS

‎libc/src/math/generic/cospif16.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) {
3737
// k = round(x * 32)
3838
// y = x * 32 - k
3939
//
40-
// Once k and y are computed, we then deduce the answer by the sine of sum
40+
// Once k and y are computed, we then deduce the answer by the cosine of sum
4141
// formula:
4242
// cos(x * pi) = cos((k + y) * pi/32)
4343
// = cos(k * pi/32) * cos(y * pi/32) +

‎libc/src/math/generic/tanpif16.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//===-- Half-precision tanpif function ------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/tanpif16.h"
10+
#include "hdr/errno_macros.h"
11+
#include "hdr/fenv_macros.h"
12+
#include "sincosf16_utils.h"
13+
#include "src/__support/FPUtil/FEnvImpl.h"
14+
#include "src/__support/FPUtil/FPBits.h"
15+
#include "src/__support/FPUtil/cast.h"
16+
#include "src/__support/FPUtil/except_value_utils.h"
17+
#include "src/__support/FPUtil/multiply_add.h"
18+
#include "src/__support/macros/optimization.h"
19+
20+
namespace LIBC_NAMESPACE_DECL {
21+
22+
constexpr size_t N_EXCEPTS = 21;
23+
24+
constexpr fputil::ExceptValues<float16, N_EXCEPTS> TANF16_EXCEPTS{{
25+
// (input, RZ output, RU offset, RD offset, RN offset)
26+
{0x07f2, 0x0e3d, 1, 0, 0}, {0x086a, 0x0eee, 1, 0, 1},
27+
{0x08db, 0x0fa0, 1, 0, 0}, {0x094c, 0x1029, 1, 0, 0},
28+
{0x0b10, 0x118c, 1, 0, 0}, {0x1ce0, 0x23a8, 1, 0, 1},
29+
{0x1235, 0x18e0, 1, 0, 0}, {0x2579, 0x2c4e, 1, 0, 0},
30+
{0x28b2, 0x2f68, 1, 0, 1}, {0x2a43, 0x30f4, 1, 0, 1},
31+
{0x31b7, 0x3907, 1, 0, 0}, {0x329d, 0x3a12, 1, 0, 1},
32+
{0x34f1, 0x3dd7, 1, 0, 0}, {0x3658, 0x41ee, 1, 0, 0},
33+
{0x38d4, 0xc1ee, 0, 1, 0}, {0x3d96, 0x41ee, 1, 0, 0},
34+
{0x3e6a, 0xc1ee, 0, 1, 0}, {0x40cb, 0x41ee, 1, 0, 0},
35+
{0x4135, 0xc1ee, 0, 1, 0}, {0x42cb, 0x41ee, 1, 0, 0},
36+
{0x4335, 0xc1ee, 0, 1, 0},
37+
}};
38+
39+
LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) {
40+
using FPBits = typename fputil::FPBits<float16>;
41+
FPBits xbits(x);
42+
43+
uint16_t x_u = xbits.uintval();
44+
uint16_t x_abs = x_u & 0x7fff;
45+
46+
// Handle exceptional values
47+
if (LIBC_UNLIKELY(x_abs <= 0x4335)) {
48+
if (LIBC_UNLIKELY(x_abs == 0U))
49+
return x;
50+
51+
bool x_sign = x_u >> 15;
52+
if (auto r = TANF16_EXCEPTS.lookup_odd(x_abs, x_sign);
53+
LIBC_UNLIKELY(r.has_value()))
54+
return r.value();
55+
}
56+
57+
// Numbers greater or equal to 2^10 are integers, or infinity, or NaN
58+
if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
59+
// Check for NaN or infinity values
60+
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
61+
if (x_abs == 0x7c00) {
62+
fputil::set_errno_if_required(EDOM);
63+
fputil::raise_except_if_required(FE_INVALID);
64+
}
65+
66+
return x + FPBits::quiet_nan().get_val();
67+
}
68+
69+
return FPBits::zero(xbits.sign()).get_val();
70+
}
71+
// Range reduction:
72+
// For |x| > 1/32, we perform range reduction as follows:
73+
// Find k and y such that:
74+
// x = (k + y) * 1/32
75+
// k is an integer
76+
// |y| < 0.5
77+
//
78+
// This is done by performing:
79+
// k = round(x * 32)
80+
// y = x * 32 - k
81+
//
82+
// Once k and y are computed, we then deduce the answer by tthe formula:
83+
// tan(x) = sin(x) / cos(x)
84+
// = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
85+
float xf = x;
86+
float sin_k, cos_k, sin_y, cosm1_y;
87+
sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
88+
89+
if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) {
90+
fputil::set_errno_if_required(EDOM);
91+
fputil::raise_except_if_required(FE_DIVBYZERO);
92+
93+
int16_t x_mp5_u = static_cast<int16_t>(x - 0.5);
94+
return ((x_mp5_u & 0x1) ? -1 : 1) * FPBits::inf().get_val();
95+
}
96+
97+
using fputil::multiply_add;
98+
return fputil::cast<float16>(
99+
multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /
100+
multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k)));
101+
}
102+
103+
} // namespace LIBC_NAMESPACE_DECL

‎libc/src/math/tanpif16.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for tanpif16 ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_TANPIF16_H
10+
#define LLVM_LIBC_SRC_MATH_TANPIF16_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
float16 tanpif16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_TANPIF16_H

‎libc/test/src/math/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,17 @@ add_fp_unittest(
168168
libc.src.__support.FPUtil.fp_bits
169169
)
170170

171+
add_fp_unittest(
172+
tanpif16_test
173+
NEED_MPFR
174+
SUITE
175+
libc-math-unittests
176+
SRCS
177+
tanpif16_test.cpp
178+
DEPENDS
179+
libc.src.math.tanpif16
180+
)
181+
171182
add_fp_unittest(
172183
fabs_test
173184
NEED_MPFR

‎libc/test/src/math/smoke/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ add_fp_unittest(
9999
libc.src.__support.FPUtil.fp_bits
100100
)
101101

102+
add_fp_unittest(
103+
tanpif16_test
104+
SUITE
105+
libc-math-smoke-tests
106+
SRCS
107+
tanpif16_test.cpp
108+
DEPENDS
109+
libc.src.errno.errno
110+
libc.src.math.tanpif16
111+
)
112+
102113
add_fp_unittest(
103114
fabs_test
104115
SUITE
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- Unittests for tanpif16 --------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/errno/libc_errno.h"
10+
#include "src/math/tanpif16.h"
11+
#include "test/UnitTest/FPMatcher.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
using LlvmLibcTanpif16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
15+
16+
TEST_F(LlvmLibcTanpif16Test, SpecialNumbers) {
17+
LIBC_NAMESPACE::libc_errno = 0;
18+
19+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::tanpif16(aNaN));
20+
EXPECT_MATH_ERRNO(0);
21+
22+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::tanpif16(zero));
23+
EXPECT_MATH_ERRNO(0);
24+
25+
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::tanpif16(neg_zero));
26+
EXPECT_MATH_ERRNO(0);
27+
28+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::tanpif16(inf));
29+
EXPECT_MATH_ERRNO(EDOM);
30+
31+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::tanpif16(neg_inf));
32+
EXPECT_MATH_ERRNO(EDOM);
33+
}

‎libc/test/src/math/tanpif16_test.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- Exhaustive test for cospif16 --------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/tanpif16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcTanpif16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
15+
16+
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
17+
18+
// Range: [0, Inf]
19+
static constexpr uint16_t POS_START = 0x0000U;
20+
static constexpr uint16_t POS_STOP = 0x7c00U;
21+
22+
// Range: [-Inf, 0]
23+
static constexpr uint16_t NEG_START = 0x8000U;
24+
static constexpr uint16_t NEG_STOP = 0xfc00U;
25+
26+
TEST_F(LlvmLibcTanpif16Test, PositiveRange) {
27+
for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
28+
float16 x = FPBits(v).get_val();
29+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x,
30+
LIBC_NAMESPACE::tanpif16(x), 0.5);
31+
}
32+
}
33+
34+
TEST_F(LlvmLibcTanpif16Test, NegativeRange) {
35+
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
36+
float16 x = FPBits(v).get_val();
37+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanpi, x,
38+
LIBC_NAMESPACE::tanpif16(x), 0.5);
39+
}
40+
}

‎libc/utils/MPFRWrapper/MPFRUtils.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,56 @@ class MPFRNumber {
560560
return result;
561561
}
562562

563+
MPFRNumber tanpi() const {
564+
MPFRNumber result(*this);
565+
566+
#if MPFR_VERSION_MAJOR > 4 || \
567+
(MPFR_VERSION_MAJOR == 4 && MPFR_VERSION_MINOR >= 2)
568+
569+
mpfr_tanpi(result.value, value, mpfr_rounding);
570+
return result;
571+
#else
572+
MPFRNumber value_ret_exact(*this);
573+
MPFRNumber value_one(*this);
574+
mpfr_set_si(value_one.value, 1, MPFR_RNDN);
575+
mpfr_fmod(value_ret_exact.value, value, value_one.value, mpfr_rounding);
576+
mpfr_mul_si(value_ret_exact.value, value_ret_exact.value, 4, MPFR_RNDN);
577+
578+
if (mpfr_integer_p(value_ret_exact.value)) {
579+
int mod = mpfr_get_si(value_ret_exact.value, MPFR_RNDN);
580+
mod = (mod < 0 ? -1 * mod : mod);
581+
582+
switch (mod) {
583+
case 0:
584+
mpfr_set_si(result.value, 0, mpfr_rounding);
585+
break;
586+
case 1:
587+
mpfr_set_si(result.value, (mpfr_signbit(value) ? -1 : 1),
588+
mpfr_rounding);
589+
break;
590+
case 2: {
591+
auto d = mpfr_get_si(value, MPFR_RNDZ);
592+
d += mpfr_sgn(value) > 0 ? 0 : 1;
593+
mpfr_set_inf(result.value, (d & 1) ? -1 : 1);
594+
break;
595+
}
596+
case 3:
597+
mpfr_set_si(result.value, (mpfr_signbit(value) ? 1 : -1),
598+
mpfr_rounding);
599+
break;
600+
}
601+
602+
return result;
603+
}
604+
605+
MPFRNumber value_pi(0.0, 1280);
606+
mpfr_const_pi(value_pi.value, MPFR_RNDN);
607+
mpfr_mul(value_pi.value, value_pi.value, value, MPFR_RNDN);
608+
mpfr_tan(result.value, value_pi.value, mpfr_rounding);
609+
return result;
610+
#endif
611+
}
612+
563613
MPFRNumber trunc() const {
564614
MPFRNumber result(*this);
565615
mpfr_trunc(result.value, value);
@@ -798,6 +848,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
798848
return mpfrInput.tan();
799849
case Operation::Tanh:
800850
return mpfrInput.tanh();
851+
case Operation::Tanpi:
852+
return mpfrInput.tanpi();
801853
case Operation::Trunc:
802854
return mpfrInput.trunc();
803855
default:

‎libc/utils/MPFRWrapper/MPFRUtils.h

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ enum class Operation : int {
6060
Sqrt,
6161
Tan,
6262
Tanh,
63+
Tanpi,
6364
Trunc,
6465
EndUnaryOperationsSingleOutput,
6566

0 commit comments

Comments
 (0)
Please sign in to comment.