|
| 1 | +//===-- Half-precision 10^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/exp10f16.h" |
| 10 | +#include "expxf16.h" |
| 11 | +#include "hdr/errno_macros.h" |
| 12 | +#include "hdr/fenv_macros.h" |
| 13 | +#include "src/__support/CPP/array.h" |
| 14 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 15 | +#include "src/__support/FPUtil/FPBits.h" |
| 16 | +#include "src/__support/FPUtil/PolyEval.h" |
| 17 | +#include "src/__support/FPUtil/except_value_utils.h" |
| 18 | +#include "src/__support/FPUtil/multiply_add.h" |
| 19 | +#include "src/__support/FPUtil/nearest_integer.h" |
| 20 | +#include "src/__support/FPUtil/rounding_mode.h" |
| 21 | +#include "src/__support/common.h" |
| 22 | +#include "src/__support/macros/config.h" |
| 23 | +#include "src/__support/macros/optimization.h" |
| 24 | +#include "src/__support/macros/properties/cpu_features.h" |
| 25 | + |
| 26 | +namespace LIBC_NAMESPACE_DECL { |
| 27 | + |
| 28 | +#ifdef LIBC_TARGET_CPU_HAS_FMA |
| 29 | +static constexpr size_t N_EXP10F16_EXCEPTS = 5; |
| 30 | +#else |
| 31 | +static constexpr size_t N_EXP10F16_EXCEPTS = 8; |
| 32 | +#endif |
| 33 | + |
| 34 | +static constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS> |
| 35 | + EXP10F16_EXCEPTS = {{ |
| 36 | + // x = 0x1.8f4p-2, exp10f16(x) = 0x1.3ap+1 (RZ) |
| 37 | + {0x363dU, 0x40e8U, 1U, 0U, 1U}, |
| 38 | + // x = 0x1.95cp-2, exp10f16(x) = 0x1.3ecp+1 (RZ) |
| 39 | + {0x3657U, 0x40fbU, 1U, 0U, 0U}, |
| 40 | + // x = -0x1.018p-4, exp10f16(x) = 0x1.bbp-1 (RZ) |
| 41 | + {0xac06U, 0x3aecU, 1U, 0U, 0U}, |
| 42 | + // x = -0x1.c28p+0, exp10f16(x) = 0x1.1ccp-6 (RZ) |
| 43 | + {0xbf0aU, 0x2473U, 1U, 0U, 0U}, |
| 44 | + // x = -0x1.e1cp+1, exp10f16(x) = 0x1.694p-13 (RZ) |
| 45 | + {0xc387U, 0x09a5U, 1U, 0U, 0U}, |
| 46 | +#ifndef LIBC_TARGET_CPU_HAS_FMA |
| 47 | + // x = 0x1.0cp+1, exp10f16(x) = 0x1.f04p+6 (RZ) |
| 48 | + {0x4030U, 0x57c1U, 1U, 0U, 1U}, |
| 49 | + // x = 0x1.1b8p+1, exp10f16(x) = 0x1.47cp+7 (RZ) |
| 50 | + {0x406eU, 0x591fU, 1U, 0U, 1U}, |
| 51 | + // x = 0x1.1b8p+2, exp10f16(x) = 0x1.a4p+14 (RZ) |
| 52 | + {0x446eU, 0x7690U, 1U, 0U, 1U}, |
| 53 | +#endif |
| 54 | + }}; |
| 55 | + |
| 56 | +// Generated by Sollya with the following commands: |
| 57 | +// > display = hexadecimal; |
| 58 | +// > round(log2(10), SG, RN); |
| 59 | +static constexpr float LOG2F_10 = 0x1.a934fp+1f; |
| 60 | + |
| 61 | +// Generated by Sollya with the following commands: |
| 62 | +// > display = hexadecimal; |
| 63 | +// > round(log10(2), SG, RN); |
| 64 | +static constexpr float LOG10F_2 = 0x1.344136p-2f; |
| 65 | + |
| 66 | +LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) { |
| 67 | + using FPBits = fputil::FPBits<float16>; |
| 68 | + FPBits x_bits(x); |
| 69 | + |
| 70 | + uint16_t x_u = x_bits.uintval(); |
| 71 | + uint16_t x_abs = x_u & 0x7fffU; |
| 72 | + |
| 73 | + // When |x| >= 5, or x is NaN. |
| 74 | + if (LIBC_UNLIKELY(x_abs >= 0x4500U)) { |
| 75 | + // exp10(NaN) = NaN |
| 76 | + if (x_bits.is_nan()) { |
| 77 | + if (x_bits.is_signaling_nan()) { |
| 78 | + fputil::raise_except_if_required(FE_INVALID); |
| 79 | + return FPBits::quiet_nan().get_val(); |
| 80 | + } |
| 81 | + |
| 82 | + return x; |
| 83 | + } |
| 84 | + |
| 85 | + // When x >= 5. |
| 86 | + if (x_bits.is_pos()) { |
| 87 | + // exp10(+inf) = +inf |
| 88 | + if (x_bits.is_inf()) |
| 89 | + return FPBits::inf().get_val(); |
| 90 | + |
| 91 | + switch (fputil::quick_get_round()) { |
| 92 | + case FE_TONEAREST: |
| 93 | + case FE_UPWARD: |
| 94 | + fputil::set_errno_if_required(ERANGE); |
| 95 | + fputil::raise_except_if_required(FE_OVERFLOW); |
| 96 | + return FPBits::inf().get_val(); |
| 97 | + default: |
| 98 | + return FPBits::max_normal().get_val(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // When x <= -8. |
| 103 | + if (x_u >= 0xc800U) { |
| 104 | + // exp10(-inf) = +0 |
| 105 | + if (x_bits.is_inf()) |
| 106 | + return FPBits::zero().get_val(); |
| 107 | + |
| 108 | + fputil::set_errno_if_required(ERANGE); |
| 109 | + fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT); |
| 110 | + |
| 111 | + if (fputil::fenv_is_round_up()) |
| 112 | + return FPBits::min_subnormal().get_val(); |
| 113 | + return FPBits::zero().get_val(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // When x is 1, 2, 3, or 4. These are hard-to-round cases with exact results. |
| 118 | + if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) { |
| 119 | + switch (x_u) { |
| 120 | + case 0x3c00U: // x = 1.0f16 |
| 121 | + return static_cast<float16>(10.0); |
| 122 | + case 0x4000U: // x = 2.0f16 |
| 123 | + return static_cast<float16>(100.0); |
| 124 | + case 0x4200U: // x = 3.0f16 |
| 125 | + return static_cast<float16>(1'000.0); |
| 126 | + case 0x4400U: // x = 4.0f16 |
| 127 | + return static_cast<float16>(10'000.0); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (auto r = EXP10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
| 132 | + return r.value(); |
| 133 | + |
| 134 | + // For -8 < x < 5, to compute 10^x, we perform the following range reduction: |
| 135 | + // find hi, mid, lo, such that: |
| 136 | + // x = (hi + mid) * log2(10) + lo, in which |
| 137 | + // hi is an integer, |
| 138 | + // mid * 2^3 is an integer, |
| 139 | + // -2^(-4) <= lo < 2^(-4). |
| 140 | + // In particular, |
| 141 | + // hi + mid = round(x * 2^3) * 2^(-3). |
| 142 | + // Then, |
| 143 | + // 10^x = 10^(hi + mid + lo) = 2^((hi + mid) * log2(10)) + 10^lo |
| 144 | + // We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid |
| 145 | + // by adding hi to the exponent field of 2^mid. 10^lo is computed using a |
| 146 | + // degree-4 minimax polynomial generated by Sollya. |
| 147 | + |
| 148 | + float xf = x; |
| 149 | + float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f)); |
| 150 | + int x_hi_mid = static_cast<int>(kf); |
| 151 | + int x_hi = x_hi_mid >> 3; |
| 152 | + int x_mid = x_hi_mid & 0x7; |
| 153 | + // lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x |
| 154 | + float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf); |
| 155 | + |
| 156 | + uint32_t exp2_hi_mid_bits = |
| 157 | + EXP2_MID_BITS[x_mid] + |
| 158 | + static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN); |
| 159 | + float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val(); |
| 160 | + // Degree-4 minimax polynomial generated by Sollya with the following |
| 161 | + // commands: |
| 162 | + // > display = hexadecimal; |
| 163 | + // > P = fpminimax((10^x - 1)/x, 3, [|SG...|], [-2^-4, 2^-4]); |
| 164 | + // > 1 + x * P; |
| 165 | + float exp10_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.26bb14p+1f, 0x1.53526p+1f, |
| 166 | + 0x1.04b434p+1f, 0x1.2bcf9ep+0f); |
| 167 | + return static_cast<float16>(exp2_hi_mid * exp10_lo); |
| 168 | +} |
| 169 | + |
| 170 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments