Skip to content

Commit 1f68b66

Browse files
committed
Add comments, update docs
1 parent 3304e7e commit 1f68b66

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

libc/docs/headers/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ Higher Math Functions
346346
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
347347
| sqrt | |check| | |check| | |check| | |check| | |check| | 7.12.7.10 | F.10.4.10 |
348348
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
349-
| tan | |check| | |check| | | | | 7.12.4.7 | F.10.1.7 |
349+
| tan | |check| | |check| | | |check| | | 7.12.4.7 | F.10.1.7 |
350350
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
351351
| tanh | |check| | | | |check| | | 7.12.5.6 | F.10.2.6 |
352352
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/src/math/generic/sincosf16_utils.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,20 @@ LIBC_INLINE int32_t range_reduction_sincospif16(float x, float &y) {
4949

5050
// Recall, range reduction:
5151
// k = round(x * 32/pi)
52-
// y = x * 32/pi - k
5352
//
54-
// The constant 0x1.45f306dc9c883p3 is 32/pi rounded to double-precision.
55-
// 32/pi is generated by Sollya with the following commands:
56-
// > display = hexadecimal;
57-
// > round(32/pi, D, RN);
58-
//
59-
// The precision choice of 'double' is to minimize rounding errors
60-
// in this initial scaling step, preserving enough bits so errors accumulated
61-
// while computing the subtraction: y = x * 32/pi - round(x * 32/pi)
53+
// The precision choice of 'double' in the following function is to minimize
54+
// rounding errors in this initial scaling step,
55+
// preserving enough bits so errors accumulated while computing the subtraction:
56+
// y = x * 32/pi - round(x * 32/pi)
6257
// are beyond the least-significant bit of single-precision used during
6358
// further intermediate computation.
6459
LIBC_INLINE int32_t range_reduction_sincosf16(float x, float &y) {
65-
double prod = x * 0x1.45f306dc9c883p3;
66-
double kd = fputil::nearest_integer(prod);
60+
// Generated by Sollya with:
61+
// > D(32/pi);
62+
constexpr double THIRTYTWO_OVER_PI = 0x1.45f306dc9c883p3;
6763

64+
double prod = x * THIRTYTWO_OVER_PI;
65+
double kd = fputil::nearest_integer(prod);
6866
y = static_cast<float>(prod - kd);
6967

7068
return static_cast<int32_t>(kd);

libc/src/math/generic/tanf16.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ LLVM_LIBC_FUNCTION(float16, tanf16, (float16 x)) {
5050

5151
// |x| <= 0x1.d1p-5
5252
if (LIBC_UNLIKELY(x_abs <= 0x2b44)) {
53+
// |x| <= 1.398p-11
5354
if (LIBC_UNLIKELY(x_abs <= 0x10e6)) {
5455
// tan(+/-0) = +/-0
55-
if (LIBC_UNLIKELY(x_abs == 0U))
56+
if (LIBC_UNLIKELY(x_abs == 0))
5657
return x;
5758

5859
int rounding = fputil::quick_get_round();
@@ -64,20 +65,22 @@ LLVM_LIBC_FUNCTION(float16, tanf16, (float16 x)) {
6465
if ((xbits.is_pos() && rounding == FE_UPWARD) ||
6566
(xbits.is_neg() && rounding == FE_DOWNWARD))
6667
return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
67-
else
68-
return x;
68+
return x;
6969
}
7070

7171
float xsq = xf * xf;
7272

73+
// Degree-6 minimax odd polynomial of tan(x) generated by Sollya with:
74+
// > P = fpminimax(tan(x)/x, [|0, 2, 4, 6|], [|1, SG...|], [0, pi/32]);
7375
float result = fputil::polyeval(xsq, 0x1p0f, 0x1.555556p-2f, 0x1.110ee4p-3f,
7476
0x1.be80f6p-5f);
7577

7678
return fputil::cast<float16>(xf * result);
7779
}
7880

79-
// tan(+/-inf)= NaN, and tan(NaN) = NaN
81+
// tan(+/-inf) = NaN, and tan(NaN) = NaN
8082
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
83+
// x = +/-inf
8184
if (x_abs == 0x7c00) {
8285
fputil::set_errno_if_required(EDOM);
8386
fputil::raise_except_if_required(FE_INVALID);

0 commit comments

Comments
 (0)