1 //===-- Half-precision 2^x - 1 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/exp2m1f16.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/multiply_add.h" 19 #include "src/__support/FPUtil/rounding_mode.h" 20 #include "src/__support/common.h" 21 #include "src/__support/macros/config.h" 22 #include "src/__support/macros/optimization.h" 23 #include "src/__support/macros/properties/cpu_features.h" 24 25 namespace LIBC_NAMESPACE_DECL { 26 27 static constexpr fputil::ExceptValues<float16, 6> EXP2M1F16_EXCEPTS_LO = {{ 28 // (input, RZ output, RU offset, RD offset, RN offset) 29 // x = 0x1.cf4p-13, exp2m1f16(x) = 0x1.41p-13 (RZ) 30 {0x0b3dU, 0x0904U, 1U, 0U, 1U}, 31 // x = 0x1.4fcp-12, exp2m1f16(x) = 0x1.d14p-13 (RZ) 32 {0x0d3fU, 0x0b45U, 1U, 0U, 1U}, 33 // x = 0x1.63p-11, exp2m1f16(x) = 0x1.ec4p-12 (RZ) 34 {0x118cU, 0x0fb1U, 1U, 0U, 0U}, 35 // x = 0x1.6fp-7, exp2m1f16(x) = 0x1.fe8p-8 (RZ) 36 {0x21bcU, 0x1ffaU, 1U, 0U, 1U}, 37 // x = -0x1.c6p-10, exp2m1f16(x) = -0x1.3a8p-10 (RZ) 38 {0x9718U, 0x94eaU, 0U, 1U, 0U}, 39 // x = -0x1.cfcp-10, exp2m1f16(x) = -0x1.414p-10 (RZ) 40 {0x973fU, 0x9505U, 0U, 1U, 0U}, 41 }}; 42 43 #ifdef LIBC_TARGET_CPU_HAS_FMA 44 static constexpr size_t N_EXP2M1F16_EXCEPTS_HI = 6; 45 #else 46 static constexpr size_t N_EXP2M1F16_EXCEPTS_HI = 7; 47 #endif 48 49 static constexpr fputil::ExceptValues<float16, N_EXP2M1F16_EXCEPTS_HI> 50 EXP2M1F16_EXCEPTS_HI = {{ 51 // (input, RZ output, RU offset, RD offset, RN offset) 52 // x = 0x1.e58p-3, exp2m1f16(x) = 0x1.6dcp-3 (RZ) 53 {0x3396U, 0x31b7U, 1U, 0U, 0U}, 54 #ifndef LIBC_TARGET_CPU_HAS_FMA 55 // x = 0x1.2e8p-2, exp2m1f16(x) = 0x1.d14p-3 (RZ) 56 {0x34baU, 0x3345U, 1U, 0U, 0U}, 57 #endif 58 // x = 0x1.ad8p-2, exp2m1f16(x) = 0x1.598p-2 (RZ) 59 {0x36b6U, 0x3566U, 1U, 0U, 0U}, 60 #ifdef LIBC_TARGET_CPU_HAS_FMA 61 // x = 0x1.edcp-2, exp2m1f16(x) = 0x1.964p-2 (RZ) 62 {0x37b7U, 0x3659U, 1U, 0U, 1U}, 63 #endif 64 // x = -0x1.804p-3, exp2m1f16(x) = -0x1.f34p-4 (RZ) 65 {0xb201U, 0xafcdU, 0U, 1U, 1U}, 66 // x = -0x1.f3p-3, exp2m1f16(x) = -0x1.3e4p-3 (RZ) 67 {0xb3ccU, 0xb0f9U, 0U, 1U, 0U}, 68 // x = -0x1.294p-1, exp2m1f16(x) = -0x1.53p-2 (RZ) 69 {0xb8a5U, 0xb54cU, 0U, 1U, 1U}, 70 #ifndef LIBC_TARGET_CPU_HAS_FMA 71 // x = -0x1.a34p-1, exp2m1f16(x) = -0x1.bb4p-2 (RZ) 72 {0xba8dU, 0xb6edU, 0U, 1U, 1U}, 73 #endif 74 }}; 75 76 LLVM_LIBC_FUNCTION(float16, exp2m1f16, (float16 x)) { 77 using FPBits = fputil::FPBits<float16>; 78 FPBits x_bits(x); 79 80 uint16_t x_u = x_bits.uintval(); 81 uint16_t x_abs = x_u & 0x7fffU; 82 83 // When |x| <= 2^(-3), or |x| >= 11, or x is NaN. 84 if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x4980U)) { 85 // exp2m1(NaN) = NaN 86 if (x_bits.is_nan()) { 87 if (x_bits.is_signaling_nan()) { 88 fputil::raise_except_if_required(FE_INVALID); 89 return FPBits::quiet_nan().get_val(); 90 } 91 92 return x; 93 } 94 95 // When x >= 16. 96 if (x_u >= 0x4c00 && x_bits.is_pos()) { 97 // exp2m1(+inf) = +inf 98 if (x_bits.is_inf()) 99 return FPBits::inf().get_val(); 100 101 switch (fputil::quick_get_round()) { 102 case FE_TONEAREST: 103 case FE_UPWARD: 104 fputil::set_errno_if_required(ERANGE); 105 fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT); 106 return FPBits::inf().get_val(); 107 default: 108 return FPBits::max_normal().get_val(); 109 } 110 } 111 112 // When x < -11. 113 if (x_u > 0xc980U) { 114 // exp2m1(-inf) = -1 115 if (x_bits.is_inf()) 116 return FPBits::one(Sign::NEG).get_val(); 117 118 // When -12 < x < -11, round(2^x - 1, HP, RN) = -0x1.ffcp-1. 119 if (x_u < 0xca00U) 120 return fputil::round_result_slightly_down( 121 fputil::cast<float16>(-0x1.ffcp-1)); 122 123 // When x <= -12, round(2^x - 1, HP, RN) = -1. 124 switch (fputil::quick_get_round()) { 125 case FE_TONEAREST: 126 case FE_DOWNWARD: 127 return FPBits::one(Sign::NEG).get_val(); 128 default: 129 return fputil::cast<float16>(-0x1.ffcp-1); 130 } 131 } 132 133 // When |x| <= 2^(-3). 134 if (x_abs <= 0x3000U) { 135 if (auto r = EXP2M1F16_EXCEPTS_LO.lookup(x_u); 136 LIBC_UNLIKELY(r.has_value())) 137 return r.value(); 138 139 float xf = x; 140 // Degree-5 minimax polynomial generated by Sollya with the following 141 // commands: 142 // > display = hexadecimal; 143 // > P = fpminimax((2^x - 1)/x, 4, [|SG...|], [-2^-3, 2^-3]); 144 // > x * P; 145 return fputil::cast<float16>( 146 xf * fputil::polyeval(xf, 0x1.62e43p-1f, 0x1.ebfbdep-3f, 147 0x1.c6af88p-5f, 0x1.3b45d6p-7f, 148 0x1.641e7cp-10f)); 149 } 150 } 151 152 if (auto r = EXP2M1F16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value())) 153 return r.value(); 154 155 // exp2(x) = exp2(hi + mid) * exp2(lo) 156 auto [exp2_hi_mid, exp2_lo] = exp2_range_reduction(x); 157 // exp2m1(x) = exp2(hi + mid) * exp2(lo) - 1 158 return fputil::cast<float16>( 159 fputil::multiply_add(exp2_hi_mid, exp2_lo, -1.0f)); 160 } 161 162 } // namespace LIBC_NAMESPACE_DECL 163