1//===-- Common header for helpers to set exceptional values -----*- 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_FPUTIL_EXCEPT_VALUE_UTILS_H
10#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_EXCEPT_VALUE_UTILS_H
11
12#include "hdr/errno_macros.h"
13#include "hdr/fenv_macros.h"
14#include "src/__support/CPP/optional.h"
15#include "src/__support/CPP/type_traits.h"
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/rounding_mode.h"
20#include "src/__support/macros/attributes.h"
21#include "src/__support/macros/config.h"
22#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
23#include "src/__support/macros/properties/cpu_features.h"
24#include "src/__support/macros/properties/types.h"
25#include "src/__support/sign.h"
26
27namespace LIBC_NAMESPACE_DECL {
28
29namespace fputil {
30
31// This file contains utility functions and classes to manage exceptional values
32// when there are many of them.
33//
34// Example usage:
35//
36// Define list of exceptional inputs and outputs:
37// static constexpr int N = ...; // Number of exceptional values.
38// static constexpr fputil::ExceptValues<StorageType, N> Excepts {
39// <list of input bits, output bits and offsets>
40// };
41//
42// Check for exceptional inputs:
43// if (auto r = Excepts.lookup(x_bits); LIBC_UNLIKELY(r.has_value()))
44// return r.value();
45
46template <typename T, size_t N> struct ExceptValues {
47 static_assert(cpp::is_floating_point_v<T>, "Must be a floating point type.");
48
49 using StorageType = typename FPBits<T>::StorageType;
50
51 struct Mapping {
52 StorageType input;
53 StorageType rnd_towardzero_result;
54 StorageType rnd_upward_offset;
55 StorageType rnd_downward_offset;
56 StorageType rnd_tonearest_offset;
57 };
58
59 Mapping values[N];
60
61 LIBC_INLINE LIBC_CONSTEXPR_DEFAULT cpp::optional<T>
62 lookup(StorageType x_bits) const {
63 for (size_t i = 0; i < N; ++i) {
64 if (LIBC_UNLIKELY(x_bits == values[i].input)) {
65 StorageType out_bits = values[i].rnd_towardzero_result;
66#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
67 out_bits += values[i].rnd_tonearest_offset;
68#else
69 switch (fputil::quick_get_round()) {
70 case FE_UPWARD:
71 out_bits += values[i].rnd_upward_offset;
72 break;
73 case FE_DOWNWARD:
74 out_bits += values[i].rnd_downward_offset;
75 break;
76 case FE_TONEAREST:
77 out_bits += values[i].rnd_tonearest_offset;
78 break;
79 }
80#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
81 return FPBits<T>(out_bits).get_val();
82 }
83 }
84 return cpp::nullopt;
85 }
86
87 LIBC_INLINE LIBC_CONSTEXPR_DEFAULT cpp::optional<T>
88 lookup_odd(StorageType x_abs, bool sign) const {
89 for (size_t i = 0; i < N; ++i) {
90 if (LIBC_UNLIKELY(x_abs == values[i].input)) {
91 StorageType out_bits = values[i].rnd_towardzero_result;
92#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
93 out_bits += values[i].rnd_tonearest_offset;
94#else
95 switch (fputil::quick_get_round()) {
96 case FE_UPWARD:
97 if (sign)
98 out_bits += values[i].rnd_downward_offset;
99 else
100 out_bits += values[i].rnd_upward_offset;
101 break;
102 case FE_DOWNWARD:
103 // Use conditionals instead of ternary operator to work around gcc's
104 // -Wconversion false positive bug:
105 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101537
106 if (sign)
107 out_bits += values[i].rnd_upward_offset;
108 else
109 out_bits += values[i].rnd_downward_offset;
110 break;
111 case FE_TONEAREST:
112 out_bits += values[i].rnd_tonearest_offset;
113 break;
114 }
115#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
116 T result = FPBits<T>(out_bits).get_val();
117 if (sign)
118 result = -result;
119
120 return result;
121 }
122 }
123 return cpp::nullopt;
124 }
125};
126
127// Helper functions to set results for exceptional cases.
128template <typename T>
129LIBC_INLINE LIBC_CONSTEXPR_DEFAULT T round_result_slightly_down(T value_rn) {
130#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
131 fputil::raise_except_if_required(FE_INEXACT);
132 return value_rn;
133#else
134 if (cpp::is_constant_evaluated()) {
135 fputil::raise_except_if_required(FE_INEXACT);
136 return value_rn;
137 } else {
138 volatile T tmp = value_rn;
139 tmp -= FPBits<T>::min_normal().get_val();
140 fputil::raise_except_if_required(FE_INEXACT);
141 return tmp;
142 }
143#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
144}
145
146template <typename T>
147LIBC_INLINE LIBC_CONSTEXPR_DEFAULT T round_result_slightly_up(T value_rn) {
148#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
149 fputil::raise_except_if_required(FE_INEXACT);
150 return value_rn;
151#else
152 if (cpp::is_constant_evaluated()) {
153 fputil::raise_except_if_required(FE_INEXACT);
154 return value_rn;
155 } else {
156 volatile T tmp = value_rn;
157 tmp += FPBits<T>::min_normal().get_val();
158 fputil::raise_except_if_required(FE_INEXACT);
159 return tmp;
160 }
161#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
162}
163
164#if defined(LIBC_TYPES_HAS_FLOAT16) && \
165 !defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS)
166template <>
167LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16
168round_result_slightly_down(float16 value_rn) {
169#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
170 fputil::raise_except_if_required(FE_INEXACT);
171 return value_rn;
172#else
173 if (cpp::is_constant_evaluated()) {
174 return value_rn;
175 } else {
176 volatile float tmp = value_rn;
177 tmp -= FPBits<float16>::min_normal().get_val();
178 fputil::raise_except_if_required(FE_INEXACT);
179 return cast<float16>(x: tmp);
180 }
181#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
182}
183
184template <>
185LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16
186round_result_slightly_up(float16 value_rn) {
187#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
188 fputil::raise_except_if_required(FE_INEXACT);
189 return value_rn;
190#else
191 if (cpp::is_constant_evaluated()) {
192 return value_rn;
193 } else {
194 volatile float tmp = value_rn;
195 tmp += FPBits<float16>::min_normal().get_val();
196 fputil::raise_except_if_required(FE_INEXACT);
197 return cast<float16>(x: tmp);
198 }
199#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
200}
201#endif
202
203// Set errno = ERANGE, raise FE_OVERFLOW | FE_INEXACT if required, and return
204// the rounded overflow result (+/-inf or +/-max_normal) for the active
205// rounding mode.
206template <typename T>
207LIBC_INLINE LIBC_CONSTEXPR_DEFAULT T set_overflow(Sign sign = Sign::POS) {
208 static_assert(cpp::is_floating_point_v<T>, "Must be a floating point type.");
209 if (cpp::is_constant_evaluated())
210 return FPBits<T>::inf(sign).get_val();
211
212 fputil::set_errno_if_required(ERANGE);
213#if defined(LIBC_MATH_HAS_NO_EXCEPT) && \
214 defined(LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY)
215 return FPBits<T>::inf(sign).get_val();
216#else
217 if constexpr (internal::HAS_HW_FPU<T>) {
218 return internal::OVERFLOW_FACTOR<T> *
219 internal::OVERFLOW_VAL<T>[sign.is_neg()];
220 } else {
221 fputil::raise_overflow_except_if_required<T>();
222#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
223 int rounding = fputil::quick_get_round();
224 if (rounding == FE_TOWARDZERO ||
225 (rounding == FE_DOWNWARD && sign.is_pos()) ||
226 (rounding == FE_UPWARD && sign.is_neg()))
227 return FPBits<T>::max_normal(sign).get_val();
228#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
229 return FPBits<T>::inf(sign).get_val();
230 }
231#endif
232}
233
234// Set errno = ERANGE, raise FE_UNDERFLOW | FE_INEXACT if required, and return
235// the rounded underflow result (+/-0.0 or +/-min_subnormal) for the active
236// rounding mode.
237template <typename T>
238LIBC_INLINE LIBC_CONSTEXPR_DEFAULT T set_underflow(Sign sign = Sign::POS) {
239 static_assert(cpp::is_floating_point_v<T>, "Must be a floating point type.");
240 if (cpp::is_constant_evaluated())
241 return FPBits<T>::zero(sign).get_val();
242
243 fputil::set_errno_if_required(ERANGE);
244#if defined(LIBC_MATH_HAS_NO_EXCEPT) && \
245 defined(LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY)
246 return FPBits<T>::zero(sign).get_val();
247#else
248 if constexpr (internal::HAS_HW_FPU<T>) {
249 return internal::UNDERFLOW_FACTOR<T> *
250 internal::UNDERFLOW_VAL<T>[sign.is_neg()];
251 } else {
252 fputil::raise_underflow_except_if_required<T>();
253#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
254 int rounding = fputil::quick_get_round();
255 if ((rounding == FE_UPWARD && sign.is_pos()) ||
256 (rounding == FE_DOWNWARD && sign.is_neg()))
257 return FPBits<T>::min_subnormal(sign).get_val();
258#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
259 return FPBits<T>::zero(sign).get_val();
260 }
261#endif
262}
263
264} // namespace fputil
265
266} // namespace LIBC_NAMESPACE_DECL
267
268#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_EXCEPT_VALUE_UTILS_H
269