Skip to content

Commit 51d48be

Browse files
committed
[libc][math] Refactor cospif16 implementation to header-only in src/__support/math folder.
1 parent 3ce25ab commit 51d48be

File tree

9 files changed

+159
-83
lines changed

9 files changed

+159
-83
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "math/coshf.h"
3939
#include "math/coshf16.h"
4040
#include "math/cospif.h"
41+
#include "math/cospif16.h"
4142
#include "math/erff.h"
4243
#include "math/exp.h"
4344
#include "math/exp10.h"

libc/shared/math/cospif16.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Shared cospif16 function --------------------------------*- 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_SHARED_MATH_COSPIF16_H
10+
#define LLVM_LIBC_SHARED_MATH_COSPIF16_H
11+
12+
#include "shared/libc_common.h"
13+
14+
#ifdef LIBC_TYPES_HAS_FLOAT16
15+
16+
#include "src/__support/math/cospif16.h"
17+
18+
namespace LIBC_NAMESPACE_DECL {
19+
namespace shared {
20+
21+
using math::cospif16;
22+
23+
} // namespace shared
24+
} // namespace LIBC_NAMESPACE_DECL
25+
26+
#endif // LIBC_TYPES_HAS_FLOAT16
27+
28+
#endif // LLVM_LIBC_SHARED_MATH_COSPIF_H

libc/src/__support/math/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,19 @@ add_header_library(
444444
libc.src.__support.macros.optimization
445445
)
446446

447+
add_header_library(
448+
cospif16
449+
HDRS
450+
cospif16.h
451+
DEPENDS
452+
.sincosf16_utils
453+
libc.src.__support.FPUtil.cast
454+
libc.src.__support.FPUtil.fenv_impl
455+
libc.src.__support.FPUtil.fp_bits
456+
libc.src.__support.FPUtil.multiply_add
457+
libc.src.__support.macros.optimization
458+
)
459+
447460
add_header_library(
448461
erff
449462
HDRS

libc/src/__support/math/cospif16.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//===-- Implementation header for cospif16 ----------------------*- 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___SUPPORT_MATH_COSPIF16_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF16_H
11+
12+
#include "include/llvm-libc-macros/float16-macros.h"
13+
14+
#ifdef LIBC_TYPES_HAS_FLOAT16
15+
16+
#include "sincosf16_utils.h"
17+
#include "src/__support/FPUtil/FEnvImpl.h"
18+
#include "src/__support/FPUtil/FPBits.h"
19+
#include "src/__support/FPUtil/cast.h"
20+
#include "src/__support/FPUtil/multiply_add.h"
21+
#include "src/__support/macros/optimization.h"
22+
23+
namespace LIBC_NAMESPACE_DECL {
24+
25+
namespace math {
26+
27+
LIBC_INLINE static constexpr float16 cospif16(float16 x) {
28+
29+
using namespace sincosf16_internal;
30+
using FPBits = typename fputil::FPBits<float16>;
31+
FPBits xbits(x);
32+
33+
uint16_t x_u = xbits.uintval();
34+
uint16_t x_abs = x_u & 0x7fff;
35+
float xf = x;
36+
37+
// Range reduction:
38+
// For |x| > 1/32, we perform range reduction as follows:
39+
// Find k and y such that:
40+
// x = (k + y) * 1/32
41+
// k is an integer
42+
// |y| < 0.5
43+
//
44+
// This is done by performing:
45+
// k = round(x * 32)
46+
// y = x * 32 - k
47+
//
48+
// Once k and y are computed, we then deduce the answer by the cosine of sum
49+
// formula:
50+
// cos(x * pi) = cos((k + y) * pi/32)
51+
// = cos(k * pi/32) * cos(y * pi/32) +
52+
// sin(y * pi/32) * sin(k * pi/32)
53+
54+
// For signed zeros
55+
if (LIBC_UNLIKELY(x_abs == 0U))
56+
return fputil::cast<float16>(1.0f);
57+
58+
// Numbers greater or equal to 2^10 are integers, or infinity, or NaN
59+
if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
60+
if (LIBC_UNLIKELY(x_abs <= 0x67FF))
61+
return fputil::cast<float16>((x_abs & 0x1) ? -1.0f : 1.0f);
62+
63+
// Check for NaN or infintiy values
64+
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
65+
if (xbits.is_signaling_nan()) {
66+
fputil::raise_except_if_required(FE_INVALID);
67+
return FPBits::quiet_nan().get_val();
68+
}
69+
// If value is equal to infinity
70+
if (x_abs == 0x7c00) {
71+
fputil::set_errno_if_required(EDOM);
72+
fputil::raise_except_if_required(FE_INVALID);
73+
}
74+
75+
return x + FPBits::quiet_nan().get_val();
76+
}
77+
78+
return fputil::cast<float16>(1.0f);
79+
}
80+
81+
float sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
82+
sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
83+
84+
if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0))
85+
return fputil::cast<float16>(0.0f);
86+
87+
// Since, cosm1_y = cos_y - 1, therefore:
88+
// cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
89+
return fputil::cast<float16>(fputil::multiply_add(
90+
cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
91+
}
92+
93+
} // namespace math
94+
95+
} // namespace LIBC_NAMESPACE_DECL
96+
97+
#endif // LIBC_TYPES_HAS_FLOAT16
98+
99+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF16_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,7 @@ add_entrypoint_object(
325325
HDRS
326326
../cospif16.h
327327
DEPENDS
328-
libc.hdr.errno_macros
329-
libc.hdr.fenv_macros
330-
libc.src.__support.FPUtil.cast
331-
libc.src.__support.FPUtil.fenv_impl
332-
libc.src.__support.FPUtil.fp_bits
333-
libc.src.__support.FPUtil.multiply_add
334-
libc.src.__support.macros.optimization
335-
libc.src.__support.math.sincosf16_utils
328+
libc.src.__support.math.cospif16
336329
)
337330

