Skip to content

Commit 49b868b

Browse files
committed
Add sinf16 function
1 parent 4f2651c commit 49b868b

File tree

11 files changed

+251
-4
lines changed

11 files changed

+251
-4
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
699699
libc.src.math.scalbnf16
700700
libc.src.math.setpayloadf16
701701
libc.src.math.setpayloadsigf16
702+
libc.src.math.sinf16
702703
libc.src.math.sinhf16
703704
libc.src.math.sinpif16
704705
libc.src.math.sqrtf16

libc/newhdrgen/yaml/math.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,13 @@ functions:
23392339
return_type: float
23402340
arguments:
23412341
- type: float
2342+
- name: sinf16
2343+
standards:
2344+
- stdc
2345+
return_type: _Float16
2346+
arguments:
2347+
- type: _Float16
2348+
guard: LIBC_TYPES_HAS_FLOAT16
23422349
- name: sinhf
23432350
standards:
23442351
- stdc

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ add_math_entrypoint_object(sincosf)
484484

485485
add_math_entrypoint_object(sin)
486486
add_math_entrypoint_object(sinf)
487+
add_math_entrypoint_object(sinf16)
487488
add_math_entrypoint_object(sinpif)
488489
add_math_entrypoint_object(sinpif16)
489490

libc/src/math/generic/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,25 @@ add_entrypoint_object(
496496
-O3
497497
)
498498

