Skip to content

Commit f7bb129

Browse files
authored
[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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 20 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 103 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

0 commit comments

Comments
 (0)