1//===-- Implementation header for acospif16 ---------------------*- 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_ACOSPIF16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_ACOSPIF16_H
11
12#include "include/llvm-libc-macros/float16-macros.h"
13
14#ifdef LIBC_TYPES_HAS_FLOAT16
15
16#include "src/__support/FPUtil/FEnvImpl.h"
17#include "src/__support/FPUtil/FPBits.h"
18#include "src/__support/FPUtil/PolyEval.h"
19#include "src/__support/FPUtil/cast.h"
20#include "src/__support/FPUtil/multiply_add.h"
21#include "src/__support/FPUtil/sqrt.h"
22#include "src/__support/macros/optimization.h"
23
24namespace LIBC_NAMESPACE_DECL {
25
26namespace math {
27
28LIBC_INLINE constexpr float16 acospif16(float16 x) {
29 using FPBits = fputil::FPBits<float16>;
30 FPBits xbits(x);
31
32 uint16_t x_u = xbits.uintval();
33 uint16_t x_abs = x_u & 0x7fff;
34 uint16_t x_sign = x_u >> 15;
35
36 // |x| > 0x1p0, |x| > 1, or x is NaN.
37 if (LIBC_UNLIKELY(x_abs > 0x3c00)) {
38 // acospif16(NaN) = NaN
39 if (xbits.is_nan()) {
40 if (xbits.is_signaling_nan()) {
41 fputil::raise_except_if_required(FE_INVALID);
42 return FPBits::quiet_nan().get_val();
43 }
44
45 return x;
46 }
47
48 // 1 < |x| <= +inf
49 fputil::raise_except_if_required(FE_INVALID);
50 fputil::set_errno_if_required(EDOM);
51
52 return FPBits::quiet_nan().get_val();
53 }
54
55 // |x| == 0x1p0, x is 1 or -1
56 // if x is (-)1, return 1
57 // if x is (+)1, return 0
58 if (LIBC_UNLIKELY(x_abs == 0x3c00))
59 return fputil::cast<float16>(x: x_sign ? 1.0f : 0.0f);
60
61 float xf = x;
62 float xsq = xf * xf;
63
64 // Degree-6 minimax polynomial coefficients of asin(x) generated by Sollya
65 // with: > P = fpminimax(asin(x)/(pi * x), [|0, 2, 4, 6, 8|], [|SG...|], [0,
66 // 0.5]);
67 constexpr float POLY_COEFFS[5] = {0x1.45f308p-2f, 0x1.b2900cp-5f,
68 0x1.897e36p-6f, 0x1.9efafcp-7f,
69 0x1.06d884p-6f};
70 // |x| <= 0x1p-1, |x| <= 0.5
71 if (x_abs <= 0x3800) {
72 // if x is 0, return 0.5
73 if (LIBC_UNLIKELY(x_abs == 0))
74 return fputil::cast<float16>(x: 0.5f);
75
76 // Note that: acos(x) = pi/2 + asin(-x) = pi/2 - asin(x), then
77 // acospi(x) = 0.5 - asin(x)/pi
78 float interm =
79 fputil::polyeval(x: xsq, a0: POLY_COEFFS[0], a: POLY_COEFFS[1], a: POLY_COEFFS[2],
80 a: POLY_COEFFS[3], a: POLY_COEFFS[4]);
81
82 return fputil::cast<float16>(x: fputil::multiply_add(x: -xf, y: interm, z: 0.5f));
83 }
84
85 // When |x| > 0.5, assume that 0.5 < |x| <= 1
86 //
87 // Step-by-step range-reduction proof:
88 // 1: Let y = asin(x), such that, x = sin(y)
89 // 2: From complimentary angle identity:
90 // x = sin(y) = cos(pi/2 - y)
91 // 3: Let z = pi/2 - y, such that x = cos(z)
92 // 4: From double angle formula; cos(2A) = 1 - 2 * sin^2(A):
93 // z = 2A, z/2 = A
94 // cos(z) = 1 - 2 * sin^2(z/2)
95 // 5: Make sin(z/2) subject of the formula:
96 // sin(z/2) = sqrt((1 - cos(z))/2)
97 // 6: Recall [3]; x = cos(z). Therefore:
98 // sin(z/2) = sqrt((1 - x)/2)
99 // 7: Let u = (1 - x)/2
100 // 8: Therefore:
101 // asin(sqrt(u)) = z/2
102 // 2 * asin(sqrt(u)) = z
103 // 9: Recall [3]; z = pi/2 - y. Therefore:
104 // y = pi/2 - z
105 // y = pi/2 - 2 * asin(sqrt(u))
106 // 10: Recall [1], y = asin(x). Therefore:
107 // asin(x) = pi/2 - 2 * asin(sqrt(u))
108 // 11: Recall that: acos(x) = pi/2 + asin(-x) = pi/2 - asin(x)
109 // Therefore:
110 // acos(x) = pi/2 - (pi/2 - 2 * asin(sqrt(u)))
111 // acos(x) = 2 * asin(sqrt(u))
112 // acospi(x) = 2 * (asin(sqrt(u)) / pi)
113 //
114 // THE RANGE REDUCTION, HOW?
115 // 12: Recall [7], u = (1 - x)/2
116 // 13: Since 0.5 < x <= 1, therefore:
117 // 0 <= u <= 0.25 and 0 <= sqrt(u) <= 0.5
118 //
119 // Hence, we can reuse the same [0, 0.5] domain polynomial approximation for
120 // Step [11] as `sqrt(u)` is in range.
121 // When -1 < x <= -0.5, the identity:
122 // acos(x) = pi - acos(-x)
123 // acospi(x) = 1 - acos(-x)/pi
124 // allows us to compute for the negative x value (lhs)
125 // with a positive x value instead (rhs).
126
127 float xf_abs = (xf < 0 ? -xf : xf);
128 float u = fputil::multiply_add(x: -0.5f, y: xf_abs, z: 0.5f);
129 float sqrt_u = fputil::sqrt<float>(x: u);
130
131 float asin_sqrt_u =
132 sqrt_u * fputil::polyeval(x: u, a0: POLY_COEFFS[0], a: POLY_COEFFS[1],
133 a: POLY_COEFFS[2], a: POLY_COEFFS[3], a: POLY_COEFFS[4]);
134
135 // Same as acos(x), but devided the expression with pi
136 return fputil::cast<float16>(
137 x: x_sign ? fputil::multiply_add(x: -2.0f, y: asin_sqrt_u, z: 1.0f)
138 : 2.0f * asin_sqrt_u);
139}
140
141} // namespace math
142
143} // namespace LIBC_NAMESPACE_DECL
144
145#endif // LIBC_TYPES_HAS_FLOAT16
146
147#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ACOSPIF16_H
148