1//===-- Implementation header for expf16 ------------------------*- 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_EXPF16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPF16_H
11
12#include "include/llvm-libc-macros/float16-macros.h"
13
14#ifdef LIBC_TYPES_HAS_FLOAT16
15
16#include "hdr/fenv_macros.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/polyeval.h"
22#include "src/__support/common.h"
23#include "src/__support/macros/config.h"
24#include "src/__support/macros/optimization.h"
25#include "src/__support/math/expf16_utils.h"
26
27namespace LIBC_NAMESPACE_DECL {
28
29namespace math {
30
31LIBC_INLINE constexpr float16 expf16(float16 x) {
32#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
33 constexpr fputil::ExceptValues<float16, 2> EXPF16_EXCEPTS_LO = {.values: {
34 // (input, RZ output, RU offset, RD offset, RN offset)
35 // x = 0x1.de4p-8, expf16(x) = 0x1.01cp+0 (RZ)
36 {.input: 0x1f79U, .rnd_towardzero_result: 0x3c07U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
37 // x = 0x1.73cp-6, expf16(x) = 0x1.05cp+0 (RZ)
38 {.input: 0x25cfU, .rnd_towardzero_result: 0x3c17U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
39 }};
40
41 constexpr fputil::ExceptValues<float16, 3> EXPF16_EXCEPTS_HI = {.values: {
42 // (input, RZ output, RU offset, RD offset, RN offset)
43 // x = 0x1.c34p+0, expf16(x) = 0x1.74cp+2 (RZ)
44 {.input: 0x3f0dU, .rnd_towardzero_result: 0x45d3U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
45 // x = -0x1.488p-5, expf16(x) = 0x1.ebcp-1 (RZ)
46 {.input: 0xa922U, .rnd_towardzero_result: 0x3bafU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
47 // x = -0x1.55p-5, expf16(x) = 0x1.ebp-1 (RZ)
48 {.input: 0xa954U, .rnd_towardzero_result: 0x3bacU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
49 }};
50#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
51
52 using FPBits = fputil::FPBits<float16>;
53 FPBits x_bits(x);
54
55 uint16_t x_u = x_bits.uintval();
56 uint16_t x_abs = x_u & 0x7fffU;
57
58 // When 0 < |x| <= 2^(-5), or |x| >= 12, or x is NaN.
59 if (LIBC_UNLIKELY(x_abs <= 0x2800U || x_abs >= 0x4a00U)) {
60 // exp(NaN) = NaN
61 if (x_bits.is_nan()) {
62 if (x_bits.is_signaling_nan()) {
63 fputil::raise_except_if_required(FE_INVALID);
64 return FPBits::quiet_nan().get_val();
65 }
66
67 return x;
68 }
69
70 // When x >= 12.
71 if (x_bits.is_pos() && x_abs >= 0x4a00U) {
72 // exp(+inf) = +inf
73 if (x_bits.is_inf())
74 return FPBits::inf().get_val();
75
76 return fputil::set_overflow<float16>();
77 }
78
79 // When x <= -18.
80 if (x_u >= 0xcc80U) {
81 // exp(-inf) = +0
82 if (x_bits.is_inf())
83 return FPBits::zero().get_val();
84
85 return fputil::set_underflow<float16>();
86 }
87
88 // When 0 < |x| <= 2^(-5).
89 if (x_abs <= 0x2800U && !x_bits.is_zero()) {
90#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
91 if (auto r = EXPF16_EXCEPTS_LO.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value()))
92 return r.value();
93#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
94
95 float xf = x;
96 // Degree-3 minimax polynomial generated by Sollya with the following
97 // commands:
98 // > display = hexadecimal;
99 // > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
100 // > 1 + x * P;
101 return fputil::cast<float16>(
102 x: fputil::polyeval(x: xf, a0: 0x1p+0f, a: 0x1p+0f, a: 0x1.0004p-1f, a: 0x1.555778p-3f));
103 }
104 }
105
106#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
107 if (auto r = EXPF16_EXCEPTS_HI.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value()))
108 return r.value();
109#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
110
111 // exp(x) = exp(hi + mid) * exp(lo)
112 auto [exp_hi_mid, exp_lo] = exp_range_reduction(x);
113 return fputil::cast<float16>(x: exp_hi_mid * exp_lo);
114}
115
116} // namespace math
117
118} // namespace LIBC_NAMESPACE_DECL
119
120#endif // LIBC_TYPES_HAS_FLOAT16
121
122#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPF16_H
123