Skip to content

Commit ddc3f2d

Browse files
authored
[libc] Add sinpif16 function (#110994)
Half-precision floating point (16-bit) implementation of the trigonometric function Sin for inputs scaled by pi
1 parent 7cbb365 commit ddc3f2d

File tree

13 files changed

+307
-3
lines changed

13 files changed

+307
-3
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
679679
libc.src.math.scalbnf16
680680
libc.src.math.setpayloadf16
681681
libc.src.math.setpayloadsigf16
682+
libc.src.math.sinpif16
682683
libc.src.math.totalorderf16
683684
libc.src.math.totalordermagf16
684685
libc.src.math.truncf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
677677
libc.src.math.scalbnf16
678678
libc.src.math.setpayloadf16
679679
libc.src.math.setpayloadsigf16
680+
libc.src.math.sinpif16
680681
libc.src.math.totalorderf16
681682
libc.src.math.totalordermagf16
682683
libc.src.math.truncf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ Higher Math Functions
342342
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
343343
| sinh | |check| | | | | | 7.12.5.5 | F.10.2.5 |
344344
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
345-
| sinpi | |check| | | | | | 7.12.4.13 | F.10.1.13 |
345+
| sinpi | |check| | | | |check| | | 7.12.4.13 | F.10.1.13 |
346346
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
347347
| sqrt | |check| | |check| | |check| | | |check| | 7.12.7.10 | F.10.4.10 |
348348
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/newhdrgen/yaml/math.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,13 @@ functions:
22972297
return_type: float
22982298
arguments:
22992299
- type: float
2300+
- name: sinpif16
2301+
standards:
2302+
- stdc
2303+
return_type: _Float16
2304+
arguments:
2305+
- type: _Float16
2306+
guard: LIBC_TYPES_HAS_FLOAT16
23002307
- name: sqrt
23012308
standards:
23022309
- stdc

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ add_math_entrypoint_object(sincosf)
475475
add_math_entrypoint_object(sin)
476476
add_math_entrypoint_object(sinf)
477477
add_math_entrypoint_object(sinpif)
478+
add_math_entrypoint_object(sinpif16)
478479

479480
add_math_entrypoint_object(sinh)
480481
add_math_entrypoint_object(sinhf)

libc/src/math/generic/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,25 @@ add_entrypoint_object(
528528
-O3
529529
)
530530

531+
add_entrypoint_object(
532+
sinpif16
533+
SRCS
534+
sinpif16.cpp
535+
HDRS
536+
../sinpif16.h
537+
DEPENDS
538+
libc.src.__support.common
539+
libc.src.__support.FPUtil.cast
540+
libc.src.__support.FPUtil.fenv_impl
541+
libc.src.__support.FPUtil.fp_bits
542+
libc.src.__support.FPUtil.multiply_add
543+
libc.src.__support.FPUtil.nearest_integer
544+
libc.src.__support.FPUtil.polyeval
545+
libc.src.__support.macros.properties.types
546+
COMPILE_OPTIONS
547+
-O3
548+
)
549+
531550
add_entrypoint_object(
532551
tan
533552
SRCS

libc/src/math/generic/sinpif16.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//===-- Half-precision sinpif 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/sinpif16.h"
10+
#include "src/__support/FPUtil/FEnvImpl.h"
11+
#include "src/__support/FPUtil/FPBits.h"
12+
#include "src/__support/FPUtil/PolyEval.h"
13+
#include "src/__support/FPUtil/cast.h"
14+
#include "src/__support/FPUtil/multiply_add.h"
15+
#include "src/__support/FPUtil/nearest_integer.h"
16+
#include "src/__support/common.h"
17+
#include "src/__support/macros/config.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
// Lookup table for sin(k * pi / 32) with k = 0, ..., 63.
22+
// Table is generated with Sollya as follows:
23+
// > display = hexadecimmal;
24+
// > for k from 0 to 63 do { round(sin(k * pi/32), SG, RN); };
25+
static constexpr float SIN_K_PI_OVER_32[64] = {
26+
0x0.0p0, 0x1.917a6cp-4, 0x1.8f8b84p-3, 0x1.294062p-2,
27+
0x1.87de2ap-2, 0x1.e2b5d4p-2, 0x1.1c73b4p-1, 0x1.44cf32p-1,
28+
0x1.6a09e6p-1, 0x1.8bc806p-1, 0x1.a9b662p-1, 0x1.c38b3p-1,
29+
0x1.d906bcp-1, 0x1.e9f416p-1, 0x1.f6297cp-1, 0x1.fd88dap-1,
30+
0x1p0, 0x1.fd88dap-1, 0x1.f6297cp-1, 0x1.e9f416p-1,
31+
0x1.d906bcp-1, 0x1.c38b3p-1, 0x1.a9b662p-1, 0x1.8bc806p-1,
32+
0x1.6a09e6p-1, 0x1.44cf32p-1, 0x1.1c73b4p-1, 0x1.e2b5d4p-2,
33+
0x1.87de2ap-2, 0x1.294062p-2, 0x1.8f8b84p-3, 0x1.917a6cp-4,
34+
0x0.0p0, -0x1.917a6cp-4, -0x1.8f8b84p-3, -0x1.294062p-2,
35+
-0x1.87de2ap-2, -0x1.e2b5d4p-2, -0x1.1c73b4p-1, -0x1.44cf32p-1,
36+
-0x1.6a09e6p-1, -0x1.8bc806p-1, -0x1.a9b662p-1, -0x1.c38b3p-1,
37+
-0x1.d906bcp-1, -0x1.e9f416p-1, -0x1.f6297ep-1, -0x1.fd88dap-1,
38+
-0x1p0, -0x1.fd88dap-1, -0x1.f6297cp-1, -0x1.e9f416p-1,
39+
-0x1.d906bcp-1, -0x1.c38b3p-1, -0x1.a9b662p-1, -0x1.8bc806p-1,
40+
-0x1.6a09e6p-1, -0x1.44cf32p-1, -0x1.1c73b4p-1, -0x1.e2b5d4p-2,
41+
-0x1.87de2ap-2, -0x1.294062p-2, -0x1.8f8b84p-3, -0x1.917a6cp-4};
42+
43+
static LIBC_INLINE int32_t range_reduction(float x, float &y) {
44+
float kf = fputil::nearest_integer(x * 32);
45+
y = fputil::multiply_add<float>(x, 32.0, -kf);
46+
47+
return static_cast<int32_t>(kf);
48+
}
49+
50+
LLVM_LIBC_FUNCTION(float16, sinpif16, (float16 x)) {
51+
using FPBits = typename fputil::FPBits<float16>;
52+
FPBits xbits(x);
53+
54+
uint16_t x_u = xbits.uintval();
55+
uint16_t x_abs = x_u & 0x7fff;
56+
57+
// Range reduction:
58+
// For |x| > 1/32, we perform range reduction as follows:
59+
// Find k and y such that:
60+
// x = (k + y) * 1/32
61+
// k is an integer
62+
// |y| < 0.5
63+
//
64+
// This is done by performing:
65+
// k = round(x * 32)
66+
// y = x * 32 - k
67+
//
68+
// Once k and y are computed, we then deduce the answer by the sine of sum
69+
// formula:
70+
// sin(x * pi) = sin((k + y) * pi/32)
71+
// = sin(k * pi/32) * cos(y * pi/32) + sin (y * pi/32) * cos (k *
72+
// pi/32)
73+
// The values of sin(k * pi/32) and cos (k * pi/32) for k = 0...63 are
74+
// precomputed and stored using a vector of 64 single precision floats. sin(y
75+
// * pi/32) and cos(y * pi/32) are computed using degree-9 chebyshev
76+
// polynomials generated by Sollya.
77+
78+
// For signed zeros
79+
if (LIBC_UNLIKELY(x_abs == 0U))
80+
return x;
81+
82+
// Numbers greater or equal to 2^10 are integers, or infinity, or NaN
83+
if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
84+
// Check for NaN or infinity values
85+
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
86+
// If value is equal to infinity
87+
if (x_abs == 0x7c00) {
88+
fputil::set_errno_if_required(EDOM);
89+
fputil::raise_except_if_required(FE_INVALID);
90+
}
91+
92+
return x + FPBits::quiet_nan().get_val();
93+
}
94+
return FPBits::zero(xbits.sign()).get_val();
95+
}
96+
97+
float f32 = x;
98+
float y;
99+
int32_t k = range_reduction(f32, y);
100+
101+
float sin_k = SIN_K_PI_OVER_32[k & 63];
102+
float cos_k = SIN_K_PI_OVER_32[(k + 16) & 63];
103+
104+
// Recall;
105+
// sin(x * pi/32) = sin((k + y) * pi/32)
106+
// = sin(y * pi/32) * cos(k * pi/32) + cos(y * pi/32) * sin(k *
107+
// pi/32) Recall, after range reduction, -0.5 <= y <= 0.5. For very small
108+
// values of y, calculating sin(y * p/32) can be inaccurate. Generating a
109+
// polynomial for sin(y * p/32)/y instead significantly reduces the relative
110+
// errors.
111+
float ysq = y * y;
112+
113+
// Degree-6 minimax even polynomial for sin(y*pi/32)/y generated by Sollya
114+
// with: > Q = fpminimax(sin(y*pi/32)/y, [|0, 2, 4, 6|], [|SG...|], [0, 0.5]);
115+
float sin_y = y * fputil::polyeval(ysq, 0x1.921fb6p-4f, -0x1.4aeabcp-13f,
116+
0x1.a03354p-21f, -0x1.ad02d2p-20f);
117+
118+
// Note that cosm1_y = cos(y*pi/32) - 1 = cos_y - 1
119+
// Derivation:
120+
// sin(x * pi) = sin((k + y) * pi/32)
121+
// = sin_y * cos_k + cos_y * sin_k
122+
// = cos_k * sin_y + sin_k * (1 + cos_y - 1)
123+
// Degree-6 minimax even polynomial for cos(y*pi/32) generated by Sollya with:
124+
// > P = fpminimax(cos(y*pi/32), [|0, 2, 4, 6|],[|1, SG...|], [0, 0.5]);
125+
float cosm1_y = ysq * fputil::polyeval(ysq, -0x1.3bd3ccp-8f, 0x1.03a61ap-18f,
126+
0x1.a6f7a2p-29f);
127+
128+
if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
129+
return FPBits::zero(xbits.sign()).get_val();
130+
131+
// Since, cosm1_y = cos_y - 1, therefore:
132+
// sin(x * pi) = cos_k * sin_y + sin_k + (cosm1_y * sin_k)
133+
return fputil::cast<float16>(fputil::multiply_add(
134+
sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
135+
}
136+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/sinpif16.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for sinpif16 ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache Licese 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_SINPIF16_H
10+
#define LLVM_LIBC_SRC_MATH_SINPIF16_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 sinpif16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_SINPIF16_H

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ add_fp_unittest(
9090
libc.src.__support.FPUtil.fp_bits
9191
)
9292

93+
add_fp_unittest(
94+
sinpif16_test
95+
NEED_MPFR
96+
SUITE
97+
libc-math-unittests
98+
SRCS
99+
sinpif16_test.cpp
100+
DEPENDS
101+
libc.src.math.sinpif16
102+
)
103+
93104
add_fp_unittest(
94105
sin_test
95106
NEED_MPFR

libc/test/src/math/sinpif16_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- Exhaustive test for sinpif16 --------------------------------------===//
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/sinpif16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcSinpif16Test = 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(LlvmLibcSinpif16Test, 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::Sinpi, x,
30+
LIBC_NAMESPACE::sinpif16(x), 0.5);
31+
}
32+
}
33+
34+
TEST_F(LlvmLibcSinpif16Test, 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::Sinpi, x,
38+
LIBC_NAMESPACE::sinpif16(x), 0.5);
39+
}
40+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ add_fp_unittest(
5151
libc.src.__support.FPUtil.fp_bits
5252
)
5353

