1//===-- Implementation header for asinf16 -----------------------*- 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_ASINF16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINF16_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 asinf16(float16 x) {
29
30 // Generated by Sollya using the following command:
31 // > round(pi/2, D, RN);
32 constexpr float PI_2 = 0x1.921fb54442d18p0f;
33
34 using FPBits = fputil::FPBits<float16>;
35 FPBits xbits(x);
36
37 uint16_t x_u = xbits.uintval();
38 uint16_t x_abs = x_u & 0x7fff;
39 float xf = x;
40
41 // |x| > 0x1p0, |x| > 1, or x is NaN.
42 if (LIBC_UNLIKELY(x_abs > 0x3c00)) {
43 // asinf16(NaN) = NaN
44 if (xbits.is_nan()) {
45 if (xbits.is_signaling_nan()) {
46 fputil::raise_except_if_required(FE_INVALID);
47 return FPBits::quiet_nan().get_val();
48 }
49
50 return x;
51 }
52
53 // 1 < |x| <= +/-inf
54 fputil::raise_except_if_required(FE_INVALID);
55 fputil::set_errno_if_required(EDOM);
56
57 return FPBits::quiet_nan().get_val();
58 }
59
60 float xsq = xf * xf;
61
62 // |x| <= 0x1p-1, |x| <= 0.5
63 if (x_abs <= 0x3800) {
64 // asinf16(+/-0) = +/-0
65 if (LIBC_UNLIKELY(x_abs == 0))
66 return x;
67
68 // Exhaustive tests show that,
69 // for |x| <= 0x1.878p-9, when:
70 // x > 0, and rounding upward, or
71 // x < 0, and rounding downward, then,
72 // asin(x) = x * 2^-11 + x
73 // else, in other rounding modes,
74 // asin(x) = x
75 if (LIBC_UNLIKELY(x_abs <= 0x1a1e)) {
76#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
77 int rounding = fputil::quick_get_round();
78
79 if ((xbits.is_pos() && rounding == FE_UPWARD) ||
80 (xbits.is_neg() && rounding == FE_DOWNWARD))
81 return fputil::cast<float16>(x: fputil::multiply_add(x: xf, y: 0x1.0p-11f, z: xf));
82#endif
83 return x;
84 }
85
86 // Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:
87 // > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
88 float result =
89 fputil::polyeval(x: xsq, a0: 0x1.000002p0f, a: 0x1.554c2ap-3f, a: 0x1.3541ccp-4f,
90 a: 0x1.43b2d6p-5f, a: 0x1.a0d73ep-5f);
91 return fputil::cast<float16>(x: xf * result);
92 }
93
94 // When |x| > 0.5, assume that 0.5 < |x| <= 1,
95 //
96 // Step-by-step range-reduction proof:
97 // 1: Let y = asin(x), such that, x = sin(y)
98 // 2: From complimentary angle identity:
99 // x = sin(y) = cos(pi/2 - y)
100 // 3: Let z = pi/2 - y, such that x = cos(z)
101 // 4: From double angle formula; cos(2A) = 1 - sin^2(A):
102 // z = 2A, z/2 = A
103 // cos(z) = 1 - 2 * sin^2(z/2)
104 // 5: Make sin(z/2) subject of the formula:
105 // sin(z/2) = sqrt((1 - cos(z))/2)
106 // 6: Recall [3]; x = cos(z). Therefore:
107 // sin(z/2) = sqrt((1 - x)/2)
108 // 7: Let u = (1 - x)/2
109 // 8: Therefore:
110 // asin(sqrt(u)) = z/2
111 // 2 * asin(sqrt(u)) = z
112 // 9: Recall [3], z = pi/2 - y. Therefore:
113 // y = pi/2 - z
114 // y = pi/2 - 2 * asin(sqrt(u))
115 // 10: Recall [1], y = asin(x). Therefore:
116 // asin(x) = pi/2 - 2 * asin(sqrt(u))
117 //
118 // WHY?
119 // 11: Recall [7], u = (1 - x)/2
120 // 12: Since 0.5 < x <= 1, therefore:
121 // 0 <= u <= 0.25 and 0 <= sqrt(u) <= 0.5
122 //
123 // Hence, we can reuse the same [0, 0.5] domain polynomial approximation for
124 // Step [10] as `sqrt(u)` is in range.
125
126 // 0x1p-1 < |x| <= 0x1p0, 0.5 < |x| <= 1.0
127 float xf_abs = (xf < 0 ? -xf : xf);
128 float sign = (xbits.uintval() >> 15 == 1 ? -1.0 : 1.0);
129 float u = fputil::multiply_add(x: -0.5f, y: xf_abs, z: 0.5f);
130 float u_sqrt = fputil::sqrt<float>(x: u);
131
132 // Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:
133 // > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
134 float asin_sqrt_u =
135 u_sqrt * fputil::polyeval(x: u, a0: 0x1.000002p0f, a: 0x1.554c2ap-3f,
136 a: 0x1.3541ccp-4f, a: 0x1.43b2d6p-5f, a: 0x1.a0d73ep-5f);
137
138 return fputil::cast<float16>(x: sign *
139 fputil::multiply_add(x: -2.0f, y: asin_sqrt_u, z: PI_2));
140}
141
142} // namespace math
143
144} // namespace LIBC_NAMESPACE_DECL
145
146#endif // LIBC_TYPES_HAS_FLOAT16
147
148#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ASINF16_H
149