xref: /aosp_15_r20/external/llvm-libc/src/math/generic/exp10f16.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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/cast.h"
18 #include "src/__support/FPUtil/except_value_utils.h"
19 #include "src/__support/FPUtil/multiply_add.h"
20 #include "src/__support/FPUtil/nearest_integer.h"
21 #include "src/__support/FPUtil/rounding_mode.h"
22 #include "src/__support/common.h"
23 #include "src/__support/macros/config.h"
24 #include "src/__support/macros/optimization.h"
25 #include "src/__support/macros/properties/cpu_features.h"
26 
27 namespace LIBC_NAMESPACE_DECL {
28 
29 #ifdef LIBC_TARGET_CPU_HAS_FMA
30 static constexpr size_t N_EXP10F16_EXCEPTS = 5;
31 #else
32 static constexpr size_t N_EXP10F16_EXCEPTS = 8;
33 #endif
34 
35 static constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS>
36     EXP10F16_EXCEPTS = {{
37         // x = 0x1.8f4p-2, exp10f16(x) = 0x1.3ap+1 (RZ)
38         {0x363dU, 0x40e8U, 1U, 0U, 1U},
39         // x = 0x1.95cp-2, exp10f16(x) = 0x1.3ecp+1 (RZ)
40         {0x3657U, 0x40fbU, 1U, 0U, 0U},
41         // x = -0x1.018p-4, exp10f16(x) = 0x1.bbp-1 (RZ)
42         {0xac06U, 0x3aecU, 1U, 0U, 0U},
43         // x = -0x1.c28p+0, exp10f16(x) = 0x1.1ccp-6 (RZ)
44         {0xbf0aU, 0x2473U, 1U, 0U, 0U},
45         // x = -0x1.e1cp+1, exp10f16(x) = 0x1.694p-13 (RZ)
46         {0xc387U, 0x09a5U, 1U, 0U, 0U},
47 #ifndef LIBC_TARGET_CPU_HAS_FMA
48         // x = 0x1.0cp+1, exp10f16(x) = 0x1.f04p+6 (RZ)
49         {0x4030U, 0x57c1U, 1U, 0U, 1U},
50         // x = 0x1.1b8p+1, exp10f16(x) = 0x1.47cp+7 (RZ)
51         {0x406eU, 0x591fU, 1U, 0U, 1U},
52         // x = 0x1.1b8p+2, exp10f16(x) = 0x1.a4p+14 (RZ)
53         {0x446eU, 0x7690U, 1U, 0U, 1U},
54 #endif
55     }};
56 
57 LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) {
58   using FPBits = fputil::FPBits<float16>;
59   FPBits x_bits(x);
60 
61   uint16_t x_u = x_bits.uintval();
62   uint16_t x_abs = x_u & 0x7fffU;
63 
64   // When |x| >= 5, or x is NaN.
65   if (LIBC_UNLIKELY(x_abs >= 0x4500U)) {
66     // exp10(NaN) = NaN
67     if (x_bits.is_nan()) {
68       if (x_bits.is_signaling_nan()) {
69         fputil::raise_except_if_required(FE_INVALID);
70         return FPBits::quiet_nan().get_val();
71       }
72 
73       return x;
74     }
75 
76     // When x >= 5.
77     if (x_bits.is_pos()) {
78       // exp10(+inf) = +inf
79       if (x_bits.is_inf())
80         return FPBits::inf().get_val();
81 
82       switch (fputil::quick_get_round()) {
83       case FE_TONEAREST:
84       case FE_UPWARD:
85         fputil::set_errno_if_required(ERANGE);
86         fputil::raise_except_if_required(FE_OVERFLOW);
87         return FPBits::inf().get_val();
88       default:
89         return FPBits::max_normal().get_val();
90       }
91     }
92 
93     // When x <= -8.
94     if (x_u >= 0xc800U) {
95       // exp10(-inf) = +0
96       if (x_bits.is_inf())
97         return FPBits::zero().get_val();
98 
99       fputil::set_errno_if_required(ERANGE);
100       fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
101 
102       if (fputil::fenv_is_round_up())
103         return FPBits::min_subnormal().get_val();
104       return FPBits::zero().get_val();
105     }
106   }
107 
108   // When x is 1, 2, 3, or 4. These are hard-to-round cases with exact results.
109   if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) {
110     switch (x_u) {
111     case 0x3c00U: // x = 1.0f16
112       return fputil::cast<float16>(10.0);
113     case 0x4000U: // x = 2.0f16
114       return fputil::cast<float16>(100.0);
115     case 0x4200U: // x = 3.0f16
116       return fputil::cast<float16>(1'000.0);
117     case 0x4400U: // x = 4.0f16
118       return fputil::cast<float16>(10'000.0);
119     }
120   }
121 
122   if (auto r = EXP10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
123     return r.value();
124 
125   // 10^x = 2^((hi + mid) * log2(10)) * 10^lo
126   auto [exp2_hi_mid, exp10_lo] = exp10_range_reduction(x);
127   return fputil::cast<float16>(exp2_hi_mid * exp10_lo);
128 }
129 
130 } // namespace LIBC_NAMESPACE_DECL
131