499+
add_entrypoint_object(
500+
sinf16
501+
SRCS
502+
sinf16.cpp
503+
HDRS
504+
../sinf16.h
505+
DEPENDS
506+
.sincosf16_utils
507+
libc.hdr.errno_macros
508+
libc.hdr.fenv_macros
509+
libc.src.__support.FPUtil.cast
510+
libc.src.__support.FPUtil.fenv_impl
511+
libc.src.__support.FPUtil.fp_bits
512+
libc.src.__support.FPUtil.multiply_add
513+
libc.src.__support.macros.optimization
514+
COMPILE_OPTIONS
515+
-O3
516+
)
517+
499518
add_entrypoint_object(
500519
sincos
501520
SRCS

libc/src/math/generic/sincosf16_utils.h

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,30 @@ constexpr float SIN_K_PI_OVER_32[64] = {
3939
-0x1.6a09e6p-1, -0x1.44cf32p-1, -0x1.1c73b4p-1, -0x1.e2b5d4p-2,
4040
-0x1.87de2ap-2, -0x1.294062p-2, -0x1.8f8b84p-3, -0x1.917a6cp-4};
4141

42+
static constexpr float THIRTYTWO_OVER_PI[4] = {
43+
//0x1.45fp3, 0x1.837p-12, -0x1.b1b8p-28, -0x1.f568p-43
44+
0x1.46p3, -0x1.9f4p-10, 0x1.b94p-22, -0x1.bcp-36
45+
};
46+
4247
LIBC_INLINE int32_t range_reduction_sincospif16(float x, float &y) {
4348
float kf = fputil::nearest_integer(x * 32);
4449
y = fputil::multiply_add<float>(x, 32.0, -kf);
4550

4651
return static_cast<int32_t>(kf);
4752
}
4853

49-
LIBC_INLINE void sincospif16_eval(float xf, float &sin_k, float &cos_k,
50-
float &sin_y, float &cosm1_y) {
51-
float y;
52-
int32_t k = range_reduction_sincospif16(xf, y);
54+
LIBC_INLINE int32_t range_reduction_sincosf16(float x, float &y) {
55+
double xd = x;
56+
double prod = xd * 0x1.45f306dc9c883p3;
57+
double kf = fputil::nearest_integer(prod);
58+
double yd = prod - kf;
5359

60+
y = static_cast<float>(yd);
61+
return static_cast<int32_t>(kf);
62+
}
63+
64+
static LIBC_INLINE void sincosf16_poly_eval(int32_t k, float y, float &sin_k, float &cos_k, float &sin_y, float &cosm1_y) {
65+
5466
sin_k = SIN_K_PI_OVER_32[k & 63];
5567
cos_k = SIN_K_PI_OVER_32[(k + 16) & 63];
5668

@@ -72,6 +84,21 @@ LIBC_INLINE void sincospif16_eval(float xf, float &sin_k, float &cos_k,
7284
0x1.a6f7a2p-29f);
7385
}
7486

87+
LIBC_INLINE void sincosf16_eval(float xf, float &sin_k, float &cos_k, float &sin_y, float &cosm1_y) {
88+
float y;
89+
int32_t k = range_reduction_sincosf16(xf, y);
90+
91+
sincosf16_poly_eval(k, y, sin_k, cos_k, sin_y, cosm1_y);
92+
}
93+
94+
LIBC_INLINE void sincospif16_eval(float xf, float &sin_k, float &cos_k,
95+
float &sin_y, float &cosm1_y) {
96+
float y;
97+
int32_t k = range_reduction_sincospif16(xf, y);
98+
99+
sincosf16_poly_eval(k, y, sin_k, cos_k, sin_y, cosm1_y);
100+
}
101+
75102
} // namespace LIBC_NAMESPACE_DECL
76103

77104
#endif // LLVM_LIBC_SRC_MATH_GENERIC_SINCOSF16_UTILS_H

libc/src/math/generic/sinf16.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===-- Half-precision sin 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/sinf16.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/multiply_add.h"
17+
#include "src/__support/macros/optimization.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) {
22+
using FPBits = typename fputil::FPBits<float16>;
23+
FPBits xbits(x);
24+
25+
uint16_t x_u = xbits.uintval();
26+
uint16_t x_abs = x_u & 0x7fff;
27+
float xf = x;
28+
29+
// Range reduction:
30+
// For !x| > pi/32, we perform range reduction as follows:
31+
// Find k and y such that:
32+
// x = (k + y) * pi/32
33+
// k is an integer, |y| < 0.5
34+
//
35+
// This is done by performing:
36+
// k = round(x * 32/pi)
37+
// y = x * 32/pi - k
38+
//
39+
// Once k and y are computed, we then deduce the answer by the sine of sum
40+
// formula:
41+
// sin(x) = sin((k + y) * pi/32)
42+
// = sin(k * pi/32) * cos(y * pi/32) +
43+
// sin(y * pi/32) * cos(k * pi/32)
44+
45+
if (LIBC_UNLIKELY(x_abs <= 0x13d0)) {
46+
int rounding = fputil::quick_get_round();
47+
48+
// For signed zeros
49+
if ((LIBC_UNLIKELY(x_abs == 0U)) ||
50+
(rounding == FE_UPWARD && xbits.is_pos()) ||
51+
(rounding == FE_DOWNWARD && xbits.is_neg()))
52+
return x;
53+
}
54+
55+
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
56+
// If value is equal to infinity
57+
if (x_abs == 0x7c00) {
58+
fputil::set_errno_if_required(EDOM);
59+
fputil::raise_except_if_required(FE_INVALID);
60+
}
61+
62+
return x + FPBits::quiet_nan().get_val();
63+
}
64+
65+
66+
float sin_k, cos_k, sin_y, cosm1_y;
67+
sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
68+
69+
if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
70+
return FPBits::zero(xbits.sign()).get_val();
71+
72+
// Since, cosm1_y = cos_y - 1, therfore:
73+
// sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k)
74+
return fputil::cast<float16>(fputil::multiply_add(sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
75+
}
76+
77+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/sinf16.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for sinf16 ------------------------*- 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_SINF16_H
10+
#define LLVM_LIBC_SRC_MATH_SINF16_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 sinf16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_SINF16_H

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ add_fp_unittest(
8585
libc.src.__support.FPUtil.fp_bits
8686
)
8787

88+
add_fp_unittest(
89+
sinf16_test
90+
NEED_MPFR
91+
SUITE
92+
libc-math-unittests
93+
SRCS
94+
sinf16_test.cpp
95+
DEPENDS
96+
libc.src.math.sinf16
97+
)
98+
8899
add_fp_unittest(
89100
sinpif_test
90101
NEED_MPFR

libc/test/src/math/sinf16_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- Exhaustive test for sinf16 ----------------------------------------===//
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/sinf16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcSinf16Test = 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(LlvmLibcSinf16Test, 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::Sin, x, LIBC_NAMESPACE::sinf16(x), 0.5);
30+
}
31+
}
32+
33+
TEST_F(LlvmLibcSinf16Test, NegativeRange) {
34+
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
35+
float16 x = FPBits(v).get_val();
36+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x, LIBC_NAMESPACE::sinf16(x), 0.5);
37+
}
38+
}

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

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

52+
add_fp_unittest(
53+
sinf16_test
54+
SUITE
55+
libc-math-smoke-tests
56+
SRCS
57+
sinf16_test.cpp
58+
DEPENDS
59+
libc.src.errno.errno
60+
libc.src.math.sinf16
61+
)
62+
5263
add_fp_unittest(
5364
sinpif_test
5465
SUITE

0 commit comments

Comments
 (0)