1//===-- Half-precision asinf16(x) 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/asinf16.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/PolyEval.h"
15#include "src/__support/FPUtil/cast.h"
16#include "src/__support/FPUtil/multiply_add.h"
17#include "src/__support/FPUtil/sqrt.h"
18#include "src/__support/macros/optimization.h"
19
20namespace LIBC_NAMESPACE_DECL {
21
22// Generated by Sollya using the following command:
23// > round(pi/2, D, RN);
24static constexpr float PI_2 = 0x1.921fb54442d18p0f;
25
26LLVM_LIBC_FUNCTION(float16, asinf16, (float16 x)) {
27 using FPBits = fputil::FPBits<float16>;
28 FPBits xbits(x);
29
30 uint16_t x_u = xbits.uintval();
31 uint16_t x_abs = x_u & 0x7fff;
32 float xf = x;
33
34 // |x| > 0x1p0, |x| > 1, or x is NaN.
35 if (LIBC_UNLIKELY(x_abs > 0x3c00)) {
36 // asinf16(NaN) = NaN
37 if (xbits.is_nan()) {
38 if (xbits.is_signaling_nan()) {
39 fputil::raise_except_if_required(FE_INVALID);
40 return FPBits::quiet_nan().get_val();
41 }
42
43 return x;
44 }
45
46 // 1 < |x| <= +/-inf
47 fputil::raise_except_if_required(FE_INVALID);
48 fputil::set_errno_if_required(EDOM);
49
50 return FPBits::quiet_nan().get_val();
51 }
52
53 float xsq = xf * xf;
54
55 // |x| <= 0x1p-1, |x| <= 0.5
56 if (x_abs <= 0x3800) {
57 // asinf16(+/-0) = +/-0
58 if (LIBC_UNLIKELY(x_abs == 0))
59 return x;
60
61 // Exhaustive tests show that,
62 // for |x| <= 0x1.878p-9, when:
63 // x > 0, and rounding upward, or
64 // x < 0, and rounding downward, then,
65 // asin(x) = x * 2^-11 + x
66 // else, in other rounding modes,
67 // asin(x) = x
68 if (LIBC_UNLIKELY(x_abs <= 0x1a1e)) {
69 int rounding = fputil::quick_get_round();
70
71 if ((xbits.is_pos() && rounding == FE_UPWARD) ||
72 (xbits.is_neg() && rounding == FE_DOWNWARD))
73 return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
74 return x;
75 }
76
77 // Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:
78 // > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
79 float result =
80 fputil::polyeval(xsq, 0x1.000002p0f, 0x1.554c2ap-3f, 0x1.3541ccp-4f,
81 0x1.43b2d6p-5f, 0x1.a0d73ep-5f);
82 return fputil::cast<float16>(xf * result);
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 - 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 //
109 // WHY?
110 // 11: Recall [7], u = (1 - x)/2
111 // 12: Since 0.5 < x <= 1, therefore:
112 // 0 <= u <= 0.25 and 0 <= sqrt(u) <= 0.5
113 //
114 // Hence, we can reuse the same [0, 0.5] domain polynomial approximation for
115 // Step [10] as `sqrt(u)` is in range.
116
117 // 0x1p-1 < |x| <= 0x1p0, 0.5 < |x| <= 1.0
118 float xf_abs = (xf < 0 ? -xf : xf);
119 float sign = (xbits.uintval() >> 15 == 1 ? -1.0 : 1.0);
120 float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f);
121 float u_sqrt = fputil::sqrt<float>(u);
122
123 // Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:
124 // > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
125 float asin_sqrt_u =
126 u_sqrt * fputil::polyeval(u, 0x1.000002p0f, 0x1.554c2ap-3f,
127 0x1.3541ccp-4f, 0x1.43b2d6p-5f, 0x1.a0d73ep-5f);
128
129 return fputil::cast<float16>(sign *
130 fputil::multiply_add(-2.0f, asin_sqrt_u, PI_2));
131}
132
133} // namespace LIBC_NAMESPACE_DECL
134

source code of libc/src/math/generic/asinf16.cpp