| 1 | //===-- Check exceptions for exp functions ----------------------*- 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_CHECK_EXP_EXCEPTIONS_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_MATH_CHECK_EXP_EXCEPTIONS_H |
| 11 | |
| 12 | #include "hdr/fenv_macros.h" |
| 13 | #include "src/__support/FPUtil/FPBits.h" |
| 14 | #include "src/__support/common.h" |
| 15 | #include "src/__support/macros/config.h" |
| 16 | |
| 17 | namespace LIBC_NAMESPACE_DECL { |
| 18 | |
| 19 | namespace math { |
| 20 | |
| 21 | namespace check { |
| 22 | |
| 23 | namespace exp_internal { |
| 24 | |
| 25 | template <typename T> struct Bounds; |
| 26 | |
| 27 | template <> struct Bounds<float> { |
| 28 | // Smallest value that will cause overflow, generated from Sollya: |
| 29 | // > float_max = 2^127 * (2 - 2^-23); |
| 30 | // > upper = round(log(float_max), SG, RU); |
| 31 | // > printfloat(upper); |
| 32 | static constexpr float UPPER = 0x1.62e43p6f; |
| 33 | static constexpr uint32_t UPPER_BITS = 0x42b1'7218; |
| 34 | // Largest value that will cause underflow, generated from Sollya: |
| 35 | // > float_min = 2^-126; |
| 36 | // > lower = round(log(float_min), SG, RD); |
| 37 | // > printfloat(lower); |
| 38 | static constexpr float LOWER = -0x1.5d58ap6f; |
| 39 | static constexpr uint32_t LOWER_BITS = 0xc2ae'ac50; |
| 40 | }; |
| 41 | |
| 42 | template <> struct Bounds<double> { |
| 43 | // Smallest value that will cause overflow, generated from Sollya: |
| 44 | // > double_max = 2^1023 * (2 - 2^-52); |
| 45 | // > upper = round(log(double_max), D, RU); |
| 46 | // > printdouble(upper); |
| 47 | static constexpr double UPPER = 0x1.62e42fefa39fp9; |
| 48 | static constexpr uint64_t UPPER_BITS = 0x4086'2e42'fefa'39f0; |
| 49 | // Largest value that will cause underflow, generated from Sollya: |
| 50 | // > double_min = 2^-1022; |
| 51 | // > lower = round(log(double_min), D, RD); |
| 52 | // > printfloat(lower); |
| 53 | static constexpr double LOWER = -0x1.6232bdd7abcd3p9; |
| 54 | static constexpr uint64_t LOWER_BITS = 0xc086'232b'dd7a'bcd3; |
| 55 | }; |
| 56 | |
| 57 | } // namespace exp_internal |
| 58 | |
| 59 | template <typename T> LIBC_INLINE int exp_exceptions(T x, int rounding_mode) { |
| 60 | using FPBits = typename fputil::FPBits<T>; |
| 61 | using StorageType = typename FPBits::StorageType; |
| 62 | |
| 63 | FPBits x_bits(x); |
| 64 | if (x_bits.is_signaling_nan()) |
| 65 | return FE_INVALID; |
| 66 | if (x_bits.is_inf_or_nan() || x_bits.is_zero()) |
| 67 | return 0; |
| 68 | StorageType x_u = x_bits.uintval(); |
| 69 | if (x_u >= exp_internal::Bounds<T>::UPPER_BITS && x_bits.is_pos()) |
| 70 | return (rounding_mode == FE_TONEAREST || rounding_mode == FE_UPWARD) |
| 71 | ? (FE_OVERFLOW | FE_INEXACT) |
| 72 | : FE_INEXACT; |
| 73 | if (x_u >= exp_internal::Bounds<T>::LOWER_BITS) |
| 74 | return FE_UNDERFLOW | FE_INEXACT; |
| 75 | return FE_INEXACT; |
| 76 | } |
| 77 | |
| 78 | } // namespace check |
| 79 | |
| 80 | } // namespace math |
| 81 | |
| 82 | } // namespace LIBC_NAMESPACE_DECL |
| 83 | |
| 84 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_CHECK_EXP_EXCEPTIONS_H |
| 85 | |