|
| 1 | +//===-- Implementation header for expm1f16 ----------------------*- 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_EXPM1F16_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1F16_H |
| 11 | + |
| 12 | +#include "include/llvm-libc-macros/float16-macros.h" |
| 13 | + |
| 14 | +#ifdef LIBC_TYPES_HAS_FLOAT16 |
| 15 | + |
| 16 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 17 | +#include "src/__support/FPUtil/FPBits.h" |
| 18 | +#include "src/__support/FPUtil/PolyEval.h" |
| 19 | +#include "src/__support/FPUtil/cast.h" |
| 20 | +#include "src/__support/FPUtil/except_value_utils.h" |
| 21 | +#include "src/__support/FPUtil/multiply_add.h" |
| 22 | +#include "src/__support/FPUtil/rounding_mode.h" |
| 23 | +#include "src/__support/common.h" |
| 24 | +#include "src/__support/macros/config.h" |
| 25 | +#include "src/__support/macros/optimization.h" |
| 26 | +#include "src/__support/math/expxf16_utils.h" |
| 27 | + |
| 28 | +namespace LIBC_NAMESPACE_DECL { |
| 29 | + |
| 30 | +namespace math { |
| 31 | + |
| 32 | +LIBC_INLINE static constexpr float16 expm1f16(float16 x) { |
| 33 | +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 34 | + constexpr fputil::ExceptValues<float16, 1> EXPM1F16_EXCEPTS_LO = {{ |
| 35 | + // (input, RZ output, RU offset, RD offset, RN offset) |
| 36 | + // x = 0x1.564p-5, expm1f16(x) = 0x1.5d4p-5 (RZ) |
| 37 | + {0x2959U, 0x2975U, 1U, 0U, 1U}, |
| 38 | + }}; |
| 39 | + |
| 40 | +#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 41 | + constexpr size_t N_EXPM1F16_EXCEPTS_HI = 2; |
| 42 | +#else |
| 43 | + constexpr size_t N_EXPM1F16_EXCEPTS_HI = 3; |
| 44 | +#endif |
| 45 | + |
| 46 | + constexpr fputil::ExceptValues<float16, N_EXPM1F16_EXCEPTS_HI> |
| 47 | + EXPM1F16_EXCEPTS_HI = {{ |
| 48 | + // (input, RZ output, RU offset, RD offset, RN offset) |
| 49 | + // x = 0x1.c34p+0, expm1f16(x) = 0x1.34cp+2 (RZ) |
| 50 | + {0x3f0dU, 0x44d3U, 1U, 0U, 1U}, |
| 51 | + // x = -0x1.e28p-3, expm1f16(x) = -0x1.adcp-3 (RZ) |
| 52 | + {0xb38aU, 0xb2b7U, 0U, 1U, 1U}, |
| 53 | +#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 54 | + // x = 0x1.a08p-3, exp10m1f(x) = 0x1.cdcp-3 (RZ) |
| 55 | + {0x3282U, 0x3337U, 1U, 0U, 0U}, |
| 56 | +#endif |
| 57 | + }}; |
| 58 | +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 59 | + |
| 60 | + using namespace math::expxf16_internal; |
| 61 | + using FPBits = fputil::FPBits<float16>; |
| 62 | + FPBits x_bits(x); |
| 63 | + |
| 64 | + uint16_t x_u = x_bits.uintval(); |
| 65 | + uint16_t x_abs = x_u & 0x7fffU; |
| 66 | + |
| 67 | + // When |x| <= 2^(-3), or |x| >= -11 * log(2), or x is NaN. |
| 68 | + if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x47a0U)) { |
| 69 | + // expm1(NaN) = NaN |
| 70 | + if (x_bits.is_nan()) { |
| 71 | + if (x_bits.is_signaling_nan()) { |
| 72 | + fputil::raise_except_if_required(FE_INVALID); |
| 73 | + return FPBits::quiet_nan().get_val(); |
| 74 | + } |
| 75 | + |
| 76 | + return x; |
| 77 | + } |
| 78 | + |
| 79 | + // expm1(+/-0) = +/-0 |
| 80 | + if (x_abs == 0) |
| 81 | + return x; |
| 82 | + |
| 83 | + // When x >= 16 * log(2). |
| 84 | + if (x_bits.is_pos() && x_abs >= 0x498cU) { |
| 85 | + // expm1(+inf) = +inf |
| 86 | + if (x_bits.is_inf()) |
| 87 | + return FPBits::inf().get_val(); |
| 88 | + |
| 89 | + switch (fputil::quick_get_round()) { |
| 90 | + case FE_TONEAREST: |
| 91 | + case FE_UPWARD: |
| 92 | + fputil::set_errno_if_required(ERANGE); |
| 93 | + fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT); |
| 94 | + return FPBits::inf().get_val(); |
| 95 | + default: |
| 96 | + return FPBits::max_normal().get_val(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // When x <= -11 * log(2). |
| 101 | + if (x_u >= 0xc7a0U) { |
| 102 | + // expm1(-inf) = -1 |
| 103 | + if (x_bits.is_inf()) |
| 104 | + return FPBits::one(Sign::NEG).get_val(); |
| 105 | + |
| 106 | + // When x > -0x1.0ap+3, round(expm1(x), HP, RN) = -1. |
| 107 | + if (x_u > 0xc828U) |
| 108 | + return fputil::round_result_slightly_up( |
| 109 | + FPBits::one(Sign::NEG).get_val()); |
| 110 | + // When x <= -0x1.0ap+3, round(expm1(x), HP, RN) = -0x1.ffcp-1. |
| 111 | + return fputil::round_result_slightly_down( |
| 112 | + fputil::cast<float16>(-0x1.ffcp-1)); |
| 113 | + } |
| 114 | + |
| 115 | + // When 0 < |x| <= 2^(-3). |
| 116 | + if (x_abs <= 0x3000U && !x_bits.is_zero()) { |
| 117 | + |
| 118 | +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 119 | + if (auto r = EXPM1F16_EXCEPTS_LO.lookup(x_u); |
| 120 | + LIBC_UNLIKELY(r.has_value())) |
| 121 | + return r.value(); |
| 122 | +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 123 | + |
| 124 | + float xf = x; |
| 125 | + // Degree-5 minimax polynomial generated by Sollya with the following |
| 126 | + // commands: |
| 127 | + // > display = hexadecimal; |
| 128 | + // > P = fpminimax(expm1(x)/x, 4, [|SG...|], [-2^-3, 2^-3]); |
| 129 | + // > x * P; |
| 130 | + return fputil::cast<float16>( |
| 131 | + xf * fputil::polyeval(xf, 0x1p+0f, 0x1.fffff8p-2f, 0x1.555556p-3f, |
| 132 | + 0x1.55905ep-5f, 0x1.1124c2p-7f)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 137 | + if (auto r = EXPM1F16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
| 138 | + return r.value(); |
| 139 | +#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 140 | + |
| 141 | + // exp(x) = exp(hi + mid) * exp(lo) |
| 142 | + auto [exp_hi_mid, exp_lo] = exp_range_reduction(x); |
| 143 | + // expm1(x) = exp(hi + mid) * exp(lo) - 1 |
| 144 | + return fputil::cast<float16>(fputil::multiply_add(exp_hi_mid, exp_lo, -1.0f)); |
| 145 | +} |
| 146 | + |
| 147 | +} // namespace math |
| 148 | + |
| 149 | +} // namespace LIBC_NAMESPACE_DECL |
| 150 | + |
| 151 | +#endif // LIBC_TYPES_HAS_FLOAT16 |
| 152 | + |
| 153 | +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1F16_H |
0 commit comments