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