1//===-- Implementation header for exp2f16 -----------------------*- 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_EXP2F16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP2F16_H
11
12#include "include/llvm-libc-macros/float16-macros.h"
13
14#ifdef LIBC_TYPES_HAS_FLOAT16
15
16#include "expxf16_utils.h"
17#include "src/__support/FPUtil/FEnvImpl.h"
18#include "src/__support/FPUtil/FPBits.h"
19#include "src/__support/FPUtil/cast.h"
20#include "src/__support/FPUtil/except_value_utils.h"
21#include "src/__support/FPUtil/rounding_mode.h"
22#include "src/__support/common.h"
23#include "src/__support/macros/config.h"
24#include "src/__support/macros/optimization.h"
25
26namespace LIBC_NAMESPACE_DECL {
27
28namespace math {
29
30LIBC_INLINE constexpr float16 exp2f16(float16 x) {
31
32#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
33 constexpr fputil::ExceptValues<float16, 3> EXP2F16_EXCEPTS = {.values: {
34 // (input, RZ output, RU offset, RD offset, RN offset)
35 // x = 0x1.714p-11, exp2f16(x) = 0x1p+0 (RZ)
36 {.input: 0x11c5U, .rnd_towardzero_result: 0x3c00U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
37 // x = -0x1.558p-4, exp2f16(x) = 0x1.e34p-1 (RZ)
38 {.input: 0xad56U, .rnd_towardzero_result: 0x3b8dU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
39 // x = -0x1.d5cp-4, exp2f16(x) = 0x1.d8cp-1 (RZ)
40 {.input: 0xaf57U, .rnd_towardzero_result: 0x3b63U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
41 }};
42#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
43
44 using namespace math::expxf16_internal;
45 using FPBits = fputil::FPBits<float16>;
46 FPBits x_bits(x);
47
48 uint16_t x_u = x_bits.uintval();
49 uint16_t x_abs = x_u & 0x7fffU;
50
51 // When |x| >= 16, or x is NaN.
52 if (LIBC_UNLIKELY(x_abs >= 0x4c00U)) {
53 // exp2(NaN) = NaN
54 if (x_bits.is_nan()) {
55 if (x_bits.is_signaling_nan()) {
56 fputil::raise_except_if_required(FE_INVALID);
57 return FPBits::quiet_nan().get_val();
58 }
59
60 return x;
61 }
62
63 // When x >= 16.
64 if (x_bits.is_pos()) {
65 // exp2(+inf) = +inf
66 if (x_bits.is_inf())
67 return FPBits::inf().get_val();
68
69#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
70 fputil::set_errno_if_required(ERANGE);
71 fputil::raise_except_if_required(FE_OVERFLOW);
72 return FPBits::inf().get_val();
73#else
74 switch (fputil::quick_get_round()) {
75 case FE_TONEAREST:
76 case FE_UPWARD:
77 fputil::set_errno_if_required(ERANGE);
78 fputil::raise_except_if_required(FE_OVERFLOW);
79 return FPBits::inf().get_val();
80 default:
81 return FPBits::max_normal().get_val();
82 }
83#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
84 }
85
86 // When x <= -25.
87 if (x_u >= 0xce40U) {
88 // exp2(-inf) = +0
89 if (x_bits.is_inf())
90 return FPBits::zero().get_val();
91
92 fputil::set_errno_if_required(ERANGE);
93 fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
94
95#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
96 if (fputil::fenv_is_round_up())
97 return FPBits::min_subnormal().get_val();
98#endif
99 return FPBits::zero().get_val();
100 }
101 }
102
103#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
104 if (auto r = EXP2F16_EXCEPTS.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value()))
105 return r.value();
106#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
107
108 // exp2(x) = exp2(hi + mid) * exp2(lo)
109 auto [exp2_hi_mid, exp2_lo] = exp2_range_reduction(x);
110 return fputil::cast<float16>(x: exp2_hi_mid * exp2_lo);
111}
112
113} // namespace math
114
115} // namespace LIBC_NAMESPACE_DECL
116
117#endif // LIBC_TYPES_HAS_FLOAT16
118
119#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP2F16_H
120