Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / math / generic / acoshf.cpp
blob9438be1bee74eb4614739319a35f200b17c5f568
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 //===----------------------------------------------------------------------===//
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/optimization.h" // LIBC_UNLIKELY
16 #include "src/math/generic/common_constants.h"
17 #include "src/math/generic/explogxf.h"
19 namespace LIBC_NAMESPACE {
21 LLVM_LIBC_FUNCTION(float, acoshf, (float x)) {
22 using FPBits_t = typename fputil::FPBits<float>;
23 FPBits_t xbits(x);
24 uint32_t x_u = xbits.uintval();
26 if (LIBC_UNLIKELY(x <= 1.0f)) {
27 if (x == 1.0f)
28 return 0.0f;
29 // x < 1.
30 fputil::set_errno_if_required(EDOM);
31 fputil::raise_except_if_required(FE_INVALID);
32 return FPBits_t::build_quiet_nan(0);
35 if (LIBC_UNLIKELY(x_u >= 0x4f8ffb03)) {
36 // Check for exceptional values.
37 uint32_t x_abs = x_u & FPBits_t::FloatProp::EXP_MANT_MASK;
38 if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
39 // x is +inf or NaN.
40 return x;
43 // Helper functions to set results for exceptional cases.
44 auto round_result_slightly_down = [](float r) -> float {
45 volatile float tmp = r;
46 tmp = tmp - 0x1.0p-25f;
47 return tmp;
49 auto round_result_slightly_up = [](float r) -> float {
50 volatile float tmp = r;
51 tmp = tmp + 0x1.0p-25f;
52 return tmp;
55 switch (x_u) {
56 case 0x4f8ffb03: // x = 0x1.1ff606p32f
57 return round_result_slightly_up(0x1.6fdd34p4f);
58 case 0x5c569e88: // x = 0x1.ad3d1p57f
59 return round_result_slightly_up(0x1.45c146p5f);
60 case 0x5e68984e: // x = 0x1.d1309cp61f
61 return round_result_slightly_up(0x1.5c9442p5f);
62 case 0x655890d3: // x = 0x1.b121a6p75f
63 return round_result_slightly_down(0x1.a9a3f2p5f);
64 case 0x6eb1a8ec: // x = 0x1.6351d8p94f
65 return round_result_slightly_down(0x1.08b512p6f);
66 case 0x7997f30a: // x = 0x1.2fe614p116f
67 return round_result_slightly_up(0x1.451436p6f);
71 double x_d = static_cast<double>(x);
72 // acosh(x) = log(x + sqrt(x^2 - 1))
73 return static_cast<float>(
74 log_eval(x_d + fputil::sqrt(fputil::multiply_add(x_d, x_d, -1.0))));
77 } // namespace LIBC_NAMESPACE