54+
add_fp_unittest(
55+
sinpif16_test
56+
SUITE
57+
libc-math-smoke-tests
58+
SRCS
59+
sinpif16_test.cpp
60+
DEPENDS
61+
libc.src.errno.errno
62+
libc.src.math.sinpif16
63+
)
64+
5465
add_fp_unittest(
5566
sincosf_test
5667
SUITE
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===-- Unittests for sinpif16 --------------------------------------------===//
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/sinpif16.h"
11+
#include "test/UnitTest/FPMatcher.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
using LlvmLibcSinpif16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
15+
16+
TEST_F(LlvmLibcSinpif16Test, SpecialNumbers) {
17+
LIBC_NAMESPACE::libc_errno = 0;
18+
19+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinpif16(aNaN));
20+
EXPECT_MATH_ERRNO(0);
21+
22+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpif16(zero));
23+
EXPECT_MATH_ERRNO(0);
24+
25+
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpif16(neg_zero));
26+
EXPECT_MATH_ERRNO(0);
27+
28+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinpif16(inf));
29+
EXPECT_MATH_ERRNO(EDOM);
30+
31+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinpif16(neg_inf));
32+
EXPECT_MATH_ERRNO(EDOM);
33+
}
34+
35+
TEST_F(LlvmLibcSinpif16Test, Integers) {
36+
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpif16(-0x420));
37+
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpif16(-0x1p+10));
38+
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpif16(-0x1.4p+14));
39+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpif16(0x420));
40+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpif16(0x1.cp+15));
41+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpif16(0x1.cp+7));
42+
}

