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