|
| 1 | +#ifndef NUMPY_CORE_SRC_COMMON_FLOAT_STATUS_HPP |
| 2 | +#define NUMPY_CORE_SRC_COMMON_FLOAT_STATUS_HPP |
| 3 | + |
| 4 | +#include "npstd.hpp" |
| 5 | + |
| 6 | +#include <fenv.h> |
| 7 | + |
| 8 | +namespace np { |
| 9 | + |
| 10 | +/// @addtogroup cpp_core_utility |
| 11 | +/// @{ |
| 12 | +/** |
| 13 | + * Class wraps floating-point environment operations, |
| 14 | + * provides lazy access to its functionality. |
| 15 | + */ |
| 16 | +class FloatStatus { |
| 17 | + public: |
| 18 | +/* |
| 19 | + * According to the C99 standard FE_DIVBYZERO, etc. may not be provided when |
| 20 | + * unsupported. In such cases NumPy will not report these correctly, but we |
| 21 | + * should still allow compiling (whether tests pass or not). |
| 22 | + * By defining them as 0 locally, we make them no-ops. Unlike these defines, |
| 23 | + * for example `musl` still defines all of the functions (as no-ops): |
| 24 | + * https://git.musl-libc.org/cgit/musl/tree/src/fenv/fenv.c |
| 25 | + * and does similar replacement in its tests: |
| 26 | + * http://nsz.repo.hu/git/?p=libc-test;a=blob;f=src/common/mtest.h;h=706c1ba23ea8989b17a2f72ed1a919e187c06b6a;hb=HEAD#l30 |
| 27 | + */ |
| 28 | +#ifdef FE_DIVBYZERO |
| 29 | + static constexpr int kDivideByZero = FE_DIVBYZERO; |
| 30 | +#else |
| 31 | + static constexpr int kDivideByZero = 0; |
| 32 | +#endif |
| 33 | +#ifdef FE_INVALID |
| 34 | + static constexpr int kInvalid = FE_INVALID; |
| 35 | +#else |
| 36 | + static constexpr int kInvalid = 0; |
| 37 | +#endif |
| 38 | +#ifdef FE_INEXACT |
| 39 | + static constexpr int kInexact = FE_INEXACT; |
| 40 | +#else |
| 41 | + static constexpr int kInexact = 0; |
| 42 | +#endif |
| 43 | +#ifdef FE_OVERFLOW |
| 44 | + static constexpr int kOverflow = FE_OVERFLOW; |
| 45 | +#else |
| 46 | + static constexpr int kOverflow = 0; |
| 47 | +#endif |
| 48 | +#ifdef FE_UNDERFLOW |
| 49 | + static constexpr int kUnderflow = FE_UNDERFLOW; |
| 50 | +#else |
| 51 | + static constexpr int kUnderflow = 0; |
| 52 | +#endif |
| 53 | + static constexpr int kAllExcept = (kDivideByZero | kInvalid | kInexact | |
| 54 | + kOverflow | kUnderflow); |
| 55 | + |
| 56 | + FloatStatus(bool clear_on_dst=true) |
| 57 | + : clear_on_dst_(clear_on_dst) |
| 58 | + { |
| 59 | + if constexpr (kAllExcept != 0) { |
| 60 | + fpstatus_ = fetestexcept(kAllExcept); |
| 61 | + } |
| 62 | + else { |
| 63 | + fpstatus_ = 0; |
| 64 | + } |
| 65 | + } |
| 66 | + ~FloatStatus() |
| 67 | + { |
| 68 | + if constexpr (kAllExcept != 0) { |
| 69 | + if (fpstatus_ != 0 && clear_on_dst_) { |
| 70 | + feclearexcept(kAllExcept); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + constexpr bool IsDivideByZero() const |
| 75 | + { |
| 76 | + return (fpstatus_ & kDivideByZero) != 0; |
| 77 | + } |
| 78 | + constexpr bool IsInexact() const |
| 79 | + { |
| 80 | + return (fpstatus_ & kInexact) != 0; |
| 81 | + } |
| 82 | + constexpr bool IsInvalid() const |
| 83 | + { |
| 84 | + return (fpstatus_ & kInvalid) != 0; |
| 85 | + } |
| 86 | + constexpr bool IsOverFlow() const |
| 87 | + { |
| 88 | + return (fpstatus_ & kOverflow) != 0; |
| 89 | + } |
| 90 | + constexpr bool IsUnderFlow() const |
| 91 | + { |
| 92 | + return (fpstatus_ & kUnderflow) != 0; |
| 93 | + } |
| 94 | + static void RaiseDivideByZero() |
| 95 | + { |
| 96 | + if constexpr (kDivideByZero != 0) { |
| 97 | + feraiseexcept(kDivideByZero); |
| 98 | + } |
| 99 | + } |
| 100 | + static void RaiseInexact() |
| 101 | + { |
| 102 | + if constexpr (kInexact != 0) { |
| 103 | + feraiseexcept(kInexact); |
| 104 | + } |
| 105 | + } |
| 106 | + static void RaiseInvalid() |
| 107 | + { |
| 108 | + if constexpr (kInvalid != 0) { |
| 109 | + feraiseexcept(kInvalid); |
| 110 | + } |
| 111 | + } |
| 112 | + static void RaiseOverflow() |
| 113 | + { |
| 114 | + if constexpr (kOverflow != 0) { |
| 115 | + feraiseexcept(kOverflow); |
| 116 | + } |
| 117 | + } |
| 118 | + static void RaiseUnderflow() |
| 119 | + { |
| 120 | + if constexpr (kUnderflow != 0) { |
| 121 | + feraiseexcept(kUnderflow); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private: |
| 126 | + bool clear_on_dst_; |
| 127 | + int fpstatus_; |
| 128 | +}; |
| 129 | + |
| 130 | +/// @} cpp_core_utility |
| 131 | +} // namespace np |
| 132 | + |
| 133 | +#endif // NUMPY_CORE_SRC_COMMON_FLOAT_STATUS_HPP |
| 134 | + |
0 commit comments