338331
add_entrypoint_object(

libc/src/math/generic/cospif16.cpp

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/cospif16.h"
10-
#include "hdr/errno_macros.h"
11-
#include "hdr/fenv_macros.h"
12-
#include "src/__support/FPUtil/FEnvImpl.h"
13-
#include "src/__support/FPUtil/FPBits.h"
14-
#include "src/__support/FPUtil/cast.h"
15-
#include "src/__support/FPUtil/multiply_add.h"
16-
#include "src/__support/macros/optimization.h"
17-
#include "src/__support/math/sincosf16_utils.h"
10+
#include "src/__support/math/cospif16.h"
1811

1912
namespace LIBC_NAMESPACE_DECL {
2013

21-
LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) {
22-
using namespace sincosf16_internal;
23-
using FPBits = typename fputil::FPBits<float16>;
24-
FPBits xbits(x);
25-
26-
uint16_t x_u = xbits.uintval();
27-
uint16_t x_abs = x_u & 0x7fff;
28-
float xf = x;
29-
30-
// Range reduction:
31-
// For |x| > 1/32, we perform range reduction as follows:
32-
// Find k and y such that:
33-
// x = (k + y) * 1/32
34-
// k is an integer
35-
// |y| < 0.5
36-
//
37-
// This is done by performing:
38-
// k = round(x * 32)
39-
// y = x * 32 - k
40-
//
41-
// Once k and y are computed, we then deduce the answer by the cosine of sum
42-
// formula:
43-
// cos(x * pi) = cos((k + y) * pi/32)
44-
// = cos(k * pi/32) * cos(y * pi/32) +
45-
// sin(y * pi/32) * sin(k * pi/32)
46-
47-
// For signed zeros
48-
if (LIBC_UNLIKELY(x_abs == 0U))
49-
return fputil::cast<float16>(1.0f);
50-
51-
// Numbers greater or equal to 2^10 are integers, or infinity, or NaN
52-
if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
53-
if (LIBC_UNLIKELY(x_abs <= 0x67FF))
54-
return fputil::cast<float16>((x_abs & 0x1) ? -1.0f : 1.0f);
55-
56-
// Check for NaN or infintiy values
57-
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
58-
if (xbits.is_signaling_nan()) {
59-
fputil::raise_except_if_required(FE_INVALID);
60-
return FPBits::quiet_nan().get_val();
61-
}
62-
// If value is equal to infinity
63-
if (x_abs == 0x7c00) {
64-
fputil::set_errno_if_required(EDOM);
65-
fputil::raise_except_if_required(FE_INVALID);
66-
}
67-
68-
return x + FPBits::quiet_nan().get_val();
69-
}
70-
71-
return fputil::cast<float16>(1.0f);
72-
}
73-
74-
float sin_k, cos_k, sin_y, cosm1_y;
75-
sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
76-
77-
if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0))
78-
return fputil::cast<float16>(0.0f);
79-
80-
// Since, cosm1_y = cos_y - 1, therefore:
81-
// cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
82-
return fputil::cast<float16>(fputil::multiply_add(
83-
cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
84-
}
14+
LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) { return math::cospif16(x); }
8515

8616
} // namespace LIBC_NAMESPACE_DECL

libc/test/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_fp_unittest(
3434
libc.src.__support.math.coshf
3535
libc.src.__support.math.coshf16
3636
libc.src.__support.math.cospif
37+
libc.src.__support.math.cospif16
3738
libc.src.__support.math.erff
3839
libc.src.__support.math.exp
3940
libc.src.__support.math.exp10

libc/test/shared/shared_math_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
2323
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::atanhf16(0.0f16));
2424
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::cosf16(0.0f16));
2525
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::coshf16(0.0f16));
26+
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::cospif16(0.0f16));
2627
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::exp10f16(0.0f16));
2728

2829
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::expf16(0.0f16));

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,18 @@ libc_support_library(
24072407
],
24082408
)
24092409

2410+
libc_support_library(
2411+
name = "__support_math_cospif16",
2412+
hdrs = ["src/__support/math/cospif16.h"],
2413+
deps = [
2414+
":__support_fputil_cast",
2415+
":__support_fputil_fenv_impl",
2416+
":__support_fputil_multiply_add",
2417+
":__support_macros_optimization",
2418+
":__support_math_sincosf16_utils",
2419+
],
2420+
)
2421+
24102422
libc_support_library(
24112423
name = "__support_math_erff",
24122424
hdrs = ["src/__support/math/erff.h"],
@@ -3242,9 +3254,7 @@ libc_math_function(
32423254
libc_math_function(
32433255
name = "cospif16",
32443256
additional_deps = [
3245-
":__support_fputil_multiply_add",
3246-
":__support_macros_optimization",
3247-
":__support_math_sincosf16_utils",
3257+
":__support_math_cospif16",
32483258
],
32493259
)
32503260

0 commit comments

Comments
 (0)