libc/utils/MPFRWrapper/MPFRUtils.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,28 @@ class MPFRNumber {
488488
(MPFR_VERSION_MAJOR == 4 && MPFR_VERSION_MINOR >= 2)
489489

490490
mpfr_sinpi(result.value, value, mpfr_rounding);
491+
return result;
491492
#else
493+
if (mpfr_integer_p(value)) {
494+
mpfr_set_si(result.value, 0, mpfr_rounding);
495+
return result;
496+
}
497+
498+
MPFRNumber value_mul_two(*this);
499+
mpfr_mul_si(value_mul_two.value, value, 2, MPFR_RNDN);
500+
501+
if (mpfr_integer_p(value_mul_two.value)) {
502+
auto d = mpfr_get_si(value, MPFR_RNDD);
503+
mpfr_set_si(result.value, (d & 1) ? -1 : 1, mpfr_rounding);
504+
return result;
505+
}
506+
492507
MPFRNumber value_pi(0.0, 1280);
493508
mpfr_const_pi(value_pi.value, MPFR_RNDN);
494509
mpfr_mul(value_pi.value, value_pi.value, value, MPFR_RNDN);
495510
mpfr_sin(result.value, value_pi.value, mpfr_rounding);
496-
#endif
497-
498511
return result;
512+
#endif
499513
}
500514

501515
MPFRNumber sinh() const {

0 commit comments

Comments
 (0)