xref: /llvm-project/libc/src/math/generic/exp2f16.cpp (revision ce65d4e974145199554d759c8cb0423df615767a)
1936515c7SOverMighty //===-- Half-precision 2^x function ---------------------------------------===//
2936515c7SOverMighty //
3936515c7SOverMighty // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4936515c7SOverMighty // See https://llvm.org/LICENSE.txt for license information.
5936515c7SOverMighty // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6936515c7SOverMighty //
7936515c7SOverMighty //===----------------------------------------------------------------------===//
8936515c7SOverMighty 
9936515c7SOverMighty #include "src/math/exp2f16.h"
1059338ad8SOverMighty #include "expxf16.h"
11936515c7SOverMighty #include "hdr/errno_macros.h"
12936515c7SOverMighty #include "hdr/fenv_macros.h"
13936515c7SOverMighty #include "src/__support/FPUtil/FEnvImpl.h"
14936515c7SOverMighty #include "src/__support/FPUtil/FPBits.h"
15127349fcSOverMighty #include "src/__support/FPUtil/cast.h"
16936515c7SOverMighty #include "src/__support/FPUtil/except_value_utils.h"
17936515c7SOverMighty #include "src/__support/FPUtil/rounding_mode.h"
18936515c7SOverMighty #include "src/__support/common.h"
19936515c7SOverMighty #include "src/__support/macros/config.h"
20936515c7SOverMighty #include "src/__support/macros/optimization.h"
21936515c7SOverMighty 
22936515c7SOverMighty namespace LIBC_NAMESPACE_DECL {
23936515c7SOverMighty 
24936515c7SOverMighty static constexpr fputil::ExceptValues<float16, 3> EXP2F16_EXCEPTS = {{
25936515c7SOverMighty     // (input, RZ output, RU offset, RD offset, RN offset)
26936515c7SOverMighty     // x = 0x1.714p-11, exp2f16(x) = 0x1p+0 (RZ)
27936515c7SOverMighty     {0x11c5U, 0x3c00U, 1U, 0U, 1U},
28936515c7SOverMighty     // x = -0x1.558p-4, exp2f16(x) = 0x1.e34p-1 (RZ)
29936515c7SOverMighty     {0xad56U, 0x3b8dU, 1U, 0U, 0U},
30936515c7SOverMighty     // x = -0x1.d5cp-4, exp2f16(x) = 0x1.d8cp-1 (RZ)
31936515c7SOverMighty     {0xaf57U, 0x3b63U, 1U, 0U, 0U},
32936515c7SOverMighty }};
33936515c7SOverMighty 
34936515c7SOverMighty LLVM_LIBC_FUNCTION(float16, exp2f16, (float16 x)) {
35936515c7SOverMighty   using FPBits = fputil::FPBits<float16>;
36936515c7SOverMighty   FPBits x_bits(x);
37936515c7SOverMighty 
38936515c7SOverMighty   uint16_t x_u = x_bits.uintval();
39936515c7SOverMighty   uint16_t x_abs = x_u & 0x7fffU;
40936515c7SOverMighty 
41936515c7SOverMighty   // When |x| >= 16, or x is NaN.
42936515c7SOverMighty   if (LIBC_UNLIKELY(x_abs >= 0x4c00U)) {
43936515c7SOverMighty     // exp2(NaN) = NaN
44936515c7SOverMighty     if (x_bits.is_nan()) {
45936515c7SOverMighty       if (x_bits.is_signaling_nan()) {
46936515c7SOverMighty         fputil::raise_except_if_required(FE_INVALID);
47936515c7SOverMighty         return FPBits::quiet_nan().get_val();
48936515c7SOverMighty       }
49936515c7SOverMighty 
50936515c7SOverMighty       return x;
51936515c7SOverMighty     }
52936515c7SOverMighty 
53936515c7SOverMighty     // When x >= 16.
54936515c7SOverMighty     if (x_bits.is_pos()) {
55936515c7SOverMighty       // exp2(+inf) = +inf
56936515c7SOverMighty       if (x_bits.is_inf())
57936515c7SOverMighty         return FPBits::inf().get_val();
58936515c7SOverMighty 
59936515c7SOverMighty       switch (fputil::quick_get_round()) {
60936515c7SOverMighty       case FE_TONEAREST:
61936515c7SOverMighty       case FE_UPWARD:
62936515c7SOverMighty         fputil::set_errno_if_required(ERANGE);
63936515c7SOverMighty         fputil::raise_except_if_required(FE_OVERFLOW);
64936515c7SOverMighty         return FPBits::inf().get_val();
65936515c7SOverMighty       default:
66936515c7SOverMighty         return FPBits::max_normal().get_val();
67936515c7SOverMighty       }
68936515c7SOverMighty     }
69936515c7SOverMighty 
70936515c7SOverMighty     // When x <= -25.
71936515c7SOverMighty     if (x_u >= 0xce40U) {
72936515c7SOverMighty       // exp2(-inf) = +0
73936515c7SOverMighty       if (x_bits.is_inf())
74936515c7SOverMighty         return FPBits::zero().get_val();
75936515c7SOverMighty 
76936515c7SOverMighty       fputil::set_errno_if_required(ERANGE);
77936515c7SOverMighty       fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
78936515c7SOverMighty 
79936515c7SOverMighty       if (fputil::fenv_is_round_up())
80936515c7SOverMighty         return FPBits::min_subnormal().get_val();
81936515c7SOverMighty       return FPBits::zero().get_val();
82936515c7SOverMighty     }
83936515c7SOverMighty   }
84936515c7SOverMighty 
85936515c7SOverMighty   if (auto r = EXP2F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
86936515c7SOverMighty     return r.value();
87936515c7SOverMighty 
88*ce65d4e9SOverMighty   // exp2(x) = exp2(hi + mid) * exp2(lo)
89*ce65d4e9SOverMighty   auto [exp2_hi_mid, exp2_lo] = exp2_range_reduction(x);
90127349fcSOverMighty   return fputil::cast<float16>(exp2_hi_mid * exp2_lo);
91936515c7SOverMighty }
92936515c7SOverMighty 
93936515c7SOverMighty } // namespace LIBC_NAMESPACE_DECL
94