xref: /aosp_15_r20/external/llvm-libc/src/math/generic/acoshf.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Single-precision acosh 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/acoshf.h"
10 #include "src/__support/FPUtil/FEnvImpl.h"
11 #include "src/__support/FPUtil/FPBits.h"
12 #include "src/__support/FPUtil/PolyEval.h"
13 #include "src/__support/FPUtil/multiply_add.h"
14 #include "src/__support/FPUtil/sqrt.h"
15 #include "src/__support/macros/config.h"
16 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
17 #include "src/math/generic/common_constants.h"
18 #include "src/math/generic/explogxf.h"
19 
20 namespace LIBC_NAMESPACE_DECL {
21 
22 LLVM_LIBC_FUNCTION(float, acoshf, (float x)) {
23   using FPBits_t = typename fputil::FPBits<float>;
24   FPBits_t xbits(x);
25   uint32_t x_u = xbits.uintval();
26 
27   if (LIBC_UNLIKELY(x <= 1.0f)) {
28     if (x == 1.0f)
29       return 0.0f;
30     // x < 1.
31     fputil::set_errno_if_required(EDOM);
32     fputil::raise_except_if_required(FE_INVALID);
33     return FPBits_t::quiet_nan().get_val();
34   }
35 
36   if (LIBC_UNLIKELY(x_u >= 0x4f8ffb03)) {
37     if (LIBC_UNLIKELY(xbits.is_inf_or_nan()))
38       return x;
39 
40     // Helper functions to set results for exceptional cases.
__anondded61990102(float r) 41     auto round_result_slightly_down = [](float r) -> float {
42       volatile float tmp = r;
43       tmp = tmp - 0x1.0p-25f;
44       return tmp;
45     };
__anondded61990202(float r) 46     auto round_result_slightly_up = [](float r) -> float {
47       volatile float tmp = r;
48       tmp = tmp + 0x1.0p-25f;
49       return tmp;
50     };
51 
52     switch (x_u) {
53     case 0x4f8ffb03: // x = 0x1.1ff606p32f
54       return round_result_slightly_up(0x1.6fdd34p4f);
55     case 0x5c569e88: // x = 0x1.ad3d1p57f
56       return round_result_slightly_up(0x1.45c146p5f);
57     case 0x5e68984e: // x = 0x1.d1309cp61f
58       return round_result_slightly_up(0x1.5c9442p5f);
59     case 0x655890d3: // x = 0x1.b121a6p75f
60       return round_result_slightly_down(0x1.a9a3f2p5f);
61     case 0x6eb1a8ec: // x = 0x1.6351d8p94f
62       return round_result_slightly_down(0x1.08b512p6f);
63     case 0x7997f30a: // x = 0x1.2fe614p116f
64       return round_result_slightly_up(0x1.451436p6f);
65     }
66   }
67 
68   double x_d = static_cast<double>(x);
69   // acosh(x) = log(x + sqrt(x^2 - 1))
70   return static_cast<float>(log_eval(
71       x_d + fputil::sqrt<double>(fputil::multiply_add(x_d, x_d, -1.0))));
72 }
73 
74 } // namespace LIBC_NAMESPACE_DECL
75