|
| 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/errno_macros.h" |
| 17 | +#include "hdr/fenv_macros.h" |
| 18 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 19 | +#include "src/__support/FPUtil/FPBits.h" |
| 20 | +#include "src/__support/FPUtil/PolyEval.h" |
| 21 | +#include "src/__support/FPUtil/cast.h" |
| 22 | +#include "src/__support/FPUtil/except_value_utils.h" |
| 23 | +#include "src/__support/FPUtil/rounding_mode.h" |
| 24 | +#include "src/__support/common.h" |
| 25 | +#include "src/__support/macros/config.h" |
| 26 | +#include "src/__support/macros/optimization.h" |
| 27 | + |
| 28 | +#include "expf16_utils.h" |
| 29 | + |
| 30 | +namespace LIBC_NAMESPACE_DECL { |
| 31 | + |
| 32 | +namespace math { |
| 33 | + |
| 34 | +static constexpr float16 expf16(float16 x) { |
| 35 | +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 36 | + constexpr fputil::ExceptValues<float16, 2> EXPF16_EXCEPTS_LO = {{ |
| 37 | + // (input, RZ output, RU offset, RD offset, RN offset) |
| 38 | + // x = 0x1.de4p-8, expf16(x) = 0x1.01cp+0 (RZ) |
| 39 | + {0x1f79U, 0x3c07U, 1U, 0U, 0U}, |
| 40 | + // x = 0x1.73cp-6, expf16(x) = 0x1.05cp+0 (RZ) |
| 41 | + {0x25cfU, 0x3c17U, 1U, 0U, 0U}, |
| 42 | + }}; |
| 43 | + |
| 44 | + constexpr fputil::ExceptValues<float16, 3> EXPF16_EXCEPTS_HI = {{ |
| 45 | + // (input, RZ output, RU offset, RD offset, RN offset) |
| 46 | + // x = 0x1.c34p+0, expf16(x) = 0x1.74cp+2 (RZ) |
| 47 | + {0x3f0dU, 0x45d3U, 1U, 0U, 1U}, |
| 48 | + // x = -0x1.488p-5, expf16(x) = 0x1.ebcp-1 (RZ) |
| 49 | + {0xa922U, 0x3bafU, 1U, 0U, 0U}, |
| 50 | + // x = -0x1.55p-5, expf16(x) = 0x1.ebp-1 (RZ) |
| 51 | + {0xa954U, 0x3bacU, 1U, 0U, 0U}, |
| 52 | + }}; |
| 53 | +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 54 | + |
| 55 | + using FPBits = fputil::FPBits<float16>; |
| 56 | + FPBits x_bits(x); |
| 57 | + |
| 58 | + uint16_t x_u = x_bits.uintval(); |
| 59 | + uint16_t x_abs = x_u & 0x7fffU; |
| 60 | + |
| 61 | + // When 0 < |x| <= 2^(-5), or |x| >= 12, or x is NaN. |
| 62 | + if (LIBC_UNLIKELY(x_abs <= 0x2800U || x_abs >= 0x4a00U)) { |
| 63 | + // exp(NaN) = NaN |
| 64 | + if (x_bits.is_nan()) { |
| 65 | + if (x_bits.is_signaling_nan()) { |
| 66 | + fputil::raise_except_if_required(FE_INVALID); |
| 67 | + return FPBits::quiet_nan().get_val(); |
| 68 | + } |
| 69 | + |
| 70 | + return x; |
| 71 | + } |
| 72 | + |
| 73 | + // When x >= 12. |
| 74 | + if (x_bits.is_pos() && x_abs >= 0x4a00U) { |
| 75 | + // exp(+inf) = +inf |
| 76 | + if (x_bits.is_inf()) |
| 77 | + return FPBits::inf().get_val(); |
| 78 | + |
| 79 | + switch (fputil::quick_get_round()) { |
| 80 | + case FE_TONEAREST: |
| 81 | + case FE_UPWARD: |
| 82 | + fputil::set_errno_if_required(ERANGE); |
| 83 | + fputil::raise_except_if_required(FE_OVERFLOW); |
| 84 | + return FPBits::inf().get_val(); |
| 85 | + default: |
| 86 | + return FPBits::max_normal().get_val(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // When x <= -18. |
| 91 | + if (x_u >= 0xcc80U) { |
| 92 | + // exp(-inf) = +0 |
| 93 | + if (x_bits.is_inf()) |
| 94 | + return FPBits::zero().get_val(); |
| 95 | + |
| 96 | + fputil::set_errno_if_required(ERANGE); |
| 97 | + fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT); |
| 98 | + |
| 99 | + switch (fputil::quick_get_round()) { |
| 100 | + case FE_UPWARD: |
| 101 | + return FPBits::min_subnormal().get_val(); |
| 102 | + default: |
| 103 | + return FPBits::zero().get_val(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // When 0 < |x| <= 2^(-5). |
| 108 | + if (x_abs <= 0x2800U && !x_bits.is_zero()) { |
| 109 | +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 110 | + if (auto r = EXPF16_EXCEPTS_LO.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
| 111 | + return r.value(); |
| 112 | +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 113 | + |
| 114 | + float xf = x; |
| 115 | + // Degree-3 minimax polynomial generated by Sollya with the following |
| 116 | + // commands: |
| 117 | + // > display = hexadecimal; |
| 118 | + // > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-5, 2^-5]); |
| 119 | + // > 1 + x * P; |
| 120 | + return fputil::cast<float16>( |
| 121 | + fputil::polyeval(xf, 0x1p+0f, 0x1p+0f, 0x1.0004p-1f, 0x1.555778p-3f)); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 126 | + if (auto r = EXPF16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
| 127 | + return r.value(); |
| 128 | +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 129 | + |
| 130 | + // exp(x) = exp(hi + mid) * exp(lo) |
| 131 | + auto [exp_hi_mid, exp_lo] = exp_range_reduction(x); |
| 132 | + return fputil::cast<float16>(exp_hi_mid * exp_lo); |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace math |
| 136 | + |
| 137 | +} // namespace LIBC_NAMESPACE_DECL |
| 138 | + |
| 139 | +#endif // LIBC_TYPES_HAS_FLOAT16 |
| 140 | + |
| 141 | +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPF16_H |
0 commit comments