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 "exp10f16_utils.h"
17#include "src/__support/FPUtil/FEnvImpl.h"
18#include "src/__support/FPUtil/FPBits.h"
19#include "src/__support/FPUtil/cast.h"
20#include "src/__support/FPUtil/except_value_utils.h"
21#include "src/__support/FPUtil/rounding_mode.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
26namespace LIBC_NAMESPACE_DECL {
27
28namespace math {
29
30#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
31#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
32LIBC_INLINE_VAR constexpr size_t N_EXP10F16_EXCEPTS = 5;
33#else
34LIBC_INLINE_VAR constexpr size_t N_EXP10F16_EXCEPTS = 8;
35#endif
36
37LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS>
38 EXP10F16_EXCEPTS = {.values: {
39 // x = 0x1.8f4p-2, exp10f16(x) = 0x1.3ap+1 (RZ)
40 {.input: 0x363dU, .rnd_towardzero_result: 0x40e8U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
41 // x = 0x1.95cp-2, exp10f16(x) = 0x1.3ecp+1 (RZ)
42 {.input: 0x3657U, .rnd_towardzero_result: 0x40fbU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
43 // x = -0x1.018p-4, exp10f16(x) = 0x1.bbp-1 (RZ)
44 {.input: 0xac06U, .rnd_towardzero_result: 0x3aecU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
45 // x = -0x1.c28p+0, exp10f16(x) = 0x1.1ccp-6 (RZ)
46 {.input: 0xbf0aU, .rnd_towardzero_result: 0x2473U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
47 // x = -0x1.e1cp+1, exp10f16(x) = 0x1.694p-13 (RZ)
48 {.input: 0xc387U, .rnd_towardzero_result: 0x09a5U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
49#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
50 // x = 0x1.0cp+1, exp10f16(x) = 0x1.f04p+6 (RZ)
51 {.input: 0x4030U, .rnd_towardzero_result: 0x57c1U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
52 // x = 0x1.1b8p+1, exp10f16(x) = 0x1.47cp+7 (RZ)
53 {.input: 0x406eU, .rnd_towardzero_result: 0x591fU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
54 // x = 0x1.1b8p+2, exp10f16(x) = 0x1.a4p+14 (RZ)
55 {.input: 0x446eU, .rnd_towardzero_result: 0x7690U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
56#endif
57 }};
58#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
59
60LIBC_INLINE constexpr float16 exp10f16(float16 x) {
61 using FPBits = fputil::FPBits<float16>;
62 FPBits x_bits(x);
63
64 uint16_t x_u = x_bits.uintval();
65 uint16_t x_abs = x_u & 0x7fffU;
66
67 // When |x| >= 5, or x is NaN.
68 if (LIBC_UNLIKELY(x_abs >= 0x4500U)) {
69 // exp10(NaN) = NaN
70 if (x_bits.is_nan()) {
71 if (x_bits.is_signaling_nan()) {
72 fputil::raise_except_if_required(FE_INVALID);
73 return FPBits::quiet_nan().get_val();
74 }
75
76 return x;
77 }
78
79 // When x >= 5.
80 if (x_bits.is_pos()) {
81 // exp10(+inf) = +inf
82 if (x_bits.is_inf())
83 return FPBits::inf().get_val();
84
85#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
86 fputil::set_errno_if_required(ERANGE);
87 fputil::raise_except_if_required(FE_OVERFLOW);
88 return FPBits::inf().get_val();
89#else
90 switch (fputil::quick_get_round()) {
91 case FE_TONEAREST:
92 case FE_UPWARD:
93 fputil::set_errno_if_required(ERANGE);
94 fputil::raise_except_if_required(FE_OVERFLOW);
95 return FPBits::inf().get_val();
96 default:
97 return FPBits::max_normal().get_val();
98 }
99#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
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#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
112 if (fputil::fenv_is_round_up())
113 return FPBits::min_subnormal().get_val();
114#endif
115 return FPBits::zero().get_val();
116 }
117 }
118
119 // When x is 1, 2, 3, or 4. These are hard-to-round cases with exact results.
120 if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) {
121 switch (x_u) {
122 case 0x3c00U: // x = 1.0f16
123 return fputil::cast<float16>(x: 10.0);
124 case 0x4000U: // x = 2.0f16
125 return fputil::cast<float16>(x: 100.0);
126 case 0x4200U: // x = 3.0f16
127 return fputil::cast<float16>(x: 1'000.0);
128 case 0x4400U: // x = 4.0f16
129 return fputil::cast<float16>(x: 10'000.0);
130 }
131 }
132
133#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
134 if (auto r = EXP10F16_EXCEPTS.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value()))
135 return r.value();
136#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
137
138 // 10^x = 2^((hi + mid) * log2(10)) * 10^lo
139 auto [exp2_hi_mid, exp10_lo] = exp10_range_reduction(x);
140 return fputil::cast<float16>(x: exp2_hi_mid * exp10_lo);
141}
142
143} // namespace math
144
145} // namespace LIBC_NAMESPACE_DECL
146
147#endif // LIBC_TYPES_HAS_FLOAT16
148
149#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F16_H
150