| 1 | //===---- Free-standing function to detect rounding mode --------*- 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_ROUNDING_MODE_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_ROUNDING_MODE_H |
| 11 | |
| 12 | #include "hdr/fenv_macros.h" |
| 13 | #include "src/__support/CPP/type_traits.h" // is_constant_evaluated |
| 14 | #include "src/__support/macros/attributes.h" // LIBC_INLINE |
| 15 | #include "src/__support/macros/config.h" |
| 16 | |
| 17 | namespace LIBC_NAMESPACE_DECL { |
| 18 | namespace fputil { |
| 19 | |
| 20 | namespace generic { |
| 21 | |
| 22 | // Quick free-standing test whether fegetround() == FE_UPWARD. |
| 23 | // Using the following observation: |
| 24 | // 1.0f + 2^-25 = 1.0f for FE_TONEAREST, FE_DOWNWARD, FE_TOWARDZERO |
| 25 | // = 0x1.000002f for FE_UPWARD. |
| 26 | LIBC_INLINE bool fenv_is_round_up() { |
| 27 | static volatile float x = 0x1.0p-25f; |
| 28 | return (1.0f + x != 1.0f); |
| 29 | } |
| 30 | |
| 31 | // Quick free-standing test whether fegetround() == FE_DOWNWARD. |
| 32 | // Using the following observation: |
| 33 | // -1.0f - 2^-25 = -1.0f for FE_TONEAREST, FE_UPWARD, FE_TOWARDZERO |
| 34 | // = -0x1.000002f for FE_DOWNWARD. |
| 35 | LIBC_INLINE bool fenv_is_round_down() { |
| 36 | static volatile float x = 0x1.0p-25f; |
| 37 | return (-1.0f - x != -1.0f); |
| 38 | } |
| 39 | |
| 40 | // Quick free-standing test whether fegetround() == FE_TONEAREST. |
| 41 | // Using the following observation: |
| 42 | // 1.5f + 2^-24 = 1.5f for FE_TONEAREST, FE_DOWNWARD, FE_TOWARDZERO |
| 43 | // = 0x1.100002p0f for FE_UPWARD, |
| 44 | // 1.5f - 2^-24 = 1.5f for FE_TONEAREST, FE_UPWARD |
| 45 | // = 0x1.0ffffep-1f for FE_DOWNWARD, FE_TOWARDZERO |
| 46 | LIBC_INLINE bool fenv_is_round_to_nearest() { |
| 47 | static volatile float x = 0x1.0p-24f; |
| 48 | float y = 1.5f + x; |
| 49 | return (y == 1.5f - x); |
| 50 | } |
| 51 | |
| 52 | // Quick free-standing test whether fegetround() == FE_TOWARDZERO. |
| 53 | // Using the following observation: |
| 54 | // 1.0f + 2^-23 + 2^-24 = 0x1.000002p0f for FE_DOWNWARD, FE_TOWARDZERO |
| 55 | // = 0x1.000004p0f for FE_TONEAREST, FE_UPWARD, |
| 56 | // -1.0f - 2^-24 = -1.0f for FE_TONEAREST, FE_UPWARD, FE_TOWARDZERO |
| 57 | // = -0x1.000002p0f for FE_DOWNWARD |
| 58 | // So: |
| 59 | // (0x1.000002p0f + 2^-24) + (-1.0f - 2^-24) = 2^-23 for FE_TOWARDZERO |
| 60 | // = 2^-22 for FE_TONEAREST, FE_UPWARD |
| 61 | // = 0 for FE_DOWNWARD |
| 62 | LIBC_INLINE bool fenv_is_round_to_zero() { |
| 63 | static volatile float x = 0x1.0p-24f; |
| 64 | float y = x; |
| 65 | return ((0x1.000002p0f + y) + (-1.0f - y) == 0x1.0p-23f); |
| 66 | } |
| 67 | |
| 68 | // Quick free standing get rounding mode based on the above observations. |
| 69 | LIBC_INLINE int quick_get_round() { |
| 70 | static volatile float x = 0x1.0p-24f; |
| 71 | float y = x; |
| 72 | float z = (0x1.000002p0f + y) + (-1.0f - y); |
| 73 | |
| 74 | if (z == 0.0f) |
| 75 | return FE_DOWNWARD; |
| 76 | if (z == 0x1.0p-23f) |
| 77 | return FE_TOWARDZERO; |
| 78 | return (2.0f + y == 2.0f) ? FE_TONEAREST : FE_UPWARD; |
| 79 | } |
| 80 | |
| 81 | } // namespace generic |
| 82 | |
| 83 | LIBC_INLINE static constexpr bool fenv_is_round_up() { |
| 84 | if (cpp::is_constant_evaluated()) { |
| 85 | return false; |
| 86 | } else { |
| 87 | return generic::fenv_is_round_up(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | LIBC_INLINE static constexpr bool fenv_is_round_down() { |
| 92 | if (cpp::is_constant_evaluated()) { |
| 93 | return false; |
| 94 | } else { |
| 95 | return generic::fenv_is_round_down(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | LIBC_INLINE static constexpr bool fenv_is_round_to_nearest() { |
| 100 | if (cpp::is_constant_evaluated()) { |
| 101 | return true; |
| 102 | } else { |
| 103 | return generic::fenv_is_round_to_nearest(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | LIBC_INLINE static constexpr bool fenv_is_round_to_zero() { |
| 108 | if (cpp::is_constant_evaluated()) { |
| 109 | return false; |
| 110 | } else { |
| 111 | return generic::fenv_is_round_to_zero(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Quick free standing get rounding mode based on the above observations. |
| 116 | LIBC_INLINE static constexpr int quick_get_round() { |
| 117 | if (cpp::is_constant_evaluated()) { |
| 118 | return FE_TONEAREST; |
| 119 | } else { |
| 120 | return generic::quick_get_round(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } // namespace fputil |
| 125 | } // namespace LIBC_NAMESPACE_DECL |
| 126 | |
| 127 | #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_ROUNDING_MODE_H |
| 128 | |